1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef EXTENSIONS_BROWSER_INFO_MAP_H_
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define EXTENSIONS_BROWSER_INFO_MAP_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/process_map.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/quota_service.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/common/extension_set.h"
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "extensions/common/permissions/api_permission.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace base {
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class FilePath;
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions {
24010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class ContentVerifier;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Extension;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Contains extension data that needs to be accessed on the IO thread. It can
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be created/destroyed on any thread, but all other methods must be called on
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the IO thread.
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class InfoMap : public base::RefCountedThreadSafe<InfoMap> {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  InfoMap();
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ExtensionSet& extensions() const { return extensions_; }
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ExtensionSet& disabled_extensions() const {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return disabled_extensions_;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Information about which extensions are assigned to which render processes.
406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  const ProcessMap& process_map() const { return process_map_; }
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Callback for when new extensions are loaded.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddExtension(const extensions::Extension* extension,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    base::Time install_time,
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                    bool incognito_enabled,
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                    bool notifications_disabled);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Callback for when an extension is unloaded.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveExtension(const std::string& extension_id,
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                       const extensions::UnloadedExtensionInfo::Reason reason);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the time the extension was installed, or base::Time() if not found.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time GetInstallTime(const std::string& extension_id) const;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the user has allowed this extension to run in incognito
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // mode.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsIncognitoEnabled(const std::string& extension_id) const;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the given extension can see events and data from another
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sub-profile (incognito to original profile, or vice versa).
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CanCrossIncognito(const extensions::Extension* extension) const;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds an entry to process_map_.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RegisterExtensionProcess(const std::string& extension_id,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                int process_id,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                int site_instance_id);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes an entry from process_map_.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UnregisterExtensionProcess(const std::string& extension_id,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  int process_id,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  int site_instance_id);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UnregisterAllExtensionsInProcess(int process_id);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns the subset of extensions which has the same |origin| in
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // |process_id| with the specified |permission|.
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void GetExtensionsWithAPIPermissionForSecurityOrigin(
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const GURL& origin,
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int process_id,
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::APIPermission::ID permission,
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ExtensionSet* extensions) const;
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if there is exists an extension with the same origin as
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |origin| in |process_id| with |permission|.
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool SecurityOriginHasAPIPermission(const GURL& origin,
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                      int process_id,
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                      extensions::APIPermission::ID permission)
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Maps a |file_url| to a |file_path| on the local filesystem, including
906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // resources in extensions. Returns true on success. See NaClBrowserDelegate
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // for full details.
926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool MapUrlToLocalFilePath(const GURL& file_url,
936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                             bool use_blocking_api,
946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                             base::FilePath* file_path);
956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns the IO thread QuotaService. Creates the instance on first call.
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  QuotaService* GetQuotaService();
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Keep track of the signin process, so we can restrict extension access to
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // it.
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void SetSigninProcess(int process_id);
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool IsSigninProcess(int process_id) const;
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Notifications can be enabled/disabled in real time by the user.
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void SetNotificationsDisabled(const std::string& extension_id,
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                bool notifications_disabled);
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool AreNotificationsDisabled(const std::string& extension_id) const;
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
109010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void SetContentVerifier(ContentVerifier* verifier);
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ContentVerifier* content_verifier() { return content_verifier_.get(); }
111010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
113f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  friend class base::RefCountedThreadSafe<InfoMap>;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extra dynamic data related to an extension.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct ExtraData;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Map of extension_id to ExtraData.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<std::string, ExtraData> ExtraDataMap;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ~InfoMap();
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExtensionSet extensions_;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExtensionSet disabled_extensions_;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Extra data associated with enabled extensions.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExtraDataMap extra_data_;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used by dispatchers to limit API quota for individual extensions.
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The QuotaService is not thread safe. We need to create and destroy it on
130f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // the IO thread.
131f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<QuotaService> quota_service_;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Assignment of extensions to renderer processes.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extensions::ProcessMap process_map_;
13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  int signin_process_id_;
137010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
138010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  scoped_refptr<ContentVerifier> content_verifier_;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace extensions
142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif  // EXTENSIONS_BROWSER_INFO_MAP_H_
144