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 CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
6 #define CHROME_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"
19 class GuestViewManagerFactory
;
24 } // namespace content
27 class TestGuestViewManager
;
28 } // namespace guestview
30 class GuestViewManager
: public content::BrowserPluginGuestManager
,
31 public base::SupportsUserData::Data
{
33 explicit GuestViewManager(content::BrowserContext
* context
);
34 virtual ~GuestViewManager();
36 static GuestViewManager
* FromBrowserContext(content::BrowserContext
* context
);
38 // Overrides factory for testing. Default (NULL) value indicates regular
39 // (non-test) environment.
40 static void set_factory_for_testing(GuestViewManagerFactory
* factory
) {
41 GuestViewManager::factory_
= factory
;
43 // Returns the guest WebContents associated with the given |guest_instance_id|
44 // if the provided |embedder_render_process_id| is allowed to access it.
45 // If the embedder is not allowed access, the embedder will be killed, and
46 // this method will return NULL. If no WebContents exists with the given
47 // instance ID, then NULL will also be returned.
48 content::WebContents
* GetGuestByInstanceIDSafely(
49 int guest_instance_id
,
50 int embedder_render_process_id
);
52 int GetNextInstanceID();
54 typedef base::Callback
<void(content::WebContents
*)>
55 WebContentsCreatedCallback
;
57 const std::string
& view_type
,
58 const std::string
& embedder_extension_id
,
59 int embedder_render_process_id
,
60 const base::DictionaryValue
& create_params
,
61 const WebContentsCreatedCallback
& callback
);
63 content::WebContents
* CreateGuestWithWebContentsParams(
64 const std::string
& view_type
,
65 const std::string
& embedder_extension_id
,
66 int embedder_render_process_id
,
67 const content::WebContents::CreateParams
& create_params
);
69 content::SiteInstance
* GetGuestSiteInstance(
70 const GURL
& guest_site
);
72 // BrowserPluginGuestManager implementation.
73 virtual void MaybeGetGuestByInstanceIDOrKill(
74 int guest_instance_id
,
75 int embedder_render_process_id
,
76 const GuestByInstanceIDCallback
& callback
) OVERRIDE
;
77 virtual bool ForEachGuest(content::WebContents
* embedder_web_contents
,
78 const GuestCallback
& callback
) OVERRIDE
;
81 friend class AppViewGuest
;
82 friend class GuestViewBase
;
83 friend class guestview::TestGuestViewManager
;
84 FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest
, AddRemove
);
86 // Can be overriden in tests.
87 virtual void AddGuest(int guest_instance_id
,
88 content::WebContents
* guest_web_contents
);
90 void RemoveGuest(int guest_instance_id
);
92 content::WebContents
* GetGuestByInstanceID(int guest_instance_id
);
94 bool CanEmbedderAccessInstanceIDMaybeKill(
95 int embedder_render_process_id
,
96 int guest_instance_id
);
98 bool CanEmbedderAccessInstanceID(int embedder_render_process_id
,
99 int guest_instance_id
);
101 // Returns true if |guest_instance_id| can be used to add a new guest to this
103 // We disallow adding new guest with instance IDs that were previously removed
104 // from this manager using RemoveGuest.
105 bool CanUseGuestInstanceID(int guest_instance_id
);
107 // Static factory instance (always NULL for non-test).
108 static GuestViewManagerFactory
* factory_
;
110 // Contains guests' WebContents, mapping from their instance ids.
111 typedef std::map
<int, content::WebContents
*> GuestInstanceMap
;
112 GuestInstanceMap guest_web_contents_by_instance_id_
;
114 int current_instance_id_
;
116 // Any instance ID whose number not greater than this was removed via
118 // This is used so that we don't have store all removed instance IDs in
119 // |removed_instance_ids_|.
120 int last_instance_id_removed_
;
121 // The remaining instance IDs that are greater than
122 // |last_instance_id_removed_| are kept here.
123 std::set
<int> removed_instance_ids_
;
125 content::BrowserContext
* context_
;
127 DISALLOW_COPY_AND_ASSIGN(GuestViewManager
);
130 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_