Chromium Code Reviews| 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.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.view.ViewGroup; | |
| 14 import android.widget.FrameLayout; | |
| 15 import android.widget.LinearLayout; | |
| 16 | |
| 17 import org.chromium.base.CalledByNative; | |
| 18 import org.chromium.base.JNINamespace; | |
| 19 import org.chromium.content.browser.ContentView; | |
| 20 import org.chromium.content.browser.ContentViewCore; | |
| 21 import org.chromium.content.browser.ContentViewRenderView; | |
| 22 import org.chromium.content.browser.LoadUrlParams; | |
| 23 import org.chromium.content.browser.WebContentsObserverAndroid; | |
| 24 import org.chromium.ui.base.WindowAndroid; | |
| 25 | |
| 26 /** | |
| 27 * Container for the various UI components that make up a shell window. | |
| 28 */ | |
| 29 @JNINamespace("chromecast::shell") | |
| 30 public class CastWindowAndroid extends LinearLayout { | |
| 31 public static final String TAG = "CastWindowAndroid"; | |
| 32 | |
| 33 public static final String ACTION_PAGE_LOADED = "castPageLoaded"; | |
| 34 public static final String ACTION_ENABLE_DEV_TOOLS = "castEnableDevTools"; | |
| 35 public static final String ACTION_DISABLE_DEV_TOOLS = "castDisableDevTools"; | |
| 36 | |
| 37 private ContentViewCore mContentViewCore; | |
| 38 private ContentViewRenderView mContentViewRenderView; | |
| 39 private WebContentsObserverAndroid mWebContentsObserver; | |
| 40 private WindowAndroid mWindow; | |
| 41 | |
| 42 /** | |
| 43 * Constructor for inflating via XML. | |
| 44 */ | |
| 45 public CastWindowAndroid(Context context, AttributeSet attrs) { | |
| 46 super(context, attrs); | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Set the SurfaceView being renderered to as soon as it is available. | |
| 51 */ | |
| 52 public void setContentViewRenderView(ContentViewRenderView contentViewRender View) { | |
| 53 FrameLayout contentViewHolder = (FrameLayout) findViewById(R.id.contentv iew_holder); | |
| 54 if (contentViewRenderView == null) { | |
| 55 if (mContentViewRenderView != null) { | |
| 56 contentViewHolder.removeView(mContentViewRenderView); | |
| 57 } | |
| 58 } else { | |
| 59 contentViewHolder.addView(contentViewRenderView, | |
| 60 new FrameLayout.LayoutParams( | |
| 61 FrameLayout.LayoutParams.MATCH_PARENT, | |
| 62 FrameLayout.LayoutParams.MATCH_PARENT)); | |
| 63 } | |
| 64 mContentViewRenderView = contentViewRenderView; | |
| 65 } | |
| 66 | |
| 67 /** | |
| 68 * @param window The owning window for this shell. | |
| 69 */ | |
| 70 public void setWindow(WindowAndroid window) { | |
| 71 mWindow = window; | |
| 72 } | |
| 73 | |
| 74 /** | |
| 75 * Loads an URL. This will perform minimal amounts of sanitizing of the URL to attempt to | |
| 76 * make it valid. | |
| 77 * | |
| 78 * @param url The URL to be loaded by the shell. | |
| 79 */ | |
| 80 public void loadUrl(String url) { | |
| 81 if (url == null) return; | |
| 82 | |
| 83 if (TextUtils.equals(url, mContentViewCore.getUrl())) { | |
| 84 mContentViewCore.reload(true); | |
| 85 } else { | |
| 86 mContentViewCore.loadUrl(new LoadUrlParams(sanitizeUrl(url))); | |
| 87 } | |
| 88 | |
| 89 // TODO(aurimas): Remove this when crbug.com/174541 is fixed. | |
| 90 mContentViewCore.getContainerView().clearFocus(); | |
| 91 mContentViewCore.getContainerView().requestFocus(); | |
| 92 } | |
| 93 | |
| 94 /** | |
| 95 * Given an URL, this performs minimal sanitizing to ensure it will be valid . | |
| 96 * @param url The url to be sanitized. | |
| 97 * @return The sanitized URL. | |
| 98 */ | |
| 99 public static String sanitizeUrl(String url) { | |
|
Yaron
2014/08/20 18:11:02
This looks to be copied from ContentShell. I think
gunsch
2014/08/22 22:44:30
Done.
| |
| 100 if (url == null) return url; | |
| 101 if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url; | |
| 102 return url; | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Initializes the ContentView based on the native tab contents pointer pass ed in. | |
| 107 * @param nativeWebContents The pointer to the native tab contents object. | |
| 108 */ | |
| 109 @SuppressWarnings("unused") | |
| 110 @CalledByNative | |
| 111 private void initFromNativeTabContents(long nativeWebContents) { | |
| 112 Context context = getContext(); | |
| 113 mContentViewCore = new ContentViewCore(context); | |
| 114 ContentView view = ContentView.newInstance(context, mContentViewCore); | |
| 115 mContentViewCore.initialize(view, view, nativeWebContents, mWindow); | |
| 116 | |
| 117 if (getParent() != null) mContentViewCore.onShow(); | |
| 118 ((FrameLayout) findViewById(R.id.contentview_holder)).addView(view, | |
| 119 new FrameLayout.LayoutParams( | |
| 120 FrameLayout.LayoutParams.MATCH_PARENT, | |
| 121 FrameLayout.LayoutParams.MATCH_PARENT)); | |
| 122 view.requestFocus(); | |
| 123 mContentViewRenderView.setCurrentContentViewCore(mContentViewCore); | |
| 124 | |
| 125 mWebContentsObserver = new WebContentsObserverAndroid(mContentViewCore.g etWebContents()) { | |
| 126 @Override | |
| 127 public void didStopLoading(String url) { | |
| 128 Uri intentUri = Uri.parse(mContentViewCore | |
| 129 .getOriginalUrlForActiveNavigationEntry()); | |
| 130 Log.v(TAG, "Broadcast ACTION_PAGE_LOADED: scheme=" + intentUri.g etScheme() | |
| 131 + ", host=" + intentUri.getHost()); | |
| 132 LocalBroadcastManager.getInstance(getContext()).sendBroadcast( | |
| 133 new Intent(ACTION_PAGE_LOADED, intentUri)); | |
| 134 } | |
| 135 }; | |
| 136 } | |
| 137 | |
| 138 /** | |
| 139 * @return The {@link ViewGroup} currently shown by this Shell. | |
| 140 */ | |
| 141 public ViewGroup getContentView() { | |
| 142 return mContentViewCore.getContainerView(); | |
| 143 } | |
| 144 | |
| 145 /** | |
| 146 * @return The {@link ContentViewCore} currently managing the view shown by this Shell. | |
| 147 */ | |
| 148 public ContentViewCore getContentViewCore() { | |
| 149 return mContentViewCore; | |
| 150 } | |
| 151 } | |
| OLD | NEW |