[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::HasExtensionForTesting( |
| 42 | const std::string& extension_id) const { |
| 43 | return extension_flags_.find(extension_id) != extension_flags_.end(); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void RuntimeData::ClearAll() { |
| 47 | extension_flags_.clear(); |
| 48 | } |
| 49 | |
[email protected] | e51232f3 | 2014-04-18 20:05:36 | [diff] [blame] | 50 | void RuntimeData::OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 51 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 52 | UnloadedExtensionReason reason) { |
jdoerrie | a1e1598b | 2018-10-10 09:10:37 | [diff] [blame] | 53 | auto iter = extension_flags_.find(extension->id()); |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 54 | if (iter != extension_flags_.end()) |
| 55 | iter->second = iter->second & kPersistAcrossUnloadMask; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 56 | } |
| 57 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 58 | bool RuntimeData::HasFlag(const std::string& extension_id, |
| 59 | RuntimeFlag flag) const { |
jdoerrie | a1e1598b | 2018-10-10 09:10:37 | [diff] [blame] | 60 | auto it = extension_flags_.find(extension_id); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 61 | if (it == extension_flags_.end()) |
| 62 | return false; |
| 63 | return !!(it->second & flag); |
| 64 | } |
| 65 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 66 | void RuntimeData::SetFlag(const std::string& extension_id, |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 67 | RuntimeFlag flag, |
| 68 | bool value) { |
| 69 | if (value) |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 70 | extension_flags_[extension_id] |= flag; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 71 | else |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 72 | extension_flags_[extension_id] &= ~flag; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } // namespace extensions |