OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 "base/bind.h" | |
6 #include "chromeos/dbus/console_service_client.h" | |
7 #include "chromeos/dbus/dbus_thread_manager.h" | |
8 #include "dbus/bus.h" | |
9 #include "dbus/exported_object.h" | |
10 #include "dbus/message.h" | |
11 #include "third_party/cros_system_api/dbus/service_constants.h" | |
12 | |
13 namespace dbus { | |
14 class Bus; | |
15 class MethodCall; | |
16 class ExportedObject; | |
17 } | |
18 | |
19 static chromeos::ConsoleServiceClient* s_console_service_client = NULL; | |
20 | |
21 class ConsoleServiceClientImpl : public chromeos::ConsoleServiceClient { | |
22 public: | |
23 ConsoleServiceClientImpl(); | |
24 ~ConsoleServiceClientImpl(); | |
Daniel Erat
2014/10/31 18:15:02
add override
dsodman
2014/10/31 18:43:25
Done.
| |
25 | |
26 void AddObserver(Observer* observer) override; | |
27 void RemoveObserver(Observer* observer) override; | |
28 bool HasObserver(Observer* observer) override; | |
29 | |
30 void OnOwnership(const std::string& service_name, bool success); | |
Daniel Erat
2014/10/31 18:15:02
these three methods seem like they should all be p
dsodman
2014/10/31 18:43:25
Done.
| |
31 | |
32 void OnExported(const std::string& interface_name, | |
33 const std::string& method_name, | |
34 bool success); | |
35 | |
36 void ActivateConsole(dbus::MethodCall* method_call, | |
37 dbus::ExportedObject::ResponseSender response_sender); | |
38 | |
39 protected: | |
40 void Init(dbus::Bus* bus) override; | |
41 | |
42 private: | |
43 dbus::Bus* bus_; | |
44 ObserverList<Observer> observers_; | |
45 scoped_refptr<dbus::ExportedObject> exported_object_; | |
46 base::WeakPtrFactory<ConsoleServiceClientImpl> weak_ptr_factory_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(ConsoleServiceClientImpl); | |
49 }; | |
50 | |
51 ConsoleServiceClientImpl::ConsoleServiceClientImpl() | |
52 : bus_(NULL), weak_ptr_factory_(this) { | |
53 } | |
54 | |
55 ConsoleServiceClientImpl::~ConsoleServiceClientImpl() { | |
56 s_console_service_client = NULL; | |
57 } | |
58 | |
59 void ConsoleServiceClientImpl::AddObserver(Observer* observer) { | |
60 observers_.AddObserver(observer); | |
61 } | |
62 | |
63 void ConsoleServiceClientImpl::RemoveObserver(Observer* observer) { | |
64 observers_.RemoveObserver(observer); | |
65 } | |
66 | |
67 bool ConsoleServiceClientImpl::HasObserver(Observer* observer) { | |
68 return observers_.HasObserver(observer); | |
69 } | |
70 | |
71 // OnOwnership | |
Daniel Erat
2014/10/31 18:15:02
please delete these method names in comments; i ha
dsodman
2014/10/31 18:43:25
Done.
| |
72 // This method will be called when the class receives ownership of the bus. | |
73 // |success| will be true if ownership succeeded and false otherwise. | |
Daniel Erat
2014/10/31 18:15:02
these comments should live above the corresponding
dsodman
2014/10/31 18:43:25
Done.
| |
74 void ConsoleServiceClientImpl::OnOwnership(const std::string& service_name, | |
75 bool success) { | |
76 LOG_IF(FATAL, !success) << "failed to own: " << service_name; | |
77 } | |
78 | |
79 // OnExported | |
80 // This method will be called when a dbus method is exported. The |success| | |
81 // parameter will be true if the export is successful. It will be false | |
82 // otherwise. | |
83 void ConsoleServiceClientImpl::OnExported(const std::string& interface_name, | |
84 const std::string& method_name, | |
85 bool success) { | |
86 if (!success) { | |
87 LOG(ERROR) << "failed to export " << interface_name << "." << method_name; | |
88 } | |
89 } | |
90 | |
91 // ActivateConsole | |
92 // This method will get called when a external process sends the dbus | |
93 // method ConsoleServiceInterface.ActivateConsole. The method receives | |
94 // a single integer parameter |console_id| which indicates the console id. | |
95 // Chrome will take ownership of the display service when the console | |
96 // id is 0 or 1. It will relinquish control when the console_id is 2 or more | |
97 // to allow other entities (such as the console) to take control of the | |
98 // display. | |
99 void ConsoleServiceClientImpl::ActivateConsole( | |
100 dbus::MethodCall* method_call, | |
101 dbus::ExportedObject::ResponseSender response_sender) { | |
102 dbus::MessageReader reader(method_call); | |
103 int console_id = 0; | |
104 if (reader.PopInt32(&console_id)) { | |
105 FOR_EACH_OBSERVER(Observer, observers_, OnActivateConsole(console_id)); | |
106 } else { | |
107 LOG(ERROR) << "Unable to parse " << chromeos::kActivateConsole | |
108 << " request"; | |
109 } | |
110 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | |
111 } | |
112 | |
113 // Init | |
114 // This will be called to initialize the class after the dbus system is | |
115 // operational. This cannot be in the constructor because there is no | |
116 // guarantee that the constructor will be called after the dbus service is | |
117 // running. | |
118 void ConsoleServiceClientImpl::Init(dbus::Bus* bus) { | |
119 bus_ = chromeos::DBusThreadManager::Get()->GetSystemBus(); | |
120 | |
121 if (!bus_) { | |
122 LOG(ERROR) << "Unable to get system d-bus for the ConsoleServiceClient"; | |
Daniel Erat
2014/10/31 18:15:02
any reason that this shouldn't just be CHECK(bus_)
dsodman
2014/10/31 18:43:24
Done.
| |
123 return; | |
124 } | |
125 | |
126 bus_->RequestOwnership(chromeos::kConsoleServiceName, | |
Daniel Erat
2014/10/31 18:15:02
it seems a bit strange to use a new service here i
dsodman
2014/10/31 18:43:25
That was what was done initially. This console se
| |
127 dbus::Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT, | |
128 base::Bind(&ConsoleServiceClientImpl::OnOwnership, | |
129 base::Unretained(this))); | |
130 | |
131 exported_object_ = bus_->GetExportedObject( | |
132 dbus::ObjectPath(dbus::ObjectPath(chromeos::kConsoleServicePath))); | |
133 | |
134 exported_object_->ExportMethod( | |
135 chromeos::kConsoleServiceInterface, | |
136 chromeos::kActivateConsole, | |
137 base::Bind(&ConsoleServiceClientImpl::ActivateConsole, | |
138 weak_ptr_factory_.GetWeakPtr()), | |
139 base::Bind(&ConsoleServiceClientImpl::OnExported, | |
140 weak_ptr_factory_.GetWeakPtr())); | |
141 } | |
142 | |
143 namespace chromeos { | |
144 | |
145 ConsoleServiceClient::ConsoleServiceClient() { | |
146 } | |
147 | |
148 ConsoleServiceClient::~ConsoleServiceClient() { | |
149 } | |
150 | |
151 ConsoleServiceClient* ConsoleServiceClient::Create() { | |
152 s_console_service_client = new ConsoleServiceClientImpl(); | |
153 CHECK_EQ(NULL, s_console_service_client); | |
Daniel Erat
2014/10/31 18:15:02
this check is wrong. i think that steven meant tha
dsodman
2014/10/31 18:43:25
I see. Fixed.
| |
154 return s_console_service_client; | |
155 } | |
156 | |
157 ConsoleServiceClient* ConsoleServiceClient::GetInstance() { | |
158 return s_console_service_client; | |
159 } | |
160 | |
161 } // namespace chromeos | |
OLD | NEW |