Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Unified Diff: chrome/browser/android/offline_pages/prerendering_loader.h

Issue 1968593002: PrerenderingLoader initial integration with PrerenderManager/PrerenderHandle (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address some feedback (some remains for followup) Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/offline_pages/prerendering_loader.h
diff --git a/chrome/browser/android/offline_pages/prerendering_loader.h b/chrome/browser/android/offline_pages/prerendering_loader.h
index c83c2770fa582bca502324ed078691baa6c96d94..be500fd51948b00af8d5d0352494d811e3773437 100644
--- a/chrome/browser/android/offline_pages/prerendering_loader.h
+++ b/chrome/browser/android/offline_pages/prerendering_loader.h
@@ -5,13 +5,17 @@
#ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_
#define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_PRERENDERING_LOADER_H_
+#include <memory>
+
#include "base/callback.h"
+#include "chrome/browser/prerender/prerender_handle.h"
+#include "chrome/browser/prerender/prerender_manager.h"
#include "components/offline_pages/background/offliner.h"
class GURL;
-class PrerenderManager;
namespace content {
+class BrowserContext;
class WebContents;
class SessionStorageNamespace;
} // namespace content
@@ -24,31 +28,129 @@ namespace offline_pages {
// A client-side page loader that integrates with the PrerenderManager to do
// 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
-class PrerenderingLoader {
+class PrerenderingLoader : public prerender::PrerenderHandle::Observer {
public:
+ // Adapter for making prerender stack calls internally. This provides virtual
+ // methods that may be overridden or mocked to allow for unit testing.
+ class PrerenderingAdapter {
+ public:
+ PrerenderingAdapter() {}
+ virtual ~PrerenderingAdapter() {}
+
+ // Returns whether prerendering is enabled and configured.
+ virtual bool CanPrerender() const;
+
+ // Requests prerendering of |url|.
+ virtual bool AddPrerenderForOffline(
+ content::BrowserContext* browser_context,
+ const GURL& url,
+ content::SessionStorageNamespace* session_storage_namespace,
+ const gfx::Size& size);
+
+ // Sets an observer on prerendering events.
+ virtual void SetObserver(prerender::PrerenderHandle::Observer* observer);
+
+ // Returns whether actively prerendering.
+ virtual bool IsPrerendering() const;
+
+ // Reports that prerendering should be canceled.
+ virtual void OnCancel();
+
+ // Returns a pointer to the prerendered WebContents. This should only be
+ // called once prerendering observer events indicate content is loaded.
+ // It may be used for snapshotting the page. The caller does NOT get
+ // ownership on the contents and must call PrerenderingLoader::StopLoading()
+ // to report it no longer needs the contents.
+ 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.
+
+ // Returns the final status of prerendering.
+ virtual prerender::FinalStatus GetFinalStatus() const;
+
+ // Returns whether there is an active prerendering.
+ virtual bool IsActive() const;
+
+ // Returns whether there is an active prerendering associated with
+ // the |handle|. This may be used to confirm that an observed prerender
+ // event for |handle| applies to the active prerendering.
+ virtual bool IsActive(prerender::PrerenderHandle* handle) const;
+
+ // Destroys and clears any current prerendering operation and state.
+ virtual void DestroyActive();
+
+ private:
+ std::unique_ptr<prerender::PrerenderHandle> active_handle_;
+ };
+
// Reports status of a load page request with loaded contents if available.
- typedef base::Callback<void(Offliner::CompletionStatus,
- content::WebContents*)>
+ typedef base::Callback<void(bool /* loaded */, content::WebContents*)>
LoadPageCallback;
- explicit PrerenderingLoader(PrerenderManager* prerender_manager);
- ~PrerenderingLoader();
+ explicit PrerenderingLoader(content::BrowserContext* browser_context);
+ virtual ~PrerenderingLoader();
// Loads a page in the background if possible and returns whether the
// request was accepted. If so, the LoadPageCallback will be informed
// of status. Only one load request may exist as a time. If a previous
- // request is still in progress it must be canceled before a new
+ // request is still in progress it must be stopped before a new
// request will be accepted.
- bool LoadPage(const GURL& url,
- content::SessionStorageNamespace* session_storage_namespace,
- const gfx::Size& size,
- const LoadPageCallback& callback);
+ bool LoadPage(const GURL& url, const LoadPageCallback& callback);
// Stops (completes or cancels) the load request. Must be called when
- // LoadPageCallback is done with consuming the contents.
+ // LoadPageCallback is done with consuming the contents. May be called
+ // prior to LoadPageCallback in order to cancel the current request (in
+ // which case the callback will not be called).
// This loader should also be responsible for stopping offline
// prerenders when Chrome is transitioned to foreground.
void StopLoading();
+
+ // Returns whether prerendering is possible for this device's configuration
+ // and the browser context.
+ bool CanPrerender();
+
+ // Returns whether the loader is idle and able to accept new LoadPage
+ // request.
+ bool IsIdle();
+
+ // Overrides the prerender stack adapter for unit testing.
+ void SetAdapterForTesting(PrerenderingAdapter* prerendering_adapter);
+
+ private:
+ // Determines and returns the session storage namespace to use for the
+ // prerendering request.
+ content::SessionStorageNamespace* GetSessionStorageNamespace();
+
+ // Determines and returns the window size to use for the prerendering request.
+ const gfx::Size GetSize();
+
+ // Reports the load result to the LoadPageCallback.
+ void ReportLoaded();
+
+ // Reports a load failure to the LoadPageCallback.
+ void ReportLoadFailed();
+
+ // Cancels any current prerender and moves loader to idle state.
+ void CancelPrerender();
+
+ // PrerenderHandle::Observer overrides:
+ void OnPrerenderStart(prerender::PrerenderHandle* handle) override;
+ void OnPrerenderStopLoading(prerender::PrerenderHandle* handle) override;
+ void OnPrerenderDomContentLoaded(prerender::PrerenderHandle* handle) override;
+ void OnPrerenderStop(prerender::PrerenderHandle* handle) override;
+ void OnPrerenderCreatedMatchCompleteReplacement(
+ prerender::PrerenderHandle* handle) override;
+
+ // State of the loader (only one request may be active at a time).
+ enum class State {
+ kIdle, // No active request.
+ kPending, // Request pending the start of prerendering.
+ kLoading, // Loading in progress.
+ kLoaded, // Loaded and now waiting for requestor to StopLoading().
+ };
+ State state_;
+
+ content::BrowserContext* browser_context_; // Not owned.
+ std::unique_ptr<PrerenderingAdapter> adapter_;
+ LoadPageCallback callback_;
};
} // namespace offline_pages

Powered by Google App Engine
This is Rietveld 408576698