diff options
author | fgorski <fgorski@chromium.org> | 2015-06-16 08:36:25 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-16 15:36:54 +0000 |
commit | d1c862afcb67b7079104536e09da669d1ec92290 (patch) | |
tree | c7cde96cdb9af4db554ffbaf276887c75c4e2425 /components/offline_pages | |
parent | 4b01cac89121b3bf9d4b14154a6fc813d1ca4e00 (diff) | |
download | chromium_src-d1c862afcb67b7079104536e09da669d1ec92290.zip chromium_src-d1c862afcb67b7079104536e09da669d1ec92290.tar.gz chromium_src-d1c862afcb67b7079104536e09da669d1ec92290.tar.bz2 |
[Offline] Adding archiver interface
BUG=491352
Review URL: https://codereview.chromium.org/1174803002
Cr-Commit-Position: refs/heads/master@{#334608}
Diffstat (limited to 'components/offline_pages')
-rw-r--r-- | components/offline_pages/BUILD.gn | 1 | ||||
-rw-r--r-- | components/offline_pages/offline_page_archiver.h | 90 | ||||
-rw-r--r-- | components/offline_pages/offline_page_model.cc | 17 | ||||
-rw-r--r-- | components/offline_pages/offline_page_model.h | 17 | ||||
-rw-r--r-- | components/offline_pages/offline_page_model_unittest.cc | 43 |
5 files changed, 161 insertions, 7 deletions
diff --git a/components/offline_pages/BUILD.gn b/components/offline_pages/BUILD.gn index e4de8cc..36cee85 100644 --- a/components/offline_pages/BUILD.gn +++ b/components/offline_pages/BUILD.gn @@ -5,6 +5,7 @@ # GYP: //components/offline_pages.gypi:offline_pages static_library("offline_pages") { sources = [ + "offline_page_archiver.h", "offline_page_item.cc", "offline_page_item.h", "offline_page_metadata_store.cc", diff --git a/components/offline_pages/offline_page_archiver.h b/components/offline_pages/offline_page_archiver.h new file mode 100644 index 0000000..8cc0566 --- /dev/null +++ b/components/offline_pages/offline_page_archiver.h @@ -0,0 +1,90 @@ +// 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 COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_ARCHIVER_H_ +#define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_ARCHIVER_H_ + +#include "base/files/file_path.h" +#include "url/gurl.h" + +namespace offline_pages { + +// Interface of a class responsible for creation of the archive for offline use. +// +// Archiver will be implemented by embedder and may have additional methods that +// are not interesting from the perspective of OfflinePageModel. Example of such +// extra information or capability is a way to enumerate available WebContents +// to find the one that needs to be used to create archive (or to map it to the +// URL passed in CreateArchive in some other way). +// +// Archiver will be responsible for naming the file that is being saved (it has +// URL, title and the whole page content at its disposal). For that it should be +// also configured with the path where the archives are stored. +// +// Archiver should be able to archive multiple pages in parallel, as these are +// asynchronous calls carried out by some other component. +// +// If archiver gets two consecutive requests to archive the same page (may be +// run in parallel) it can generate 2 different names for files and save the +// same page separately, as if these were 2 completely unrelated pages. It is up +// to the caller (e.g. OfflinePageModel) to make sure that situation like that +// does not happen. +// +// If the page is not completely loaded, it is up to the implementation of the +// archiver whether to respond with ERROR_CONTENT_UNAVAILBLE, wait longer to +// actually snapshot a complete page, or snapshot whatever is available at that +// point in time (what the user sees). +// +// TODO(fgorski): Add ability to delete archive. +// TODO(fgorski): Add ability to check that archive exists. +// TODO(fgorski): Add ability to refresh an existing archive in one step. +// TODO(fgorski): Add ability to identify all of the archives in the directory, +// to enable to model to reconcile the archives. +class OfflinePageArchiver { + public: + // Represents an in progress request to archive a page. + class Request { + public: + virtual ~Request() {} + + // Cancels an in progress request to archive a page. + virtual void Cancel() = 0; + virtual const GURL& url() const = 0; + }; + + // Errors that will be reported when archive creation fails. + enum ArchiverResult { + SUCCESSFULLY_CREATED, // Archive created successfully. + ERROR_UNKNOWN, // Don't know what went wrong. + ERROR_DEVICE_FULL, // Cannot save the archive - device is full. + ERROR_CANCELLED, // Caller cancelled the request. + ERROR_CONTENT_UNAVAILABLE, // Content to archive is not available. + }; + + // Interface of the clients that requests to archive pages. + class Client { + public: + virtual ~Client() {} + + // Callback called by the archiver when archiver creation is complete. + // |result| will indicate SUCCESSFULLY_CREATED upon success, or a specific + // error, when it failed. + virtual void OnCreateArchiveDone(Request* request, + ArchiverResult result, + const base::FilePath& file_path) = 0; + }; + + virtual ~OfflinePageArchiver() {} + + // Starts creating the archive. Will pass result by calling methods on the + // passed in client. Caller owns the returned request object. + // If request is deleted during the archiving, the callback will not be + // invoked. The archive might however be created. + virtual scoped_ptr<Request> CreateArchive(const GURL& url, + Client* client) = 0; +}; + +} // namespace offline_pages + +#endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_ARCHIVER_H_ diff --git a/components/offline_pages/offline_page_model.cc b/components/offline_pages/offline_page_model.cc index 5fd54d0..f357d7e3 100644 --- a/components/offline_pages/offline_page_model.cc +++ b/components/offline_pages/offline_page_model.cc @@ -11,18 +11,29 @@ namespace offline_pages { -OfflinePageModel::OfflinePageModel(scoped_ptr<OfflinePageMetadataStore> store) - : store_(store.Pass()) { +OfflinePageModel::OfflinePageModel(scoped_ptr<OfflinePageMetadataStore> store, + OfflinePageArchiver* archiver) + : store_(store.Pass()), + archiver_(archiver) { + DCHECK(archiver); } OfflinePageModel::~OfflinePageModel() { } void OfflinePageModel::Shutdown() { - NOTIMPLEMENTED(); } void OfflinePageModel::SavePageOffline(const GURL& url) { +} + +void OfflinePageModel::OnCreateArchiveDone( + OfflinePageArchiver::Request* request, + OfflinePageArchiver::ArchiverResult result, + const base::FilePath& file_path) { + // TODO(fgorski): Match request against one of the expected requests + // TODO(fgorski): Create an entry in the offline pages metadata store for that + // request. NOTIMPLEMENTED(); } diff --git a/components/offline_pages/offline_page_model.h b/components/offline_pages/offline_page_model.h index d32ace1..44eaf37 100644 --- a/components/offline_pages/offline_page_model.h +++ b/components/offline_pages/offline_page_model.h @@ -10,6 +10,7 @@ #include "base/macros.h" #include "base/memory/scoped_ptr.h" #include "components/keyed_service/core/keyed_service.h" +#include "components/offline_pages/offline_page_archiver.h" class GURL; @@ -20,14 +21,21 @@ class OfflinePageMetadataStore; // Serivce for saving pages offline, storing the offline copy and metadata, and // retrieving them upon request. -class OfflinePageModel : public KeyedService { +class OfflinePageModel : public KeyedService, + public OfflinePageArchiver::Client { public: - explicit OfflinePageModel(scoped_ptr<OfflinePageMetadataStore> store); + OfflinePageModel(scoped_ptr<OfflinePageMetadataStore> store, + OfflinePageArchiver* archiver); ~OfflinePageModel() override; - // KeyedService: + // KeyedService implementation. void Shutdown() override; + // OfflinePageArchiver::Client implementation. + void OnCreateArchiveDone(OfflinePageArchiver::Request* request, + OfflinePageArchiver::ArchiverResult error, + const base::FilePath& file_path) override; + // Saves the page loaded in the web contents offline. void SavePageOffline(const GURL& url); @@ -41,6 +49,9 @@ class OfflinePageModel : public KeyedService { // Persistent store for offline page metadata. scoped_ptr<OfflinePageMetadataStore> store_; + // Offline page archiver. Outlives the model. Owned by the embedder. + OfflinePageArchiver* archiver_; + DISALLOW_COPY_AND_ASSIGN(OfflinePageModel); }; diff --git a/components/offline_pages/offline_page_model_unittest.cc b/components/offline_pages/offline_page_model_unittest.cc index b6119ef..d153190 100644 --- a/components/offline_pages/offline_page_model_unittest.cc +++ b/components/offline_pages/offline_page_model_unittest.cc @@ -38,12 +38,53 @@ void OfflinePageTestStore::RemoveOfflinePage(const GURL& page_url, const UpdateCallback& callback) { } +class OfflinePageTestArchiverRequest : public OfflinePageArchiver::Request { + public: + explicit OfflinePageTestArchiverRequest(const GURL& url) : url_(url) {} + ~OfflinePageTestArchiverRequest() override {} + + void Cancel() override; + const GURL& url() const override { return url_; } + + private: + GURL url_; +}; + +void OfflinePageTestArchiverRequest::Cancel() { +} + + +class OfflinePageTestArchiver : public OfflinePageArchiver { + public: + ~OfflinePageTestArchiver() override; + + // OfflinePageArchiver implementation: + scoped_ptr<Request> CreateArchive(const GURL& url, + Client* client) override; +}; + +OfflinePageTestArchiver::~OfflinePageTestArchiver() { +} + +scoped_ptr<OfflinePageArchiver::Request> OfflinePageTestArchiver::CreateArchive( + const GURL& url, + Client* client) { + scoped_ptr<OfflinePageTestArchiverRequest> request( + new OfflinePageTestArchiverRequest(url)); + return request.Pass(); +} + class OfflinePageModelTest : public testing::Test { public: OfflinePageModelTest(); ~OfflinePageModelTest() override; scoped_ptr<OfflinePageMetadataStore> BuildStore(); + + OfflinePageTestArchiver* archiver() { return &archiver_; } + + private: + OfflinePageTestArchiver archiver_; }; OfflinePageModelTest::OfflinePageModelTest() { @@ -59,7 +100,7 @@ scoped_ptr<OfflinePageMetadataStore> OfflinePageModelTest::BuildStore() { TEST_F(OfflinePageModelTest, Initialize) { scoped_ptr<OfflinePageMetadataStore> store = BuildStore(); OfflinePageMetadataStore* store_ptr = store.get(); - OfflinePageModel model(store.Pass()); + OfflinePageModel model(store.Pass(), archiver()); EXPECT_EQ(store_ptr, model.GetStoreForTesting()); } |