Home | History | Annotate | Download | only in chrome_frame
      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.
      4 
      5 #include "chrome_frame/policy_settings.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "base/logging.h"
     10 #include "base/strings/string_util.h"
     11 #include "base/strings/utf_string_conversions.h"
     12 #include "base/win/registry.h"
     13 #include "chrome_frame/utils.h"
     14 #include "policy/policy_constants.h"
     15 
     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 
     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 
     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 
     58 const CommandLine& PolicySettings::AdditionalLaunchParameters() const {
     59   return additional_launch_parameters_;
     60 }
     61 
     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);
     68 
     69   *default_renderer = RENDERER_NOT_SPECIFIED;
     70   renderer_exclusion_list->clear();
     71 
     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;
     82     }
     83   }
     84 
     85   DCHECK(value == RENDERER_NOT_SPECIFIED ||
     86          value == RENDER_IN_HOST ||
     87          value == RENDER_IN_CHROME_FRAME) <<
     88       "invalid default renderer setting: " << value;
     89 
     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;
     97 
     98     EnumerateKeyValues(config_key.Handle(),
     99         ASCIIToWide(exclusion_list_name).c_str(), renderer_exclusion_list);
    100 
    101     DVLOG(1) << "Default renderer as specified via policy: "
    102              << *default_renderer
    103              << " exclusion list size: " << renderer_exclusion_list->size();
    104   }
    105 }
    106 
    107 // static
    108 void PolicySettings::ReadContentTypeSetting(
    109     std::vector<std::wstring>* content_type_list) {
    110   DCHECK(content_type_list);
    111 
    112   std::wstring sub_key(policy::kRegistryChromePolicyKey);
    113   sub_key += L"\\";
    114   sub_key += ASCIIToWide(policy::key::kChromeFrameContentTypes);
    115 
    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);
    120   }
    121 }
    122 
    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;
    136     }
    137   }
    138 }
    139 
    140 // static
    141 void PolicySettings::ReadBoolSetting(const char* value_name, bool* value) {
    142   DCHECK(value);
    143   base::win::RegKey config_key;
    144   string16 value_name_str(ASCIIToWide(value_name));
    145   DWORD dword_value = 0;
    146   for (int i = 0; i < arraysize(kRootKeys); ++i) {
    147     if ((config_key.Open(kRootKeys[i], policy::kRegistryChromePolicyKey,
    148                          KEY_QUERY_VALUE) == ERROR_SUCCESS) &&
    149         (config_key.ReadValueDW(value_name_str.c_str(),
    150                                 &dword_value) == ERROR_SUCCESS)) {
    151       *value = (dword_value != 0);
    152       break;
    153     }
    154   }
    155 }
    156 
    157 void PolicySettings::RefreshFromRegistry() {
    158   RendererForUrl default_renderer;
    159   std::vector<std::wstring> renderer_exclusion_list;
    160   std::vector<std::wstring> content_type_list;
    161   std::wstring application_locale;
    162   CommandLine additional_launch_parameters(CommandLine::NO_PROGRAM);
    163   std::wstring additional_parameters_str;
    164   bool suppress_turndown_prompt = false;
    165 
    166   // Read the latest settings from the registry
    167   ReadUrlSettings(&default_renderer, &renderer_exclusion_list);
    168   ReadContentTypeSetting(&content_type_list);
    169   ReadStringSetting(policy::key::kApplicationLocaleValue, &application_locale);
    170   ReadStringSetting(policy::key::kAdditionalLaunchParameters,
    171                     &additional_parameters_str);
    172   if (!additional_parameters_str.empty()) {
    173     additional_parameters_str.insert(0, L"fake.exe ");
    174     additional_launch_parameters.ParseFromString(additional_parameters_str);
    175   }
    176   ReadBoolSetting(policy::key::kSuppressChromeFrameTurndownPrompt,
    177                   &suppress_turndown_prompt);
    178 
    179   // Nofail swap in the new values.  (Note: this is all that need be protected
    180   // under a mutex if/when this becomes thread safe.)
    181   using std::swap;
    182 
    183   swap(default_renderer_, default_renderer);
    184   swap(renderer_exclusion_list_, renderer_exclusion_list);
    185   swap(content_type_list_, content_type_list);
    186   swap(application_locale_, application_locale);
    187   swap(additional_launch_parameters_, additional_launch_parameters);
    188   swap(suppress_turndown_prompt_, suppress_turndown_prompt);
    189 }
    190 
    191 // static
    192 PolicySettings* PolicySettings::GetInstance() {
    193   return Singleton<PolicySettings>::get();
    194 }
    195