OLD | NEW |
---|---|
(Empty) | |
1 // 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.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser; | |
6 | |
7 import android.content.Context; | |
8 import android.view.ContextThemeWrapper; | |
9 import android.view.LayoutInflater; | |
10 import android.view.View; | |
11 import android.view.ViewGroup; | |
12 import android.widget.FrameLayout; | |
13 | |
14 import org.chromium.chrome.browser.prerender.ExternalPrerenderHandler; | |
15 import org.chromium.chrome.browser.profiles.Profile; | |
16 | |
17 /** | |
18 * This class is a singleton that holds utilities for warming up Chrome and prer endering urls | |
19 * without creating the Activity. | |
20 */ | |
21 public class WarmupManager { | |
22 private static WarmupManager sWarmupManager; | |
23 | |
24 private long mPrerenderedWebContents; | |
25 private boolean mPrerendered; | |
26 private ViewGroup mMainView; | |
27 private ExternalPrerenderHandler mExternalPrerenderHandler; | |
28 | |
29 /** | |
30 * @return The singleton instance for the WarmupManager, creating one if nec essary. | |
31 */ | |
32 public static WarmupManager getInstance() { | |
33 if (sWarmupManager == null) sWarmupManager = new WarmupManager(); | |
34 return sWarmupManager; | |
35 } | |
36 | |
37 private WarmupManager() { | |
38 } | |
39 | |
40 /** | |
41 * Check whether prerender manager has the given url prerendered. This also works with | |
42 * redirected urls. | |
43 * @param url The url to check. | |
44 * @return Whether the given url has been prerendered. | |
45 */ | |
46 public boolean hasPrerenderedUrl(String url) { | |
47 return hasAnyPrerenderedUrl() && ExternalPrerenderHandler.hasPrerendered Url( | |
48 Profile.getLastUsedProfile(), url, mPrerenderedWebContents); | |
49 } | |
50 | |
51 /** | |
52 * @return Whether any url has been prerendered. | |
53 */ | |
54 public boolean hasAnyPrerenderedUrl() { | |
55 return mPrerendered; | |
56 } | |
57 | |
58 /** | |
59 * @return Native pointer for the prerendered web contents clearing out the reference | |
60 * WarmupManager owns. | |
61 */ | |
62 public long takePrerenderedNativeWebContents() { | |
63 long prerenderedWebContents = mPrerenderedWebContents; | |
64 assert (mPrerenderedWebContents != 0); | |
65 mPrerenderedWebContents = 0; | |
66 return prerenderedWebContents; | |
67 } | |
68 | |
69 /** | |
70 * Prerenders the given url using the prerender_manager. | |
71 * @param url The url to prerender. | |
72 * @param referrer The referrer url to be used while prerendering | |
73 * @param widthPix The width in pixels to which the page should be prerender ed. | |
74 * @param heightPix The height in pixels to which the page should be prerend ered. | |
75 */ | |
76 public void prerenderUrl(final String url, final String referrer, | |
77 final int widthPix, final int heightPix) { | |
78 clearWebContentsIfNecessary(); | |
79 if (mExternalPrerenderHandler == null) { | |
80 mExternalPrerenderHandler = new ExternalPrerenderHandler(); | |
81 } | |
82 | |
83 mPrerenderedWebContents = mExternalPrerenderHandler.addPrerender( | |
84 Profile.getLastUsedProfile(), url, referrer, widthPix, heightPix ); | |
85 if (mPrerenderedWebContents != 0) mPrerendered = true; | |
86 } | |
87 | |
88 /** | |
89 * Inflates and constructs the view hierarchy that the app will use. | |
90 * @param baseContext The base context to use for creating the ContextWrappe r. | |
91 * @param themeId | |
92 * @param layoutId | |
93 */ | |
94 public void initializeViewHierarchy(Context baseContext, int themeId, int la youtId) { | |
95 ContextThemeWrapper context = new ContextThemeWrapper(baseContext, theme Id); | |
96 FrameLayout contentHolder = new FrameLayout(context); | |
97 mMainView = (ViewGroup) LayoutInflater.from(context).inflate(layoutId, c ontentHolder); | |
98 } | |
99 | |
100 /** | |
101 * Transfers all the children in the view hierarchy to the giving ViewGroup as child. | |
102 * @param contentView The parent ViewGroup to use for the transfer. | |
103 */ | |
104 public void transferViewHierarchyTo(ViewGroup contentView) { | |
105 ViewGroup viewHierarchy = takeMainView(); | |
106 if (viewHierarchy == null) return; | |
107 while (viewHierarchy.getChildCount() > 0) { | |
108 View currentChild = viewHierarchy.getChildAt(0); | |
109 viewHierarchy.removeView(currentChild); | |
110 contentView.addView(currentChild); | |
111 } | |
112 } | |
113 | |
114 /** | |
115 * Destroys the native WebContents instance the WarmupManager currently hold s onto. | |
116 */ | |
117 public void clearWebContentsIfNecessary() { | |
118 mPrerendered = false; | |
119 if (mPrerenderedWebContents == 0) return; | |
120 | |
121 ContentViewUtil.destroyNativeWebContents(mPrerenderedWebContents); | |
122 mPrerenderedWebContents = 0; | |
123 } | |
124 | |
125 /** | |
126 * Cancel the current prerender. | |
127 */ | |
128 public void cancelCurrentPrerender() { | |
129 clearWebContentsIfNecessary(); | |
130 if (mExternalPrerenderHandler == null) return; | |
131 | |
132 mExternalPrerenderHandler.cancelCurrentPrerender(); | |
133 } | |
134 | |
135 /** | |
136 * @return Whether the view hierarchy has been prebuilt. | |
137 */ | |
138 public boolean hasBuiltViewHierarchy() { | |
139 return mMainView != null; | |
140 } | |
141 | |
142 /** | |
143 * @return The prebuilt view hierarchy and clears the reference WarmupManage r owns. | |
144 */ | |
145 private ViewGroup takeMainView() { | |
146 ViewGroup mainView = mMainView; | |
147 mMainView = null; | |
148 return mainView; | |
149 } | |
150 } | |
OLD | NEW |