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