[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame^] | 1 | // Copyright (c) 2012 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 | |
| 5 | #include "chrome/browser/extensions/extension_info_map.h" |
| 6 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 7 | #include "chrome/common/extensions/extension.h" |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 8 | #include "chrome/common/extensions/extension_set.h" |
| 9 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 10 | #include "content/public/browser/browser_thread.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 11 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 12 | using content::BrowserThread; |
| 13 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 14 | namespace { |
| 15 | |
[email protected] | 2cce7ae | 2011-10-23 15:54:36 | [diff] [blame] | 16 | void CheckOnValidThread() { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 17 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | } // namespace |
| 21 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 22 | |
| 23 | struct ExtensionInfoMap::ExtraData { |
| 24 | // When the extension was installed. |
| 25 | base::Time install_time; |
| 26 | |
| 27 | // True if the user has allowed this extension to run in incognito mode. |
| 28 | bool incognito_enabled; |
| 29 | |
| 30 | ExtraData(); |
| 31 | ~ExtraData(); |
| 32 | }; |
| 33 | |
| 34 | ExtensionInfoMap::ExtraData::ExtraData() : incognito_enabled(false) { |
| 35 | } |
| 36 | |
| 37 | ExtensionInfoMap::ExtraData::~ExtraData() { |
| 38 | } |
| 39 | |
| 40 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 41 | ExtensionInfoMap::ExtensionInfoMap() { |
| 42 | } |
| 43 | |
| 44 | ExtensionInfoMap::~ExtensionInfoMap() { |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame^] | 45 | if (quota_service_.get()) { |
| 46 | BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
| 47 | quota_service_.release()); |
| 48 | } |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 51 | const extensions::ProcessMap& ExtensionInfoMap::process_map() const { |
| 52 | return process_map_; |
| 53 | } |
| 54 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 55 | void ExtensionInfoMap::AddExtension(const Extension* extension, |
| 56 | base::Time install_time, |
| 57 | bool incognito_enabled) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 58 | CheckOnValidThread(); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 59 | extensions_.Insert(extension); |
| 60 | disabled_extensions_.Remove(extension->id()); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 61 | |
| 62 | extra_data_[extension->id()].install_time = install_time; |
| 63 | extra_data_[extension->id()].incognito_enabled = incognito_enabled; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 66 | void ExtensionInfoMap::RemoveExtension(const std::string& extension_id, |
[email protected] | 814a7bf0f | 2011-08-13 05:30:59 | [diff] [blame] | 67 | const extension_misc::UnloadedExtensionReason reason) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 68 | CheckOnValidThread(); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 69 | const Extension* extension = extensions_.GetByID(extension_id); |
| 70 | extra_data_.erase(extension_id); // we don't care about disabled extra data |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 71 | bool was_uninstalled = (reason != extension_misc::UNLOAD_REASON_DISABLE && |
| 72 | reason != extension_misc::UNLOAD_REASON_TERMINATE); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 73 | if (extension) { |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 74 | if (!was_uninstalled) |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 75 | disabled_extensions_.Insert(extension); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 76 | extensions_.Remove(extension_id); |
[email protected] | b3f7fe2 | 2011-11-11 19:27:56 | [diff] [blame] | 77 | } else if (was_uninstalled) { |
[email protected] | dd163fb0 | 2011-05-04 22:22:17 | [diff] [blame] | 78 | // If the extension was uninstalled, make sure it's removed from the map of |
| 79 | // disabled extensions. |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 80 | disabled_extensions_.Remove(extension_id); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 81 | } else { |
| 82 | // NOTE: This can currently happen if we receive multiple unload |
| 83 | // notifications, e.g. setting incognito-enabled state for a |
| 84 | // disabled extension (e.g., via sync). See |
| 85 | // http://code.google.com/p/chromium/issues/detail?id=50582 . |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 86 | NOTREACHED() << extension_id; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 87 | } |
| 88 | } |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 89 | |
| 90 | base::Time ExtensionInfoMap::GetInstallTime( |
| 91 | const std::string& extension_id) const { |
| 92 | ExtraDataMap::const_iterator iter = extra_data_.find(extension_id); |
| 93 | if (iter != extra_data_.end()) |
| 94 | return iter->second.install_time; |
| 95 | return base::Time(); |
| 96 | } |
| 97 | |
| 98 | bool ExtensionInfoMap::IsIncognitoEnabled( |
| 99 | const std::string& extension_id) const { |
[email protected] | 8d3132f6 | 2011-10-12 07:13:42 | [diff] [blame] | 100 | // Keep in sync with duplicate in extension_process_manager.cc. |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 101 | ExtraDataMap::const_iterator iter = extra_data_.find(extension_id); |
| 102 | if (iter != extra_data_.end()) |
| 103 | return iter->second.incognito_enabled; |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | bool ExtensionInfoMap::CanCrossIncognito(const Extension* extension) { |
| 108 | // This is duplicated from ExtensionService :(. |
| 109 | return IsIncognitoEnabled(extension->id()) && |
| 110 | !extension->incognito_split_mode(); |
| 111 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 112 | |
[email protected] | 2cce7ae | 2011-10-23 15:54:36 | [diff] [blame] | 113 | void ExtensionInfoMap::RegisterExtensionProcess(const std::string& extension_id, |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 114 | int process_id, |
| 115 | int site_instance_id) { |
| 116 | if (!process_map_.Insert(extension_id, process_id, site_instance_id)) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 117 | NOTREACHED() << "Duplicate extension process registration for: " |
| 118 | << extension_id << "," << process_id << "."; |
| 119 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 2cce7ae | 2011-10-23 15:54:36 | [diff] [blame] | 122 | void ExtensionInfoMap::UnregisterExtensionProcess( |
| 123 | const std::string& extension_id, |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 124 | int process_id, |
| 125 | int site_instance_id) { |
| 126 | if (!process_map_.Remove(extension_id, process_id, site_instance_id)) { |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 127 | NOTREACHED() << "Unknown extension process registration for: " |
| 128 | << extension_id << "," << process_id << "."; |
| 129 | } |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 132 | void ExtensionInfoMap::UnregisterAllExtensionsInProcess(int process_id) { |
[email protected] | 6bc04fd8 | 2011-12-04 02:29:35 | [diff] [blame] | 133 | process_map_.RemoveAllFromProcess(process_id); |
[email protected] | 8add541 | 2011-10-01 21:02:14 | [diff] [blame] | 134 | } |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 135 | |
| 136 | bool ExtensionInfoMap::SecurityOriginHasAPIPermission( |
| 137 | const GURL& origin, int process_id, |
| 138 | ExtensionAPIPermission::ID permission) const { |
| 139 | if (origin.SchemeIs(chrome::kExtensionScheme)) { |
| 140 | const std::string& id = origin.host(); |
| 141 | return extensions_.GetByID(id)->HasAPIPermission(permission) && |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 142 | process_map_.Contains(id, process_id); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 145 | ExtensionSet::const_iterator i = extensions_.begin(); |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 146 | for (; i != extensions_.end(); ++i) { |
[email protected] | 84df833 | 2011-12-06 18:22:46 | [diff] [blame] | 147 | if ((*i)->web_extent().MatchesSecurityOrigin(origin) && |
| 148 | process_map_.Contains((*i)->id(), process_id) && |
| 149 | (*i)->HasAPIPermission(permission)) { |
[email protected] | 34eec7ffe3 | 2011-11-02 23:49:02 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | } |
| 153 | return false; |
| 154 | } |
[email protected] | 3629691 | 2012-03-20 11:08:49 | [diff] [blame^] | 155 | |
| 156 | ExtensionsQuotaService* ExtensionInfoMap::GetQuotaService() { |
| 157 | CheckOnValidThread(); |
| 158 | if (!quota_service_.get()) |
| 159 | quota_service_.reset(new ExtensionsQuotaService()); |
| 160 | return quota_service_.get(); |
| 161 | } |