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 EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
6 #define EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/lazy_instance.h"
12 #include "base/macros.h"
13 #include "content/public/browser/browser_plugin_guest_manager.h"
14 #include "content/public/browser/site_instance.h"
15 #include "content/public/browser/web_contents.h"
22 } // namespace content
26 class GuestViewManagerFactory
;
28 class GuestViewManager
: public content::BrowserPluginGuestManager
,
29 public base::SupportsUserData::Data
{
31 explicit GuestViewManager(content::BrowserContext
* context
);
32 ~GuestViewManager() override
;
34 static GuestViewManager
* FromBrowserContext(content::BrowserContext
* context
);
36 // Overrides factory for testing. Default (NULL) value indicates regular
37 // (non-test) environment.
38 static void set_factory_for_testing(GuestViewManagerFactory
* factory
) {
39 GuestViewManager::factory_
= factory
;
41 // Returns the guest WebContents associated with the given |guest_instance_id|
42 // if the provided |embedder_render_process_id| is allowed to access it.
43 // If the embedder is not allowed access, the embedder will be killed, and
44 // this method will return NULL. If no WebContents exists with the given
45 // instance ID, then NULL will also be returned.
46 content::WebContents
* GetGuestByInstanceIDSafely(
47 int guest_instance_id
,
48 int embedder_render_process_id
);
50 // Associates the Browser Plugin with |element_instance_id| to a
51 // guest that has ID of |guest_instance_id| and sets initialization
52 // parameters, |params| for it.
53 void AttachGuest(int embedder_render_process_id
,
54 int embedder_routing_id
,
55 int element_instance_id
,
56 int guest_instance_id
,
57 const base::DictionaryValue
& attach_params
);
59 // Removes the association between |element_instance_id| and a guest instance
61 void DetachGuest(GuestViewBase
* guest
, int element_instance_id
);
63 int GetNextInstanceID();
64 int GetGuestInstanceIDForElementID(
65 content::WebContents
* owner_web_contents
,
66 int element_instance_id
);
68 using WebContentsCreatedCallback
=
69 base::Callback
<void(content::WebContents
*)>;
70 void CreateGuest(const std::string
& view_type
,
71 content::WebContents
* owner_web_contents
,
72 const base::DictionaryValue
& create_params
,
73 const WebContentsCreatedCallback
& callback
);
75 content::WebContents
* CreateGuestWithWebContentsParams(
76 const std::string
& view_type
,
77 content::WebContents
* owner_web_contents
,
78 const content::WebContents::CreateParams
& create_params
);
80 content::SiteInstance
* GetGuestSiteInstance(
81 const GURL
& guest_site
);
83 // BrowserPluginGuestManager implementation.
84 content::WebContents
* GetGuestByInstanceID(
85 content::WebContents
* owner_web_contents
,
86 int element_instance_id
) override
;
87 bool ForEachGuest(content::WebContents
* owner_web_contents
,
88 const GuestCallback
& callback
) override
;
91 friend class GuestViewBase
;
92 FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest
, AddRemove
);
94 // Can be overriden in tests.
95 virtual void AddGuest(int guest_instance_id
,
96 content::WebContents
* guest_web_contents
);
98 // Can be overriden in tests.
99 virtual void RemoveGuest(int guest_instance_id
);
101 content::WebContents
* GetGuestByInstanceID(int guest_instance_id
);
103 bool CanEmbedderAccessInstanceIDMaybeKill(
104 int embedder_render_process_id
,
105 int guest_instance_id
);
107 bool CanEmbedderAccessInstanceID(int embedder_render_process_id
,
108 int guest_instance_id
);
110 // Returns true if |guest_instance_id| can be used to add a new guest to this
112 // We disallow adding new guest with instance IDs that were previously removed
113 // from this manager using RemoveGuest.
114 bool CanUseGuestInstanceID(int guest_instance_id
);
116 // Static factory instance (always NULL for non-test).
117 static GuestViewManagerFactory
* factory_
;
119 // Contains guests' WebContents, mapping from their instance ids.
120 using GuestInstanceMap
= std::map
<int, content::WebContents
*>;
121 GuestInstanceMap guest_web_contents_by_instance_id_
;
123 struct ElementInstanceKey
{
124 content::WebContents
* owner_web_contents
;
125 int element_instance_id
;
128 : owner_web_contents(nullptr),
129 element_instance_id(0) {}
131 ElementInstanceKey(content::WebContents
* owner_web_contents
,
132 int element_instance_id
)
133 : owner_web_contents(owner_web_contents
),
134 element_instance_id(element_instance_id
) {}
136 bool operator<(const ElementInstanceKey
& other
) const {
137 if (owner_web_contents
!= other
.owner_web_contents
)
138 return owner_web_contents
< other
.owner_web_contents
;
139 return element_instance_id
< other
.element_instance_id
;
142 bool operator==(const ElementInstanceKey
& other
) const {
143 return (owner_web_contents
== other
.owner_web_contents
) &&
144 (element_instance_id
== other
.element_instance_id
);
148 using GuestInstanceIDMap
= std::map
<ElementInstanceKey
, int>;
149 GuestInstanceIDMap instance_id_map_
;
151 // The reverse map of GuestInstanceIDMap.
152 using GuestInstanceIDReverseMap
= std::map
<int, ElementInstanceKey
>;
153 GuestInstanceIDReverseMap reverse_instance_id_map_
;
155 int current_instance_id_
;
157 // Any instance ID whose number not greater than this was removed via
159 // This is used so that we don't have store all removed instance IDs in
160 // |removed_instance_ids_|.
161 int last_instance_id_removed_
;
162 // The remaining instance IDs that are greater than
163 // |last_instance_id_removed_| are kept here.
164 std::set
<int> removed_instance_ids_
;
166 content::BrowserContext
* context_
;
168 DISALLOW_COPY_AND_ASSIGN(GuestViewManager
);
171 } // namespace extensions
173 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_