[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome_frame/policy_settings.h" |
| 6 | |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 10 | #include "base/string_util.h" |
| 11 | #include "base/utf_string_conversions.h" |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 12 | #include "base/win/registry.h" |
[email protected] | 5ac670a | 2010-10-04 23:07:40 | [diff] [blame] | 13 | #include "chrome_frame/utils.h" |
[email protected] | 98818cdc | 2011-01-28 13:24:32 | [diff] [blame] | 14 | #include "policy/policy_constants.h" |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 15 | |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 16 | namespace { |
| 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). |
| 21 | const HKEY kRootKeys[] = { |
| 22 | HKEY_LOCAL_MACHINE, |
| 23 | HKEY_CURRENT_USER |
| 24 | }; |
| 25 | |
| 26 | } // namespace |
| 27 | |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 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; |
| 38 | } |
| 39 | } |
| 40 | return renderer; |
| 41 | } |
| 42 | |
[email protected] | 5ac670a | 2010-10-04 23:07:40 | [diff] [blame] | 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; |
| 53 | } |
| 54 | } |
| 55 | return renderer; |
| 56 | } |
| 57 | |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 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); |
| 64 | |
| 65 | *default_renderer = RENDERER_NOT_SPECIFIED; |
| 66 | renderer_exclusion_list->clear(); |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 67 | |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 68 | base::win::RegKey config_key; |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 69 | DWORD value = RENDERER_NOT_SPECIFIED; |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 70 | std::wstring settings_value( |
| 71 | ASCIIToWide(policy::key::kChromeFrameRendererSettings)); |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 72 | for (int i = 0; i < arraysize(kRootKeys); ++i) { |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 73 | 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] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 78 | } |
| 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] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 87 | DVLOG(1) << "default renderer not specified via policy"; |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 88 | } else { |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 89 | *default_renderer = static_cast<RendererForUrl>(value); |
| 90 | const char* exclusion_list_name = (*default_renderer == RENDER_IN_HOST) ? |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 91 | policy::key::kRenderInChromeFrameList : |
| 92 | policy::key::kRenderInHostList; |
| 93 | |
[email protected] | 5ac670a | 2010-10-04 23:07:40 | [diff] [blame] | 94 | EnumerateKeyValues(config_key.Handle(), |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 95 | ASCIIToWide(exclusion_list_name).c_str(), renderer_exclusion_list); |
[email protected] | 5ac670a | 2010-10-04 23:07:40 | [diff] [blame] | 96 | |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 97 | DVLOG(1) << "Default renderer as specified via policy: " |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 98 | << *default_renderer |
| 99 | << " exclusion list size: " << renderer_exclusion_list->size(); |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 100 | } |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // static |
| 104 | void PolicySettings::ReadContentTypeSetting( |
| 105 | std::vector<std::wstring>* content_type_list) { |
| 106 | DCHECK(content_type_list); |
[email protected] | 5ac670a | 2010-10-04 23:07:40 | [diff] [blame] | 107 | |
| 108 | std::wstring sub_key(policy::kRegistrySubKey); |
| 109 | sub_key += L"\\"; |
| 110 | sub_key += ASCIIToWide(policy::key::kChromeFrameContentTypes); |
| 111 | |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 112 | content_type_list->clear(); |
[email protected] | f6b8ce3 | 2011-03-02 00:03:18 | [diff] [blame] | 113 | for (int i = 0; i < arraysize(kRootKeys) && content_type_list->empty(); |
[email protected] | 5ac670a | 2010-10-04 23:07:40 | [diff] [blame] | 114 | ++i) { |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 115 | EnumerateKeyValues(kRootKeys[i], sub_key.c_str(), content_type_list); |
[email protected] | 5ac670a | 2010-10-04 23:07:40 | [diff] [blame] | 116 | } |
[email protected] | 55604d7 | 2010-09-15 14:41:48 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 119 | // static |
| 120 | void 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] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 129 | 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] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 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; |
| 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] | 687b960 | 2010-12-08 10:43:08 | [diff] [blame] | 159 | // static |
| 160 | PolicySettings* PolicySettings::GetInstance() { |
| 161 | return Singleton<PolicySettings>::get(); |
| 162 | } |