15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/content_settings/content_settings_utils.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_vector.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_split.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/content_settings/host_content_settings_map.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/content_settings/core/browser/content_settings_provider.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/content_settings/core/browser/content_settings_rule.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/content_settings/core/common/content_settings_pattern.h"
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The names of the ContentSettingsType values, for use with dictionary prefs.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char* kTypeNames[] = {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "cookies",
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "images",
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "javascript",
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "plugins",
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "popups",
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "geolocation",
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "notifications",
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "auto-select-certificate",
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "fullscreen",
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "mouselock",
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "mixed-script",
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "media-stream",
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  "media-stream-mic",
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  "media-stream-camera",
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "register-protocol-handler",
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  "ppapi-broker",
427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  "multiple-automatic-downloads",
43558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  "midi-sysex",
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  "push-messaging",
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  "ssl-cert-decisions",
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN)
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  "metro-switch-to-desktop",
480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#elif defined(OS_ANDROID) || defined(OS_CHROMEOS)
4968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  "protected-media-identifier",
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_ANDROID)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  "app-banner",
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               type_names_incorrect_size);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kPatternSeparator[] = ",";
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content_settings {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GetTypeName(ContentSettingsType type) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return std::string(kTypeNames[type]);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool GetTypeFromName(const std::string& name,
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                     ContentSettingsType* return_setting) {
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) {
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (name.compare(kTypeNames[type]) == 0) {
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      *return_setting = static_cast<ContentSettingsType>(type);
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return true;
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return false;
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)std::string ContentSettingToString(ContentSetting setting) {
8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  switch (setting) {
8103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case CONTENT_SETTING_ALLOW:
8203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return "allow";
8303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case CONTENT_SETTING_ASK:
8403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return "ask";
8503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case CONTENT_SETTING_BLOCK:
8603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return "block";
8703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case CONTENT_SETTING_SESSION_ONLY:
8803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return "session";
8903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case CONTENT_SETTING_DEFAULT:
9003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return "default";
9103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case CONTENT_SETTING_NUM_SETTINGS:
9203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      NOTREACHED();
9303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
9403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
9503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  return std::string();
9603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
9703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
9803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)ContentSetting ContentSettingFromString(const std::string& name) {
9903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (name == "allow")
10003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    return CONTENT_SETTING_ALLOW;
10103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (name == "ask")
10203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    return CONTENT_SETTING_ASK;
10303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (name == "block")
10403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    return CONTENT_SETTING_BLOCK;
10503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (name == "session")
10603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    return CONTENT_SETTING_SESSION_ONLY;
10703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
10803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  NOTREACHED() << name << " is not a recognized content setting.";
10903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  return CONTENT_SETTING_DEFAULT;
11003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
11103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string CreatePatternString(
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ContentSettingsPattern& item_pattern,
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ContentSettingsPattern& top_level_frame_pattern) {
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return item_pattern.ToString()
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         + std::string(kPatternSeparator)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         + top_level_frame_pattern.ToString();
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PatternPair ParsePatternString(const std::string& pattern_str) {
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> pattern_str_list;
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::SplitString(pattern_str, kPatternSeparator[0], &pattern_str_list);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the |pattern_str| is an empty string then the |pattern_string_list|
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // contains a single empty string. In this case the empty string will be
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // removed to signal an invalid |pattern_str|. Invalid pattern strings are
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // handle by the "if"-statment below. So the order of the if statements here
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // must be preserved.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pattern_str_list.size() == 1) {
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (pattern_str_list[0].empty()) {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pattern_str_list.pop_back();
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pattern_str_list.push_back("*");
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pattern_str_list.size() > 2 ||
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pattern_str_list.size() == 0) {
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return PatternPair(ContentSettingsPattern(),
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       ContentSettingsPattern());
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PatternPair pattern_pair;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pattern_pair.first =
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ContentSettingsPattern::FromString(pattern_str_list[0]);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pattern_pair.second =
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ContentSettingsPattern::FromString(pattern_str_list[1]);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return pattern_pair;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ContentSetting ValueToContentSetting(const base::Value* value) {
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ContentSetting setting = CONTENT_SETTING_DEFAULT;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool valid = ParseContentSettingValue(value, &setting);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(valid);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return setting;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ParseContentSettingValue(const base::Value* value,
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              ContentSetting* setting) {
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!value) {
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    *setting = CONTENT_SETTING_DEFAULT;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int int_value = -1;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!value->GetAsInteger(&int_value))
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *setting = IntToContentSetting(int_value);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return *setting != CONTENT_SETTING_DEFAULT;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::Value* GetContentSettingValueAndPatterns(
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ProviderInterface* provider,
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& primary_url,
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& secondary_url,
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentSettingsType content_type,
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& resource_identifier,
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool include_incognito,
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentSettingsPattern* primary_pattern,
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentSettingsPattern* secondary_pattern) {
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (include_incognito) {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Check incognito-only specific settings. It's essential that the
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // |RuleIterator| gets out of scope before we get a rule iterator for the
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // normal mode.
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<RuleIterator> incognito_rule_iterator(
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        provider->GetRuleIterator(content_type, resource_identifier, true));
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::Value* value = GetContentSettingValueAndPatterns(
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        incognito_rule_iterator.get(), primary_url, secondary_url,
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        primary_pattern, secondary_pattern);
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (value)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return value;
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // No settings from the incognito; use the normal mode.
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<RuleIterator> rule_iterator(
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      provider->GetRuleIterator(content_type, resource_identifier, false));
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetContentSettingValueAndPatterns(
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rule_iterator.get(), primary_url, secondary_url,
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      primary_pattern, secondary_pattern);
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::Value* GetContentSettingValueAndPatterns(
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RuleIterator* rule_iterator,
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& primary_url,
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& secondary_url,
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentSettingsPattern* primary_pattern,
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentSettingsPattern* secondary_pattern) {
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (rule_iterator->HasNext()) {
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Rule& rule = rule_iterator->Next();
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (rule.primary_pattern.Matches(primary_url) &&
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        rule.secondary_pattern.Matches(secondary_url)) {
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (primary_pattern)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        *primary_pattern = rule.primary_pattern;
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (secondary_pattern)
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        *secondary_pattern = rule.secondary_pattern;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return rule.value.get()->DeepCopy();
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::Value* GetContentSettingValue(const ProviderInterface* provider,
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    const GURL& primary_url,
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    const GURL& secondary_url,
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    ContentSettingsType content_type,
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    const std::string& resource_identifier,
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    bool include_incognito) {
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetContentSettingValueAndPatterns(provider, primary_url, secondary_url,
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               content_type, resource_identifier,
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               include_incognito, NULL, NULL);
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ContentSetting GetContentSetting(const ProviderInterface* provider,
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const GURL& primary_url,
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const GURL& secondary_url,
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 ContentSettingsType content_type,
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const std::string& resource_identifier,
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 bool include_incognito) {
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::Value> value(
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetContentSettingValue(provider, primary_url, secondary_url,
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             content_type, resource_identifier,
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             include_incognito));
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ValueToContentSetting(value.get());
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GetRendererContentSettingRules(const HostContentSettingsMap* map,
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    RendererContentSettingRules* rules) {
246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  map->GetSettingsForOneType(
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &(rules->image_rules));
248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  map->GetSettingsForOneType(
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), &(rules->script_rules));
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content_settings
253