[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / extensions / browser / guest_view / guest_view_manager.h
bloba38dbb72aade3b4b9a282956b049ab12dff98de4
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_
8 #include <map>
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"
17 class GURL;
19 namespace content {
20 class BrowserContext;
21 class WebContents;
22 } // namespace content
24 namespace extensions{
25 class GuestViewBase;
26 class GuestViewManagerFactory;
28 class GuestViewManager : public content::BrowserPluginGuestManager,
29 public base::SupportsUserData::Data {
30 public:
31 explicit GuestViewManager(content::BrowserContext* context);
32 ~GuestViewManager() override;
34 // Returns the GuestViewManager associated with |context|. If one isn't
35 // available, then it is created and returned.
36 static GuestViewManager* FromBrowserContext(content::BrowserContext* context);
38 // Returns the GuestViewManager associated with |context|. If one isn't
39 // available, then nullptr is returned.
40 static GuestViewManager* FromBrowserContextIfAvailable(
41 content::BrowserContext* context);
43 // Overrides factory for testing. Default (NULL) value indicates regular
44 // (non-test) environment.
45 static void set_factory_for_testing(GuestViewManagerFactory* factory) {
46 GuestViewManager::factory_ = factory;
48 // Returns the guest WebContents associated with the given |guest_instance_id|
49 // if the provided |embedder_render_process_id| is allowed to access it.
50 // If the embedder is not allowed access, the embedder will be killed, and
51 // this method will return NULL. If no WebContents exists with the given
52 // instance ID, then NULL will also be returned.
53 content::WebContents* GetGuestByInstanceIDSafely(
54 int guest_instance_id,
55 int embedder_render_process_id);
57 // Associates the Browser Plugin with |element_instance_id| to a
58 // guest that has ID of |guest_instance_id| and sets initialization
59 // parameters, |params| for it.
60 void AttachGuest(int embedder_render_process_id,
61 int embedder_routing_id,
62 int element_instance_id,
63 int guest_instance_id,
64 const base::DictionaryValue& attach_params);
66 // Removes the association between |element_instance_id| and a guest instance
67 // ID if one exists.
68 void DetachGuest(GuestViewBase* guest, int element_instance_id);
70 int GetNextInstanceID();
71 int GetGuestInstanceIDForElementID(
72 content::WebContents* owner_web_contents,
73 int element_instance_id);
75 using WebContentsCreatedCallback =
76 base::Callback<void(content::WebContents*)>;
77 void CreateGuest(const std::string& view_type,
78 content::WebContents* owner_web_contents,
79 const base::DictionaryValue& create_params,
80 const WebContentsCreatedCallback& callback);
82 content::WebContents* CreateGuestWithWebContentsParams(
83 const std::string& view_type,
84 content::WebContents* owner_web_contents,
85 const content::WebContents::CreateParams& create_params);
87 content::SiteInstance* GetGuestSiteInstance(
88 const GURL& guest_site);
90 // BrowserPluginGuestManager implementation.
91 content::WebContents* GetGuestByInstanceID(
92 content::WebContents* owner_web_contents,
93 int element_instance_id) override;
94 bool ForEachGuest(content::WebContents* owner_web_contents,
95 const GuestCallback& callback) override;
97 protected:
98 friend class GuestViewBase;
99 FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest, AddRemove);
101 // Can be overriden in tests.
102 virtual void AddGuest(int guest_instance_id,
103 content::WebContents* guest_web_contents);
105 // Can be overriden in tests.
106 virtual void RemoveGuest(int guest_instance_id);
108 content::WebContents* GetGuestByInstanceID(int guest_instance_id);
110 bool CanEmbedderAccessInstanceIDMaybeKill(
111 int embedder_render_process_id,
112 int guest_instance_id);
114 bool CanEmbedderAccessInstanceID(int embedder_render_process_id,
115 int guest_instance_id);
117 // Returns true if |guest_instance_id| can be used to add a new guest to this
118 // manager.
119 // We disallow adding new guest with instance IDs that were previously removed
120 // from this manager using RemoveGuest.
121 bool CanUseGuestInstanceID(int guest_instance_id);
123 // Static factory instance (always NULL for non-test).
124 static GuestViewManagerFactory* factory_;
126 // Contains guests' WebContents, mapping from their instance ids.
127 using GuestInstanceMap = std::map<int, content::WebContents*>;
128 GuestInstanceMap guest_web_contents_by_instance_id_;
130 struct ElementInstanceKey {
131 content::WebContents* owner_web_contents;
132 int element_instance_id;
134 ElementInstanceKey()
135 : owner_web_contents(nullptr),
136 element_instance_id(0) {}
138 ElementInstanceKey(content::WebContents* owner_web_contents,
139 int element_instance_id)
140 : owner_web_contents(owner_web_contents),
141 element_instance_id(element_instance_id) {}
143 bool operator<(const ElementInstanceKey& other) const {
144 if (owner_web_contents != other.owner_web_contents)
145 return owner_web_contents < other.owner_web_contents;
146 return element_instance_id < other.element_instance_id;
149 bool operator==(const ElementInstanceKey& other) const {
150 return (owner_web_contents == other.owner_web_contents) &&
151 (element_instance_id == other.element_instance_id);
155 using GuestInstanceIDMap = std::map<ElementInstanceKey, int>;
156 GuestInstanceIDMap instance_id_map_;
158 // The reverse map of GuestInstanceIDMap.
159 using GuestInstanceIDReverseMap = std::map<int, ElementInstanceKey>;
160 GuestInstanceIDReverseMap reverse_instance_id_map_;
162 int current_instance_id_;
164 // Any instance ID whose number not greater than this was removed via
165 // RemoveGuest.
166 // This is used so that we don't have store all removed instance IDs in
167 // |removed_instance_ids_|.
168 int last_instance_id_removed_;
169 // The remaining instance IDs that are greater than
170 // |last_instance_id_removed_| are kept here.
171 std::set<int> removed_instance_ids_;
173 content::BrowserContext* context_;
175 DISALLOW_COPY_AND_ASSIGN(GuestViewManager);
178 } // namespace extensions
180 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_