Index: chromecast/browser/cast_web_contents.cc |
diff --git a/chromecast/browser/cast_web_contents.cc b/chromecast/browser/cast_web_contents.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d31df3acf65c9fdeb072d981171863ce0349653f |
--- /dev/null |
+++ b/chromecast/browser/cast_web_contents.cc |
@@ -0,0 +1,182 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromecast/browser/cast_web_contents.h" |
+ |
+#include "base/logging.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "chromecast/base/metrics/cast_metrics_helper.h" |
+#include "content/public/browser/render_frame_host.h" |
+#include "content/public/browser/render_view_host.h" |
+#include "content/public/browser/render_widget_host.h" |
+#include "net/base/net_errors.h" |
+#include "url/gurl.h" |
+ |
+#if defined(OS_ANDROID) |
+#include "chromecast/browser/android/cast_web_contents_activity.h" |
+#endif // defined(OS_ANDROID) |
+ |
+namespace chromecast { |
+ |
+namespace { |
+// The time (in milliseconds) we wait for after a page is closed (i.e. |
+// after an app is stopped) before we delete the corresponding WebContents. |
+constexpr int kWebContentsDestructionDelayInMs = 50; |
+} // namespace |
+ |
+CastWebContents::CastWebContents(Delegate* delegate, |
+ content::BrowserContext* browser_context) |
+ : delegate_(delegate), |
+ browser_context_(browser_context), |
+ window_(shell::CastContentWindow::Create(delegate)), |
+ web_contents_(window_->CreateWebContents(browser_context_)), |
alokp
2017/01/14 00:43:40
The overlap between CastContentWindow and CastWebC
derekjchow1
2017/01/18 03:21:20
Great idea. Done.
|
+ weak_factory_(this) { |
+ DCHECK(delegate_); |
+ DCHECK(browser_context_); |
+ DCHECK(window_); |
+ content::WebContentsObserver::Observe(web_contents_.get()); |
+ web_contents_->SetDelegate(this); |
+} |
+ |
+CastWebContents::~CastWebContents() {} |
+ |
+void CastWebContents::LoadUrl(GURL url) { |
+ web_contents_->GetController().LoadURL( |
+ url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, ""); |
+} |
+ |
+void CastWebContents::ClosePage() { |
+ content::WebContentsObserver::Observe(nullptr); |
+ web_contents_->ClosePage(); |
+} |
+ |
+void CastWebContents::CloseContents(content::WebContents* source) { |
+ DCHECK_EQ(source, web_contents_.get()); |
+ |
+ // We need to delay the deletion of web_contents_ (currently for 50ms) to |
+ // give (and guarantee) the renderer enough time to finish 'onunload' |
+ // handler (but we don't want to wait any longer than that to delay the |
+ // starting of next app). |
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
+ FROM_HERE, base::Bind(&CastWebContents::DelayedCloseContents, |
+ weak_factory_.GetWeakPtr()), |
+ base::TimeDelta::FromMilliseconds(kWebContentsDestructionDelayInMs)); |
+} |
+ |
+void CastWebContents::DelayedCloseContents() { |
+ // Delete the WebContents object here so that the gfx surface will be |
+ // deleted as part of destroying RenderWidgetHostViewCast object. |
+ // We want to delete the surface before we start the next app because |
+ // the next app could be an external one whose Start() function would |
+ // destroy the primary gfx plane. |
+ web_contents_.reset(); |
+ delegate_->OnPageStopped(net::OK); |
+} |
+ |
+void CastWebContents::MakeVisible() { |
+ window_->ShowWebContents(web_contents_.get()); |
+ web_contents_->Focus(); |
+} |
+ |
+content::WebContents* CastWebContents::OpenURLFromTab( |
+ content::WebContents* source, |
+ const content::OpenURLParams& params) { |
+ LOG(INFO) << "Change url: " << params.url; |
+ // If source is NULL which means current tab, use web_contents_ of this class. |
+ if (!source) |
+ source = web_contents_.get(); |
+ DCHECK_EQ(source, web_contents_.get()); |
+ // We don't want to create another web_contents. Load url only when source is |
+ // specified. |
+ if (source) { |
+ source->GetController().LoadURL(params.url, params.referrer, |
+ params.transition, params.extra_headers); |
+ } |
+ return source; |
+} |
+ |
+void CastWebContents::LoadingStateChanged(content::WebContents* source, |
+ bool to_different_document) { |
+ delegate_->OnLoadingStateChanged(source->IsLoading()); |
+} |
+ |
+void CastWebContents::ActivateContents(content::WebContents* contents) { |
+ DCHECK_EQ(contents, web_contents_.get()); |
+ contents->GetRenderViewHost()->GetWidget()->Focus(); |
+} |
+ |
+#if defined(OS_ANDROID) |
+base::android::ScopedJavaLocalRef<jobject> |
+CastWebContents::GetContentVideoViewEmbedder() { |
+ DCHECK(web_contents_); |
+ auto activity = shell::CastWebContentsActivity::Get(web_contents_.get()); |
+ return activity->GetContentVideoViewEmbedder(); |
+} |
+#endif // defined(OS_ANDROID) |
+ |
+void CastWebContents::RenderProcessGone(base::TerminationStatus status) { |
+ LOG(INFO) << "APP_ERROR_CHILD_PROCESS_CRASHED"; |
+ delegate_->OnPageStopped(net::ERR_UNEXPECTED); |
+} |
+ |
+void CastWebContents::DidFailProvisionalLoad( |
+ content::RenderFrameHost* render_frame_host, |
+ const GURL& validated_url, |
+ int error_code, |
+ const base::string16& error_description, |
+ bool was_ignored_by_handler) { |
+ // If we abort errors in an iframe, it can create a really confusing |
+ // and fragile user experience. Rather than create a list of errors |
+ // that are most likely to occur, we ignore all of them for now. |
+ if (render_frame_host->GetParent()) { |
+ LOG(ERROR) << "Got error on sub-iframe: url=" |
+ << validated_url.spec() << ", error=" << error_code; |
+ return; |
+ } |
+ |
+ LOG(ERROR) << "Got error on provisional load: url=" << validated_url.spec() |
+ << ", error_code=" << error_code |
+ << "description=" << error_description; |
+ delegate_->OnPageStopped(error_code); |
+} |
+ |
+void CastWebContents::DidFailLoad(content::RenderFrameHost* render_frame_host, |
+ const GURL& validated_url, |
+ int error_code, |
+ const base::string16& error_description, |
+ bool was_ignored_by_handler) { |
+ // Only report an error if we are the main frame. See b/8433611. |
+ if (render_frame_host->GetParent()) { |
+ LOG(ERROR) << "Got error on sub-iframe: url=" |
+ << validated_url.spec() << ", error=" << error_code; |
+ } else if (error_code == net::ERR_ABORTED) { |
+ // ERR_ABORTED means download was aborted by the app, typically this happens |
+ // when flinging URL for direct playback, the initial URLRequest gets |
+ // cancelled/aborted and then the same URL is requested via the buffered |
+ // data source for media::Pipeline playback. |
+ LOG(INFO) << "Load canceled: url=" << validated_url.spec(); |
+ } else { |
+ LOG(ERROR) << "Got error on load: url=" << validated_url.spec() |
+ << ", error_code=" << error_code; |
+ delegate_->OnPageStopped(error_code); |
+ } |
+} |
+ |
+void CastWebContents::DidFirstVisuallyNonEmptyPaint() { |
+ metrics::CastMetricsHelper::GetInstance()->LogTimeToFirstPaint(); |
+} |
+ |
+void CastWebContents::MediaStartedPlaying( |
+ const MediaPlayerInfo& media_info, |
+ const MediaPlayerId& id) { |
+ metrics::CastMetricsHelper::GetInstance()->LogMediaPlay(); |
+} |
+ |
+void CastWebContents::MediaStoppedPlaying( |
+ const MediaPlayerInfo& media_info, |
+ const MediaPlayerId& id) { |
+ metrics::CastMetricsHelper::GetInstance()->LogMediaPause(); |
+} |
+ |
+} // namespace chromecast |