summaryrefslogtreecommitdiffstats
path: root/components/html_viewer/html_frame_tree_manager.cc
blob: 9de240fb6972b271c2eff8419e64b3ae876161ca (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// 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 "components/html_viewer/html_frame_tree_manager.h"

#include <algorithm>

#include "base/command_line.h"
#include "base/logging.h"
#include "components/html_viewer/blink_basic_type_converters.h"
#include "components/html_viewer/blink_url_request_type_converters.h"
#include "components/html_viewer/document_resource_waiter.h"
#include "components/html_viewer/global_state.h"
#include "components/html_viewer/html_frame.h"
#include "components/html_viewer/html_frame_delegate.h"
#include "components/html_viewer/html_viewer_switches.h"
#include "components/view_manager/public/cpp/view_manager.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebRemoteFrame.h"
#include "third_party/WebKit/public/web/WebTreeScopeType.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "ui/gfx/geometry/dip_util.h"
#include "ui/gfx/geometry/size.h"

namespace html_viewer {
namespace {

// Returns the index of the FrameData with the id of |frame_id| in |index|. On
// success returns true, otherwise false.
bool FindFrameDataIndex(const mojo::Array<mandoline::FrameDataPtr>& frame_data,
                        uint32_t frame_id,
                        size_t* index) {
  for (size_t i = 0; i < frame_data.size(); ++i) {
    if (frame_data[i]->frame_id == frame_id) {
      *index = i;
      return true;
    }
  }
  return false;
}

}  // namespace

// static
HTMLFrameTreeManager::TreeMap* HTMLFrameTreeManager::instances_ = nullptr;

// static
HTMLFrame* HTMLFrameTreeManager::CreateFrameAndAttachToTree(
    GlobalState* global_state,
    mojo::View* view,
    scoped_ptr<DocumentResourceWaiter> resource_waiter,
    HTMLFrameDelegate* delegate) {
  if (!instances_)
    instances_ = new TreeMap;

  mojo::InterfaceRequest<mandoline::FrameTreeClient> frame_tree_client_request;
  mandoline::FrameTreeServerPtr frame_tree_server;
  mojo::Array<mandoline::FrameDataPtr> frame_data;
  uint32_t change_id;
  resource_waiter->Release(&frame_tree_client_request, &frame_tree_server,
                           &frame_data, &change_id);
  resource_waiter.reset();

  HTMLFrameTreeManager* frame_tree = nullptr;

  if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kOOPIFAlwaysCreateNewFrameTree)) {
    if (instances_->count(frame_data[0]->frame_id))
      frame_tree = (*instances_)[frame_data[0]->frame_id];
  }

  if (!frame_tree) {
    frame_tree = new HTMLFrameTreeManager(global_state);
    frame_tree->Init(delegate, view, frame_data, change_id);
    if (frame_data[0]->frame_id == view->id())
      (*instances_)[frame_data[0]->frame_id] = frame_tree;
  } else {
    // We're going to share a frame tree. There are two possibilities:
    // . We already know about the frame, in which case we swap it to local.
    // . We don't know about the frame (most likely because of timing issues),
    //   but we better know about the parent. Create a new frame for it.
    CHECK(view->id() != frame_data[0]->frame_id);
    HTMLFrame* existing_frame = frame_tree->root_->FindFrame(view->id());
    size_t frame_data_index = 0u;
    CHECK(FindFrameDataIndex(frame_data, view->id(), &frame_data_index));
    const mandoline::FrameDataPtr& data = frame_data[frame_data_index];
    if (existing_frame) {
      CHECK(!existing_frame->IsLocal());
      existing_frame->SwapToLocal(delegate, view, data->client_properties);
    } else {
      HTMLFrame* parent = frame_tree->root_->FindFrame(data->parent_id);
      CHECK(parent);
      HTMLFrame::CreateParams params(frame_tree, parent, view->id(), view,
                                     data->client_properties, delegate);
      delegate->CreateHTMLFrame(&params);
    }
  }

  HTMLFrame* frame = frame_tree->root_->FindFrame(view->id());
  DCHECK(frame);
  frame->Bind(frame_tree_server.Pass(), frame_tree_client_request.Pass());
  return frame;
}

blink::WebView* HTMLFrameTreeManager::GetWebView() {
  return root_->web_view();
}

void HTMLFrameTreeManager::OnFrameDestroyed(HTMLFrame* frame) {
  if (frame == root_)
    root_ = nullptr;

  if (frame == local_root_)
    local_root_ = nullptr;

  if (!in_process_on_frame_removed_)
    pending_remove_ids_.insert(frame->id());

  if (!local_root_ || !local_root_->HasLocalDescendant())
    delete this;
}

HTMLFrameTreeManager::HTMLFrameTreeManager(GlobalState* global_state)
    : global_state_(global_state),
      root_(nullptr),
      local_root_(nullptr),
      change_id_(0u),
      in_process_on_frame_removed_(false),
      weak_factory_(this) {}

HTMLFrameTreeManager::~HTMLFrameTreeManager() {
  DCHECK(!root_ || !local_root_);
  RemoveFromInstances();
}

