ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chromecast / browser / android / cast_window_android.cc
blobc95bd375a5de3db92923c43158a985e0e91c04eb
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/message_loop/message_loop_proxy.h"
8 #include "chromecast/browser/android/cast_window_manager.h"
9 #include "chromecast/browser/cast_content_window.h"
10 #include "content/public/browser/devtools_agent_host.h"
11 #include "content/public/browser/navigation_controller.h"
12 #include "content/public/browser/navigation_entry.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/render_widget_host_view.h"
16 #include "content/public/common/renderer_preferences.h"
17 #include "jni/CastWindowAndroid_jni.h"
18 #include "ui/gfx/skia_util.h"
20 namespace chromecast {
21 namespace shell {
23 namespace {
25 // The time (in milliseconds) we wait for after a page is closed (i.e.
26 // after an app is stopped) before we delete the corresponding WebContents.
27 const int kWebContentsDestructionDelayInMs = 50;
29 } // namespace
31 // static
32 bool CastWindowAndroid::RegisterJni(JNIEnv* env) {
33 return RegisterNativesImpl(env);
36 // static
37 CastWindowAndroid* CastWindowAndroid::CreateNewWindow(
38 content::BrowserContext* browser_context,
39 const GURL& url) {
40 CastWindowAndroid* window_android = new CastWindowAndroid(browser_context);
41 window_android->Initialize();
43 if (!url.is_empty())
44 window_android->LoadURL(url);
46 content::RenderWidgetHostView* rwhv =
47 window_android->web_contents_->GetRenderWidgetHostView();
48 if (rwhv) {
49 rwhv->SetBackgroundColor(SK_ColorBLACK);
52 return window_android;
55 CastWindowAndroid::CastWindowAndroid(content::BrowserContext* browser_context)
56 : browser_context_(browser_context),
57 content_window_(new CastContentWindow),
58 weak_factory_(this) {
61 void CastWindowAndroid::Initialize() {
62 web_contents_ =
63 content_window_->CreateWebContents(gfx::Size(), browser_context_);
64 web_contents_->SetDelegate(this);
65 content::WebContentsObserver::Observe(web_contents_.get());
67 JNIEnv* env = base::android::AttachCurrentThread();
68 window_java_.Reset(CreateCastWindowView(this));
70 Java_CastWindowAndroid_initFromNativeWebContents(
71 env, window_java_.obj(), web_contents_->GetJavaWebContents().obj(),
72 web_contents_->GetRenderProcessHost()->GetID());
74 // Enabling hole-punching also requires runtime renderer preference
75 web_contents_->GetMutableRendererPrefs()->
76 use_video_overlay_for_embedded_encrypted_video = true;
77 web_contents_->GetRenderViewHost()->SyncRendererPrefs();
80 CastWindowAndroid::~CastWindowAndroid() {
83 void CastWindowAndroid::Close() {
84 // Close page first, which fires the window.unload event. The WebContents
85 // itself will be destroyed after browser-process has received renderer
86 // notification that the page is closed.
87 web_contents_->GetRenderViewHost()->ClosePage();
90 void CastWindowAndroid::Destroy() {
91 // Note: if multiple windows becomes supported, this may close other devtools
92 // sessions.
93 content::DevToolsAgentHost::DetachAllClients();
94 CloseCastWindowView(window_java_.obj());
95 delete this;
98 void CastWindowAndroid::LoadURL(const GURL& url) {
99 content::NavigationController::LoadURLParams params(url);
100 params.transition_type = ui::PageTransitionFromInt(
101 ui::PAGE_TRANSITION_TYPED |
102 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
103 web_contents_->GetController().LoadURLWithParams(params);
104 web_contents_->Focus();
107 void CastWindowAndroid::AddNewContents(content::WebContents* source,
108 content::WebContents* new_contents,
109 WindowOpenDisposition disposition,
110 const gfx::Rect& initial_rect,
111 bool user_gesture,
112 bool* was_blocked) {
113 NOTIMPLEMENTED();
114 if (was_blocked) {
115 *was_blocked = true;
119 void CastWindowAndroid::CloseContents(content::WebContents* source) {
120 DCHECK_EQ(source, web_contents_.get());
122 // We need to delay the deletion of web_contents_ (currently for 50ms) to
123 // give (and guarantee) the renderer enough time to finish 'onunload'
124 // handler (but we don't want to wait any longer than that to delay the
125 // starting of next app).
126 base::MessageLoopProxy::current()->PostDelayedTask(
127 FROM_HERE,
128 base::Bind(&CastWindowAndroid::Destroy, weak_factory_.GetWeakPtr()),
129 base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs));
132 bool CastWindowAndroid::CanOverscrollContent() const {
133 return false;
136 bool CastWindowAndroid::AddMessageToConsole(content::WebContents* source,
137 int32 level,
138 const base::string16& message,
139 int32 line_no,
140 const base::string16& source_id) {
141 return false;
144 void CastWindowAndroid::ActivateContents(content::WebContents* contents) {
145 DCHECK_EQ(contents, web_contents_.get());
146 contents->GetRenderViewHost()->Focus();
149 void CastWindowAndroid::DeactivateContents(content::WebContents* contents) {
150 DCHECK_EQ(contents, web_contents_.get());
151 contents->GetRenderViewHost()->Blur();
154 void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status) {
155 LOG(ERROR) << "Render process gone: status=" << status;
156 Destroy();
159 } // namespace shell
160 } // namespace chromecast