forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast_web_view_extension.cc
136 lines (117 loc) · 4.85 KB
/
cast_web_view_extension.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2018 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_view_extension.h"
#include "base/logging.h"
#include "chromecast/browser/cast_browser_process.h"
#include "chromecast/browser/cast_extension_host.h"
#include "chromecast/browser/devtools/remote_debugging_server.h"
#include "content/public/browser/web_contents_observer.h"
#include "extensions/browser/extension_system.h"
#include "net/base/net_errors.h"
namespace chromecast {
CastWebViewExtension::CastWebViewExtension(
const CreateParams& params,
content::BrowserContext* browser_context,
scoped_refptr<content::SiteInstance> site_instance,
const extensions::Extension* extension,
const GURL& initial_url)
: delegate_(params.delegate),
window_(shell::CastContentWindow::Create(params.delegate,
params.is_headless,
params.enable_touch_input)),
extension_host_(std::make_unique<CastExtensionHost>(
browser_context,
params.delegate,
extension,
initial_url,
site_instance.get(),
extensions::VIEW_TYPE_EXTENSION_POPUP)),
remote_debugging_server_(
shell::CastBrowserProcess::GetInstance()->remote_debugging_server()) {
DCHECK(delegate_);
content::WebContentsObserver::Observe(web_contents());
// If this CastWebView is enabled for development, start the remote debugger.
if (params.enabled_for_dev) {
LOG(INFO) << "Enabling dev console for " << web_contents()->GetVisibleURL();
remote_debugging_server_->EnableWebContentsForDebugging(web_contents());
}
}
CastWebViewExtension::~CastWebViewExtension() {
content::WebContentsObserver::Observe(nullptr);
}
shell::CastContentWindow* CastWebViewExtension::window() const {
return window_.get();
}
content::WebContents* CastWebViewExtension::web_contents() const {
return extension_host_->host_contents();
}
void CastWebViewExtension::LoadUrl(GURL url) {
extension_host_->CreateRenderViewSoon();
}
void CastWebViewExtension::ClosePage(const base::TimeDelta& shutdown_delay) {}
void CastWebViewExtension::InitializeWindow(
CastWindowManager* window_manager,
bool is_visible,
CastWindowManager::WindowId z_order,
VisibilityPriority initial_priority) {
window_->CreateWindowForWebContents(web_contents(), window_manager,
is_visible, z_order, initial_priority);
web_contents()->Focus();
}
void CastWebViewExtension::WebContentsDestroyed() {
delegate_->OnPageStopped(net::OK);
}
void CastWebViewExtension::RenderViewCreated(
content::RenderViewHost* render_view_host) {
content::RenderWidgetHostView* view =
render_view_host->GetWidget()->GetView();
if (view) {
view->SetBackgroundColor(SK_ColorTRANSPARENT);
}
}
void CastWebViewExtension::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// If the navigation was not committed, it means either the page was a
// download or error 204/205, or the navigation never left the previous
// URL. Basically, this isn't a problem since we stayed at the existing URL.
if (!navigation_handle->HasCommitted())
return;
// The navigation committed. If we navigated to an error page then
// this is a bad state, and should be notified. Otherwise the navigation
// completed as intended.
if (!navigation_handle->IsErrorPage())
return;
net::Error error_code = navigation_handle->GetNetErrorCode();
// Ignore sub-frame errors.
if (!navigation_handle->IsInMainFrame()) {
LOG(ERROR) << "Got error on sub-frame: url=" << navigation_handle->GetURL()
<< ", error=" << error_code
<< ", description=" << net::ErrorToShortString(error_code);
return;
}
LOG(ERROR) << "Got error on navigation: url=" << navigation_handle->GetURL()
<< ", error_code=" << error_code
<< ", description= " << net::ErrorToShortString(error_code);
delegate_->OnPageStopped(error_code);
}
void CastWebViewExtension::DidFailLoad(
content::RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code,
const base::string16& error_description) {
// Only report an error if we are the main frame.
if (render_frame_host->GetParent()) {
LOG(ERROR) << "Got error on sub-frame: url=" << validated_url.spec()
<< ", error=" << error_code << ": " << error_description;
return;
}
LOG(ERROR) << "Got error on load: url=" << validated_url.spec()
<< ", error_code=" << error_code << ": " << error_description;
;
delegate_->OnPageStopped(error_code);
}
void CastWebViewExtension::RenderProcessGone(base::TerminationStatus status) {
delegate_->OnPageStopped(net::ERR_UNEXPECTED);
}
} // namespace chromecast