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( | 26 scoped_ptr<Event> MakeLocalDeviceStateChangedEvent( |
27 bool available, | 27 bool available, |
28 const gcd_private::GCDDevice& device) { | 28 const gcd_private::GCDDevice& device) { |
29 scoped_ptr<base::ListValue> params = | 29 scoped_ptr<base::ListValue> params = |
30 gcd_private::OnCloudDeviceStateChanged::Create(available, device); | 30 gcd_private::OnLocalDeviceStateChanged::Create(available, device); |
31 scoped_ptr<Event> event(new Event( | 31 scoped_ptr<Event> event(new Event( |
32 gcd_private::OnCloudDeviceStateChanged::kEventName, params.Pass())); | 32 gcd_private::OnLocalDeviceStateChanged::kEventName, params.Pass())); |
33 return event.Pass(); | 33 return event.Pass(); |
34 } | 34 } |
35 | 35 |
36 const int kNumRequestsNeeded = 2; | 36 const int kNumRequestsNeeded = 2; |
37 | 37 |
38 const char kIDPrefixCloudPrinter[] = "cloudprint:"; | 38 const char kIDPrefixCloudPrinter[] = "cloudprint:"; |
39 const char kIDPrefixGcd[] = "gcd:"; | 39 const char kIDPrefixGcd[] = "gcd:"; |
40 const char kIDPrefixMdns[] = "mdns:"; | 40 const char kIDPrefixMdns[] = "mdns:"; |
41 | 41 |
42 #if defined(ENABLE_WIFI_BOOTSTRAPPING) | 42 #if defined(ENABLE_WIFI_BOOTSTRAPPING) |
(...skipping 23 matching lines...) Expand all Loading... |
66 token_service, | 66 token_service, |
67 signin_manager->GetAuthenticatedAccountId()); | 67 signin_manager->GetAuthenticatedAccountId()); |
68 } | 68 } |
69 | 69 |
70 } // namespace | 70 } // namespace |
71 | 71 |
72 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) | 72 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) |
73 : num_device_listeners_(0), browser_context_(context) { | 73 : num_device_listeners_(0), browser_context_(context) { |
74 DCHECK(browser_context_); | 74 DCHECK(browser_context_); |
75 EventRouter::Get(context)->RegisterObserver( | 75 EventRouter::Get(context)->RegisterObserver( |
76 this, gcd_private::OnCloudDeviceStateChanged::kEventName); | 76 this, gcd_private::OnLocalDeviceStateChanged::kEventName); |
77 } | 77 } |
78 | 78 |
79 GcdPrivateAPI::~GcdPrivateAPI() { | 79 GcdPrivateAPI::~GcdPrivateAPI() { |
80 } | 80 } |
81 | 81 |
82 // static | 82 // static |
83 BrowserContextKeyedAPIFactory<GcdPrivateAPI>* | 83 BrowserContextKeyedAPIFactory<GcdPrivateAPI>* |
84 GcdPrivateAPI::GetFactoryInstance() { | 84 GcdPrivateAPI::GetFactoryInstance() { |
85 return g_factory.Pointer(); | 85 return g_factory.Pointer(); |
86 } | 86 } |
(...skipping 26 matching lines...) Expand all Loading... |
113 base::Unretained(this)))); | 113 base::Unretained(this)))); |
114 bootstrapping_device_lister_->Start(); | 114 bootstrapping_device_lister_->Start(); |
115 #endif // ENABLE_WIFI_BOOTSTRAPPING | 115 #endif // ENABLE_WIFI_BOOTSTRAPPING |
116 } | 116 } |
117 | 117 |
118 for (GCDDeviceMap::iterator i = known_devices_.begin(); | 118 for (GCDDeviceMap::iterator i = known_devices_.begin(); |
119 i != known_devices_.end(); | 119 i != known_devices_.end(); |
120 i++) { | 120 i++) { |
121 EventRouter::Get(browser_context_)->DispatchEventToExtension( | 121 EventRouter::Get(browser_context_)->DispatchEventToExtension( |
122 details.extension_id, | 122 details.extension_id, |
123 MakeCloudDeviceStateChangedEvent(true, *i->second)); | 123 MakeLocalDeviceStateChangedEvent(true, *i->second)); |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 void GcdPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) { | 127 void GcdPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) { |
128 num_device_listeners_--; | 128 num_device_listeners_--; |
129 | 129 |
130 if (num_device_listeners_ == 0) { | 130 if (num_device_listeners_ == 0) { |
131 privet_device_lister_.reset(); | 131 privet_device_lister_.reset(); |
132 service_discovery_client_ = NULL; | 132 service_discovery_client_ = NULL; |
133 | 133 |
(...skipping 13 matching lines...) Expand all Loading... |
147 device->id_string = kIDPrefixMdns + name; | 147 device->id_string = kIDPrefixMdns + name; |
148 device->device_type = description.type; | 148 device->device_type = description.type; |
149 device->device_name = description.name; | 149 device->device_name = description.name; |
150 device->device_description = description.description; | 150 device->device_description = description.description; |
151 if (!description.id.empty()) | 151 if (!description.id.empty()) |
152 device->cloud_id.reset(new std::string(description.id)); | 152 device->cloud_id.reset(new std::string(description.id)); |
153 | 153 |
154 known_devices_[device->id_string] = device; | 154 known_devices_[device->id_string] = device; |
155 | 155 |
156 EventRouter::Get(browser_context_) | 156 EventRouter::Get(browser_context_) |
157 ->BroadcastEvent(MakeCloudDeviceStateChangedEvent(true, *device)); | 157 ->BroadcastEvent(MakeLocalDeviceStateChangedEvent(true, *device)); |
158 } | 158 } |
159 | 159 |
160 void GcdPrivateAPI::DeviceRemoved(const std::string& name) { | 160 void GcdPrivateAPI::DeviceRemoved(const std::string& name) { |
161 GCDDeviceMap::iterator found = known_devices_.find(kIDPrefixMdns + name); | 161 GCDDeviceMap::iterator found = known_devices_.find(kIDPrefixMdns + name); |
162 DCHECK(found != known_devices_.end()); | 162 DCHECK(found != known_devices_.end()); |
163 linked_ptr<gcd_private::GCDDevice> device = found->second; | 163 linked_ptr<gcd_private::GCDDevice> device = found->second; |
164 known_devices_.erase(found); | 164 known_devices_.erase(found); |
165 | 165 |
166 EventRouter::Get(browser_context_) | 166 EventRouter::Get(browser_context_) |
167 ->BroadcastEvent(MakeCloudDeviceStateChangedEvent(false, *device)); | 167 ->BroadcastEvent(MakeLocalDeviceStateChangedEvent(false, *device)); |
168 } | 168 } |
169 | 169 |
170 void GcdPrivateAPI::DeviceCacheFlushed() { | 170 void GcdPrivateAPI::DeviceCacheFlushed() { |
171 for (GCDDeviceMap::iterator i = known_devices_.begin(); | 171 for (GCDDeviceMap::iterator i = known_devices_.begin(); |
172 i != known_devices_.end(); | 172 i != known_devices_.end(); |
173 i++) { | 173 i++) { |
174 EventRouter::Get(browser_context_) | 174 EventRouter::Get(browser_context_) |
175 ->BroadcastEvent(MakeCloudDeviceStateChangedEvent(false, *i->second)); | 175 ->BroadcastEvent(MakeLocalDeviceStateChangedEvent(false, *i->second)); |
176 } | 176 } |
177 | 177 |
178 known_devices_.clear(); | 178 known_devices_.clear(); |
179 } | 179 } |
180 | 180 |
181 #if defined(ENABLE_WIFI_BOOTSTRAPPING) | 181 #if defined(ENABLE_WIFI_BOOTSTRAPPING) |
182 void GcdPrivateAPI::OnBootstrappingDeviceUpdate( | 182 void GcdPrivateAPI::OnBootstrappingDeviceUpdate( |
183 bool available, | 183 bool available, |
184 const local_discovery::wifi::BootstrappingDeviceDescription& description) { | 184 const local_discovery::wifi::BootstrappingDeviceDescription& description) { |
185 linked_ptr<gcd_private::GCDDevice> device; | 185 linked_ptr<gcd_private::GCDDevice> device; |
186 if (available) { | 186 if (available) { |
187 device.reset(new gcd_private::GCDDevice); | 187 device.reset(new gcd_private::GCDDevice); |
188 device->setup_type = gcd_private::SETUP_TYPE_WIFI; | 188 device->setup_type = gcd_private::SETUP_TYPE_WIFI; |
189 device->id_string = kIDPrefixWifi + description.device_ssid; | 189 device->id_string = kIDPrefixWifi + description.device_ssid; |
190 device->device_type = description.device_kind; | 190 device->device_type = description.device_kind; |
191 device->device_name = description.device_name; | 191 device->device_name = description.device_name; |
192 | 192 |
193 known_devices_[device->id_string] = device; | 193 known_devices_[device->id_string] = device; |
194 } else { | 194 } else { |
195 GCDDeviceMap::iterator found = | 195 GCDDeviceMap::iterator found = |
196 known_devices_.find(kIDPrefixWifi + description.device_ssid); | 196 known_devices_.find(kIDPrefixWifi + description.device_ssid); |
197 DCHECK(found != known_devices_.end()); | 197 DCHECK(found != known_devices_.end()); |
198 device = found->second; | 198 device = found->second; |
199 known_devices_.erase(found); | 199 known_devices_.erase(found); |
200 } | 200 } |
201 | 201 |
202 EventRouter::Get(browser_context_) | 202 EventRouter::Get(browser_context_) |
203 ->BroadcastEvent(MakeCloudDeviceStateChangedEvent(available, *device)); | 203 ->BroadcastEvent(MakeLocalDeviceStateChangedEvent(available, *device)); |
204 } | 204 } |
205 #endif // ENABLE_WIFI_BOOTSTRAPPING | 205 #endif // ENABLE_WIFI_BOOTSTRAPPING |
206 | 206 |
| 207 bool GcdPrivateAPI::QueryForDevices() { |
| 208 if (!privet_device_lister_) |
| 209 return false; |
| 210 |
| 211 privet_device_lister_->DiscoverNewDevices(true); |
| 212 |
| 213 return true; |
| 214 } |
| 215 |
207 // static | 216 // static |
208 void GcdPrivateAPI::SetGCDApiFlowFactoryForTests( | 217 void GcdPrivateAPI::SetGCDApiFlowFactoryForTests( |
209 GCDApiFlowFactoryForTests* factory) { | 218 GCDApiFlowFactoryForTests* factory) { |
210 g_gcd_api_flow_factory = factory; | 219 g_gcd_api_flow_factory = factory; |
211 } | 220 } |
212 | 221 |
213 GcdPrivateGetCloudDeviceListFunction::GcdPrivateGetCloudDeviceListFunction() { | 222 GcdPrivateGetCloudDeviceListFunction::GcdPrivateGetCloudDeviceListFunction() { |
214 } | 223 } |
215 GcdPrivateGetCloudDeviceListFunction::~GcdPrivateGetCloudDeviceListFunction() { | 224 GcdPrivateGetCloudDeviceListFunction::~GcdPrivateGetCloudDeviceListFunction() { |
216 } | 225 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 | 287 |
279 devices.push_back(device); | 288 devices.push_back(device); |
280 } | 289 } |
281 | 290 |
282 results_ = gcd_private::GetCloudDeviceList::Results::Create(devices); | 291 results_ = gcd_private::GetCloudDeviceList::Results::Create(devices); |
283 | 292 |
284 SendResponse(true); | 293 SendResponse(true); |
285 Release(); | 294 Release(); |
286 } | 295 } |
287 | 296 |
| 297 GcdPrivateQueryForNewLocalDevicesFunction:: |
| 298 GcdPrivateQueryForNewLocalDevicesFunction() { |
| 299 } |
| 300 |
| 301 GcdPrivateQueryForNewLocalDevicesFunction:: |
| 302 ~GcdPrivateQueryForNewLocalDevicesFunction() { |
| 303 } |
| 304 |
| 305 bool GcdPrivateQueryForNewLocalDevicesFunction::RunSync() { |
| 306 GcdPrivateAPI* gcd_api = |
| 307 BrowserContextKeyedAPIFactory<GcdPrivateAPI>::Get(GetProfile()); |
| 308 |
| 309 if (!gcd_api) |
| 310 return false; |
| 311 |
| 312 if (!gcd_api->QueryForDevices()) { |
| 313 error_ = |
| 314 "You must first subscribe to onLocalDeviceStateChanged notifications"; |
| 315 return false; |
| 316 } |
| 317 |
| 318 return true; |
| 319 } |
| 320 |
288 } // namespace extensions | 321 } // namespace extensions |
OLD | NEW |