[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 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 "extensions/browser/runtime_data.h" |
| 6 | |
| 7 | #include "extensions/browser/extension_registry.h" |
| 8 | #include "extensions/common/extension.h" |
| 9 | #include "extensions/common/manifest_handlers/background_info.h" |
| 10 | |
| 11 | namespace extensions { |
| 12 | |
| 13 | RuntimeData::RuntimeData(ExtensionRegistry* registry) : registry_(registry) { |
| 14 | registry_->AddObserver(this); |
| 15 | } |
| 16 | |
| 17 | RuntimeData::~RuntimeData() { |
| 18 | registry_->RemoveObserver(this); |
| 19 | } |
| 20 | |
| 21 | bool RuntimeData::IsBackgroundPageReady(const Extension* extension) const { |
| 22 | if (!BackgroundInfo::HasPersistentBackgroundPage(extension)) |
| 23 | return true; |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 24 | return HasFlag(extension->id(), BACKGROUND_PAGE_READY); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 25 | } |
| 26 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 27 | void RuntimeData::SetBackgroundPageReady(const std::string& extension_id, |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 28 | bool value) { |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 29 | SetFlag(extension_id, BACKGROUND_PAGE_READY, value); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 30 | } |
| 31 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 32 | bool RuntimeData::IsBeingUpgraded(const std::string& extension_id) const { |
| 33 | return HasFlag(extension_id, BEING_UPGRADED); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 34 | } |
| 35 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 36 | void RuntimeData::SetBeingUpgraded(const std::string& extension_id, |
| 37 | bool value) { |
| 38 | SetFlag(extension_id, BEING_UPGRADED, value); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 39 | } |
| 40 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 41 | bool RuntimeData::HasUsedWebRequest(const std::string& extension_id) const { |
| 42 | return HasFlag(extension_id, HAS_USED_WEBREQUEST); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 43 | } |
| 44 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 45 | void RuntimeData::SetHasUsedWebRequest(const std::string& extension_id, |
| 46 | bool value) { |
| 47 | SetFlag(extension_id, HAS_USED_WEBREQUEST, value); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 48 | } |
| 49 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 50 | bool RuntimeData::HasExtensionForTesting( |
| 51 | const std::string& extension_id) const { |
| 52 | return extension_flags_.find(extension_id) != extension_flags_.end(); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void RuntimeData::ClearAll() { |
| 56 | extension_flags_.clear(); |
| 57 | } |
| 58 | |
[email protected] | e51232f3 | 2014-04-18 20:05:36 | [diff] [blame] | 59 | void RuntimeData::OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 60 | const Extension* extension, |
| 61 | UnloadedExtensionInfo::Reason reason) { |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 62 | ExtensionFlagsMap::iterator iter = extension_flags_.find(extension->id()); |
| 63 | if (iter != extension_flags_.end()) |
| 64 | iter->second = iter->second & kPersistAcrossUnloadMask; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 65 | } |
| 66 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 67 | bool RuntimeData::HasFlag(const std::string& extension_id, |
| 68 | RuntimeFlag flag) const { |
| 69 | ExtensionFlagsMap::const_iterator it = extension_flags_.find(extension_id); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 70 | if (it == extension_flags_.end()) |
| 71 | return false; |
| 72 | return !!(it->second & flag); |
| 73 | } |
| 74 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 75 | void RuntimeData::SetFlag(const std::string& extension_id, |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 76 | RuntimeFlag flag, |
| 77 | bool value) { |
| 78 | if (value) |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 79 | extension_flags_[extension_id] |= flag; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 80 | else |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 81 | extension_flags_[extension_id] &= ~flag; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } // namespace extensions |