[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 1 | // Copyright (c) 2012 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" |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 9 | #include "chrome/common/extensions/incognito_handler.h" |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 10 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 11 | #include "content/public/browser/browser_thread.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 12 | #include "extensions/common/constants.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 13 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 14 | using content::BrowserThread; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 15 | using extensions::Extension; |
[email protected] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 16 | using extensions::UnloadedExtensionInfo; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 17 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 18 | namespace { |
| 19 | |
[email protected] | 2cce7ae | 2011-10-23 15:54:36 | [diff] [blame] | 20 | void CheckOnValidThread() { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 21 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | } // namespace |
| 25 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 26 | struct ExtensionInfoMap::ExtraData { |
| 27 | // When the extension was installed. |
| 28 | base::Time install_time; |
| 29 | |
| 30 | // True if the user has allowed this extension to run in incognito mode. |
| 31 | bool incognito_enabled; |
| 32 | |
| 33 | ExtraData(); |
| 34 | ~ExtraData(); |
| 35 | }; |
| 36 | |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 37 | ExtensionInfoMap::ExtraData::ExtraData() : incognito_enabled(false) {} |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 38 | |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 39 | ExtensionInfoMap::ExtraData::~ExtraData() {} |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 40 | |
[email protected] | 1deace2 | 2013-05-22 06:14:46 | [diff] [blame] | 41 | ExtensionInfoMap::ExtensionInfoMap() : signin_process_id_(-1) {} |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 42 | |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 43 | const extensions::ProcessMap& ExtensionInfoMap::process_map() const { |
| 44 | return process_map_; |
| 45 | } |
| 46 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 47 | void ExtensionInfoMap::AddExtension(const Extension* extension, |
| 48 | base::Time install_time, |
| 49 | bool incognito_enabled) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 50 | CheckOnValidThread(); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 51 | extensions_.Insert(extension); |
| 52 | disabled_extensions_.Remove(extension->id()); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 53 | |
| 54 | extra_data_[extension->id()].install_time = install_time; |
| 55 | extra_data_[extension->id()].incognito_enabled = incognito_enabled; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 58 | void ExtensionInfoMap::RemoveExtension( |
| 59 | const std::string& extension_id, |
| 60 | const UnloadedExtensionInfo::Reason reason) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 61 | CheckOnValidThread(); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 62 | const Extension* extension = extensions_.GetByID(extension_id); |
| 63 | extra_data_.erase(extension_id); // we don't care about disabled extra data |
[email protected] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 64 | bool was_uninstalled = (reason != UnloadedExtensionInfo::REASON_DISABLE && |
| 65 | reason != UnloadedExtensionInfo::REASON_TERMINATE); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 66 | if (extension) { |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 67 | if (!was_uninstalled) |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 68 | disabled_extensions_.Insert(extension); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 69 | extensions_.Remove(extension_id); |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 70 | } else if (was_uninstalled) { |
[email protected] | dd163fb0 | 2011-05-04 22:22:17 | [diff] [blame] | 71 | // If the extension was uninstalled, make sure it's removed from the map of |
| 72 | // disabled extensions. |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 73 | disabled_extensions_.Remove(extension_id); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 74 | } else { |
| 75 | // NOTE: This can currently happen if we receive multiple unload |
| 76 | // notifications, e.g. setting incognito-enabled state for a |
| 77 | // disabled extension (e.g., via sync). See |
| 78 | // http://code.google.com/p/chromium/issues/detail?id=50582 . |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 79 | NOTREACHED() << extension_id; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 80 | } |
| 81 | } |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 82 | |
| 83 | base::Time ExtensionInfoMap::GetInstallTime( |
| 84 | const std::string& extension_id) const { |
| 85 | ExtraDataMap::const_iterator iter = extra_data_.find(extension_id); |
| 86 | if (iter != extra_data_.end()) |
| 87 | return iter->second.install_time; |
| 88 | return base::Time(); |
| 89 | } |
| 90 | |
| 91 | bool ExtensionInfoMap::IsIncognitoEnabled( |
| 92 | const std::string& extension_id) const { |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 93 | // Keep in sync with duplicate in extension_process_manager.cc. |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 94 | ExtraDataMap::const_iterator iter = extra_data_.find(extension_id); |
| 95 | if (iter != extra_data_.end()) |
| 96 | return iter->second.incognito_enabled; |
| 97 | return false; |
| 98 | } |
| 99 | |
[email protected] | 3dfa4c0 | 2012-07-30 17:21:41 | [diff] [blame] | 100 | bool ExtensionInfoMap::CanCrossIncognito(const Extension* extension) const { |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 101 | // This is duplicated from ExtensionService :(. |
| 102 | return IsIncognitoEnabled(extension->id()) && |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 103 | !extensions::IncognitoInfo::IsSplitMode(extension); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 104 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 105 | |
[email protected] | 2cce7ae | 2011-10-23 15:54:36 | [diff] [blame] | 106 | void ExtensionInfoMap::RegisterExtensionProcess(const std::string& extension_id, |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 107 | int process_id, |
| 108 | int site_instance_id) { |
| 109 | if (!process_map_.Insert(extension_id, process_id, site_instance_id)) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 110 | NOTREACHED() << "Duplicate extension process registration for: " |
| 111 | << extension_id << "," << process_id << "."; |
| 112 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | 2cce7ae | 2011-10-23 15:54:36 | [diff] [blame] | 115 | void ExtensionInfoMap::UnregisterExtensionProcess( |
| 116 | const std::string& extension_id, |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 117 | int process_id, |
| 118 | int site_instance_id) { |
| 119 | if (!process_map_.Remove(extension_id, process_id, site_instance_id)) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 120 | NOTREACHED() << "Unknown extension process registration for: " |
| 121 | << extension_id << "," << process_id << "."; |
| 122 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 125 | void ExtensionInfoMap::UnregisterAllExtensionsInProcess(int process_id) { |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 126 | process_map_.RemoveAllFromProcess(process_id); |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 127 | } |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 128 | |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 129 | void ExtensionInfoMap::GetExtensionsWithAPIPermissionForSecurityOrigin( |
| 130 | const GURL& origin, |
| 131 | int process_id, |
| 132 | extensions::APIPermission::ID permission, |
| 133 | ExtensionSet* extensions) const { |
| 134 | DCHECK(extensions); |
| 135 | |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 136 | if (origin.SchemeIs(extensions::kExtensionScheme)) { |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 137 | const std::string& id = origin.host(); |
[email protected] | af73c25 | 2012-07-27 00:16:39 | [diff] [blame] | 138 | const Extension* extension = extensions_.GetByID(id); |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 139 | if (extension && extension->HasAPIPermission(permission) && |
| 140 | process_map_.Contains(id, process_id)) { |
| 141 | extensions->Insert(extension); |
| 142 | } |
| 143 | return; |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 146 | ExtensionSet::const_iterator i = extensions_.begin(); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 147 | for (; i != extensions_.end(); ++i) { |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 148 | if ((*i)->web_extent().MatchesSecurityOrigin(origin) && |
| 149 | process_map_.Contains((*i)->id(), process_id) && |
| 150 | (*i)->HasAPIPermission(permission)) { |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 151 | extensions->Insert(*i); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 152 | } |
| 153 | } |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool ExtensionInfoMap::SecurityOriginHasAPIPermission( |
| 157 | const GURL& origin, int process_id, |
| 158 | extensions::APIPermission::ID permission) const { |
| 159 | ExtensionSet extensions; |
| 160 | GetExtensionsWithAPIPermissionForSecurityOrigin( |
| 161 | origin, process_id, permission, &extensions); |
| 162 | return !extensions.is_empty(); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 163 | } |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 164 | |
| 165 | ExtensionsQuotaService* ExtensionInfoMap::GetQuotaService() { |
| 166 | CheckOnValidThread(); |
[email protected] | 3eeddd89 | 2013-04-17 17:00:11 | [diff] [blame] | 167 | if (!quota_service_) |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 168 | quota_service_.reset(new ExtensionsQuotaService()); |
| 169 | return quota_service_.get(); |
| 170 | } |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 171 | |
[email protected] | 1deace2 | 2013-05-22 06:14:46 | [diff] [blame] | 172 | void ExtensionInfoMap::SetSigninProcess(int process_id) { |
| 173 | signin_process_id_ = process_id; |
| 174 | } |
| 175 | |
| 176 | bool ExtensionInfoMap::IsSigninProcess(int process_id) const { |
| 177 | return process_id == signin_process_id_; |
| 178 | } |
| 179 | |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 180 | ExtensionInfoMap::~ExtensionInfoMap() { |
[email protected] | 3eeddd89 | 2013-04-17 17:00:11 | [diff] [blame] | 181 | if (quota_service_) { |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 182 | BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
| 183 | quota_service_.release()); |
| 184 | } |
| 185 | } |