OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/cloud/component_cloud_policy_updater.h" | 5 #include "chrome/browser/policy/cloud/component_cloud_policy_updater.h" |
6 | 6 |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
11 #include "base/logging.h" | 9 #include "base/logging.h" |
12 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
13 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
14 #include "chrome/browser/policy/cloud/component_cloud_policy_store.h" | 12 #include "chrome/browser/policy/cloud/component_cloud_policy_store.h" |
15 #include "chrome/browser/policy/cloud/external_policy_data_fetcher.h" | 13 #include "chrome/browser/policy/cloud/external_policy_data_fetcher.h" |
16 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" | 14 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" |
17 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | 15 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" |
18 #include "components/policy/core/common/policy_namespace.h" | |
19 | 16 |
20 namespace em = enterprise_management; | 17 namespace em = enterprise_management; |
21 | 18 |
22 namespace policy { | 19 namespace policy { |
23 | 20 |
24 namespace { | 21 namespace { |
25 | 22 |
26 // The maximum size of the serialized policy protobuf. | 23 // The maximum size of the serialized policy protobuf. |
27 const size_t kPolicyProtoMaxSize = 16 * 1024; | 24 const size_t kPolicyProtoMaxSize = 16 * 1024; |
28 | 25 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // Validate the policy before doing anything else. | 57 // Validate the policy before doing anything else. |
61 PolicyNamespace ns; | 58 PolicyNamespace ns; |
62 em::ExternalPolicyData data; | 59 em::ExternalPolicyData data; |
63 if (!store_->ValidatePolicy(response.Pass(), &ns, &data)) { | 60 if (!store_->ValidatePolicy(response.Pass(), &ns, &data)) { |
64 LOG(ERROR) << "Failed to validate component policy fetched from DMServer"; | 61 LOG(ERROR) << "Failed to validate component policy fetched from DMServer"; |
65 return; | 62 return; |
66 } | 63 } |
67 | 64 |
68 // Maybe the data for this hash has already been downloaded and cached. | 65 // Maybe the data for this hash has already been downloaded and cached. |
69 const std::string& cached_hash = store_->GetCachedHash(ns); | 66 const std::string& cached_hash = store_->GetCachedHash(ns); |
70 if (!cached_hash.empty() && data.secure_hash() == cached_hash) { | 67 if (!cached_hash.empty() && data.secure_hash() == cached_hash) |
71 return; | 68 return; |
72 } | |
73 | 69 |
74 // TODO(joaodasilva): implement the other two auth methods. | 70 // TODO(joaodasilva): implement the other two auth methods. |
75 if (data.download_auth_method() != em::ExternalPolicyData::NONE) | 71 if (data.download_auth_method() != em::ExternalPolicyData::NONE) |
76 return; | 72 return; |
77 | 73 |
78 // Encode |ns| into a string |key|. | 74 const std::string key = NamespaceToKey(ns); |
79 const std::string domain = base::IntToString(ns.domain); | |
80 const std::string key = | |
81 base::IntToString(domain.size()) + ":" + domain + ":" + ns.component_id; | |
82 | 75 |
83 if (data.download_url().empty() || !data.has_secure_hash()) { | 76 if (data.download_url().empty() || !data.has_secure_hash()) { |
84 // If there is no policy for this component or the policy has been removed, | 77 // If there is no policy for this component or the policy has been removed, |
85 // cancel any existing request to fetch policy for this component. | 78 // cancel any existing request to fetch policy for this component. |
86 external_policy_data_updater_.CancelExternalDataFetch(key); | 79 external_policy_data_updater_.CancelExternalDataFetch(key); |
87 | 80 |
88 // Delete any existing policy for this component. | 81 // Delete any existing policy for this component. |
89 store_->Delete(ns); | 82 store_->Delete(ns); |
90 } else { | 83 } else { |
91 // Make a request to fetch policy for this component. If another fetch | 84 // Make a request to fetch policy for this component. If another fetch |
92 // request is already pending for the component, it will be canceled. | 85 // request is already pending for the component, it will be canceled. |
93 external_policy_data_updater_.FetchExternalData( | 86 external_policy_data_updater_.FetchExternalData( |
94 key, | 87 key, |
95 ExternalPolicyDataUpdater::Request(data.download_url(), | 88 ExternalPolicyDataUpdater::Request(data.download_url(), |
96 data.secure_hash(), | 89 data.secure_hash(), |
97 kPolicyDataMaxSize), | 90 kPolicyDataMaxSize), |
98 base::Bind(&ComponentCloudPolicyStore::Store, base::Unretained(store_), | 91 base::Bind(&ComponentCloudPolicyStore::Store, base::Unretained(store_), |
99 ns, | 92 ns, |
100 serialized_response, | 93 serialized_response, |
101 data.secure_hash())); | 94 data.secure_hash())); |
102 } | 95 } |
103 } | 96 } |
104 | 97 |
| 98 void ComponentCloudPolicyUpdater::CancelUpdate(const PolicyNamespace& ns) { |
| 99 external_policy_data_updater_.CancelExternalDataFetch(NamespaceToKey(ns)); |
| 100 } |
| 101 |
| 102 std::string ComponentCloudPolicyUpdater::NamespaceToKey( |
| 103 const PolicyNamespace& ns) { |
| 104 const std::string domain = base::IntToString(ns.domain); |
| 105 const std::string size = base::IntToString(domain.size()); |
| 106 return size + ":" + domain + ":" + ns.component_id; |
| 107 } |
| 108 |
105 } // namespace policy | 109 } // namespace policy |
OLD | NEW |