1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_BASE_PROVIDER_H_
6#define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_BASE_PROVIDER_H_
7#pragma once
8
9#include <map>
10#include <string>
11#include <utility>
12
13#include "base/synchronization/lock.h"
14#include "chrome/browser/content_settings/content_settings_provider.h"
15
16namespace content_settings {
17
18typedef std::pair<ContentSettingsType, std::string>
19    ContentSettingsTypeResourceIdentifierPair;
20typedef std::map<ContentSettingsTypeResourceIdentifierPair, ContentSetting>
21    ResourceContentSettings;
22
23struct ExtendedContentSettings {
24  ExtendedContentSettings();
25  ExtendedContentSettings(const ExtendedContentSettings& rhs);
26  ~ExtendedContentSettings();
27
28  ContentSettings content_settings;
29  ResourceContentSettings content_settings_for_resources;
30};
31
32// Map for ContentSettings.
33typedef std::map<std::string, ExtendedContentSettings> HostContentSettings;
34
35// BaseProvider is the abstract base class for all content-settings-provider
36// classes.
37class BaseProvider : public ProviderInterface {
38 public:
39  // Maps CONTENT_SETTING_ASK for the CONTENT_SETTINGS_TYPE_PLUGINS to
40  // CONTENT_SETTING_BLOCK if click-to-play is not enabled.
41  static ContentSetting ClickToPlayFixup(ContentSettingsType content_type,
42                                         ContentSetting setting);
43
44  explicit BaseProvider(bool is_otr);
45  virtual ~BaseProvider();
46
47
48  // Initializes the Provider.
49  virtual void Init() = 0;
50
51  // ProviderInterface Implementation
52  virtual bool ContentSettingsTypeIsManaged(
53      ContentSettingsType content_type) = 0;
54
55  virtual ContentSetting GetContentSetting(
56      const GURL& requesting_url,
57      const GURL& embedding_url,
58      ContentSettingsType content_type,
59      const ResourceIdentifier& resource_identifier) const;
60
61  virtual void SetContentSetting(
62      const ContentSettingsPattern& requesting_pattern,
63      const ContentSettingsPattern& embedding_pattern,
64      ContentSettingsType content_type,
65      const ResourceIdentifier& resource_identifier,
66      ContentSetting content_setting) = 0;
67
68  virtual void GetAllContentSettingsRules(
69      ContentSettingsType content_type,
70      const ResourceIdentifier& resource_identifier,
71      Rules* content_setting_rules) const;
72
73  virtual void ClearAllContentSettingsRules(
74      ContentSettingsType content_type) = 0;
75
76  virtual void ResetToDefaults() = 0;
77
78 protected:
79  // Returns true if the |content_type| requires a resource identifier.
80  bool RequiresResourceIdentifier(
81      ContentSettingsType content_type) const;
82
83  // Returns true if the passed |settings| object contains only
84  // CONTENT_SETTING_DEFAULT values.
85  bool AllDefault(const ExtendedContentSettings& settings) const;
86
87  void UpdateContentSettingsMap(
88      const ContentSettingsPattern& requesting_pattern,
89      const ContentSettingsPattern& embedding_pattern,
90      ContentSettingsType content_type,
91      const ResourceIdentifier& resource_identifier,
92      ContentSetting content_setting);
93
94  // TODO(markusheintz): LEGACY method. Will be removed in a future re-factoring
95  // step.
96  ContentSettings GetNonDefaultContentSettings(const GURL& url) const;
97
98  // Accessors
99  HostContentSettings* host_content_settings() {
100    return &host_content_settings_;
101  }
102
103  HostContentSettings* incognito_settings() {
104    return &incognito_settings_;
105  }
106
107  base::Lock& lock() const {
108    return lock_;
109  }
110
111  bool is_incognito() const {
112    return is_incognito_;
113  }
114
115 private:
116  // Copies of the pref data, so that we can read it on threads other than the
117  // UI thread.
118  HostContentSettings host_content_settings_;
119
120  // Whether this settings map is for an OTR session.
121  bool is_incognito_;
122
123  // Differences to the preference-stored host content settings for
124  // incognito settings.
125  HostContentSettings incognito_settings_;
126
127  // Used around accesses to the content_settings_ object to guarantee
128  // thread safety.
129  mutable base::Lock lock_;
130};
131
132}  // namespace content_settings
133
134#endif  // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_BASE_PROVIDER_H_
135