cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / mojo / shell / external_application_registrar_connection.cc
bloba29734e3d950536ddfcf95c8bc4bb03aa9612b21
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.
5 #include "mojo/shell/external_application_registrar_connection.h"
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/external_application_registrar.mojom.h"
16 #include "net/base/net_errors.h"
17 #include "net/socket/socket_descriptor.h"
18 #include "net/socket/unix_domain_client_socket_posix.h"
19 #include "url/gurl.h"
21 namespace mojo {
22 namespace shell {
24 ExternalApplicationRegistrarConnection::ExternalApplicationRegistrarConnection(
25 const base::FilePath& socket_path)
26 : client_socket_(
27 new net::UnixDomainClientSocket(socket_path.value(), false)) {
30 ExternalApplicationRegistrarConnection::
31 ~ExternalApplicationRegistrarConnection() {
32 channel_init_.WillDestroySoon();
35 void ExternalApplicationRegistrarConnection::OnConnectionError() {
36 channel_init_.WillDestroySoon();
39 void ExternalApplicationRegistrarConnection::Connect(
40 const net::CompletionCallback& callback) {
41 DCHECK(client_socket_) << "Single use only.";
42 int rv = client_socket_->Connect(
43 base::Bind(&ExternalApplicationRegistrarConnection::OnConnect,
44 base::Unretained(this),
45 callback));
46 if (rv != net::ERR_IO_PENDING) {
47 DVLOG(1) << "Connect returning immediately: " << net::ErrorToString(rv);
48 OnConnect(callback, rv);
49 return;
51 DVLOG(1) << "Waiting for connection.";
54 void ExternalApplicationRegistrarConnection::Register(
55 const GURL& app_url,
56 ShellPtr* shell,
57 base::Closure register_complete_callback) {
58 DCHECK(!client_socket_);
59 registrar_->Register(
60 String::From(app_url), GetProxy(shell), register_complete_callback);
63 void ExternalApplicationRegistrarConnection::OnConnect(
64 net::CompletionCallback callback,
65 int rv) {
66 DVLOG(1) << "OnConnect called: " << net::ErrorToString(rv);
67 if (rv != net::OK) {
68 callback.Run(rv);
69 return;
72 mojo::ScopedMessagePipeHandle ptr_message_pipe_handle =
73 channel_init_.Init(client_socket_->ReleaseConnectedSocket(),
74 base::MessageLoopProxy::current());
75 CHECK(ptr_message_pipe_handle.is_valid());
76 client_socket_.reset(); // This is dead now, ensure it can't be reused.
78 registrar_.Bind(ptr_message_pipe_handle.Pass());
79 callback.Run(rv);
82 } // namespace shell
83 } // namespace mojo