• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "mojo/public/cpp/platform/named_platform_channel.h"
6 
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 
10 namespace mojo {
11 
12 const char NamedPlatformChannel::kNamedHandleSwitch[] =
13     "mojo-named-platform-channel-pipe";
14 
NamedPlatformChannel(const Options & options)15 NamedPlatformChannel::NamedPlatformChannel(const Options& options) {
16   server_endpoint_ = PlatformChannelServerEndpoint(
17       CreateServerEndpoint(options, &server_name_));
18 }
19 
20 NamedPlatformChannel::NamedPlatformChannel(NamedPlatformChannel&& other) =
21     default;
22 
23 NamedPlatformChannel::~NamedPlatformChannel() = default;
24 
25 NamedPlatformChannel& NamedPlatformChannel::operator=(
26     NamedPlatformChannel&& other) = default;
27 
28 // static
ServerNameFromUTF8(base::StringPiece name)29 NamedPlatformChannel::ServerName NamedPlatformChannel::ServerNameFromUTF8(
30     base::StringPiece name) {
31 #if defined(OS_WIN)
32   return base::UTF8ToUTF16(name);
33 #else
34   return name.as_string();
35 #endif
36 }
37 
PassServerNameOnCommandLine(base::CommandLine * command_line)38 void NamedPlatformChannel::PassServerNameOnCommandLine(
39     base::CommandLine* command_line) {
40   command_line->AppendSwitchNative(kNamedHandleSwitch, server_name_);
41 }
42 
43 // static
ConnectToServer(const ServerName & server_name)44 PlatformChannelEndpoint NamedPlatformChannel::ConnectToServer(
45     const ServerName& server_name) {
46   DCHECK(!server_name.empty());
47   return CreateClientEndpoint(server_name);
48 }
49 
50 // static
ConnectToServer(const base::CommandLine & command_line)51 PlatformChannelEndpoint NamedPlatformChannel::ConnectToServer(
52     const base::CommandLine& command_line) {
53   ServerName name = command_line.GetSwitchValueNative(kNamedHandleSwitch);
54   if (name.empty())
55     return PlatformChannelEndpoint();
56   return ConnectToServer(name);
57 }
58 
59 }  // namespace mojo
60