Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mandoline / tab / frame.h
blobea6f2b707e505e3d8aa55cb1e13563bbc8b9eedd
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 <vector>
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "components/view_manager/public/cpp/view_observer.h"
13 #include "mandoline/tab/public/interfaces/frame_tree.mojom.h"
14 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
16 namespace mandoline {
18 class FrameTree;
19 class FrameTreeClient;
20 class FrameUserData;
22 enum class ViewOwnership {
23 OWNS_VIEW,
24 DOESNT_OWN_VIEW,
27 // Frame represents an embedding in a frame. Frames own their children.
28 // Frames automatically delete themself if the View the frame is associated
29 // with is deleted.
31 // In general each Frame has a View. When a new Frame is created by a client
32 // there may be a small amount of time where the View is not yet known to us
33 // (separate pipes are used for the view and frame, resulting in undefined
34 // message ordering). In this case the view is null and will be set once we
35 // see the view (OnTreeChanged()).
37 // When the FrameTreeClient creates a new Frame there is no associated
38 // FrameTreeClient for the child Frame.
39 class Frame : public mojo::ViewObserver, public FrameTreeServer {
40 public:
41 Frame(FrameTree* tree,
42 mojo::View* view,
43 uint32_t id,
44 ViewOwnership view_ownership,
45 FrameTreeClient* frame_tree_client,
46 scoped_ptr<FrameUserData> user_data);
47 ~Frame() override;
49 void Init(Frame* parent);
51 // Walks the View tree starting at |view| going up returning the first
52 // Frame that is associated with |view|. For example, if |view|
53 // has a Frame associated with it, then that is returned. Otherwise
54 // this checks view->parent() and so on.
55 static Frame* FindFirstFrameAncestor(mojo::View* view);
57 FrameTree* tree() { return tree_; }
59 Frame* parent() { return parent_; }
60 const Frame* parent() const { return parent_; }
62 mojo::View* view() { return view_; }
63 const mojo::View* view() const { return view_; }
65 uint32_t id() const { return id_; }
67 // Finds the descendant with the specified id.
68 Frame* FindFrame(uint32_t id) {
69 return const_cast<Frame*>(const_cast<const Frame*>(this)->FindFrame(id));
71 const Frame* FindFrame(uint32_t id) const;
73 bool HasAncestor(const Frame* frame) const;
75 FrameUserData* user_data() { return user_data_.get(); }
77 const std::vector<Frame*>& children() { return children_; }
79 const mojo::String& name() const { return name_; }
81 // Returns true if this Frame or any child Frame is loading.
82 bool IsLoading() const;
84 // Returns the sum total of loading progress from this Frame and all of its
85 // children, as well as the number of Frames accumulated.
86 double GatherProgress(int* frame_count) const;
88 private:
89 friend class FrameTree;
91 void SetView(mojo::View* view);
93 // Adds this to |frames| and recurses through the children calling the
94 // same function.
95 void BuildFrameTree(std::vector<const Frame*>* frames) const;
97 void Add(Frame* node);
98 void Remove(Frame* node);
100 // The implementation of the various FrameTreeServer functions that take
101 // frame_id call into these.
102 void LoadingStartedImpl();
103 void LoadingStoppedImpl();
104 void ProgressChangedImpl(double progress);
105 void SetFrameNameImpl(const mojo::String& name);
107 // Returns the Frame whose id is |frame_id|. Returns nullptr if |frame_id| is
108 // not from the same connection as this.
109 Frame* FindTargetFrame(uint32_t frame_id);
111 // Notifies the client and all descendants as appropriate.
112 void NotifyAdded(const Frame* source, const Frame* added_node);
113 void NotifyRemoved(const Frame* source, const Frame* removed_node);
114 void NotifyFrameNameChanged(const Frame* source);
116 // mojo::ViewObserver:
117 void OnTreeChanged(const TreeChangeParams& params) override;
118 void OnViewDestroying(mojo::View* view) override;
120 // FrameTreeServer:
121 void PostMessageEventToFrame(uint32_t frame_id,
122 MessageEventPtr event) override;
123 void LoadingStarted(uint32_t frame_id) override;
124 void LoadingStopped(uint32_t frame_id) override;
125 void ProgressChanged(uint32_t frame_id, double progress) override;
126 void SetFrameName(uint32_t frame_id, const mojo::String& name) override;
127 void OnCreatedFrame(uint32_t parent_id, uint32_t frame_id) override;
128 void RequestNavigate(uint32_t frame_id,
129 NavigationTarget target,
130 mojo::URLRequestPtr request) override;
131 void DidNavigateLocally(uint32_t frame_id, const mojo::String& url) override;
133 FrameTree* const tree_;
134 // WARNING: this may be null. See class description for details.
135 mojo::View* view_;
136 const uint32_t id_;
137 Frame* parent_;
138 ViewOwnership view_ownership_;
139 std::vector<Frame*> children_;
140 scoped_ptr<FrameUserData> user_data_;
142 // WARNING: this may be null. See class description for details.
143 FrameTreeClient* frame_tree_client_;
145 bool loading_;
146 double progress_;
148 mojo::String name_;
150 mojo::Binding<FrameTreeServer> frame_tree_server_binding_;
152 DISALLOW_COPY_AND_ASSIGN(Frame);
155 } // namespace mandoline
157 #endif // MANDOLINE_TAB_FRAME_H_