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 // Deletes the children and initializes the new FrameTreeClient.
56 void Swap(FrameTreeClient
* frame_tree_client
,
57 scoped_ptr
<FrameUserData
> user_data
);
59 // Walks the View tree starting at |view| going up returning the first
60 // Frame that is associated with |view|. For example, if |view|
61 // has a Frame associated with it, then that is returned. Otherwise
62 // this checks view->parent() and so on.
63 static Frame
* FindFirstFrameAncestor(mojo::View
* view
);
65 FrameTree
* tree() { return tree_
; }
67 Frame
* parent() { return parent_
; }
68 const Frame
* parent() const { return parent_
; }
70 mojo::View
* view() { return view_
; }
71 const mojo::View
* view() const { return view_
; }
73 uint32_t id() const { return id_
; }
75 const ClientPropertyMap
& client_properties() const {
76 return client_properties_
;
79 // Finds the descendant with the specified id.
80 Frame
* FindFrame(uint32_t id
) {
81 return const_cast<Frame
*>(const_cast<const Frame
*>(this)->FindFrame(id
));
83 const Frame
* FindFrame(uint32_t id
) const;
85 bool HasAncestor(const Frame
* frame
) const;
87 FrameUserData
* user_data() { return user_data_
.get(); }
89 const std::vector
<Frame
*>& children() { return children_
; }
91 // Returns true if this Frame or any child Frame is loading.
92 bool IsLoading() const;
94 // Returns the sum total of loading progress from this Frame and all of its
95 // children, as well as the number of Frames accumulated.
96 double GatherProgress(int* frame_count
) const;
99 friend class FrameTree
;
101 // Initializes the client by sending it the state of the tree.
104 void SetView(mojo::View
* view
);
106 // Returns the first ancestor (starting at |this|) that has a
108 Frame
* GetAncestorWithFrameTreeClient();
110 // Adds this to |frames| and recurses through the children calling the
112 void BuildFrameTree(std::vector
<const Frame
*>* frames
) const;
114 void Add(Frame
* node
);
115 void Remove(Frame
* node
);
117 // The implementation of the various FrameTreeServer functions that take
118 // frame_id call into these.
119 void LoadingStartedImpl();
120 void LoadingStoppedImpl();
121 void ProgressChangedImpl(double progress
);
122 void SetClientPropertyImpl(const mojo::String
& name
,
123 mojo::Array
<uint8_t> value
);
125 // Returns the Frame whose id is |frame_id|. Returns nullptr if |frame_id| is
126 // not from the same connection as this.
127 Frame
* FindTargetFrame(uint32_t frame_id
);
129 // Notifies the client and all descendants as appropriate.
130 void NotifyAdded(const Frame
* source
,
131 const Frame
* added_node
,
133 void NotifyRemoved(const Frame
* source
,
134 const Frame
* removed_node
,
136 void NotifyClientPropertyChanged(const Frame
* source
,
137 const mojo::String
& name
,
138 const mojo::Array
<uint8_t>& value
);
140 // mojo::ViewObserver:
141 void OnTreeChanged(const TreeChangeParams
& params
) override
;
142 void OnViewDestroying(mojo::View
* view
) override
;
145 void PostMessageEventToFrame(uint32_t source_frame_id
,
146 uint32_t target_frame_id
,
147 HTMLMessageEventPtr event
) override
;
148 void LoadingStarted(uint32_t frame_id
) override
;
149 void LoadingStopped(uint32_t frame_id
) override
;
150 void ProgressChanged(uint32_t frame_id
, double progress
) override
;
151 void SetClientProperty(uint32_t frame_id
,
152 const mojo::String
& name
,
153 mojo::Array
<uint8_t> value
) override
;
157 mojo::Map
<mojo::String
, mojo::Array
<uint8_t>> client_properties
) override
;
158 void RequestNavigate(NavigationTargetType target_type
,
159 uint32_t target_frame_id
,
160 mojo::URLRequestPtr request
) override
;
161 void DidNavigateLocally(uint32_t frame_id
, const mojo::String
& url
) override
;
163 FrameTree
* const tree_
;
164 // WARNING: this may be null. See class description for details.
168 ViewOwnership view_ownership_
;
169 std::vector
<Frame
*> children_
;
170 scoped_ptr
<FrameUserData
> user_data_
;
172 // WARNING: this may be null. See class description for details.
173 FrameTreeClient
* frame_tree_client_
;
178 ClientPropertyMap client_properties_
;
180 mojo::Binding
<FrameTreeServer
> frame_tree_server_binding_
;
182 DISALLOW_COPY_AND_ASSIGN(Frame
);
185 } // namespace mandoline
187 #endif // MANDOLINE_TAB_FRAME_H_