- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / configuration_policy_provider.cc
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/browser/policy/configuration_policy_provider.h"
6
7 #include "base/callback.h"
8 #include "chrome/browser/policy/external_data_fetcher.h"
9 #include "chrome/browser/policy/policy_domain_descriptor.h"
10 #include "chrome/browser/policy/policy_map.h"
11 #include "policy/policy_constants.h"
12
13 namespace policy {
14
15 namespace {
16
17 const char* kProxyPolicies[] = {
18   key::kProxyMode,
19   key::kProxyServerMode,
20   key::kProxyServer,
21   key::kProxyPacUrl,
22   key::kProxyBypassList,
23 };
24
25 // Helper that converts deprecated chrome policies into their corresponding
26 // actual policies.
27 void FixDeprecatedPolicies(PolicyMap* policies) {
28   // Proxy settings have been configured by 5 policies that didn't mix well
29   // together, and maps of policies had to take this into account when merging
30   // policy sources. The proxy settings will eventually be configured by a
31   // single Dictionary policy when all providers have support for that. For
32   // now, the individual policies are mapped here to a single Dictionary policy
33   // that the rest of the policy machinery uses.
34
35   // The highest (level, scope) pair for an existing proxy policy is determined
36   // first, and then only policies with those exact attributes are merged.
37   PolicyMap::Entry current_priority;  // Defaults to the lowest priority.
38   scoped_ptr<DictionaryValue> proxy_settings(new DictionaryValue);
39   for (size_t i = 0; i < arraysize(kProxyPolicies); ++i) {
40     const PolicyMap::Entry* entry = policies->Get(kProxyPolicies[i]);
41     if (entry) {
42       if (entry->has_higher_priority_than(current_priority)) {
43         proxy_settings->Clear();
44         current_priority = *entry;
45       }
46       if (!entry->has_higher_priority_than(current_priority) &&
47           !current_priority.has_higher_priority_than(*entry)) {
48         proxy_settings->Set(kProxyPolicies[i], entry->value->DeepCopy());
49       }
50       policies->Erase(kProxyPolicies[i]);
51     }
52   }
53   // Sets the new |proxy_settings| if kProxySettings isn't set yet, or if the
54   // new priority is higher.
55   const PolicyMap::Entry* existing = policies->Get(key::kProxySettings);
56   if (!proxy_settings->empty() &&
57       (!existing || current_priority.has_higher_priority_than(*existing))) {
58     policies->Set(key::kProxySettings,
59                   current_priority.level,
60                   current_priority.scope,
61                   proxy_settings.release(),
62                   NULL);
63   }
64 }
65
66 }  // namespace
67
68 ConfigurationPolicyProvider::Observer::~Observer() {}
69
70 ConfigurationPolicyProvider::ConfigurationPolicyProvider()
71     : did_shutdown_(false) {}
72
73 ConfigurationPolicyProvider::~ConfigurationPolicyProvider() {
74   DCHECK(did_shutdown_);
75 }
76
77 void ConfigurationPolicyProvider::Init() {}
78
79 void ConfigurationPolicyProvider::Shutdown() {
80   did_shutdown_ = true;
81 }
82
83 bool ConfigurationPolicyProvider::IsInitializationComplete(
84     PolicyDomain domain) const {
85   return true;
86 }
87
88 void ConfigurationPolicyProvider::UpdatePolicy(
89     scoped_ptr<PolicyBundle> bundle) {
90   if (bundle.get())
91     policy_bundle_.Swap(bundle.get());
92   else
93     policy_bundle_.Clear();
94   FixDeprecatedPolicies(&policy_bundle_.Get(
95       PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())));
96   FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer,
97                     observer_list_,
98                     OnUpdatePolicy(this));
99 }
100
101 void ConfigurationPolicyProvider::AddObserver(Observer* observer) {
102   observer_list_.AddObserver(observer);
103 }
104
105 void ConfigurationPolicyProvider::RemoveObserver(Observer* observer) {
106   observer_list_.RemoveObserver(observer);
107 }
108
109 void ConfigurationPolicyProvider::RegisterPolicyDomain(
110     scoped_refptr<const PolicyDomainDescriptor> descriptor) {}
111
112 }  // namespace policy