Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / info_map.h
1 // Copyright 2013 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_INFO_MAP_H_
6 #define EXTENSIONS_BROWSER_INFO_MAP_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time/time.h"
14 #include "extensions/browser/process_map.h"
15 #include "extensions/browser/quota_service.h"
16 #include "extensions/common/extension_set.h"
17
18 namespace extensions {
19 class Extension;
20
21 // Contains extension data that needs to be accessed on the IO thread. It can
22 // be created/destroyed on any thread, but all other methods must be called on
23 // the IO thread.
24 class InfoMap : public base::RefCountedThreadSafe<InfoMap> {
25  public:
26   InfoMap();
27
28   const ExtensionSet& extensions() const { return extensions_; }
29   const ExtensionSet& disabled_extensions() const {
30     return disabled_extensions_;
31   }
32
33   // Information about which extensions are assigned to which render processes.
34   const extensions::ProcessMap& process_map() const;
35   // Information about which extensions are assigned to which worker processes.
36   const extensions::ProcessMap& worker_process_map() const;
37
38   // Callback for when new extensions are loaded.
39   void AddExtension(const extensions::Extension* extension,
40                     base::Time install_time,
41                     bool incognito_enabled,
42                     bool notifications_disabled);
43
44   // Callback for when an extension is unloaded.
45   void RemoveExtension(const std::string& extension_id,
46                        const extensions::UnloadedExtensionInfo::Reason reason);
47
48   // Returns the time the extension was installed, or base::Time() if not found.
49   base::Time GetInstallTime(const std::string& extension_id) const;
50
51   // Returns true if the user has allowed this extension to run in incognito
52   // mode.
53   bool IsIncognitoEnabled(const std::string& extension_id) const;
54
55   // Returns true if the given extension can see events and data from another
56   // sub-profile (incognito to original profile, or vice versa).
57   bool CanCrossIncognito(const extensions::Extension* extension) const;
58
59   // Adds an entry to process_map_.
60   void RegisterExtensionProcess(const std::string& extension_id,
61                                 int process_id,
62                                 int site_instance_id);
63
64   // Removes an entry from process_map_.
65   void UnregisterExtensionProcess(const std::string& extension_id,
66                                   int process_id,
67                                   int site_instance_id);
68   void UnregisterAllExtensionsInProcess(int process_id);
69
70   // Adds an entry to worker_process_map_.
71   void RegisterExtensionWorkerProcess(const std::string& extension_id,
72                                       int process_id,
73                                       int site_instance_id);
74
75   // Removes an entry from worker_process_map_.
76   void UnregisterExtensionWorkerProcess(int process_id);
77
78   // Returns the subset of extensions which has the same |origin| in
79   // |process_id| with the specified |permission|.
80   void GetExtensionsWithAPIPermissionForSecurityOrigin(
81       const GURL& origin,
82       int process_id,
83       extensions::APIPermission::ID permission,
84       ExtensionSet* extensions) const;
85
86   // Returns true if there is exists an extension with the same origin as
87   // |origin| in |process_id| with |permission|.
88   bool SecurityOriginHasAPIPermission(const GURL& origin,
89                                       int process_id,
90                                       extensions::APIPermission::ID permission)
91       const;
92
93   // Returns the IO thread QuotaService. Creates the instance on first call.
94   QuotaService* GetQuotaService();
95
96   // Keep track of the signin process, so we can restrict extension access to
97   // it.
98   void SetSigninProcess(int process_id);
99   bool IsSigninProcess(int process_id) const;
100
101   // Notifications can be enabled/disabled in real time by the user.
102   void SetNotificationsDisabled(const std::string& extension_id,
103                                 bool notifications_disabled);
104   bool AreNotificationsDisabled(const std::string& extension_id) const;
105
106  private:
107   friend class base::RefCountedThreadSafe<InfoMap>;
108
109   // Extra dynamic data related to an extension.
110   struct ExtraData;
111   // Map of extension_id to ExtraData.
112   typedef std::map<std::string, ExtraData> ExtraDataMap;
113
114   ~InfoMap();
115
116   ExtensionSet extensions_;
117   ExtensionSet disabled_extensions_;
118
119   // Extra data associated with enabled extensions.
120   ExtraDataMap extra_data_;
121
122   // Used by dispatchers to limit API quota for individual extensions.
123   // The QuotaService is not thread safe. We need to create and destroy it on
124   // the IO thread.
125   scoped_ptr<QuotaService> quota_service_;
126
127   // Assignment of extensions to renderer processes.
128   extensions::ProcessMap process_map_;
129
130   // Assignment of extensions to worker processes.
131   extensions::ProcessMap worker_process_map_;
132
133   int signin_process_id_;
134 };
135
136 }  // namespace extensions
137
138 #endif  // EXTENSIONS_BROWSER_INFO_MAP_H_