| 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" |
| 9 #include "components/offline_pages/background/offliner.h" | 12 #include "components/offline_pages/background/offliner.h" |
| 10 | 13 |
| 11 class GURL; | 14 class GURL; |
| 12 | 15 |
| 13 namespace content { | 16 namespace content { |
| 14 class BrowserContext; | 17 class BrowserContext; |
| 15 class WebContents; | 18 class WebContents; |
| 16 class SessionStorageNamespace; | 19 class SessionStorageNamespace; |
| 17 } // namespace content | 20 } // namespace content |
| 18 | 21 |
| 19 namespace gfx { | 22 namespace gfx { |
| 20 class Size; | 23 class Size; |
| 21 } // namespace gfx | 24 } // namespace gfx |
| 22 | 25 |
| 23 namespace offline_pages { | 26 namespace offline_pages { |
| 24 | 27 |
| 25 // A client-side page loader that integrates with the PrerenderManager to do | 28 // A client-side page loader that integrates with the PrerenderManager to do |
| 26 // the page loading in the background. | 29 // the page loading in the background. It operates on a single thread and |
| 27 class PrerenderingLoader { | 30 // needs to run on BrowserThread::UI to work with the PrerenderManager. |
| 31 // It supports a single load request at a time. |
| 32 class PrerenderingLoader : public PrerenderAdapter::Observer { |
| 28 public: | 33 public: |
| 29 // Reports status of a load page request with loaded contents if available. | 34 // Reports status of a load page request with loaded contents if available. |
| 30 typedef base::Callback<void(Offliner::CompletionStatus, | 35 typedef base::Callback<void(Offliner::RequestStatus, content::WebContents*)> |
| 31 content::WebContents*)> | |
| 32 LoadPageCallback; | 36 LoadPageCallback; |
| 33 | 37 |
| 34 explicit PrerenderingLoader(content::BrowserContext* browser_context); | 38 explicit PrerenderingLoader(content::BrowserContext* browser_context); |
| 35 virtual ~PrerenderingLoader(); | 39 ~PrerenderingLoader() override; |
| 36 | 40 |
| 37 // Loads a page in the background if possible and returns whether the | 41 // Loads a page in the background if possible and returns whether the |
| 38 // request was accepted. If so, the LoadPageCallback will be informed | 42 // 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 | 43 // 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 | 44 // request is still in progress it must be stopped before a new |
| 41 // request will be accepted. | 45 // request will be accepted. The callback may be called more than |
| 46 // once - first for a successful load and then if canceled after the |
| 47 // load (which may be from resources being reclaimed) at which point |
| 48 // the retrieved WebContents should no longer be used. |
| 42 virtual bool LoadPage(const GURL& url, const LoadPageCallback& callback); | 49 virtual bool LoadPage(const GURL& url, const LoadPageCallback& callback); |
| 43 | 50 |
| 44 // Stops (completes or cancels) the load request. Must be called when | 51 // Stops (completes or cancels) the load request. Must be called when |
| 45 // LoadPageCallback is done with consuming the contents. | 52 // LoadPageCallback is done with consuming the contents. May be called |
| 53 // prior to LoadPageCallback in order to cancel the current request (in |
| 54 // which case the callback will not be run). |
| 46 // This loader should also be responsible for stopping offline | 55 // This loader should also be responsible for stopping offline |
| 47 // prerenders when Chrome is transitioned to foreground. | 56 // prerenders when Chrome is transitioned to foreground. |
| 48 virtual void StopLoading(); | 57 virtual void StopLoading(); |
| 58 |
| 59 // Returns whether prerendering is possible for this device's configuration |
| 60 // and the browser context. |
| 61 virtual bool CanPrerender(); |
| 62 |
| 63 // Returns whether the loader is idle and able to accept new LoadPage |
| 64 // request. |
| 65 virtual bool IsIdle(); |
| 66 |
| 67 // Returns whether the loader has successfully loaded web contents. |
| 68 // Note that |StopLoading()| should be used to clear this state once |
| 69 // the loaded web contents are no longer needed. |
| 70 virtual bool IsLoaded(); |
| 71 |
| 72 // Overrides the prerender stack adapter for unit testing. |
| 73 void SetAdapterForTesting( |
| 74 std::unique_ptr<PrerenderAdapter> prerender_adapter); |
| 75 |
| 76 // PrerenderAdapter::Observer implementation: |
| 77 void OnPrerenderStart() override; |
| 78 void OnPrerenderStopLoading() override; |
| 79 void OnPrerenderDomContentLoaded() override; |
| 80 void OnPrerenderStop() override; |
| 81 |
| 82 private: |
| 83 // State of the loader (only one request may be active at a time). |
| 84 enum class State { |
| 85 IDLE, // No active load request. |
| 86 PENDING, // Load request is pending the start of prerendering. |
| 87 LOADING, // Loading in progress. |
| 88 LOADED, // Loaded and now waiting for requestor to StopLoading(). |
| 89 }; |
| 90 |
| 91 // Handles some event/signal that the load request has succeeded or failed. |
| 92 // It may be due to some asynchronous trigger that occurs after the request |
| 93 // has completed for some other reason/event. |
| 94 void HandleLoadEvent(); |
| 95 |
| 96 // Handles some event/signal that loading has stopped (whether due to a |
| 97 // failure, cancel, or stop request). It may be due to some asynchronous |
| 98 // trigger that occurs after the request has stopped for some other reason. |
| 99 void HandleLoadingStopped(); |
| 100 |
| 101 // Cancels any current prerender and moves loader to idle state. |
| 102 void CancelPrerender(); |
| 103 |
| 104 // Tracks loading state including whether the Loader is idle. |
| 105 State state_; |
| 106 |
| 107 // Not owned. |
| 108 content::BrowserContext* browser_context_; |
| 109 |
| 110 // Adapter for handling calls to the prerender stack. |
| 111 std::unique_ptr<PrerenderAdapter> adapter_; |
| 112 |
| 113 // A WebContents for the active load request that is used to hold the session |
| 114 // storage namespace for rendering. This will NOT have the loaded page. |
| 115 std::unique_ptr<content::WebContents> session_contents_; |
| 116 |
| 117 // Callback to call when the active load request completes, fails, or is |
| 118 // canceled. |
| 119 LoadPageCallback callback_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(PrerenderingLoader); |
| 49 }; | 122 }; |
| 50 | 123 |
| 51 } // namespace offline_pages | 124 } // namespace offline_pages |
| 52 | 125 |
| 53 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ | 126 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_ |
| OLD | NEW |