blob: 1042f638127640b236ca738c08e807911c04f76b [file] [log] [blame]
[email protected]38427a12013-11-09 17:34:201// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]4361c7c2010-09-30 21:57:532// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]38427a12013-11-09 17:34:205#include "extensions/browser/info_map.h"
[email protected]4361c7c2010-09-30 21:57:536
[email protected]c38831a12011-10-28 12:44:497#include "content/public/browser/browser_thread.h"
[email protected]fd3df7782014-05-08 23:54:278#include "extensions/browser/content_verifier.h"
[email protected]885c0e92012-11-13 20:27:429#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:4110#include "extensions/common/extension.h"
[email protected]289c44b2013-12-17 03:26:5711#include "extensions/common/extension_set.h"
[email protected]1f7de252013-11-06 22:02:0012#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]076ebeda2014-06-06 21:47:2613#include "extensions/common/permissions/permissions_data.h"
[email protected]4361c7c2010-09-30 21:57:5314
[email protected]631bb742011-11-02 11:29:3915using content::BrowserThread;
[email protected]38427a12013-11-09 17:34:2016
17namespace extensions {
[email protected]631bb742011-11-02 11:29:3918
[email protected]4361c7c2010-09-30 21:57:5319namespace {
20
[email protected]54ee8192014-03-29 17:37:2421void CheckOnValidThread() { DCHECK_CURRENTLY_ON(BrowserThread::IO); }
[email protected]4361c7c2010-09-30 21:57:5322
23} // namespace
24
[email protected]38427a12013-11-09 17:34:2025struct InfoMap::ExtraData {
[email protected]c357acb42011-06-09 20:52:4226 // 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]9afacd22013-11-13 20:23:3132 // True if the user has disabled notifications for this extension manually.
33 bool notifications_disabled;
34
[email protected]c357acb42011-06-09 20:52:4235 ExtraData();
36 ~ExtraData();
37};
38
[email protected]38427a12013-11-09 17:34:2039InfoMap::ExtraData::ExtraData() : incognito_enabled(false) {}
[email protected]c357acb42011-06-09 20:52:4240
[email protected]38427a12013-11-09 17:34:2041InfoMap::ExtraData::~ExtraData() {}
[email protected]c357acb42011-06-09 20:52:4242
[email protected]38427a12013-11-09 17:34:2043InfoMap::InfoMap() : signin_process_id_(-1) {}
[email protected]4361c7c2010-09-30 21:57:5344
[email protected]38427a12013-11-09 17:34:2045const ProcessMap& InfoMap::process_map() const { return process_map_; }
[email protected]6f371442011-11-09 06:45:4646
[email protected]896c6d52014-01-28 21:40:2147const ProcessMap& InfoMap::worker_process_map() const {
48 return worker_process_map_;
49}
50
[email protected]38427a12013-11-09 17:34:2051void InfoMap::AddExtension(const Extension* extension,
52 base::Time install_time,
[email protected]9afacd22013-11-13 20:23:3153 bool incognito_enabled,
54 bool notifications_disabled) {
[email protected]4361c7c2010-09-30 21:57:5355 CheckOnValidThread();
[email protected]be0a2cfd2011-06-02 21:36:4256 extensions_.Insert(extension);
57 disabled_extensions_.Remove(extension->id());
[email protected]c357acb42011-06-09 20:52:4258
59 extra_data_[extension->id()].install_time = install_time;
60 extra_data_[extension->id()].incognito_enabled = incognito_enabled;
[email protected]9afacd22013-11-13 20:23:3161 extra_data_[extension->id()].notifications_disabled = notifications_disabled;
[email protected]4361c7c2010-09-30 21:57:5362}
63
[email protected]38427a12013-11-09 17:34:2064void InfoMap::RemoveExtension(const std::string& extension_id,
65 const UnloadedExtensionInfo::Reason reason) {
[email protected]4361c7c2010-09-30 21:57:5366 CheckOnValidThread();
[email protected]c357acb42011-06-09 20:52:4267 const Extension* extension = extensions_.GetByID(extension_id);
68 extra_data_.erase(extension_id); // we don't care about disabled extra data
[email protected]b0af4792013-10-23 09:12:1369 bool was_uninstalled = (reason != UnloadedExtensionInfo::REASON_DISABLE &&
70 reason != UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]be0a2cfd2011-06-02 21:36:4271 if (extension) {
[email protected]b3f7fe22011-11-11 19:27:5672 if (!was_uninstalled)
[email protected]be0a2cfd2011-06-02 21:36:4273 disabled_extensions_.Insert(extension);
[email protected]c357acb42011-06-09 20:52:4274 extensions_.Remove(extension_id);
[email protected]b3f7fe22011-11-11 19:27:5675 } else if (was_uninstalled) {
[email protected]dd163fb02011-05-04 22:22:1776 // If the extension was uninstalled, make sure it's removed from the map of
77 // disabled extensions.
[email protected]c357acb42011-06-09 20:52:4278 disabled_extensions_.Remove(extension_id);
[email protected]4361c7c2010-09-30 21:57:5379 } 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]c357acb42011-06-09 20:52:4284 NOTREACHED() << extension_id;
[email protected]4361c7c2010-09-30 21:57:5385 }
86}
[email protected]c357acb42011-06-09 20:52:4287
[email protected]38427a12013-11-09 17:34:2088base::Time InfoMap::GetInstallTime(const std::string& extension_id) const {
[email protected]c357acb42011-06-09 20:52:4289 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]38427a12013-11-09 17:34:2095bool InfoMap::IsIncognitoEnabled(const std::string& extension_id) const {
[email protected]98b6d942013-11-10 00:34:0796 // Keep in sync with duplicate in extensions/browser/process_manager.cc.
[email protected]c357acb42011-06-09 20:52:4297 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]38427a12013-11-09 17:34:20103bool InfoMap::CanCrossIncognito(const Extension* extension) const {
[email protected]c357acb42011-06-09 20:52:42104 // This is duplicated from ExtensionService :(.
105 return IsIncognitoEnabled(extension->id()) &&
[email protected]38427a12013-11-09 17:34:20106 !IncognitoInfo::IsSplitMode(extension);
[email protected]c357acb42011-06-09 20:52:42107}
[email protected]8add5412011-10-01 21:02:14108
[email protected]38427a12013-11-09 17:34:20109void InfoMap::RegisterExtensionProcess(const std::string& extension_id,
110 int process_id,
111 int site_instance_id) {
[email protected]6bc04fd82011-12-04 02:29:35112 if (!process_map_.Insert(extension_id, process_id, site_instance_id)) {
[email protected]6f371442011-11-09 06:45:46113 NOTREACHED() << "Duplicate extension process registration for: "
114 << extension_id << "," << process_id << ".";
115 }
[email protected]8add5412011-10-01 21:02:14116}
117
[email protected]38427a12013-11-09 17:34:20118void InfoMap::UnregisterExtensionProcess(const std::string& extension_id,
119 int process_id,
120 int site_instance_id) {
[email protected]6bc04fd82011-12-04 02:29:35121 if (!process_map_.Remove(extension_id, process_id, site_instance_id)) {
[email protected]6f371442011-11-09 06:45:46122 NOTREACHED() << "Unknown extension process registration for: "
123 << extension_id << "," << process_id << ".";
124 }
[email protected]8add5412011-10-01 21:02:14125}
126
[email protected]38427a12013-11-09 17:34:20127void InfoMap::UnregisterAllExtensionsInProcess(int process_id) {
[email protected]6bc04fd82011-12-04 02:29:35128 process_map_.RemoveAllFromProcess(process_id);
[email protected]8add5412011-10-01 21:02:14129}
[email protected]34eec7ffe32011-11-02 23:49:02130
[email protected]896c6d52014-01-28 21:40:21131void 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
140void InfoMap::UnregisterExtensionWorkerProcess(int process_id) {
141 worker_process_map_.RemoveAllFromProcess(process_id);
142}
143
[email protected]38427a12013-11-09 17:34:20144void InfoMap::GetExtensionsWithAPIPermissionForSecurityOrigin(
[email protected]c7cd5942013-04-30 03:31:01145 const GURL& origin,
146 int process_id,
[email protected]38427a12013-11-09 17:34:20147 APIPermission::ID permission,
[email protected]c7cd5942013-04-30 03:31:01148 ExtensionSet* extensions) const {
149 DCHECK(extensions);
150
[email protected]38427a12013-11-09 17:34:20151 if (origin.SchemeIs(kExtensionScheme)) {
[email protected]34eec7ffe32011-11-02 23:49:02152 const std::string& id = origin.host();
[email protected]af73c252012-07-27 00:16:39153 const Extension* extension = extensions_.GetByID(id);
[email protected]076ebeda2014-06-06 21:47:26154 if (extension &&
155 extension->permissions_data()->HasAPIPermission(permission) &&
[email protected]c7cd5942013-04-30 03:31:01156 process_map_.Contains(id, process_id)) {
157 extensions->Insert(extension);
158 }
159 return;
[email protected]34eec7ffe32011-11-02 23:49:02160 }
161
[email protected]84df8332011-12-06 18:22:46162 ExtensionSet::const_iterator i = extensions_.begin();
[email protected]34eec7ffe32011-11-02 23:49:02163 for (; i != extensions_.end(); ++i) {
[email protected]84df8332011-12-06 18:22:46164 if ((*i)->web_extent().MatchesSecurityOrigin(origin) &&
165 process_map_.Contains((*i)->id(), process_id) &&
[email protected]076ebeda2014-06-06 21:47:26166 (*i)->permissions_data()->HasAPIPermission(permission)) {
[email protected]c7cd5942013-04-30 03:31:01167 extensions->Insert(*i);
[email protected]34eec7ffe32011-11-02 23:49:02168 }
169 }
[email protected]c7cd5942013-04-30 03:31:01170}
171
[email protected]38427a12013-11-09 17:34:20172bool InfoMap::SecurityOriginHasAPIPermission(const GURL& origin,
173 int process_id,
174 APIPermission::ID permission)
175 const {
[email protected]c7cd5942013-04-30 03:31:01176 ExtensionSet extensions;
177 GetExtensionsWithAPIPermissionForSecurityOrigin(
178 origin, process_id, permission, &extensions);
179 return !extensions.is_empty();
[email protected]34eec7ffe32011-11-02 23:49:02180}
[email protected]36296912012-03-20 11:08:49181
[email protected]38427a12013-11-09 17:34:20182QuotaService* InfoMap::GetQuotaService() {
[email protected]36296912012-03-20 11:08:49183 CheckOnValidThread();
[email protected]3eeddd892013-04-17 17:00:11184 if (!quota_service_)
[email protected]38427a12013-11-09 17:34:20185 quota_service_.reset(new QuotaService());
[email protected]36296912012-03-20 11:08:49186 return quota_service_.get();
187}
[email protected]5f2a4752012-04-27 22:18:58188
[email protected]38427a12013-11-09 17:34:20189void InfoMap::SetSigninProcess(int process_id) {
[email protected]1deace22013-05-22 06:14:46190 signin_process_id_ = process_id;
191}
192
[email protected]38427a12013-11-09 17:34:20193bool InfoMap::IsSigninProcess(int process_id) const {
[email protected]1deace22013-05-22 06:14:46194 return process_id == signin_process_id_;
195}
196
[email protected]9afacd22013-11-13 20:23:31197void 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
205bool 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]fd3df7782014-05-08 23:54:27213void InfoMap::SetContentVerifier(ContentVerifier* verifier) {
214 content_verifier_ = verifier;
215}
216
[email protected]38427a12013-11-09 17:34:20217InfoMap::~InfoMap() {
[email protected]3eeddd892013-04-17 17:00:11218 if (quota_service_) {
[email protected]38427a12013-11-09 17:34:20219 BrowserThread::DeleteSoon(
220 BrowserThread::IO, FROM_HERE, quota_service_.release());
[email protected]5f2a4752012-04-27 22:18:58221 }
222}
[email protected]38427a12013-11-09 17:34:20223
224} // namespace extensions