| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/edk/embedder/platform_channel_pair.h" | 5 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/posix/global_descriptors.h" | 13 #include "base/posix/global_descriptors.h" |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 16 #include "mojo/edk/platform/platform_handle.h" | 15 #include "mojo/edk/platform/platform_handle.h" |
| 16 #include "mojo/edk/util/string_number_conversions.h" |
| 17 | 17 |
| 18 using mojo::platform::PlatformHandle; | 18 using mojo::platform::PlatformHandle; |
| 19 using mojo::platform::ScopedPlatformHandle; | 19 using mojo::platform::ScopedPlatformHandle; |
| 20 using mojo::util::NumberToString; |
| 21 using mojo::util::StringToNumberWithError; |
| 20 | 22 |
| 21 namespace mojo { | 23 namespace mojo { |
| 22 namespace embedder { | 24 namespace embedder { |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 bool IsTargetDescriptorUsed( | 28 bool IsTargetDescriptorUsed( |
| 27 const base::FileHandleMappingVector& file_handle_mapping, | 29 const base::FileHandleMappingVector& file_handle_mapping, |
| 28 int target_fd) { | 30 int target_fd) { |
| 29 for (size_t i = 0; i < file_handle_mapping.size(); i++) { | 31 for (size_t i = 0; i < file_handle_mapping.size(); i++) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 } | 73 } |
| 72 | 74 |
| 73 ScopedPlatformHandle PlatformChannelPair::PassClientHandle() { | 75 ScopedPlatformHandle PlatformChannelPair::PassClientHandle() { |
| 74 return client_handle_.Pass(); | 76 return client_handle_.Pass(); |
| 75 } | 77 } |
| 76 | 78 |
| 77 // static | 79 // static |
| 78 ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess( | 80 ScopedPlatformHandle PlatformChannelPair::PassClientHandleFromParentProcess( |
| 79 const std::string& string_from_parent) { | 81 const std::string& string_from_parent) { |
| 80 int client_fd = -1; | 82 int client_fd = -1; |
| 81 if (!base::StringToInt(string_from_parent, &client_fd)) { | 83 if (!StringToNumberWithError<int>(string_from_parent, &client_fd)) { |
| 82 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch; | 84 LOG(ERROR) << "Missing or invalid --" << kMojoPlatformChannelHandleSwitch; |
| 83 return ScopedPlatformHandle(); | 85 return ScopedPlatformHandle(); |
| 84 } | 86 } |
| 85 | 87 |
| 86 return ScopedPlatformHandle(PlatformHandle(client_fd)); | 88 return ScopedPlatformHandle(PlatformHandle(client_fd)); |
| 87 } | 89 } |
| 88 | 90 |
| 89 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess( | 91 void PlatformChannelPair::PrepareToPassClientHandleToChildProcess( |
| 90 std::string* string_for_child, | 92 std::string* string_for_child, |
| 91 HandlePassingInformation* handle_passing_info) const { | 93 HandlePassingInformation* handle_passing_info) const { |
| 92 DCHECK(string_for_child); | 94 DCHECK(string_for_child); |
| 93 DCHECK(handle_passing_info); | 95 DCHECK(handle_passing_info); |
| 94 // This is an arbitrary sanity check. (Note that this guarantees that the loop | 96 // This is an arbitrary sanity check. (Note that this guarantees that the loop |
| 95 // below will terminate sanely.) | 97 // below will terminate sanely.) |
| 96 CHECK_LT(handle_passing_info->size(), 1000u); | 98 CHECK_LT(handle_passing_info->size(), 1000u); |
| 97 | 99 |
| 98 DCHECK(client_handle_.is_valid()); | 100 DCHECK(client_handle_.is_valid()); |
| 99 | 101 |
| 100 // Find a suitable FD to map our client handle to in the child process. | 102 // Find a suitable FD to map our client handle to in the child process. |
| 101 // This has quadratic time complexity in the size of |*handle_passing_info|, | 103 // This has quadratic time complexity in the size of |*handle_passing_info|, |
| 102 // but |*handle_passing_info| should be very small (usually/often empty). | 104 // but |*handle_passing_info| should be very small (usually/often empty). |
| 103 int target_fd = base::GlobalDescriptors::kBaseDescriptor; | 105 int target_fd = base::GlobalDescriptors::kBaseDescriptor; |
| 104 while (IsTargetDescriptorUsed(*handle_passing_info, target_fd)) | 106 while (IsTargetDescriptorUsed(*handle_passing_info, target_fd)) |
| 105 target_fd++; | 107 target_fd++; |
| 106 | 108 |
| 107 handle_passing_info->push_back( | 109 handle_passing_info->push_back( |
| 108 std::pair<int, int>(client_handle_.get().fd, target_fd)); | 110 std::pair<int, int>(client_handle_.get().fd, target_fd)); |
| 109 *string_for_child = base::IntToString(target_fd); | 111 *string_for_child = NumberToString<int>(target_fd); |
| 110 } | 112 } |
| 111 | 113 |
| 112 void PlatformChannelPair::ChildProcessLaunched() { | 114 void PlatformChannelPair::ChildProcessLaunched() { |
| 113 DCHECK(client_handle_.is_valid()); | 115 DCHECK(client_handle_.is_valid()); |
| 114 client_handle_.reset(); | 116 client_handle_.reset(); |
| 115 } | 117 } |
| 116 | 118 |
| 117 } // namespace embedder | 119 } // namespace embedder |
| 118 } // namespace mojo | 120 } // namespace mojo |
| OLD | NEW |