blob: 42b85c550341b9d60c0e9bcc7f8f9636f7131290 [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]4361c7c2010-09-30 21:57:5313
[email protected]631bb742011-11-02 11:29:3914using content::BrowserThread;
[email protected]38427a12013-11-09 17:34:2015
16namespace extensions {
[email protected]631bb742011-11-02 11:29:3917
[email protected]4361c7c2010-09-30 21:57:5318namespace {
19
[email protected]54ee8192014-03-29 17:37:2420void CheckOnValidThread() { DCHECK_CURRENTLY_ON(BrowserThread::IO); }
[email protected]4361c7c2010-09-30 21:57:5321
22} // namespace
23
[email protected]38427a12013-11-09 17:34:2024struct InfoMap::ExtraData {
[email protected]c357acb42011-06-09 20:52:4225 // 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]9afacd22013-11-13 20:23:3131 // True if the user has disabled notifications for this extension manually.
32 bool notifications_disabled;
33
[email protected]c357acb42011-06-09 20:52:4234 ExtraData();
35 ~ExtraData();
36};
37
[email protected]38427a12013-11-09 17:34:2038InfoMap::ExtraData::ExtraData() : incognito_enabled(false) {}
[email protected]c357acb42011-06-09 20:52:4239
[email protected]38427a12013-11-09 17:34:2040InfoMap::ExtraData::~ExtraData() {}
[email protected]c357acb42011-06-09 20:52:4241
[email protected]38427a12013-11-09 17:34:2042InfoMap::InfoMap() : signin_process_id_(-1) {}
[email protected]4361c7c2010-09-30 21:57:5343
[email protected]38427a12013-11-09 17:34:2044const ProcessMap& InfoMap::process_map() const { return process_map_; }
[email protected]6f371442011-11-09 06:45:4645
[email protected]896c6d52014-01-28 21:40:2146const ProcessMap& InfoMap::worker_process_map() const {
47 return worker_process_map_;
48}
49
[email protected]38427a12013-11-09 17:34:2050void InfoMap::AddExtension(const Extension* extension,
51 base::Time install_time,
[email protected]9afacd22013-11-13 20:23:3152 bool incognito_enabled,
53 bool notifications_disabled) {
[email protected]4361c7c2010-09-30 21:57:5354 CheckOnValidThread();
[email protected]be0a2cfd2011-06-02 21:36:4255 extensions_.Insert(extension);
56 disabled_extensions_.Remove(extension->id());
[email protected]c357acb42011-06-09 20:52:4257
58 extra_data_[extension->id()].install_time = install_time;
59 extra_data_[extension->id()].incognito_enabled = incognito_enabled;
[email protected]9afacd22013-11-13 20:23:3160 extra_data_[extension->id()].notifications_disabled = notifications_disabled;
[email protected]4361c7c2010-09-30 21:57:5361}
62
[email protected]38427a12013-11-09 17:34:2063void InfoMap::RemoveExtension(const std::string& extension_id,
64 const UnloadedExtensionInfo::Reason reason) {
[email protected]4361c7c2010-09-30 21:57:5365 CheckOnValidThread();
[email protected]c357acb42011-06-09 20:52:4266 const Extension* extension = extensions_.GetByID(extension_id);
67 extra_data_.erase(extension_id); // we don't care about disabled extra data
[email protected]b0af4792013-10-23 09:12:1368 bool was_uninstalled = (reason != UnloadedExtensionInfo::REASON_DISABLE &&
69 reason != UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]be0a2cfd2011-06-02 21:36:4270 if (extension) {
[email protected]b3f7fe22011-11-11 19:27:5671 if (!was_uninstalled)
[email protected]be0a2cfd2011-06-02 21:36:4272 disabled_extensions_.Insert(extension);
[email protected]c357acb42011-06-09 20:52:4273 extensions_.Remove(extension_id);
[email protected]b3f7fe22011-11-11 19:27:5674 } else if (was_uninstalled) {
[email protected]dd163fb02011-05-04 22:22:1775 // If the extension was uninstalled, make sure it's removed from the map of
76 // disabled extensions.
[email protected]c357acb42011-06-09 20:52:4277 disabled_extensions_.Remove(extension_id);
[email protected]4361c7c2010-09-30 21:57:5378 } 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]c357acb42011-06-09 20:52:4283 NOTREACHED() << extension_id;
[email protected]4361c7c2010-09-30 21:57:5384 }
85}
[email protected]c357acb42011-06-09 20:52:4286
[email protected]38427a12013-11-09 17:34:2087base::Time InfoMap::GetInstallTime(const std::string& extension_id) const {
[email protected]c357acb42011-06-09 20:52:4288 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]38427a12013-11-09 17:34:2094bool InfoMap::IsIncognitoEnabled(const std::string& extension_id) const {
[email protected]98b6d942013-11-10 00:34:0795 // Keep in sync with duplicate in extensions/browser/process_manager.cc.
[email protected]c357acb42011-06-09 20:52:4296 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]38427a12013-11-09 17:34:20102bool InfoMap::CanCrossIncognito(const Extension* extension) const {
[email protected]c357acb42011-06-09 20:52:42103 // This is duplicated from ExtensionService :(.
104 return IsIncognitoEnabled(extension->id()) &&
[email protected]38427a12013-11-09 17:34:20105 !IncognitoInfo::IsSplitMode(extension);
[email protected]c357acb42011-06-09 20:52:42106}
[email protected]8add5412011-10-01 21:02:14107
[email protected]38427a12013-11-09 17:34:20108void InfoMap::RegisterExtensionProcess(const std::string& extension_id,
109 int process_id,
110 int site_instance_id) {
[email protected]6bc04fd82011-12-04 02:29:35111 if (!process_map_.Insert(extension_id, process_id, site_instance_id)) {
[email protected]6f371442011-11-09 06:45:46112 NOTREACHED() << "Duplicate extension process registration for: "
113 << extension_id << "," << process_id << ".";
114 }
[email protected]8add5412011-10-01 21:02:14115}
116
[email protected]38427a12013-11-09 17:34:20117void InfoMap::UnregisterExtensionProcess(const std::string& extension_id,
118 int process_id,
119 int site_instance_id) {
[email protected]6bc04fd82011-12-04 02:29:35120 if (!process_map_.Remove(extension_id, process_id, site_instance_id)) {
[email protected]6f371442011-11-09 06:45:46121 NOTREACHED() << "Unknown extension process registration for: "
122 << extension_id << "," << process_id << ".";
123 }
[email protected]8add5412011-10-01 21:02:14124}
125
[email protected]38427a12013-11-09 17:34:20126void InfoMap::UnregisterAllExtensionsInProcess(int process_id) {
[email protected]6bc04fd82011-12-04 02:29:35127 process_map_.RemoveAllFromProcess(process_id);
[email protected]8add5412011-10-01 21:02:14128}
[email protected]34eec7ffe32011-11-02 23:49:02129
[email protected]896c6d52014-01-28 21:40:21130void 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
139void InfoMap::UnregisterExtensionWorkerProcess(int process_id) {
140 worker_process_map_.RemoveAllFromProcess(process_id);
141}
142
[email protected]38427a12013-11-09 17:34:20143void InfoMap::GetExtensionsWithAPIPermissionForSecurityOrigin(
[email protected]c7cd5942013-04-30 03:31:01144 const GURL& origin,
145 int process_id,
[email protected]38427a12013-11-09 17:34:20146 APIPermission::ID permission,
[email protected]c7cd5942013-04-30 03:31:01147 ExtensionSet* extensions) const {
148 DCHECK(extensions);
149
[email protected]38427a12013-11-09 17:34:20150 if (origin.SchemeIs(kExtensionScheme)) {
[email protected]34eec7ffe32011-11-02 23:49:02151 const std::string& id = origin.host();
[email protected]af73c252012-07-27 00:16:39152 const Extension* extension = extensions_.GetByID(id);
[email protected]c7cd5942013-04-30 03:31:01153 if (extension && extension->HasAPIPermission(permission) &&
154 process_map_.Contains(id, process_id)) {
155 extensions->Insert(extension);
156 }
157 return;
[email protected]34eec7ffe32011-11-02 23:49:02158 }
159
[email protected]84df8332011-12-06 18:22:46160 ExtensionSet::const_iterator i = extensions_.begin();
[email protected]34eec7ffe32011-11-02 23:49:02161 for (; i != extensions_.end(); ++i) {
[email protected]84df8332011-12-06 18:22:46162 if ((*i)->web_extent().MatchesSecurityOrigin(origin) &&
163 process_map_.Contains((*i)->id(), process_id) &&
164 (*i)->HasAPIPermission(permission)) {
[email protected]c7cd5942013-04-30 03:31:01165 extensions->Insert(*i);
[email protected]34eec7ffe32011-11-02 23:49:02166 }
167 }
[email protected]c7cd5942013-04-30 03:31:01168}
169
[email protected]38427a12013-11-09 17:34:20170bool InfoMap::SecurityOriginHasAPIPermission(const GURL& origin,
171 int process_id,
172 APIPermission::ID permission)
173 const {
[email protected]c7cd5942013-04-30 03:31:01174 ExtensionSet extensions;
175 GetExtensionsWithAPIPermissionForSecurityOrigin(
176 origin, process_id, permission, &extensions);
177 return !extensions.is_empty();
[email protected]34eec7ffe32011-11-02 23:49:02178}
[email protected]36296912012-03-20 11:08:49179
[email protected]38427a12013-11-09 17:34:20180QuotaService* InfoMap::GetQuotaService() {
[email protected]36296912012-03-20 11:08:49181 CheckOnValidThread();
[email protected]3eeddd892013-04-17 17:00:11182 if (!quota_service_)
[email protected]38427a12013-11-09 17:34:20183 quota_service_.reset(new QuotaService());
[email protected]36296912012-03-20 11:08:49184 return quota_service_.get();
185}
[email protected]5f2a4752012-04-27 22:18:58186
[email protected]38427a12013-11-09 17:34:20187void InfoMap::SetSigninProcess(int process_id) {
[email protected]1deace22013-05-22 06:14:46188 signin_process_id_ = process_id;
189}
190
[email protected]38427a12013-11-09 17:34:20191bool InfoMap::IsSigninProcess(int process_id) const {
[email protected]1deace22013-05-22 06:14:46192 return process_id == signin_process_id_;
193}
194
[email protected]9afacd22013-11-13 20:23:31195void 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
203bool 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]fd3df7782014-05-08 23:54:27211void InfoMap::SetContentVerifier(ContentVerifier* verifier) {
212 content_verifier_ = verifier;
213}
214
[email protected]38427a12013-11-09 17:34:20215InfoMap::~InfoMap() {
[email protected]3eeddd892013-04-17 17:00:11216 if (quota_service_) {
[email protected]38427a12013-11-09 17:34:20217 BrowserThread::DeleteSoon(
218 BrowserThread::IO, FROM_HERE, quota_service_.release());
[email protected]5f2a4752012-04-27 22:18:58219 }
220}
[email protected]38427a12013-11-09 17:34:20221
222} // namespace extensions