[email protected] | dd163fb0 | 2011-05-04 22:22:17 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/extension_info_map.h" |
| 6 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 7 | #include "chrome/common/extensions/extension.h" |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 8 | #include "chrome/common/extensions/extension_set.h" |
| 9 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 10 | #include "content/public/browser/browser_thread.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 11 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 12 | using content::BrowserThread; |
| 13 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 14 | namespace { |
| 15 | |
[email protected] | 2cce7ae | 2011-10-23 15:54:36 | [diff] [blame] | 16 | void CheckOnValidThread() { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 17 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | } // namespace |
| 21 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 22 | |
| 23 | struct ExtensionInfoMap::ExtraData { |
| 24 | // When the extension was installed. |
| 25 | base::Time install_time; |
| 26 | |
| 27 | // True if the user has allowed this extension to run in incognito mode. |
| 28 | bool incognito_enabled; |
| 29 | |
| 30 | ExtraData(); |
| 31 | ~ExtraData(); |
| 32 | }; |
| 33 | |
| 34 | ExtensionInfoMap::ExtraData::ExtraData() : incognito_enabled(false) { |
| 35 | } |
| 36 | |
| 37 | ExtensionInfoMap::ExtraData::~ExtraData() { |
| 38 | } |
| 39 | |
| 40 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 41 | ExtensionInfoMap::ExtensionInfoMap() { |
| 42 | } |
| 43 | |
| 44 | ExtensionInfoMap::~ExtensionInfoMap() { |
| 45 | } |
| 46 | |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 47 | const extensions::ProcessMap& ExtensionInfoMap::process_map() const { |
| 48 | return process_map_; |
| 49 | } |
| 50 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 51 | void ExtensionInfoMap::AddExtension(const Extension* extension, |
| 52 | base::Time install_time, |
| 53 | bool incognito_enabled) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 54 | CheckOnValidThread(); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 55 | extensions_.Insert(extension); |
| 56 | disabled_extensions_.Remove(extension->id()); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 57 | |
| 58 | extra_data_[extension->id()].install_time = install_time; |
| 59 | extra_data_[extension->id()].incognito_enabled = incognito_enabled; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 62 | void ExtensionInfoMap::RemoveExtension(const std::string& extension_id, |
[email protected] | 814a7bf0f | 2011-08-13 05:30:59 | [diff] [blame] | 63 | const extension_misc::UnloadedExtensionReason reason) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 64 | CheckOnValidThread(); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 65 | const Extension* extension = extensions_.GetByID(extension_id); |
| 66 | extra_data_.erase(extension_id); // we don't care about disabled extra data |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 67 | bool was_uninstalled = (reason != extension_misc::UNLOAD_REASON_DISABLE && |
| 68 | reason != extension_misc::UNLOAD_REASON_TERMINATE); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 69 | if (extension) { |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 70 | if (!was_uninstalled) |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 71 | disabled_extensions_.Insert(extension); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 72 | extensions_.Remove(extension_id); |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 73 | } else if (was_uninstalled) { |
[email protected] | dd163fb0 | 2011-05-04 22:22:17 | [diff] [blame] | 74 | // If the extension was uninstalled, make sure it's removed from the map of |
| 75 | // disabled extensions. |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 76 | disabled_extensions_.Remove(extension_id); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 77 | } else { |
| 78 | // NOTE: This can currently happen if we receive multiple unload |
| 79 | // notifications, e.g. setting incognito-enabled state for a |
| 80 | // disabled extension (e.g., via sync). See |
| 81 | // http://code.google.com/p/chromium/issues/detail?id=50582 . |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 82 | NOTREACHED() << extension_id; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 83 | } |
| 84 | } |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 85 | |
| 86 | base::Time ExtensionInfoMap::GetInstallTime( |
| 87 | const std::string& extension_id) const { |
| 88 | ExtraDataMap::const_iterator iter = extra_data_.find(extension_id); |
| 89 | if (iter != extra_data_.end()) |
| 90 | return iter->second.install_time; |
| 91 | return base::Time(); |
| 92 | } |
| 93 | |
| 94 | bool ExtensionInfoMap::IsIncognitoEnabled( |
| 95 | const std::string& extension_id) const { |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 96 | // Keep in sync with duplicate in extension_process_manager.cc. |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 97 | ExtraDataMap::const_iterator iter = extra_data_.find(extension_id); |
| 98 | if (iter != extra_data_.end()) |
| 99 | return iter->second.incognito_enabled; |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | bool ExtensionInfoMap::CanCrossIncognito(const Extension* extension) { |
| 104 | // This is duplicated from ExtensionService :(. |
| 105 | return IsIncognitoEnabled(extension->id()) && |
| 106 | !extension->incognito_split_mode(); |
| 107 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 108 | |
[email protected] | 2cce7ae | 2011-10-23 15:54:36 | [diff] [blame] | 109 | void ExtensionInfoMap::RegisterExtensionProcess(const std::string& extension_id, |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 110 | int process_id, |
| 111 | int site_instance_id) { |
| 112 | if (!process_map_.Insert(extension_id, process_id, site_instance_id)) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 113 | NOTREACHED() << "Duplicate extension process registration for: " |
| 114 | << extension_id << "," << process_id << "."; |
| 115 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 2cce7ae | 2011-10-23 15:54:36 | [diff] [blame] | 118 | void ExtensionInfoMap::UnregisterExtensionProcess( |
| 119 | const std::string& extension_id, |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 120 | int process_id, |
| 121 | int site_instance_id) { |
| 122 | if (!process_map_.Remove(extension_id, process_id, site_instance_id)) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 123 | NOTREACHED() << "Unknown extension process registration for: " |
| 124 | << extension_id << "," << process_id << "."; |
| 125 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 126 | } |
| 127 | |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 128 | void ExtensionInfoMap::UnregisterAllExtensionsInProcess(int process_id) { |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 129 | process_map_.RemoveAllFromProcess(process_id); |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 130 | } |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 131 | |
| 132 | bool ExtensionInfoMap::SecurityOriginHasAPIPermission( |
| 133 | const GURL& origin, int process_id, |
| 134 | ExtensionAPIPermission::ID permission) const { |
| 135 | if (origin.SchemeIs(chrome::kExtensionScheme)) { |
| 136 | const std::string& id = origin.host(); |
| 137 | return extensions_.GetByID(id)->HasAPIPermission(permission) && |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 138 | process_map_.Contains(id, process_id); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 141 | ExtensionSet::const_iterator i = extensions_.begin(); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 142 | for (; i != extensions_.end(); ++i) { |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 143 | if ((*i)->web_extent().MatchesSecurityOrigin(origin) && |
| 144 | process_map_.Contains((*i)->id(), process_id) && |
| 145 | (*i)->HasAPIPermission(permission)) { |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | return false; |
| 150 | } |