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 #ifndef COMPONENTS_WEB_VIEW_FRAME_H_
6 #define COMPONENTS_WEB_VIEW_FRAME_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "components/view_manager/public/cpp/view_observer.h"
14 #include "components/web_view/public/interfaces/frame_tree.mojom.h"
15 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
20 class FrameTreeClient
;
23 enum class ViewOwnership
{
28 // Frame represents an embedding in a frame. Frames own their children.
29 // Frames automatically delete themself if the View the frame is associated
32 // In general each Frame has a View. When a new Frame is created by a client
33 // there may be a small amount of time where the View is not yet known to us
34 // (separate pipes are used for the view and frame, resulting in undefined
35 // message ordering). In this case the view is null and will be set once we
36 // see the view (OnTreeChanged()).
38 // When the FrameTreeClient creates a new Frame there is no associated
39 // FrameTreeClient for the child Frame.
41 // Each frame has an identifier of the app providing the FrameTreeClient
42 // (|app_id|). This id is used when servicing a request to navigate the frame.
43 // When navigating, if the id of the new app matches that of the existing app,
44 // then it is expected that the new FrameTreeClient will take over rendering to
45 // the existing view. Because of this a new ViewTreeClient is not obtained and
46 // Embed() is not invoked on the View. The FrameTreeClient can detect this case
47 // by the argument |reuse_existing_view| supplied to OnConnect(). Typically the
48 // id is that of content handler id, but this is left up to the
49 // FrameTreeDelegate to decide.
50 class Frame
: public mojo::ViewObserver
, public FrameTreeServer
{
52 using ClientPropertyMap
= std::map
<std::string
, std::vector
<uint8_t>>;
54 Frame(FrameTree
* tree
,
58 ViewOwnership view_ownership
,
59 FrameTreeClient
* frame_tree_client
,
60 scoped_ptr
<FrameUserData
> user_data
,
61 const ClientPropertyMap
& client_properties
);
64 void Init(Frame
* parent
);
66 // Walks the View tree starting at |view| going up returning the first
67 // Frame that is associated with |view|. For example, if |view|
68 // has a Frame associated with it, then that is returned. Otherwise
69 // this checks view->parent() and so on.
70 static Frame
* FindFirstFrameAncestor(mojo::View
* view
);
72 FrameTree
* tree() { return tree_
; }
74 Frame
* parent() { return parent_
; }
75 const Frame
* parent() const { return parent_
; }
77 mojo::View
* view() { return view_
; }
78 const mojo::View
* view() const { return view_
; }
80 uint32_t id() const { return id_
; }
82 uint32_t app_id() const { return app_id_
; }
84 const ClientPropertyMap
& client_properties() const {
85 return client_properties_
;
88 // Finds the descendant with the specified id.
89 Frame
* FindFrame(uint32_t id
) {
90 return const_cast<Frame
*>(const_cast<const Frame
*>(this)->FindFrame(id
));
92 const Frame
* FindFrame(uint32_t id
) const;
94 bool HasAncestor(const Frame
* frame
) const;
96 FrameUserData
* user_data() { return user_data_
.get(); }
98 const std::vector
<Frame
*>& children() { return children_
; }
100 // Returns true if this Frame or any child Frame is loading.
101 bool IsLoading() const;
103 // Returns the sum total of loading progress from this Frame and all of its
104 // children, as well as the number of Frames accumulated.
105 double GatherProgress(int* frame_count
) const;
108 friend class FrameTree
;
110 // Identifies whether the FrameTreeClient is from the same app or a different
112 enum class ClientType
{ SAME_APP
, NEW_APP
};
114 struct FrameTreeServerBinding
;
116 // Initializes the client by sending it the state of the tree.
117 // |frame_tree_server_binding| contains the current FrameTreeServerBinding
118 // (if any) and is destroyed after the connection responds to OnConnect().
120 // If |client_type| is SAME_APP we can't destroy the existing client
121 // (and related data) until we get back the ack from OnConnect(). This way
122 // we know the client has completed the switch. If we did not do this it
123 // would be possible for the app to see it's existing FrameTreeServer
124 // connection lost (and assume the frame is being torn down) before the
126 void InitClient(ClientType client_type
,
127 scoped_ptr
<FrameTreeServerBinding
> frame_tree_server_binding
);
129 // Callback from OnConnect(). This does nothing (other than destroying
130 // |frame_tree_server_binding|). See InitClient() for details as to why
131 // destruction of |frame_tree_server_binding| happens after OnConnect().
132 static void OnConnectAck(
133 scoped_ptr
<FrameTreeServerBinding
> frame_tree_server_binding
);
135 // Completes a navigation request; swapping the existing FrameTreeClient to
136 // the supplied arguments.
137 void ChangeClient(FrameTreeClient
* frame_tree_client
,
138 scoped_ptr
<FrameUserData
> user_data
,
139 mojo::ViewTreeClientPtr view_tree_client
,
142 void SetView(mojo::View
* view
);
144 // Returns the first ancestor (starting at |this|) that has a
146 Frame
* GetAncestorWithFrameTreeClient();
148 // Adds this to |frames| and recurses through the children calling the
150 void BuildFrameTree(std::vector
<const Frame
*>* frames
) const;
152 void Add(Frame
* node
);
153 void Remove(Frame
* node
);
155 // Starts a new navigation to |request|. The navigation proceeds as long
156 // as there is a View and once OnWillNavigate() has returned. If there is
157 // no View the navigation waits until the View is available.
158 void StartNavigate(mojo::URLRequestPtr request
);
159 void OnCanNavigateFrame(uint32_t app_id
,
160 FrameTreeClient
* frame_tree_client
,
161 scoped_ptr
<FrameUserData
> user_data
,
162 mojo::ViewTreeClientPtr view_tree_client
);
164 // The implementation of the various FrameTreeServer functions that take
165 // frame_id call into these.
166 void LoadingStartedImpl();
167 void LoadingStoppedImpl();
168 void ProgressChangedImpl(double progress
);
169 void TitleChangedImpl(const mojo::String
& title
);
170 void SetClientPropertyImpl(const mojo::String
& name
,
171 mojo::Array
<uint8_t> value
);
173 // Returns the Frame whose id is |frame_id|. Returns nullptr if |frame_id| is
174 // not from the same connection as this.
175 Frame
* FindFrameWithIdFromSameApp(uint32_t frame_id
);
177 // Notifies the client and all descendants as appropriate.
178 void NotifyAdded(const Frame
* source
,
179 const Frame
* added_node
,
181 void NotifyRemoved(const Frame
* source
,
182 const Frame
* removed_node
,
184 void NotifyClientPropertyChanged(const Frame
* source
,
185 const mojo::String
& name
,
186 const mojo::Array
<uint8_t>& value
);
188 // mojo::ViewObserver:
189 void OnTreeChanged(const TreeChangeParams
& params
) override
;
190 void OnViewDestroying(mojo::View
* view
) override
;
193 void PostMessageEventToFrame(uint32_t source_frame_id
,
194 uint32_t target_frame_id
,
195 HTMLMessageEventPtr event
) override
;
196 void LoadingStarted(uint32_t frame_id
) override
;
197 void LoadingStopped(uint32_t frame_id
) override
;
198 void ProgressChanged(uint32_t frame_id
, double progress
) override
;
199 void TitleChanged(uint32_t frame_id
, const mojo::String
& title
) override
;
200 void SetClientProperty(uint32_t frame_id
,
201 const mojo::String
& name
,
202 mojo::Array
<uint8_t> value
) override
;
206 mojo::Map
<mojo::String
, mojo::Array
<uint8_t>> client_properties
) override
;
207 void RequestNavigate(NavigationTargetType target_type
,
208 uint32_t target_frame_id
,
209 mojo::URLRequestPtr request
) override
;
210 void DidNavigateLocally(uint32_t frame_id
, const mojo::String
& url
) override
;
212 FrameTree
* const tree_
;
213 // WARNING: this may be null. See class description for details.
215 // ID for the frame, which is the same as that of the view.
217 // ID of the app providing the FrameTreeClient and ViewTreeClient.
220 ViewOwnership view_ownership_
;
221 std::vector
<Frame
*> children_
;
222 scoped_ptr
<FrameUserData
> user_data_
;
224 // WARNING: this may be null. See class description for details.
225 FrameTreeClient
* frame_tree_client_
;
230 ClientPropertyMap client_properties_
;
232 // StartNavigate() stores the request here if the view isn't available at
233 // the time of StartNavigate().
234 mojo::URLRequestPtr pending_navigate_
;
236 scoped_ptr
<mojo::Binding
<FrameTreeServer
>> frame_tree_server_binding_
;
238 base::WeakPtrFactory
<Frame
> weak_factory_
;
240 base::WeakPtrFactory
<Frame
> navigate_weak_ptr_factory_
;
242 DISALLOW_COPY_AND_ASSIGN(Frame
);
245 } // namespace web_view
247 #endif // COMPONENTS_WEB_VIEW_FRAME_H_