Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chromeos/dbus/fake_shill_manager_client.cc

Issue 330833003: Rely on Service.Visible instead of Manager.Services (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix stubs for tests Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chromeos/dbus/fake_shill_manager_client.h ('k') | chromeos/dbus/fake_shill_profile_client.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chromeos/dbus/fake_shill_manager_client.h" 5 #include "chromeos/dbus/fake_shill_manager_client.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 const base::DictionaryValue* properties = 57 const base::DictionaryValue* properties =
58 service_client->GetServiceProperties(service_path); 58 service_client->GetServiceProperties(service_path);
59 if (!properties) { 59 if (!properties) {
60 LOG(ERROR) << "Properties not found for service: " << service_path; 60 LOG(ERROR) << "Properties not found for service: " << service_path;
61 continue; 61 continue;
62 } 62 }
63 std::string type; 63 std::string type;
64 properties->GetString(shill::kTypeProperty, &type); 64 properties->GetString(shill::kTypeProperty, &type);
65 if (type != match_type) 65 if (type != match_type)
66 continue; 66 continue;
67 if (!technology_enabled) {
68 std::string profile;
69 properties->GetString(shill::kProfileProperty, &profile);
70 if (profile.empty())
71 continue;
72 }
73 bool visible = false; 67 bool visible = false;
74 if (technology_enabled) 68 if (technology_enabled)
75 properties->GetBoolean(shill::kVisibleProperty, &visible); 69 properties->GetBoolean(shill::kVisibleProperty, &visible);
76 if (!visible) { 70 if (!visible) {
77 disabled_service_list_out->push_back(service_path); 71 disabled_service_list_out->push_back(service_path);
78 continue; 72 continue;
79 } 73 }
80 std::string state; 74 std::string state;
81 properties->GetString(shill::kStateProperty, &state); 75 properties->GetString(shill::kStateProperty, &state);
82 if (state == shill::kStateOnline || 76 if (state == shill::kStateOnline ||
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 stub_properties_.Clear(); 449 stub_properties_.Clear();
456 } 450 }
457 451
458 void FakeShillManagerClient::SetManagerProperty(const std::string& key, 452 void FakeShillManagerClient::SetManagerProperty(const std::string& key,
459 const base::Value& value) { 453 const base::Value& value) {
460 SetProperty(key, value, 454 SetProperty(key, value,
461 base::Bind(&base::DoNothing), base::Bind(&LogErrorCallback)); 455 base::Bind(&base::DoNothing), base::Bind(&LogErrorCallback));
462 } 456 }
463 457
464 void FakeShillManagerClient::AddManagerService( 458 void FakeShillManagerClient::AddManagerService(
465 const std::string& service_path) { 459 const std::string& service_path,
460 bool notify_observers) {
466 DVLOG(2) << "AddManagerService: " << service_path; 461 DVLOG(2) << "AddManagerService: " << service_path;
467 GetListProperty(shill::kServiceCompleteListProperty)->AppendIfNotPresent( 462 GetListProperty(shill::kServiceCompleteListProperty)->AppendIfNotPresent(
468 base::Value::CreateStringValue(service_path)); 463 base::Value::CreateStringValue(service_path));
469 SortManagerServices(false); 464 SortManagerServices(false);
470 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty); 465 if (notify_observers)
471 // SortManagerServices will add the service to Services if it is visible. 466 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
472 const base::ListValue* services = GetListProperty(shill::kServicesProperty);
473 if (services->Find(base::StringValue(service_path)) != services->end())
474 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
475 } 467 }
476 468
477 void FakeShillManagerClient::RemoveManagerService( 469 void FakeShillManagerClient::RemoveManagerService(
478 const std::string& service_path) { 470 const std::string& service_path) {
479 DVLOG(2) << "RemoveManagerService: " << service_path; 471 DVLOG(2) << "RemoveManagerService: " << service_path;
480 base::StringValue service_path_value(service_path); 472 base::StringValue service_path_value(service_path);
481 GetListProperty(shill::kServiceCompleteListProperty)->Remove( 473 GetListProperty(shill::kServiceCompleteListProperty)->Remove(
482 service_path_value, NULL); 474 service_path_value, NULL);
483 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty); 475 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
484 if (GetListProperty(shill::kServicesProperty)->Remove(
485 service_path_value, NULL)) {
486 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
487 }
488 } 476 }
489 477
490 void FakeShillManagerClient::ClearManagerServices() { 478 void FakeShillManagerClient::ClearManagerServices() {
491 DVLOG(1) << "ClearManagerServices"; 479 DVLOG(1) << "ClearManagerServices";
492 GetListProperty(shill::kServiceCompleteListProperty)->Clear(); 480 GetListProperty(shill::kServiceCompleteListProperty)->Clear();
493 GetListProperty(shill::kServicesProperty)->Clear();
494 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty); 481 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
495 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
496 } 482 }
497 483
498 void FakeShillManagerClient::ServiceStateChanged( 484 void FakeShillManagerClient::ServiceStateChanged(
499 const std::string& service_path, 485 const std::string& service_path,
500 const std::string& state) { 486 const std::string& state) {
501 if (service_path == default_service_ && !IsConnectedState(state)) { 487 if (service_path == default_service_ && !IsConnectedState(state)) {
502 // Default service is no longer connected; clear. 488 // Default service is no longer connected; clear.
503 default_service_.clear(); 489 default_service_.clear();
504 base::StringValue default_service_value(default_service_); 490 base::StringValue default_service_value(default_service_);
505 SetManagerProperty(shill::kDefaultServiceProperty, default_service_value); 491 SetManagerProperty(shill::kDefaultServiceProperty, default_service_value);
506 } 492 }
507 } 493 }
508 494
509 void FakeShillManagerClient::SortManagerServices(bool notify) { 495 void FakeShillManagerClient::SortManagerServices(bool notify) {
510 DVLOG(1) << "SortManagerServices"; 496 DVLOG(1) << "SortManagerServices";
511 static const char* ordered_types[] = {shill::kTypeEthernet, 497 static const char* ordered_types[] = {shill::kTypeEthernet,
512 shill::kTypeWifi, 498 shill::kTypeWifi,
513 shill::kTypeCellular, 499 shill::kTypeCellular,
514 shill::kTypeWimax, 500 shill::kTypeWimax,
515 shill::kTypeVPN}; 501 shill::kTypeVPN};
516 502
517 base::ListValue* service_list = GetListProperty(shill::kServicesProperty);
518 scoped_ptr<base::ListValue> prev_service_list(service_list->DeepCopy());
519 base::ListValue* complete_list = 503 base::ListValue* complete_list =
520 GetListProperty(shill::kServiceCompleteListProperty); 504 GetListProperty(shill::kServiceCompleteListProperty);
521 if (complete_list->empty()) 505 if (complete_list->empty())
522 return; 506 return;
523 scoped_ptr<base::ListValue> prev_complete_list(complete_list->DeepCopy()); 507 scoped_ptr<base::ListValue> prev_complete_list(complete_list->DeepCopy());
524 508
525 std::vector<std::string> active_services; 509 std::vector<std::string> active_services;
526 std::vector<std::string> inactive_services; 510 std::vector<std::string> inactive_services;
527 std::vector<std::string> disabled_services; 511 std::vector<std::string> disabled_services;
528 for (size_t i = 0; i < arraysize(ordered_types); ++i) { 512 for (size_t i = 0; i < arraysize(ordered_types); ++i) {
529 AppendServicesForType(complete_list, 513 AppendServicesForType(complete_list,
530 ordered_types[i], 514 ordered_types[i],
531 TechnologyEnabled(ordered_types[i]), 515 TechnologyEnabled(ordered_types[i]),
532 &active_services, 516 &active_services,
533 &inactive_services, 517 &inactive_services,
534 &disabled_services); 518 &disabled_services);
535 } 519 }
536 service_list->Clear();
537 complete_list->Clear(); 520 complete_list->Clear();
538 for (size_t i = 0; i < active_services.size(); ++i) { 521 for (size_t i = 0; i < active_services.size(); ++i)
539 complete_list->AppendString(active_services[i]); 522 complete_list->AppendString(active_services[i]);
540 service_list->AppendString(active_services[i]); 523 for (size_t i = 0; i < inactive_services.size(); ++i)
541 }
542 for (size_t i = 0; i < inactive_services.size(); ++i) {
543 complete_list->AppendString(inactive_services[i]); 524 complete_list->AppendString(inactive_services[i]);
544 service_list->AppendString(inactive_services[i]); 525 for (size_t i = 0; i < disabled_services.size(); ++i)
545 }
546 for (size_t i = 0; i < disabled_services.size(); ++i) {
547 complete_list->AppendString(disabled_services[i]); 526 complete_list->AppendString(disabled_services[i]);
548 }
549 527
550 if (notify) { 528 if (notify && !complete_list->Equals(prev_complete_list.get()))
551 if (!service_list->Equals(prev_service_list.get())) 529 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
552 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
553 if (!complete_list->Equals(prev_complete_list.get()))
554 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
555 }
556 530
557 // Set the first active service as the Default service. 531 // Set the first active service as the Default service.
558 std::string new_default_service; 532 std::string new_default_service;
559 if (!active_services.empty()) { 533 if (!active_services.empty()) {
560 ShillServiceClient::TestInterface* service_client = 534 ShillServiceClient::TestInterface* service_client =
561 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 535 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
562 std::string service_path = active_services[0]; 536 std::string service_path = active_services[0];
563 const base::DictionaryValue* properties = 537 const base::DictionaryValue* properties =
564 service_client->GetServiceProperties(service_path); 538 service_client->GetServiceProperties(service_path);
565 if (!properties) { 539 if (!properties) {
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 782
809 SortManagerServices(true); 783 SortManagerServices(true);
810 } 784 }
811 785
812 // Private methods 786 // Private methods
813 787
814 void FakeShillManagerClient::PassStubProperties( 788 void FakeShillManagerClient::PassStubProperties(
815 const DictionaryValueCallback& callback) const { 789 const DictionaryValueCallback& callback) const {
816 scoped_ptr<base::DictionaryValue> stub_properties( 790 scoped_ptr<base::DictionaryValue> stub_properties(
817 stub_properties_.DeepCopy()); 791 stub_properties_.DeepCopy());
792 stub_properties->SetWithoutPathExpansion(
793 shill::kServiceCompleteListProperty,
794 GetEnabledServiceList(shill::kServiceCompleteListProperty));
818 callback.Run(DBUS_METHOD_CALL_SUCCESS, *stub_properties); 795 callback.Run(DBUS_METHOD_CALL_SUCCESS, *stub_properties);
819 } 796 }
820 797
821 void FakeShillManagerClient::PassStubGeoNetworks( 798 void FakeShillManagerClient::PassStubGeoNetworks(
822 const DictionaryValueCallback& callback) const { 799 const DictionaryValueCallback& callback) const {
823 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_geo_networks_); 800 callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_geo_networks_);
824 } 801 }
825 802
826 void FakeShillManagerClient::CallNotifyObserversPropertyChanged( 803 void FakeShillManagerClient::CallNotifyObserversPropertyChanged(
827 const std::string& property) { 804 const std::string& property) {
828 // Avoid unnecessary delayed task if we have no observers (e.g. during 805 // Avoid unnecessary delayed task if we have no observers (e.g. during
829 // initial setup). 806 // initial setup).
830 if (!observer_list_.might_have_observers()) 807 if (!observer_list_.might_have_observers())
831 return; 808 return;
832 base::MessageLoop::current()->PostTask( 809 base::MessageLoop::current()->PostTask(
833 FROM_HERE, 810 FROM_HERE,
834 base::Bind(&FakeShillManagerClient::NotifyObserversPropertyChanged, 811 base::Bind(&FakeShillManagerClient::NotifyObserversPropertyChanged,
835 weak_ptr_factory_.GetWeakPtr(), 812 weak_ptr_factory_.GetWeakPtr(),
836 property)); 813 property));
837 } 814 }
838 815
839 void FakeShillManagerClient::NotifyObserversPropertyChanged( 816 void FakeShillManagerClient::NotifyObserversPropertyChanged(
840 const std::string& property) { 817 const std::string& property) {
841 DVLOG(1) << "NotifyObserversPropertyChanged: " << property; 818 DVLOG(1) << "NotifyObserversPropertyChanged: " << property;
842 base::Value* value = NULL; 819 base::Value* value = NULL;
843 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { 820 if (!stub_properties_.GetWithoutPathExpansion(property, &value)) {
844 LOG(ERROR) << "Notify for unknown property: " << property; 821 LOG(ERROR) << "Notify for unknown property: " << property;
845 return; 822 return;
846 } 823 }
824 if (property == shill::kServiceCompleteListProperty) {
825 scoped_ptr<base::ListValue> services(GetEnabledServiceList(property));
826 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
827 observer_list_,
828 OnPropertyChanged(property, *(services.get())));
829 return;
830 }
847 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, 831 FOR_EACH_OBSERVER(ShillPropertyChangedObserver,
848 observer_list_, 832 observer_list_,
849 OnPropertyChanged(property, *value)); 833 OnPropertyChanged(property, *value));
850 } 834 }
851 835
852 base::ListValue* FakeShillManagerClient::GetListProperty( 836 base::ListValue* FakeShillManagerClient::GetListProperty(
853 const std::string& property) { 837 const std::string& property) {
854 base::ListValue* list_property = NULL; 838 base::ListValue* list_property = NULL;
855 if (!stub_properties_.GetListWithoutPathExpansion( 839 if (!stub_properties_.GetListWithoutPathExpansion(
856 property, &list_property)) { 840 property, &list_property)) {
(...skipping 27 matching lines...) Expand all
884 enabled_list->AppendIfNotPresent(new base::StringValue(type)); 868 enabled_list->AppendIfNotPresent(new base::StringValue(type));
885 else 869 else
886 enabled_list->Remove(base::StringValue(type), NULL); 870 enabled_list->Remove(base::StringValue(type), NULL);
887 CallNotifyObserversPropertyChanged( 871 CallNotifyObserversPropertyChanged(
888 shill::kEnabledTechnologiesProperty); 872 shill::kEnabledTechnologiesProperty);
889 base::MessageLoop::current()->PostTask(FROM_HERE, callback); 873 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
890 // May affect available services. 874 // May affect available services.
891 SortManagerServices(true); 875 SortManagerServices(true);
892 } 876 }
893 877
878 base::ListValue* FakeShillManagerClient::GetEnabledServiceList(
879 const std::string& property) const {
880 base::ListValue* new_service_list = new base::ListValue;
881 const base::ListValue* service_list;
882 if (stub_properties_.GetListWithoutPathExpansion(property, &service_list)) {
883 ShillServiceClient::TestInterface* service_client =
884 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
885 for (base::ListValue::const_iterator iter = service_list->begin();
886 iter != service_list->end(); ++iter) {
887 std::string service_path;
888 if (!(*iter)->GetAsString(&service_path))
889 continue;
890 const base::DictionaryValue* properties =
891 service_client->GetServiceProperties(service_path);
892 if (!properties) {
893 LOG(ERROR) << "Properties not found for service: " << service_path;
894 continue;
895 }
896 std::string type;
897 properties->GetString(shill::kTypeProperty, &type);
898 if (TechnologyEnabled(type))
899 new_service_list->Append((*iter)->DeepCopy());
900 }
901 }
902 return new_service_list;
903 }
904
894 void FakeShillManagerClient::ScanCompleted(const std::string& device_path, 905 void FakeShillManagerClient::ScanCompleted(const std::string& device_path,
895 const base::Closure& callback) { 906 const base::Closure& callback) {
896 if (!device_path.empty()) { 907 if (!device_path.empty()) {
897 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface()-> 908 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface()->
898 SetDeviceProperty(device_path, 909 SetDeviceProperty(device_path,
899 shill::kScanningProperty, 910 shill::kScanningProperty,
900 base::FundamentalValue(false)); 911 base::FundamentalValue(false));
901 } 912 }
902 DVLOG(2) << "ScanCompleted"; 913 DVLOG(2) << "ScanCompleted";
903 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty); 914 CallNotifyObserversPropertyChanged(shill::kServiceCompleteListProperty);
904 CallNotifyObserversPropertyChanged(shill::kServicesProperty);
905 base::MessageLoop::current()->PostTask(FROM_HERE, callback); 915 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
906 } 916 }
907 917
908 void FakeShillManagerClient::ParseCommandLineSwitch() { 918 void FakeShillManagerClient::ParseCommandLineSwitch() {
909 CommandLine* command_line = CommandLine::ForCurrentProcess(); 919 CommandLine* command_line = CommandLine::ForCurrentProcess();
910 if (command_line->HasSwitch(switches::kShillStub)) { 920 if (command_line->HasSwitch(switches::kShillStub)) {
911 std::string option_str = 921 std::string option_str =
912 command_line->GetSwitchValueASCII(switches::kShillStub); 922 command_line->GetSwitchValueASCII(switches::kShillStub);
913 base::StringPairs string_pairs; 923 base::StringPairs string_pairs;
914 base::SplitStringIntoKeyValuePairs(option_str, '=', ',', &string_pairs); 924 base::SplitStringIntoKeyValuePairs(option_str, '=', ',', &string_pairs);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1014 *enabled = true; 1024 *enabled = true;
1015 if ((state == shill::kStatePortal && type != shill::kTypeWifi) || 1025 if ((state == shill::kStatePortal && type != shill::kTypeWifi) ||
1016 (state == kNetworkActivated && type != shill::kTypeCellular)) { 1026 (state == kNetworkActivated && type != shill::kTypeCellular)) {
1017 LOG(WARNING) << "Invalid state: " << state << " for " << type; 1027 LOG(WARNING) << "Invalid state: " << state << " for " << type;
1018 return shill::kStateIdle; 1028 return shill::kStateIdle;
1019 } 1029 }
1020 return state; 1030 return state;
1021 } 1031 }
1022 1032
1023 } // namespace chromeos 1033 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/fake_shill_manager_client.h ('k') | chromeos/dbus/fake_shill_profile_client.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698