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 #include "chromecast/browser/android/cast_window_android.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "chromecast/browser/android/cast_window_manager.h"
10 #include "chromecast/browser/cast_content_window.h"
11 #include "content/public/browser/devtools_agent_host.h"
12 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/render_widget_host_view.h"
17 #include "content/public/common/renderer_preferences.h"
18 #include "jni/CastWindowAndroid_jni.h"
19 #include "ui/gfx/skia_util.h"
21 namespace chromecast
{
26 // The time (in milliseconds) we wait for after a page is closed (i.e.
27 // after an app is stopped) before we delete the corresponding WebContents.
28 const int kWebContentsDestructionDelayInMs
= 50;
33 bool CastWindowAndroid::RegisterJni(JNIEnv
* env
) {
34 return RegisterNativesImpl(env
);
38 CastWindowAndroid
* CastWindowAndroid::CreateNewWindow(
39 content::BrowserContext
* browser_context
,
41 CastWindowAndroid
* window_android
= new CastWindowAndroid(browser_context
);
42 window_android
->Initialize();
45 window_android
->LoadURL(url
);
47 content::RenderWidgetHostView
* rwhv
=
48 window_android
->web_contents_
->GetRenderWidgetHostView();
50 rwhv
->SetBackgroundColor(SK_ColorBLACK
);
53 return window_android
;
56 CastWindowAndroid::CastWindowAndroid(content::BrowserContext
* browser_context
)
57 : browser_context_(browser_context
),
58 content_window_(new CastContentWindow
),
62 void CastWindowAndroid::Initialize() {
64 content_window_
->CreateWebContents(gfx::Size(), browser_context_
);
65 web_contents_
->SetDelegate(this);
66 content::WebContentsObserver::Observe(web_contents_
.get());
68 JNIEnv
* env
= base::android::AttachCurrentThread();
69 window_java_
.Reset(CreateCastWindowView(this));
71 Java_CastWindowAndroid_initFromNativeWebContents(
72 env
, window_java_
.obj(), web_contents_
->GetJavaWebContents().obj(),
73 web_contents_
->GetRenderProcessHost()->GetID());
75 // Enabling hole-punching also requires runtime renderer preference
76 content::RendererPreferences
* prefs
=
77 web_contents_
->GetMutableRendererPrefs();
78 prefs
->use_video_overlay_for_embedded_encrypted_video
= true;
79 prefs
->use_view_overlay_for_all_video
= true;
80 web_contents_
->GetRenderViewHost()->SyncRendererPrefs();
83 CastWindowAndroid::~CastWindowAndroid() {
86 void CastWindowAndroid::Close() {
87 // Close page first, which fires the window.unload event. The WebContents
88 // itself will be destroyed after browser-process has received renderer
89 // notification that the page is closed.
90 web_contents_
->ClosePage();
93 void CastWindowAndroid::Destroy() {
94 // Note: if multiple windows becomes supported, this may close other devtools
96 content::DevToolsAgentHost::DetachAllClients();
97 CloseCastWindowView(window_java_
.obj());
101 void CastWindowAndroid::LoadURL(const GURL
& url
) {
102 content::NavigationController::LoadURLParams
params(url
);
103 params
.transition_type
= ui::PageTransitionFromInt(
104 ui::PAGE_TRANSITION_TYPED
|
105 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR
);
106 web_contents_
->GetController().LoadURLWithParams(params
);
107 web_contents_
->Focus();
110 void CastWindowAndroid::AddNewContents(content::WebContents
* source
,
111 content::WebContents
* new_contents
,
112 WindowOpenDisposition disposition
,
113 const gfx::Rect
& initial_rect
,
122 void CastWindowAndroid::CloseContents(content::WebContents
* source
) {
123 DCHECK_EQ(source
, web_contents_
.get());
125 // We need to delay the deletion of web_contents_ (currently for 50ms) to
126 // give (and guarantee) the renderer enough time to finish 'onunload'
127 // handler (but we don't want to wait any longer than that to delay the
128 // starting of next app).
129 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
131 base::Bind(&CastWindowAndroid::Destroy
, weak_factory_
.GetWeakPtr()),
132 base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs
));
135 bool CastWindowAndroid::CanOverscrollContent() const {
139 bool CastWindowAndroid::AddMessageToConsole(content::WebContents
* source
,
141 const base::string16
& message
,
143 const base::string16
& source_id
) {
147 void CastWindowAndroid::ActivateContents(content::WebContents
* contents
) {
148 DCHECK_EQ(contents
, web_contents_
.get());
149 contents
->GetRenderViewHost()->Focus();
152 void CastWindowAndroid::DeactivateContents(content::WebContents
* contents
) {
153 DCHECK_EQ(contents
, web_contents_
.get());
154 contents
->GetRenderViewHost()->Blur();
157 void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status
) {
158 LOG(ERROR
) << "Render process gone: status=" << status
;
163 } // namespace chromecast