| 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 "", |
| 316 net::HostPortPair(host_string, port), |
| 317 browser_context_->GetRequestContext())); |
| 318 |
| 368 int session_id = last_session_id_++; | 319 int session_id = last_session_id_++; |
| 369 linked_ptr<GcdPrivateSessionHolder> session_handler( | 320 linked_ptr<local_discovery::PrivetV3Session> session_handler( |
| 370 new GcdPrivateSessionHolder( | 321 new local_discovery::PrivetV3Session(http_client.Pass())); |
| 371 ip_address, port, browser_context_->GetRequestContext())); | |
| 372 sessions_[session_id] = session_handler; | 322 sessions_[session_id] = session_handler; |
| 373 session_handler->Start(base::Bind(callback, session_id)); | 323 session_handler->Init(base::Bind(callback, session_id)); |
| 324 } |
| 325 |
| 326 void GcdPrivateAPIImpl::StartPairing(int session_id, |
| 327 api::gcd_private::PairingType pairing_type, |
| 328 const SessionCallback& callback) { |
| 329 auto found = sessions_.find(session_id); |
| 330 |
| 331 if (found == sessions_.end()) |
| 332 return callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR); |
| 333 |
| 334 found->second->StartPairing(pairing_type, callback); |
| 374 } | 335 } |
| 375 | 336 |
| 376 void GcdPrivateAPIImpl::ConfirmCode(int session_id, | 337 void GcdPrivateAPIImpl::ConfirmCode(int session_id, |
| 377 const std::string& code, | 338 const std::string& code, |
| 378 SessionEstablishedCallback callback) { | 339 const SessionCallback& callback) { |
| 379 GCDSessionMap::iterator found = sessions_.find(session_id); | 340 auto found = sessions_.find(session_id); |
| 380 | 341 |
| 381 if (found == sessions_.end()) { | 342 if (found == sessions_.end()) |
| 382 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR); | 343 return callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR); |
| 383 return; | |
| 384 } | |
| 385 | 344 |
| 386 found->second->ConfirmCode(code, callback); | 345 found->second->ConfirmCode(code, callback); |
| 387 } | 346 } |
| 388 | 347 |
| 389 void GcdPrivateAPIImpl::SendMessage(int session_id, | 348 void GcdPrivateAPIImpl::SendMessage(int session_id, |
| 390 const std::string& api, | 349 const std::string& api, |
| 391 const base::DictionaryValue& input, | 350 const base::DictionaryValue& input, |
| 392 MessageResponseCallback callback) { | 351 const MessageResponseCallback& callback) { |
| 393 const base::DictionaryValue* input_actual = &input; | 352 const base::DictionaryValue* input_actual = &input; |
| 394 scoped_ptr<base::DictionaryValue> input_cloned; | 353 scoped_ptr<base::DictionaryValue> input_cloned; |
| 395 | 354 |
| 396 if (api == kPrivatAPISetup) { | 355 if (api == kPrivatAPISetup) { |
| 397 const base::DictionaryValue* wifi = NULL; | 356 const base::DictionaryValue* wifi = NULL; |
| 398 | 357 |
| 399 if (input.GetDictionary(kPrivetKeyWifi, &wifi)) { | 358 if (input.GetDictionary(kPrivetKeyWifi, &wifi)) { |
| 400 std::string ssid; | 359 std::string ssid; |
| 401 | 360 |
| 402 if (!wifi->GetString(kPrivetKeySSID, &ssid)) { | 361 if (!wifi->GetString(kPrivetKeySSID, &ssid)) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 416 return; | 375 return; |
| 417 } | 376 } |
| 418 | 377 |
| 419 input_cloned.reset(input.DeepCopy()); | 378 input_cloned.reset(input.DeepCopy()); |
| 420 input_cloned->SetString(kPrivetKeyPassphraseDotted, found->second); | 379 input_cloned->SetString(kPrivetKeyPassphraseDotted, found->second); |
| 421 input_actual = input_cloned.get(); | 380 input_actual = input_cloned.get(); |
| 422 } | 381 } |
| 423 } | 382 } |
| 424 } | 383 } |
| 425 | 384 |
| 426 GCDSessionMap::iterator found = sessions_.find(session_id); | 385 auto found = sessions_.find(session_id); |
| 427 | 386 |
| 428 if (found == sessions_.end()) { | 387 if (found == sessions_.end()) { |
| 429 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR, | 388 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR, |
| 430 base::DictionaryValue()); | 389 base::DictionaryValue()); |
| 431 return; | 390 return; |
| 432 } | 391 } |
| 433 | 392 |
| 434 found->second->SendMessage(api, *input_actual, callback); | 393 found->second->SendMessage(api, *input_actual, callback); |
| 435 } | 394 } |
| 436 | 395 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 for (PasswordMap::iterator i = wifi_passwords_.begin(); | 439 for (PasswordMap::iterator i = wifi_passwords_.begin(); |
| 481 i != wifi_passwords_.end(); | 440 i != wifi_passwords_.end(); |
| 482 i++) { | 441 i++) { |
| 483 retval->AppendString(i->first); | 442 retval->AppendString(i->first); |
| 484 } | 443 } |
| 485 #endif | 444 #endif |
| 486 | 445 |
| 487 return retval.Pass(); | 446 return retval.Pass(); |
| 488 } | 447 } |
| 489 | 448 |
| 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 | 449 |
| 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 | 450 |
| 612 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) | 451 GcdPrivateAPI::GcdPrivateAPI(content::BrowserContext* context) |
| 613 : impl_(new GcdPrivateAPIImpl(context)) { | 452 : impl_(new GcdPrivateAPIImpl(context)) { |
| 614 } | 453 } |
| 615 | 454 |
| 616 GcdPrivateAPI::~GcdPrivateAPI() { | 455 GcdPrivateAPI::~GcdPrivateAPI() { |
| 617 } | 456 } |
| 618 | 457 |
| 619 // static | 458 // static |
| 620 BrowserContextKeyedAPIFactory<GcdPrivateAPI>* | 459 BrowserContextKeyedAPIFactory<GcdPrivateAPI>* |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 | 603 |
| 765 bool GcdPrivateEstablishSessionFunction::RunAsync() { | 604 bool GcdPrivateEstablishSessionFunction::RunAsync() { |
| 766 scoped_ptr<gcd_private::EstablishSession::Params> params = | 605 scoped_ptr<gcd_private::EstablishSession::Params> params = |
| 767 gcd_private::EstablishSession::Params::Create(*args_); | 606 gcd_private::EstablishSession::Params::Create(*args_); |
| 768 | 607 |
| 769 if (!params) | 608 if (!params) |
| 770 return false; | 609 return false; |
| 771 | 610 |
| 772 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 611 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
| 773 | 612 |
| 774 gcd_api->EstablishSession( | 613 GcdPrivateAPIImpl::EstablishSessionCallback callback = base::Bind( |
| 775 params->ip_address, | 614 &GcdPrivateEstablishSessionFunction::OnSessionInitialized, this); |
| 776 params->port, | 615 gcd_api->EstablishSession(params->ip_address, params->port, callback); |
| 777 base::Bind(&GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback, | |
| 778 this)); | |
| 779 | 616 |
| 780 return true; | 617 return true; |
| 781 } | 618 } |
| 782 | 619 |
| 783 void GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback( | 620 void GcdPrivateEstablishSessionFunction::OnSessionInitialized( |
| 784 int session_id, | 621 int session_id, |
| 785 gcd_private::Status status, | 622 api::gcd_private::Status status, |
| 786 const std::string& confirm_code, | 623 const std::vector<api::gcd_private::PairingType>& pairing_types) { |
| 787 gcd_private::ConfirmationType confirmation_type) { | 624 results_ = gcd_private::EstablishSession::Results::Create( |
| 788 gcd_private::ConfirmationInfo info; | 625 session_id, status, 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); | 626 SendResponse(true); |
| 798 } | 627 } |
| 799 | 628 |
| 629 GcdPrivateStartPairingFunction::GcdPrivateStartPairingFunction() { |
| 630 } |
| 631 |
| 632 GcdPrivateStartPairingFunction::~GcdPrivateStartPairingFunction() { |
| 633 } |
| 634 |
| 635 bool GcdPrivateStartPairingFunction::RunAsync() { |
| 636 scoped_ptr<gcd_private::StartPairing::Params> params = |
| 637 gcd_private::StartPairing::Params::Create(*args_); |
| 638 |
| 639 if (!params) |
| 640 return false; |
| 641 |
| 642 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
| 643 |
| 644 gcd_api->StartPairing( |
| 645 params->session_id, |
| 646 params->pairing_type, |
| 647 base::Bind(&GcdPrivateStartPairingFunction::OnPairingStarted, this)); |
| 648 |
| 649 return true; |
| 650 } |
| 651 |
| 652 void GcdPrivateStartPairingFunction::OnPairingStarted( |
| 653 api::gcd_private::Status status) { |
| 654 results_ = gcd_private::StartPairing::Results::Create(status); |
| 655 SendResponse(true); |
| 656 } |
| 657 |
| 800 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() { | 658 GcdPrivateConfirmCodeFunction::GcdPrivateConfirmCodeFunction() { |
| 801 } | 659 } |
| 802 | 660 |
| 803 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() { | 661 GcdPrivateConfirmCodeFunction::~GcdPrivateConfirmCodeFunction() { |
| 804 } | 662 } |
| 805 | 663 |
| 806 bool GcdPrivateConfirmCodeFunction::RunAsync() { | 664 bool GcdPrivateConfirmCodeFunction::RunAsync() { |
| 807 scoped_ptr<gcd_private::ConfirmCode::Params> params = | 665 scoped_ptr<gcd_private::ConfirmCode::Params> params = |
| 808 gcd_private::ConfirmCode::Params::Create(*args_); | 666 gcd_private::ConfirmCode::Params::Create(*args_); |
| 809 | 667 |
| 810 if (!params) | 668 if (!params) |
| 811 return false; | 669 return false; |
| 812 | 670 |
| 813 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 671 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
| 814 | 672 |
| 815 gcd_api->ConfirmCode( | 673 gcd_api->ConfirmCode( |
| 816 params->session_id, | 674 params->session_id, |
| 817 params->code, | 675 params->code, |
| 818 base::Bind(&GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback, | 676 base::Bind(&GcdPrivateConfirmCodeFunction::OnCodeConfirmed, this)); |
| 819 this)); | |
| 820 | 677 |
| 821 return true; | 678 return true; |
| 822 } | 679 } |
| 823 | 680 |
| 824 void GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback( | 681 void GcdPrivateConfirmCodeFunction::OnCodeConfirmed( |
| 825 api::gcd_private::Status status) { | 682 api::gcd_private::Status status) { |
| 826 results_ = gcd_private::ConfirmCode::Results::Create(status); | 683 results_ = gcd_private::ConfirmCode::Results::Create(status); |
| 827 SendResponse(true); | 684 SendResponse(true); |
| 828 } | 685 } |
| 829 | 686 |
| 830 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() { | 687 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() { |
| 831 } | 688 } |
| 832 | 689 |
| 833 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() { | 690 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() { |
| 834 } | 691 } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 947 } | 804 } |
| 948 | 805 |
| 949 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { | 806 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { |
| 950 } | 807 } |
| 951 | 808 |
| 952 bool GcdPrivateGetCommandsListFunction::RunAsync() { | 809 bool GcdPrivateGetCommandsListFunction::RunAsync() { |
| 953 return false; | 810 return false; |
| 954 } | 811 } |
| 955 | 812 |
| 956 } // namespace extensions | 813 } // namespace extensions |
| OLD | NEW |