Remove srpcgen-diff test from scons
[chromium-blink-merge.git] / chrome_frame / policy_settings.cc
blobd3a6f33d606467c007ef2609e8a13d2aed8496e8
1 // Copyright (c) 2010 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.
5 #include "chrome_frame/policy_settings.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/string_util.h"
11 #include "base/utf_string_conversions.h"
12 #include "base/win/registry.h"
13 #include "chrome_frame/utils.h"
14 #include "policy/policy_constants.h"
16 namespace {
18 // This array specifies the order in which registry keys are tested. Do not
19 // change this unless the decision is made product-wide (i.e., in Chrome's
20 // configuration policy provider).
21 const HKEY kRootKeys[] = {
22 HKEY_LOCAL_MACHINE,
23 HKEY_CURRENT_USER
26 } // namespace
28 PolicySettings::RendererForUrl PolicySettings::GetRendererForUrl(
29 const wchar_t* url) {
30 RendererForUrl renderer = default_renderer_;
31 std::vector<std::wstring>::const_iterator it;
32 for (it = renderer_exclusion_list_.begin();
33 it != renderer_exclusion_list_.end(); ++it) {
34 if (MatchPattern(url, (*it))) {
35 renderer = (renderer == RENDER_IN_HOST) ?
36 RENDER_IN_CHROME_FRAME : RENDER_IN_HOST;
37 break;
40 return renderer;
43 PolicySettings::RendererForUrl PolicySettings::GetRendererForContentType(
44 const wchar_t* content_type) {
45 DCHECK(content_type);
46 RendererForUrl renderer = RENDERER_NOT_SPECIFIED;
47 std::vector<std::wstring>::const_iterator it;
48 for (it = content_type_list_.begin();
49 it != content_type_list_.end(); ++it) {
50 if (lstrcmpiW(content_type, (*it).c_str()) == 0) {
51 renderer = RENDER_IN_CHROME_FRAME;
52 break;
55 return renderer;
58 // static
59 void PolicySettings::ReadUrlSettings(
60 RendererForUrl* default_renderer,
61 std::vector<std::wstring>* renderer_exclusion_list) {
62 DCHECK(default_renderer);
63 DCHECK(renderer_exclusion_list);
65 *default_renderer = RENDERER_NOT_SPECIFIED;
66 renderer_exclusion_list->clear();
68 base::win::RegKey config_key;
69 DWORD value = RENDERER_NOT_SPECIFIED;
70 std::wstring settings_value(
71 ASCIIToWide(policy::key::kChromeFrameRendererSettings));
72 for (int i = 0; i < arraysize(kRootKeys); ++i) {
73 if ((config_key.Open(kRootKeys[i], policy::kRegistryMandatorySubKey,
74 KEY_READ) == ERROR_SUCCESS) &&
75 (config_key.ReadValueDW(settings_value.c_str(),
76 &value) == ERROR_SUCCESS)) {
77 break;
81 DCHECK(value == RENDERER_NOT_SPECIFIED ||
82 value == RENDER_IN_HOST ||
83 value == RENDER_IN_CHROME_FRAME) <<
84 "invalid default renderer setting: " << value;
86 if (value != RENDER_IN_HOST && value != RENDER_IN_CHROME_FRAME) {
87 DVLOG(1) << "default renderer not specified via policy";
88 } else {
89 *default_renderer = static_cast<RendererForUrl>(value);
90 const char* exclusion_list_name = (*default_renderer == RENDER_IN_HOST) ?
91 policy::key::kRenderInChromeFrameList :
92 policy::key::kRenderInHostList;
94 EnumerateKeyValues(config_key.Handle(),
95 ASCIIToWide(exclusion_list_name).c_str(), renderer_exclusion_list);
97 DVLOG(1) << "Default renderer as specified via policy: "
98 << *default_renderer
99 << " exclusion list size: " << renderer_exclusion_list->size();
103 // static
104 void PolicySettings::ReadContentTypeSetting(
105 std::vector<std::wstring>* content_type_list) {
106 DCHECK(content_type_list);
108 std::wstring sub_key(policy::kRegistryMandatorySubKey);
109 sub_key += L"\\";
110 sub_key += ASCIIToWide(policy::key::kChromeFrameContentTypes);
112 content_type_list->clear();
113 for (int i = 0; i < arraysize(kRootKeys) && content_type_list->empty();
114 ++i) {
115 EnumerateKeyValues(kRootKeys[i], sub_key.c_str(), content_type_list);
119 // static
120 void PolicySettings::ReadApplicationLocaleSetting(
121 std::wstring* application_locale) {
122 DCHECK(application_locale);
124 application_locale->clear();
125 base::win::RegKey config_key;
126 std::wstring application_locale_value(
127 ASCIIToWide(policy::key::kApplicationLocaleValue));
128 for (int i = 0; i < arraysize(kRootKeys); ++i) {
129 if ((config_key.Open(kRootKeys[i], policy::kRegistryMandatorySubKey,
130 KEY_READ) == ERROR_SUCCESS) &&
131 (config_key.ReadValue(application_locale_value.c_str(),
132 application_locale) == ERROR_SUCCESS)) {
133 break;
138 void PolicySettings::RefreshFromRegistry() {
139 RendererForUrl default_renderer;
140 std::vector<std::wstring> renderer_exclusion_list;
141 std::vector<std::wstring> content_type_list;
142 std::wstring application_locale;
144 // Read the latest settings from the registry
145 ReadUrlSettings(&default_renderer, &renderer_exclusion_list);
146 ReadContentTypeSetting(&content_type_list);
147 ReadApplicationLocaleSetting(&application_locale);
149 // Nofail swap in the new values. (Note: this is all that need be protected
150 // under a mutex if/when this becomes thread safe.)
151 using std::swap;
153 swap(default_renderer_, default_renderer);
154 swap(renderer_exclusion_list_, renderer_exclusion_list);
155 swap(content_type_list_, content_type_list);
156 swap(application_locale_, application_locale);
159 // static
160 PolicySettings* PolicySettings::GetInstance() {
161 return Singleton<PolicySettings>::get();