Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / web_view / frame.h
blob9356a6cf34a7041d16719611ba4d22c160c8d830
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_
8 #include <map>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "components/mus/public/cpp/types.h"
14 #include "components/mus/public/cpp/view_observer.h"
15 #include "components/web_view/public/interfaces/frame_tree.mojom.h"
16 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
18 namespace web_view {
20 class FrameTest;
21 class FrameTree;
22 class FrameTreeClient;
23 class FrameUserData;
25 enum class ViewOwnership {
26 OWNS_VIEW,
27 DOESNT_OWN_VIEW,
30 // Frame represents an embedding in a frame. Frames own their children.
31 // Frames automatically delete themself if the View the frame is associated
32 // with is deleted.
34 // In general each Frame has a View. When a new Frame is created by a client
35 // there may be a small amount of time where the View is not yet known
36 // (separate pipes are used for the view and frame, resulting in undefined
37 // message ordering). In this case the view is null and will be set once we
38 // see the view (OnTreeChanged()).
40 // Each frame has an identifier of the app providing the FrameTreeClient
41 // (|app_id|). This id is used when servicing a request to navigate the frame.
42 // When navigating, if the id of the new app matches that of the existing app,
43 // then it is expected that the new FrameTreeClient will take over rendering to
44 // the existing view. Because of this a new ViewTreeClient is not obtained and
45 // Embed() is not invoked on the View. The FrameTreeClient can detect this case
46 // by the argument |reuse_existing_view| supplied to OnConnect(). Typically the
47 // id is that of content handler id, but this is left up to the
48 // FrameTreeDelegate to decide.
49 class Frame : public mus::ViewObserver, public FrameTreeServer {
50 public:
51 using ClientPropertyMap = std::map<std::string, std::vector<uint8_t>>;
53 Frame(FrameTree* tree,
54 mus::View* view,
55 uint32_t frame_id,
56 uint32_t app_id,
57 ViewOwnership view_ownership,
58 FrameTreeClient* frame_tree_client,
59 scoped_ptr<FrameUserData> user_data,
60 const ClientPropertyMap& client_properties);
61 ~Frame() override;
63 void Init(Frame* parent,
64 mojo::ViewTreeClientPtr view_tree_client,
65 mojo::InterfaceRequest<FrameTreeServer> server_request);
67 // Walks the View tree starting at |view| going up returning the first
68 // Frame that is associated with |view|. For example, if |view|
69 // has a Frame associated with it, then that is returned. Otherwise
70 // this checks view->parent() and so on.
71 static Frame* FindFirstFrameAncestor(mus::View* view);
73 FrameTree* tree() { return tree_; }
75 Frame* parent() { return parent_; }
76 const Frame* parent() const { return parent_; }
78 mus::View* view() { return view_; }
79 const mus::View* view() const { return view_; }
81 uint32_t id() const { return id_; }
83 uint32_t app_id() const { return app_id_; }
85 const ClientPropertyMap& client_properties() const {
86 return client_properties_;
89 // Finds the descendant with the specified id.
90 Frame* FindFrame(uint32_t id) {
91 return const_cast<Frame*>(const_cast<const Frame*>(this)->FindFrame(id));
93 const Frame* FindFrame(uint32_t id) const;
95 bool HasAncestor(const Frame* frame) const;
97 FrameUserData* user_data() { return user_data_.get(); }
99 const std::vector<Frame*>& children() const { return children_; }
101 // Returns true if this Frame or any child Frame is loading.
102 bool IsLoading() const;
104 // Returns the sum total of loading progress from this Frame and all of its
105 // children, as well as the number of Frames accumulated.
106 double GatherProgress(int* frame_count) const;
108 private:
109 friend class FrameTest;
110 friend class FrameTree;
112 // Identifies whether the FrameTreeClient is from the same app or a different
113 // app.
114 enum class ClientType {
115 // The client is either the root frame, or navigating an existing frame
116 // to a different app.
117 EXISTING_FRAME_NEW_APP,
119 // The client is the result of navigating an existing frame to a new app.
120 EXISTING_FRAME_SAME_APP,
122 // The client is the result of a new frame (not the root).
123 NEW_CHILD_FRAME
126 struct FrameTreeServerBinding;
128 // Initializes the client by sending it the state of the tree.
129 // |frame_tree_server_binding| contains the current FrameTreeServerBinding
130 // (if any) and is destroyed after the connection responds to OnConnect().
132 // If |client_type| is SAME_APP we can't destroy the existing client
133 // (and related data) until we get back the ack from OnConnect(). This way
134 // we know the client has completed the switch. If we did not do this it
135 // would be possible for the app to see it's existing FrameTreeServer
136 // connection lost (and assume the frame is being torn down) before the
137 // OnConnect().
138 void InitClient(ClientType client_type,
139 scoped_ptr<FrameTreeServerBinding> frame_tree_server_binding,
140 mojo::ViewTreeClientPtr view_tree_client,
141 mojo::InterfaceRequest<FrameTreeServer> server_request);
143 // Callback from OnConnect(). This does nothing (other than destroying
144 // |frame_tree_server_binding|). See InitClient() for details as to why
145 // destruction of |frame_tree_server_binding| happens after OnConnect().
146 static void OnConnectAck(
147 scoped_ptr<FrameTreeServerBinding> frame_tree_server_binding);
149 // Callback from OnEmbed().
150 void OnEmbedAck(bool success, mus::ConnectionSpecificId connection_id);
152 // Completes a navigation request; swapping the existing FrameTreeClient to
153 // the supplied arguments.
154 void ChangeClient(FrameTreeClient* frame_tree_client,
155 scoped_ptr<FrameUserData> user_data,
156 mojo::ViewTreeClientPtr view_tree_client,
157 uint32 app_id);
159 void SetView(mus::View* view);
161 // Adds this to |frames| and recurses through the children calling the
162 // same function.
163 void BuildFrameTree(std::vector<const Frame*>* frames) const;
165 void Add(Frame* node);
166 void Remove(Frame* node);
168 // Starts a new navigation to |request|. The navigation proceeds as long
169 // as there is a View and once OnWillNavigate() has returned. If there is
170 // no View the navigation waits until the View is available.
171 void StartNavigate(mojo::URLRequestPtr request);
172 void OnCanNavigateFrame(uint32_t app_id,
173 FrameTreeClient* frame_tree_client,
174 scoped_ptr<FrameUserData> user_data,
175 mojo::ViewTreeClientPtr view_tree_client);
177 // Notifies the client and all descendants as appropriate.
178 void NotifyAdded(const Frame* source,
179 const Frame* added_node,
180 uint32_t change_id);
181 void NotifyRemoved(const Frame* source,
182 const Frame* removed_node,
183 uint32_t change_id);
184 void NotifyClientPropertyChanged(const Frame* source,
185 const mojo::String& name,
186 const mojo::Array<uint8_t>& value);
187 void NotifyFrameLoadingStateChanged(const Frame* frame, bool loading);
188 void NotifyDispatchFrameLoadEvent(const Frame* frame);
190 // mus::ViewObserver:
191 void OnTreeChanged(const TreeChangeParams& params) override;
192 void OnViewDestroying(mus::View* view) override;
193 void OnViewEmbeddedAppDisconnected(mus::View* view) override;
195 // FrameTreeServer:
196 void PostMessageEventToFrame(uint32_t target_frame_id,
197 HTMLMessageEventPtr event) override;
198 void LoadingStateChanged(bool loading, double progress) override;
199 void TitleChanged(const mojo::String& title) override;
200 void DidCommitProvisionalLoad() override;
201 void SetClientProperty(const mojo::String& name,
202 mojo::Array<uint8_t> value) override;
203 void OnCreatedFrame(
204 mojo::InterfaceRequest<FrameTreeServer> server_request,
205 FrameTreeClientPtr client,
206 uint32_t frame_id,
207 mojo::Map<mojo::String, mojo::Array<uint8_t>> client_properties) override;
208 void RequestNavigate(NavigationTargetType target_type,
209 uint32_t target_frame_id,
210 mojo::URLRequestPtr request) override;
211 void DidNavigateLocally(const mojo::String& url) override;
212 void DispatchLoadEventToParent() override;
214 FrameTree* const tree_;
215 // WARNING: this may be null. See class description for details.
216 mus::View* view_;
217 // The connection id returned from ViewManager::Embed(). Frames created by
218 // way of OnCreatedFrame() inherit the id from the parent.
219 mus::ConnectionSpecificId embedded_connection_id_;
220 // ID for the frame, which is the same as that of the view.
221 const uint32_t id_;
222 // ID of the app providing the FrameTreeClient and ViewTreeClient.
223 uint32_t app_id_;
224 Frame* parent_;
225 ViewOwnership view_ownership_;
226 std::vector<Frame*> children_;
227 scoped_ptr<FrameUserData> user_data_;
229 FrameTreeClient* frame_tree_client_;
231 bool loading_;
232 double progress_;
234 ClientPropertyMap client_properties_;
236 // StartNavigate() stores the request here if the view isn't available at
237 // the time of StartNavigate().
238 mojo::URLRequestPtr pending_navigate_;
240 scoped_ptr<mojo::Binding<FrameTreeServer>> frame_tree_server_binding_;
242 base::WeakPtrFactory<Frame> embed_weak_ptr_factory_;
244 base::WeakPtrFactory<Frame> navigate_weak_ptr_factory_;
246 DISALLOW_COPY_AND_ASSIGN(Frame);
249 } // namespace web_view
251 #endif // COMPONENTS_WEB_VIEW_FRAME_H_