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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_
6 #define COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_
11 #include "base/bind.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/lazy_instance.h"
14 #include "base/macros.h"
15 #include "content/public/browser/browser_plugin_guest_manager.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h"
24 } // namespace content
26 namespace guest_view
{
29 class GuestViewManagerDelegate
;
30 class GuestViewManagerFactory
;
32 class GuestViewManager
: public content::BrowserPluginGuestManager
,
33 public base::SupportsUserData::Data
{
35 GuestViewManager(content::BrowserContext
* context
,
36 scoped_ptr
<GuestViewManagerDelegate
> delegate
);
37 ~GuestViewManager() override
;
39 // Returns the GuestViewManager associated with |context|. If one isn't
40 // available, then it is created and returned.
41 static GuestViewManager
* CreateWithDelegate(
42 content::BrowserContext
* context
,
43 scoped_ptr
<GuestViewManagerDelegate
> delegate
);
45 // Returns the GuestViewManager associated with |context|. If one isn't
46 // available, then nullptr is returned.
47 static GuestViewManager
* FromBrowserContext(content::BrowserContext
* context
);
49 // Overrides factory for testing. Default (NULL) value indicates regular
50 // (non-test) environment.
51 static void set_factory_for_testing(GuestViewManagerFactory
* factory
) {
52 GuestViewManager::factory_
= factory
;
54 // Returns the guest WebContents associated with the given |guest_instance_id|
55 // if the provided |embedder_render_process_id| is allowed to access it.
56 // If the embedder is not allowed access, the embedder will be killed, and
57 // this method will return NULL. If no WebContents exists with the given
58 // instance ID, then NULL will also be returned.
59 content::WebContents
* GetGuestByInstanceIDSafely(
60 int guest_instance_id
,
61 int embedder_render_process_id
);
63 // Associates the Browser Plugin with |element_instance_id| to a
64 // guest that has ID of |guest_instance_id| and sets initialization
65 // parameters, |params| for it.
66 void AttachGuest(int embedder_process_id
,
67 int element_instance_id
,
68 int guest_instance_id
,
69 const base::DictionaryValue
& attach_params
);
71 // Removes the association between |element_instance_id| and a guest instance
73 void DetachGuest(GuestViewBase
* guest
);
75 // Indicates whether the |guest| is owned by an extension or Chrome App.
76 bool IsOwnedByExtension(GuestViewBase
* guest
);
78 int GetNextInstanceID();
79 int GetGuestInstanceIDForElementID(
81 int element_instance_id
);
84 void RegisterGuestViewType() {
85 // If the GuestView type |T| is already registered, then there is nothing
86 // more to do. If an existing entry in the registry was created by this
87 // function for type |T|, then registering again would have no effect, and
88 // if it was registered elsewhere, then we do not want to overwrite it. Note
89 // that it is possible for tests to have special test factory methods
91 if (guest_view_registry_
.count(T::Type
))
93 auto registry_entry
= std::make_pair(
95 GuestViewData(base::Bind(&T::Create
), base::Bind(&T::CleanUp
)));
96 guest_view_registry_
.insert(registry_entry
);
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
);
106 using WebContentsCreatedCallback
=
107 base::Callback
<void(content::WebContents
*)>;
108 void CreateGuest(const std::string
& view_type
,
109 content::WebContents
* owner_web_contents
,
110 const base::DictionaryValue
& create_params
,
111 const WebContentsCreatedCallback
& callback
);
113 content::WebContents
* CreateGuestWithWebContentsParams(
114 const std::string
& view_type
,
115 content::WebContents
* owner_web_contents
,
116 const content::WebContents::CreateParams
& create_params
);
118 content::SiteInstance
* GetGuestSiteInstance(
119 const GURL
& guest_site
);
121 // BrowserPluginGuestManager implementation.
122 content::WebContents
* GetGuestByInstanceID(
123 int owner_process_id
,
124 int element_instance_id
) override
;
125 bool ForEachGuest(content::WebContents
* owner_web_contents
,
126 const GuestCallback
& callback
) override
;
127 content::WebContents
* GetFullPageGuest(
128 content::WebContents
* embedder_web_contents
) override
;
131 friend class GuestViewBase
;
132 friend class GuestViewEvent
;
133 friend class GuestViewMessageFilter
;
135 // These methods are virtual so that they can be overriden in tests.
137 virtual void AddGuest(int guest_instance_id
,
138 content::WebContents
* guest_web_contents
);
139 virtual void RemoveGuest(int guest_instance_id
);
141 // This method is called when the embedder process with ID
142 // |embedder_process_id| is about to be destroyed.
143 virtual void EmbedderWillBeDestroyed(int embedder_process_id
);
145 // Called when a GuestView has been created in JavaScript.
146 virtual void ViewCreated(int embedder_process_id
,
147 int view_instance_id
,
148 const std::string
& view_type
);
150 // Called when a GuestView has been garbage collected in JavaScript.
151 virtual void ViewGarbageCollected(int embedder_process_id
,
152 int view_instance_id
);
154 // Creates a guest of the provided |view_type|.
155 GuestViewBase
* CreateGuestInternal(content::WebContents
* owner_web_contents
,
156 const std::string
& view_type
);
158 // Adds GuestView types to the GuestView registry.
159 void RegisterGuestViewTypes();
161 // Indicates whether the provided |guest| can be used in the context it has
163 bool IsGuestAvailableToContext(GuestViewBase
* guest
);
165 // Dispatches the event with |name| with the provided |args| to the embedder
166 // of the given |guest| with |instance_id| for routing.
167 void DispatchEvent(const std::string
& event_name
,
168 scoped_ptr
<base::DictionaryValue
> args
,
169 GuestViewBase
* guest
,
172 content::WebContents
* GetGuestByInstanceID(int guest_instance_id
);
174 bool CanEmbedderAccessInstanceIDMaybeKill(
175 int embedder_render_process_id
,
176 int guest_instance_id
);
178 bool CanEmbedderAccessInstanceID(int embedder_render_process_id
,
179 int guest_instance_id
);
181 // Returns true if |guest_instance_id| can be used to add a new guest to this
183 // We disallow adding new guest with instance IDs that were previously removed
184 // from this manager using RemoveGuest.
185 bool CanUseGuestInstanceID(int guest_instance_id
);
187 static bool GetFullPageGuestHelper(content::WebContents
** result
,
188 content::WebContents
* guest_web_contents
);
190 // Static factory instance (always NULL for non-test).
191 static GuestViewManagerFactory
* factory_
;
193 // Contains guests' WebContents, mapping from their instance ids.
194 using GuestInstanceMap
= std::map
<int, content::WebContents
*>;
195 GuestInstanceMap guest_web_contents_by_instance_id_
;
197 struct ElementInstanceKey
{
198 int embedder_process_id
;
199 int element_instance_id
;
201 ElementInstanceKey();
202 ElementInstanceKey(int embedder_process_id
,
203 int element_instance_id
);
205 bool operator<(const ElementInstanceKey
& other
) const;
206 bool operator==(const ElementInstanceKey
& other
) const;
209 using GuestInstanceIDMap
= std::map
<ElementInstanceKey
, int>;
210 GuestInstanceIDMap instance_id_map_
;
212 // The reverse map of GuestInstanceIDMap.
213 using GuestInstanceIDReverseMap
= std::map
<int, ElementInstanceKey
>;
214 GuestInstanceIDReverseMap reverse_instance_id_map_
;
216 using GuestViewCreateFunction
=
217 base::Callback
<GuestViewBase
*(content::WebContents
*)>;
218 using GuestViewCleanUpFunction
= base::Callback
<void(int, int)>;
219 struct GuestViewData
{
220 GuestViewData(const GuestViewCreateFunction
& create_function
,
221 const GuestViewCleanUpFunction
& cleanup_function
);
223 const GuestViewCreateFunction create_function
;
224 const GuestViewCleanUpFunction cleanup_function
;
226 using GuestViewMethodMap
= std::map
<std::string
, GuestViewData
>;
227 GuestViewMethodMap guest_view_registry_
;
229 int current_instance_id_
;
231 // Any instance ID whose number not greater than this was removed via
233 // This is used so that we don't have store all removed instance IDs in
234 // |removed_instance_ids_|.
235 int last_instance_id_removed_
;
236 // The remaining instance IDs that are greater than
237 // |last_instance_id_removed_| are kept here.
238 std::set
<int> removed_instance_ids_
;
240 content::BrowserContext
* context_
;
242 scoped_ptr
<GuestViewManagerDelegate
> delegate_
;
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 using Callbacks
= std::vector
<base::Closure
> ;
247 using CallbacksForEachViewID
= std::map
<int, Callbacks
> ;
248 using CallbacksForEachEmbedderID
= std::map
<int, CallbacksForEachViewID
> ;
249 CallbacksForEachEmbedderID view_destruction_callback_map_
;
251 DISALLOW_COPY_AND_ASSIGN(GuestViewManager
);
254 } // namespace guest_view
256 #endif // COMPONETS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_