blob: 97c08107bf57eb4f56949d9fe5b9f3b49c2efdc9 [file] [log] [blame]
[email protected]55604d72010-09-15 14:41:481// 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.
4
5#include "chrome_frame/policy_settings.h"
6
[email protected]6ae3d492010-10-20 14:01:217#include <algorithm>
8
[email protected]55604d72010-09-15 14:41:489#include "base/logging.h"
[email protected]55604d72010-09-15 14:41:4810#include "base/string_util.h"
11#include "base/utf_string_conversions.h"
[email protected]2d6503982010-10-17 04:41:5412#include "base/win/registry.h"
[email protected]5ac670a2010-10-04 23:07:4013#include "chrome_frame/utils.h"
[email protected]98818cdc2011-01-28 13:24:3214#include "policy/policy_constants.h"
[email protected]55604d72010-09-15 14:41:4815
[email protected]6ae3d492010-10-20 14:01:2116namespace {
17
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).
21const HKEY kRootKeys[] = {
22 HKEY_LOCAL_MACHINE,
23 HKEY_CURRENT_USER
24};
25
26} // namespace
27
[email protected]55604d72010-09-15 14:41:4828PolicySettings::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;
38 }
39 }
40 return renderer;
41}
42
[email protected]5ac670a2010-10-04 23:07:4043PolicySettings::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;
53 }
54 }
55 return renderer;
56}
57
[email protected]6ae3d492010-10-20 14:01:2158// static
59void PolicySettings::ReadUrlSettings(
60 RendererForUrl* default_renderer,
61 std::vector<std::wstring>* renderer_exclusion_list) {
62 DCHECK(default_renderer);
63 DCHECK(renderer_exclusion_list);
64
65 *default_renderer = RENDERER_NOT_SPECIFIED;
66 renderer_exclusion_list->clear();
[email protected]55604d72010-09-15 14:41:4867
[email protected]2d6503982010-10-17 04:41:5468 base::win::RegKey config_key;
[email protected]55604d72010-09-15 14:41:4869 DWORD value = RENDERER_NOT_SPECIFIED;
[email protected]55604d72010-09-15 14:41:4870 std::wstring settings_value(
71 ASCIIToWide(policy::key::kChromeFrameRendererSettings));
[email protected]6ae3d492010-10-20 14:01:2172 for (int i = 0; i < arraysize(kRootKeys); ++i) {
[email protected]e06f4d52011-01-19 07:28:4673 if ((config_key.Open(kRootKeys[i], policy::kRegistrySubKey,
74 KEY_READ) == ERROR_SUCCESS) &&
75 (config_key.ReadValueDW(settings_value.c_str(),
76 &value) == ERROR_SUCCESS)) {
77 break;
[email protected]55604d72010-09-15 14:41:4878 }
79 }
80
81 DCHECK(value == RENDERER_NOT_SPECIFIED ||
82 value == RENDER_IN_HOST ||
83 value == RENDER_IN_CHROME_FRAME) <<
84 "invalid default renderer setting: " << value;
85
86 if (value != RENDER_IN_HOST && value != RENDER_IN_CHROME_FRAME) {
[email protected]2b9a9f162010-10-19 20:30:4587 DVLOG(1) << "default renderer not specified via policy";
[email protected]55604d72010-09-15 14:41:4888 } else {
[email protected]6ae3d492010-10-20 14:01:2189 *default_renderer = static_cast<RendererForUrl>(value);
90 const char* exclusion_list_name = (*default_renderer == RENDER_IN_HOST) ?
[email protected]55604d72010-09-15 14:41:4891 policy::key::kRenderInChromeFrameList :
92 policy::key::kRenderInHostList;
93
[email protected]5ac670a2010-10-04 23:07:4094 EnumerateKeyValues(config_key.Handle(),
[email protected]6ae3d492010-10-20 14:01:2195 ASCIIToWide(exclusion_list_name).c_str(), renderer_exclusion_list);
[email protected]5ac670a2010-10-04 23:07:4096
[email protected]2b9a9f162010-10-19 20:30:4597 DVLOG(1) << "Default renderer as specified via policy: "
[email protected]6ae3d492010-10-20 14:01:2198 << *default_renderer
99 << " exclusion list size: " << renderer_exclusion_list->size();
[email protected]55604d72010-09-15 14:41:48100 }
[email protected]6ae3d492010-10-20 14:01:21101}
102
103// static
104void PolicySettings::ReadContentTypeSetting(
105 std::vector<std::wstring>* content_type_list) {
106 DCHECK(content_type_list);
[email protected]5ac670a2010-10-04 23:07:40107
108 std::wstring sub_key(policy::kRegistrySubKey);
109 sub_key += L"\\";
110 sub_key += ASCIIToWide(policy::key::kChromeFrameContentTypes);
111
[email protected]6ae3d492010-10-20 14:01:21112 content_type_list->clear();
[email protected]f6b8ce32011-03-02 00:03:18113 for (int i = 0; i < arraysize(kRootKeys) && content_type_list->empty();
[email protected]5ac670a2010-10-04 23:07:40114 ++i) {
[email protected]6ae3d492010-10-20 14:01:21115 EnumerateKeyValues(kRootKeys[i], sub_key.c_str(), content_type_list);
[email protected]5ac670a2010-10-04 23:07:40116 }
[email protected]55604d72010-09-15 14:41:48117}
118
[email protected]6ae3d492010-10-20 14:01:21119// static
120void PolicySettings::ReadApplicationLocaleSetting(
121 std::wstring* application_locale) {
122 DCHECK(application_locale);
123
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) {
[email protected]e06f4d52011-01-19 07:28:46129 if ((config_key.Open(kRootKeys[i], policy::kRegistrySubKey,
130 KEY_READ) == ERROR_SUCCESS) &&
131 (config_key.ReadValue(application_locale_value.c_str(),
132 application_locale) == ERROR_SUCCESS)) {
133 break;
[email protected]6ae3d492010-10-20 14:01:21134 }
135 }
136}
137
138void 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;
143
144 // Read the latest settings from the registry
145 ReadUrlSettings(&default_renderer, &renderer_exclusion_list);
146 ReadContentTypeSetting(&content_type_list);
147 ReadApplicationLocaleSetting(&application_locale);
148
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;
152
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);
157}
158
[email protected]687b9602010-12-08 10:43:08159// static
160PolicySettings* PolicySettings::GetInstance() {
161 return Singleton<PolicySettings>::get();
162}