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 "services/service_manager/public/cpp/service_context.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/logging.h" | |
11 #include "mojo/public/cpp/bindings/interface_request.h" | |
12 #include "services/service_manager/public/cpp/connector.h" | |
13 #include "services/service_manager/public/cpp/service.h" | |
14 | |
15 namespace service_manager { | |
16 | |
17 //////////////////////////////////////////////////////////////////////////////// | |
18 // ServiceContext, public: | |
19 | |
20 ServiceContext::ServiceContext( | |
21 std::unique_ptr<service_manager::Service> service, | |
22 mojom::ServiceRequest request, | |
23 std::unique_ptr<Connector> connector, | |
24 mojom::ConnectorRequest connector_request) | |
25 : pending_connector_request_(std::move(connector_request)), | |
26 service_(std::move(service)), | |
27 binding_(this, std::move(request)), | |
28 connector_(std::move(connector)), | |
29 weak_factory_(this) { | |
30 DCHECK(binding_.is_bound()); | |
31 binding_.set_connection_error_handler( | |
32 base::Bind(&ServiceContext::OnConnectionError, base::Unretained(this))); | |
33 if (!connector_) { | |
34 connector_ = Connector::Create(&pending_connector_request_); | |
35 } else { | |
36 DCHECK(pending_connector_request_.is_pending()); | |
37 } | |
38 service_->SetContext(this); | |
39 } | |
40 | |
41 ServiceContext::~ServiceContext() {} | |
42 | |
43 void ServiceContext::SetQuitClosure(const base::Closure& closure) { | |
44 if (service_quit_) { | |
45 // CAUTION: May delete |this|. | |
46 closure.Run(); | |
47 } else { | |
48 quit_closure_ = closure; | |
49 } | |
50 } | |
51 | |
52 void ServiceContext::RequestQuit() { | |
53 DCHECK(service_control_.is_bound()); | |
54 service_control_->RequestQuit(); | |
55 } | |
56 | |
57 void ServiceContext::DisconnectFromServiceManager() { | |
58 if (binding_.is_bound()) | |
59 binding_.Close(); | |
60 connector_.reset(); | |
61 } | |
62 | |
63 void ServiceContext::QuitNow() { | |
64 service_quit_ = true; | |
65 if (binding_.is_bound()) | |
66 binding_.Close(); | |
67 if (!quit_closure_.is_null()) { | |
68 // CAUTION: May delete |this|. | |
69 base::ResetAndReturn(&quit_closure_).Run(); | |
70 } | |
71 } | |
72 | |
73 //////////////////////////////////////////////////////////////////////////////// | |
74 // ServiceContext, mojom::Service implementation: | |
75 | |
76 void ServiceContext::OnStart(const Identity& identity, | |
77 const OnStartCallback& callback) { | |
78 identity_ = identity; | |
79 callback.Run(std::move(pending_connector_request_), | |
80 mojo::MakeRequest(&service_control_)); | |
81 service_->OnStart(); | |
82 } | |
83 | |
84 void ServiceContext::OnBindInterface( | |
85 const BindSourceInfo& source_info, | |
86 const std::string& interface_name, | |
87 mojo::ScopedMessagePipeHandle interface_pipe, | |
88 const OnBindInterfaceCallback& callback) { | |
89 // Acknowledge the request regardless of whether it's accepted. | |
90 callback.Run(); | |
91 | |
92 service_->OnBindInterface(source_info, interface_name, | |
93 std::move(interface_pipe)); | |
94 } | |
95 | |
96 //////////////////////////////////////////////////////////////////////////////// | |
97 // ServiceContext, private: | |
98 | |
99 void ServiceContext::OnConnectionError() { | |
100 if (service_->OnServiceManagerConnectionLost()) { | |
101 // CAUTION: May delete |this|. | |
102 QuitNow(); | |
103 } | |
104 } | |
105 | |
106 } // namespace service_manager | |
OLD | NEW |