Home | History | Annotate | Download | only in browser
      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 "chrome/common/extensions/extension_set.h"
     15 #include "extensions/browser/process_map.h"
     16 #include "extensions/browser/quota_service.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   const extensions::ProcessMap& process_map() const;
     34 
     35   // Callback for when new extensions are loaded.
     36   void AddExtension(const extensions::Extension* extension,
     37                     base::Time install_time,
     38                     bool incognito_enabled,
     39                     bool notifications_disabled);
     40 
     41   // Callback for when an extension is unloaded.
     42   void RemoveExtension(const std::string& extension_id,
     43                        const extensions::UnloadedExtensionInfo::Reason reason);
     44 
     45   // Returns the time the extension was installed, or base::Time() if not found.
     46   base::Time GetInstallTime(const std::string& extension_id) const;
     47 
     48   // Returns true if the user has allowed this extension to run in incognito
     49   // mode.
     50   bool IsIncognitoEnabled(const std::string& extension_id) const;
     51 
     52   // Returns true if the given extension can see events and data from another
     53   // sub-profile (incognito to original profile, or vice versa).
     54   bool CanCrossIncognito(const extensions::Extension* extension) const;
     55 
     56   // Adds an entry to process_map_.
     57   void RegisterExtensionProcess(const std::string& extension_id,
     58                                 int process_id,
     59                                 int site_instance_id);
     60 
     61   // Removes an entry from process_map_.
     62   void UnregisterExtensionProcess(const std::string& extension_id,
     63                                   int process_id,
     64                                   int site_instance_id);
     65   void UnregisterAllExtensionsInProcess(int process_id);
     66 
     67   // Returns the subset of extensions which has the same |origin| in
     68   // |process_id| with the specified |permission|.
     69   void GetExtensionsWithAPIPermissionForSecurityOrigin(
     70       const GURL& origin,
     71       int process_id,
     72       extensions::APIPermission::ID permission,
     73       ExtensionSet* extensions) const;
     74 
     75   // Returns true if there is exists an extension with the same origin as
     76   // |origin| in |process_id| with |permission|.
     77   bool SecurityOriginHasAPIPermission(const GURL& origin,
     78                                       int process_id,
     79                                       extensions::APIPermission::ID permission)
     80       const;
     81 
     82   QuotaService* GetQuotaService();
     83 
     84   // Keep track of the signin process, so we can restrict extension access to
     85   // it.
     86   void SetSigninProcess(int process_id);
     87   bool IsSigninProcess(int process_id) const;
     88 
     89   // Notifications can be enabled/disabled in real time by the user.
     90   void SetNotificationsDisabled(const std::string& extension_id,
     91                                bool notifications_disabled);
     92   bool AreNotificationsDisabled(const std::string& extension_id)
     93       const;
     94 
     95  private:
     96   friend class base::RefCountedThreadSafe<InfoMap>;
     97 
     98   // Extra dynamic data related to an extension.
     99   struct ExtraData;
    100   // Map of extension_id to ExtraData.
    101   typedef std::map<std::string, ExtraData> ExtraDataMap;
    102 
    103   ~InfoMap();
    104 
    105   ExtensionSet extensions_;
    106   ExtensionSet disabled_extensions_;
    107 
    108   // Extra data associated with enabled extensions.
    109   ExtraDataMap extra_data_;
    110 
    111   // Used by dispatchers to limit API quota for individual extensions.
    112   // The QuotaService is not thread safe. We need to create and destroy it on
    113   // the IO thread.
    114   scoped_ptr<QuotaService> quota_service_;
    115 
    116   // Assignment of extensions to processes.
    117   extensions::ProcessMap process_map_;
    118 
    119   int signin_process_id_;
    120 };
    121 
    122 }  // namespace extensions
    123 
    124 #endif  // EXTENSIONS_BROWSER_INFO_MAP_H_
    125