Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / guest_view / guest_view_manager.h
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.
4
5 #ifndef EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
6 #define EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
7
8 #include <map>
9
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"
16
17 class GURL;
18
19 namespace content {
20 class BrowserContext;
21 class WebContents;
22 }  // namespace content
23
24 namespace extensions{
25 class GuestViewBase;
26 class GuestViewManagerFactory;
27
28 class GuestViewManager : public content::BrowserPluginGuestManager,
29                          public base::SupportsUserData::Data {
30  public:
31   explicit GuestViewManager(content::BrowserContext* context);
32   virtual ~GuestViewManager();
33
34   static GuestViewManager* FromBrowserContext(content::BrowserContext* context);
35
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;
40   }
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);
49
50   int GetNextInstanceID();
51
52   typedef base::Callback<void(content::WebContents*)>
53       WebContentsCreatedCallback;
54   void CreateGuest(const std::string& view_type,
55                    const std::string& embedder_extension_id,
56                    content::WebContents* embedder_web_contents,
57                    const base::DictionaryValue& create_params,
58                    const WebContentsCreatedCallback& callback);
59
60   content::WebContents* CreateGuestWithWebContentsParams(
61       const std::string& view_type,
62       const std::string& embedder_extension_id,
63       int embedder_render_process_id,
64       const content::WebContents::CreateParams& create_params);
65
66   content::SiteInstance* GetGuestSiteInstance(
67       const GURL& guest_site);
68
69   // BrowserPluginGuestManager implementation.
70   virtual void MaybeGetGuestByInstanceIDOrKill(
71       int guest_instance_id,
72       int embedder_render_process_id,
73       const GuestByInstanceIDCallback& callback) OVERRIDE;
74   virtual bool ForEachGuest(content::WebContents* embedder_web_contents,
75                             const GuestCallback& callback) OVERRIDE;
76
77  protected:
78   friend class GuestViewBase;
79   FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest, AddRemove);
80
81   // Can be overriden in tests.
82   virtual void AddGuest(int guest_instance_id,
83                         content::WebContents* guest_web_contents);
84
85   void RemoveGuest(int guest_instance_id);
86
87   content::WebContents* GetGuestByInstanceID(int guest_instance_id);
88
89   bool CanEmbedderAccessInstanceIDMaybeKill(
90       int embedder_render_process_id,
91       int guest_instance_id);
92
93   bool CanEmbedderAccessInstanceID(int embedder_render_process_id,
94                                    int guest_instance_id);
95
96   // Returns true if |guest_instance_id| can be used to add a new guest to this
97   // manager.
98   // We disallow adding new guest with instance IDs that were previously removed
99   // from this manager using RemoveGuest.
100   bool CanUseGuestInstanceID(int guest_instance_id);
101
102   // Static factory instance (always NULL for non-test).
103   static GuestViewManagerFactory* factory_;
104
105   // Contains guests' WebContents, mapping from their instance ids.
106   typedef std::map<int, content::WebContents*> GuestInstanceMap;
107   GuestInstanceMap guest_web_contents_by_instance_id_;
108
109   int current_instance_id_;
110
111   // Any instance ID whose number not greater than this was removed via
112   // RemoveGuest.
113   // This is used so that we don't have store all removed instance IDs in
114   // |removed_instance_ids_|.
115   int last_instance_id_removed_;
116   // The remaining instance IDs that are greater than
117   // |last_instance_id_removed_| are kept here.
118   std::set<int> removed_instance_ids_;
119
120   content::BrowserContext* context_;
121
122   DISALLOW_COPY_AND_ASSIGN(GuestViewManager);
123 };
124
125 }  // namespace extensions
126
127 #endif  // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_