blob: f790868f6475ae2269653596a43d2984ca23cac4 [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]6f229e82010-11-02 17:47:267#include "chrome/common/extensions/extension.h"
[email protected]34eec7ffe32011-11-02 23:49:028#include "chrome/common/extensions/extension_set.h"
[email protected]c38831a12011-10-28 12:44:499#include "content/public/browser/browser_thread.h"
[email protected]885c0e92012-11-13 20:27:4210#include "extensions/common/constants.h"
[email protected]1f7de252013-11-06 22:02:0011#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]4361c7c2010-09-30 21:57:5312
[email protected]631bb742011-11-02 11:29:3913using content::BrowserThread;
[email protected]38427a12013-11-09 17:34:2014
15namespace extensions {
[email protected]631bb742011-11-02 11:29:3916
[email protected]4361c7c2010-09-30 21:57:5317namespace {
18
[email protected]2cce7ae2011-10-23 15:54:3619void CheckOnValidThread() {
[email protected]ca4b5fa32010-10-09 12:42:1820 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]4361c7c2010-09-30 21:57:5321}
22
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
32 ExtraData();
33 ~ExtraData();
34};
35
[email protected]38427a12013-11-09 17:34:2036InfoMap::ExtraData::ExtraData() : incognito_enabled(false) {}
[email protected]c357acb42011-06-09 20:52:4237
[email protected]38427a12013-11-09 17:34:2038InfoMap::ExtraData::~ExtraData() {}
[email protected]c357acb42011-06-09 20:52:4239
[email protected]38427a12013-11-09 17:34:2040InfoMap::InfoMap() : signin_process_id_(-1) {}
[email protected]4361c7c2010-09-30 21:57:5341
[email protected]38427a12013-11-09 17:34:2042const ProcessMap& InfoMap::process_map() const { return process_map_; }
[email protected]6f371442011-11-09 06:45:4643
[email protected]38427a12013-11-09 17:34:2044void InfoMap::AddExtension(const Extension* extension,
45 base::Time install_time,
46 bool incognito_enabled) {
[email protected]4361c7c2010-09-30 21:57:5347 CheckOnValidThread();
[email protected]be0a2cfd2011-06-02 21:36:4248 extensions_.Insert(extension);
49 disabled_extensions_.Remove(extension->id());
[email protected]c357acb42011-06-09 20:52:4250
51 extra_data_[extension->id()].install_time = install_time;
52 extra_data_[extension->id()].incognito_enabled = incognito_enabled;
[email protected]4361c7c2010-09-30 21:57:5353}
54
[email protected]38427a12013-11-09 17:34:2055void InfoMap::RemoveExtension(const std::string& extension_id,
56 const UnloadedExtensionInfo::Reason reason) {
[email protected]4361c7c2010-09-30 21:57:5357 CheckOnValidThread();
[email protected]c357acb42011-06-09 20:52:4258 const Extension* extension = extensions_.GetByID(extension_id);
59 extra_data_.erase(extension_id); // we don't care about disabled extra data
[email protected]b0af4792013-10-23 09:12:1360 bool was_uninstalled = (reason != UnloadedExtensionInfo::REASON_DISABLE &&
61 reason != UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]be0a2cfd2011-06-02 21:36:4262 if (extension) {
[email protected]b3f7fe22011-11-11 19:27:5663 if (!was_uninstalled)
[email protected]be0a2cfd2011-06-02 21:36:4264 disabled_extensions_.Insert(extension);
[email protected]c357acb42011-06-09 20:52:4265 extensions_.Remove(extension_id);
[email protected]b3f7fe22011-11-11 19:27:5666 } else if (was_uninstalled) {
[email protected]dd163fb02011-05-04 22:22:1767 // If the extension was uninstalled, make sure it's removed from the map of
68 // disabled extensions.
[email protected]c357acb42011-06-09 20:52:4269 disabled_extensions_.Remove(extension_id);
[email protected]4361c7c2010-09-30 21:57:5370 } else {
71 // NOTE: This can currently happen if we receive multiple unload
72 // notifications, e.g. setting incognito-enabled state for a
73 // disabled extension (e.g., via sync). See
74 // http://code.google.com/p/chromium/issues/detail?id=50582 .
[email protected]c357acb42011-06-09 20:52:4275 NOTREACHED() << extension_id;
[email protected]4361c7c2010-09-30 21:57:5376 }
77}
[email protected]c357acb42011-06-09 20:52:4278
[email protected]38427a12013-11-09 17:34:2079base::Time InfoMap::GetInstallTime(const std::string& extension_id) const {
[email protected]c357acb42011-06-09 20:52:4280 ExtraDataMap::const_iterator iter = extra_data_.find(extension_id);
81 if (iter != extra_data_.end())
82 return iter->second.install_time;
83 return base::Time();
84}
85
[email protected]38427a12013-11-09 17:34:2086bool InfoMap::IsIncognitoEnabled(const std::string& extension_id) const {
[email protected]98b6d942013-11-10 00:34:0787 // Keep in sync with duplicate in extensions/browser/process_manager.cc.
[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.incognito_enabled;
91 return false;
92}
93
[email protected]38427a12013-11-09 17:34:2094bool InfoMap::CanCrossIncognito(const Extension* extension) const {
[email protected]c357acb42011-06-09 20:52:4295 // This is duplicated from ExtensionService :(.
96 return IsIncognitoEnabled(extension->id()) &&
[email protected]38427a12013-11-09 17:34:2097 !IncognitoInfo::IsSplitMode(extension);
[email protected]c357acb42011-06-09 20:52:4298}
[email protected]8add5412011-10-01 21:02:1499
[email protected]38427a12013-11-09 17:34:20100void InfoMap::RegisterExtensionProcess(const std::string& extension_id,
101 int process_id,
102 int site_instance_id) {
[email protected]6bc04fd82011-12-04 02:29:35103 if (!process_map_.Insert(extension_id, process_id, site_instance_id)) {
[email protected]6f371442011-11-09 06:45:46104 NOTREACHED() << "Duplicate extension process registration for: "
105 << extension_id << "," << process_id << ".";
106 }
[email protected]8add5412011-10-01 21:02:14107}
108
[email protected]38427a12013-11-09 17:34:20109void InfoMap::UnregisterExtensionProcess(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_.Remove(extension_id, process_id, site_instance_id)) {
[email protected]6f371442011-11-09 06:45:46113 NOTREACHED() << "Unknown 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::UnregisterAllExtensionsInProcess(int process_id) {
[email protected]6bc04fd82011-12-04 02:29:35119 process_map_.RemoveAllFromProcess(process_id);
[email protected]8add5412011-10-01 21:02:14120}
[email protected]34eec7ffe32011-11-02 23:49:02121
[email protected]38427a12013-11-09 17:34:20122void InfoMap::GetExtensionsWithAPIPermissionForSecurityOrigin(
[email protected]c7cd5942013-04-30 03:31:01123 const GURL& origin,
124 int process_id,
[email protected]38427a12013-11-09 17:34:20125 APIPermission::ID permission,
[email protected]c7cd5942013-04-30 03:31:01126 ExtensionSet* extensions) const {
127 DCHECK(extensions);
128
[email protected]38427a12013-11-09 17:34:20129 if (origin.SchemeIs(kExtensionScheme)) {
[email protected]34eec7ffe32011-11-02 23:49:02130 const std::string& id = origin.host();
[email protected]af73c252012-07-27 00:16:39131 const Extension* extension = extensions_.GetByID(id);
[email protected]c7cd5942013-04-30 03:31:01132 if (extension && extension->HasAPIPermission(permission) &&
133 process_map_.Contains(id, process_id)) {
134 extensions->Insert(extension);
135 }
136 return;
[email protected]34eec7ffe32011-11-02 23:49:02137 }
138
[email protected]84df8332011-12-06 18:22:46139 ExtensionSet::const_iterator i = extensions_.begin();
[email protected]34eec7ffe32011-11-02 23:49:02140 for (; i != extensions_.end(); ++i) {
[email protected]84df8332011-12-06 18:22:46141 if ((*i)->web_extent().MatchesSecurityOrigin(origin) &&
142 process_map_.Contains((*i)->id(), process_id) &&
143 (*i)->HasAPIPermission(permission)) {
[email protected]c7cd5942013-04-30 03:31:01144 extensions->Insert(*i);
[email protected]34eec7ffe32011-11-02 23:49:02145 }
146 }
[email protected]c7cd5942013-04-30 03:31:01147}
148
[email protected]38427a12013-11-09 17:34:20149bool InfoMap::SecurityOriginHasAPIPermission(const GURL& origin,
150 int process_id,
151 APIPermission::ID permission)
152 const {
[email protected]c7cd5942013-04-30 03:31:01153 ExtensionSet extensions;
154 GetExtensionsWithAPIPermissionForSecurityOrigin(
155 origin, process_id, permission, &extensions);
156 return !extensions.is_empty();
[email protected]34eec7ffe32011-11-02 23:49:02157}
[email protected]36296912012-03-20 11:08:49158
[email protected]38427a12013-11-09 17:34:20159QuotaService* InfoMap::GetQuotaService() {
[email protected]36296912012-03-20 11:08:49160 CheckOnValidThread();
[email protected]3eeddd892013-04-17 17:00:11161 if (!quota_service_)
[email protected]38427a12013-11-09 17:34:20162 quota_service_.reset(new QuotaService());
[email protected]36296912012-03-20 11:08:49163 return quota_service_.get();
164}
[email protected]5f2a4752012-04-27 22:18:58165
[email protected]38427a12013-11-09 17:34:20166void InfoMap::SetSigninProcess(int process_id) {
[email protected]1deace22013-05-22 06:14:46167 signin_process_id_ = process_id;
168}
169
[email protected]38427a12013-11-09 17:34:20170bool InfoMap::IsSigninProcess(int process_id) const {
[email protected]1deace22013-05-22 06:14:46171 return process_id == signin_process_id_;
172}
173
[email protected]38427a12013-11-09 17:34:20174InfoMap::~InfoMap() {
[email protected]3eeddd892013-04-17 17:00:11175 if (quota_service_) {
[email protected]38427a12013-11-09 17:34:20176 BrowserThread::DeleteSoon(
177 BrowserThread::IO, FROM_HERE, quota_service_.release());
[email protected]5f2a4752012-04-27 22:18:58178 }
179}
[email protected]38427a12013-11-09 17:34:20180
181} // namespace extensions