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 #include "chromecast/browser/android/cast_window_android.h" | |
6 | |
7 #include "base/single_thread_task_runner.h" | |
8 #include "base/threading/thread_task_runner_handle.h" | |
9 #include "chromecast/base/version.h" | |
10 #include "chromecast/browser/android/cast_window_manager.h" | |
11 #include "chromecast/browser/cast_content_window.h" | |
12 #include "content/public/browser/devtools_agent_host.h" | |
13 #include "content/public/browser/navigation_controller.h" | |
14 #include "content/public/browser/navigation_entry.h" | |
15 #include "content/public/browser/render_process_host.h" | |
16 #include "content/public/browser/render_view_host.h" | |
17 #include "content/public/browser/render_widget_host.h" | |
18 #include "content/public/browser/render_widget_host_view.h" | |
19 #include "content/public/common/renderer_preferences.h" | |
20 #include "jni/CastWindowAndroid_jni.h" | |
21 #include "ui/gfx/skia_util.h" | |
22 | |
23 namespace chromecast { | |
24 namespace shell { | |
25 | |
26 namespace { | |
27 | |
28 // The time (in milliseconds) we wait for after a page is closed (i.e. | |
29 // after an app is stopped) before we delete the corresponding WebContents. | |
30 const int kWebContentsDestructionDelayInMs = 50; | |
31 | |
32 } // namespace | |
33 | |
34 // static | |
35 CastWindowAndroid* CastWindowAndroid::CreateNewWindow( | |
36 content::BrowserContext* browser_context, | |
37 const GURL& url) { | |
38 CastWindowAndroid* window_android = new CastWindowAndroid(browser_context); | |
39 window_android->Initialize(); | |
40 | |
41 if (!url.is_empty()) | |
42 window_android->LoadURL(url); | |
43 | |
44 content::RenderWidgetHostView* rwhv = | |
45 window_android->web_contents_->GetRenderWidgetHostView(); | |
46 if (rwhv) { | |
47 rwhv->SetBackgroundColor(SK_ColorBLACK); | |
48 } | |
49 | |
50 return window_android; | |
51 } | |
52 | |
53 CastWindowAndroid::CastWindowAndroid(content::BrowserContext* browser_context) | |
54 : browser_context_(browser_context), | |
55 content_window_(new CastContentWindow), | |
56 weak_factory_(this) { | |
57 } | |
58 | |
59 void CastWindowAndroid::Initialize() { | |
60 web_contents_ = content_window_->CreateWebContents(browser_context_); | |
61 web_contents_->SetDelegate(this); | |
62 content::WebContentsObserver::Observe(web_contents_.get()); | |
63 | |
64 JNIEnv* env = base::android::AttachCurrentThread(); | |
65 window_java_.Reset(CreateCastWindowView(this)); | |
66 | |
67 Java_CastWindowAndroid_initFromNativeWebContents( | |
68 env, window_java_, web_contents_->GetJavaWebContents(), | |
69 web_contents_->GetRenderProcessHost()->GetID(), | |
70 base::android::ConvertUTF8ToJavaString(env, PRODUCT_VERSION)); | |
71 | |
72 // Enabling hole-punching also requires runtime renderer preference | |
73 content::RendererPreferences* prefs = | |
74 web_contents_->GetMutableRendererPrefs(); | |
75 prefs->use_video_overlay_for_embedded_encrypted_video = true; | |
76 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); | |
77 } | |
78 | |
79 CastWindowAndroid::~CastWindowAndroid() { | |
80 } | |
81 | |
82 void CastWindowAndroid::Close() { | |
83 // Close page first, which fires the window.unload event. The WebContents | |
84 // itself will be destroyed after browser-process has received renderer | |
85 // notification that the page is closed. | |
86 web_contents_->ClosePage(); | |
87 } | |
88 | |
89 void CastWindowAndroid::Destroy() { | |
90 // Note: if multiple windows becomes supported, this may close other devtools | |
91 // sessions. | |
92 content::DevToolsAgentHost::DetachAllClients(); | |
93 CloseCastWindowView(window_java_.obj()); | |
94 delete this; | |
95 } | |
96 | |
97 void CastWindowAndroid::LoadURL(const GURL& url) { | |
98 content::NavigationController::LoadURLParams params(url); | |
99 params.transition_type = ui::PageTransitionFromInt( | |
100 ui::PAGE_TRANSITION_TYPED | | |
101 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | |
102 web_contents_->GetController().LoadURLWithParams(params); | |
103 web_contents_->Focus(); | |
104 } | |
105 | |
106 void CastWindowAndroid::AddNewContents(content::WebContents* source, | |
107 content::WebContents* new_contents, | |
108 WindowOpenDisposition disposition, | |
109 const gfx::Rect& initial_rect, | |
110 bool user_gesture, | |
111 bool* was_blocked) { | |
112 NOTIMPLEMENTED(); | |
113 if (was_blocked) { | |
114 *was_blocked = true; | |
115 } | |
116 } | |
117 | |
118 void CastWindowAndroid::CloseContents(content::WebContents* source) { | |
119 DCHECK_EQ(source, web_contents_.get()); | |
120 | |
121 // We need to delay the deletion of web_contents_ (currently for 50ms) to | |
122 // give (and guarantee) the renderer enough time to finish 'onunload' | |
123 // handler (but we don't want to wait any longer than that to delay the | |
124 // starting of next app). | |
125 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
126 FROM_HERE, | |
127 base::Bind(&CastWindowAndroid::Destroy, weak_factory_.GetWeakPtr()), | |
128 base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs)); | |
129 } | |
130 | |
131 bool CastWindowAndroid::CanOverscrollContent() const { | |
132 return false; | |
133 } | |
134 | |
135 bool CastWindowAndroid::DidAddMessageToConsole( | |
136 content::WebContents* source, | |
137 int32_t level, | |
138 const base::string16& message, | |
139 int32_t line_no, | |
140 const base::string16& source_id) { | |
141 return false; | |
142 } | |
143 | |
144 void CastWindowAndroid::ActivateContents(content::WebContents* contents) { | |
145 DCHECK_EQ(contents, web_contents_.get()); | |
146 contents->GetRenderViewHost()->GetWidget()->Focus(); | |
147 } | |
148 | |
149 base::android::ScopedJavaLocalRef<jobject> | |
150 CastWindowAndroid::GetContentVideoViewEmbedder() { | |
151 JNIEnv* env = base::android::AttachCurrentThread(); | |
152 return Java_CastWindowAndroid_getContentVideoViewEmbedder(env, window_java_); | |
153 } | |
154 | |
155 void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status) { | |
156 LOG(ERROR) << "Render process gone: status=" << status; | |
157 Destroy(); | |
158 } | |
159 | |
160 } // namespace shell | |
161 } // namespace chromecast | |
OLD | NEW |