OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/api/gcd_private/gcd_private_api.h" | 5 #include "chrome/browser/extensions/api/gcd_private/gcd_private_api.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "chrome/browser/local_discovery/cloud_device_list.h" | 9 #include "chrome/browser/local_discovery/cloud_device_list.h" |
10 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" | 10 #include "chrome/browser/local_discovery/cloud_print_printer_list.h" |
11 #include "chrome/browser/local_discovery/gcd_constants.h" | 11 #include "chrome/browser/local_discovery/gcd_constants.h" |
12 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" | 12 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
15 #include "chrome/browser/signin/signin_manager_factory.h" | 15 #include "chrome/browser/signin/signin_manager_factory.h" |
16 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 16 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
17 #include "components/signin/core/browser/signin_manager.h" | 17 #include "components/signin/core/browser/signin_manager.h" |
18 #include "components/signin/core/browser/signin_manager_base.h" | 18 #include "components/signin/core/browser/signin_manager_base.h" |
19 | 19 |
20 namespace extensions { | 20 namespace extensions { |
21 | 21 |
22 namespace gcd_private = api::gcd_private; | 22 namespace gcd_private = api::gcd_private; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
| 26 scoped_ptr<Event> MakeCloudDeviceStateChangedEvent( |
| 27 bool available, |
| 28 const gcd_private::GCDDevice& device) { |
| 29 scoped_ptr<base::ListValue> params = |
| 30 gcd_private::OnCloudDeviceStateChanged::Create(available, device); |
| 31 scoped_ptr<Event> event(new Event( |
| 32 gcd_private::OnCloudDeviceStateChanged::kEventName, params.Pass())); |
| 33 return event.Pass(); |
| 34 } |
| 35 |
26 const int kNumRequestsNeeded = 2; | 36 const int kNumRequestsNeeded = 2; |
27 | 37 |
28 const char kIDPrefixCloudPrinter[] = "cloudprint:"; | 38 const char kIDPrefixCloudPrinter[] = "cloudprint:"; |
29 const char kIDPrefixGcd[] = "gcd:"; | 39 const char kIDPrefixGcd[] = "gcd:"; |
30 const char kIDPrefixMdns[] = "mdns:"; | 40 const char kIDPrefixMdns[] = "mdns:"; |
31 | 41 |
32 scoped_ptr<Event> MakeDeviceStateChangedEvent( | |
33 const gcd_private::GCDDevice& device) { | |
34 scoped_ptr<base::ListValue> params = | |
35 gcd_private::OnDeviceStateChanged::Create(device); | |
36 scoped_ptr<Event> event( | |
37 new Event(gcd_private::OnDeviceStateChanged::kEventName, params.Pass())); | |
38 return event.Pass(); | |
39 } | |
40 | |
41 scoped_ptr<Event> MakeDeviceRemovedEvent(const std::string& device) { | |
42 scoped_ptr<base::ListValue> params = | |
43 gcd_private::OnDeviceRemoved::Create(device); | |
44 scoped_ptr<Event> event( | |
45 new Event(gcd_private::OnDeviceRemoved::kEventName, params.Pass())); | |
46 return event.Pass(); | |
47 } | |
48 | |
49 GcdPrivateAPI::GCDApiFlowFactoryForTests* g_gcd_api_flow_factory = NULL; | 42 GcdPrivateAPI::GCDApiFlowFactoryForTests* g_gcd_api_flow_factory = NULL; |
50 | 43 |
51 base::LazyInstance<BrowserContextKeyedAPIFactory<GcdPrivateAPI> > g_factory = | 44 base::LazyInstance<BrowserContextKeyedAPIFactory<GcdPrivateAPI> > g_factory = |
52 LAZY_INSTANCE_INITIALIZER; | 45 LAZY_INSTANCE_INITIALIZER; |
53 | 46 |
54 scoped_ptr<local_discovery::GCDApiFlow> MakeGCDApiFlow(Profile* profile) { | 47 scoped_ptr<local_discovery::GCDApiFlow> MakeGCDApiFlow(Profile* profile) { |
55 if (g_gcd_api_flow_factory) { | 48 if (g_gcd_api_flow_factory) { |
56 return g_gcd_api_flow_factory->CreateGCDApiFlow(); | 49 return g_gcd_api_flow_factory->CreateGCDApiFlow(); |
57 } | 50 } |
58 | 51 |
(...skipping 10 matching lines...) Expand all Loading... |
69 token_service, | 62 token_service, |
70 signin_manager->GetAuthenticatedAccountId()); | 63 signin_manager->GetAuthenticatedAccountId()); |
71 } | 64 } |
72 | 65 |
73 } // namespace | 66 } // namespace |
74 | 67 |
75 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) | 68 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) |
76 : num_device_listeners_(0), browser_context_(context) { | 69 : num_device_listeners_(0), browser_context_(context) { |
77 DCHECK(browser_context_); | 70 DCHECK(browser_context_); |
78 if (EventRouter::Get(context)) { | 71 if (EventRouter::Get(context)) { |
79 EventRouter::Get(context) | 72 EventRouter::Get(context)->RegisterObserver( |
80 ->RegisterObserver(this, gcd_private::OnDeviceStateChanged::kEventName); | 73 this, gcd_private::OnCloudDeviceStateChanged::kEventName); |
81 EventRouter::Get(context) | |
82 ->RegisterObserver(this, gcd_private::OnDeviceRemoved::kEventName); | |
83 } | 74 } |
84 } | 75 } |
85 | 76 |
86 GcdPrivateAPI::~GcdPrivateAPI() { | 77 GcdPrivateAPI::~GcdPrivateAPI() { |
87 if (EventRouter::Get(browser_context_)) { | 78 if (EventRouter::Get(browser_context_)) { |
88 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 79 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
89 } | 80 } |
90 } | 81 } |
91 | 82 |
92 // static | 83 // static |
93 BrowserContextKeyedAPIFactory<GcdPrivateAPI>* | 84 BrowserContextKeyedAPIFactory<GcdPrivateAPI>* |
94 GcdPrivateAPI::GetFactoryInstance() { | 85 GcdPrivateAPI::GetFactoryInstance() { |
95 return g_factory.Pointer(); | 86 return g_factory.Pointer(); |
96 } | 87 } |
97 | 88 |
98 void GcdPrivateAPI::OnListenerAdded(const EventListenerInfo& details) { | 89 void GcdPrivateAPI::OnListenerAdded(const EventListenerInfo& details) { |
99 if (details.event_name == gcd_private::OnDeviceStateChanged::kEventName || | 90 num_device_listeners_++; |
100 details.event_name == gcd_private::OnDeviceRemoved::kEventName) { | |
101 num_device_listeners_++; | |
102 | 91 |
103 if (num_device_listeners_ == 1) { | 92 if (num_device_listeners_ == 1) { |
104 service_discovery_client_ = | 93 service_discovery_client_ = |
105 local_discovery::ServiceDiscoverySharedClient::GetInstance(); | 94 local_discovery::ServiceDiscoverySharedClient::GetInstance(); |
106 privet_device_lister_.reset(new local_discovery::PrivetDeviceListerImpl( | 95 privet_device_lister_.reset(new local_discovery::PrivetDeviceListerImpl( |
107 service_discovery_client_.get(), this)); | 96 service_discovery_client_.get(), this)); |
108 privet_device_lister_->Start(); | 97 privet_device_lister_->Start(); |
109 } | 98 } |
110 | 99 |
111 for (GCDDeviceMap::iterator i = known_devices_.begin(); | 100 for (GCDDeviceMap::iterator i = known_devices_.begin(); |
112 i != known_devices_.end(); | 101 i != known_devices_.end(); |
113 i++) { | 102 i++) { |
114 EventRouter::Get(browser_context_)->DispatchEventToExtension( | 103 EventRouter::Get(browser_context_)->DispatchEventToExtension( |
115 details.extension_id, MakeDeviceStateChangedEvent(*i->second)); | 104 details.extension_id, |
116 } | 105 MakeCloudDeviceStateChangedEvent(true, *i->second)); |
117 } | 106 } |
118 } | 107 } |
119 | 108 |
120 void GcdPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) { | 109 void GcdPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) { |
121 if (details.event_name == gcd_private::OnDeviceStateChanged::kEventName || | 110 num_device_listeners_--; |
122 details.event_name == gcd_private::OnDeviceRemoved::kEventName) { | |
123 num_device_listeners_--; | |
124 | 111 |
125 if (num_device_listeners_ == 0) { | 112 if (num_device_listeners_ == 0) { |
126 privet_device_lister_.reset(); | 113 privet_device_lister_.reset(); |
127 service_discovery_client_ = NULL; | 114 service_discovery_client_ = NULL; |
128 } | |
129 } | 115 } |
130 } | 116 } |
131 | 117 |
132 void GcdPrivateAPI::DeviceChanged( | 118 void GcdPrivateAPI::DeviceChanged( |
133 bool added, | 119 bool added, |
134 const std::string& name, | 120 const std::string& name, |
135 const local_discovery::DeviceDescription& description) { | 121 const local_discovery::DeviceDescription& description) { |
136 linked_ptr<gcd_private::GCDDevice> device(new gcd_private::GCDDevice); | 122 linked_ptr<gcd_private::GCDDevice> device(new gcd_private::GCDDevice); |
137 device->setup_type = gcd_private::SETUP_TYPE_MDNS; | 123 device->setup_type = gcd_private::SETUP_TYPE_MDNS; |
138 device->device_id = kIDPrefixMdns + name; | 124 device->id_string = kIDPrefixMdns + name; |
139 device->device_type = description.type; | 125 device->device_type = description.type; |
140 device->device_name = description.name; | 126 device->device_name = description.name; |
141 device->device_description = description.description; | 127 device->device_description = description.description; |
142 if (!description.id.empty()) | 128 if (!description.id.empty()) |
143 device->cloud_id.reset(new std::string(description.id)); | 129 device->cloud_id.reset(new std::string(description.id)); |
144 | 130 |
145 known_devices_[device->device_id] = device; | 131 known_devices_[device->id_string] = device; |
146 | 132 |
147 EventRouter::Get(browser_context_) | 133 EventRouter::Get(browser_context_) |
148 ->BroadcastEvent(MakeDeviceStateChangedEvent(*device)); | 134 ->BroadcastEvent(MakeCloudDeviceStateChangedEvent(true, *device)); |
149 } | 135 } |
150 | 136 |
151 void GcdPrivateAPI::DeviceRemoved(const std::string& name) { | 137 void GcdPrivateAPI::DeviceRemoved(const std::string& name) { |
152 GCDDeviceMap::iterator found = known_devices_.find(kIDPrefixMdns + name); | 138 GCDDeviceMap::iterator found = known_devices_.find(kIDPrefixMdns + name); |
153 linked_ptr<gcd_private::GCDDevice> device = found->second; | 139 linked_ptr<gcd_private::GCDDevice> device = found->second; |
154 known_devices_.erase(found); | 140 known_devices_.erase(found); |
155 | 141 |
156 EventRouter::Get(browser_context_) | 142 EventRouter::Get(browser_context_) |
157 ->BroadcastEvent(MakeDeviceRemovedEvent(device->device_id)); | 143 ->BroadcastEvent(MakeCloudDeviceStateChangedEvent(false, *device)); |
158 } | 144 } |
159 | 145 |
160 void GcdPrivateAPI::DeviceCacheFlushed() { | 146 void GcdPrivateAPI::DeviceCacheFlushed() { |
161 for (GCDDeviceMap::iterator i = known_devices_.begin(); | 147 for (GCDDeviceMap::iterator i = known_devices_.begin(); |
162 i != known_devices_.end(); | 148 i != known_devices_.end(); |
163 i++) { | 149 i++) { |
164 EventRouter::Get(browser_context_) | 150 EventRouter::Get(browser_context_) |
165 ->BroadcastEvent(MakeDeviceRemovedEvent(i->second->device_id)); | 151 ->BroadcastEvent(MakeCloudDeviceStateChangedEvent(false, *i->second)); |
166 } | 152 } |
167 | 153 |
168 known_devices_.clear(); | 154 known_devices_.clear(); |
169 } | 155 } |
170 | 156 |
171 bool GcdPrivateAPI::QueryForDevices() { | 157 bool GcdPrivateAPI::QueryForDevices() { |
172 if (!privet_device_lister_) | 158 if (!privet_device_lister_) |
173 return false; | 159 return false; |
174 | 160 |
175 privet_device_lister_->DiscoverNewDevices(true); | 161 privet_device_lister_->DiscoverNewDevices(true); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 SendResponse(false); | 218 SendResponse(false); |
233 return; | 219 return; |
234 } | 220 } |
235 | 221 |
236 std::vector<linked_ptr<gcd_private::GCDDevice> > devices; | 222 std::vector<linked_ptr<gcd_private::GCDDevice> > devices; |
237 | 223 |
238 for (DeviceList::iterator i = devices_.begin(); i != devices_.end(); i++) { | 224 for (DeviceList::iterator i = devices_.begin(); i != devices_.end(); i++) { |
239 linked_ptr<gcd_private::GCDDevice> device(new gcd_private::GCDDevice); | 225 linked_ptr<gcd_private::GCDDevice> device(new gcd_private::GCDDevice); |
240 device->setup_type = gcd_private::SETUP_TYPE_CLOUD; | 226 device->setup_type = gcd_private::SETUP_TYPE_CLOUD; |
241 if (i->type == local_discovery::kGCDTypePrinter) { | 227 if (i->type == local_discovery::kGCDTypePrinter) { |
242 device->device_id = kIDPrefixCloudPrinter + i->id; | 228 device->id_string = kIDPrefixCloudPrinter + i->id; |
243 } else { | 229 } else { |
244 device->device_id = kIDPrefixGcd + i->id; | 230 device->id_string = kIDPrefixGcd + i->id; |
245 } | 231 } |
246 | 232 |
247 device->cloud_id.reset(new std::string(i->id)); | 233 device->cloud_id.reset(new std::string(i->id)); |
248 device->device_type = i->type; | 234 device->device_type = i->type; |
249 device->device_name = i->display_name; | 235 device->device_name = i->display_name; |
250 device->device_description = i->description; | 236 device->device_description = i->description; |
251 | 237 |
252 devices.push_back(device); | 238 devices.push_back(device); |
253 } | 239 } |
254 | 240 |
(...skipping 29 matching lines...) Expand all Loading... |
284 GcdPrivateStartSetupFunction::GcdPrivateStartSetupFunction() { | 270 GcdPrivateStartSetupFunction::GcdPrivateStartSetupFunction() { |
285 } | 271 } |
286 | 272 |
287 GcdPrivateStartSetupFunction::~GcdPrivateStartSetupFunction() { | 273 GcdPrivateStartSetupFunction::~GcdPrivateStartSetupFunction() { |
288 } | 274 } |
289 | 275 |
290 bool GcdPrivateStartSetupFunction::RunAsync() { | 276 bool GcdPrivateStartSetupFunction::RunAsync() { |
291 return false; | 277 return false; |
292 } | 278 } |
293 | 279 |
294 GcdPrivateSetWiFiNetworkFunction::GcdPrivateSetWiFiNetworkFunction() { | 280 GcdPrivateSetWiFiNetworksFunction::GcdPrivateSetWiFiNetworksFunction() { |
295 } | 281 } |
296 | 282 |
297 GcdPrivateSetWiFiNetworkFunction::~GcdPrivateSetWiFiNetworkFunction() { | 283 GcdPrivateSetWiFiNetworksFunction::~GcdPrivateSetWiFiNetworksFunction() { |
298 } | 284 } |
299 | 285 |
300 bool GcdPrivateSetWiFiNetworkFunction::RunAsync() { | 286 bool GcdPrivateSetWiFiNetworksFunction::RunAsync() { |
301 return false; | 287 return false; |
302 } | 288 } |
303 | 289 |
304 GcdPrivateSetWiFiPasswordFunction::GcdPrivateSetWiFiPasswordFunction() { | 290 GcdPrivateSetWiFiCredentialsFunction::GcdPrivateSetWiFiCredentialsFunction() { |
305 } | 291 } |
306 | 292 |
307 GcdPrivateSetWiFiPasswordFunction::~GcdPrivateSetWiFiPasswordFunction() { | 293 GcdPrivateSetWiFiCredentialsFunction::~GcdPrivateSetWiFiCredentialsFunction() { |
308 } | 294 } |
309 | 295 |
310 bool GcdPrivateSetWiFiPasswordFunction::RunAsync() { | 296 bool GcdPrivateSetWiFiCredentialsFunction::RunAsync() { |
311 return false; | 297 return false; |
312 } | 298 } |
313 | 299 |
314 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() { | 300 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() { |
315 } | 301 } |
316 | 302 |
317 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() { | 303 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() { |
318 } | 304 } |
319 | 305 |
320 bool GcdPrivateConfirmCodeFunction::RunAsync() { | 306 bool GcdPrivateConfirmCodeFunction::RunAsync() { |
321 return false; | 307 return false; |
322 } | 308 } |
323 | 309 |
324 GcdPrivateStopSetupFunction::GcdPrivateStopSetupFunction() { | 310 GcdPrivateStopSetupFunction::GcdPrivateStopSetupFunction() { |
325 } | 311 } |
326 | 312 |
327 GcdPrivateStopSetupFunction::~GcdPrivateStopSetupFunction() { | 313 GcdPrivateStopSetupFunction::~GcdPrivateStopSetupFunction() { |
328 } | 314 } |
329 | 315 |
330 bool GcdPrivateStopSetupFunction::RunAsync() { | 316 bool GcdPrivateStopSetupFunction::RunAsync() { |
331 return false; | 317 return false; |
332 } | 318 } |
333 | 319 |
334 GcdPrivateGetCommandDefinitionsFunction:: | |
335 GcdPrivateGetCommandDefinitionsFunction() { | |
336 } | |
337 | |
338 GcdPrivateGetCommandDefinitionsFunction:: | |
339 ~GcdPrivateGetCommandDefinitionsFunction() { | |
340 } | |
341 | |
342 bool GcdPrivateGetCommandDefinitionsFunction::RunAsync() { | |
343 return false; | |
344 } | |
345 | |
346 GcdPrivateInsertCommandFunction::GcdPrivateInsertCommandFunction() { | |
347 } | |
348 | |
349 GcdPrivateInsertCommandFunction::~GcdPrivateInsertCommandFunction() { | |
350 } | |
351 | |
352 bool GcdPrivateInsertCommandFunction::RunAsync() { | |
353 return false; | |
354 } | |
355 | |
356 GcdPrivateGetCommandFunction::GcdPrivateGetCommandFunction() { | |
357 } | |
358 | |
359 GcdPrivateGetCommandFunction::~GcdPrivateGetCommandFunction() { | |
360 } | |
361 | |
362 bool GcdPrivateGetCommandFunction::RunAsync() { | |
363 return false; | |
364 } | |
365 | |
366 GcdPrivateCancelCommandFunction::GcdPrivateCancelCommandFunction() { | |
367 } | |
368 | |
369 GcdPrivateCancelCommandFunction::~GcdPrivateCancelCommandFunction() { | |
370 } | |
371 | |
372 bool GcdPrivateCancelCommandFunction::RunAsync() { | |
373 return false; | |
374 } | |
375 | |
376 GcdPrivateGetCommandsListFunction::GcdPrivateGetCommandsListFunction() { | |
377 } | |
378 | |
379 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { | |
380 } | |
381 | |
382 bool GcdPrivateGetCommandsListFunction::RunAsync() { | |
383 return false; | |
384 } | |
385 | |
386 } // namespace extensions | 320 } // namespace extensions |
OLD | NEW |