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 MANDOLINE_TAB_FRAME_H_
6 #define MANDOLINE_TAB_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 "mandoline/tab/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.
40 class Frame
: public mojo::ViewObserver
, public FrameTreeServer
{
42 using ClientPropertyMap
= std::map
<std::string
, std::vector
<uint8_t>>;
44 Frame(FrameTree
* tree
,
47 ViewOwnership view_ownership
,
48 FrameTreeClient
* frame_tree_client
,
49 scoped_ptr
<FrameUserData
> user_data
,
50 const ClientPropertyMap
& client_properties
);
53 void Init(Frame
* parent
);
55 // Walks the View tree starting at |view| going up returning the first
56 // Frame that is associated with |view|. For example, if |view|
57 // has a Frame associated with it, then that is returned. Otherwise
58 // this checks view->parent() and so on.
59 static Frame
* FindFirstFrameAncestor(mojo::View
* view
);
61 FrameTree
* tree() { return tree_
; }
63 Frame
* parent() { return parent_
; }
64 const Frame
* parent() const { return parent_
; }
66 mojo::View
* view() { return view_
; }
67 const mojo::View
* view() const { return view_
; }
69 uint32_t id() const { return id_
; }
71 const ClientPropertyMap
& client_properties() const {
72 return client_properties_
;
75 // Finds the descendant with the specified id.
76 Frame
* FindFrame(uint32_t id
) {
77 return const_cast<Frame
*>(const_cast<const Frame
*>(this)->FindFrame(id
));
79 const Frame
* FindFrame(uint32_t id
) const;
81 bool HasAncestor(const Frame
* frame
) const;
83 FrameUserData
* user_data() { return user_data_
.get(); }
85 const std::vector
<Frame
*>& children() { return children_
; }
87 // Returns true if this Frame or any child Frame is loading.
88 bool IsLoading() const;
90 // Returns the sum total of loading progress from this Frame and all of its
91 // children, as well as the number of Frames accumulated.
92 double GatherProgress(int* frame_count
) const;
95 friend class FrameTree
;
97 // Initializes the client by sending it the state of the tree.
100 // Called in response to FrameTreeClient::OnWillNavigate().
101 void OnWillNavigateAck(FrameTreeClient
* frame_tree_client
,
102 scoped_ptr
<FrameUserData
> user_data
,
103 mojo::ViewTreeClientPtr view_tree_client
);
105 void SetView(mojo::View
* view
);
107 // Returns the first ancestor (starting at |this|) that has a
109 Frame
* GetAncestorWithFrameTreeClient();
111 // Adds this to |frames| and recurses through the children calling the
113 void BuildFrameTree(std::vector
<const Frame
*>* frames
) const;
115 void Add(Frame
* node
);
116 void Remove(Frame
* node
);
118 // Starts a new navigation to |request|. The navigation proceeds as long
119 // as there is a View and once OnWillNavigate() has returned. If there is
120 // no View the navigation waits until the View is available.
121 void StartNavigate(mojo::URLRequestPtr request
);
123 // The implementation of the various FrameTreeServer functions that take
124 // frame_id call into these.
125 void LoadingStartedImpl();
126 void LoadingStoppedImpl();
127 void ProgressChangedImpl(double progress
);
128 void SetClientPropertyImpl(const mojo::String
& name
,
129 mojo::Array
<uint8_t> value
);
131 // Returns the Frame whose id is |frame_id|. Returns nullptr if |frame_id| is
132 // not from the same connection as this.
133 Frame
* FindTargetFrame(uint32_t frame_id
);
135 // Notifies the client and all descendants as appropriate.
136 void NotifyAdded(const Frame
* source
,
137 const Frame
* added_node
,
139 void NotifyRemoved(const Frame
* source
,
140 const Frame
* removed_node
,
142 void NotifyClientPropertyChanged(const Frame
* source
,
143 const mojo::String
& name
,
144 const mojo::Array
<uint8_t>& value
);
146 // mojo::ViewObserver:
147 void OnTreeChanged(const TreeChangeParams
& params
) override
;
148 void OnViewDestroying(mojo::View
* view
) override
;
151 void PostMessageEventToFrame(uint32_t source_frame_id
,
152 uint32_t target_frame_id
,
153 HTMLMessageEventPtr event
) override
;
154 void LoadingStarted(uint32_t frame_id
) override
;
155 void LoadingStopped(uint32_t frame_id
) override
;
156 void ProgressChanged(uint32_t frame_id
, double progress
) override
;
157 void SetClientProperty(uint32_t frame_id
,
158 const mojo::String
& name
,
159 mojo::Array
<uint8_t> value
) override
;
163 mojo::Map
<mojo::String
, mojo::Array
<uint8_t>> client_properties
) override
;
164 void RequestNavigate(NavigationTargetType target_type
,
165 uint32_t target_frame_id
,
166 mojo::URLRequestPtr request
) override
;
167 void DidNavigateLocally(uint32_t frame_id
, const mojo::String
& url
) override
;
169 FrameTree
* const tree_
;
170 // WARNING: this may be null. See class description for details.
174 ViewOwnership view_ownership_
;
175 std::vector
<Frame
*> children_
;
176 scoped_ptr
<FrameUserData
> user_data_
;
178 // WARNING: this may be null. See class description for details.
179 FrameTreeClient
* frame_tree_client_
;
184 ClientPropertyMap client_properties_
;
186 // StartNavigate() stores the request here if the view isn't available at
187 // the time of StartNavigate().
188 mojo::URLRequestPtr pending_navigate_
;
190 mojo::Binding
<FrameTreeServer
> frame_tree_server_binding_
;
192 base::WeakPtrFactory
<Frame
> weak_factory_
;
194 DISALLOW_COPY_AND_ASSIGN(Frame
);
197 } // namespace mandoline
199 #endif // MANDOLINE_TAB_FRAME_H_