Skip to content

Commit

Permalink
[Offline pages] Adding OfflinePageModelFactory
Browse files Browse the repository at this point in the history
BUG=491352

Review URL: https://codereview.chromium.org/1231013003

Cr-Commit-Position: refs/heads/master@{#339485}
  • Loading branch information
fgorski authored and Commit bot committed Jul 20, 2015
1 parent d1675a0 commit 9ab9562
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 4 deletions.
70 changes: 70 additions & 0 deletions chrome/browser/android/offline_pages/offline_page_model_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/android/offline_pages/offline_page_model_factory.h"

#include "base/files/file_path.h"
#include "base/memory/singleton.h"
#include "base/path_service.h"
#include "base/sequenced_task_runner.h"
#include "chrome/browser/offline_pages/offline_page_metadata_store_impl.h"
#include "chrome/browser/offline_pages/offline_pages.pb.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_paths.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/leveldb_proto/proto_database_impl.h"
#include "components/offline_pages/offline_page_model.h"
#include "content/public/browser/browser_thread.h"

namespace offline_pages {

OfflinePageModelFactory::OfflinePageModelFactory()
: BrowserContextKeyedServiceFactory(
"OfflinePageModel",
BrowserContextDependencyManager::GetInstance()) {
}

// static
OfflinePageModelFactory* OfflinePageModelFactory::GetInstance() {
return Singleton<OfflinePageModelFactory>::get();
}

// static
OfflinePageModel* OfflinePageModelFactory::GetForBrowserContext(
content::BrowserContext* context) {
if (context->IsOffTheRecord())
return nullptr;

return static_cast<OfflinePageModel*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}

KeyedService* OfflinePageModelFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
DCHECK(!context->IsOffTheRecord());

scoped_refptr<base::SequencedTaskRunner> background_task_runner =
content::BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
content::BrowserThread::GetBlockingPool()->GetSequenceToken());

scoped_ptr<leveldb_proto::ProtoDatabaseImpl<OfflinePageEntry>> database(
new leveldb_proto::ProtoDatabaseImpl<OfflinePageEntry>(
background_task_runner));

base::FilePath store_path;
CHECK(PathService::Get(chrome::DIR_OFFLINE_PAGE_METADATA, &store_path));
scoped_ptr<OfflinePageMetadataStoreImpl> metadata_store(
new OfflinePageMetadataStoreImpl(database.Pass(), store_path));

return new OfflinePageModel(metadata_store.Pass(), background_task_runner);
}

content::BrowserContext* OfflinePageModelFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}

} // namespace offline_pages

42 changes: 42 additions & 0 deletions chrome/browser/android/offline_pages/offline_page_model_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_FACTORY_H_
#define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_FACTORY_H_

#include "base/macros.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"

template <typename T>
struct DefaultSingletonTraits;

namespace offline_pages {

class OfflinePageModel;

// A factory to create one unique OfflinePageModel.
class OfflinePageModelFactory : public BrowserContextKeyedServiceFactory {
public:
static OfflinePageModelFactory* GetInstance();
static OfflinePageModel* GetForBrowserContext(
content::BrowserContext* context);

private:
friend struct DefaultSingletonTraits<OfflinePageModelFactory>;

OfflinePageModelFactory();
~OfflinePageModelFactory() override {}

KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;

content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;

DISALLOW_COPY_AND_ASSIGN(OfflinePageModelFactory);
};

} // namespace offline_pages

#endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_FACTORY_H_
2 changes: 2 additions & 0 deletions chrome/chrome_browser.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,8 @@
'browser/android/offline_pages/offline_page_bridge.h',
'browser/android/offline_pages/offline_page_mhtml_archiver.cc',
'browser/android/offline_pages/offline_page_mhtml_archiver.h',
'browser/android/offline_pages/offline_page_model_factory.cc',
'browser/android/offline_pages/offline_page_model_factory.h',
'browser/enhanced_bookmarks/android/enhanced_bookmarks_bridge.cc',
'browser/enhanced_bookmarks/android/enhanced_bookmarks_bridge.h',
],
Expand Down
2 changes: 2 additions & 0 deletions chrome/common/chrome_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ const base::FilePath::CharType kLoginDataFileName[] = FPL("Login Data");
const base::FilePath::CharType kMediaCacheDirname[] = FPL("Media Cache");
const base::FilePath::CharType kNetworkPersistentStateFilename[] =
FPL("Network Persistent State");
const base::FilePath::CharType kOfflinePageMetadataDirname[] =
FPL("Offline Pages");
const base::FilePath::CharType kPreferencesFilename[] = FPL("Preferences");
const base::FilePath::CharType kProtectedPreferencesFilenameDeprecated[] =
FPL("Protected Preferences");
Expand Down
1 change: 1 addition & 0 deletions chrome/common/chrome_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ extern const base::FilePath::CharType kLocalStorePoolName[];
extern const base::FilePath::CharType kLoginDataFileName[];
extern const base::FilePath::CharType kMediaCacheDirname[];
extern const base::FilePath::CharType kNetworkPersistentStateFilename[];
extern const base::FilePath::CharType kOfflinePageMetadataDirname[];
extern const base::FilePath::CharType kPreferencesFilename[];
extern const base::FilePath::CharType kProtectedPreferencesFilenameDeprecated[];
extern const base::FilePath::CharType kReadmeFilename[];
Expand Down
7 changes: 7 additions & 0 deletions chrome/common/chrome_paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ bool PathProvider(int key, base::FilePath* result) {
cur = cur.Append(kGCMStoreDirname);
break;
#endif // !defined(OS_ANDROID)
#if defined(OS_ANDROID)
case chrome::DIR_OFFLINE_PAGE_METADATA:
if (!PathService::Get(chrome::DIR_USER_DATA, &cur))
return false;
cur = cur.Append(kOfflinePageMetadataDirname);
break;
#endif // defined(OS_ANDROID)

default:
return false;
Expand Down
4 changes: 4 additions & 0 deletions chrome/common/chrome_paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ enum {
DIR_GLOBAL_GCM_STORE, // Directory where the global GCM instance
// stores its data.
#endif
#if defined(OS_ANDROID)
DIR_OFFLINE_PAGE_METADATA, // Directory where offline page metadata is
// stored.
#endif

// Valid only in development environment; TODO(darin): move these
DIR_GEN_TEST_DATA, // Directory where generated test data resides.
Expand Down
5 changes: 3 additions & 2 deletions components/offline_pages/offline_page_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/sequenced_task_runner.h"
#include "components/offline_pages/offline_page_item.h"
#include "components/offline_pages/offline_page_metadata_store.h"
#include "url/gurl.h"
Expand Down Expand Up @@ -50,7 +51,7 @@ SavePageResult ToSavePageResult(ArchiverResult archiver_result) {

OfflinePageModel::OfflinePageModel(
scoped_ptr<OfflinePageMetadataStore> store,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
: store_(store.Pass()),
task_runner_(task_runner),
weak_ptr_factory_(this) {
Expand Down Expand Up @@ -187,7 +188,7 @@ void OfflinePageModel::OnLoadDoneForDeletion(
void OfflinePageModel::DeleteArchiverFile(const base::FilePath& file_path,
bool* success) {
DCHECK(success);
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(task_runner_->RunsTasksOnCurrentThread());

*success = base::DeleteFile(file_path, false);
}
Expand Down
4 changes: 2 additions & 2 deletions components/offline_pages/offline_page_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class OfflinePageModel : public KeyedService {
// All blocking calls/disk access will happen on the provided |task_runner|.
OfflinePageModel(
scoped_ptr<OfflinePageMetadataStore> store,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
const scoped_refptr<base::SequencedTaskRunner>& task_runner);
~OfflinePageModel() override;

// KeyedService implementation.
Expand Down Expand Up @@ -146,7 +146,7 @@ class OfflinePageModel : public KeyedService {
// Persistent store for offline page metadata.
scoped_ptr<OfflinePageMetadataStore> store_;

scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;

// Pending archivers owned by this model.
PendingArchivers pending_archivers_;
Expand Down

0 comments on commit 9ab9562

Please sign in to comment.