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/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 93 |
94 class GcdPrivateSessionHolder; | 94 class GcdPrivateSessionHolder; |
95 | 95 |
96 class GcdPrivateAPIImpl : public EventRouter::Observer, | 96 class GcdPrivateAPIImpl : public EventRouter::Observer, |
97 public local_discovery::PrivetDeviceLister::Delegate { | 97 public local_discovery::PrivetDeviceLister::Delegate { |
98 public: | 98 public: |
99 typedef base::Callback<void(bool success)> SuccessCallback; | 99 typedef base::Callback<void(bool success)> SuccessCallback; |
100 | 100 |
101 typedef base::Callback<void(int session_id, | 101 typedef base::Callback<void(int session_id, |
102 api::gcd_private::Status status, | 102 api::gcd_private::Status status, |
103 const std::string& code, | 103 const std::vector<api::gcd_private::PairingType>& |
104 api::gcd_private::ConfirmationType type)> | 104 pairing_types)> EstablishSessionCallback; |
105 ConfirmationCodeCallback; | |
106 | 105 |
107 typedef base::Callback<void(api::gcd_private::Status status)> | 106 typedef base::Callback<void(api::gcd_private::Status status)> SessionCallback; |
108 SessionEstablishedCallback; | |
109 | 107 |
110 typedef base::Callback<void(api::gcd_private::Status status, | 108 typedef base::Callback<void(api::gcd_private::Status status, |
111 const base::DictionaryValue& response)> | 109 const base::DictionaryValue& response)> |
112 MessageResponseCallback; | 110 MessageResponseCallback; |
113 | 111 |
114 explicit GcdPrivateAPIImpl(content::BrowserContext* context); | 112 explicit GcdPrivateAPIImpl(content::BrowserContext* context); |
115 virtual ~GcdPrivateAPIImpl(); | 113 virtual ~GcdPrivateAPIImpl(); |
116 | 114 |
117 static GcdPrivateAPIImpl* Get(content::BrowserContext* context); | 115 static GcdPrivateAPIImpl* Get(content::BrowserContext* context); |
118 | 116 |
119 bool QueryForDevices(); | 117 bool QueryForDevices(); |
120 | 118 |
121 void EstablishSession(const std::string& ip_address, | 119 void EstablishSession(const std::string& ip_address, |
122 int port, | 120 int port, |
123 ConfirmationCodeCallback callback); | 121 const EstablishSessionCallback& callback); |
| 122 |
| 123 void StartPairing(int session_id, |
| 124 api::gcd_private::PairingType pairing_type, |
| 125 const SessionCallback& callback); |
124 | 126 |
125 void ConfirmCode(int session_id, | 127 void ConfirmCode(int session_id, |
126 const std::string& code, | 128 const std::string& code, |
127 SessionEstablishedCallback callback); | 129 const SessionCallback& callback); |
128 | 130 |
129 void SendMessage(int session_id, | 131 void SendMessage(int session_id, |
130 const std::string& api, | 132 const std::string& api, |
131 const base::DictionaryValue& input, | 133 const base::DictionaryValue& input, |
132 MessageResponseCallback callback); | 134 const MessageResponseCallback& callback); |
133 | 135 |
134 void RequestWifiPassword(const std::string& ssid, | 136 void RequestWifiPassword(const std::string& ssid, |
135 const SuccessCallback& callback); | 137 const SuccessCallback& callback); |
136 | 138 |
137 void RemoveSession(int session_id); | 139 void RemoveSession(int session_id); |
138 | 140 |
139 scoped_ptr<base::ListValue> GetPrefetchedSSIDList(); | 141 scoped_ptr<base::ListValue> GetPrefetchedSSIDList(); |
140 | 142 |
141 private: | 143 private: |
142 typedef std::map<std::string /* id_string */, | 144 typedef std::map<std::string /* id_string */, |
143 linked_ptr<api::gcd_private::GCDDevice> > GCDDeviceMap; | 145 linked_ptr<api::gcd_private::GCDDevice> > GCDDeviceMap; |
144 | 146 |
145 typedef std::map<int /* session id*/, linked_ptr<GcdPrivateSessionHolder> > | |
146 GCDSessionMap; | |
147 | |
148 typedef std::map<std::string /* ssid */, std::string /* password */> | 147 typedef std::map<std::string /* ssid */, std::string /* password */> |
149 PasswordMap; | 148 PasswordMap; |
150 | 149 |
151 // EventRouter::Observer implementation. | 150 // EventRouter::Observer implementation. |
152 void OnListenerAdded(const EventListenerInfo& details) override; | 151 void OnListenerAdded(const EventListenerInfo& details) override; |
153 void OnListenerRemoved(const EventListenerInfo& details) override; | 152 void OnListenerRemoved(const EventListenerInfo& details) override; |
154 | 153 |
155 // local_discovery::PrivetDeviceLister implementation. | 154 // local_discovery::PrivetDeviceLister implementation. |
156 void DeviceChanged( | 155 void DeviceChanged( |
157 bool added, | 156 bool added, |
(...skipping 14 matching lines...) Expand all Loading... |
172 const std::string& password); | 171 const std::string& password); |
173 void StartWifiIfNotStarted(); | 172 void StartWifiIfNotStarted(); |
174 #endif | 173 #endif |
175 | 174 |
176 int num_device_listeners_; | 175 int num_device_listeners_; |
177 scoped_refptr<local_discovery::ServiceDiscoverySharedClient> | 176 scoped_refptr<local_discovery::ServiceDiscoverySharedClient> |
178 service_discovery_client_; | 177 service_discovery_client_; |
179 scoped_ptr<local_discovery::PrivetDeviceLister> privet_device_lister_; | 178 scoped_ptr<local_discovery::PrivetDeviceLister> privet_device_lister_; |
180 GCDDeviceMap known_devices_; | 179 GCDDeviceMap known_devices_; |
181 | 180 |
182 GCDSessionMap sessions_; | 181 std::map<int, linked_ptr<local_discovery::PrivetV3Session>> sessions_; |
183 int last_session_id_; | 182 int last_session_id_; |
184 | 183 |
185 content::BrowserContext* const browser_context_; | 184 content::BrowserContext* const browser_context_; |
186 | 185 |
187 #if defined(ENABLE_WIFI_BOOTSTRAPPING) | 186 #if defined(ENABLE_WIFI_BOOTSTRAPPING) |
188 scoped_ptr<local_discovery::wifi::WifiManager> wifi_manager_; | 187 scoped_ptr<local_discovery::wifi::WifiManager> wifi_manager_; |
189 #endif | 188 #endif |
190 PasswordMap wifi_passwords_; | 189 PasswordMap wifi_passwords_; |
191 }; | 190 }; |
192 | 191 |
193 class GcdPrivateRequest : public local_discovery::PrivetV3Session::Request { | |
194 public: | |
195 GcdPrivateRequest(const std::string& api, | |
196 const base::DictionaryValue& input, | |
197 const GcdPrivateAPIImpl::MessageResponseCallback& callback, | |
198 GcdPrivateSessionHolder* session_holder); | |
199 ~GcdPrivateRequest() override; | |
200 | |
201 // local_discovery::PrivetV3Session::Request implementation. | |
202 std::string GetName() override; | |
203 const base::DictionaryValue& GetInput() override; | |
204 void OnError() override; | |
205 void OnParsedJson(const base::DictionaryValue& value, | |
206 bool has_error) override; | |
207 | |
208 void RunCallback(gcd_private::Status status, | |
209 const base::DictionaryValue& value); | |
210 | |
211 private: | |
212 std::string api_; | |
213 scoped_ptr<base::DictionaryValue> input_; | |
214 GcdPrivateAPIImpl::MessageResponseCallback callback_; | |
215 GcdPrivateSessionHolder* session_holder_; | |
216 }; | |
217 | |
218 class GcdPrivateSessionHolder | |
219 : public local_discovery::PrivetV3Session::Delegate { | |
220 public: | |
221 typedef base::Callback<void(api::gcd_private::Status status, | |
222 const std::string& code, | |
223 api::gcd_private::ConfirmationType type)> | |
224 ConfirmationCodeCallback; | |
225 | |
226 GcdPrivateSessionHolder(const std::string& ip_address, | |
227 int port, | |
228 net::URLRequestContextGetter* request_context); | |
229 ~GcdPrivateSessionHolder() override; | |
230 | |
231 void Start(const ConfirmationCodeCallback& callback); | |
232 | |
233 void ConfirmCode( | |
234 const std::string& code, | |
235 const GcdPrivateAPIImpl::SessionEstablishedCallback& callback); | |
236 | |
237 void SendMessage(const std::string& api, | |
238 const base::DictionaryValue& input, | |
239 GcdPrivateAPIImpl::MessageResponseCallback callback); | |
240 | |
241 void DeleteRequest(GcdPrivateRequest* request); | |
242 | |
243 private: | |
244 // local_discovery::PrivetV3Session::Delegate implementation. | |
245 void OnSetupConfirmationNeeded( | |
246 const std::string& confirmation_code, | |
247 api::gcd_private::ConfirmationType confirmation_type) override; | |
248 void OnSessionStatus(api::gcd_private::Status status) override; | |
249 | |
250 scoped_ptr<local_discovery::PrivetHTTPClient> http_client_; | |
251 scoped_ptr<local_discovery::PrivetV3Session> privet_session_; | |
252 typedef ScopedVector<GcdPrivateRequest> RequestVector; | |
253 RequestVector requests_; | |
254 | |
255 ConfirmationCodeCallback confirm_callback_; | |
256 GcdPrivateAPIImpl::SessionEstablishedCallback session_established_callback_; | |
257 }; | |
258 | 192 |
259 GcdPrivateAPIImpl::GcdPrivateAPIImpl(content::BrowserContext* context) | 193 GcdPrivateAPIImpl::GcdPrivateAPIImpl(content::BrowserContext* context) |
260 : num_device_listeners_(0), last_session_id_(0), browser_context_(context) { | 194 : num_device_listeners_(0), last_session_id_(0), browser_context_(context) { |
261 DCHECK(browser_context_); | 195 DCHECK(browser_context_); |
262 if (EventRouter::Get(context)) { | 196 if (EventRouter::Get(context)) { |
263 EventRouter::Get(context) | 197 EventRouter::Get(context) |
264 ->RegisterObserver(this, gcd_private::OnDeviceStateChanged::kEventName); | 198 ->RegisterObserver(this, gcd_private::OnDeviceStateChanged::kEventName); |
265 EventRouter::Get(context) | 199 EventRouter::Get(context) |
266 ->RegisterObserver(this, gcd_private::OnDeviceRemoved::kEventName); | 200 ->RegisterObserver(this, gcd_private::OnDeviceRemoved::kEventName); |
267 } | 201 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 | 289 |
356 bool GcdPrivateAPIImpl::QueryForDevices() { | 290 bool GcdPrivateAPIImpl::QueryForDevices() { |
357 if (!privet_device_lister_) | 291 if (!privet_device_lister_) |
358 return false; | 292 return false; |
359 | 293 |
360 privet_device_lister_->DiscoverNewDevices(true); | 294 privet_device_lister_->DiscoverNewDevices(true); |
361 | 295 |
362 return true; | 296 return true; |
363 } | 297 } |
364 | 298 |
365 void GcdPrivateAPIImpl::EstablishSession(const std::string& ip_address, | 299 void GcdPrivateAPIImpl::EstablishSession( |
366 int port, | 300 const std::string& ip_address, |
367 ConfirmationCodeCallback callback) { | 301 int port, |
| 302 const EstablishSessionCallback& callback) { |
| 303 std::string host_string; |
| 304 net::IPAddressNumber address_number; |
| 305 |
| 306 if (net::ParseIPLiteralToNumber(ip_address, &address_number) && |
| 307 address_number.size() == net::kIPv6AddressSize) { |
| 308 host_string = base::StringPrintf("[%s]", ip_address.c_str()); |
| 309 } else { |
| 310 host_string = ip_address; |
| 311 } |
| 312 |
| 313 scoped_ptr<local_discovery::PrivetHTTPClient> http_client( |
| 314 new local_discovery::PrivetHTTPClientImpl( |
| 315 "", net::HostPortPair(host_string, port), |
| 316 browser_context_->GetRequestContext())); |
| 317 |
368 int session_id = last_session_id_++; | 318 int session_id = last_session_id_++; |
369 linked_ptr<GcdPrivateSessionHolder> session_handler( | 319 linked_ptr<local_discovery::PrivetV3Session> session_handler( |
370 new GcdPrivateSessionHolder( | 320 new local_discovery::PrivetV3Session(http_client.Pass())); |
371 ip_address, port, browser_context_->GetRequestContext())); | |
372 sessions_[session_id] = session_handler; | 321 sessions_[session_id] = session_handler; |
373 session_handler->Start(base::Bind(callback, session_id)); | 322 session_handler->Init(base::Bind(callback, session_id)); |
| 323 } |
| 324 |
| 325 void GcdPrivateAPIImpl::StartPairing(int session_id, |
| 326 api::gcd_private::PairingType pairing_type, |
| 327 const SessionCallback& callback) { |
| 328 auto found = sessions_.find(session_id); |
| 329 |
| 330 if (found == sessions_.end()) |
| 331 return callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR); |
| 332 |
| 333 found->second->StartPairing(pairing_type, callback); |
374 } | 334 } |
375 | 335 |
376 void GcdPrivateAPIImpl::ConfirmCode(int session_id, | 336 void GcdPrivateAPIImpl::ConfirmCode(int session_id, |
377 const std::string& code, | 337 const std::string& code, |
378 SessionEstablishedCallback callback) { | 338 const SessionCallback& callback) { |
379 GCDSessionMap::iterator found = sessions_.find(session_id); | 339 auto found = sessions_.find(session_id); |
380 | 340 |
381 if (found == sessions_.end()) { | 341 if (found == sessions_.end()) |
382 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR); | 342 return callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR); |
383 return; | |
384 } | |
385 | 343 |
386 found->second->ConfirmCode(code, callback); | 344 found->second->ConfirmCode(code, callback); |
387 } | 345 } |
388 | 346 |
389 void GcdPrivateAPIImpl::SendMessage(int session_id, | 347 void GcdPrivateAPIImpl::SendMessage(int session_id, |
390 const std::string& api, | 348 const std::string& api, |
391 const base::DictionaryValue& input, | 349 const base::DictionaryValue& input, |
392 MessageResponseCallback callback) { | 350 const MessageResponseCallback& callback) { |
393 const base::DictionaryValue* input_actual = &input; | 351 const base::DictionaryValue* input_actual = &input; |
394 scoped_ptr<base::DictionaryValue> input_cloned; | 352 scoped_ptr<base::DictionaryValue> input_cloned; |
395 | 353 |
396 if (api == kPrivatAPISetup) { | 354 if (api == kPrivatAPISetup) { |
397 const base::DictionaryValue* wifi = NULL; | 355 const base::DictionaryValue* wifi = NULL; |
398 | 356 |
399 if (input.GetDictionary(kPrivetKeyWifi, &wifi)) { | 357 if (input.GetDictionary(kPrivetKeyWifi, &wifi)) { |
400 std::string ssid; | 358 std::string ssid; |
401 | 359 |
402 if (!wifi->GetString(kPrivetKeySSID, &ssid)) { | 360 if (!wifi->GetString(kPrivetKeySSID, &ssid)) { |
(...skipping 13 matching lines...) Expand all Loading... |
416 return; | 374 return; |
417 } | 375 } |
418 | 376 |
419 input_cloned.reset(input.DeepCopy()); | 377 input_cloned.reset(input.DeepCopy()); |
420 input_cloned->SetString(kPrivetKeyPassphraseDotted, found->second); | 378 input_cloned->SetString(kPrivetKeyPassphraseDotted, found->second); |
421 input_actual = input_cloned.get(); | 379 input_actual = input_cloned.get(); |
422 } | 380 } |
423 } | 381 } |
424 } | 382 } |
425 | 383 |
426 GCDSessionMap::iterator found = sessions_.find(session_id); | 384 auto found = sessions_.find(session_id); |
427 | 385 |
428 if (found == sessions_.end()) { | 386 if (found == sessions_.end()) { |
429 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR, | 387 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR, |
430 base::DictionaryValue()); | 388 base::DictionaryValue()); |
431 return; | 389 return; |
432 } | 390 } |
433 | 391 |
434 found->second->SendMessage(api, *input_actual, callback); | 392 found->second->SendMessage(api, *input_actual, callback); |
435 } | 393 } |
436 | 394 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 for (PasswordMap::iterator i = wifi_passwords_.begin(); | 438 for (PasswordMap::iterator i = wifi_passwords_.begin(); |
481 i != wifi_passwords_.end(); | 439 i != wifi_passwords_.end(); |
482 i++) { | 440 i++) { |
483 retval->AppendString(i->first); | 441 retval->AppendString(i->first); |
484 } | 442 } |
485 #endif | 443 #endif |
486 | 444 |
487 return retval.Pass(); | 445 return retval.Pass(); |
488 } | 446 } |
489 | 447 |
490 GcdPrivateRequest::GcdPrivateRequest( | |
491 const std::string& api, | |
492 const base::DictionaryValue& input, | |
493 const GcdPrivateAPIImpl::MessageResponseCallback& callback, | |
494 GcdPrivateSessionHolder* session_holder) | |
495 : api_(api), | |
496 input_(input.DeepCopy()), | |
497 callback_(callback), | |
498 session_holder_(session_holder) { | |
499 } | |
500 | 448 |
501 GcdPrivateRequest::~GcdPrivateRequest() { | |
502 } | |
503 | |
504 std::string GcdPrivateRequest::GetName() { | |
505 return api_; | |
506 } | |
507 | |
508 const base::DictionaryValue& GcdPrivateRequest::GetInput() { | |
509 return *input_; | |
510 } | |
511 | |
512 void GcdPrivateRequest::OnError() { | |
513 RunCallback(gcd_private::STATUS_CONNECTIONERROR, base::DictionaryValue()); | |
514 session_holder_->DeleteRequest(this); | |
515 } | |
516 | |
517 void GcdPrivateRequest::OnParsedJson(const base::DictionaryValue& value, | |
518 bool has_error) { | |
519 RunCallback(gcd_private::STATUS_SUCCESS, value); | |
520 session_holder_->DeleteRequest(this); | |
521 } | |
522 | |
523 void GcdPrivateRequest::RunCallback(gcd_private::Status status, | |
524 const base::DictionaryValue& value) { | |
525 if (callback_.is_null()) | |
526 return; | |
527 callback_.Run(status, value); | |
528 callback_.Reset(); | |
529 } | |
530 | |
531 GcdPrivateSessionHolder::GcdPrivateSessionHolder( | |
532 const std::string& ip_address, | |
533 int port, | |
534 net::URLRequestContextGetter* request_context) { | |
535 std::string host_string; | |
536 net::IPAddressNumber address_number; | |
537 | |
538 if (net::ParseIPLiteralToNumber(ip_address, &address_number) && | |
539 address_number.size() == net::kIPv6AddressSize) { | |
540 host_string = base::StringPrintf("[%s]", ip_address.c_str()); | |
541 } else { | |
542 host_string = ip_address; | |
543 } | |
544 | |
545 http_client_.reset(new local_discovery::PrivetHTTPClientImpl( | |
546 "", net::HostPortPair(host_string, port), request_context)); | |
547 } | |
548 | |
549 GcdPrivateSessionHolder::~GcdPrivateSessionHolder() { | |
550 } | |
551 | |
552 void GcdPrivateSessionHolder::Start(const ConfirmationCodeCallback& callback) { | |
553 confirm_callback_ = callback; | |
554 | |
555 privet_session_.reset( | |
556 new local_discovery::PrivetV3Session(http_client_.Pass(), this)); | |
557 privet_session_->Start(); | |
558 } | |
559 | |
560 void GcdPrivateSessionHolder::ConfirmCode( | |
561 const std::string& code, | |
562 const GcdPrivateAPIImpl::SessionEstablishedCallback& callback) { | |
563 session_established_callback_ = callback; | |
564 privet_session_->ConfirmCode(code); | |
565 } | |
566 | |
567 void GcdPrivateSessionHolder::SendMessage( | |
568 const std::string& api, | |
569 const base::DictionaryValue& input, | |
570 GcdPrivateAPIImpl::MessageResponseCallback callback) { | |
571 GcdPrivateRequest* request = | |
572 new GcdPrivateRequest(api, input, callback, this); | |
573 requests_.push_back(request); | |
574 privet_session_->StartRequest(request); | |
575 } | |
576 | |
577 void GcdPrivateSessionHolder::DeleteRequest(GcdPrivateRequest* request) { | |
578 // TODO(noamsml): Does this need to be optimized? | |
579 for (RequestVector::iterator i = requests_.begin(); i != requests_.end(); | |
580 i++) { | |
581 if (*i == request) { | |
582 requests_.erase(i); | |
583 break; | |
584 } | |
585 } | |
586 } | |
587 | |
588 void GcdPrivateSessionHolder::OnSetupConfirmationNeeded( | |
589 const std::string& confirmation_code, | |
590 gcd_private::ConfirmationType confirmation_type) { | |
591 if (confirm_callback_.is_null()) | |
592 return; | |
593 confirm_callback_.Run( | |
594 gcd_private::STATUS_SUCCESS, confirmation_code, confirmation_type); | |
595 confirm_callback_.Reset(); | |
596 } | |
597 | |
598 void GcdPrivateSessionHolder::OnSessionStatus(gcd_private::Status status) { | |
599 if (!session_established_callback_.is_null()) { | |
600 session_established_callback_.Run(status); | |
601 session_established_callback_.Reset(); | |
602 } | |
603 // Fail all requests created before session established. | |
604 RequestVector tmp_requests; | |
605 tmp_requests.swap(requests_); | |
606 for (GcdPrivateRequest* request : tmp_requests) { | |
607 request->RunCallback(gcd_private::STATUS_SESSIONERROR, | |
608 base::DictionaryValue()); | |
609 } | |
610 } | |
611 | 449 |
612 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) | 450 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) |
613 : impl_(new GcdPrivateAPIImpl(context)) { | 451 : impl_(new GcdPrivateAPIImpl(context)) { |
614 } | 452 } |
615 | 453 |
616 GcdPrivateAPI::~GcdPrivateAPI() { | 454 GcdPrivateAPI::~GcdPrivateAPI() { |
617 } | 455 } |
618 | 456 |
619 // static | 457 // static |
620 BrowserContextKeyedAPIFactory<GcdPrivateAPI>* | 458 BrowserContextKeyedAPIFactory<GcdPrivateAPI>* |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 | 602 |
765 bool GcdPrivateEstablishSessionFunction::RunAsync() { | 603 bool GcdPrivateEstablishSessionFunction::RunAsync() { |
766 scoped_ptr<gcd_private::EstablishSession::Params> params = | 604 scoped_ptr<gcd_private::EstablishSession::Params> params = |
767 gcd_private::EstablishSession::Params::Create(*args_); | 605 gcd_private::EstablishSession::Params::Create(*args_); |
768 | 606 |
769 if (!params) | 607 if (!params) |
770 return false; | 608 return false; |
771 | 609 |
772 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 610 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
773 | 611 |
774 gcd_api->EstablishSession( | 612 GcdPrivateAPIImpl::EstablishSessionCallback callback = base::Bind( |
775 params->ip_address, | 613 &GcdPrivateEstablishSessionFunction::OnSessionInitialized, this); |
776 params->port, | 614 gcd_api->EstablishSession(params->ip_address, params->port, callback); |
777 base::Bind(&GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback, | |
778 this)); | |
779 | 615 |
780 return true; | 616 return true; |
781 } | 617 } |
782 | 618 |
783 void GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback( | 619 void GcdPrivateEstablishSessionFunction::OnSessionInitialized( |
784 int session_id, | 620 int session_id, |
785 gcd_private::Status status, | 621 api::gcd_private::Status status, |
786 const std::string& confirm_code, | 622 const std::vector<api::gcd_private::PairingType>& pairing_types) { |
787 gcd_private::ConfirmationType confirmation_type) { | 623 results_ = gcd_private::EstablishSession::Results::Create(session_id, status, |
788 gcd_private::ConfirmationInfo info; | 624 pairing_types); |
789 | |
790 info.type = confirmation_type; | |
791 if (!confirm_code.empty()) { | |
792 info.code.reset(new std::string(confirm_code)); | |
793 } | |
794 | |
795 results_ = | |
796 gcd_private::EstablishSession::Results::Create(session_id, status, info); | |
797 SendResponse(true); | 625 SendResponse(true); |
798 } | 626 } |
799 | 627 |
| 628 GcdPrivateStartPairingFunction::GcdPrivateStartPairingFunction() { |
| 629 } |
| 630 |
| 631 GcdPrivateStartPairingFunction::~GcdPrivateStartPairingFunction() { |
| 632 } |
| 633 |
| 634 bool GcdPrivateStartPairingFunction::RunAsync() { |
| 635 scoped_ptr<gcd_private::StartPairing::Params> params = |
| 636 gcd_private::StartPairing::Params::Create(*args_); |
| 637 |
| 638 if (!params) |
| 639 return false; |
| 640 |
| 641 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
| 642 |
| 643 gcd_api->StartPairing( |
| 644 params->session_id, params->pairing_type, |
| 645 base::Bind(&GcdPrivateStartPairingFunction::OnPairingStarted, this)); |
| 646 |
| 647 return true; |
| 648 } |
| 649 |
| 650 void GcdPrivateStartPairingFunction::OnPairingStarted( |
| 651 api::gcd_private::Status status) { |
| 652 results_ = gcd_private::StartPairing::Results::Create(status); |
| 653 SendResponse(true); |
| 654 } |
| 655 |
800 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() { | 656 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() { |
801 } | 657 } |
802 | 658 |
803 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() { | 659 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() { |
804 } | 660 } |
805 | 661 |
806 bool GcdPrivateConfirmCodeFunction::RunAsync() { | 662 bool GcdPrivateConfirmCodeFunction::RunAsync() { |
807 scoped_ptr<gcd_private::ConfirmCode::Params> params = | 663 scoped_ptr<gcd_private::ConfirmCode::Params> params = |
808 gcd_private::ConfirmCode::Params::Create(*args_); | 664 gcd_private::ConfirmCode::Params::Create(*args_); |
809 | 665 |
810 if (!params) | 666 if (!params) |
811 return false; | 667 return false; |
812 | 668 |
813 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 669 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
814 | 670 |
815 gcd_api->ConfirmCode( | 671 gcd_api->ConfirmCode( |
816 params->session_id, | 672 params->session_id, params->code, |
817 params->code, | 673 base::Bind(&GcdPrivateConfirmCodeFunction::OnCodeConfirmed, this)); |
818 base::Bind(&GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback, | |
819 this)); | |
820 | 674 |
821 return true; | 675 return true; |
822 } | 676 } |
823 | 677 |
824 void GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback( | 678 void GcdPrivateConfirmCodeFunction::OnCodeConfirmed( |
825 api::gcd_private::Status status) { | 679 api::gcd_private::Status status) { |
826 results_ = gcd_private::ConfirmCode::Results::Create(status); | 680 results_ = gcd_private::ConfirmCode::Results::Create(status); |
827 SendResponse(true); | 681 SendResponse(true); |
828 } | 682 } |
829 | 683 |
830 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() { | 684 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() { |
831 } | 685 } |
832 | 686 |
833 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() { | 687 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() { |
834 } | 688 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
947 } | 801 } |
948 | 802 |
949 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { | 803 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { |
950 } | 804 } |
951 | 805 |
952 bool GcdPrivateGetCommandsListFunction::RunAsync() { | 806 bool GcdPrivateGetCommandsListFunction::RunAsync() { |
953 return false; | 807 return false; |
954 } | 808 } |
955 | 809 |
956 } // namespace extensions | 810 } // namespace extensions |
OLD | NEW |