1 // Copyright (c) 2015 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 "components/content_settings/core/browser/content_settings_global_value_map.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/synchronization/lock.h"
11 #include "components/content_settings/core/browser/content_settings_rule.h"
12 #include "components/content_settings/core/common/content_settings.h"
13 
14 namespace content_settings {
15 
16 namespace {
17 
18 class RuleIteratorSimple : public RuleIterator {
19  public:
RuleIteratorSimple(ContentSetting setting)20   RuleIteratorSimple(ContentSetting setting) : setting_(setting) {}
21 
HasNext() const22   bool HasNext() const override { return !is_done_; }
23 
Next()24   Rule Next() override {
25     DCHECK(HasNext());
26     is_done_ = true;
27     return Rule(ContentSettingsPattern::Wildcard(),
28                 ContentSettingsPattern::Wildcard(), base::Value(setting_),
29                 base::Time(), SessionModel::Durable);
30   }
31 
32  private:
33   const ContentSetting setting_;
34   bool is_done_ = false;
35 
36   DISALLOW_COPY_AND_ASSIGN(RuleIteratorSimple);
37 };
38 
39 }  // namespace
40 
GlobalValueMap()41 GlobalValueMap::GlobalValueMap() {}
42 
~GlobalValueMap()43 GlobalValueMap::~GlobalValueMap() {}
44 
GetRuleIterator(ContentSettingsType content_type) const45 std::unique_ptr<RuleIterator> GlobalValueMap::GetRuleIterator(
46     ContentSettingsType content_type) const {
47   auto it = settings_.find(content_type);
48   if (it == settings_.end())
49     return nullptr;
50 
51   return std::make_unique<RuleIteratorSimple>(it->second);
52 }
53 
SetContentSetting(ContentSettingsType content_type,ContentSetting setting)54 void GlobalValueMap::SetContentSetting(ContentSettingsType content_type,
55                                        ContentSetting setting) {
56   if (setting == CONTENT_SETTING_DEFAULT)
57     settings_.erase(content_type);
58   else
59     settings_[content_type] = setting;
60 }
61 
GetContentSetting(ContentSettingsType content_type) const62 ContentSetting GlobalValueMap::GetContentSetting(
63     ContentSettingsType content_type) const {
64   auto it = settings_.find(content_type);
65   return it == settings_.end() ? CONTENT_SETTING_DEFAULT : it->second;
66 }
67 
68 }  // namespace content_settings
69