Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 #include "chromeos/dbus/fake_auth_policy_client.h" | 5 #include "chromeos/dbus/fake_auth_policy_client.h" |
| 5 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/path_service.h" | |
| 12 #include "base/task_runner_util.h" | |
| 13 #include "base/threading/worker_pool.h" | |
| 14 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" | |
| 15 #include "chromeos/chromeos_paths.h" | |
| 16 #include "components/policy/proto/device_management_backend.pb.h" | |
| 17 | |
| 18 namespace em = enterprise_management; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Create minimal stub device policy file and drop it at the place where | |
| 23 // SessionManagerClientStubImpl is looking for it. | |
| 24 bool WriteDevicePolicyFile() { | |
| 25 em::ChromeDeviceSettingsProto policy; | |
| 26 em::PolicyData data; | |
| 27 policy.SerializeToString(data.mutable_policy_value()); | |
| 28 data.set_policy_type("google/chromeos/device"); | |
| 29 | |
| 30 em::PolicyFetchResponse response; | |
| 31 data.SerializeToString(response.mutable_policy_data()); | |
| 32 std::string serialized_response; | |
| 33 response.SerializeToString(&serialized_response); | |
| 34 | |
| 35 base::FilePath owner_key_path; | |
| 36 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)) | |
| 37 return false; | |
| 38 | |
| 39 const base::FilePath device_policy_path = | |
| 40 owner_key_path.DirName().AppendASCII("stub_device_policy"); | |
| 41 | |
| 42 // Note that in theory there could be a short time window in which a | |
| 43 // concurrent reader sees a partial (and thus invalid) file, but given the | |
| 44 // small file size that seems very unlikely in practice. | |
| 45 const int bytes_written = | |
| 46 base::WriteFile(device_policy_path, serialized_response.c_str(), | |
| 47 serialized_response.size()); | |
| 48 if (bytes_written < 0) | |
| 49 return false; | |
| 50 return bytes_written == static_cast<int>(serialized_response.size()); | |
|
achuithb
2016/12/01 20:26:14
It's a matter of taste, but worth considering comb
Thiemo Nagel
2016/12/01 20:47:21
I find the way I wrote it much clearer which prove
| |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 6 namespace chromeos { | 55 namespace chromeos { |
| 7 | 56 |
| 8 FakeAuthPolicyClient::FakeAuthPolicyClient() {} | 57 FakeAuthPolicyClient::FakeAuthPolicyClient() {} |
| 9 | 58 |
| 10 FakeAuthPolicyClient::~FakeAuthPolicyClient() {} | 59 FakeAuthPolicyClient::~FakeAuthPolicyClient() {} |
| 11 | 60 |
| 12 void FakeAuthPolicyClient::Init(dbus::Bus* bus) {} | 61 void FakeAuthPolicyClient::Init(dbus::Bus* bus) {} |
| 13 | 62 |
| 14 void FakeAuthPolicyClient::JoinAdDomain(const std::string& machine_name, | 63 void FakeAuthPolicyClient::JoinAdDomain(const std::string& machine_name, |
| 15 const std::string& user, | 64 const std::string& user, |
| 16 int password_fd, | 65 int password_fd, |
| 17 const JoinCallback& callback) { | 66 const JoinCallback& callback) { |
| 18 callback.Run(authpolicy::AD_JOIN_ERROR_NONE); | 67 callback.Run(authpolicy::AD_JOIN_ERROR_NONE); |
| 19 } | 68 } |
| 20 | 69 |
| 21 void FakeAuthPolicyClient::RefreshDevicePolicy( | 70 void FakeAuthPolicyClient::RefreshDevicePolicy( |
| 22 const RefreshPolicyCallback& callback) { | 71 const RefreshPolicyCallback& callback) { |
| 23 callback.Run(true); | 72 if (!base::PostTaskAndReplyWithResult( |
| 24 }; | 73 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(), |
| 74 FROM_HERE, base::Bind(&WriteDevicePolicyFile), callback)) { | |
| 75 callback.Run(false); | |
| 76 } | |
| 77 } | |
| 25 | 78 |
| 26 void FakeAuthPolicyClient::RefreshUserPolicy( | 79 void FakeAuthPolicyClient::RefreshUserPolicy( |
| 27 const std::string& account_id, | 80 const std::string& account_id, |
| 28 const RefreshPolicyCallback& callback) { | 81 const RefreshPolicyCallback& callback) { |
| 29 callback.Run(true); | 82 callback.Run(true); |
| 30 }; | 83 } |
| 31 | 84 |
| 32 } // namespace chromeos | 85 } // namespace chromeos |
| OLD | NEW |