OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
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.chromecast.shell; | |
6 | |
7 import android.app.Activity; | |
8 import android.content.Context; | |
9 import android.content.Intent; | |
10 import android.net.Uri; | |
11 import android.support.v4.content.LocalBroadcastManager; | |
12 import android.text.TextUtils; | |
13 import android.util.AttributeSet; | |
14 import android.view.ViewGroup; | |
15 import android.widget.FrameLayout; | |
16 import android.widget.LinearLayout; | |
17 | |
18 import org.chromium.base.Log; | |
19 import org.chromium.base.annotations.CalledByNative; | |
20 import org.chromium.base.annotations.JNINamespace; | |
21 import org.chromium.content.browser.ActivityContentVideoViewEmbedder; | |
22 import org.chromium.content.browser.ContentVideoViewEmbedder; | |
23 import org.chromium.content.browser.ContentView; | |
24 import org.chromium.content.browser.ContentViewCore; | |
25 import org.chromium.content.browser.ContentViewRenderView; | |
26 import org.chromium.content_public.browser.LoadUrlParams; | |
27 import org.chromium.content_public.browser.NavigationController; | |
28 import org.chromium.content_public.browser.WebContents; | |
29 import org.chromium.content_public.browser.WebContentsObserver; | |
30 import org.chromium.ui.base.ViewAndroidDelegate; | |
31 import org.chromium.ui.base.WindowAndroid; | |
32 | |
33 /** | |
34 * Container for the various UI components that make up a shell window. | |
35 */ | |
36 @JNINamespace("chromecast::shell") | |
37 public class CastWindowAndroid extends LinearLayout { | |
38 public static final String TAG = "CastWindowAndroid"; | |
39 | |
40 public static final String ACTION_PAGE_LOADED = "castPageLoaded"; | |
41 public static final String ACTION_ENABLE_DEV_TOOLS = "castEnableDevTools"; | |
42 public static final String ACTION_DISABLE_DEV_TOOLS = "castDisableDevTools"; | |
43 | |
44 private ContentViewCore mContentViewCore; | |
45 private ContentViewRenderView mContentViewRenderView; | |
46 private NavigationController mNavigationController; | |
47 private int mRenderProcessId; | |
48 private WebContents mWebContents; | |
49 private WebContentsObserver mWebContentsObserver; | |
50 private WindowAndroid mWindow; | |
51 | |
52 /** | |
53 * Constructor for inflating via XML. | |
54 */ | |
55 public CastWindowAndroid(Context context, AttributeSet attrs) { | |
56 super(context, attrs); | |
57 } | |
58 | |
59 /** | |
60 * Set the SurfaceView being renderered to as soon as it is available. | |
61 */ | |
62 public void setContentViewRenderView(ContentViewRenderView contentViewRender
View) { | |
63 FrameLayout contentViewHolder = (FrameLayout) findViewById(R.id.contentv
iew_holder); | |
64 if (contentViewRenderView == null) { | |
65 if (mContentViewRenderView != null) { | |
66 contentViewHolder.removeView(mContentViewRenderView); | |
67 } | |
68 } else { | |
69 contentViewHolder.addView(contentViewRenderView, | |
70 new FrameLayout.LayoutParams( | |
71 FrameLayout.LayoutParams.MATCH_PARENT, | |
72 FrameLayout.LayoutParams.MATCH_PARENT)); | |
73 } | |
74 mContentViewRenderView = contentViewRenderView; | |
75 } | |
76 | |
77 /** | |
78 * @param window The owning window for this shell. | |
79 */ | |
80 public void setWindow(WindowAndroid window) { | |
81 mWindow = window; | |
82 } | |
83 | |
84 /** | |
85 * Loads an URL. This will perform minimal amounts of sanitizing of the URL
to attempt to | |
86 * make it valid. | |
87 * | |
88 * @param url The URL to be loaded by the shell. | |
89 */ | |
90 public void loadUrl(String url) { | |
91 if (url == null) return; | |
92 | |
93 if (TextUtils.equals(url, mWebContents.getUrl())) { | |
94 mNavigationController.reload(true); | |
95 } else { | |
96 mNavigationController.loadUrl(new LoadUrlParams(normalizeUrl(url))); | |
97 } | |
98 | |
99 // TODO(aurimas): Remove this when crbug.com/174541 is fixed. | |
100 mContentViewCore.getContainerView().clearFocus(); | |
101 mContentViewCore.getContainerView().requestFocus(); | |
102 } | |
103 | |
104 /** | |
105 * Returns the render_process_id for the associated web contents | |
106 */ | |
107 public int getRenderProcessId() { | |
108 return mRenderProcessId; | |
109 } | |
110 | |
111 /** | |
112 * Given a URI String, performs minimal normalization to attempt to build a
usable URL from it. | |
113 * @param uriString The passed-in path to be normalized. | |
114 * @return The normalized URL, as a string. | |
115 */ | |
116 private static String normalizeUrl(String uriString) { | |
117 if (uriString == null) return uriString; | |
118 Uri uri = Uri.parse(uriString); | |
119 if (uri.getScheme() == null) { | |
120 uri = Uri.parse("http://" + uriString); | |
121 } | |
122 return uri.toString(); | |
123 } | |
124 | |
125 /** | |
126 * Initializes the ContentView based on the native tab contents pointer pass
ed in. | |
127 * @param nativeWebContents The pointer to the native tab contents object. | |
128 * @param renderProcessId ID of the corresponding render process host. | |
129 * @param productVersion String containing version description. | |
130 */ | |
131 @SuppressWarnings("unused") | |
132 @CalledByNative | |
133 private void initFromNativeWebContents(WebContents webContents, int renderPr
ocessId, | |
134 String productVersion) { | |
135 Context context = getContext(); | |
136 mContentViewCore = new ContentViewCore(context, productVersion); | |
137 ContentView view = ContentView.createContentView(context, mContentViewCo
re); | |
138 mContentViewCore.initialize(ViewAndroidDelegate.createBasicDelegate(view
), view, | |
139 webContents, mWindow); | |
140 mWebContents = mContentViewCore.getWebContents(); | |
141 mNavigationController = mWebContents.getNavigationController(); | |
142 mRenderProcessId = renderProcessId; | |
143 | |
144 if (getParent() != null) mContentViewCore.onShow(); | |
145 ((FrameLayout) findViewById(R.id.contentview_holder)).addView(view, | |
146 new FrameLayout.LayoutParams( | |
147 FrameLayout.LayoutParams.MATCH_PARENT, | |
148 FrameLayout.LayoutParams.MATCH_PARENT)); | |
149 view.requestFocus(); | |
150 mContentViewRenderView.setCurrentContentViewCore(mContentViewCore); | |
151 | |
152 mWebContentsObserver = new WebContentsObserver(mWebContents) { | |
153 @Override | |
154 public void didStopLoading(String url) { | |
155 Uri intentUri = Uri.parse(mNavigationController | |
156 .getOriginalUrlForVisibleNavigationEntry()); | |
157 Log.v(TAG, "Broadcast ACTION_PAGE_LOADED: scheme=" + intentUri.g
etScheme() | |
158 + ", host=" + intentUri.getHost()); | |
159 LocalBroadcastManager.getInstance(getContext()).sendBroadcast( | |
160 new Intent(ACTION_PAGE_LOADED, intentUri)); | |
161 } | |
162 }; | |
163 } | |
164 | |
165 @CalledByNative | |
166 private ContentVideoViewEmbedder getContentVideoViewEmbedder() { | |
167 return new ActivityContentVideoViewEmbedder((Activity) getContext()); | |
168 } | |
169 | |
170 /** | |
171 * @return The {@link ViewGroup} currently shown by this Shell. | |
172 */ | |
173 public ViewGroup getContentView() { | |
174 return mContentViewCore.getContainerView(); | |
175 } | |
176 | |
177 /** | |
178 * @return The {@link ContentViewCore} currently managing the view shown by
this Shell. | |
179 */ | |
180 public ContentViewCore getContentViewCore() { | |
181 return mContentViewCore; | |
182 } | |
183 | |
184 /** | |
185 * @return The {@link WebContents} managed by this class. | |
186 */ | |
187 public WebContents getWebContents() { | |
188 return mWebContents; | |
189 } | |
190 } | |
OLD | NEW |