15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/runtime_data.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_registry.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/common/extension.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/common/manifest_handlers/background_info.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace extensions {
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)RuntimeData::RuntimeData(ExtensionRegistry* registry) : registry_(registry) {
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  registry_->AddObserver(this);
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)RuntimeData::~RuntimeData() {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  registry_->RemoveObserver(this);
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool RuntimeData::IsBackgroundPageReady(const Extension* extension) const {
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!BackgroundInfo::HasPersistentBackgroundPage(extension))
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return HasFlag(extension, BACKGROUND_PAGE_READY);
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RuntimeData::SetBackgroundPageReady(const Extension* extension,
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                         bool value) {
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFlag(extension, BACKGROUND_PAGE_READY, value);
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool RuntimeData::IsBeingUpgraded(const Extension* extension) const {
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return HasFlag(extension, BEING_UPGRADED);
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RuntimeData::SetBeingUpgraded(const Extension* extension, bool value) {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFlag(extension, BEING_UPGRADED, value);
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool RuntimeData::HasUsedWebRequest(const Extension* extension) const {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return HasFlag(extension, HAS_USED_WEBREQUEST);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RuntimeData::SetHasUsedWebRequest(const Extension* extension, bool value) {
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetFlag(extension, HAS_USED_WEBREQUEST, value);
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool RuntimeData::HasExtensionForTesting(const Extension* extension) const {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return extension_flags_.find(extension->id()) != extension_flags_.end();
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RuntimeData::ClearAll() {
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  extension_flags_.clear();
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RuntimeData::OnExtensionUnloaded(content::BrowserContext* browser_context,
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                      const Extension* extension,
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                      UnloadedExtensionInfo::Reason reason) {
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  extension_flags_.erase(extension->id());
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool RuntimeData::HasFlag(const Extension* extension, RuntimeFlag flag) const {
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ExtensionFlagsMap::const_iterator it = extension_flags_.find(extension->id());
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (it == extension_flags_.end())
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return !!(it->second & flag);
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RuntimeData::SetFlag(const Extension* extension,
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          RuntimeFlag flag,
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          bool value) {
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (value)
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    extension_flags_[extension->id()] |= flag;
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  else
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    extension_flags_[extension->id()] &= ~flag;
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace extensions
79