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