| 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 |
| 27 class PrerenderingLoader { | 32 // needs to run on BrowserThread::UI to work with the PrerenderManager. |
| 33 class PrerenderingLoader : public prerender::PrerenderHandle::Observer { |
| 28 public: | 34 public: |
| 29 // Reports status of a load page request with loaded contents if available. | 35 // Reports status of a load page request with loaded contents if available. |
| 30 typedef base::Callback<void(Offliner::CompletionStatus, | 36 typedef base::Callback<void(Offliner::CompletionStatus, |
| 31 content::WebContents*)> | 37 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 ~PrerenderingLoader() override; |
| 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 bool CanPrerender(); |
| 61 |
| 62 // Returns whether the loader is idle and able to accept new LoadPage |
| 63 // request. |
| 64 bool IsIdle(); |
| 65 |
| 66 // Overrides the prerender stack adapter for unit testing. |
| 67 void SetAdapterForTesting(PrerenderAdapter* prerender_adapter); |
| 68 |
| 69 // PrerenderHandle::Observer overrides: |
| 70 void OnPrerenderStart(prerender::PrerenderHandle* handle) override; |
| 71 void OnPrerenderStopLoading(prerender::PrerenderHandle* handle) override; |
| 72 void OnPrerenderDomContentLoaded(prerender::PrerenderHandle* handle) override; |
| 73 void OnPrerenderStop(prerender::PrerenderHandle* handle) override; |
| 74 |
| 75 private: |
| 76 // Returns the session storage namespace to use for the |
| 77 // prerendering request obtained from [session_contents|. |
| 78 content::SessionStorageNamespace* GetSessionStorageNamespace( |
| 79 content::WebContents* session_contents); |
| 80 |
| 81 // Returns the window size to render from |session_contents|. |
| 82 const gfx::Size GetSize(content::WebContents* session_contents); |
| 83 |
| 84 // Reports the load result to the LoadPageCallback. |
| 85 void ReportLoaded(); |
| 86 |
| 87 // Reports a load failure to the LoadPageCallback. |
| 88 void ReportLoadFailed(); |
| 89 |
| 90 // Cancels any current prerender and moves loader to idle state. |
| 91 void CancelPrerender(); |
| 92 |
| 93 // State of the loader (only one request may be active at a time). |
| 94 enum class State { |
| 95 kIdle, // No active request. |
| 96 kPending, // Request pending the start of prerendering. |
| 97 kLoading, // Loading in progress. |
| 98 kLoaded, // Loaded and now waiting for requestor to StopLoading(). |
| 99 }; |
| 100 State state_; |
| 101 |
| 102 content::BrowserContext* browser_context_; // Not owned. |
| 103 std::unique_ptr<PrerenderAdapter> adapter_; |
| 104 std::unique_ptr<content::WebContents> session_contents_; |
| 105 LoadPageCallback callback_; |
| 49 }; | 106 }; |
| 50 | 107 |
| 51 } // namespace offline_pages | 108 } // namespace offline_pages |
| 52 | 109 |
| 53 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ | 110 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ |
| OLD | NEW |