| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/shell/external_application_registrar_connection.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "mojo/edk/embedder/channel_init.h" | |
| 12 #include "mojo/public/cpp/bindings/error_handler.h" | |
| 13 #include "mojo/public/interfaces/application/application.mojom.h" | |
| 14 #include "mojo/public/interfaces/application/shell.mojom.h" | |
| 15 #include "mojo/shell/domain_socket/net_errors.h" | |
| 16 #include "mojo/shell/domain_socket/socket_descriptor.h" | |
| 17 #include "mojo/shell/domain_socket/unix_domain_client_socket_posix.h" | |
| 18 #include "mojo/shell/external_application_registrar.mojom.h" | |
| 19 #include "url/gurl.h" | |
| 20 | |
| 21 namespace mojo { | |
| 22 namespace shell { | |
| 23 | |
| 24 ExternalApplicationRegistrarConnection::ExternalApplicationRegistrarConnection( | |
| 25 const base::FilePath& socket_path) | |
| 26 : client_socket_(new UnixDomainClientSocket(socket_path.value(), false)) { | |
| 27 } | |
| 28 | |
| 29 ExternalApplicationRegistrarConnection:: | |
| 30 ~ExternalApplicationRegistrarConnection() { | |
| 31 channel_init_.WillDestroySoon(); | |
| 32 } | |
| 33 | |
| 34 void ExternalApplicationRegistrarConnection::OnConnectionError() { | |
| 35 channel_init_.WillDestroySoon(); | |
| 36 } | |
| 37 | |
| 38 void ExternalApplicationRegistrarConnection::Connect( | |
| 39 const CompletionCallback& callback) { | |
| 40 DCHECK(client_socket_) << "Single use only."; | |
| 41 int rv = client_socket_->Connect( | |
| 42 base::Bind(&ExternalApplicationRegistrarConnection::OnConnect, | |
| 43 base::Unretained(this), | |
| 44 callback)); | |
| 45 if (rv != net::ERR_IO_PENDING) { | |
| 46 DVLOG(1) << "Connect returning immediately: " << net::ErrorToString(rv); | |
| 47 OnConnect(callback, rv); | |
| 48 return; | |
| 49 } | |
| 50 DVLOG(1) << "Waiting for connection."; | |
| 51 } | |
| 52 | |
| 53 void ExternalApplicationRegistrarConnection::Register( | |
| 54 const GURL& app_url, | |
| 55 base::Callback<void(ShellPtr)> register_complete_callback) { | |
| 56 DCHECK(!client_socket_); | |
| 57 registrar_->Register(String::From(app_url), register_complete_callback); | |
| 58 } | |
| 59 | |
| 60 void ExternalApplicationRegistrarConnection::OnConnect( | |
| 61 CompletionCallback callback, | |
| 62 int rv) { | |
| 63 DVLOG(1) << "OnConnect called: " << net::ErrorToString(rv); | |
| 64 if (rv != net::OK) { | |
| 65 callback.Run(rv); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 mojo::ScopedMessagePipeHandle ptr_message_pipe_handle = | |
| 70 channel_init_.Init(client_socket_->ReleaseConnectedSocket(), | |
| 71 base::MessageLoopProxy::current()); | |
| 72 CHECK(ptr_message_pipe_handle.is_valid()); | |
| 73 client_socket_.reset(); // This is dead now, ensure it can't be reused. | |
| 74 | |
| 75 registrar_.Bind(ptr_message_pipe_handle.Pass()); | |
| 76 callback.Run(rv); | |
| 77 } | |
| 78 | |
| 79 } // namespace shell | |
| 80 } // namespace mojo | |
| OLD | NEW |