1 // Copyright 2015 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.
5 #include "components/web_view/web_view_impl.h"
7 #include "base/command_line.h"
8 #include "components/devtools_service/public/cpp/switches.h"
9 #include "components/view_manager/public/cpp/scoped_view_ptr.h"
10 #include "components/view_manager/public/cpp/view.h"
11 #include "components/view_manager/public/cpp/view_tree_connection.h"
12 #include "components/web_view/frame.h"
13 #include "components/web_view/frame_connection.h"
14 #include "components/web_view/frame_devtools_agent.h"
15 #include "components/web_view/frame_tree.h"
16 #include "components/web_view/pending_web_view_load.h"
17 #include "mojo/application/public/cpp/application_impl.h"
18 #include "mojo/converters/geometry/geometry_type_converters.h"
24 bool EnableRemoteDebugging() {
25 return base::CommandLine::ForCurrentProcess()->HasSwitch(
26 devtools_service::kRemoteDebuggingPort
);
31 ////////////////////////////////////////////////////////////////////////////////
32 // WebViewImpl, public:
34 WebViewImpl::WebViewImpl(mojo::ApplicationImpl
* app
,
35 mojom::WebViewClientPtr client
,
36 mojo::InterfaceRequest
<mojom::WebView
> request
)
38 client_(client
.Pass()),
39 binding_(this, request
.Pass()),
42 if (EnableRemoteDebugging())
43 devtools_agent_
.reset(new FrameDevToolsAgent(app_
, this));
46 WebViewImpl::~WebViewImpl() {
48 content_
->RemoveObserver(this);
50 root_
->RemoveObserver(this);
51 mojo::ScopedViewPtr::DeleteViewOrViewManager(root_
);
55 void WebViewImpl::OnLoad() {
56 scoped_ptr
<PendingWebViewLoad
> pending_load(pending_load_
.Pass());
57 scoped_ptr
<FrameConnection
> frame_connection(
58 pending_load
->frame_connection());
59 mojo::ViewTreeClientPtr view_tree_client
=
60 frame_connection
->GetViewTreeClient();
62 Frame::ClientPropertyMap client_properties
;
63 if (devtools_agent_
) {
64 devtools_service::DevToolsAgentPtr forward_agent
;
65 frame_connection
->application_connection()->ConnectToService(
67 devtools_agent_
->AttachFrame(forward_agent
.Pass(), &client_properties
);
70 FrameTreeClient
* frame_tree_client
= frame_connection
->frame_tree_client();
71 const uint32_t content_handler_id
= frame_connection
->GetContentHandlerID();
72 frame_tree_
.reset(new FrameTree(content_handler_id
, content_
, this,
73 frame_tree_client
, frame_connection
.Pass(),
75 content_
->Embed(view_tree_client
.Pass());
78 ////////////////////////////////////////////////////////////////////////////////
79 // WebViewImpl, WebView implementation:
81 void WebViewImpl::LoadRequest(mojo::URLRequestPtr request
) {
82 pending_load_
.reset(new PendingWebViewLoad(this));
83 pending_load_
->Init(request
.Pass());
86 void WebViewImpl::GetViewTreeClient(
87 mojo::InterfaceRequest
<mojo::ViewTreeClient
> view_tree_client
) {
88 mojo::ViewTreeConnection::Create(this, view_tree_client
.Pass());
91 ////////////////////////////////////////////////////////////////////////////////
92 // WebViewImpl, mojo::ViewTreeDelegate implementation:
94 void WebViewImpl::OnEmbed(mojo::View
* root
) {
95 root
->connection()->SetEmbedRoot();
96 root
->AddObserver(this);
98 content_
= root
->connection()->CreateView();
99 content_
->SetBounds(*mojo::Rect::From(gfx::Rect(0, 0, root
->bounds().width
,
100 root
->bounds().height
)));
101 root
->AddChild(content_
);
102 content_
->SetVisible(true);
103 content_
->AddObserver(this);
105 if (pending_load_
&& pending_load_
->is_content_handler_id_valid())
109 void WebViewImpl::OnConnectionLost(mojo::ViewTreeConnection
* connection
) {
113 ////////////////////////////////////////////////////////////////////////////////
114 // WebViewImpl, mojo::ViewObserver implementation:
116 void WebViewImpl::OnViewBoundsChanged(mojo::View
* view
,
117 const mojo::Rect
& old_bounds
,
118 const mojo::Rect
& new_bounds
) {
119 if (view
!= content_
) {
121 rect
.width
= new_bounds
.width
;
122 rect
.height
= new_bounds
.height
;
123 content_
->SetBounds(rect
);
127 void WebViewImpl::OnViewDestroyed(mojo::View
* view
) {
128 // |FrameTree| cannot outlive the content view.
129 if (view
== content_
) {
135 ////////////////////////////////////////////////////////////////////////////////
136 // WebViewImpl, FrameTreeDelegate implementation:
138 bool WebViewImpl::CanPostMessageEventToFrame(const Frame
* source
,
140 HTMLMessageEvent
* event
) {
144 void WebViewImpl::LoadingStateChanged(bool loading
) {
145 client_
->LoadingStateChanged(loading
);
148 void WebViewImpl::ProgressChanged(double progress
) {
149 client_
->ProgressChanged(progress
);
152 void WebViewImpl::NavigateTopLevel(Frame
* source
, mojo::URLRequestPtr request
) {
153 client_
->TopLevelNavigate(request
.Pass());
156 void WebViewImpl::CanNavigateFrame(Frame
* target
,
157 mojo::URLRequestPtr request
,
158 const CanNavigateFrameCallback
& callback
) {
159 FrameConnection::CreateConnectionForCanNavigateFrame(
160 app_
, target
, request
.Pass(), callback
);
163 void WebViewImpl::DidStartNavigation(Frame
* frame
) {}
165 ////////////////////////////////////////////////////////////////////////////////
166 // WebViewImpl, FrameDevToolsAgentDelegate implementation:
168 void WebViewImpl::HandlePageNavigateRequest(const GURL
& url
) {
169 mojo::URLRequestPtr
request(mojo::URLRequest::New());
170 request
->url
= url
.spec();
171 client_
->TopLevelNavigate(request
.Pass());
174 } // namespace web_view