Make castv2 performance test work.
[chromium-blink-merge.git] / extensions / browser / guest_view / guest_view_manager.h
blob934c46747dd4656b813caf6118ed1df0fbfac3d9
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_process_id,
61 int element_instance_id,
62 int guest_instance_id,
63 const base::DictionaryValue& attach_params);
65 // Removes the association between |element_instance_id| and a guest instance
66 // ID if one exists.
67 void DetachGuest(GuestViewBase* guest);
69 int GetNextInstanceID();
70 int GetGuestInstanceIDForElementID(
71 int owner_process_id,
72 int element_instance_id);
74 using WebContentsCreatedCallback =
75 base::Callback<void(content::WebContents*)>;
76 void CreateGuest(const std::string& view_type,
77 content::WebContents* owner_web_contents,
78 const base::DictionaryValue& create_params,
79 const WebContentsCreatedCallback& callback);
81 content::WebContents* CreateGuestWithWebContentsParams(
82 const std::string& view_type,
83 content::WebContents* owner_web_contents,
84 const content::WebContents::CreateParams& create_params);
86 content::SiteInstance* GetGuestSiteInstance(
87 const GURL& guest_site);
89 // BrowserPluginGuestManager implementation.
90 content::WebContents* GetGuestByInstanceID(
91 int owner_process_id,
92 int element_instance_id) override;
93 bool ForEachGuest(content::WebContents* owner_web_contents,
94 const GuestCallback& callback) override;
96 protected:
97 friend class GuestViewBase;
98 FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest, AddRemove);
100 // Can be overriden in tests.
101 virtual void AddGuest(int guest_instance_id,
102 content::WebContents* guest_web_contents);
104 // Can be overriden in tests.
105 virtual void RemoveGuest(int guest_instance_id);
107 content::WebContents* GetGuestByInstanceID(int guest_instance_id);
109 bool CanEmbedderAccessInstanceIDMaybeKill(
110 int embedder_render_process_id,
111 int guest_instance_id);
113 bool CanEmbedderAccessInstanceID(int embedder_render_process_id,
114 int guest_instance_id);
116 // Returns true if |guest_instance_id| can be used to add a new guest to this
117 // manager.
118 // We disallow adding new guest with instance IDs that were previously removed
119 // from this manager using RemoveGuest.
120 bool CanUseGuestInstanceID(int guest_instance_id);
122 // Static factory instance (always NULL for non-test).
123 static GuestViewManagerFactory* factory_;
125 // Contains guests' WebContents, mapping from their instance ids.
126 using GuestInstanceMap = std::map<int, content::WebContents*>;
127 GuestInstanceMap guest_web_contents_by_instance_id_;
129 struct ElementInstanceKey {
130 int embedder_process_id;
131 int element_instance_id;
133 ElementInstanceKey();
134 ElementInstanceKey(int embedder_process_id,
135 int element_instance_id);
137 bool operator<(const ElementInstanceKey& other) const;
138 bool operator==(const ElementInstanceKey& other) const;
141 using GuestInstanceIDMap = std::map<ElementInstanceKey, int>;
142 GuestInstanceIDMap instance_id_map_;
144 // The reverse map of GuestInstanceIDMap.
145 using GuestInstanceIDReverseMap = std::map<int, ElementInstanceKey>;
146 GuestInstanceIDReverseMap reverse_instance_id_map_;
148 int current_instance_id_;
150 // Any instance ID whose number not greater than this was removed via
151 // RemoveGuest.
152 // This is used so that we don't have store all removed instance IDs in
153 // |removed_instance_ids_|.
154 int last_instance_id_removed_;
155 // The remaining instance IDs that are greater than
156 // |last_instance_id_removed_| are kept here.
157 std::set<int> removed_instance_ids_;
159 content::BrowserContext* context_;
161 DISALLOW_COPY_AND_ASSIGN(GuestViewManager);
164 } // namespace extensions
166 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_