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 COMPONENTS_HTML_VIEWER_HTML_FRAME_TREE_MANAGER_H_
6 #define COMPONENTS_HTML_VIEWER_HTML_FRAME_TREE_MANAGER_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "components/web_view/public/interfaces/frame.mojom.h"
25 namespace html_viewer
{
27 class DocumentResourceWaiter
;
30 class HTMLFrameDelegate
;
31 class HTMLFrameTreeManagerObserver
;
33 // HTMLFrameTreeManager is responsible for managing the frames that comprise a
34 // document. Some of the frames may be remote. HTMLFrameTreeManager updates its
35 // state in response to changes from the server Frame, as well as changes
36 // from the underlying frames. The frame tree has at least one local frame
37 // that is backed by a mus::View.
38 class HTMLFrameTreeManager
{
40 // Returns a new HTMLFrame or null if a HTMLFrame does not need to be created.
41 // If this returns non-null the caller owns the return value and must call
43 static HTMLFrame
* CreateFrameAndAttachToTree(
44 GlobalState
* global_state
,
46 scoped_ptr
<DocumentResourceWaiter
> resource_waiter
,
47 HTMLFrameDelegate
* delegate
);
49 // Returns the HTMLFrameTreeManager with the specified root id.
50 static HTMLFrameTreeManager
* FindFrameTreeWithRoot(uint32_t root_frame_id
);
52 GlobalState
* global_state() { return global_state_
; }
54 blink::WebView
* GetWebView();
56 // Ever increasing value that is used to identify the state the tree is in.
57 // That is, the value increases every time a structural change is made to
58 // the tree, eg add or remove.
59 uint32_t change_id() const { return change_id_
; }
61 void AddObserver(HTMLFrameTreeManagerObserver
* observer
);
62 void RemoveObserver(HTMLFrameTreeManagerObserver
* observer
);
65 friend class HTMLFrame
;
66 class ChangeIdAdvancedNotifier
;
67 using TreeMap
= std::map
<uint32_t, HTMLFrameTreeManager
*>;
68 using ChangeIDSet
= std::set
<uint32_t>;
70 explicit HTMLFrameTreeManager(GlobalState
* global_state
);
71 ~HTMLFrameTreeManager();
73 void Init(HTMLFrameDelegate
* delegate
,
74 mus::View
* local_view
,
75 const mojo::Array
<web_view::mojom::FrameDataPtr
>& frame_data
,
78 // Creates a Frame per FrameData element in |frame_data|. Returns the root.
79 HTMLFrame
* BuildFrameTree(
80 HTMLFrameDelegate
* delegate
,
81 const mojo::Array
<web_view::mojom::FrameDataPtr
>& frame_data
,
82 uint32_t local_frame_id
,
83 mus::View
* local_view
);
85 // Returns this HTMLFrameTreeManager from |instances_|.
86 void RemoveFromInstances();
88 // Invoked when a Frame is destroyed.
89 void OnFrameDestroyed(HTMLFrame
* frame
);
91 // Call before applying a structure change from the server. |source| is the
92 // the source of the change, and |change_id| the id from the server. Returns
93 // true if the change should be applied, false otherwise.
94 bool PrepareForStructureChange(HTMLFrame
* source
, uint32_t change_id
);
96 // Each HTMLFrame delegates FrameClient methods to the HTMLFrameTreeManager
97 // the frame is in. HTMLFrameTreeManager only responds to changes from the
98 // |local_root_| (this is because each FrameClient sees the same change, and
99 // a change only need be processed once).
100 void ProcessOnFrameAdded(HTMLFrame
* source
,
102 web_view::mojom::FrameDataPtr frame_data
);
103 void ProcessOnFrameRemoved(HTMLFrame
* source
,
106 void ProcessOnFrameClientPropertyChanged(HTMLFrame
* source
,
108 const mojo::String
& name
,
109 mojo::Array
<uint8_t> new_data
);
111 static TreeMap
* instances_
;
113 GlobalState
* global_state_
;
117 // The |local_root_| is the HTMLFrame that is the highest frame that is
119 HTMLFrame
* local_root_
;
123 // When a frame is locally removed the id is added here. When the server
124 // notifies us we remove from this set. This is done to ensure an add/remove
125 // followed by notification from the server doesn't attempt to add the frame
126 // back that was locally removed.
127 ChangeIDSet pending_remove_ids_
;
129 // If true, we're in ProcessOnFrameRemoved().
130 bool in_process_on_frame_removed_
;
132 base::ObserverList
<HTMLFrameTreeManagerObserver
> observers_
;
134 base::WeakPtrFactory
<HTMLFrameTreeManager
> weak_factory_
;
136 DISALLOW_COPY_AND_ASSIGN(HTMLFrameTreeManager
);
139 } // namespace html_viewer
141 #endif // COMPONENTS_HTML_VIEWER_HTML_FRAME_TREE_MANAGER_H_