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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 void SendMessage(int session_id, | 129 void SendMessage(int session_id, |
130 const std::string& api, | 130 const std::string& api, |
131 const base::DictionaryValue& input, | 131 const base::DictionaryValue& input, |
132 MessageResponseCallback callback); | 132 MessageResponseCallback callback); |
133 | 133 |
134 void RequestWifiPassword(const std::string& ssid, | 134 void RequestWifiPassword(const std::string& ssid, |
135 const SuccessCallback& callback); | 135 const SuccessCallback& callback); |
136 | 136 |
137 void RemoveSession(int session_id); | 137 void RemoveSession(int session_id); |
138 | 138 |
139 scoped_ptr<base::ListValue> GetPrefetchedSSIDList(); | |
140 | |
141 private: | 139 private: |
142 typedef std::map<std::string /* id_string */, | 140 typedef std::map<std::string /* id_string */, |
143 linked_ptr<api::gcd_private::GCDDevice> > GCDDeviceMap; | 141 linked_ptr<api::gcd_private::GCDDevice> > GCDDeviceMap; |
144 | 142 |
145 typedef std::map<int /* session id*/, linked_ptr<GcdPrivateSessionHolder> > | 143 typedef std::map<int /* session id*/, linked_ptr<GcdPrivateSessionHolder> > |
146 GCDSessionMap; | 144 GCDSessionMap; |
147 | 145 |
148 typedef std::map<std::string /* ssid */, std::string /* password */> | 146 typedef std::map<std::string /* ssid */, std::string /* password */> |
149 PasswordMap; | 147 PasswordMap; |
150 | 148 |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 const std::string& api, | 384 const std::string& api, |
387 const base::DictionaryValue& input, | 385 const base::DictionaryValue& input, |
388 MessageResponseCallback callback) { | 386 MessageResponseCallback callback) { |
389 const base::DictionaryValue* input_actual = &input; | 387 const base::DictionaryValue* input_actual = &input; |
390 #if defined(ENABLE_WIFI_BOOTSTRAPPING) | 388 #if defined(ENABLE_WIFI_BOOTSTRAPPING) |
391 scoped_ptr<base::DictionaryValue> input_cloned; | 389 scoped_ptr<base::DictionaryValue> input_cloned; |
392 | 390 |
393 if (api == kPrivatAPISetup) { | 391 if (api == kPrivatAPISetup) { |
394 const base::DictionaryValue* wifi = NULL; | 392 const base::DictionaryValue* wifi = NULL; |
395 | 393 |
396 if (input.GetDictionary(kPrivetKeyWifi, &wifi)) { | 394 if (input.GetDictionary(kPrivetKeyWifi, &wifi) && |
| 395 !wifi->HasKey(kPrivetKeyPassphrase)) { |
| 396 // If the message is a setup message, has a wifi section, try sending the |
| 397 // passphrase. |
397 std::string ssid; | 398 std::string ssid; |
398 | 399 |
399 if (!wifi->GetString(kPrivetKeySSID, &ssid)) { | 400 if (!wifi->GetString(kPrivetKeySSID, &ssid)) { |
400 callback.Run(gcd_private::STATUS_SETUPPARSEERROR, | 401 callback.Run(gcd_private::STATUS_SETUPPARSEERROR, |
401 base::DictionaryValue()); | 402 base::DictionaryValue()); |
402 return; | 403 return; |
403 } | 404 } |
404 | 405 |
405 if (!wifi->HasKey(kPrivetKeyPassphrase)) { | 406 PasswordMap::iterator found = wifi_passwords_.find(ssid); |
406 // If the message is a setup message, has a wifi section, try sending | 407 if (found == wifi_passwords_.end()) { |
407 // the passphrase. | 408 callback.Run(gcd_private::STATUS_WIFIPASSWORDERROR, |
| 409 base::DictionaryValue()); |
| 410 return; |
| 411 } |
408 | 412 |
409 PasswordMap::iterator found = wifi_passwords_.find(ssid); | 413 input_cloned.reset(input.DeepCopy()); |
410 if (found == wifi_passwords_.end()) { | 414 input_cloned->SetString(kPrivetKeyPassphraseDotted, found->second); |
411 callback.Run(gcd_private::STATUS_WIFIPASSWORDERROR, | 415 input_actual = input_cloned.get(); |
412 base::DictionaryValue()); | |
413 return; | |
414 } | |
415 | |
416 input_cloned.reset(input.DeepCopy()); | |
417 input_cloned->SetString(kPrivetKeyPassphraseDotted, found->second); | |
418 input_actual = input_cloned.get(); | |
419 } | |
420 } | 416 } |
421 } | 417 } |
422 #endif | 418 #endif |
423 | 419 |
424 GCDSessionMap::iterator found = sessions_.find(session_id); | 420 GCDSessionMap::iterator found = sessions_.find(session_id); |
425 | 421 |
426 if (found == sessions_.end()) { | 422 if (found == sessions_.end()) { |
427 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR, | 423 callback.Run(gcd_private::STATUS_UNKNOWNSESSIONERROR, |
428 base::DictionaryValue()); | 424 base::DictionaryValue()); |
429 return; | 425 return; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 wifi_manager_->Start(); | 460 wifi_manager_->Start(); |
465 } | 461 } |
466 } | 462 } |
467 | 463 |
468 #endif | 464 #endif |
469 | 465 |
470 void GcdPrivateAPIImpl::RemoveSession(int session_id) { | 466 void GcdPrivateAPIImpl::RemoveSession(int session_id) { |
471 sessions_.erase(session_id); | 467 sessions_.erase(session_id); |
472 } | 468 } |
473 | 469 |
474 scoped_ptr<base::ListValue> GcdPrivateAPIImpl::GetPrefetchedSSIDList() { | |
475 scoped_ptr<base::ListValue> retval(new base::ListValue); | |
476 | |
477 #if defined(ENABLE_WIFI_BOOTSTRAPPING) | |
478 for (PasswordMap::iterator i = wifi_passwords_.begin(); | |
479 i != wifi_passwords_.end(); | |
480 i++) { | |
481 retval->AppendString(i->first); | |
482 } | |
483 #endif | |
484 | |
485 return retval.Pass(); | |
486 } | |
487 | |
488 GcdPrivateRequest::GcdPrivateRequest( | 470 GcdPrivateRequest::GcdPrivateRequest( |
489 const std::string& api, | 471 const std::string& api, |
490 const base::DictionaryValue& input, | 472 const base::DictionaryValue& input, |
491 const GcdPrivateAPIImpl::MessageResponseCallback& callback, | 473 const GcdPrivateAPIImpl::MessageResponseCallback& callback, |
492 GcdPrivateSessionHolder* session_holder) | 474 GcdPrivateSessionHolder* session_holder) |
493 : api_(api), | 475 : api_(api), |
494 input_(input.DeepCopy()), | 476 input_(input.DeepCopy()), |
495 callback_(callback), | 477 callback_(callback), |
496 session_holder_(session_holder) { | 478 session_holder_(session_holder) { |
497 } | 479 } |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 GcdPrivateQueryForNewLocalDevicesFunction() { | 679 GcdPrivateQueryForNewLocalDevicesFunction() { |
698 } | 680 } |
699 | 681 |
700 GcdPrivateQueryForNewLocalDevicesFunction:: | 682 GcdPrivateQueryForNewLocalDevicesFunction:: |
701 ~GcdPrivateQueryForNewLocalDevicesFunction() { | 683 ~GcdPrivateQueryForNewLocalDevicesFunction() { |
702 } | 684 } |
703 | 685 |
704 bool GcdPrivateQueryForNewLocalDevicesFunction::RunSync() { | 686 bool GcdPrivateQueryForNewLocalDevicesFunction::RunSync() { |
705 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 687 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
706 | 688 |
| 689 if (!gcd_api) |
| 690 return false; |
| 691 |
707 if (!gcd_api->QueryForDevices()) { | 692 if (!gcd_api->QueryForDevices()) { |
708 error_ = | 693 error_ = |
709 "You must first subscribe to onDeviceStateChanged or onDeviceRemoved " | 694 "You must first subscribe to onDeviceStateChanged or onDeviceRemoved " |
710 "notifications"; | 695 "notifications"; |
711 return false; | 696 return false; |
712 } | 697 } |
713 | 698 |
714 return true; | 699 return true; |
715 } | 700 } |
716 | 701 |
717 GcdPrivatePrefetchWifiPasswordFunction:: | 702 GcdPrivatePrefetchWifiPasswordFunction:: |
718 GcdPrivatePrefetchWifiPasswordFunction() { | 703 GcdPrivatePrefetchWifiPasswordFunction() { |
719 } | 704 } |
720 | 705 |
721 GcdPrivatePrefetchWifiPasswordFunction:: | 706 GcdPrivatePrefetchWifiPasswordFunction:: |
722 ~GcdPrivatePrefetchWifiPasswordFunction() { | 707 ~GcdPrivatePrefetchWifiPasswordFunction() { |
723 } | 708 } |
724 | 709 |
725 bool GcdPrivatePrefetchWifiPasswordFunction::RunAsync() { | 710 bool GcdPrivatePrefetchWifiPasswordFunction::RunAsync() { |
726 scoped_ptr<gcd_private::PrefetchWifiPassword::Params> params = | 711 scoped_ptr<gcd_private::PrefetchWifiPassword::Params> params = |
727 gcd_private::PrefetchWifiPassword::Params::Create(*args_); | 712 gcd_private::PrefetchWifiPassword::Params::Create(*args_); |
728 | 713 |
729 if (!params) | 714 if (!params) |
730 return false; | 715 return false; |
731 | 716 |
732 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 717 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
733 | 718 |
| 719 if (!gcd_api) |
| 720 return false; |
| 721 |
734 gcd_api->RequestWifiPassword( | 722 gcd_api->RequestWifiPassword( |
735 params->ssid, | 723 params->ssid, |
736 base::Bind(&GcdPrivatePrefetchWifiPasswordFunction::OnResponse, this)); | 724 base::Bind(&GcdPrivatePrefetchWifiPasswordFunction::OnResponse, this)); |
737 | 725 |
738 return true; | 726 return true; |
739 } | 727 } |
740 | 728 |
741 void GcdPrivatePrefetchWifiPasswordFunction::OnResponse(bool response) { | 729 void GcdPrivatePrefetchWifiPasswordFunction::OnResponse(bool response) { |
742 scoped_ptr<base::FundamentalValue> response_value( | 730 scoped_ptr<base::FundamentalValue> response_value( |
743 new base::FundamentalValue(response)); | 731 new base::FundamentalValue(response)); |
744 SetResult(response_value.release()); | 732 SetResult(response_value.release()); |
745 SendResponse(true); | 733 SendResponse(true); |
746 } | 734 } |
747 | 735 |
748 GcdPrivateEstablishSessionFunction::GcdPrivateEstablishSessionFunction() { | 736 GcdPrivateEstablishSessionFunction::GcdPrivateEstablishSessionFunction() { |
749 } | 737 } |
750 | 738 |
751 GcdPrivateEstablishSessionFunction::~GcdPrivateEstablishSessionFunction() { | 739 GcdPrivateEstablishSessionFunction::~GcdPrivateEstablishSessionFunction() { |
752 } | 740 } |
753 | 741 |
754 bool GcdPrivateEstablishSessionFunction::RunAsync() { | 742 bool GcdPrivateEstablishSessionFunction::RunAsync() { |
755 scoped_ptr<gcd_private::EstablishSession::Params> params = | 743 scoped_ptr<gcd_private::EstablishSession::Params> params = |
756 gcd_private::EstablishSession::Params::Create(*args_); | 744 gcd_private::EstablishSession::Params::Create(*args_); |
757 | 745 |
758 if (!params) | 746 if (!params) |
759 return false; | 747 return false; |
760 | 748 |
761 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 749 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
762 | 750 |
| 751 if (!gcd_api) |
| 752 return false; |
| 753 |
763 gcd_api->EstablishSession( | 754 gcd_api->EstablishSession( |
764 params->ip_address, | 755 params->ip_address, |
765 params->port, | 756 params->port, |
766 base::Bind(&GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback, | 757 base::Bind(&GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback, |
767 this)); | 758 this)); |
768 | 759 |
769 return true; | 760 return true; |
770 } | 761 } |
771 | 762 |
772 void GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback( | 763 void GcdPrivateEstablishSessionFunction::OnConfirmCodeCallback( |
(...skipping 14 matching lines...) Expand all Loading... |
787 | 778 |
788 bool GcdPrivateConfirmCodeFunction::RunAsync() { | 779 bool GcdPrivateConfirmCodeFunction::RunAsync() { |
789 scoped_ptr<gcd_private::ConfirmCode::Params> params = | 780 scoped_ptr<gcd_private::ConfirmCode::Params> params = |
790 gcd_private::ConfirmCode::Params::Create(*args_); | 781 gcd_private::ConfirmCode::Params::Create(*args_); |
791 | 782 |
792 if (!params) | 783 if (!params) |
793 return false; | 784 return false; |
794 | 785 |
795 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 786 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
796 | 787 |
| 788 if (!gcd_api) |
| 789 return false; |
| 790 |
797 gcd_api->ConfirmCode( | 791 gcd_api->ConfirmCode( |
798 params->session_id, | 792 params->session_id, |
799 base::Bind(&GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback, | 793 base::Bind(&GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback, |
800 this)); | 794 this)); |
801 | 795 |
802 return true; | 796 return true; |
803 } | 797 } |
804 | 798 |
805 void GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback( | 799 void GcdPrivateConfirmCodeFunction::OnSessionEstablishedCallback( |
806 api::gcd_private::Status status) { | 800 api::gcd_private::Status status) { |
807 results_ = gcd_private::ConfirmCode::Results::Create(status); | 801 results_ = gcd_private::ConfirmCode::Results::Create(status); |
808 SendResponse(true); | 802 SendResponse(true); |
809 } | 803 } |
810 | 804 |
811 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() { | 805 GcdPrivateSendMessageFunction::GcdPrivateSendMessageFunction() { |
812 } | 806 } |
813 | 807 |
814 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() { | 808 GcdPrivateSendMessageFunction::~GcdPrivateSendMessageFunction() { |
815 } | 809 } |
816 | 810 |
817 bool GcdPrivateSendMessageFunction::RunAsync() { | 811 bool GcdPrivateSendMessageFunction::RunAsync() { |
818 scoped_ptr<gcd_private::PassMessage::Params> params = | 812 scoped_ptr<gcd_private::PassMessage::Params> params = |
819 gcd_private::PassMessage::Params::Create(*args_); | 813 gcd_private::PassMessage::Params::Create(*args_); |
820 | 814 |
821 if (!params) | 815 if (!params) |
822 return false; | 816 return false; |
823 | 817 |
824 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 818 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
825 | 819 |
| 820 if (!gcd_api) |
| 821 return false; |
826 | 822 |
827 gcd_api->SendMessage( | 823 gcd_api->SendMessage( |
828 params->session_id, | 824 params->session_id, |
829 params->api, | 825 params->api, |
830 params->input.additional_properties, | 826 params->input.additional_properties, |
831 base::Bind(&GcdPrivateSendMessageFunction::OnMessageSentCallback, this)); | 827 base::Bind(&GcdPrivateSendMessageFunction::OnMessageSentCallback, this)); |
832 | 828 |
833 return true; | 829 return true; |
834 } | 830 } |
835 | 831 |
(...skipping 15 matching lines...) Expand all Loading... |
851 | 847 |
852 bool GcdPrivateTerminateSessionFunction::RunAsync() { | 848 bool GcdPrivateTerminateSessionFunction::RunAsync() { |
853 scoped_ptr<gcd_private::TerminateSession::Params> params = | 849 scoped_ptr<gcd_private::TerminateSession::Params> params = |
854 gcd_private::TerminateSession::Params::Create(*args_); | 850 gcd_private::TerminateSession::Params::Create(*args_); |
855 | 851 |
856 if (!params) | 852 if (!params) |
857 return false; | 853 return false; |
858 | 854 |
859 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | 855 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); |
860 | 856 |
| 857 if (!gcd_api) |
| 858 return false; |
| 859 |
861 gcd_api->RemoveSession(params->session_id); | 860 gcd_api->RemoveSession(params->session_id); |
862 | 861 |
863 SendResponse(true); | 862 SendResponse(true); |
864 return true; | 863 return true; |
865 } | 864 } |
866 | 865 |
867 GcdPrivateGetCommandDefinitionsFunction:: | 866 GcdPrivateGetCommandDefinitionsFunction:: |
868 GcdPrivateGetCommandDefinitionsFunction() { | 867 GcdPrivateGetCommandDefinitionsFunction() { |
869 } | 868 } |
870 | 869 |
871 GcdPrivateGetCommandDefinitionsFunction:: | 870 GcdPrivateGetCommandDefinitionsFunction:: |
872 ~GcdPrivateGetCommandDefinitionsFunction() { | 871 ~GcdPrivateGetCommandDefinitionsFunction() { |
873 } | 872 } |
874 | 873 |
875 GcdPrivateGetPrefetchedWifiNameListFunction:: | |
876 GcdPrivateGetPrefetchedWifiNameListFunction() { | |
877 } | |
878 | |
879 GcdPrivateGetPrefetchedWifiNameListFunction:: | |
880 ~GcdPrivateGetPrefetchedWifiNameListFunction() { | |
881 } | |
882 | |
883 bool GcdPrivateGetPrefetchedWifiNameListFunction::RunSync() { | |
884 GcdPrivateAPIImpl* gcd_api = GcdPrivateAPIImpl::Get(GetProfile()); | |
885 | |
886 scoped_ptr<base::ListValue> ssid_list = gcd_api->GetPrefetchedSSIDList(); | |
887 | |
888 SetResult(ssid_list.release()); | |
889 | |
890 return true; | |
891 } | |
892 | |
893 bool GcdPrivateGetCommandDefinitionsFunction::RunAsync() { | 874 bool GcdPrivateGetCommandDefinitionsFunction::RunAsync() { |
894 return false; | 875 return false; |
895 } | 876 } |
896 | 877 |
897 GcdPrivateInsertCommandFunction::GcdPrivateInsertCommandFunction() { | 878 GcdPrivateInsertCommandFunction::GcdPrivateInsertCommandFunction() { |
898 } | 879 } |
899 | 880 |
900 GcdPrivateInsertCommandFunction::~GcdPrivateInsertCommandFunction() { | 881 GcdPrivateInsertCommandFunction::~GcdPrivateInsertCommandFunction() { |
901 } | 882 } |
902 | 883 |
(...skipping 25 matching lines...) Expand all Loading... |
928 } | 909 } |
929 | 910 |
930 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { | 911 GcdPrivateGetCommandsListFunction::~GcdPrivateGetCommandsListFunction() { |
931 } | 912 } |
932 | 913 |
933 bool GcdPrivateGetCommandsListFunction::RunAsync() { | 914 bool GcdPrivateGetCommandsListFunction::RunAsync() { |
934 return false; | 915 return false; |
935 } | 916 } |
936 | 917 |
937 } // namespace extensions | 918 } // namespace extensions |
OLD | NEW |