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.
5 package org.chromium.chromecast.shell;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.net.Uri;
10 import android.support.v4.content.LocalBroadcastManager;
11 import android.text.TextUtils;
12 import android.util.AttributeSet;
13 import android.util.Log;
14 import android.view.ViewGroup;
15 import android.widget.FrameLayout;
16 import android.widget.LinearLayout;
18 import org.chromium.base.CalledByNative;
19 import org.chromium.base.JNINamespace;
20 import org.chromium.content.browser.ContentView;
21 import org.chromium.content.browser.ContentViewCore;
22 import org.chromium.content.browser.ContentViewRenderView;
23 import org.chromium.content.browser.WebContentsObserverAndroid;
24 import org.chromium.content_public.browser.LoadUrlParams;
25 import org.chromium.content_public.browser.NavigationController;
26 import org.chromium.content_public.browser.WebContents;
27 import org.chromium.ui.base.WindowAndroid;
30 * Container for the various UI components that make up a shell window.
32 @JNINamespace("chromecast::shell")
33 public class CastWindowAndroid extends LinearLayout {
34 public static final String TAG = "CastWindowAndroid";
36 public static final String ACTION_PAGE_LOADED = "castPageLoaded";
37 public static final String ACTION_ENABLE_DEV_TOOLS = "castEnableDevTools";
38 public static final String ACTION_DISABLE_DEV_TOOLS = "castDisableDevTools";
40 private ContentViewCore mContentViewCore;
41 private ContentViewRenderView mContentViewRenderView;
42 private NavigationController mNavigationController;
43 private WebContents mWebContents;
44 private WebContentsObserverAndroid mWebContentsObserver;
45 private WindowAndroid mWindow;
48 * Constructor for inflating via XML.
50 public CastWindowAndroid(Context context, AttributeSet attrs) {
51 super(context, attrs);
55 * Set the SurfaceView being renderered to as soon as it is available.
57 public void setContentViewRenderView(ContentViewRenderView contentViewRenderView) {
58 FrameLayout contentViewHolder = (FrameLayout) findViewById(R.id.contentview_holder);
59 if (contentViewRenderView == null) {
60 if (mContentViewRenderView != null) {
61 contentViewHolder.removeView(mContentViewRenderView);
64 contentViewHolder.addView(contentViewRenderView,
65 new FrameLayout.LayoutParams(
66 FrameLayout.LayoutParams.MATCH_PARENT,
67 FrameLayout.LayoutParams.MATCH_PARENT));
69 mContentViewRenderView = contentViewRenderView;
73 * @param window The owning window for this shell.
75 public void setWindow(WindowAndroid window) {
80 * Loads an URL. This will perform minimal amounts of sanitizing of the URL to attempt to
83 * @param url The URL to be loaded by the shell.
85 public void loadUrl(String url) {
86 if (url == null) return;
88 if (TextUtils.equals(url, mWebContents.getUrl())) {
89 mNavigationController.reload(true);
91 mNavigationController.loadUrl(new LoadUrlParams(normalizeUrl(url)));
94 // TODO(aurimas): Remove this when crbug.com/174541 is fixed.
95 mContentViewCore.getContainerView().clearFocus();
96 mContentViewCore.getContainerView().requestFocus();
100 * Given a URI String, performs minimal normalization to attempt to build a usable URL from it.
101 * @param uriString The passed-in path to be normalized.
102 * @return The normalized URL, as a string.
104 private static String normalizeUrl(String uriString) {
105 if (uriString == null) return uriString;
106 Uri uri = Uri.parse(uriString);
107 if (uri.getScheme() == null) {
108 uri = Uri.parse("http://" + uriString);
110 return uri.toString();
114 * Initializes the ContentView based on the native tab contents pointer passed in.
115 * @param nativeWebContents The pointer to the native tab contents object.
117 @SuppressWarnings("unused")
119 private void initFromNativeWebContents(long nativeWebContents) {
120 Context context = getContext();
121 mContentViewCore = new ContentViewCore(context);
122 ContentView view = ContentView.newInstance(context, mContentViewCore);
123 mContentViewCore.initialize(view, view, nativeWebContents, mWindow);
124 mWebContents = mContentViewCore.getWebContents();
125 mNavigationController = mWebContents.getNavigationController();
127 if (getParent() != null) mContentViewCore.onShow();
128 ((FrameLayout) findViewById(R.id.contentview_holder)).addView(view,
129 new FrameLayout.LayoutParams(
130 FrameLayout.LayoutParams.MATCH_PARENT,
131 FrameLayout.LayoutParams.MATCH_PARENT));
133 mContentViewRenderView.setCurrentContentViewCore(mContentViewCore);
135 mWebContentsObserver = new WebContentsObserverAndroid(mWebContents) {
137 public void didStopLoading(String url) {
138 Uri intentUri = Uri.parse(mNavigationController
139 .getOriginalUrlForVisibleNavigationEntry());
140 Log.v(TAG, "Broadcast ACTION_PAGE_LOADED: scheme=" + intentUri.getScheme()
141 + ", host=" + intentUri.getHost());
142 LocalBroadcastManager.getInstance(getContext()).sendBroadcast(
143 new Intent(ACTION_PAGE_LOADED, intentUri));
149 * @return The {@link ViewGroup} currently shown by this Shell.
151 public ViewGroup getContentView() {
152 return mContentViewCore.getContainerView();
156 * @return The {@link ContentViewCore} currently managing the view shown by this Shell.
158 public ContentViewCore getContentViewCore() {
159 return mContentViewCore;