OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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/cast_web_view.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/threading/thread_task_runner_handle.h" | |
9 #include "chromecast/base/metrics/cast_metrics_helper.h" | |
10 #include "content/public/browser/render_frame_host.h" | |
11 #include "content/public/browser/render_view_host.h" | |
12 #include "content/public/browser/render_widget_host.h" | |
13 #include "content/public/browser/render_widget_host_view.h" | |
14 #include "ipc/ipc_message.h" | |
15 #include "net/base/net_errors.h" | |
16 #include "ui/display/display.h" | |
17 #include "ui/display/screen.h" | |
18 #include "url/gurl.h" | |
19 | |
20 #if defined(OS_ANDROID) | |
21 #include "chromecast/browser/android/cast_web_contents_activity.h" | |
22 #endif // defined(OS_ANDROID) | |
23 | |
24 #if defined(USE_AURA) | |
25 #include "ui/aura/window.h" | |
26 #endif | |
27 | |
28 namespace chromecast { | |
29 | |
30 namespace { | |
31 // The time (in milliseconds) we wait for after a page is closed (i.e. | |
32 // after an app is stopped) before we delete the corresponding WebContents. | |
33 constexpr int kWebContentsDestructionDelayInMs = 50; | |
alokp
2017/01/19 05:26:36
this seems like a hack. How did you arrive at this
halliwell
2017/01/19 17:54:30
This is long-standing behaviour of Cast. Note tha
| |
34 | |
35 std::unique_ptr<content::WebContents> CreateWebContents( | |
36 content::BrowserContext* browser_context) { | |
37 CHECK(display::Screen::GetScreen()); | |
38 gfx::Size display_size = | |
39 display::Screen::GetScreen()->GetPrimaryDisplay().size(); | |
40 | |
41 content::WebContents::CreateParams create_params(browser_context, NULL); | |
42 create_params.routing_id = MSG_ROUTING_NONE; | |
43 create_params.initial_size = display_size; | |
44 content::WebContents* web_contents = | |
45 content::WebContents::Create(create_params); | |
46 | |
47 #if defined(USE_AURA) | |
48 // Resize window | |
49 aura::Window* content_window = web_contents->GetNativeView(); | |
50 content_window->SetBounds( | |
51 gfx::Rect(display_size.width(), display_size.height())); | |
52 #endif | |
53 | |
54 #if defined(OS_ANDROID) | |
55 content::RendererPreferences* prefs = web_contents->GetMutableRendererPrefs(); | |
56 prefs->use_video_overlay_for_embedded_encrypted_video = true; | |
57 web_contents->GetRenderViewHost()->SyncRendererPrefs(); | |
58 #endif | |
59 | |
60 return base::WrapUnique(web_contents); | |
61 } | |
62 | |
63 } // namespace | |
64 | |
65 CastWebView::CastWebView(Delegate* delegate, | |
66 content::BrowserContext* browser_context, | |
67 GURL url, | |
68 bool transparent) | |
69 : delegate_(delegate), | |
70 browser_context_(browser_context), | |
71 transparent_(transparent), | |
72 window_(shell::CastContentWindow::Create(delegate)), | |
73 web_contents_(CreateWebContents(browser_context_)), | |
74 weak_factory_(this) { | |
75 DCHECK(delegate_); | |
76 DCHECK(browser_context_); | |
77 DCHECK(window_); | |
78 content::WebContentsObserver::Observe(web_contents_.get()); | |
79 web_contents_->SetDelegate(this); | |
80 | |
81 if (transparent_) | |
82 window_->SetTransparent(); | |
83 | |
84 web_contents_->GetController().LoadURL( | |
85 url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, ""); | |
86 } | |
87 | |
88 CastWebView::~CastWebView() {} | |
89 | |
90 void CastWebView::ClosePage() { | |
91 content::WebContentsObserver::Observe(nullptr); | |
92 web_contents_->ClosePage(); | |
93 } | |
94 | |
95 void CastWebView::CloseContents(content::WebContents* source) { | |
alokp
2017/01/19 05:26:36
nit: please define functions in the same order as
derekjchow1
2017/01/19 21:59:16
Done (regarding reordering).
Although this functi
| |
96 DCHECK_EQ(source, web_contents_.get()); | |
97 | |
98 // We need to delay the deletion of web_contents_ (currently for 50ms) to | |
99 // give (and guarantee) the renderer enough time to finish 'onunload' | |
100 // handler (but we don't want to wait any longer than that to delay the | |
101 // starting of next app). | |
102 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
103 FROM_HERE, base::Bind(&CastWebView::DelayedCloseContents, | |
alokp
2017/01/19 05:26:36
Hmm. I do not understand this code. You do not sta
derekjchow1
2017/01/19 21:59:16
I added an additional comment. This function gets
| |
104 weak_factory_.GetWeakPtr()), | |
105 base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs)); | |
106 } | |
107 | |
108 void CastWebView::DelayedCloseContents() { | |
109 // Delete the WebContents object here so that the gfx surface will be | |
110 // deleted as part of destroying RenderWidgetHostViewCast object. | |
111 // We want to delete the surface before we start the next app because | |
112 // the next app could be an external one whose Start() function would | |
113 // destroy the primary gfx plane. | |
114 web_contents_.reset(); | |
115 delegate_->OnPageStopped(net::OK); | |
116 } | |
117 | |
118 void CastWebView::Show() { | |
119 window_->ShowWebContents(web_contents_.get()); | |
120 web_contents_->Focus(); | |
121 } | |
122 | |
123 content::WebContents* CastWebView::OpenURLFromTab( | |
124 content::WebContents* source, | |
125 const content::OpenURLParams& params) { | |
126 LOG(INFO) << "Change url: " << params.url; | |
127 // If source is NULL which means current tab, use web_contents_ of this class. | |
128 if (!source) | |
129 source = web_contents_.get(); | |
130 DCHECK_EQ(source, web_contents_.get()); | |
131 // We don't want to create another web_contents. Load url only when source is | |
132 // specified. | |
133 if (source) { | |
134 source->GetController().LoadURL(params.url, params.referrer, | |
135 params.transition, params.extra_headers); | |
136 } | |
137 return source; | |
138 } | |
139 | |
140 void CastWebView::LoadingStateChanged(content::WebContents* source, | |
141 bool to_different_document) { | |
142 delegate_->OnLoadingStateChanged(source->IsLoading()); | |
143 } | |
144 | |
145 void CastWebView::ActivateContents(content::WebContents* contents) { | |
146 DCHECK_EQ(contents, web_contents_.get()); | |
147 contents->GetRenderViewHost()->GetWidget()->Focus(); | |
148 } | |
149 | |
150 #if defined(OS_ANDROID) | |
151 base::android::ScopedJavaLocalRef<jobject> | |
152 CastWebView::GetContentVideoViewEmbedder() { | |
153 DCHECK(web_contents_); | |
154 auto activity = shell::CastWebContentsActivity::Get(web_contents_.get()); | |
155 return activity->GetContentVideoViewEmbedder(); | |
156 } | |
157 #endif // defined(OS_ANDROID) | |
158 | |
159 void CastWebView::RenderProcessGone(base::TerminationStatus status) { | |
160 LOG(INFO) << "APP_ERROR_CHILD_PROCESS_CRASHED"; | |
161 delegate_->OnPageStopped(net::ERR_UNEXPECTED); | |
162 } | |
163 | |
164 void CastWebView::RenderViewCreated(content::RenderViewHost* render_view_host) { | |
165 content::RenderWidgetHostView* view = | |
166 render_view_host->GetWidget()->GetView(); | |
167 if (view) { | |
168 view->SetBackgroundColor(transparent_ ? SK_ColorTRANSPARENT | |
169 : SK_ColorBLACK); | |
170 } | |
171 } | |
172 | |
173 void CastWebView::DidFailProvisionalLoad( | |
174 content::RenderFrameHost* render_frame_host, | |
175 const GURL& validated_url, | |
176 int error_code, | |
177 const base::string16& error_description, | |
178 bool was_ignored_by_handler) { | |
179 // If we abort errors in an iframe, it can create a really confusing | |
180 // and fragile user experience. Rather than create a list of errors | |
181 // that are most likely to occur, we ignore all of them for now. | |
182 if (render_frame_host->GetParent()) { | |
183 LOG(ERROR) << "Got error on sub-iframe: url=" | |
184 << validated_url.spec() << ", error=" << error_code; | |
185 return; | |
186 } | |
187 | |
188 LOG(ERROR) << "Got error on provisional load: url=" << validated_url.spec() | |
189 << ", error_code=" << error_code | |
190 << "description=" << error_description; | |
191 delegate_->OnPageStopped(error_code); | |
192 } | |
193 | |
194 void CastWebView::DidFailLoad(content::RenderFrameHost* render_frame_host, | |
195 const GURL& validated_url, | |
196 int error_code, | |
197 const base::string16& error_description, | |
198 bool was_ignored_by_handler) { | |
199 // Only report an error if we are the main frame. See b/8433611. | |
200 if (render_frame_host->GetParent()) { | |
201 LOG(ERROR) << "Got error on sub-iframe: url=" | |
202 << validated_url.spec() << ", error=" << error_code; | |
203 } else if (error_code == net::ERR_ABORTED) { | |
204 // ERR_ABORTED means download was aborted by the app, typically this happens | |
205 // when flinging URL for direct playback, the initial URLRequest gets | |
206 // cancelled/aborted and then the same URL is requested via the buffered | |
207 // data source for media::Pipeline playback. | |
208 LOG(INFO) << "Load canceled: url=" << validated_url.spec(); | |
209 } else { | |
210 LOG(ERROR) << "Got error on load: url=" << validated_url.spec() | |
211 << ", error_code=" << error_code; | |
212 delegate_->OnPageStopped(error_code); | |
213 } | |
214 } | |
215 | |
216 void CastWebView::DidFirstVisuallyNonEmptyPaint() { | |
217 metrics::CastMetricsHelper::GetInstance()->LogTimeToFirstPaint(); | |
218 } | |
219 | |
220 void CastWebView::MediaStartedPlaying( | |
221 const MediaPlayerInfo& media_info, | |
222 const MediaPlayerId& id) { | |
223 metrics::CastMetricsHelper::GetInstance()->LogMediaPlay(); | |
224 } | |
225 | |
226 void CastWebView::MediaStoppedPlaying( | |
227 const MediaPlayerInfo& media_info, | |
228 const MediaPlayerId& id) { | |
229 metrics::CastMetricsHelper::GetInstance()->LogMediaPause(); | |
230 } | |
231 | |
232 } // namespace chromecast | |
OLD | NEW |