summaryrefslogtreecommitdiffstats
path: root/mandoline/tab/web_view_impl.cc
blob: 3f00839b3f7db7b8cc423238899a563ae7956150 (plain)
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
137
// Copyright 2015 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 "mandoline/tab/web_view_impl.h"

#include "base/callback.h"
#include "components/view_manager/public/cpp/view.h"
#include "components/view_manager/public/cpp/view_manager.h"
#include "mandoline/tab/frame.h"
#include "mandoline/tab/frame_connection.h"
#include "mandoline/tab/frame_tree.h"
#include "mojo/application/public/cpp/application_impl.h"
#include "mojo/converters/geometry/geometry_type_converters.h"

// TODO(beng): remove once these classes are in the web_view namespace.
using mandoline::FrameConnection;
using mandoline::FrameTreeClient;
using mandoline::FrameUserData;

namespace web_view {

////////////////////////////////////////////////////////////////////////////////
// WebViewImpl, public:

WebViewImpl::WebViewImpl(mojo::ApplicationImpl* app,
                         mojom::WebViewClientPtr client,
                         mojo::InterfaceRequest<mojom::WebView> request)
    : app_(app),
      client_(client.Pass()),
      binding_(this, request.Pass()),
      content_(nullptr),
      view_manager_client_factory_(app->shell(), this) {
}

WebViewImpl::~WebViewImpl() {}

////////////////////////////////////////////////////////////////////////////////
// WebViewImpl, WebView implementation:

void WebViewImpl::LoadRequest(mojo::URLRequestPtr request) {
  if (!content_) {
    // We haven't been embedded yet, store the request for when we are.
    pending_request_ = request.Pass();
    return;
  }
  scoped_ptr<FrameConnection> frame_connection(new FrameConnection);
  mojo::ViewManagerClientPtr view_manager_client;
  frame_connection->Init(app_, request.Pass(), &view_manager_client);
  FrameTreeClient* frame_tree_client = frame_connection->frame_tree_client();
  frame_tree_.reset(new FrameTree(content_, this, frame_tree_client,
                                  frame_connection.Pass()));
  content_->Embed(view_manager_client.Pass());
}

void WebViewImpl::GetViewManagerClient(
    mojo::InterfaceRequest<mojo::ViewManagerClient> view_manager_client) {
  view_manager_client_factory_.Create(nullptr, view_manager_client.Pass());
}

////////////////////////////////////////////////////////////////////////////////
// WebViewImpl, mojo::ViewManagerDelegate implementation:

void WebViewImpl::OnEmbed(mojo::View* root) {
  root->view_manager()->SetEmbedRoot();
  root->AddObserver(this);
  content_ = root->view_manager()->CreateView();
  content_->SetBounds(*mojo::Rect::From(gfx::Rect(0, 0, root->bounds().width,
                                                  root->bounds().height)));
  root->AddChild(content_);
  content_->SetVisible(true);
  content_->AddObserver(this);

  if (!pending_request_.is_null())
    LoadRequest(pending_request_.Pass());
}

void WebViewImpl::OnViewManagerDestroyed(mojo::ViewManager* view_manager) {
}

////////////////////////////////////////////////////////////////////////////////
// WebViewImpl, mojo::ViewObserver implementation:

void WebViewImpl::OnViewBoundsChanged(mojo::View* view,
                                      const mojo::Rect& old_bounds,
                                      const mojo::Rect& new_bounds) {
  if (view != content_) {
    mojo::Rect rect;
    rect.width = new_bounds.width;
    rect.height = new_bounds.height;
    content_->SetBounds(rect);
  }
}

void WebViewImpl::OnViewDestroyed(mojo::View* view) {
  // |FrameTree| cannot outlive the content view.
  if (view == content_)
    frame_tree_.reset();
}

////////////////////////////////////////////////////////////////////////////////
// WebViewImpl, mandoline::FrameTreeDelegate implementation:

bool WebViewImpl::CanPostMessageEventToFrame(const Frame* source,
                                             const Frame* target,
                                             HTMLMessageEvent* event) {
  return true;
}

void WebViewImpl::LoadingStateChanged(bool loading) {
  client_->LoadingStateChanged(loading);
}

void WebViewImpl::ProgressChanged(double progress) {
  client_->ProgressChanged(progress);
}

void WebViewImpl::NavigateTopLevel(Frame* source, mojo::URLRequestPtr request) {
  client_->TopLevelNavigate(request.Pass());
}

bool WebViewImpl::CanNavigateFrame(
    Frame* target,
    mojo::URLRequestPtr request,
    FrameTreeClient** frame_tree_client,
    scoped_ptr<FrameUserData>* frame_user_data,
    mojo::ViewManagerClientPtr* view_manager_client) {
  scoped_ptr<FrameConnection> frame_connection(new FrameConnection);
  frame_connection->Init(app_, request.Pass(), view_manager_client);
  *frame_tree_client = frame_connection->frame_tree_client();
  *frame_user_data = frame_connection.Pass();
  return true;
}

void WebViewImpl::DidStartNavigation(Frame* frame) {}

}  // namespace web_view