| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chromeos/dbus/fake_permission_broker_client.h" | 5 #include "chromeos/dbus/fake_permission_broker_client.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/threading/worker_pool.h" | 17 #include "base/threading/worker_pool.h" |
| 18 #include "dbus/file_descriptor.h" | 18 #include "dbus/file_descriptor.h" |
| 19 | 19 |
| 20 namespace chromeos { | 20 namespace chromeos { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const char kOpenFailedError[] = "open_failed"; | 24 const char kOpenFailedError[] = "open_failed"; |
| 25 | 25 |
| 26 // So that real devices can be accessed by tests and "Chromium OS on Linux" this | 26 // So that real devices can be accessed by tests and "Chromium OS on Linux" this |
| 27 // function implements a simplified version of the method implemented by the | 27 // function implements a simplified version of the method implemented by the |
| 28 // permission broker by opening the path specified and returning the resulting | 28 // permission broker by opening the path specified and returning the resulting |
| 29 // file descriptor. | 29 // file descriptor. |
| 30 void OpenPathAndValidate( | 30 void OpenPath(const std::string& path, |
| 31 const std::string& path, | 31 const PermissionBrokerClient::OpenPathCallback& callback, |
| 32 const PermissionBrokerClient::OpenPathCallback& callback, | 32 const PermissionBrokerClient::ErrorCallback& error_callback, |
| 33 const PermissionBrokerClient::ErrorCallback& error_callback, | 33 scoped_refptr<base::TaskRunner> task_runner) { |
| 34 scoped_refptr<base::TaskRunner> task_runner) { | 34 base::ScopedFD fd(HANDLE_EINTR(open(path.c_str(), O_RDWR))); |
| 35 int fd = HANDLE_EINTR(open(path.c_str(), O_RDWR)); | 35 if (!fd.is_valid()) { |
| 36 if (fd < 0) { | |
| 37 int error_code = logging::GetLastSystemErrorCode(); | 36 int error_code = logging::GetLastSystemErrorCode(); |
| 38 task_runner->PostTask( | 37 task_runner->PostTask( |
| 39 FROM_HERE, | 38 FROM_HERE, |
| 40 base::Bind(error_callback, kOpenFailedError, | 39 base::Bind(error_callback, kOpenFailedError, |
| 41 base::StringPrintf( | 40 base::StringPrintf( |
| 42 "Failed to open '%s': %s", path.c_str(), | 41 "Failed to open '%s': %s", path.c_str(), |
| 43 logging::SystemErrorCodeToString(error_code).c_str()))); | 42 logging::SystemErrorCodeToString(error_code).c_str()))); |
| 44 return; | 43 return; |
| 45 } | 44 } |
| 46 | 45 |
| 47 dbus::FileDescriptor dbus_fd; | 46 task_runner->PostTask(FROM_HERE, base::Bind(callback, base::Passed(&fd))); |
| 48 dbus_fd.PutValue(fd); | |
| 49 dbus_fd.CheckValidity(); | |
| 50 task_runner->PostTask(FROM_HERE, | |
| 51 base::Bind(callback, base::Passed(&dbus_fd))); | |
| 52 } | 47 } |
| 53 | 48 |
| 54 } // namespace | 49 } // namespace |
| 55 | 50 |
| 56 FakePermissionBrokerClient::FakePermissionBrokerClient() {} | 51 FakePermissionBrokerClient::FakePermissionBrokerClient() {} |
| 57 | 52 |
| 58 FakePermissionBrokerClient::~FakePermissionBrokerClient() {} | 53 FakePermissionBrokerClient::~FakePermissionBrokerClient() {} |
| 59 | 54 |
| 60 void FakePermissionBrokerClient::Init(dbus::Bus* bus) {} | 55 void FakePermissionBrokerClient::Init(dbus::Bus* bus) {} |
| 61 | 56 |
| 62 void FakePermissionBrokerClient::CheckPathAccess( | 57 void FakePermissionBrokerClient::CheckPathAccess( |
| 63 const std::string& path, | 58 const std::string& path, |
| 64 const ResultCallback& callback) { | 59 const ResultCallback& callback) { |
| 65 callback.Run(true); | 60 callback.Run(true); |
| 66 } | 61 } |
| 67 | 62 |
| 68 void FakePermissionBrokerClient::OpenPath(const std::string& path, | 63 void FakePermissionBrokerClient::OpenPath(const std::string& path, |
| 69 const OpenPathCallback& callback, | 64 const OpenPathCallback& callback, |
| 70 const ErrorCallback& error_callback) { | 65 const ErrorCallback& error_callback) { |
| 71 base::WorkerPool::PostTask( | 66 base::WorkerPool::PostTask( |
| 72 FROM_HERE, | 67 FROM_HERE, base::Bind(&chromeos::OpenPath, path, callback, error_callback, |
| 73 base::Bind(&OpenPathAndValidate, path, callback, error_callback, | 68 base::ThreadTaskRunnerHandle::Get()), |
| 74 base::ThreadTaskRunnerHandle::Get()), | |
| 75 false); | 69 false); |
| 76 } | 70 } |
| 77 | 71 |
| 78 void FakePermissionBrokerClient::RequestTcpPortAccess( | 72 void FakePermissionBrokerClient::RequestTcpPortAccess( |
| 79 uint16_t port, | 73 uint16_t port, |
| 80 const std::string& interface, | 74 const std::string& interface, |
| 81 const dbus::FileDescriptor& lifeline_fd, | 75 int lifeline_fd, |
| 82 const ResultCallback& callback) { | 76 const ResultCallback& callback) { |
| 83 DCHECK(lifeline_fd.is_valid()); | |
| 84 callback.Run(true); | 77 callback.Run(true); |
| 85 } | 78 } |
| 86 | 79 |
| 87 void FakePermissionBrokerClient::RequestUdpPortAccess( | 80 void FakePermissionBrokerClient::RequestUdpPortAccess( |
| 88 uint16_t port, | 81 uint16_t port, |
| 89 const std::string& interface, | 82 const std::string& interface, |
| 90 const dbus::FileDescriptor& lifeline_fd, | 83 int lifeline_fd, |
| 91 const ResultCallback& callback) { | 84 const ResultCallback& callback) { |
| 92 DCHECK(lifeline_fd.is_valid()); | |
| 93 callback.Run(true); | 85 callback.Run(true); |
| 94 } | 86 } |
| 95 | 87 |
| 96 void FakePermissionBrokerClient::ReleaseTcpPort( | 88 void FakePermissionBrokerClient::ReleaseTcpPort( |
| 97 uint16_t port, | 89 uint16_t port, |
| 98 const std::string& interface, | 90 const std::string& interface, |
| 99 const ResultCallback& callback) { | 91 const ResultCallback& callback) { |
| 100 callback.Run(true); | 92 callback.Run(true); |
| 101 } | 93 } |
| 102 | 94 |
| 103 void FakePermissionBrokerClient::ReleaseUdpPort( | 95 void FakePermissionBrokerClient::ReleaseUdpPort( |
| 104 uint16_t port, | 96 uint16_t port, |
| 105 const std::string& interface, | 97 const std::string& interface, |
| 106 const ResultCallback& callback) { | 98 const ResultCallback& callback) { |
| 107 callback.Run(true); | 99 callback.Run(true); |
| 108 } | 100 } |
| 109 | 101 |
| 110 } // namespace chromeos | 102 } // namespace chromeos |
| OLD | NEW |