Index: chrome/android/java/src/org/chromium/chrome/browser/WarmupManager.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/WarmupManager.java b/chrome/android/java/src/org/chromium/chrome/browser/WarmupManager.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..41e2b4df6d190385115a7ed1385b0f3a6d589756 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/WarmupManager.java |
@@ -0,0 +1,150 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
David Trainor- moved to gerrit
2015/01/21 08:13:26
2015
Benoit L
2015/01/21 09:36:37
Done.
Thank you.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser; |
+ |
+import android.content.Context; |
+import android.view.ContextThemeWrapper; |
+import android.view.LayoutInflater; |
+import android.view.View; |
+import android.view.ViewGroup; |
+import android.widget.FrameLayout; |
+ |
+import org.chromium.chrome.browser.prerender.ExternalPrerenderHandler; |
+import org.chromium.chrome.browser.profiles.Profile; |
+ |
+/** |
+ * This class is a singleton that holds utilities for warming up Chrome and prerendering urls |
+ * without creating the Activity. |
+ */ |
+public class WarmupManager { |
+ private static WarmupManager sWarmupManager; |
+ |
+ private long mPrerenderedWebContents; |
+ private boolean mPrerendered; |
+ private ViewGroup mMainView; |
+ private ExternalPrerenderHandler mExternalPrerenderHandler; |
+ |
+ /** |
+ * @return The singleton instance for the WarmupManager, creating one if necessary. |
+ */ |
+ public static WarmupManager getInstance() { |
+ if (sWarmupManager == null) sWarmupManager = new WarmupManager(); |
+ return sWarmupManager; |
+ } |
+ |
+ private WarmupManager() { |
+ } |
+ |
+ /** |
+ * Check whether prerender manager has the given url prerendered. This also works with |
+ * redirected urls. |
+ * @param url The url to check. |
+ * @return Whether the given url has been prerendered. |
+ */ |
+ public boolean hasPrerenderedUrl(String url) { |
+ return hasAnyPrerenderedUrl() && ExternalPrerenderHandler.hasPrerenderedUrl( |
+ Profile.getLastUsedProfile(), url, mPrerenderedWebContents); |
+ } |
+ |
+ /** |
+ * @return Whether any url has been prerendered. |
+ */ |
+ public boolean hasAnyPrerenderedUrl() { |
+ return mPrerendered; |
+ } |
+ |
+ /** |
+ * @return Native pointer for the prerendered web contents clearing out the reference |
+ * WarmupManager owns. |
+ */ |
+ public long takePrerenderedNativeWebContents() { |
+ long prerenderedWebContents = mPrerenderedWebContents; |
+ assert (mPrerenderedWebContents != 0); |
+ mPrerenderedWebContents = 0; |
+ return prerenderedWebContents; |
+ } |
+ |
+ /** |
+ * Prerenders the given url using the prerender_manager. |
+ * @param url The url to prerender. |
+ * @param referrer The referrer url to be used while prerendering |
+ * @param widthPix The width in pixels to which the page should be prerendered. |
+ * @param heightPix The height in pixels to which the page should be prerendered. |
+ */ |
+ public void prerenderUrl(final String url, final String referrer, |
+ final int widthPix, final int heightPix) { |
+ clearWebContentsIfNecessary(); |
+ if (mExternalPrerenderHandler == null) { |
+ mExternalPrerenderHandler = new ExternalPrerenderHandler(); |
+ } |
+ |
+ mPrerenderedWebContents = mExternalPrerenderHandler.addPrerender( |
+ Profile.getLastUsedProfile(), url, referrer, widthPix, heightPix); |
+ if (mPrerenderedWebContents != 0) mPrerendered = true; |
+ } |
+ |
+ /** |
+ * Inflates and constructs the view hierarchy that the app will use. |
+ * @param baseContext The base context to use for creating the ContextWrapper. |
+ * @param themeId |
+ * @param layoutId |
+ */ |
+ public void initializeViewHierarchy(Context baseContext, int themeId, int layoutId) { |
+ ContextThemeWrapper context = new ContextThemeWrapper(baseContext, themeId); |
+ FrameLayout contentHolder = new FrameLayout(context); |
+ mMainView = (ViewGroup) LayoutInflater.from(context).inflate(layoutId, contentHolder); |
+ } |
+ |
+ /** |
+ * Transfers all the children in the view hierarchy to the giving ViewGroup as child. |
+ * @param contentView The parent ViewGroup to use for the transfer. |
+ */ |
+ public void transferViewHierarchyTo(ViewGroup contentView) { |
+ ViewGroup viewHierarchy = takeMainView(); |
+ if (viewHierarchy == null) return; |
+ while (viewHierarchy.getChildCount() > 0) { |
+ View currentChild = viewHierarchy.getChildAt(0); |
+ viewHierarchy.removeView(currentChild); |
+ contentView.addView(currentChild); |
+ } |
+ } |
+ |
+ /** |
+ * Destroys the native WebContents instance the WarmupManager currently holds onto. |
+ */ |
+ public void clearWebContentsIfNecessary() { |
+ mPrerendered = false; |
+ if (mPrerenderedWebContents == 0) return; |
+ |
+ ContentViewUtil.destroyNativeWebContents(mPrerenderedWebContents); |
+ mPrerenderedWebContents = 0; |
+ } |
+ |
+ /** |
+ * Cancel the current prerender. |
+ */ |
+ public void cancelCurrentPrerender() { |
+ clearWebContentsIfNecessary(); |
+ if (mExternalPrerenderHandler == null) return; |
+ |
+ mExternalPrerenderHandler.cancelCurrentPrerender(); |
+ } |
+ |
+ /** |
+ * @return Whether the view hierarchy has been prebuilt. |
+ */ |
+ public boolean hasBuiltViewHierarchy() { |
+ return mMainView != null; |
+ } |
+ |
+ /** |
+ * @return The prebuilt view hierarchy and clears the reference WarmupManager owns. |
+ */ |
+ private ViewGroup takeMainView() { |
+ ViewGroup mainView = mMainView; |
+ mMainView = null; |
+ return mainView; |
+ } |
+} |