OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromecast/shell/browser/android/cast_window_android.h" | 5 #include "chromecast/shell/browser/android/cast_window_android.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop_proxy.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "chromecast/shell/browser/android/cast_window_manager.h" | 9 #include "chromecast/shell/browser/android/cast_window_manager.h" |
10 #include "content/public/browser/devtools_agent_host.h" | 10 #include "content/public/browser/devtools_agent_host.h" |
11 #include "content/public/browser/navigation_controller.h" | 11 #include "content/public/browser/navigation_controller.h" |
12 #include "content/public/browser/navigation_entry.h" | 12 #include "content/public/browser/navigation_entry.h" |
13 #include "content/public/browser/render_view_host.h" | 13 #include "content/public/browser/render_view_host.h" |
14 #include "content/public/common/renderer_preferences.h" | 14 #include "content/public/common/renderer_preferences.h" |
15 #include "jni/CastWindowAndroid_jni.h" | 15 #include "jni/CastWindowAndroid_jni.h" |
16 | 16 |
17 namespace chromecast { | 17 namespace chromecast { |
18 namespace shell { | 18 namespace shell { |
19 | 19 |
| 20 namespace { |
| 21 |
| 22 // The time (in milliseconds) we wait for after a page is closed (i.e. |
| 23 // after an app is stopped) before we delete the corresponding WebContents. |
| 24 const int kWebContentsDestructionDelayInMs = 50; |
| 25 |
| 26 } // namespace |
| 27 |
20 // static | 28 // static |
21 bool CastWindowAndroid::RegisterJni(JNIEnv* env) { | 29 bool CastWindowAndroid::RegisterJni(JNIEnv* env) { |
22 return RegisterNativesImpl(env); | 30 return RegisterNativesImpl(env); |
23 } | 31 } |
24 | 32 |
25 CastWindowAndroid::CastWindowAndroid(content::WebContents* web_contents) | 33 CastWindowAndroid::CastWindowAndroid(content::WebContents* web_contents) |
26 : content::WebContentsObserver(web_contents) { | 34 : content::WebContentsObserver(web_contents), |
| 35 weak_factory_(this) { |
27 } | 36 } |
28 | 37 |
29 CastWindowAndroid::~CastWindowAndroid() { | 38 CastWindowAndroid::~CastWindowAndroid() { |
30 } | 39 } |
31 | 40 |
32 // static | 41 // static |
33 CastWindowAndroid* CastWindowAndroid::CreateNewWindow( | 42 CastWindowAndroid* CastWindowAndroid::CreateNewWindow( |
34 content::BrowserContext* browser_context, | 43 content::BrowserContext* browser_context, |
35 const GURL& url) { | 44 const GURL& url) { |
36 content::WebContents::CreateParams create_params(browser_context); | 45 content::WebContents::CreateParams create_params(browser_context); |
(...skipping 27 matching lines...) Expand all Loading... |
64 | 73 |
65 // Enabling hole-punching also requires runtime renderer preference | 74 // Enabling hole-punching also requires runtime renderer preference |
66 web_contents->GetMutableRendererPrefs()-> | 75 web_contents->GetMutableRendererPrefs()-> |
67 use_video_overlay_for_embedded_encrypted_video = true; | 76 use_video_overlay_for_embedded_encrypted_video = true; |
68 web_contents->GetRenderViewHost()->SyncRendererPrefs(); | 77 web_contents->GetRenderViewHost()->SyncRendererPrefs(); |
69 | 78 |
70 return shell; | 79 return shell; |
71 } | 80 } |
72 | 81 |
73 void CastWindowAndroid::Close() { | 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_->GetRenderViewHost()->ClosePage(); |
| 87 } |
| 88 |
| 89 void CastWindowAndroid::Destroy() { |
74 // Note: if multiple windows becomes supported, this may close other devtools | 90 // Note: if multiple windows becomes supported, this may close other devtools |
75 // sessions. | 91 // sessions. |
76 content::DevToolsAgentHost::DetachAllClients(); | 92 content::DevToolsAgentHost::DetachAllClients(); |
77 CloseCastWindowView(java_object_.obj()); | 93 CloseCastWindowView(java_object_.obj()); |
78 delete this; | 94 delete this; |
79 } | 95 } |
80 | 96 |
81 void CastWindowAndroid::LoadURL(const GURL& url) { | 97 void CastWindowAndroid::LoadURL(const GURL& url) { |
82 content::NavigationController::LoadURLParams params(url); | 98 content::NavigationController::LoadURLParams params(url); |
83 params.transition_type = ui::PageTransitionFromInt( | 99 params.transition_type = ui::PageTransitionFromInt( |
(...skipping 10 matching lines...) Expand all Loading... |
94 bool user_gesture, | 110 bool user_gesture, |
95 bool* was_blocked) { | 111 bool* was_blocked) { |
96 NOTIMPLEMENTED(); | 112 NOTIMPLEMENTED(); |
97 if (was_blocked) { | 113 if (was_blocked) { |
98 *was_blocked = true; | 114 *was_blocked = true; |
99 } | 115 } |
100 } | 116 } |
101 | 117 |
102 void CastWindowAndroid::CloseContents(content::WebContents* source) { | 118 void CastWindowAndroid::CloseContents(content::WebContents* source) { |
103 DCHECK_EQ(source, web_contents_.get()); | 119 DCHECK_EQ(source, web_contents_.get()); |
104 Close(); | 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 |
| 126 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { |
| 127 // When shutting down in a test context, the last remaining WebContents |
| 128 // is torn down at browser-thread shutdown time. Call Destroy directly to |
| 129 // avoid losing the last posted task to delete this object. |
| 130 // TODO(gunsch): This could probably be avoided by using a |
| 131 // CompletionCallback in StopCurrentApp to wait until the app is completely |
| 132 // stopped. This might require a separate message loop and might only be |
| 133 // appropriate for test contexts or during shutdown, since it triggers a |
| 134 // wait on the main thread. |
| 135 Destroy(); |
| 136 return; |
| 137 } |
| 138 |
| 139 base::MessageLoopProxy::current()->PostDelayedTask( |
| 140 FROM_HERE, |
| 141 base::Bind(&CastWindowAndroid::Destroy, weak_factory_.GetWeakPtr()), |
| 142 base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs)); |
105 } | 143 } |
106 | 144 |
107 bool CastWindowAndroid::CanOverscrollContent() const { | 145 bool CastWindowAndroid::CanOverscrollContent() const { |
108 return false; | 146 return false; |
109 } | 147 } |
110 | 148 |
111 bool CastWindowAndroid::AddMessageToConsole(content::WebContents* source, | 149 bool CastWindowAndroid::AddMessageToConsole(content::WebContents* source, |
112 int32 level, | 150 int32 level, |
113 const base::string16& message, | 151 const base::string16& message, |
114 int32 line_no, | 152 int32 line_no, |
115 const base::string16& source_id) { | 153 const base::string16& source_id) { |
116 return false; | 154 return false; |
117 } | 155 } |
118 | 156 |
119 void CastWindowAndroid::ActivateContents(content::WebContents* contents) { | 157 void CastWindowAndroid::ActivateContents(content::WebContents* contents) { |
120 DCHECK_EQ(contents, web_contents_.get()); | 158 DCHECK_EQ(contents, web_contents_.get()); |
121 contents->GetRenderViewHost()->Focus(); | 159 contents->GetRenderViewHost()->Focus(); |
122 } | 160 } |
123 | 161 |
124 void CastWindowAndroid::DeactivateContents(content::WebContents* contents) { | 162 void CastWindowAndroid::DeactivateContents(content::WebContents* contents) { |
125 DCHECK_EQ(contents, web_contents_.get()); | 163 DCHECK_EQ(contents, web_contents_.get()); |
126 contents->GetRenderViewHost()->Blur(); | 164 contents->GetRenderViewHost()->Blur(); |
127 } | 165 } |
128 | 166 |
129 void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status) { | 167 void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status) { |
130 LOG(ERROR) << "Render process gone: status=" << status; | 168 LOG(ERROR) << "Render process gone: status=" << status; |
131 Close(); | 169 Destroy(); |
132 } | 170 } |
133 | 171 |
134 } // namespace shell | 172 } // namespace shell |
135 } // namespace chromecast | 173 } // namespace chromecast |
OLD | NEW |