Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chromecast / browser / android / apk / src / org / chromium / chromecast / shell / CastWindowAndroid.java
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.util.Log;
14 import android.view.ViewGroup;
15 import android.widget.FrameLayout;
16 import android.widget.LinearLayout;
17
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.WebContentsObserver;
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;
28
29 /**
30  * Container for the various UI components that make up a shell window.
31  */
32 @JNINamespace("chromecast::shell")
33 public class CastWindowAndroid extends LinearLayout {
34     public static final String TAG = "CastWindowAndroid";
35
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";
39
40     private ContentViewCore mContentViewCore;
41     private ContentViewRenderView mContentViewRenderView;
42     private NavigationController mNavigationController;
43     private WebContents mWebContents;
44     private WebContentsObserver mWebContentsObserver;
45     private WindowAndroid mWindow;
46
47     /**
48      * Constructor for inflating via XML.
49      */
50     public CastWindowAndroid(Context context, AttributeSet attrs) {
51         super(context, attrs);
52     }
53
54     /**
55      * Set the SurfaceView being renderered to as soon as it is available.
56      */
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);
62             }
63         } else {
64             contentViewHolder.addView(contentViewRenderView,
65                     new FrameLayout.LayoutParams(
66                             FrameLayout.LayoutParams.MATCH_PARENT,
67                             FrameLayout.LayoutParams.MATCH_PARENT));
68         }
69         mContentViewRenderView = contentViewRenderView;
70     }
71
72     /**
73      * @param window The owning window for this shell.
74      */
75     public void setWindow(WindowAndroid window) {
76         mWindow = window;
77     }
78
79     /**
80      * Loads an URL.  This will perform minimal amounts of sanitizing of the URL to attempt to
81      * make it valid.
82      *
83      * @param url The URL to be loaded by the shell.
84      */
85     public void loadUrl(String url) {
86         if (url == null) return;
87
88         if (TextUtils.equals(url, mWebContents.getUrl())) {
89             mNavigationController.reload(true);
90         } else {
91             mNavigationController.loadUrl(new LoadUrlParams(normalizeUrl(url)));
92         }
93
94         // TODO(aurimas): Remove this when crbug.com/174541 is fixed.
95         mContentViewCore.getContainerView().clearFocus();
96         mContentViewCore.getContainerView().requestFocus();
97     }
98
99     /**
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.
103      */
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);
109         }
110         return uri.toString();
111     }
112
113     /**
114      * Initializes the ContentView based on the native tab contents pointer passed in.
115      * @param nativeWebContents The pointer to the native tab contents object.
116      */
117     @SuppressWarnings("unused")
118     @CalledByNative
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();
126
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));
132         view.requestFocus();
133         mContentViewRenderView.setCurrentContentViewCore(mContentViewCore);
134
135         mWebContentsObserver = new WebContentsObserver(mWebContents) {
136             @Override
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));
144             }
145         };
146     }
147
148     /**
149      * @return The {@link ViewGroup} currently shown by this Shell.
150      */
151     public ViewGroup getContentView() {
152         return mContentViewCore.getContainerView();
153     }
154
155     /**
156      * @return The {@link ContentViewCore} currently managing the view shown by this Shell.
157      */
158     public ContentViewCore getContentViewCore() {
159         return mContentViewCore;
160     }
161 }