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/frame.h"
9 #include "base/auto_reset.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/stl_util.h"
13 #include "components/mus/public/cpp/view.h"
14 #include "components/mus/public/cpp/view_property.h"
15 #include "components/web_view/frame_tree.h"
16 #include "components/web_view/frame_tree_delegate.h"
17 #include "components/web_view/frame_user_data.h"
18 #include "components/web_view/frame_utils.h"
19 #include "mojo/application/public/interfaces/shell.mojom.h"
23 DECLARE_VIEW_PROPERTY_TYPE(web_view::Frame
*);
27 // Used to find the Frame associated with a View.
28 DEFINE_LOCAL_VIEW_PROPERTY_KEY(Frame
*, kFrame
, nullptr);
32 const uint32_t kNoParentId
= 0u;
33 const mus::ConnectionSpecificId kInvalidConnectionId
= 0u;
35 mojom::FrameDataPtr
FrameToFrameData(const Frame
* frame
) {
36 mojom::FrameDataPtr
frame_data(mojom::FrameData::New());
37 frame_data
->frame_id
= frame
->id();
38 frame_data
->parent_id
= frame
->parent() ? frame
->parent()->id() : kNoParentId
;
39 frame_data
->client_properties
=
40 mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>::From(
41 frame
->client_properties());
42 return frame_data
.Pass();
47 struct Frame::FrameUserDataAndBinding
{
48 scoped_ptr
<FrameUserData
> user_data
;
49 scoped_ptr
<mojo::Binding
<mojom::Frame
>> frame_binding
;
52 Frame::Frame(FrameTree
* tree
,
56 ViewOwnership view_ownership
,
57 mojom::FrameClient
* frame_client
,
58 scoped_ptr
<FrameUserData
> user_data
,
59 const ClientPropertyMap
& client_properties
)
62 embedded_connection_id_(kInvalidConnectionId
),
66 view_ownership_(view_ownership
),
67 user_data_(user_data
.Pass()),
68 frame_client_(frame_client
),
71 client_properties_(client_properties
),
72 embed_weak_ptr_factory_(this),
73 navigate_weak_ptr_factory_(this) {
80 view_
->RemoveObserver(this);
81 while (!children_
.empty())
84 parent_
->Remove(this);
86 view_
->ClearLocalProperty(kFrame
);
87 if (view_ownership_
== ViewOwnership::OWNS_VIEW
)
90 tree_
->delegate_
->DidDestroyFrame(this);
93 void Frame::Init(Frame
* parent
,
94 mojo::ViewTreeClientPtr view_tree_client
,
95 mojo::InterfaceRequest
<mojom::Frame
> frame_request
) {
97 // Set the FrameClient to null so that we don't notify the client of the
98 // add before OnConnect().
99 base::AutoReset
<mojom::FrameClient
*> frame_client_resetter(&frame_client_
,
105 const ClientType client_type
= frame_request
.is_pending()
106 ? ClientType::NEW_CHILD_FRAME
107 : ClientType::EXISTING_FRAME_NEW_APP
;
108 InitClient(client_type
, nullptr, view_tree_client
.Pass(),
109 frame_request
.Pass());
111 tree_
->delegate_
->DidCreateFrame(this);
115 Frame
* Frame::FindFirstFrameAncestor(View
* view
) {
116 while (view
&& !view
->GetLocalProperty(kFrame
))
117 view
= view
->parent();
118 return view
? view
->GetLocalProperty(kFrame
) : nullptr;
121 const Frame
* Frame::FindFrame(uint32_t id
) const {
125 for (const Frame
* child
: children_
) {
126 const Frame
* match
= child
->FindFrame(id
);
133 bool Frame::HasAncestor(const Frame
* frame
) const {
134 const Frame
* current
= this;
135 while (current
&& current
!= frame
)
136 current
= current
->parent_
;
137 return current
== frame
;
140 bool Frame::IsLoading() const {
143 for (const Frame
* child
: children_
) {
144 if (child
->IsLoading())
150 double Frame::GatherProgress(int* frame_count
) const {
152 double progress
= progress_
;
153 for (const Frame
* child
: children_
)
154 progress
+= child
->GatherProgress(frame_count
);
158 void Frame::InitClient(ClientType client_type
,
159 scoped_ptr
<FrameUserDataAndBinding
> data_and_binding
,
160 mojo::ViewTreeClientPtr view_tree_client
,
161 mojo::InterfaceRequest
<mojom::Frame
> frame_request
) {
162 if (client_type
== ClientType::EXISTING_FRAME_NEW_APP
&&
163 view_tree_client
.get()) {
164 embedded_connection_id_
= kInvalidConnectionId
;
165 embed_weak_ptr_factory_
.InvalidateWeakPtrs();
167 view_tree_client
.Pass(), mojo::ViewTree::ACCESS_POLICY_DEFAULT
,
168 base::Bind(&Frame::OnEmbedAck
, embed_weak_ptr_factory_
.GetWeakPtr()));
171 if (client_type
== ClientType::NEW_CHILD_FRAME
) {
172 // Don't install an error handler. We allow for the target to only
173 // implement ViewTreeClient.
174 // This frame (and client) was created by an existing FrameClient. There
175 // is no need to send it OnConnect().
176 frame_binding_
.reset(
177 new mojo::Binding
<mojom::Frame
>(this, frame_request
.Pass()));
178 frame_client_
->OnConnect(
179 nullptr, tree_
->change_id(), id_
, mojom::VIEW_CONNECT_TYPE_USE_NEW
,
180 mojo::Array
<mojom::FrameDataPtr
>(),
181 base::Bind(&OnConnectAck
, base::Passed(&data_and_binding
)));
183 std::vector
<const Frame
*> frames
;
184 tree_
->root()->BuildFrameTree(&frames
);
186 mojo::Array
<mojom::FrameDataPtr
> array(frames
.size());
187 for (size_t i
= 0; i
< frames
.size(); ++i
)
188 array
[i
] = FrameToFrameData(frames
[i
]).Pass();
190 mojom::FramePtr frame_ptr
;
191 // Don't install an error handler. We allow for the target to only
192 // implement ViewTreeClient.
193 frame_binding_
.reset(
194 new mojo::Binding
<mojom::Frame
>(this, GetProxy(&frame_ptr
).Pass()));
195 frame_client_
->OnConnect(
196 frame_ptr
.Pass(), tree_
->change_id(), id_
,
197 client_type
== ClientType::EXISTING_FRAME_SAME_APP
198 ? mojom::VIEW_CONNECT_TYPE_USE_EXISTING
199 : mojom::VIEW_CONNECT_TYPE_USE_NEW
,
201 base::Bind(&OnConnectAck
, base::Passed(&data_and_binding
)));
202 tree_
->delegate_
->DidStartNavigation(this);
207 void Frame::OnConnectAck(scoped_ptr
<FrameUserDataAndBinding
> data_and_binding
) {
210 void Frame::ChangeClient(mojom::FrameClient
* frame_client
,
211 scoped_ptr
<FrameUserData
> user_data
,
212 mojo::ViewTreeClientPtr view_tree_client
,
214 while (!children_
.empty())
217 ClientType client_type
= view_tree_client
.get() == nullptr
218 ? ClientType::EXISTING_FRAME_SAME_APP
219 : ClientType::EXISTING_FRAME_NEW_APP
;
220 scoped_ptr
<FrameUserDataAndBinding
> data_and_binding
;
222 if (client_type
== ClientType::EXISTING_FRAME_SAME_APP
) {
223 // See comment in InitClient() for details.
224 data_and_binding
.reset(new FrameUserDataAndBinding
);
225 data_and_binding
->user_data
= user_data_
.Pass();
226 data_and_binding
->frame_binding
= frame_binding_
.Pass();
232 user_data_
= user_data
.Pass();
233 frame_client_
= frame_client
;
234 frame_binding_
.reset();
237 InitClient(client_type
, data_and_binding
.Pass(), view_tree_client
.Pass(),
241 void Frame::OnEmbedAck(bool success
, mus::ConnectionSpecificId connection_id
) {
243 embedded_connection_id_
= connection_id
;
246 void Frame::SetView(mus::View
* view
) {
248 DCHECK_EQ(id_
, view
->id());
250 view_
->SetLocalProperty(kFrame
, this);
251 view_
->AddObserver(this);
252 if (pending_navigate_
.get())
253 StartNavigate(pending_navigate_
.Pass());
256 void Frame::BuildFrameTree(std::vector
<const Frame
*>* frames
) const {
257 frames
->push_back(this);
258 for (const Frame
* frame
: children_
)
259 frame
->BuildFrameTree(frames
);
262 void Frame::Add(Frame
* node
) {
263 DCHECK(!node
->parent_
);
265 node
->parent_
= this;
266 children_
.push_back(node
);
268 tree_
->root()->NotifyAdded(this, node
, tree_
->AdvanceChangeID());
271 void Frame::Remove(Frame
* node
) {
272 DCHECK_EQ(node
->parent_
, this);
273 auto iter
= std::find(children_
.begin(), children_
.end(), node
);
274 DCHECK(iter
!= children_
.end());
275 node
->parent_
= nullptr;
276 children_
.erase(iter
);
278 tree_
->root()->NotifyRemoved(this, node
, tree_
->AdvanceChangeID());
281 void Frame::StartNavigate(mojo::URLRequestPtr request
) {
282 pending_navigate_
.reset();
284 // We need a View to navigate. When we get the View we'll complete the
287 pending_navigate_
= request
.Pass();
291 // Drop any pending navigation requests.
292 navigate_weak_ptr_factory_
.InvalidateWeakPtrs();
294 tree_
->delegate_
->CanNavigateFrame(
295 this, request
.Pass(),
296 base::Bind(&Frame::OnCanNavigateFrame
,
297 navigate_weak_ptr_factory_
.GetWeakPtr()));
300 void Frame::OnCanNavigateFrame(uint32_t app_id
,
301 mojom::FrameClient
* frame_client
,
302 scoped_ptr
<FrameUserData
> user_data
,
303 mojo::ViewTreeClientPtr view_tree_client
) {
304 if (AreAppIdsEqual(app_id
, app_id_
)) {
305 // The app currently rendering the frame will continue rendering it. In this
306 // case we do not use the ViewTreeClient (because the app has a View already
307 // and ends up reusing it).
308 DCHECK(!view_tree_client
.get());
310 frame_client_
->OnWillNavigate();
311 DCHECK(view_tree_client
.get());
313 ChangeClient(frame_client
, user_data
.Pass(), view_tree_client
.Pass(), app_id
);
316 void Frame::NotifyAdded(const Frame
* source
,
317 const Frame
* added_node
,
318 uint32_t change_id
) {
319 // |frame_client_| may be null during initial frame creation and parenting.
321 frame_client_
->OnFrameAdded(change_id
, FrameToFrameData(added_node
));
323 for (Frame
* child
: children_
)
324 child
->NotifyAdded(source
, added_node
, change_id
);
327 void Frame::NotifyRemoved(const Frame
* source
,
328 const Frame
* removed_node
,
329 uint32_t change_id
) {
330 frame_client_
->OnFrameRemoved(change_id
, removed_node
->id());
332 for (Frame
* child
: children_
)
333 child
->NotifyRemoved(source
, removed_node
, change_id
);
336 void Frame::NotifyClientPropertyChanged(const Frame
* source
,
337 const mojo::String
& name
,
338 const mojo::Array
<uint8_t>& value
) {
340 frame_client_
->OnFrameClientPropertyChanged(source
->id(), name
,
343 for (Frame
* child
: children_
)
344 child
->NotifyClientPropertyChanged(source
, name
, value
);
347 void Frame::NotifyFrameLoadingStateChanged(const Frame
* frame
, bool loading
) {
348 frame_client_
->OnFrameLoadingStateChanged(frame
->id(), loading
);
351 void Frame::NotifyDispatchFrameLoadEvent(const Frame
* frame
) {
352 frame_client_
->OnDispatchFrameLoadEvent(frame
->id());
355 void Frame::OnTreeChanged(const TreeChangeParams
& params
) {
356 if (params
.new_parent
&& this == tree_
->root()) {
357 Frame
* child_frame
= FindFrame(params
.target
->id());
358 if (child_frame
&& !child_frame
->view_
)
359 child_frame
->SetView(params
.target
);
363 void Frame::OnViewDestroying(mus::View
* view
) {
365 parent_
->Remove(this);
367 // Reset |view_ownership_| so we don't attempt to delete |view_| in the
369 view_ownership_
= ViewOwnership::DOESNT_OWN_VIEW
;
371 if (tree_
->root() == this) {
372 view_
->RemoveObserver(this);
380 void Frame::OnViewEmbeddedAppDisconnected(mus::View
* view
) {
381 // See FrameTreeDelegate::OnViewEmbeddedAppDisconnected() for details of when
384 // Currently we have no way to distinguish between the cases that lead to this
385 // being called, so we assume we can continue on. Continuing on is important
386 // for html as it's entirely possible for a page to create a frame, navigate
387 // to a bogus url and expect the frame to still exist.
388 tree_
->delegate_
->OnViewEmbeddedInFrameDisconnected(this);
391 void Frame::PostMessageEventToFrame(uint32_t target_frame_id
,
392 mojom::HTMLMessageEventPtr event
) {
393 // NOTE: |target_frame_id| is allowed to be from another connection.
394 Frame
* target
= tree_
->root()->FindFrame(target_frame_id
);
395 if (!target
|| target
== this ||
396 !tree_
->delegate_
->CanPostMessageEventToFrame(this, target
, event
.get()))
399 target
->frame_client_
->OnPostMessageEvent(id_
, target_frame_id
, event
.Pass());
402 void Frame::LoadingStateChanged(bool loading
, double progress
) {
403 bool loading_state_changed
= loading_
!= loading
;
405 progress_
= progress
;
406 tree_
->LoadingStateChanged();
408 if (loading_state_changed
&& parent_
&&
409 !AreAppIdsEqual(app_id_
, parent_
->app_id_
)) {
410 // We need to notify the parent if it is in a different app, so that it can
411 // keep track of this frame's loading state. If the parent is in the same
412 // app, we assume that the loading state is propagated directly within the
413 // app itself and no notification is needed from our side.
414 parent_
->NotifyFrameLoadingStateChanged(this, loading_
);
418 void Frame::TitleChanged(const mojo::String
& title
) {
419 // Only care about title changes on the root frame.
421 tree_
->TitleChanged(title
);
424 void Frame::DidCommitProvisionalLoad() {
425 tree_
->DidCommitProvisionalLoad(this);
428 void Frame::SetClientProperty(const mojo::String
& name
,
429 mojo::Array
<uint8_t> value
) {
430 auto iter
= client_properties_
.find(name
);
431 const bool already_in_map
= (iter
!= client_properties_
.end());
432 if (value
.is_null()) {
435 client_properties_
.erase(iter
);
437 std::vector
<uint8_t> as_vector(value
.To
<std::vector
<uint8_t>>());
438 if (already_in_map
&& iter
->second
== as_vector
)
440 client_properties_
[name
] = as_vector
;
442 tree_
->ClientPropertyChanged(this, name
, value
);
445 void Frame::OnCreatedFrame(
446 mojo::InterfaceRequest
<mojom::Frame
> frame_request
,
447 mojom::FrameClientPtr client
,
449 mojo::Map
<mojo::String
, mojo::Array
<uint8_t>> client_properties
) {
450 if ((frame_id
>> 16) != embedded_connection_id_
) {
451 // TODO(sky): kill connection here?
452 // TODO(sky): there is a race in that there is no guarantee we received the
453 // connection id before the frame tries to create a new frame. Ideally we
454 // could pause the frame until we get the connection id, but bindings don't
455 // offer such an API.
456 DVLOG(1) << "OnCreatedFrame supplied invalid frame id, expecting"
457 << embedded_connection_id_
;
461 if (FindFrame(frame_id
)) {
462 // TODO(sky): kill connection here?
463 DVLOG(1) << "OnCreatedFrame supplied id of existing frame.";
467 Frame
* child_frame
= tree_
->CreateChildFrame(
468 this, frame_request
.Pass(), client
.Pass(), frame_id
, app_id_
,
469 client_properties
.To
<ClientPropertyMap
>());
470 child_frame
->embedded_connection_id_
= embedded_connection_id_
;
473 void Frame::RequestNavigate(mojom::NavigationTargetType target_type
,
474 uint32_t target_frame_id
,
475 mojo::URLRequestPtr request
) {
476 if (target_type
== mojom::NAVIGATION_TARGET_TYPE_EXISTING_FRAME
) {
477 // |target_frame| is allowed to come from another connection.
478 Frame
* target_frame
= tree_
->root()->FindFrame(target_frame_id
);
480 DVLOG(1) << "RequestNavigate EXISTING_FRAME with no matching frame";
483 if (target_frame
!= tree_
->root()) {
484 target_frame
->StartNavigate(request
.Pass());
487 // Else case if |target_frame| == root. Treat at top level request.
489 tree_
->delegate_
->NavigateTopLevel(this, request
.Pass());
492 void Frame::DidNavigateLocally(const mojo::String
& url
) {
496 void Frame::DispatchLoadEventToParent() {
497 if (parent_
&& !AreAppIdsEqual(app_id_
, parent_
->app_id_
)) {
498 // Send notification to fire a load event in the parent, if the parent is in
499 // a different app. If the parent is in the same app, we assume that the app
500 // itself handles firing load event directly and no notification is needed
502 parent_
->NotifyDispatchFrameLoadEvent(this);
506 } // namespace web_view