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   }
30 
31  private:
32   const ContentSetting setting_;
33   bool is_done_ = false;
34 
35   DISALLOW_COPY_AND_ASSIGN(RuleIteratorSimple);
36 };
37 
38 }  // namespace
39 
GlobalValueMap()40 GlobalValueMap::GlobalValueMap() {}
41 
~GlobalValueMap()42 GlobalValueMap::~GlobalValueMap() {}
43 
GetRuleIterator(ContentSettingsType content_type,const ResourceIdentifier & resource_identifier) const44 std::unique_ptr<RuleIterator> GlobalValueMap::GetRuleIterator(
45     ContentSettingsType content_type,
46     const ResourceIdentifier& resource_identifier) const {
47   if (!resource_identifier.empty())
48     return nullptr;
49 
50   auto it = settings_.find(content_type);
51   if (it == settings_.end())
52     return nullptr;
53 
54   return std::make_unique<RuleIteratorSimple>(it->second);
55 }
56 
SetContentSetting(ContentSettingsType content_type,ContentSetting setting)57 void GlobalValueMap::SetContentSetting(ContentSettingsType content_type,
58                                        ContentSetting setting) {
59   if (setting == CONTENT_SETTING_DEFAULT)
60     settings_.erase(content_type);
61   else
62     settings_[content_type] = setting;
63 }
64 
GetContentSetting(ContentSettingsType content_type) const65 ContentSetting GlobalValueMap::GetContentSetting(
66     ContentSettingsType content_type) const {
67   auto it = settings_.find(content_type);
68   return it == settings_.end() ? CONTENT_SETTING_DEFAULT : it->second;
69 }
70 
71 }  // namespace content_settings
72