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 bool WriteDevicePolicyFile() { | |
| 23 // Create minimal stub device policy file and drop it at the place where | |
|
achuithb
2016/11/30 20:22:49
This comment may be better served as a function co
Thiemo Nagel
2016/12/01 09:52:04
Good point. Done.
| |
| 24 // SessionManagerClientStubImpl is looking for it. | |
| 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 static_assert(std::numeric_limits<int>::max() <= | |
|
achuithb
2016/11/30 20:22:49
What's your concern here?
At a minimum we need a
Thiemo Nagel
2016/12/01 09:52:04
I was afraid that a narrowing cast could lead to u
| |
| 51 std::numeric_limits<std::string::size_type>::max(), | |
| 52 "unsafe cast"); | |
| 53 return static_cast<std::string::size_type>(bytes_written) == | |
| 54 serialized_response.size(); | |
| 55 } | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 6 namespace chromeos { | 59 namespace chromeos { |
| 7 | 60 |
| 8 FakeAuthPolicyClient::FakeAuthPolicyClient() {} | 61 FakeAuthPolicyClient::FakeAuthPolicyClient() : weak_factory_(this) {} |
| 9 | 62 |
| 10 FakeAuthPolicyClient::~FakeAuthPolicyClient() {} | 63 FakeAuthPolicyClient::~FakeAuthPolicyClient() {} |
| 11 | 64 |
| 12 void FakeAuthPolicyClient::Init(dbus::Bus* bus) {} | 65 void FakeAuthPolicyClient::Init(dbus::Bus* bus) {} |
| 13 | 66 |
| 14 void FakeAuthPolicyClient::JoinAdDomain(const std::string& machine_name, | 67 void FakeAuthPolicyClient::JoinAdDomain(const std::string& machine_name, |
| 15 const std::string& user, | 68 const std::string& user, |
| 16 int password_fd, | 69 int password_fd, |
| 17 const JoinCallback& callback) { | 70 const JoinCallback& callback) { |
| 18 callback.Run(authpolicy::AD_JOIN_ERROR_NONE); | 71 callback.Run(authpolicy::AD_JOIN_ERROR_NONE); |
| 19 } | 72 } |
| 20 | 73 |
| 21 void FakeAuthPolicyClient::RefreshDevicePolicy( | 74 void FakeAuthPolicyClient::RefreshDevicePolicy( |
| 22 const RefreshPolicyCallback& callback) { | 75 const RefreshPolicyCallback& callback) { |
| 23 callback.Run(true); | 76 if (!base::PostTaskAndReplyWithResult( |
| 24 }; | 77 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(), |
| 78 FROM_HERE, base::Bind(&WriteDevicePolicyFile), | |
| 79 base::Bind(&FakeAuthPolicyClient::OnDevicePolicyFileWritten, | |
|
achuithb
2016/11/30 20:22:50
Curious: do we need OnDevicePolicyFileWritten? Can
achuithb
2016/11/30 20:26:18
Ok, I don't think just passing callback will work,
Thiemo Nagel
2016/12/01 09:52:04
Good point, and passing the callback actually does
achuithb
2016/12/01 20:26:14
I'll never understand the voodoo of functors.
| |
| 80 weak_factory_.GetWeakPtr(), callback))) { | |
| 81 callback.Run(false); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 void FakeAuthPolicyClient::OnDevicePolicyFileWritten( | |
| 86 const RefreshPolicyCallback& callback, | |
| 87 bool success) { | |
| 88 callback.Run(success); | |
| 89 } | |
| 25 | 90 |
| 26 void FakeAuthPolicyClient::RefreshUserPolicy( | 91 void FakeAuthPolicyClient::RefreshUserPolicy( |
| 27 const std::string& account_id, | 92 const std::string& account_id, |
| 28 const RefreshPolicyCallback& callback) { | 93 const RefreshPolicyCallback& callback) { |
| 29 callback.Run(true); | 94 callback.Run(true); |
| 30 }; | 95 } |
| 31 | 96 |
| 32 } // namespace chromeos | 97 } // namespace chromeos |
| OLD | NEW |