Chromium Code Reviews| 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_LOADER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ | 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ |
| 7 | 7 |
| 8 #include <memory> | |
| 9 | |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "chrome/browser/android/offline_pages/prerender_adapter.h" | |
| 12 #include "chrome/browser/prerender/prerender_handle.h" | |
| 13 #include "chrome/browser/prerender/prerender_manager.h" | |
| 9 #include "components/offline_pages/background/offliner.h" | 14 #include "components/offline_pages/background/offliner.h" |
| 10 | 15 |
| 11 class GURL; | 16 class GURL; |
| 12 | 17 |
| 13 namespace content { | 18 namespace content { |
| 14 class BrowserContext; | 19 class BrowserContext; |
| 15 class WebContents; | 20 class WebContents; |
| 16 class SessionStorageNamespace; | 21 class SessionStorageNamespace; |
| 17 } // namespace content | 22 } // namespace content |
| 18 | 23 |
| 19 namespace gfx { | 24 namespace gfx { |
| 20 class Size; | 25 class Size; |
| 21 } // namespace gfx | 26 } // namespace gfx |
| 22 | 27 |
| 23 namespace offline_pages { | 28 namespace offline_pages { |
| 24 | 29 |
| 25 // A client-side page loader that integrates with the PrerenderManager to do | 30 // A client-side page loader that integrates with the PrerenderManager to do |
| 26 // the page loading in the background. | 31 // the page loading in the background. It operates on a single thread and |
| 32 // needs to run on BrowserThread::UI to work with the PrerenderManager. | |
| 33 // It supports a single load request at a time. | |
| 27 class PrerenderingLoader { | 34 class PrerenderingLoader { |
| 28 public: | 35 public: |
| 29 // Reports status of a load page request with loaded contents if available. | 36 // Reports status of a load page request with loaded contents if available. |
| 30 typedef base::Callback<void(Offliner::CompletionStatus, | 37 typedef base::Callback<void(Offliner::RequestStatus, content::WebContents*)> |
| 31 content::WebContents*)> | |
| 32 LoadPageCallback; | 38 LoadPageCallback; |
| 33 | 39 |
| 34 explicit PrerenderingLoader(content::BrowserContext* browser_context); | 40 explicit PrerenderingLoader(content::BrowserContext* browser_context); |
| 35 virtual ~PrerenderingLoader(); | 41 virtual ~PrerenderingLoader(); |
| 36 | 42 |
| 37 // Loads a page in the background if possible and returns whether the | 43 // Loads a page in the background if possible and returns whether the |
| 38 // request was accepted. If so, the LoadPageCallback will be informed | 44 // request was accepted. If so, the LoadPageCallback will be informed |
| 39 // of status. Only one load request may exist as a time. If a previous | 45 // of status. Only one load request may exist as a time. If a previous |
| 40 // request is still in progress it must be canceled before a new | 46 // request is still in progress it must be stopped before a new |
| 41 // request will be accepted. | 47 // request will be accepted. |
| 42 virtual bool LoadPage(const GURL& url, const LoadPageCallback& callback); | 48 virtual bool LoadPage(const GURL& url, const LoadPageCallback& callback); |
| 43 | 49 |
| 44 // Stops (completes or cancels) the load request. Must be called when | 50 // Stops (completes or cancels) the load request. Must be called when |
| 45 // LoadPageCallback is done with consuming the contents. | 51 // LoadPageCallback is done with consuming the contents. May be called |
| 52 // prior to LoadPageCallback in order to cancel the current request (in | |
| 53 // which case the callback will not be called). | |
| 46 // This loader should also be responsible for stopping offline | 54 // This loader should also be responsible for stopping offline |
| 47 // prerenders when Chrome is transitioned to foreground. | 55 // prerenders when Chrome is transitioned to foreground. |
| 48 virtual void StopLoading(); | 56 virtual void StopLoading(); |
| 57 | |
| 58 // Returns whether prerendering is possible for this device's configuration | |
| 59 // and the browser context. | |
| 60 virtual bool CanPrerender(); | |
| 61 | |
| 62 // Returns whether the loader is idle and able to accept new LoadPage | |
| 63 // request. | |
| 64 virtual bool IsIdle(); | |
| 65 | |
| 66 // Returns whether the loader has successfully loaded web contents. | |
| 67 // Note that |StopLoading()| should be used to clear this state once | |
| 68 // the loaded web contents are no longer needed. | |
| 69 virtual bool IsLoaded(); | |
| 70 | |
| 71 // Overrides the prerender stack adapter for unit testing. | |
| 72 void SetAdapterForTesting( | |
| 73 std::unique_ptr<PrerenderAdapter> prerender_adapter); | |
| 74 | |
| 75 private: | |
| 76 friend class PrerenderHandleObserver; | |
| 77 | |
| 78 // Delegating observer for PrerenderHandle events. Used to restrict access | |
| 79 // to loader's observer methods. | |
| 80 class PrerenderHandleObserver : public prerender::PrerenderHandle::Observer { | |
| 81 public: | |
| 82 explicit PrerenderHandleObserver(PrerenderingLoader* loader, | |
|
fgorski
2016/05/19 04:29:39
no need for explicit, when number of arguments is
dougarnett
2016/05/20 00:27:23
Acknowledged.
| |
| 83 PrerenderAdapter* adapter); | |
| 84 ~PrerenderHandleObserver() override; | |
| 85 | |
| 86 void OnPrerenderStart(prerender::PrerenderHandle* handle) override; | |
|
pasko
2016/05/18 11:47:26
A cleaner design IMO is PrerenderHandleObserver be
dougarnett
2016/05/20 00:27:23
Done.
| |
| 87 void OnPrerenderStopLoading(prerender::PrerenderHandle* handle) override; | |
| 88 void OnPrerenderDomContentLoaded( | |
| 89 prerender::PrerenderHandle* handle) override; | |
| 90 void OnPrerenderStop(prerender::PrerenderHandle* handle) override; | |
|
pasko
2016/05/18 11:47:26
please dispatch OnPrerenderStop in tests
dougarnett
2016/05/20 00:27:23
Yes, good point. I need to report a Stop (perhaps
| |
| 91 | |
| 92 private: | |
| 93 // Loader to delegate prerender events to. Not owned. | |
| 94 PrerenderingLoader* loader_; | |
| 95 // Adapter used to check validity of observed events. Not owned. | |
| 96 PrerenderAdapter* adapter_; | |
| 97 }; | |
| 98 | |
| 99 // State of the loader (only one request may be active at a time). | |
| 100 enum class State { | |
| 101 IDLE, // No active load request. | |
| 102 PENDING, // Load request is pending the start of prerendering. | |
| 103 LOADING, // Loading in progress. | |
| 104 LOADED, // Loaded and now waiting for requestor to StopLoading(). | |
| 105 }; | |
| 106 | |
| 107 // Prerender event handling logic: | |
| 108 void OnPrerenderStart(); | |
| 109 void OnPrerenderStopLoading(); | |
| 110 void OnPrerenderDomContentLoaded(); | |
| 111 void OnPrerenderStop(); | |
| 112 | |
| 113 // Returns the session storage namespace to use for the prerendering request. | |
| 114 content::SessionStorageNamespace* GetSessionStorageNamespace(); | |
| 115 | |
| 116 // Returns the window size to render. | |
| 117 const gfx::Size GetSize(); | |
| 118 | |
| 119 // Reports the load result to the LoadPageCallback if load still in progress. | |
| 120 void ReportLoadedIfStillLoading(); | |
| 121 | |
| 122 // Reports a load failure to the LoadPageCallback if load still in progress. | |
| 123 void ReportLoadFailedIfStillLoading(); | |
| 124 | |
| 125 // Cancels any current prerender and moves loader to idle state. | |
| 126 void CancelPrerender(); | |
| 127 | |
| 128 // Tracks loading state including whether the Loader is idle. | |
| 129 State state_; | |
| 130 | |
| 131 // Not owned. | |
| 132 content::BrowserContext* browser_context_; | |
| 133 | |
| 134 // Adapter for handling calls to the prerender stack. | |
| 135 std::unique_ptr<PrerenderAdapter> adapter_; | |
| 136 | |
| 137 // Observer for prerender events. | |
| 138 std::unique_ptr<PrerenderHandleObserver> observer_; | |
| 139 | |
| 140 // A WebContents for the active load request that is used to hold the session | |
| 141 // storage namespace for rendering. This will NOT have the loaded page. | |
| 142 std::unique_ptr<content::WebContents> session_contents_; | |
| 143 | |
| 144 // Callback to call when the active load request completes, fails, or is | |
| 145 // canceled. | |
| 146 LoadPageCallback callback_; | |
| 147 | |
| 148 DISALLOW_COPY_AND_ASSIGN(PrerenderingLoader); | |
| 49 }; | 149 }; |
| 50 | 150 |
| 51 } // namespace offline_pages | 151 } // namespace offline_pages |
| 52 | 152 |
| 53 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ | 153 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ |
| OLD | NEW |