Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_INFO_MAP_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_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/browser/extensions/extensions_quota_service.h"
     15 #include "chrome/browser/extensions/process_map.h"
     16 #include "chrome/common/extensions/extension_constants.h"
     17 #include "chrome/common/extensions/extension_set.h"
     18 
     19 namespace extensions {
     20 class Extension;
     21 }
     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 ExtensionInfoMap : public base::RefCountedThreadSafe<ExtensionInfoMap> {
     27  public:
     28   ExtensionInfoMap();
     29 
     30   const ExtensionSet& extensions() const { return extensions_; }
     31   const ExtensionSet& disabled_extensions() const {
     32     return disabled_extensions_;
     33   }
     34 
     35   const extensions::ProcessMap& process_map() const;
     36 
     37   // Callback for when new extensions are loaded.
     38   void AddExtension(const extensions::Extension* extension,
     39                     base::Time install_time,
     40                     bool incognito_enabled);
     41 
     42   // Callback for when an extension is unloaded.
     43   void RemoveExtension(const std::string& extension_id,
     44                        const extension_misc::UnloadedExtensionReason reason);
     45 
     46   // Returns the time the extension was installed, or base::Time() if not found.
     47   base::Time GetInstallTime(const std::string& extension_id) const;
     48 
     49   // Returns true if the user has allowed this extension to run in incognito
     50   // mode.
     51   bool IsIncognitoEnabled(const std::string& extension_id) const;
     52 
     53   // Returns true if the given extension can see events and data from another
     54   // sub-profile (incognito to original profile, or vice versa).
     55   bool CanCrossIncognito(const extensions::Extension* extension) const;
     56 
     57   // Adds an entry to process_map_.
     58   void RegisterExtensionProcess(const std::string& extension_id,
     59                                 int process_id,
     60                                 int site_instance_id);
     61 
     62   // Removes an entry from process_map_.
     63   void UnregisterExtensionProcess(const std::string& extension_id,
     64                                   int process_id,
     65                                   int site_instance_id);
     66   void UnregisterAllExtensionsInProcess(int process_id);
     67 
     68   // Returns the subset of extensions which has the same |origin| in
     69   // |process_id| with the specified |permission|.
     70   void GetExtensionsWithAPIPermissionForSecurityOrigin(
     71       const GURL& origin,
     72       int process_id,
     73       extensions::APIPermission::ID permission,
     74       ExtensionSet* extensions) const;
     75 
     76   // Returns true if there is exists an extension with the same origin as
     77   // |origin| in |process_id| with |permission|.
     78   bool SecurityOriginHasAPIPermission(
     79       const GURL& origin, int process_id,
     80       extensions::APIPermission::ID permission) const;
     81 
     82   ExtensionsQuotaService* 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  private:
     90   friend class base::RefCountedThreadSafe<ExtensionInfoMap>;
     91 
     92   // Extra dynamic data related to an extension.
     93   struct ExtraData;
     94   // Map of extension_id to ExtraData.
     95   typedef std::map<std::string, ExtraData> ExtraDataMap;
     96 
     97   ~ExtensionInfoMap();
     98 
     99   ExtensionSet extensions_;
    100   ExtensionSet disabled_extensions_;
    101 
    102   // Extra data associated with enabled extensions.
    103   ExtraDataMap extra_data_;
    104 
    105   // Used by dispatchers to limit API quota for individual extensions.
    106   // The ExtensionQutoaService is not thread safe. We need to create and destroy
    107   // it on the IO thread.
    108   scoped_ptr<ExtensionsQuotaService> quota_service_;
    109 
    110   // Assignment of extensions to processes.
    111   extensions::ProcessMap process_map_;
    112 
    113   int signin_process_id_;
    114 };
    115 
    116 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_INFO_MAP_H_
    117