Chromium Code Reviews| 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 registry_entry = std::make_pair( |
| 94 T::Type, | |
| 95 GuestViewData(base::Bind(&T::Create), base::Bind(&T::CleanUp))); | |
| 96 guest_view_registry_.insert(registry_entry); | |
| 93 } | 97 } |
| 94 | 98 |
| 99 // Registers a callback to be called when the view identified by | |
| 100 // |embedder_process_id| and |view_instance_id| is destroyed. | |
| 101 // Note that multiple callbacks can be registered for one view. | |
| 102 void RegisterViewDestructionCallback(int embedder_process_id, | |
| 103 int view_instance_id, | |
| 104 const base::Closure& callback); | |
| 105 | |
| 95 using WebContentsCreatedCallback = | 106 using WebContentsCreatedCallback = |
| 96 base::Callback<void(content::WebContents*)>; | 107 base::Callback<void(content::WebContents*)>; |
| 97 void CreateGuest(const std::string& view_type, | 108 void CreateGuest(const std::string& view_type, |
| 98 content::WebContents* owner_web_contents, | 109 content::WebContents* owner_web_contents, |
| 99 const base::DictionaryValue& create_params, | 110 const base::DictionaryValue& create_params, |
| 100 const WebContentsCreatedCallback& callback); | 111 const WebContentsCreatedCallback& callback); |
| 101 | 112 |
| 102 content::WebContents* CreateGuestWithWebContentsParams( | 113 content::WebContents* CreateGuestWithWebContentsParams( |
| 103 const std::string& view_type, | 114 const std::string& view_type, |
| 104 content::WebContents* owner_web_contents, | 115 content::WebContents* owner_web_contents, |
| 105 const content::WebContents::CreateParams& create_params); | 116 const content::WebContents::CreateParams& create_params); |
| 106 | 117 |
| 107 content::SiteInstance* GetGuestSiteInstance( | 118 content::SiteInstance* GetGuestSiteInstance( |
| 108 const GURL& guest_site); | 119 const GURL& guest_site); |
| 109 | 120 |
| 110 // BrowserPluginGuestManager implementation. | 121 // BrowserPluginGuestManager implementation. |
| 111 content::WebContents* GetGuestByInstanceID( | 122 content::WebContents* GetGuestByInstanceID( |
| 112 int owner_process_id, | 123 int owner_process_id, |
| 113 int element_instance_id) override; | 124 int element_instance_id) override; |
| 114 bool ForEachGuest(content::WebContents* owner_web_contents, | 125 bool ForEachGuest(content::WebContents* owner_web_contents, |
| 115 const GuestCallback& callback) override; | 126 const GuestCallback& callback) override; |
| 116 content::WebContents* GetFullPageGuest( | 127 content::WebContents* GetFullPageGuest( |
| 117 content::WebContents* embedder_web_contents) override; | 128 content::WebContents* embedder_web_contents) override; |
| 118 | 129 |
| 119 protected: | 130 protected: |
| 120 friend class GuestViewBase; | 131 friend class GuestViewBase; |
| 121 friend class GuestViewEvent; | 132 friend class GuestViewEvent; |
| 122 friend class GuestViewMessageFilter; | 133 friend class GuestViewMessageFilter; |
| 123 | 134 |
| 124 // Can be overriden in tests. | 135 // The virtual methods are virtual so that they can be overriden in tests. |
|
lazyboy
2015/06/09 14:19:32
nit: These methods are virtual so that ...
paulmeyer
2015/06/09 14:49:42
Done.
| |
| 136 | |
| 125 virtual void AddGuest(int guest_instance_id, | 137 virtual void AddGuest(int guest_instance_id, |
| 126 content::WebContents* guest_web_contents); | 138 content::WebContents* guest_web_contents); |
| 127 virtual void RemoveGuest(int guest_instance_id); | 139 virtual void RemoveGuest(int guest_instance_id); |
| 140 | |
| 141 // Called when a GuestView has been created in JavaScript. | |
| 142 virtual void ViewCreated(int embedder_process_id, | |
| 143 int view_instance_id, | |
| 144 const std::string& view_type); | |
| 145 | |
| 146 // Called when a GuestView has been garbage collected in JavaScript. | |
| 128 virtual void ViewGarbageCollected(int embedder_process_id, | 147 virtual void ViewGarbageCollected(int embedder_process_id, |
| 129 int view_instance_id) {} | 148 int view_instance_id); |
| 130 | 149 |
| 131 // Creates a guest of the provided |view_type|. | 150 // Creates a guest of the provided |view_type|. |
| 132 GuestViewBase* CreateGuestInternal(content::WebContents* owner_web_contents, | 151 GuestViewBase* CreateGuestInternal(content::WebContents* owner_web_contents, |
| 133 const std::string& view_type); | 152 const std::string& view_type); |
| 134 | 153 |
| 135 // Adds GuestView types to the GuestView registry. | 154 // Adds GuestView types to the GuestView registry. |
| 136 void RegisterGuestViewTypes(); | 155 void RegisterGuestViewTypes(); |
| 137 | 156 |
| 138 // Indicates whether the provided |guest| can be used in the context it has | 157 // Indicates whether the provided |guest| can be used in the context it has |
| 139 // been created. | 158 // been created. |
| 140 bool IsGuestAvailableToContext(GuestViewBase* guest); | 159 bool IsGuestAvailableToContext(GuestViewBase* guest); |
| 141 | 160 |
| 142 // Dispatches the event with |name| with the provided |args| to the embedder | 161 // Dispatches the event with |name| with the provided |args| to the embedder |
| 143 // of the given |guest| with |instance_id| for routing. | 162 // of the given |guest| with |instance_id| for routing. |
| 144 void DispatchEvent(const std::string& event_name, | 163 void DispatchEvent(const std::string& event_name, |
| 145 scoped_ptr<base::DictionaryValue> args, | 164 scoped_ptr<base::DictionaryValue> args, |
| 146 GuestViewBase* guest, | 165 GuestViewBase* guest, |
| 147 int instance_id); | 166 int instance_id); |
| 148 | 167 |
| 168 // This method is called when the embedder with ID |embedder_process_id| is | |
| 169 // about to be destroyed. | |
| 170 void EmbedderWillBeDestroyed(int embedder_process_id); | |
| 171 | |
| 149 content::WebContents* GetGuestByInstanceID(int guest_instance_id); | 172 content::WebContents* GetGuestByInstanceID(int guest_instance_id); |
| 150 | 173 |
| 151 bool CanEmbedderAccessInstanceIDMaybeKill( | 174 bool CanEmbedderAccessInstanceIDMaybeKill( |
| 152 int embedder_render_process_id, | 175 int embedder_render_process_id, |
| 153 int guest_instance_id); | 176 int guest_instance_id); |
| 154 | 177 |
| 155 bool CanEmbedderAccessInstanceID(int embedder_render_process_id, | 178 bool CanEmbedderAccessInstanceID(int embedder_render_process_id, |
| 156 int guest_instance_id); | 179 int guest_instance_id); |
| 157 | 180 |
| 158 // Returns true if |guest_instance_id| can be used to add a new guest to this | 181 // 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; | 206 bool operator==(const ElementInstanceKey& other) const; |
| 184 }; | 207 }; |
| 185 | 208 |
| 186 using GuestInstanceIDMap = std::map<ElementInstanceKey, int>; | 209 using GuestInstanceIDMap = std::map<ElementInstanceKey, int>; |
| 187 GuestInstanceIDMap instance_id_map_; | 210 GuestInstanceIDMap instance_id_map_; |
| 188 | 211 |
| 189 // The reverse map of GuestInstanceIDMap. | 212 // The reverse map of GuestInstanceIDMap. |
| 190 using GuestInstanceIDReverseMap = std::map<int, ElementInstanceKey>; | 213 using GuestInstanceIDReverseMap = std::map<int, ElementInstanceKey>; |
| 191 GuestInstanceIDReverseMap reverse_instance_id_map_; | 214 GuestInstanceIDReverseMap reverse_instance_id_map_; |
| 192 | 215 |
| 193 using GuestCreationCallback = | 216 using GuestViewCreateFunction = |
| 194 base::Callback<GuestViewBase*(content::WebContents*)>; | 217 base::Callback<GuestViewBase*(content::WebContents*)>; |
| 195 using GuestViewCreationMap = | 218 using GuestViewCleanUpFunction = base::Callback<void(int, int)>; |
| 196 std::map<std::string, GuestViewManager::GuestCreationCallback>; | 219 struct GuestViewData { |
| 197 GuestViewCreationMap guest_view_registry_; | 220 GuestViewData(const GuestViewCreateFunction& create_function, |
| 221 const GuestViewCleanUpFunction& cleanup_function); | |
| 222 ~GuestViewData(); | |
| 223 const GuestViewCreateFunction create_function; | |
| 224 const GuestViewCleanUpFunction cleanup_function; | |
| 225 }; | |
| 226 using GuestViewMethodMap = std::map<std::string, GuestViewData>; | |
| 227 GuestViewMethodMap guest_view_registry_; | |
| 198 | 228 |
| 199 int current_instance_id_; | 229 int current_instance_id_; |
| 200 | 230 |
| 201 // Any instance ID whose number not greater than this was removed via | 231 // Any instance ID whose number not greater than this was removed via |
| 202 // RemoveGuest. | 232 // RemoveGuest. |
| 203 // This is used so that we don't have store all removed instance IDs in | 233 // This is used so that we don't have store all removed instance IDs in |
| 204 // |removed_instance_ids_|. | 234 // |removed_instance_ids_|. |
| 205 int last_instance_id_removed_; | 235 int last_instance_id_removed_; |
| 206 // The remaining instance IDs that are greater than | 236 // The remaining instance IDs that are greater than |
| 207 // |last_instance_id_removed_| are kept here. | 237 // |last_instance_id_removed_| are kept here. |
| 208 std::set<int> removed_instance_ids_; | 238 std::set<int> removed_instance_ids_; |
| 209 | 239 |
| 210 content::BrowserContext* context_; | 240 content::BrowserContext* context_; |
| 211 | 241 |
| 212 scoped_ptr<GuestViewManagerDelegate> delegate_; | 242 scoped_ptr<GuestViewManagerDelegate> delegate_; |
| 213 | 243 |
| 244 // |view_destruction_callback_map_| maps from embedder process ID to view ID | |
| 245 // to a vector of callback functions to be called when that view is destroyed. | |
| 246 typedef std::vector<base::Closure> Callbacks; | |
|
Fady Samuel
2015/06/08 20:25:56
nit: use using instead of typedef.
paulmeyer
2015/06/09 14:49:42
Done.
| |
| 247 typedef std::map<int, Callbacks> CallbacksForEachViewID; | |
| 248 typedef std::map<int, CallbacksForEachViewID> CallbacksForEachEmbedderID; | |
| 249 CallbacksForEachEmbedderID view_destruction_callback_map_; | |
| 250 | |
| 214 DISALLOW_COPY_AND_ASSIGN(GuestViewManager); | 251 DISALLOW_COPY_AND_ASSIGN(GuestViewManager); |
| 215 }; | 252 }; |
| 216 | 253 |
| 217 } // namespace guest_view | 254 } // namespace guest_view |
| 218 | 255 |
| 219 #endif // COMPONETS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_ | 256 #endif // COMPONETS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_ |
| OLD | NEW |