1// Copyright (c) 2012 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_UTILS_H_
6#define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_UTILS_H_
7
8#include <string>
9#include <utility>
10
11#include "components/content_settings/core/common/content_settings.h"
12#include "components/content_settings/core/common/content_settings_pattern.h"
13#include "components/content_settings/core/common/content_settings_types.h"
14
15namespace base {
16class Value;
17}
18
19class GURL;
20class HostContentSettingsMap;
21
22namespace content_settings {
23
24class ProviderInterface;
25class RuleIterator;
26
27typedef std::pair<ContentSettingsPattern, ContentSettingsPattern> PatternPair;
28
29std::string GetTypeName(ContentSettingsType type);
30
31bool GetTypeFromName(const std::string& name,
32                     ContentSettingsType* return_setting);
33
34std::string ContentSettingToString(ContentSetting setting);
35
36ContentSetting ContentSettingFromString(const std::string& name);
37
38// Converts |Value| to |ContentSetting|.
39ContentSetting ValueToContentSetting(const base::Value* value);
40
41// Converts a |Value| to a |ContentSetting|. Returns true if |value| encodes
42// a valid content setting, false otherwise. Note that |CONTENT_SETTING_DEFAULT|
43// is encoded as a NULL value, so it is not allowed as an integer value.
44bool ParseContentSettingValue(const base::Value* value,
45                              ContentSetting* setting);
46
47PatternPair ParsePatternString(const std::string& pattern_str);
48
49std::string CreatePatternString(
50    const ContentSettingsPattern& item_pattern,
51    const ContentSettingsPattern& top_level_frame_pattern);
52
53// Caller takes the ownership of the returned |base::Value*|.
54base::Value* GetContentSettingValueAndPatterns(
55    RuleIterator* rule_iterator,
56    const GURL& primary_url,
57    const GURL& secondary_url,
58    ContentSettingsPattern* primary_pattern,
59    ContentSettingsPattern* secondary_pattern);
60
61base::Value* GetContentSettingValueAndPatterns(
62    const ProviderInterface* provider,
63    const GURL& primary_url,
64    const GURL& secondary_url,
65    ContentSettingsType content_type,
66    const std::string& resource_identifier,
67    bool include_incognito,
68    ContentSettingsPattern* primary_pattern,
69    ContentSettingsPattern* secondary_pattern);
70
71base::Value* GetContentSettingValue(
72    const ProviderInterface* provider,
73    const GURL& primary_url,
74    const GURL& secondary_url,
75    ContentSettingsType content_type,
76    const std::string& resource_identifier,
77    bool include_incognito);
78
79ContentSetting GetContentSetting(
80    const ProviderInterface* provider,
81    const GURL& primary_url,
82    const GURL& secondary_url,
83    ContentSettingsType content_type,
84    const std::string& resource_identifier,
85    bool include_incognito);
86
87// Populates |rules| with content setting rules for content types that are
88// handled by the renderer.
89void GetRendererContentSettingRules(const HostContentSettingsMap* map,
90                                    RendererContentSettingRules* rules);
91
92}  // namespace content_settings
93
94#endif  // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_UTILS_H_
95