Only sync parent directory once after a leveldb file rename.
[chromium-blink-merge.git] / chrome_frame / policy_settings.cc
blob846fd9960a4df1dc9fc23c3ea4f3d8c024dd7ba6
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.
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 const CommandLine& PolicySettings::AdditionalLaunchParameters() const {
59 return additional_launch_parameters_;
62 // static
63 void PolicySettings::ReadUrlSettings(
64 RendererForUrl* default_renderer,
65 std::vector<std::wstring>* renderer_exclusion_list) {
66 DCHECK(default_renderer);
67 DCHECK(renderer_exclusion_list);
69 *default_renderer = RENDERER_NOT_SPECIFIED;
70 renderer_exclusion_list->clear();
72 base::win::RegKey config_key;
73 DWORD value = RENDERER_NOT_SPECIFIED;
74 std::wstring settings_value(
75 ASCIIToWide(policy::key::kChromeFrameRendererSettings));
76 for (int i = 0; i < arraysize(kRootKeys); ++i) {
77 if ((config_key.Open(kRootKeys[i], policy::kRegistryChromePolicyKey,
78 KEY_READ) == ERROR_SUCCESS) &&
79 (config_key.ReadValueDW(settings_value.c_str(),
80 &value) == ERROR_SUCCESS)) {
81 break;
85 DCHECK(value == RENDERER_NOT_SPECIFIED ||
86 value == RENDER_IN_HOST ||
87 value == RENDER_IN_CHROME_FRAME) <<
88 "invalid default renderer setting: " << value;
90 if (value != RENDER_IN_HOST && value != RENDER_IN_CHROME_FRAME) {
91 DVLOG(1) << "default renderer not specified via policy";
92 } else {
93 *default_renderer = static_cast<RendererForUrl>(value);
94 const char* exclusion_list_name = (*default_renderer == RENDER_IN_HOST) ?
95 policy::key::kRenderInChromeFrameList :
96 policy::key::kRenderInHostList;
98 EnumerateKeyValues(config_key.Handle(),
99 ASCIIToWide(exclusion_list_name).c_str(), renderer_exclusion_list);
101 DVLOG(1) << "Default renderer as specified via policy: "
102 << *default_renderer
103 << " exclusion list size: " << renderer_exclusion_list->size();
107 // static
108 void PolicySettings::ReadContentTypeSetting(
109 std::vector<std::wstring>* content_type_list) {
110 DCHECK(content_type_list);
112 std::wstring sub_key(policy::kRegistryChromePolicyKey);
113 sub_key += L"\\";
114 sub_key += ASCIIToWide(policy::key::kChromeFrameContentTypes);
116 content_type_list->clear();
117 for (int i = 0; i < arraysize(kRootKeys) && content_type_list->empty();
118 ++i) {
119 EnumerateKeyValues(kRootKeys[i], sub_key.c_str(), content_type_list);
123 // static
124 void PolicySettings::ReadStringSetting(const char* value_name,
125 std::wstring* value) {
126 DCHECK(value);
127 value->clear();
128 base::win::RegKey config_key;
129 std::wstring value_name_str(ASCIIToWide(value_name));
130 for (int i = 0; i < arraysize(kRootKeys); ++i) {
131 if ((config_key.Open(kRootKeys[i], policy::kRegistryChromePolicyKey,
132 KEY_READ) == ERROR_SUCCESS) &&
133 (config_key.ReadValue(value_name_str.c_str(),
134 value) == ERROR_SUCCESS)) {
135 break;
140 void PolicySettings::RefreshFromRegistry() {
141 RendererForUrl default_renderer;
142 std::vector<std::wstring> renderer_exclusion_list;
143 std::vector<std::wstring> content_type_list;
144 std::wstring application_locale;
145 CommandLine additional_launch_parameters(CommandLine::NO_PROGRAM);
146 std::wstring additional_parameters_str;
148 // Read the latest settings from the registry
149 ReadUrlSettings(&default_renderer, &renderer_exclusion_list);
150 ReadContentTypeSetting(&content_type_list);
151 ReadStringSetting(policy::key::kApplicationLocaleValue, &application_locale);
152 ReadStringSetting(policy::key::kAdditionalLaunchParameters,
153 &additional_parameters_str);
154 if (!additional_parameters_str.empty()) {
155 additional_parameters_str.insert(0, L"fake.exe ");
156 additional_launch_parameters.ParseFromString(additional_parameters_str);
159 // Nofail swap in the new values. (Note: this is all that need be protected
160 // under a mutex if/when this becomes thread safe.)
161 using std::swap;
163 swap(default_renderer_, default_renderer);
164 swap(renderer_exclusion_list_, renderer_exclusion_list);
165 swap(content_type_list_, content_type_list);
166 swap(application_locale_, application_locale);
167 swap(additional_launch_parameters_, additional_launch_parameters);
170 // static
171 PolicySettings* PolicySettings::GetInstance() {
172 return Singleton<PolicySettings>::get();