Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(507)

Side by Side Diff: mojo/system/platform_channel_posix.cc

Issue 134373005: Mojo: Refactor PlatformChannel stuff. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/system/platform_channel.cc ('k') | mojo/system/raw_channel_posix_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "mojo/system/platform_channel.h" 5 #include "mojo/system/platform_channel.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/command_line.h" 12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/logging.h" 13 #include "base/logging.h"
15 #include "base/posix/global_descriptors.h" 14 #include "base/posix/global_descriptors.h"
16 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
17 16
18 namespace mojo { 17 namespace mojo {
19 namespace system { 18 namespace system {
20 19
21 namespace { 20 namespace {
22 21
23 const char kMojoChannelDescriptorSwitch[] = "mojo-channel-descriptor"; 22 const char kMojoChannelDescriptorSwitch[] = "mojo-channel-descriptor";
24 23
25 bool IsTargetDescriptorUsed( 24 bool IsTargetDescriptorUsed(
26 const base::FileHandleMappingVector& file_handle_mapping, 25 const base::FileHandleMappingVector& file_handle_mapping,
27 int target_fd) { 26 int target_fd) {
28 for (size_t i = 0; i < file_handle_mapping.size(); i++) { 27 for (size_t i = 0; i < file_handle_mapping.size(); i++) {
29 if (file_handle_mapping[i].second == target_fd) 28 if (file_handle_mapping[i].second == target_fd)
30 return true; 29 return true;
31 } 30 }
32 return false; 31 return false;
33 } 32 }
34 33
35 class PlatformServerChannelPosix : public PlatformServerChannel { 34 } // namespace
36 public:
37 PlatformServerChannelPosix(const std::string& name);
38 virtual ~PlatformServerChannelPosix();
39 35
40 // |PlatformServerChannel| implementation: 36 PlatformChannelPair::PlatformChannelPair() {
41 virtual scoped_ptr<PlatformClientChannel> CreateClientChannel() OVERRIDE;
42 virtual void GetDataNeededToPassClientChannelToChildProcess(
43 CommandLine* command_line,
44 base::FileHandleMappingVector* file_handle_mapping) const OVERRIDE;
45 virtual void ChildProcessLaunched() OVERRIDE;
46
47 private:
48 PlatformChannelHandle client_handle_;
49
50 DISALLOW_COPY_AND_ASSIGN(PlatformServerChannelPosix);
51 };
52
53 PlatformServerChannelPosix::PlatformServerChannelPosix(
54 const std::string& name)
55 : PlatformServerChannel(name) {
56 // Create the Unix domain socket and set the ends to nonblocking. 37 // Create the Unix domain socket and set the ends to nonblocking.
57 int fds[2]; 38 int fds[2];
39 // TODO(vtl): Maybe fail gracefully if |socketpair()| fails.
58 PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0); 40 PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);
59 PCHECK(fcntl(fds[0], F_SETFL, O_NONBLOCK) == 0); 41 PCHECK(fcntl(fds[0], F_SETFL, O_NONBLOCK) == 0);
60 PCHECK(fcntl(fds[1], F_SETFL, O_NONBLOCK) == 0); 42 PCHECK(fcntl(fds[1], F_SETFL, O_NONBLOCK) == 0);
61 43
62 mutable_handle()->fd = fds[0]; 44 server_handle_.fd = fds[0];
63 DCHECK(is_valid()); 45 DCHECK(server_handle_.is_valid());
64 client_handle_.fd = fds[1]; 46 client_handle_.fd = fds[1];
65 DCHECK(client_handle_.is_valid()); 47 DCHECK(client_handle_.is_valid());
66 } 48 }
67 49
68 PlatformServerChannelPosix::~PlatformServerChannelPosix() { 50 // static
69 client_handle_.CloseIfNecessary(); 51 scoped_ptr<PlatformChannel>
52 PlatformChannelPair::CreateClientChannelFromParentProcess(
53 const CommandLine& command_line) {
54 std::string client_fd_string =
55 command_line.GetSwitchValueASCII(kMojoChannelDescriptorSwitch);
56 int client_fd = -1;
57 if (client_fd_string.empty() ||
58 !base::StringToInt(client_fd_string, &client_fd) ||
59 client_fd < base::GlobalDescriptors::kBaseDescriptor) {
60 LOG(ERROR) << "Missing or invalid --" << kMojoChannelDescriptorSwitch;
61 return scoped_ptr<PlatformChannel>();
62 }
63
64 return PlatformChannel::CreateFromHandle(PlatformChannelHandle(client_fd));
70 } 65 }
71 66
72 scoped_ptr<PlatformClientChannel> 67 void PlatformChannelPair::PrepareToPassClientChannelToChildProcess(
73 PlatformServerChannelPosix::CreateClientChannel() {
74 if (!client_handle_.is_valid()) {
75 NOTREACHED();
76 return scoped_ptr<PlatformClientChannel>();
77 }
78
79 scoped_ptr<PlatformClientChannel> rv =
80 PlatformClientChannel::CreateFromHandle(client_handle_);
81 DCHECK(rv->is_valid());
82 client_handle_ = PlatformChannelHandle();
83 return rv.Pass();
84 }
85
86 void PlatformServerChannelPosix::GetDataNeededToPassClientChannelToChildProcess(
87 CommandLine* command_line, 68 CommandLine* command_line,
88 base::FileHandleMappingVector* file_handle_mapping) const { 69 base::FileHandleMappingVector* file_handle_mapping) const {
89 DCHECK(command_line); 70 DCHECK(command_line);
90 DCHECK(file_handle_mapping); 71 DCHECK(file_handle_mapping);
91 // This is an arbitrary sanity check. (Note that this guarantees that the loop 72 // This is an arbitrary sanity check. (Note that this guarantees that the loop
92 // below will terminate sanely.) 73 // below will terminate sanely.)
93 CHECK_LT(file_handle_mapping->size(), 1000u); 74 CHECK_LT(file_handle_mapping->size(), 1000u);
94 75
95 DCHECK(client_handle_.is_valid()); 76 DCHECK(client_handle_.is_valid());
96 77
(...skipping 12 matching lines...) Expand all
109 LOG_IF(WARNING, command_line->HasSwitch(kMojoChannelDescriptorSwitch)) 90 LOG_IF(WARNING, command_line->HasSwitch(kMojoChannelDescriptorSwitch))
110 << "Child command line already has switch --" 91 << "Child command line already has switch --"
111 << kMojoChannelDescriptorSwitch << "=" 92 << kMojoChannelDescriptorSwitch << "="
112 << command_line->GetSwitchValueASCII(kMojoChannelDescriptorSwitch); 93 << command_line->GetSwitchValueASCII(kMojoChannelDescriptorSwitch);
113 // (Any existing switch won't actually be removed from the command line, but 94 // (Any existing switch won't actually be removed from the command line, but
114 // the last one appended takes precedence.) 95 // the last one appended takes precedence.)
115 command_line->AppendSwitchASCII(kMojoChannelDescriptorSwitch, 96 command_line->AppendSwitchASCII(kMojoChannelDescriptorSwitch,
116 base::IntToString(target_fd)); 97 base::IntToString(target_fd));
117 } 98 }
118 99
119 void PlatformServerChannelPosix::ChildProcessLaunched() { 100 void PlatformChannelPair::ChildProcessLaunched() {
120 DCHECK(client_handle_.is_valid()); 101 DCHECK(client_handle_.is_valid());
121 client_handle_.CloseIfNecessary(); 102 client_handle_.CloseIfNecessary();
122 } 103 }
123 104
124 } // namespace
125
126 // -----------------------------------------------------------------------------
127
128 // Static factory method declared in platform_channel.h.
129 // static
130 scoped_ptr<PlatformServerChannel> PlatformServerChannel::Create(
131 const std::string& name) {
132 return scoped_ptr<PlatformServerChannel>(
133 new PlatformServerChannelPosix(name));
134 }
135
136 // -----------------------------------------------------------------------------
137
138 // Static factory method declared in platform_channel.h.
139 // static
140 scoped_ptr<PlatformClientChannel>
141 PlatformClientChannel::CreateFromParentProcess(
142 const CommandLine& command_line) {
143 std::string client_fd_string =
144 command_line.GetSwitchValueASCII(kMojoChannelDescriptorSwitch);
145 int client_fd = -1;
146 if (client_fd_string.empty() ||
147 !base::StringToInt(client_fd_string, &client_fd) ||
148 client_fd < base::GlobalDescriptors::kBaseDescriptor) {
149 LOG(ERROR) << "Missing or invalid --" << kMojoChannelDescriptorSwitch;
150 return scoped_ptr<PlatformClientChannel>();
151 }
152
153 return CreateFromHandle(PlatformChannelHandle(client_fd));
154 }
155
156 } // namespace system 105 } // namespace system
157 } // namespace mojo 106 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/platform_channel.cc ('k') | mojo/system/raw_channel_posix_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698