Delete unused downloads page asset.
[chromium-blink-merge.git] / mandoline / tab / web_view_impl.cc
blobc0860917efbe618c90338a5efd677c10cabfa48a
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"
18 #include "url/gurl.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;
25 namespace web_view {
26 namespace {
28 bool EnableRemoteDebugging() {
29 return base::CommandLine::ForCurrentProcess()->HasSwitch(
30 devtools_service::kRemoteDebuggingPort);
33 } // namespace
35 ////////////////////////////////////////////////////////////////////////////////
36 // WebViewImpl, public:
38 WebViewImpl::WebViewImpl(mojo::ApplicationImpl* app,
39 mojom::WebViewClientPtr client,
40 mojo::InterfaceRequest<mojom::WebView> request)
41 : app_(app),
42 client_(client.Pass()),
43 binding_(this, request.Pass()),
44 content_(nullptr) {
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) {
55 if (!content_) {
56 // We haven't been embedded yet, store the request for when we are.
57 pending_request_ = request.Pass();
58 return;
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(
68 &forward_agent);
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_) {
110 mojo::Rect rect;
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_)
120 frame_tree_.reset();
123 ////////////////////////////////////////////////////////////////////////////////
124 // WebViewImpl, mandoline::FrameTreeDelegate implementation:
126 bool WebViewImpl::CanPostMessageEventToFrame(const Frame* source,
127 const Frame* target,
128 HTMLMessageEvent* event) {
129 return true;
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(
145 Frame* target,
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();
154 return true;
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