OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_ | 5 #ifndef COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_ |
6 #define COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_ | 6 #define COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
12 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "content/public/browser/browser_plugin_guest_manager.h" | 15 #include "content/public/browser/browser_plugin_guest_manager.h" |
15 #include "content/public/browser/site_instance.h" | 16 #include "content/public/browser/site_instance.h" |
16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
17 | 18 |
18 class GURL; | 19 class GURL; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 template <typename T> | 83 template <typename T> |
83 void RegisterGuestViewType() { | 84 void RegisterGuestViewType() { |
84 // If the GuestView type |T| is already registered, then there is nothing | 85 // If the GuestView type |T| is already registered, then there is nothing |
85 // more to do. If an existing entry in the registry was created by this | 86 // more to do. If an existing entry in the registry was created by this |
86 // function for type |T|, then registering again would have no effect, and | 87 // function for type |T|, then registering again would have no effect, and |
87 // if it was registered elsewhere, then we do not want to overwrite it. Note | 88 // if it was registered elsewhere, then we do not want to overwrite it. Note |
88 // that it is possible for tests to have special test factory methods | 89 // that it is possible for tests to have special test factory methods |
89 // registered here. | 90 // registered here. |
90 if (guest_view_registry_.count(T::Type)) | 91 if (guest_view_registry_.count(T::Type)) |
91 return; | 92 return; |
92 guest_view_registry_[T::Type] = base::Bind(&T::Create); | 93 auto new_entry = std::make_pair(T::Type, GuestViewData( |
lazyboy
2015/06/05 23:09:55
nit: Can we break lines a bit differently?
std::m
paulmeyer
2015/06/08 17:53:58
Done.
| |
94 base::Bind(&T::Create), base::Bind(&T::CleanUp))); | |
95 guest_view_registry_.insert(new_entry); | |
93 } | 96 } |
94 | 97 |
98 // Registers a callback to be called when the view identified by | |
99 // |embedder_process_id| and |view_instance_id| is destroyed. | |
100 void RegisterViewCallback(int embedder_process_id, | |
lazyboy
2015/06/05 23:09:55
RegisterViewDestructionCallback
paulmeyer
2015/06/08 17:53:58
Done.
| |
101 int view_instance_id, | |
102 const base::Closure& callback); | |
103 | |
95 using WebContentsCreatedCallback = | 104 using WebContentsCreatedCallback = |
96 base::Callback<void(content::WebContents*)>; | 105 base::Callback<void(content::WebContents*)>; |
97 void CreateGuest(const std::string& view_type, | 106 void CreateGuest(const std::string& view_type, |
98 content::WebContents* owner_web_contents, | 107 content::WebContents* owner_web_contents, |
99 const base::DictionaryValue& create_params, | 108 const base::DictionaryValue& create_params, |
100 const WebContentsCreatedCallback& callback); | 109 const WebContentsCreatedCallback& callback); |
101 | 110 |
102 content::WebContents* CreateGuestWithWebContentsParams( | 111 content::WebContents* CreateGuestWithWebContentsParams( |
103 const std::string& view_type, | 112 const std::string& view_type, |
104 content::WebContents* owner_web_contents, | 113 content::WebContents* owner_web_contents, |
105 const content::WebContents::CreateParams& create_params); | 114 const content::WebContents::CreateParams& create_params); |
106 | 115 |
107 content::SiteInstance* GetGuestSiteInstance( | 116 content::SiteInstance* GetGuestSiteInstance( |
108 const GURL& guest_site); | 117 const GURL& guest_site); |
109 | 118 |
110 // BrowserPluginGuestManager implementation. | 119 // BrowserPluginGuestManager implementation. |
111 content::WebContents* GetGuestByInstanceID( | 120 content::WebContents* GetGuestByInstanceID( |
112 int owner_process_id, | 121 int owner_process_id, |
113 int element_instance_id) override; | 122 int element_instance_id) override; |
114 bool ForEachGuest(content::WebContents* owner_web_contents, | 123 bool ForEachGuest(content::WebContents* owner_web_contents, |
115 const GuestCallback& callback) override; | 124 const GuestCallback& callback) override; |
116 content::WebContents* GetFullPageGuest( | 125 content::WebContents* GetFullPageGuest( |
117 content::WebContents* embedder_web_contents) override; | 126 content::WebContents* embedder_web_contents) override; |
118 | 127 |
119 protected: | 128 protected: |
120 friend class GuestViewBase; | 129 friend class GuestViewBase; |
121 friend class GuestViewEvent; | 130 friend class GuestViewEvent; |
122 friend class GuestViewMessageFilter; | 131 friend class GuestViewMessageFilter; |
123 | 132 |
124 // Can be overriden in tests. | 133 // The virtual methods are virtual so that they can be overriden in tests. |
134 | |
125 virtual void AddGuest(int guest_instance_id, | 135 virtual void AddGuest(int guest_instance_id, |
126 content::WebContents* guest_web_contents); | 136 content::WebContents* guest_web_contents); |
127 virtual void RemoveGuest(int guest_instance_id); | 137 virtual void RemoveGuest(int guest_instance_id); |
138 | |
139 // Called when a GuestView has been created in JavaScript. | |
140 virtual void ViewCreated(int embedder_process_id, | |
141 int view_instance_id, | |
142 const std::string& view_type); | |
143 | |
144 // Called when a GuestView has been garbage collected in JavaScript. | |
128 virtual void ViewGarbageCollected(int embedder_process_id, | 145 virtual void ViewGarbageCollected(int embedder_process_id, |
129 int view_instance_id) {} | 146 int view_instance_id); |
130 | 147 |
131 // Creates a guest of the provided |view_type|. | 148 // Creates a guest of the provided |view_type|. |
132 GuestViewBase* CreateGuestInternal(content::WebContents* owner_web_contents, | 149 GuestViewBase* CreateGuestInternal(content::WebContents* owner_web_contents, |
133 const std::string& view_type); | 150 const std::string& view_type); |
134 | 151 |
135 // Adds GuestView types to the GuestView registry. | 152 // Adds GuestView types to the GuestView registry. |
136 void RegisterGuestViewTypes(); | 153 void RegisterGuestViewTypes(); |
137 | 154 |
138 // Indicates whether the provided |guest| can be used in the context it has | 155 // Indicates whether the provided |guest| can be used in the context it has |
139 // been created. | 156 // been created. |
140 bool IsGuestAvailableToContext(GuestViewBase* guest); | 157 bool IsGuestAvailableToContext(GuestViewBase* guest); |
141 | 158 |
142 // Dispatches the event with |name| with the provided |args| to the embedder | 159 // Dispatches the event with |name| with the provided |args| to the embedder |
143 // of the given |guest| with |instance_id| for routing. | 160 // of the given |guest| with |instance_id| for routing. |
144 void DispatchEvent(const std::string& event_name, | 161 void DispatchEvent(const std::string& event_name, |
145 scoped_ptr<base::DictionaryValue> args, | 162 scoped_ptr<base::DictionaryValue> args, |
146 GuestViewBase* guest, | 163 GuestViewBase* guest, |
147 int instance_id); | 164 int instance_id); |
148 | 165 |
166 // This method is called when the embedder with ID |embedder_process_id| is | |
167 // about to be destroyed. | |
168 void EmbedderWillBeDestroyed(int embedder_process_id); | |
169 | |
149 content::WebContents* GetGuestByInstanceID(int guest_instance_id); | 170 content::WebContents* GetGuestByInstanceID(int guest_instance_id); |
150 | 171 |
151 bool CanEmbedderAccessInstanceIDMaybeKill( | 172 bool CanEmbedderAccessInstanceIDMaybeKill( |
152 int embedder_render_process_id, | 173 int embedder_render_process_id, |
153 int guest_instance_id); | 174 int guest_instance_id); |
154 | 175 |
155 bool CanEmbedderAccessInstanceID(int embedder_render_process_id, | 176 bool CanEmbedderAccessInstanceID(int embedder_render_process_id, |
156 int guest_instance_id); | 177 int guest_instance_id); |
157 | 178 |
158 // Returns true if |guest_instance_id| can be used to add a new guest to this | 179 // Returns true if |guest_instance_id| can be used to add a new guest to this |
(...skipping 24 matching lines...) Expand all Loading... | |
183 bool operator==(const ElementInstanceKey& other) const; | 204 bool operator==(const ElementInstanceKey& other) const; |
184 }; | 205 }; |
185 | 206 |
186 using GuestInstanceIDMap = std::map<ElementInstanceKey, int>; | 207 using GuestInstanceIDMap = std::map<ElementInstanceKey, int>; |
187 GuestInstanceIDMap instance_id_map_; | 208 GuestInstanceIDMap instance_id_map_; |
188 | 209 |
189 // The reverse map of GuestInstanceIDMap. | 210 // The reverse map of GuestInstanceIDMap. |
190 using GuestInstanceIDReverseMap = std::map<int, ElementInstanceKey>; | 211 using GuestInstanceIDReverseMap = std::map<int, ElementInstanceKey>; |
191 GuestInstanceIDReverseMap reverse_instance_id_map_; | 212 GuestInstanceIDReverseMap reverse_instance_id_map_; |
192 | 213 |
193 using GuestCreationCallback = | 214 using GuestViewCreateFunction = |
194 base::Callback<GuestViewBase*(content::WebContents*)>; | 215 base::Callback<GuestViewBase*(content::WebContents*)>; |
195 using GuestViewCreationMap = | 216 using GuestViewCleanUpFunction = base::Callback<void(int, int)>; |
196 std::map<std::string, GuestViewManager::GuestCreationCallback>; | 217 struct GuestViewData { |
197 GuestViewCreationMap guest_view_registry_; | 218 GuestViewData(const GuestViewCreateFunction& create, |
219 const GuestViewCleanUpFunction& clean_up); | |
220 ~GuestViewData(); | |
221 const GuestViewCreateFunction create; | |
lazyboy
2015/06/05 23:09:55
s/create/create_function
s/clean_up/cleanup_functi
paulmeyer
2015/06/08 17:53:59
Done.
| |
222 const GuestViewCleanUpFunction clean_up; | |
223 }; | |
224 using GuestViewMethodMap = std::map<std::string, GuestViewData>; | |
225 GuestViewMethodMap guest_view_registry_; | |
198 | 226 |
199 int current_instance_id_; | 227 int current_instance_id_; |
200 | 228 |
201 // Any instance ID whose number not greater than this was removed via | 229 // Any instance ID whose number not greater than this was removed via |
202 // RemoveGuest. | 230 // RemoveGuest. |
203 // This is used so that we don't have store all removed instance IDs in | 231 // This is used so that we don't have store all removed instance IDs in |
204 // |removed_instance_ids_|. | 232 // |removed_instance_ids_|. |
205 int last_instance_id_removed_; | 233 int last_instance_id_removed_; |
206 // The remaining instance IDs that are greater than | 234 // The remaining instance IDs that are greater than |
207 // |last_instance_id_removed_| are kept here. | 235 // |last_instance_id_removed_| are kept here. |
208 std::set<int> removed_instance_ids_; | 236 std::set<int> removed_instance_ids_; |
209 | 237 |
210 content::BrowserContext* context_; | 238 content::BrowserContext* context_; |
211 | 239 |
212 scoped_ptr<GuestViewManagerDelegate> delegate_; | 240 scoped_ptr<GuestViewManagerDelegate> delegate_; |
213 | 241 |
242 // |view_callback_map_| maps from embedder process ID to view ID to a | |
243 // vector of callback functions to be called when that view is destroyed. | |
244 std::map<int, std::map<int, std::vector<base::Closure>>> view_callback_map_; | |
lazyboy
2015/06/05 23:09:55
1)
It's not obvious from the usage of this map why
paulmeyer
2015/06/08 17:53:58
I addressed the first two. For the third, as per o
| |
245 | |
214 DISALLOW_COPY_AND_ASSIGN(GuestViewManager); | 246 DISALLOW_COPY_AND_ASSIGN(GuestViewManager); |
215 }; | 247 }; |
216 | 248 |
217 } // namespace guest_view | 249 } // namespace guest_view |
218 | 250 |
219 #endif // COMPONETS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_ | 251 #endif // COMPONETS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_ |
OLD | NEW |