Update V8 to version 4.6.56.
[chromium-blink-merge.git] / mandoline / tab / frame.h
blob5e8d1e875fc6deda4c909c8939b390e1e8f54b32
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_
8 #include <map>
9 #include <vector>
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"
17 namespace mandoline {
19 class FrameTree;
20 class FrameTreeClient;
21 class FrameUserData;
23 enum class ViewOwnership {
24 OWNS_VIEW,
25 DOESNT_OWN_VIEW,
28 // Frame represents an embedding in a frame. Frames own their children.
29 // Frames automatically delete themself if the View the frame is associated
30 // with is deleted.
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 {
41 public:
42 using ClientPropertyMap = std::map<std::string, std::vector<uint8_t>>;
44 Frame(FrameTree* tree,
45 mojo::View* view,
46 uint32_t id,
47 ViewOwnership view_ownership,
48 FrameTreeClient* frame_tree_client,
49 scoped_ptr<FrameUserData> user_data,
50 const ClientPropertyMap& client_properties);
51 ~Frame() override;
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;
98 private:
99 friend class FrameTree;
101 // Initializes the client by sending it the state of the tree.
102 void InitClient();
104 void SetView(mojo::View* view);
106 // Adds this to |frames| and recurses through the children calling the
107 // same function.
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, const Frame* added_node);
127 void NotifyRemoved(const Frame* source, const Frame* removed_node);
128 void NotifyClientPropertyChanged(const Frame* source,
129 const mojo::String& name,
130 const mojo::Array<uint8_t>& value);
132 // mojo::ViewObserver:
133 void OnTreeChanged(const TreeChangeParams& params) override;
134 void OnViewDestroying(mojo::View* view) override;
136 // FrameTreeServer:
137 void PostMessageEventToFrame(uint32_t frame_id,
138 MessageEventPtr event) override;
139 void LoadingStarted(uint32_t frame_id) override;
140 void LoadingStopped(uint32_t frame_id) override;
141 void ProgressChanged(uint32_t frame_id, double progress) override;
142 void SetClientProperty(uint32_t frame_id,
143 const mojo::String& name,
144 mojo::Array<uint8_t> value) override;
145 void OnCreatedFrame(
146 uint32_t parent_id,
147 uint32_t frame_id,
148 mojo::Map<mojo::String, mojo::Array<uint8_t>> client_properties) override;
149 void RequestNavigate(uint32_t frame_id,
150 NavigationTarget target,
151 mojo::URLRequestPtr request) override;
152 void DidNavigateLocally(uint32_t frame_id, const mojo::String& url) override;
154 FrameTree* const tree_;
155 // WARNING: this may be null. See class description for details.
156 mojo::View* view_;
157 const uint32_t id_;
158 Frame* parent_;
159 ViewOwnership view_ownership_;
160 std::vector<Frame*> children_;
161 scoped_ptr<FrameUserData> user_data_;
163 // WARNING: this may be null. See class description for details.
164 FrameTreeClient* frame_tree_client_;
166 bool loading_;
167 double progress_;
169 ClientPropertyMap client_properties_;
171 mojo::Binding<FrameTreeServer> frame_tree_server_binding_;
173 DISALLOW_COPY_AND_ASSIGN(Frame);
176 } // namespace mandoline
178 #endif // MANDOLINE_TAB_FRAME_H_