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 content first. This fires window.unload event and | |
84 // CastWindowAndroid::CloseContents(). | |
lcwu1
2014/09/25 19:10:54
This should probably be something like this:
"Clos
gunsch
2014/09/25 19:18:14
Done.
| |
85 web_contents_->GetRenderViewHost()->ClosePage(); | |
86 } | |
87 | |
88 void CastWindowAndroid::Destroy() { | |
74 // Note: if multiple windows becomes supported, this may close other devtools | 89 // Note: if multiple windows becomes supported, this may close other devtools |
75 // sessions. | 90 // sessions. |
76 content::DevToolsAgentHost::DetachAllClients(); | 91 content::DevToolsAgentHost::DetachAllClients(); |
77 CloseCastWindowView(java_object_.obj()); | 92 CloseCastWindowView(java_object_.obj()); |
78 delete this; | 93 delete this; |
79 } | 94 } |
80 | 95 |
81 void CastWindowAndroid::LoadURL(const GURL& url) { | 96 void CastWindowAndroid::LoadURL(const GURL& url) { |
82 content::NavigationController::LoadURLParams params(url); | 97 content::NavigationController::LoadURLParams params(url); |
83 params.transition_type = ui::PageTransitionFromInt( | 98 params.transition_type = ui::PageTransitionFromInt( |
(...skipping 10 matching lines...) Expand all Loading... | |
94 bool user_gesture, | 109 bool user_gesture, |
95 bool* was_blocked) { | 110 bool* was_blocked) { |
96 NOTIMPLEMENTED(); | 111 NOTIMPLEMENTED(); |
97 if (was_blocked) { | 112 if (was_blocked) { |
98 *was_blocked = true; | 113 *was_blocked = true; |
99 } | 114 } |
100 } | 115 } |
101 | 116 |
102 void CastWindowAndroid::CloseContents(content::WebContents* source) { | 117 void CastWindowAndroid::CloseContents(content::WebContents* source) { |
103 DCHECK_EQ(source, web_contents_.get()); | 118 DCHECK_EQ(source, web_contents_.get()); |
104 Close(); | 119 |
120 // We need to delay the deletion of web_contents_ (currently for 50ms) to | |
121 // give (and guarantee) the renderer enough time to finish 'onunload' | |
122 // handler (but we don't want to wait any longer than that to delay the | |
123 // starting of next app). | |
124 | |
125 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { | |
126 // When shutting down in a test context, the last remaining WebContents | |
127 // is torn down at browser-thread shutdown time. Call Destroy directly to | |
128 // avoid losing the last posted task to delete this object. | |
129 // TODO(gunsch): This could probably be avoided by using a | |
130 // CompletionCallback in StopCurrentApp to wait until the app is completely | |
131 // stopped. This might require a separate message loop and might only be | |
132 // appropriate for test contexts or during shutdown, since it triggers a | |
133 // wait on the main thread. | |
134 Destroy(); | |
135 return; | |
136 } | |
137 | |
138 base::MessageLoopProxy::current()->PostDelayedTask( | |
139 FROM_HERE, | |
140 base::Bind(&CastWindowAndroid::Destroy, weak_factory_.GetWeakPtr()), | |
141 base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs)); | |
105 } | 142 } |
106 | 143 |
107 bool CastWindowAndroid::CanOverscrollContent() const { | 144 bool CastWindowAndroid::CanOverscrollContent() const { |
108 return false; | 145 return false; |
109 } | 146 } |
110 | 147 |
111 bool CastWindowAndroid::AddMessageToConsole(content::WebContents* source, | 148 bool CastWindowAndroid::AddMessageToConsole(content::WebContents* source, |
112 int32 level, | 149 int32 level, |
113 const base::string16& message, | 150 const base::string16& message, |
114 int32 line_no, | 151 int32 line_no, |
115 const base::string16& source_id) { | 152 const base::string16& source_id) { |
116 return false; | 153 return false; |
117 } | 154 } |
118 | 155 |
119 void CastWindowAndroid::ActivateContents(content::WebContents* contents) { | 156 void CastWindowAndroid::ActivateContents(content::WebContents* contents) { |
120 DCHECK_EQ(contents, web_contents_.get()); | 157 DCHECK_EQ(contents, web_contents_.get()); |
121 contents->GetRenderViewHost()->Focus(); | 158 contents->GetRenderViewHost()->Focus(); |
122 } | 159 } |
123 | 160 |
124 void CastWindowAndroid::DeactivateContents(content::WebContents* contents) { | 161 void CastWindowAndroid::DeactivateContents(content::WebContents* contents) { |
125 DCHECK_EQ(contents, web_contents_.get()); | 162 DCHECK_EQ(contents, web_contents_.get()); |
126 contents->GetRenderViewHost()->Blur(); | 163 contents->GetRenderViewHost()->Blur(); |
127 } | 164 } |
128 | 165 |
129 void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status) { | 166 void CastWindowAndroid::RenderProcessGone(base::TerminationStatus status) { |
130 LOG(ERROR) << "Render process gone: status=" << status; | 167 LOG(ERROR) << "Render process gone: status=" << status; |
131 Close(); | 168 Destroy(); |
132 } | 169 } |
133 | 170 |
134 } // namespace shell | 171 } // namespace shell |
135 } // namespace chromecast | 172 } // namespace chromecast |
OLD | NEW |