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 // Adds this to |frames| and recurses through the children calling the
108 void BuildFrameTree(std::vector
<const Frame
*>* frames
) const;
110 void Add(Frame
* node
);
111 void Remove(Frame
* node
);
113 // The implementation of the various FrameTreeServer functions that take
114 // frame_id call into these.
115 void LoadingStartedImpl();
116 void LoadingStoppedImpl();
117 void ProgressChangedImpl(double progress
);
118 void SetClientPropertyImpl(const mojo::String
& name
,
119 mojo::Array
<uint8_t> value
);
121 // Returns the Frame whose id is |frame_id|. Returns nullptr if |frame_id| is
122 // not from the same connection as this.
123 Frame
* FindTargetFrame(uint32_t frame_id
);
125 // Notifies the client and all descendants as appropriate.
126 void NotifyAdded(const Frame
* source
,
127 const Frame
* added_node
,
129 void NotifyRemoved(const Frame
* source
,
130 const Frame
* removed_node
,
132 void NotifyClientPropertyChanged(const Frame
* source
,
133 const mojo::String
& name
,
134 const mojo::Array
<uint8_t>& value
);
136 // mojo::ViewObserver:
137 void OnTreeChanged(const TreeChangeParams
& params
) override
;
138 void OnViewDestroying(mojo::View
* view
) override
;
141 void PostMessageEventToFrame(uint32_t frame_id
,
142 MessageEventPtr event
) override
;
143 void LoadingStarted(uint32_t frame_id
) override
;
144 void LoadingStopped(uint32_t frame_id
) override
;
145 void ProgressChanged(uint32_t frame_id
, double progress
) override
;
146 void SetClientProperty(uint32_t frame_id
,
147 const mojo::String
& name
,
148 mojo::Array
<uint8_t> value
) override
;
152 mojo::Map
<mojo::String
, mojo::Array
<uint8_t>> client_properties
) override
;
153 void RequestNavigate(NavigationTargetType target_type
,
154 uint32_t target_frame_id
,
155 mojo::URLRequestPtr request
) override
;
156 void DidNavigateLocally(uint32_t frame_id
, const mojo::String
& url
) override
;
158 FrameTree
* const tree_
;
159 // WARNING: this may be null. See class description for details.
163 ViewOwnership view_ownership_
;
164 std::vector
<Frame
*> children_
;
165 scoped_ptr
<FrameUserData
> user_data_
;
167 // WARNING: this may be null. See class description for details.
168 FrameTreeClient
* frame_tree_client_
;
173 ClientPropertyMap client_properties_
;
175 mojo::Binding
<FrameTreeServer
> frame_tree_server_binding_
;
177 DISALLOW_COPY_AND_ASSIGN(Frame
);
180 } // namespace mandoline
182 #endif // MANDOLINE_TAB_FRAME_H_