summaryrefslogtreecommitdiffstats
path: root/extensions/browser/runtime_data.cc
blob: 3cee8466d5983d2da0dc9c733ec4d60c282ff309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/runtime_data.h"

#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_handlers/background_info.h"

namespace extensions {

RuntimeData::RuntimeData(ExtensionRegistry* registry) : registry_(registry) {
  registry_->AddObserver(this);
}

RuntimeData::~RuntimeData() {
  registry_->RemoveObserver(this);
}

bool RuntimeData::IsBackgroundPageReady(const Extension* extension) const {
  if (!BackgroundInfo::HasPersistentBackgroundPage(extension))
    return true;
  return HasFlag(extension->id(), BACKGROUND_PAGE_READY);
}

void RuntimeData::SetBackgroundPageReady(const std::string& extension_id,
                                         bool value) {
  SetFlag(extension_id, BACKGROUND_PAGE_READY, value);
}

bool RuntimeData::IsBeingUpgraded(const std::string& extension_id) const {
  return HasFlag(extension_id, BEING_UPGRADED);
}

void RuntimeData::SetBeingUpgraded(const std::string& extension_id,
                                   bool value) {
  SetFlag(extension_id, BEING_UPGRADED, value);
}

bool RuntimeData::HasUsedWebRequest(const std::string& extension_id) const {
  return HasFlag(extension_id, HAS_USED_WEBREQUEST);
}

void RuntimeData::SetHasUsedWebRequest(const std::string& extension_id,
                                       bool value) {
  SetFlag(extension_id, HAS_USED_WEBREQUEST, value);
}

bool RuntimeData::HasExtensionForTesting(
    const std::string& extension_id) const {
  return extension_flags_.find(extension_id) != extension_flags_.end();
}

void RuntimeData::ClearAll() {
  extension_flags_.clear();
}

void RuntimeData::OnExtensionUnloaded(content::BrowserContext* browser_context,
                                      const Extension* extension,
                                      UnloadedExtensionInfo::Reason reason) {
  ExtensionFlagsMap::iterator iter = extension_flags_.find(extension->id());
  if (iter != extension_flags_.end())
    iter->second = iter->second & kPersistAcrossUnloadMask;
}

bool RuntimeData::HasFlag(const std::string& extension_id,
                          RuntimeFlag flag) const {
  ExtensionFlagsMap::const_iterator it = extension_flags_.find(extension_id);
  if (it == extension_flags_.end())
    return false;
  return !!(it->second & flag);
}

void RuntimeData::SetFlag(const std::string& extension_id,
                          RuntimeFlag flag,
                          bool value) {
  if (value)
    extension_flags_[extension_id] |= flag;
  else
    extension_flags_[extension_id] &= ~flag;
}

}  // namespace extensions