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/prerender/prerender_handle.h" | |
| 12 #include "chrome/browser/prerender/prerender_manager.h" | |
| 9 #include "components/offline_pages/background/offliner.h" | 13 #include "components/offline_pages/background/offliner.h" |
| 10 | 14 |
| 11 class GURL; | 15 class GURL; |
| 12 class PrerenderManager; | |
| 13 | 16 |
| 14 namespace content { | 17 namespace content { |
| 18 class BrowserContext; | |
| 15 class WebContents; | 19 class WebContents; |
| 16 class SessionStorageNamespace; | 20 class SessionStorageNamespace; |
| 17 } // namespace content | 21 } // namespace content |
| 18 | 22 |
| 19 namespace gfx { | 23 namespace gfx { |
| 20 class Size; | 24 class Size; |
| 21 } // namespace gfx | 25 } // namespace gfx |
| 22 | 26 |
| 23 namespace offline_pages { | 27 namespace offline_pages { |
| 24 | 28 |
| 25 // A client-side page loader that integrates with the PrerenderManager to do | 29 // A client-side page loader that integrates with the PrerenderManager to do |
| 26 // the page loading in the background. | 30 // the page loading in the background. |
|
pasko
2016/05/11 12:18:34
what are the threading-related guarantees of this
dougarnett
2016/05/11 21:10:14
Updated comment. Single thread which needs to be U
| |
| 27 class PrerenderingLoader { | 31 class PrerenderingLoader : public prerender::PrerenderHandle::Observer { |
| 28 public: | 32 public: |
| 33 // Adapter for making prerender stack calls internally. This provides virtual | |
| 34 // methods that may be overridden or mocked to allow for unit testing. | |
| 35 class PrerenderingAdapter { | |
| 36 public: | |
| 37 PrerenderingAdapter() {} | |
| 38 virtual ~PrerenderingAdapter() {} | |
| 39 | |
| 40 // Returns whether prerendering is enabled and configured. | |
| 41 virtual bool CanPrerender() const; | |
| 42 | |
| 43 // Requests prerendering of |url|. | |
| 44 virtual bool AddPrerenderForOffline( | |
| 45 content::BrowserContext* browser_context, | |
| 46 const GURL& url, | |
| 47 content::SessionStorageNamespace* session_storage_namespace, | |
| 48 const gfx::Size& size); | |
| 49 | |
| 50 // Sets an observer on prerendering events. | |
| 51 virtual void SetObserver(prerender::PrerenderHandle::Observer* observer); | |
| 52 | |
| 53 // Returns whether actively prerendering. | |
| 54 virtual bool IsPrerendering() const; | |
| 55 | |
| 56 // Reports that prerendering should be canceled. | |
| 57 virtual void OnCancel(); | |
| 58 | |
| 59 // Returns a pointer to the prerendered WebContents. This should only be | |
| 60 // called once prerendering observer events indicate content is loaded. | |
| 61 // It may be used for snapshotting the page. The caller does NOT get | |
| 62 // ownership on the contents and must call PrerenderingLoader::StopLoading() | |
| 63 // to report it no longer needs the contents. | |
| 64 virtual content::WebContents* GetPrerenderContents() const; | |
|
pasko
2016/05/11 12:18:34
probably should be named as GetWebContents() becau
dougarnett
2016/05/11 21:10:14
Done.
| |
| 65 | |
| 66 // Returns the final status of prerendering. | |
| 67 virtual prerender::FinalStatus GetFinalStatus() const; | |
| 68 | |
| 69 // Returns whether there is an active prerendering. | |
| 70 virtual bool IsActive() const; | |
| 71 | |
| 72 // Returns whether there is an active prerendering associated with | |
| 73 // the |handle|. This may be used to confirm that an observed prerender | |
| 74 // event for |handle| applies to the active prerendering. | |
| 75 virtual bool IsActive(prerender::PrerenderHandle* handle) const; | |
| 76 | |
| 77 // Destroys and clears any current prerendering operation and state. | |
| 78 virtual void DestroyActive(); | |
| 79 | |
| 80 private: | |
| 81 std::unique_ptr<prerender::PrerenderHandle> active_handle_; | |
| 82 }; | |
| 83 | |
| 29 // Reports status of a load page request with loaded contents if available. | 84 // Reports status of a load page request with loaded contents if available. |
| 30 typedef base::Callback<void(Offliner::CompletionStatus, | 85 typedef base::Callback<void(bool /* loaded */, content::WebContents*)> |
| 31 content::WebContents*)> | |
| 32 LoadPageCallback; | 86 LoadPageCallback; |
| 33 | 87 |
| 34 explicit PrerenderingLoader(PrerenderManager* prerender_manager); | 88 explicit PrerenderingLoader(content::BrowserContext* browser_context); |
| 35 ~PrerenderingLoader(); | 89 virtual ~PrerenderingLoader(); |
| 36 | 90 |
| 37 // Loads a page in the background if possible and returns whether the | 91 // Loads a page in the background if possible and returns whether the |
| 38 // request was accepted. If so, the LoadPageCallback will be informed | 92 // 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 | 93 // 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 | 94 // request is still in progress it must be stopped before a new |
| 41 // request will be accepted. | 95 // request will be accepted. |
| 42 bool LoadPage(const GURL& url, | 96 bool LoadPage(const GURL& url, const LoadPageCallback& callback); |
| 43 content::SessionStorageNamespace* session_storage_namespace, | |
| 44 const gfx::Size& size, | |
| 45 const LoadPageCallback& callback); | |
| 46 | 97 |
| 47 // Stops (completes or cancels) the load request. Must be called when | 98 // Stops (completes or cancels) the load request. Must be called when |
| 48 // LoadPageCallback is done with consuming the contents. | 99 // LoadPageCallback is done with consuming the contents. May be called |
| 100 // prior to LoadPageCallback in order to cancel the current request (in | |
| 101 // which case the callback will not be called). | |
| 49 // This loader should also be responsible for stopping offline | 102 // This loader should also be responsible for stopping offline |
| 50 // prerenders when Chrome is transitioned to foreground. | 103 // prerenders when Chrome is transitioned to foreground. |
| 51 void StopLoading(); | 104 void StopLoading(); |
| 105 | |
| 106 // Returns whether prerendering is possible for this device's configuration | |
| 107 // and the browser context. | |
| 108 bool CanPrerender(); | |
| 109 | |
| 110 // Returns whether the loader is idle and able to accept new LoadPage | |
| 111 // request. | |
| 112 bool IsIdle(); | |
| 113 | |
| 114 // Overrides the prerender stack adapter for unit testing. | |
| 115 void SetAdapterForTesting(PrerenderingAdapter* prerendering_adapter); | |
| 116 | |
| 117 private: | |
| 118 // Determines and returns the session storage namespace to use for the | |
| 119 // prerendering request. | |
| 120 content::SessionStorageNamespace* GetSessionStorageNamespace(); | |
| 121 | |
| 122 // Determines and returns the window size to use for the prerendering request. | |
| 123 const gfx::Size GetSize(); | |
| 124 | |
| 125 // Reports the load result to the LoadPageCallback. | |
| 126 void ReportLoaded(); | |
| 127 | |
| 128 // Reports a load failure to the LoadPageCallback. | |
| 129 void ReportLoadFailed(); | |
| 130 | |
| 131 // Cancels any current prerender and moves loader to idle state. | |
| 132 void CancelPrerender(); | |
| 133 | |
| 134 // PrerenderHandle::Observer overrides: | |
| 135 void OnPrerenderStart(prerender::PrerenderHandle* handle) override; | |
| 136 void OnPrerenderStopLoading(prerender::PrerenderHandle* handle) override; | |
| 137 void OnPrerenderDomContentLoaded(prerender::PrerenderHandle* handle) override; | |
| 138 void OnPrerenderStop(prerender::PrerenderHandle* handle) override; | |
| 139 void OnPrerenderCreatedMatchCompleteReplacement( | |
| 140 prerender::PrerenderHandle* handle) override; | |
| 141 | |
| 142 // State of the loader (only one request may be active at a time). | |
| 143 enum class State { | |
| 144 kIdle, // No active request. | |
| 145 kPending, // Request pending the start of prerendering. | |
| 146 kLoading, // Loading in progress. | |
| 147 kLoaded, // Loaded and now waiting for requestor to StopLoading(). | |
| 148 }; | |
| 149 State state_; | |
| 150 | |
| 151 content::BrowserContext* browser_context_; // Not owned. | |
| 152 std::unique_ptr<PrerenderingAdapter> adapter_; | |
| 153 LoadPageCallback callback_; | |
| 52 }; | 154 }; |
| 53 | 155 |
| 54 } // namespace offline_pages | 156 } // namespace offline_pages |
| 55 | 157 |
| 56 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ | 158 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ |
| OLD | NEW |