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