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"
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"
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
[] = {
28 PolicySettings::RendererForUrl
PolicySettings::GetRendererForUrl(
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
;
43 PolicySettings::RendererForUrl
PolicySettings::GetRendererForContentType(
44 const wchar_t* 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
;
58 const CommandLine
& PolicySettings::AdditionalLaunchParameters() const {
59 return additional_launch_parameters_
;
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::kRegistryMandatorySubKey
,
78 KEY_READ
) == ERROR_SUCCESS
) &&
79 (config_key
.ReadValueDW(settings_value
.c_str(),
80 &value
) == ERROR_SUCCESS
)) {
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";
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: "
103 << " exclusion list size: " << renderer_exclusion_list
->size();
108 void PolicySettings::ReadContentTypeSetting(
109 std::vector
<std::wstring
>* content_type_list
) {
110 DCHECK(content_type_list
);
112 std::wstring
sub_key(policy::kRegistryMandatorySubKey
);
114 sub_key
+= ASCIIToWide(policy::key::kChromeFrameContentTypes
);
116 content_type_list
->clear();
117 for (int i
= 0; i
< arraysize(kRootKeys
) && content_type_list
->empty();
119 EnumerateKeyValues(kRootKeys
[i
], sub_key
.c_str(), content_type_list
);
124 void PolicySettings::ReadStringSetting(const char* value_name
,
125 std::wstring
* value
) {
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::kRegistryMandatorySubKey
,
132 KEY_READ
) == ERROR_SUCCESS
) &&
133 (config_key
.ReadValue(value_name_str
.c_str(),
134 value
) == ERROR_SUCCESS
)) {
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.)
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
);
171 PolicySettings
* PolicySettings::GetInstance() {
172 return Singleton
<PolicySettings
>::get();