void HTMLFrameTreeManager::Init(
    HTMLFrameDelegate* delegate,
    mojo::View* local_view,
    const mojo::Array<mandoline::FrameDataPtr>& frame_data,
    uint32_t change_id) {
  change_id_ = change_id;
  root_ = BuildFrameTree(delegate, frame_data, local_view->id(), local_view);
  local_root_ = root_->FindFrame(local_view->id());
  CHECK(local_root_);
  local_root_->UpdateFocus();
}

HTMLFrame* HTMLFrameTreeManager::BuildFrameTree(
    HTMLFrameDelegate* delegate,
    const mojo::Array<mandoline::FrameDataPtr>& frame_data,
    uint32_t local_frame_id,
    mojo::View* local_view) {
  std::vector<HTMLFrame*> parents;
  HTMLFrame* root = nullptr;
  HTMLFrame* last_frame = nullptr;
  for (size_t i = 0; i < frame_data.size(); ++i) {
    if (last_frame && frame_data[i]->parent_id == last_frame->id()) {
      parents.push_back(last_frame);
    } else if (!parents.empty()) {
      while (parents.back()->id() != frame_data[i]->parent_id)
        parents.pop_back();
    }
    HTMLFrame::CreateParams params(this,
                                   !parents.empty() ? parents.back() : nullptr,
                                   frame_data[i]->frame_id, local_view,
                                   frame_data[i]->client_properties, nullptr);
    if (frame_data[i]->frame_id == local_frame_id)
      params.delegate = delegate;

    HTMLFrame* frame = delegate->CreateHTMLFrame(&params);
    if (!last_frame)
      root = frame;
    else
      DCHECK(frame->parent());
    last_frame = frame;
  }
  return root;
}

void HTMLFrameTreeManager::RemoveFromInstances() {
  for (auto pair : *instances_) {
    if (pair.second == this) {
      instances_->erase(pair.first);
      return;
    }
  }
}

bool HTMLFrameTreeManager::PrepareForStructureChange(HTMLFrame* source,
                                                     uint32_t change_id) {
  // The change ids may differ if multiple HTMLDocuments are attached to the
  // same tree (which means we have multiple FrameTreeClients for the same
  // tree).
  if (change_id != (change_id_ + 1))
    return false;

  // We only process changes for the topmost local root.
  if (source != local_root_)
    return false;

  // Update the id as the change is going to be applied (or we can assume it
  // will be applied if we get here).
  change_id_ = change_id;
  return true;
}

void HTMLFrameTreeManager::ProcessOnFrameAdded(
    HTMLFrame* source,
    uint32_t change_id,
    mandoline::FrameDataPtr frame_data) {
  if (!PrepareForStructureChange(source, change_id))
    return;

  HTMLFrame* parent = root_->FindFrame(frame_data->parent_id);
  if (!parent) {
    DVLOG(1) << "Received invalid parent in OnFrameAdded "
             << frame_data->parent_id;
    return;
  }
  if (root_->FindFrame(frame_data->frame_id)) {
    DVLOG(1) << "Child with id already exists in OnFrameAdded "
             << frame_data->frame_id;
    return;
  }

  // Because notification is async it's entirely possible for us to create a
  // new frame, and remove it before we get the add from the server. This check
  // ensures we don't add back a frame we explicitly removed.
  if (pending_remove_ids_.count(frame_data->frame_id))
    return;

  HTMLFrame::CreateParams params(this, parent, frame_data->frame_id, nullptr,
                                 frame_data->client_properties, nullptr);
  // |parent| takes ownership of created HTMLFrame.
  source->GetLocalRoot()->delegate_->CreateHTMLFrame(&params);
}

void HTMLFrameTreeManager::ProcessOnFrameRemoved(HTMLFrame* source,
                                                 uint32_t change_id,
                                                 uint32_t frame_id) {
  if (!PrepareForStructureChange(source, change_id))
    return;

  pending_remove_ids_.erase(frame_id);

  HTMLFrame* frame = root_->FindFrame(frame_id);
  if (!frame) {
    DVLOG(1) << "OnFrameRemoved with unknown frame " << frame_id;
    return;
  }

  // We shouldn't see requests to remove the root.
  if (frame == root_) {
    DVLOG(1) << "OnFrameRemoved supplied root; ignoring";
    return;
  }

  // Requests to remove local frames are followed by the View being destroyed.
  // We handle destruction there.
  if (frame->IsLocal())
    return;

  DCHECK(!in_process_on_frame_removed_);
  in_process_on_frame_removed_ = true;
  base::WeakPtr<HTMLFrameTreeManager> ref(weak_factory_.GetWeakPtr());
  frame->Close();
  if (!ref)
    return;  // We were deleted.

  in_process_on_frame_removed_ = false;
}

void HTMLFrameTreeManager::ProcessOnFrameClientPropertyChanged(
    HTMLFrame* source,
    uint32_t frame_id,
    const mojo::String& name,
    mojo::Array<uint8_t> new_data) {
  if (source != local_root_)
    return;

  HTMLFrame* frame = root_->FindFrame(frame_id);
  if (frame)
    frame->SetValueFromClientProperty(name, new_data.Pass());
}

}  // namespace mojo