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