[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 1 | // Copyright 2013 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 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 5 | #include "extensions/browser/info_map.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 6 | |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 7 | #include "content/public/browser/browser_thread.h" |
[email protected] | fd3df778 | 2014-05-08 23:54:27 | [diff] [blame] | 8 | #include "extensions/browser/content_verifier.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 9 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 10 | #include "extensions/common/extension.h" |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 11 | #include "extensions/common/extension_set.h" |
[email protected] | 1f7de25 | 2013-11-06 22:02:00 | [diff] [blame] | 12 | #include "extensions/common/manifest_handlers/incognito_info.h" |
[email protected] | 076ebeda | 2014-06-06 21:47:26 | [diff] [blame^] | 13 | #include "extensions/common/permissions/permissions_data.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 14 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 15 | using content::BrowserThread; |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 16 | |
| 17 | namespace extensions { |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 18 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 19 | namespace { |
| 20 | |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 21 | void CheckOnValidThread() { DCHECK_CURRENTLY_ON(BrowserThread::IO); } |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 22 | |
| 23 | } // namespace |
| 24 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 25 | struct InfoMap::ExtraData { |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 26 | // When the extension was installed. |
| 27 | base::Time install_time; |
| 28 | |
| 29 | // True if the user has allowed this extension to run in incognito mode. |
| 30 | bool incognito_enabled; |
| 31 | |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 32 | // True if the user has disabled notifications for this extension manually. |
| 33 | bool notifications_disabled; |
| 34 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 35 | ExtraData(); |
| 36 | ~ExtraData(); |
| 37 | }; |
| 38 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 39 | InfoMap::ExtraData::ExtraData() : incognito_enabled(false) {} |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 40 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 41 | InfoMap::ExtraData::~ExtraData() {} |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 42 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 43 | InfoMap::InfoMap() : signin_process_id_(-1) {} |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 44 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 45 | const ProcessMap& InfoMap::process_map() const { return process_map_; } |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 46 | |
[email protected] | 896c6d5 | 2014-01-28 21:40:21 | [diff] [blame] | 47 | const ProcessMap& InfoMap::worker_process_map() const { |
| 48 | return worker_process_map_; |
| 49 | } |
| 50 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 51 | void InfoMap::AddExtension(const Extension* extension, |
| 52 | base::Time install_time, |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 53 | bool incognito_enabled, |
| 54 | bool notifications_disabled) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 55 | CheckOnValidThread(); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 56 | extensions_.Insert(extension); |
| 57 | disabled_extensions_.Remove(extension->id()); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 58 | |
| 59 | extra_data_[extension->id()].install_time = install_time; |
| 60 | extra_data_[extension->id()].incognito_enabled = incognito_enabled; |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 61 | extra_data_[extension->id()].notifications_disabled = notifications_disabled; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 64 | void InfoMap::RemoveExtension(const std::string& extension_id, |
| 65 | const UnloadedExtensionInfo::Reason reason) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 66 | CheckOnValidThread(); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 67 | const Extension* extension = extensions_.GetByID(extension_id); |
| 68 | extra_data_.erase(extension_id); // we don't care about disabled extra data |
[email protected] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 69 | bool was_uninstalled = (reason != UnloadedExtensionInfo::REASON_DISABLE && |
| 70 | reason != UnloadedExtensionInfo::REASON_TERMINATE); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 71 | if (extension) { |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 72 | if (!was_uninstalled) |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 73 | disabled_extensions_.Insert(extension); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 74 | extensions_.Remove(extension_id); |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 75 | } else if (was_uninstalled) { |
[email protected] | dd163fb0 | 2011-05-04 22:22:17 | [diff] [blame] | 76 | // If the extension was uninstalled, make sure it's removed from the map of |
| 77 | // disabled extensions. |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 78 | disabled_extensions_.Remove(extension_id); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 79 | } else { |
| 80 | // NOTE: This can currently happen if we receive multiple unload |
| 81 | // notifications, e.g. setting incognito-enabled state for a |
| 82 | // disabled extension (e.g., via sync). See |
| 83 | // http://code.google.com/p/chromium/issues/detail?id=50582 . |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 84 | NOTREACHED() << extension_id; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 85 | } |
| 86 | } |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 87 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 88 | base::Time InfoMap::GetInstallTime(const std::string& extension_id) const { |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 89 | ExtraDataMap::const_iterator iter = extra_data_.find(extension_id); |
| 90 | if (iter != extra_data_.end()) |
| 91 | return iter->second.install_time; |
| 92 | return base::Time(); |
| 93 | } |
| 94 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 95 | bool InfoMap::IsIncognitoEnabled(const std::string& extension_id) const { |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 96 | // Keep in sync with duplicate in extensions/browser/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 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 103 | bool InfoMap::CanCrossIncognito(const Extension* extension) const { |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 104 | // This is duplicated from ExtensionService :(. |
| 105 | return IsIncognitoEnabled(extension->id()) && |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 106 | !IncognitoInfo::IsSplitMode(extension); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 107 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 108 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 109 | void InfoMap::RegisterExtensionProcess(const std::string& extension_id, |
| 110 | int process_id, |
| 111 | int site_instance_id) { |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 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] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 118 | void InfoMap::UnregisterExtensionProcess(const std::string& extension_id, |
| 119 | int process_id, |
| 120 | int site_instance_id) { |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 121 | if (!process_map_.Remove(extension_id, process_id, site_instance_id)) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 122 | NOTREACHED() << "Unknown extension process registration for: " |
| 123 | << extension_id << "," << process_id << "."; |
| 124 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 127 | void InfoMap::UnregisterAllExtensionsInProcess(int process_id) { |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 128 | process_map_.RemoveAllFromProcess(process_id); |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 129 | } |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 130 | |
[email protected] | 896c6d5 | 2014-01-28 21:40:21 | [diff] [blame] | 131 | void InfoMap::RegisterExtensionWorkerProcess(const std::string& extension_id, |
| 132 | int process_id, |
| 133 | int site_instance_id) { |
| 134 | if (!worker_process_map_.Insert(extension_id, process_id, site_instance_id)) { |
| 135 | NOTREACHED() << "Duplicate extension worker process registration for: " |
| 136 | << extension_id << "," << process_id << "."; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void InfoMap::UnregisterExtensionWorkerProcess(int process_id) { |
| 141 | worker_process_map_.RemoveAllFromProcess(process_id); |
| 142 | } |
| 143 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 144 | void InfoMap::GetExtensionsWithAPIPermissionForSecurityOrigin( |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 145 | const GURL& origin, |
| 146 | int process_id, |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 147 | APIPermission::ID permission, |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 148 | ExtensionSet* extensions) const { |
| 149 | DCHECK(extensions); |
| 150 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 151 | if (origin.SchemeIs(kExtensionScheme)) { |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 152 | const std::string& id = origin.host(); |
[email protected] | af73c25 | 2012-07-27 00:16:39 | [diff] [blame] | 153 | const Extension* extension = extensions_.GetByID(id); |
[email protected] | 076ebeda | 2014-06-06 21:47:26 | [diff] [blame^] | 154 | if (extension && |
| 155 | extension->permissions_data()->HasAPIPermission(permission) && |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 156 | process_map_.Contains(id, process_id)) { |
| 157 | extensions->Insert(extension); |
| 158 | } |
| 159 | return; |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 162 | ExtensionSet::const_iterator i = extensions_.begin(); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 163 | for (; i != extensions_.end(); ++i) { |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 164 | if ((*i)->web_extent().MatchesSecurityOrigin(origin) && |
| 165 | process_map_.Contains((*i)->id(), process_id) && |
[email protected] | 076ebeda | 2014-06-06 21:47:26 | [diff] [blame^] | 166 | (*i)->permissions_data()->HasAPIPermission(permission)) { |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 167 | extensions->Insert(*i); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 168 | } |
| 169 | } |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 172 | bool InfoMap::SecurityOriginHasAPIPermission(const GURL& origin, |
| 173 | int process_id, |
| 174 | APIPermission::ID permission) |
| 175 | const { |
[email protected] | c7cd594 | 2013-04-30 03:31:01 | [diff] [blame] | 176 | ExtensionSet extensions; |
| 177 | GetExtensionsWithAPIPermissionForSecurityOrigin( |
| 178 | origin, process_id, permission, &extensions); |
| 179 | return !extensions.is_empty(); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 180 | } |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 181 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 182 | QuotaService* InfoMap::GetQuotaService() { |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 183 | CheckOnValidThread(); |
[email protected] | 3eeddd89 | 2013-04-17 17:00:11 | [diff] [blame] | 184 | if (!quota_service_) |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 185 | quota_service_.reset(new QuotaService()); |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame] | 186 | return quota_service_.get(); |
| 187 | } |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 188 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 189 | void InfoMap::SetSigninProcess(int process_id) { |
[email protected] | 1deace2 | 2013-05-22 06:14:46 | [diff] [blame] | 190 | signin_process_id_ = process_id; |
| 191 | } |
| 192 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 193 | bool InfoMap::IsSigninProcess(int process_id) const { |
[email protected] | 1deace2 | 2013-05-22 06:14:46 | [diff] [blame] | 194 | return process_id == signin_process_id_; |
| 195 | } |
| 196 | |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 197 | void InfoMap::SetNotificationsDisabled( |
| 198 | const std::string& extension_id, |
| 199 | bool notifications_disabled) { |
| 200 | ExtraDataMap::iterator iter = extra_data_.find(extension_id); |
| 201 | if (iter != extra_data_.end()) |
| 202 | iter->second.notifications_disabled = notifications_disabled; |
| 203 | } |
| 204 | |
| 205 | bool InfoMap::AreNotificationsDisabled( |
| 206 | const std::string& extension_id) const { |
| 207 | ExtraDataMap::const_iterator iter = extra_data_.find(extension_id); |
| 208 | if (iter != extra_data_.end()) |
| 209 | return iter->second.notifications_disabled; |
| 210 | return false; |
| 211 | } |
| 212 | |
[email protected] | fd3df778 | 2014-05-08 23:54:27 | [diff] [blame] | 213 | void InfoMap::SetContentVerifier(ContentVerifier* verifier) { |
| 214 | content_verifier_ = verifier; |
| 215 | } |
| 216 | |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 217 | InfoMap::~InfoMap() { |
[email protected] | 3eeddd89 | 2013-04-17 17:00:11 | [diff] [blame] | 218 | if (quota_service_) { |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 219 | BrowserThread::DeleteSoon( |
| 220 | BrowserThread::IO, FROM_HERE, quota_service_.release()); |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 221 | } |
| 222 | } |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 223 | |
| 224 | } // namespace extensions |