From 9ab9562994dac25758ecef36e3ecf554120b3647 Mon Sep 17 00:00:00 2001 From: fgorski Date: Mon, 20 Jul 2015 12:36:03 -0700 Subject: [PATCH] [Offline pages] Adding OfflinePageModelFactory BUG=491352 Review URL: https://codereview.chromium.org/1231013003 Cr-Commit-Position: refs/heads/master@{#339485} --- .../offline_page_model_factory.cc | 70 +++++++++++++++++++ .../offline_page_model_factory.h | 42 +++++++++++ chrome/chrome_browser.gypi | 2 + chrome/common/chrome_constants.cc | 2 + chrome/common/chrome_constants.h | 1 + chrome/common/chrome_paths.cc | 7 ++ chrome/common/chrome_paths.h | 4 ++ .../offline_pages/offline_page_model.cc | 5 +- components/offline_pages/offline_page_model.h | 4 +- 9 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 chrome/browser/android/offline_pages/offline_page_model_factory.cc create mode 100644 chrome/browser/android/offline_pages/offline_page_model_factory.h diff --git a/chrome/browser/android/offline_pages/offline_page_model_factory.cc b/chrome/browser/android/offline_pages/offline_page_model_factory.cc new file mode 100644 index 00000000000000..20b19a1c34a09c --- /dev/null +++ b/chrome/browser/android/offline_pages/offline_page_model_factory.cc @@ -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::get(); +} + +// static +OfflinePageModel* OfflinePageModelFactory::GetForBrowserContext( + content::BrowserContext* context) { + if (context->IsOffTheRecord()) + return nullptr; + + return static_cast( + GetInstance()->GetServiceForBrowserContext(context, true)); +} + +KeyedService* OfflinePageModelFactory::BuildServiceInstanceFor( + content::BrowserContext* context) const { + DCHECK(!context->IsOffTheRecord()); + + scoped_refptr background_task_runner = + content::BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( + content::BrowserThread::GetBlockingPool()->GetSequenceToken()); + + scoped_ptr> database( + new leveldb_proto::ProtoDatabaseImpl( + background_task_runner)); + + base::FilePath store_path; + CHECK(PathService::Get(chrome::DIR_OFFLINE_PAGE_METADATA, &store_path)); + scoped_ptr 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 + diff --git a/chrome/browser/android/offline_pages/offline_page_model_factory.h b/chrome/browser/android/offline_pages/offline_page_model_factory.h new file mode 100644 index 00000000000000..d92e3665951ac3 --- /dev/null +++ b/chrome/browser/android/offline_pages/offline_page_model_factory.h @@ -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 +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() 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_ diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 3517cdd447d67b..cc96bf147f662d 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -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', ], diff --git a/chrome/common/chrome_constants.cc b/chrome/common/chrome_constants.cc index bab2b24b06a24b..f8cf204056f9d1 100644 --- a/chrome/common/chrome_constants.cc +++ b/chrome/common/chrome_constants.cc @@ -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"); diff --git a/chrome/common/chrome_constants.h b/chrome/common/chrome_constants.h index 156fbd8d89db81..d83a042f680e2a 100644 --- a/chrome/common/chrome_constants.h +++ b/chrome/common/chrome_constants.h @@ -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[]; diff --git a/chrome/common/chrome_paths.cc b/chrome/common/chrome_paths.cc index ea52e2c5ac83b9..b48092b758a9cf 100644 --- a/chrome/common/chrome_paths.cc +++ b/chrome/common/chrome_paths.cc @@ -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; diff --git a/chrome/common/chrome_paths.h b/chrome/common/chrome_paths.h index 4244af607a38d1..b7021dfc2ddd21 100644 --- a/chrome/common/chrome_paths.h +++ b/chrome/common/chrome_paths.h @@ -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. diff --git a/components/offline_pages/offline_page_model.cc b/components/offline_pages/offline_page_model.cc index 564e71b5f7a726..09633fba864e24 100644 --- a/components/offline_pages/offline_page_model.cc +++ b/components/offline_pages/offline_page_model.cc @@ -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" @@ -50,7 +51,7 @@ SavePageResult ToSavePageResult(ArchiverResult archiver_result) { OfflinePageModel::OfflinePageModel( scoped_ptr store, - const scoped_refptr& task_runner) + const scoped_refptr& task_runner) : store_(store.Pass()), task_runner_(task_runner), weak_ptr_factory_(this) { @@ -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); } diff --git a/components/offline_pages/offline_page_model.h b/components/offline_pages/offline_page_model.h index 93ee5ebf58c3b5..f24d8f89d77fa9 100644 --- a/components/offline_pages/offline_page_model.h +++ b/components/offline_pages/offline_page_model.h @@ -84,7 +84,7 @@ class OfflinePageModel : public KeyedService { // All blocking calls/disk access will happen on the provided |task_runner|. OfflinePageModel( scoped_ptr store, - const scoped_refptr& task_runner); + const scoped_refptr& task_runner); ~OfflinePageModel() override; // KeyedService implementation. @@ -146,7 +146,7 @@ class OfflinePageModel : public KeyedService { // Persistent store for offline page metadata. scoped_ptr store_; - scoped_refptr task_runner_; + scoped_refptr task_runner_; // Pending archivers owned by this model. PendingArchivers pending_archivers_;