OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_BACKGROUND_LOADER_OFFLINER_H_ | |
6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_BACKGROUND_LOADER_OFFLINER_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/android/application_status_listener.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/values.h" | |
13 #include "components/offline_pages/content/background_loader/background_loader_c
ontents.h" | |
14 #include "components/offline_pages/core/background/offliner.h" | |
15 #include "components/offline_pages/core/offline_page_types.h" | |
16 #include "components/offline_pages/core/snapshot_controller.h" | |
17 #include "content/public/browser/web_contents_observer.h" | |
18 | |
19 namespace content { | |
20 class BrowserContext; | |
21 } // namespace content | |
22 | |
23 namespace offline_pages { | |
24 | |
25 class OfflinerPolicy; | |
26 class OfflinePageModel; | |
27 | |
28 // An Offliner implementation that attempts client-side rendering and saving | |
29 // of an offline page. It uses the BackgroundLoader to load the page and the | |
30 // OfflinePageModel to save it. Only one request may be active at a time. | |
31 class BackgroundLoaderOffliner : public Offliner, | |
32 public content::WebContentsObserver, | |
33 public SnapshotController::Client { | |
34 public: | |
35 BackgroundLoaderOffliner(content::BrowserContext* browser_context, | |
36 const OfflinerPolicy* policy, | |
37 OfflinePageModel* offline_page_model); | |
38 ~BackgroundLoaderOffliner() override; | |
39 | |
40 static BackgroundLoaderOffliner* FromWebContents( | |
41 content::WebContents* contents); | |
42 | |
43 // Offliner implementation. | |
44 bool LoadAndSave(const SavePageRequest& request, | |
45 const CompletionCallback& completion_callback, | |
46 const ProgressCallback& progress_callback) override; | |
47 bool Cancel(const CancelCallback& callback) override; | |
48 bool HandleTimeout(int64_t request_id) override; | |
49 | |
50 // WebContentsObserver implementation. | |
51 void DocumentAvailableInMainFrame() override; | |
52 void DocumentOnLoadCompletedInMainFrame() override; | |
53 void RenderProcessGone(base::TerminationStatus status) override; | |
54 void WebContentsDestroyed() override; | |
55 void DidFinishNavigation( | |
56 content::NavigationHandle* navigation_handle) override; | |
57 | |
58 // SnapshotController::Client implementation. | |
59 void StartSnapshot() override; | |
60 | |
61 void SetSnapshotControllerForTest( | |
62 std::unique_ptr<SnapshotController> controller); | |
63 void OnNetworkBytesChanged(int64_t bytes); | |
64 | |
65 protected: | |
66 // Called to reset the loader. | |
67 virtual void ResetLoader(); | |
68 | |
69 private: | |
70 friend class TestBackgroundLoaderOffliner; | |
71 | |
72 enum SaveState { NONE, SAVING, DELETE_AFTER_SAVE }; | |
73 enum PageLoadState { SUCCESS, RETRIABLE, NONRETRIABLE, DELAY_RETRY }; | |
74 | |
75 // Called when the page has been saved. | |
76 void OnPageSaved(SavePageResult save_result, int64_t offline_id); | |
77 | |
78 // Called to reset internal loader and observer state. | |
79 void ResetState(); | |
80 | |
81 // Called to attach 'this' as the observer to the loader. | |
82 void AttachObservers(); | |
83 | |
84 // Called when application state has changed. | |
85 void OnApplicationStateChange( | |
86 base::android::ApplicationState application_state); | |
87 | |
88 // Called to remember at what time we started loading. | |
89 void MarkLoadStartTime(); | |
90 | |
91 // Called to add a loading signal as we observe it. | |
92 void AddLoadingSignal(const char* signal_name); | |
93 | |
94 void DeleteOfflinePageCallback(const SavePageRequest& request, | |
95 DeletePageResult result); | |
96 | |
97 std::unique_ptr<background_loader::BackgroundLoaderContents> loader_; | |
98 // Not owned. | |
99 content::BrowserContext* browser_context_; | |
100 // Not owned. | |
101 OfflinePageModel* offline_page_model_; | |
102 // Not owned. | |
103 const OfflinerPolicy* policy_; | |
104 // Tracks pending request, if any. | |
105 std::unique_ptr<SavePageRequest> pending_request_; | |
106 // Handles determining when a page should be snapshotted. | |
107 std::unique_ptr<SnapshotController> snapshot_controller_; | |
108 // Callback when pending request completes. | |
109 CompletionCallback completion_callback_; | |
110 // Callback to report progress. | |
111 ProgressCallback progress_callback_; | |
112 // ApplicationStatusListener to monitor if Chrome moves to the foreground. | |
113 std::unique_ptr<base::android::ApplicationStatusListener> app_listener_; | |
114 // Whether we are on a low-end device. | |
115 bool is_low_end_device_; | |
116 | |
117 // Save state. | |
118 SaveState save_state_; | |
119 // Page load state. | |
120 PageLoadState page_load_state_; | |
121 // Network bytes loaded. | |
122 int64_t network_bytes_; | |
123 // Whether the low bar of snapshot quality has been met. | |
124 bool is_low_bar_met_; | |
125 // Whether the snapshot is on the last retry. | |
126 bool did_snapshot_on_last_retry_; | |
127 | |
128 // Time in ticks of when we start loading the page. | |
129 base::TimeTicks load_start_time_; | |
130 | |
131 // Saves loading signals. | |
132 // TODO(petewil): We will be replacing this with the new snapshot controller. | |
133 base::DictionaryValue signal_data_; | |
134 | |
135 // Callback for cancel. | |
136 CancelCallback cancel_callback_; | |
137 | |
138 base::WeakPtrFactory<BackgroundLoaderOffliner> weak_ptr_factory_; | |
139 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOffliner); | |
140 }; | |
141 | |
142 } // namespace offline_pages | |
143 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_BACKGROUND_LOADER_OFFLINER_H_ | |
OLD | NEW |