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 "mandoline/tab/web_view_impl.h"
7 #include "base/callback.h"
8 #include "base/command_line.h"
9 #include "components/devtools_service/public/cpp/switches.h"
10 #include "components/view_manager/public/cpp/view.h"
11 #include "components/view_manager/public/cpp/view_tree_connection.h"
12 #include "mandoline/tab/frame.h"
13 #include "mandoline/tab/frame_connection.h"
14 #include "mandoline/tab/frame_devtools_agent.h"
15 #include "mandoline/tab/frame_tree.h"
16 #include "mojo/application/public/cpp/application_impl.h"
17 #include "mojo/converters/geometry/geometry_type_converters.h"
20 // TODO(beng): remove once these classes are in the web_view namespace.
21 using mandoline::FrameConnection
;
22 using mandoline::FrameTreeClient
;
23 using mandoline::FrameUserData
;
28 bool EnableRemoteDebugging() {
29 return base::CommandLine::ForCurrentProcess()->HasSwitch(
30 devtools_service::kRemoteDebuggingPort
);
35 ////////////////////////////////////////////////////////////////////////////////
36 // WebViewImpl, public:
38 WebViewImpl::WebViewImpl(mojo::ApplicationImpl
* app
,
39 mojom::WebViewClientPtr client
,
40 mojo::InterfaceRequest
<mojom::WebView
> request
)
42 client_(client
.Pass()),
43 binding_(this, request
.Pass()),
45 if (EnableRemoteDebugging())
46 devtools_agent_
.reset(new FrameDevToolsAgent(app_
, this));
49 WebViewImpl::~WebViewImpl() {}
51 ////////////////////////////////////////////////////////////////////////////////
52 // WebViewImpl, WebView implementation:
54 void WebViewImpl::LoadRequest(mojo::URLRequestPtr request
) {
56 // We haven't been embedded yet, store the request for when we are.
57 pending_request_
= request
.Pass();
60 scoped_ptr
<FrameConnection
> frame_connection(new FrameConnection
);
61 mojo::ViewTreeClientPtr view_tree_client
;
62 frame_connection
->Init(app_
, request
.Pass(), &view_tree_client
);
64 Frame::ClientPropertyMap client_properties
;
65 if (devtools_agent_
) {
66 devtools_service::DevToolsAgentPtr forward_agent
;
67 frame_connection
->application_connection()->ConnectToService(
69 devtools_agent_
->AttachFrame(forward_agent
.Pass(), &client_properties
);
72 FrameTreeClient
* frame_tree_client
= frame_connection
->frame_tree_client();
73 frame_tree_
.reset(new FrameTree(content_
, this, frame_tree_client
,
74 frame_connection
.Pass(), client_properties
));
75 content_
->Embed(view_tree_client
.Pass());
78 void WebViewImpl::GetViewTreeClient(
79 mojo::InterfaceRequest
<mojo::ViewTreeClient
> view_tree_client
) {
80 mojo::ViewTreeConnection::Create(this, view_tree_client
.Pass());
83 ////////////////////////////////////////////////////////////////////////////////
84 // WebViewImpl, mojo::ViewTreeDelegate implementation:
86 void WebViewImpl::OnEmbed(mojo::View
* root
) {
87 root
->connection()->SetEmbedRoot();
88 root
->AddObserver(this);
89 content_
= root
->connection()->CreateView();
90 content_
->SetBounds(*mojo::Rect::From(gfx::Rect(0, 0, root
->bounds().width
,
91 root
->bounds().height
)));
92 root
->AddChild(content_
);
93 content_
->SetVisible(true);
94 content_
->AddObserver(this);
96 if (!pending_request_
.is_null())
97 LoadRequest(pending_request_
.Pass());
100 void WebViewImpl::OnConnectionLost(mojo::ViewTreeConnection
* connection
) {
103 ////////////////////////////////////////////////////////////////////////////////
104 // WebViewImpl, mojo::ViewObserver implementation:
106 void WebViewImpl::OnViewBoundsChanged(mojo::View
* view
,
107 const mojo::Rect
& old_bounds
,
108 const mojo::Rect
& new_bounds
) {
109 if (view
!= content_
) {
111 rect
.width
= new_bounds
.width
;
112 rect
.height
= new_bounds
.height
;
113 content_
->SetBounds(rect
);
117 void WebViewImpl::OnViewDestroyed(mojo::View
* view
) {
118 // |FrameTree| cannot outlive the content view.
119 if (view
== content_
)
123 ////////////////////////////////////////////////////////////////////////////////
124 // WebViewImpl, mandoline::FrameTreeDelegate implementation:
126 bool WebViewImpl::CanPostMessageEventToFrame(const Frame
* source
,
128 HTMLMessageEvent
* event
) {
132 void WebViewImpl::LoadingStateChanged(bool loading
) {
133 client_
->LoadingStateChanged(loading
);
136 void WebViewImpl::ProgressChanged(double progress
) {
137 client_
->ProgressChanged(progress
);
140 void WebViewImpl::NavigateTopLevel(Frame
* source
, mojo::URLRequestPtr request
) {
141 client_
->TopLevelNavigate(request
.Pass());
144 bool WebViewImpl::CanNavigateFrame(
146 mojo::URLRequestPtr request
,
147 FrameTreeClient
** frame_tree_client
,
148 scoped_ptr
<FrameUserData
>* frame_user_data
,
149 mojo::ViewTreeClientPtr
* view_tree_client
) {
150 scoped_ptr
<FrameConnection
> frame_connection(new FrameConnection
);
151 frame_connection
->Init(app_
, request
.Pass(), view_tree_client
);
152 *frame_tree_client
= frame_connection
->frame_tree_client();
153 *frame_user_data
= frame_connection
.Pass();
157 void WebViewImpl::DidStartNavigation(Frame
* frame
) {}
159 ////////////////////////////////////////////////////////////////////////////////
160 // WebViewImpl, FrameDevToolsAgentDelegate implementation:
162 void WebViewImpl::HandlePageNavigateRequest(const GURL
& url
) {
163 mojo::URLRequestPtr
request(mojo::URLRequest::New());
164 request
->url
= url
.spec();
165 client_
->TopLevelNavigate(request
.Pass());
168 } // namespace web_view