| 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/android/application_status_listener.h" |
| 10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 11 #include "chrome/browser/android/offline_pages/prerendering_loader.h" | 12 #include "chrome/browser/android/offline_pages/prerendering_loader.h" |
| 12 #include "components/offline_pages/background/offliner.h" | 13 #include "components/offline_pages/background/offliner.h" |
| 13 #include "components/offline_pages/offline_page_model.h" | 14 #include "components/offline_pages/offline_page_model.h" |
| 14 #include "components/offline_pages/offline_page_types.h" | 15 #include "components/offline_pages/offline_page_types.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 class BrowserContext; | 18 class BrowserContext; |
| 18 class WebContents; | 19 class WebContents; |
| 19 } // namespace content | 20 } // namespace content |
| (...skipping 14 matching lines...) Expand all Loading... |
| 34 | 35 |
| 35 // Offliner implementation. | 36 // Offliner implementation. |
| 36 bool LoadAndSave(const SavePageRequest& request, | 37 bool LoadAndSave(const SavePageRequest& request, |
| 37 const CompletionCallback& callback) override; | 38 const CompletionCallback& callback) override; |
| 38 void Cancel() override; | 39 void Cancel() override; |
| 39 | 40 |
| 40 // Allows a loader to be injected for testing. This may only be done once | 41 // Allows a loader to be injected for testing. This may only be done once |
| 41 // and must be called before any of the Offliner interface methods are called. | 42 // and must be called before any of the Offliner interface methods are called. |
| 42 void SetLoaderForTesting(std::unique_ptr<PrerenderingLoader> loader); | 43 void SetLoaderForTesting(std::unique_ptr<PrerenderingLoader> loader); |
| 43 | 44 |
| 45 void SetLowEndDeviceForTesting(bool is_low_end_device); |
| 46 |
| 47 void SetApplicationStateForTesting( |
| 48 base::android::ApplicationState application_state); |
| 49 |
| 44 protected: | 50 protected: |
| 45 // Internal method for requesting OfflinePageModel to save page. | 51 // Internal method for requesting OfflinePageModel to save page. |
| 46 // Exposed for unit testing. | 52 // Exposed for unit testing. |
| 47 // TODO(dougarnett): Consider making OfflinePageModel mockable instead. | 53 // TODO(dougarnett): Consider making OfflinePageModel mockable instead. |
| 48 virtual void SavePage(const GURL& url, | 54 virtual void SavePage(const GURL& url, |
| 49 const ClientId& client_id, | 55 const ClientId& client_id, |
| 50 std::unique_ptr<OfflinePageArchiver> archiver, | 56 std::unique_ptr<OfflinePageArchiver> archiver, |
| 51 const SavePageCallback& callback); | 57 const SavePageCallback& save_callback); |
| 52 | 58 |
| 53 private: | 59 private: |
| 54 // Callback logic for PrerenderingLoader::LoadPage(). | 60 // Callback logic for PrerenderingLoader::LoadPage(). |
| 55 void OnLoadPageDone(const SavePageRequest& request, | 61 void OnLoadPageDone(const SavePageRequest& request, |
| 56 const CompletionCallback& completion_callback, | |
| 57 Offliner::RequestStatus load_status, | 62 Offliner::RequestStatus load_status, |
| 58 content::WebContents* web_contents); | 63 content::WebContents* web_contents); |
| 59 | 64 |
| 60 // Callback logic for OfflinePageModel::SavePage(). | 65 // Callback logic for OfflinePageModel::SavePage(). |
| 61 void OnSavePageDone(const SavePageRequest& request, | 66 void OnSavePageDone(const SavePageRequest& request, |
| 62 const CompletionCallback& completion_callback, | |
| 63 SavePageResult save_result, | 67 SavePageResult save_result, |
| 64 int64_t offline_id); | 68 int64_t offline_id); |
| 65 | 69 |
| 66 PrerenderingLoader* GetOrCreateLoader(); | 70 PrerenderingLoader* GetOrCreateLoader(); |
| 67 void SetPendingRequest(int64_t request_id); | 71 |
| 68 void ClearPendingRequest(); | 72 // Listener function for changes to application background/foreground state. |
| 73 void OnApplicationStateChange( |
| 74 base::android::ApplicationState application_state); |
| 69 | 75 |
| 70 // Not owned. | 76 // Not owned. |
| 71 content::BrowserContext* browser_context_; | 77 content::BrowserContext* browser_context_; |
| 72 // Not owned. | 78 // Not owned. |
| 73 OfflinePageModel* offline_page_model_; | 79 OfflinePageModel* offline_page_model_; |
| 74 // Lazily created. | 80 // Lazily created. |
| 75 std::unique_ptr<PrerenderingLoader> loader_; | 81 std::unique_ptr<PrerenderingLoader> loader_; |
| 76 // Tracks pending request, if any. Owned copy. | 82 // Tracks pending request, if any. Owned copy. |
| 77 // May be used to ensure a callback applies to the pending request (e.g., in | 83 // May be used to ensure a callback applies to the pending request (e.g., in |
| 78 // case we receive a save page callback for an old, canceled request). | 84 // case we receive a save page callback for an old, canceled request). |
| 79 std::unique_ptr<SavePageRequest> pending_request_; | 85 std::unique_ptr<SavePageRequest> pending_request_; |
| 86 // Callback to call when pending request completes/fails. |
| 87 CompletionCallback completion_callback_; |
| 88 bool is_low_end_device_; |
| 89 // ApplicationStatusListener to monitor if the Chrome moves to the foreground. |
| 90 std::unique_ptr<base::android::ApplicationStatusListener> app_listener_; |
| 80 base::WeakPtrFactory<PrerenderingOffliner> weak_ptr_factory_; | 91 base::WeakPtrFactory<PrerenderingOffliner> weak_ptr_factory_; |
| 81 | 92 |
| 82 DISALLOW_COPY_AND_ASSIGN(PrerenderingOffliner); | 93 DISALLOW_COPY_AND_ASSIGN(PrerenderingOffliner); |
| 83 }; | 94 }; |
| 84 | 95 |
| 85 } // namespace offline_pages | 96 } // namespace offline_pages |
| 86 | 97 |
| 87 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_OFFLINER_H_ | 98 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_OFFLINER_H_ |
| OLD | NEW |