| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_OFFLINER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_OFFLINER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_OFFLINER_H_ | 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_OFFLINER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "chrome/browser/android/offline_pages/prerendering_loader.h" | 11 #include "chrome/browser/android/offline_pages/prerendering_loader.h" |
| 12 #include "components/offline_pages/background/offliner.h" | 12 #include "components/offline_pages/background/offliner.h" |
| 13 #include "components/offline_pages/offline_page_model.h" | 13 #include "components/offline_pages/offline_page_model.h" |
| 14 #include "components/offline_pages/offline_page_types.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 class BrowserContext; | 17 class BrowserContext; |
| 17 class WebContents; | 18 class WebContents; |
| 18 } // namespace content | 19 } // namespace content |
| 19 | 20 |
| 20 namespace offline_pages { | 21 namespace offline_pages { |
| 21 | 22 |
| 22 class OfflinerPolicy; | 23 class OfflinerPolicy; |
| 23 | 24 |
| 24 // An Offliner implementation that attempts client-side rendering and saving | 25 // An Offliner implementation that attempts client-side rendering and saving |
| 25 // of an offline page. It uses the PrerenderingLoader to load the page and | 26 // of an offline page. It uses the PrerenderingLoader to load the page and |
| 26 // the OfflinePageModel to save it. | 27 // the OfflinePageModel to save it. Only one request may be active at a time. |
| 27 class PrerenderingOffliner : public Offliner { | 28 class PrerenderingOffliner : public Offliner { |
| 28 public: | 29 public: |
| 29 PrerenderingOffliner(content::BrowserContext* browser_context, | 30 PrerenderingOffliner(content::BrowserContext* browser_context, |
| 30 const OfflinerPolicy* policy, | 31 const OfflinerPolicy* policy, |
| 31 OfflinePageModel* offline_page_model); | 32 OfflinePageModel* offline_page_model); |
| 32 ~PrerenderingOffliner() override; | 33 ~PrerenderingOffliner() override; |
| 33 | 34 |
| 34 // Offliner implementation. | 35 // Offliner implementation. |
| 35 bool LoadAndSave(const SavePageRequest& request, | 36 bool LoadAndSave(const SavePageRequest& request, |
| 36 const CompletionCallback& callback) override; | 37 const CompletionCallback& callback) override; |
| 37 void Cancel() override; | 38 void Cancel() override; |
| 38 | 39 |
| 39 // Allows a loader to be injected for testing. This may only be done once | 40 // Allows a loader to be injected for testing. This may only be done once |
| 40 // and must be called before any of the Offliner interface methods are called. | 41 // and must be called before any of the Offliner interface methods are called. |
| 41 void SetLoaderForTesting(std::unique_ptr<PrerenderingLoader> loader); | 42 void SetLoaderForTesting(std::unique_ptr<PrerenderingLoader> loader); |
| 42 | 43 |
| 44 protected: |
| 45 // Internal method for OfflinePageModel's check if url can be saved. |
| 46 // Exposed for unit testing. |
| 47 // TODO(dougarnett): Consider making OfflinePageModel mockable instead. |
| 48 virtual bool CanSavePage(const GURL& url); |
| 49 |
| 50 // Internal method for requesting OfflinePageModel to save page. |
| 51 // Exposed for unit testing. |
| 52 // TODO(dougarnett): Consider making OfflinePageModel mockable instead. |
| 53 virtual void SavePage(const GURL& url, |
| 54 const ClientId& client_id, |
| 55 std::unique_ptr<OfflinePageArchiver> archiver, |
| 56 const SavePageCallback& callback); |
| 57 |
| 43 private: | 58 private: |
| 44 void OnLoadPageDone(const Offliner::RequestStatus load_status, | 59 // Callback logic for PrerenderingLoader::LoadPage(). |
| 45 content::WebContents* contents); | 60 void OnLoadPageDone(const SavePageRequest& request, |
| 61 const CompletionCallback& completion_callback, |
| 62 Offliner::RequestStatus load_status, |
| 63 content::WebContents* web_contents); |
| 64 |
| 65 // Callback logic for OfflinePageModel::SavePage(). |
| 66 void OnSavePageDone(const SavePageRequest& request, |
| 67 const CompletionCallback& completion_callback, |
| 68 SavePageResult save_result, |
| 69 int64_t offline_id); |
| 46 | 70 |
| 47 PrerenderingLoader* GetOrCreateLoader(); | 71 PrerenderingLoader* GetOrCreateLoader(); |
| 72 void SetPendingRequest(int64_t request_id); |
| 73 void ClearPendingRequest(); |
| 48 | 74 |
| 49 // Not owned. | 75 // Not owned. |
| 50 content::BrowserContext* browser_context_; | 76 content::BrowserContext* browser_context_; |
| 51 // Not owned. | 77 // Not owned. |
| 52 OfflinePageModel* offline_page_model_; | 78 OfflinePageModel* offline_page_model_; |
| 53 // Lazily created. | 79 // Lazily created. |
| 54 std::unique_ptr<PrerenderingLoader> loader_; | 80 std::unique_ptr<PrerenderingLoader> loader_; |
| 81 // Tracks pending request, if any. Not owned. |
| 82 // May be used to ensure a callback applies to the pending request (e.g., in |
| 83 // case we receive a save page callback for an old, canceled request). |
| 84 SavePageRequest const* pending_request_; |
| 55 base::WeakPtrFactory<PrerenderingOffliner> weak_ptr_factory_; | 85 base::WeakPtrFactory<PrerenderingOffliner> weak_ptr_factory_; |
| 56 | 86 |
| 57 DISALLOW_COPY_AND_ASSIGN(PrerenderingOffliner); | 87 DISALLOW_COPY_AND_ASSIGN(PrerenderingOffliner); |
| 58 }; | 88 }; |
| 59 | 89 |
| 60 } // namespace offline_pages | 90 } // namespace offline_pages |
| 61 | 91 |
| 62 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_OFFLINER_H_ | 92 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_OFFLINER_H_ |
| OLD | NEW |