OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/package_manager/content_handler_connection.h" | 5 #include "mojo/package_manager/content_handler_connection.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
8 #include "mojo/shell/application_manager.h" | 9 #include "mojo/shell/application_manager.h" |
9 #include "mojo/shell/connect_to_application_params.h" | 10 #include "mojo/shell/connect_to_application_params.h" |
10 #include "mojo/shell/identity.h" | 11 #include "mojo/shell/identity.h" |
11 | 12 |
12 namespace mojo { | 13 namespace mojo { |
13 namespace package_manager { | 14 namespace package_manager { |
14 | 15 |
15 ContentHandlerConnection::ContentHandlerConnection( | 16 ContentHandlerConnection::ContentHandlerConnection( |
16 shell::ApplicationManager* manager, | 17 shell::ApplicationManager* manager, |
17 const shell::Identity& source, | 18 const shell::Identity& source, |
18 const shell::Identity& content_handler, | 19 const shell::Identity& content_handler, |
19 uint32_t id, | 20 uint32_t id, |
20 const ClosedCallback& connection_closed_callback) | 21 const ClosedCallback& connection_closed_callback) |
21 : connection_closed_callback_(connection_closed_callback), | 22 : connection_closed_callback_(connection_closed_callback), |
22 identity_(content_handler), | 23 identity_(content_handler), |
23 connection_closed_(false), | 24 connection_closed_(false), |
24 id_(id) { | 25 id_(id), |
| 26 ref_count_(0) { |
25 ServiceProviderPtr services; | 27 ServiceProviderPtr services; |
26 | 28 |
27 scoped_ptr<shell::ConnectToApplicationParams> params( | 29 scoped_ptr<shell::ConnectToApplicationParams> params( |
28 new shell::ConnectToApplicationParams); | 30 new shell::ConnectToApplicationParams); |
29 params->set_source(source); | 31 params->set_source(source); |
30 params->SetTarget(identity_); | 32 params->SetTarget(identity_); |
31 params->set_services(GetProxy(&services)); | 33 params->set_services(GetProxy(&services)); |
32 manager->ConnectToApplication(params.Pass()); | 34 manager->ConnectToApplication(params.Pass()); |
33 | 35 |
34 MessagePipe pipe; | 36 MessagePipe pipe; |
35 content_handler_.Bind( | 37 content_handler_.Bind( |
36 InterfacePtrInfo<ContentHandler>(pipe.handle0.Pass(), 0u)); | 38 InterfacePtrInfo<ContentHandler>(pipe.handle0.Pass(), 0u)); |
37 services->ConnectToService(ContentHandler::Name_, pipe.handle1.Pass()); | 39 services->ConnectToService(ContentHandler::Name_, pipe.handle1.Pass()); |
38 content_handler_.set_connection_error_handler( | 40 content_handler_.set_connection_error_handler( |
39 [this]() { CloseConnection(); }); | 41 [this]() { CloseConnection(); }); |
40 } | 42 } |
41 | 43 |
| 44 void ContentHandlerConnection::StartApplication( |
| 45 InterfaceRequest<Application> request, |
| 46 URLResponsePtr response) { |
| 47 content_handler_->StartApplication( |
| 48 request.Pass(), response.Pass(), |
| 49 base::Bind(&ContentHandlerConnection::ApplicationDestructed, |
| 50 base::Unretained(this))); |
| 51 ref_count_++; |
| 52 } |
| 53 |
42 void ContentHandlerConnection::CloseConnection() { | 54 void ContentHandlerConnection::CloseConnection() { |
43 if (connection_closed_) | 55 if (connection_closed_) |
44 return; | 56 return; |
45 connection_closed_ = true; | 57 connection_closed_ = true; |
46 connection_closed_callback_.Run(this); | 58 connection_closed_callback_.Run(this); |
47 delete this; | 59 delete this; |
48 } | 60 } |
49 | 61 |
50 ContentHandlerConnection::~ContentHandlerConnection() { | 62 ContentHandlerConnection::~ContentHandlerConnection() { |
51 // If this DCHECK fails then something has tried to delete this object without | 63 // If this DCHECK fails then something has tried to delete this object without |
52 // calling CloseConnection. | 64 // calling CloseConnection. |
53 DCHECK(connection_closed_); | 65 DCHECK(connection_closed_); |
54 } | 66 } |
55 | 67 |
| 68 void ContentHandlerConnection::ApplicationDestructed() { |
| 69 if (!--ref_count_) |
| 70 CloseConnection(); |
| 71 } |
| 72 |
56 } // namespace package_manager | 73 } // namespace package_manager |
57 } // namespace mojo | 74 } // namespace mojo |
OLD | NEW |