OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/automation/testing_automation_provider.h" | 5 #include "chrome/browser/automation/testing_automation_provider.h" |
6 | 6 |
7 #include "app/message_box_flags.h" | 7 #include "app/message_box_flags.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/json/string_escape.h" | 11 #include "base/json/string_escape.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/stringprintf.h" |
13 #include "base/time.h" | 14 #include "base/time.h" |
14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
15 #include "chrome/app/chrome_dll_resource.h" | 16 #include "chrome/app/chrome_dll_resource.h" |
16 #include "chrome/browser/app_modal_dialog.h" | 17 #include "chrome/browser/app_modal_dialog.h" |
17 #include "chrome/browser/app_modal_dialog_queue.h" | 18 #include "chrome/browser/app_modal_dialog_queue.h" |
18 #include "chrome/browser/autocomplete/autocomplete.h" | 19 #include "chrome/browser/autocomplete/autocomplete.h" |
19 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 20 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
20 #include "chrome/browser/autocomplete/autocomplete_match.h" | 21 #include "chrome/browser/autocomplete/autocomplete_match.h" |
21 #include "chrome/browser/autofill/autofill_manager.h" | 22 #include "chrome/browser/autofill/autofill_manager.h" |
22 #include "chrome/browser/automation/automation_autocomplete_edit_tracker.h" | 23 #include "chrome/browser/automation/automation_autocomplete_edit_tracker.h" |
(...skipping 2083 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2106 handler_map["FillAutoFillProfile"] = | 2107 handler_map["FillAutoFillProfile"] = |
2107 &TestingAutomationProvider::FillAutoFillProfile; | 2108 &TestingAutomationProvider::FillAutoFillProfile; |
2108 | 2109 |
2109 handler_map["GetActiveNotifications"] = | 2110 handler_map["GetActiveNotifications"] = |
2110 &TestingAutomationProvider::GetActiveNotifications; | 2111 &TestingAutomationProvider::GetActiveNotifications; |
2111 handler_map["CloseNotification"] = | 2112 handler_map["CloseNotification"] = |
2112 &TestingAutomationProvider::CloseNotification; | 2113 &TestingAutomationProvider::CloseNotification; |
2113 handler_map["WaitForNotificationCount"] = | 2114 handler_map["WaitForNotificationCount"] = |
2114 &TestingAutomationProvider::WaitForNotificationCount; | 2115 &TestingAutomationProvider::WaitForNotificationCount; |
2115 | 2116 |
| 2117 handler_map["SignInToSync"] = &TestingAutomationProvider::SignInToSync; |
| 2118 handler_map["GetSyncInfo"] = &TestingAutomationProvider::GetSyncInfo; |
| 2119 handler_map["AwaitSyncCycleCompletion"] = |
| 2120 &TestingAutomationProvider::AwaitSyncCycleCompletion; |
| 2121 handler_map["EnableSyncForDatatypes"] = |
| 2122 &TestingAutomationProvider::EnableSyncForDatatypes; |
| 2123 handler_map["DisableSyncForDatatypes"] = |
| 2124 &TestingAutomationProvider::DisableSyncForDatatypes; |
| 2125 |
2116 if (handler_map.find(std::string(command)) != handler_map.end()) { | 2126 if (handler_map.find(std::string(command)) != handler_map.end()) { |
2117 (this->*handler_map[command])(browser, dict_value, reply_message); | 2127 (this->*handler_map[command])(browser, dict_value, reply_message); |
2118 } else { | 2128 } else { |
2119 std::string error_string = "Unknown command. Options: "; | 2129 std::string error_string = "Unknown command. Options: "; |
2120 for (std::map<std::string, JsonHandler>::const_iterator it = | 2130 for (std::map<std::string, JsonHandler>::const_iterator it = |
2121 handler_map.begin(); it != handler_map.end(); ++it) { | 2131 handler_map.begin(); it != handler_map.end(); ++it) { |
2122 error_string += it->first + ", "; | 2132 error_string += it->first + ", "; |
2123 } | 2133 } |
2124 AutomationJSONReply(this, reply_message).SendError(error_string); | 2134 AutomationJSONReply(this, reply_message).SendError(error_string); |
2125 } | 2135 } |
(...skipping 1697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3823 reply.SendError("No PersonalDataManager."); | 3833 reply.SendError("No PersonalDataManager."); |
3824 return; | 3834 return; |
3825 } | 3835 } |
3826 } else { | 3836 } else { |
3827 reply.SendError("No tab at that index."); | 3837 reply.SendError("No tab at that index."); |
3828 return; | 3838 return; |
3829 } | 3839 } |
3830 reply.SendSuccess(NULL); | 3840 reply.SendSuccess(NULL); |
3831 } | 3841 } |
3832 | 3842 |
| 3843 // Sample json output: { "success": true } |
| 3844 void TestingAutomationProvider::SignInToSync(Browser* browser, |
| 3845 DictionaryValue* args, |
| 3846 IPC::Message* reply_message) { |
| 3847 AutomationJSONReply reply(this, reply_message); |
| 3848 std::string username; |
| 3849 std::string password; |
| 3850 if (!args->GetString("username", &username) || |
| 3851 !args->GetString("password", &password)) { |
| 3852 reply.SendError("Invalid or missing args"); |
| 3853 return; |
| 3854 } |
| 3855 if (sync_waiter_.get() == NULL) { |
| 3856 sync_waiter_.reset(new ProfileSyncServiceHarness( |
| 3857 browser->profile(), username, password, 0)); |
| 3858 } else { |
| 3859 sync_waiter_->SetCredentials(username, password); |
| 3860 } |
| 3861 if (sync_waiter_->SetupSync()) { |
| 3862 DictionaryValue* return_value = new DictionaryValue; |
| 3863 return_value->SetBoolean("success", true); |
| 3864 reply.SendSuccess(return_value); |
| 3865 } else { |
| 3866 reply.SendError("Signing in to sync was unsuccessful"); |
| 3867 } |
| 3868 } |
| 3869 |
| 3870 // Sample json output: |
| 3871 // {u'summary': u'SYNC DISABLED'} |
| 3872 // |
| 3873 // { u'authenticated': True, |
| 3874 // u'last synced': u'Just now', |
| 3875 // u'summary': u'READY', |
| 3876 // u'sync url': u'clients4.google.com', |
| 3877 // u'synced datatypes': [ u'Bookmarks', |
| 3878 // u'Preferences', |
| 3879 // u'Passwords', |
| 3880 // u'Autofill', |
| 3881 // u'Themes', |
| 3882 // u'Extensions', |
| 3883 // u'Apps']} |
| 3884 void TestingAutomationProvider::GetSyncInfo(Browser* browser, |
| 3885 DictionaryValue* args, |
| 3886 IPC::Message* reply_message) { |
| 3887 AutomationJSONReply reply(this, reply_message); |
| 3888 DictionaryValue* sync_info = new DictionaryValue; |
| 3889 DictionaryValue* return_value = new DictionaryValue; |
| 3890 if (sync_waiter_.get() == NULL) { |
| 3891 sync_waiter_.reset( |
| 3892 ProfileSyncServiceHarness::CreateAndAttach(browser->profile())); |
| 3893 } |
| 3894 if (!sync_waiter_->IsSyncAlreadySetup()) { |
| 3895 sync_info->SetString("summary", "SYNC DISABLED"); |
| 3896 } else { |
| 3897 ProfileSyncService* service = sync_waiter_->service(); |
| 3898 ProfileSyncService::Status status = sync_waiter_->GetStatus(); |
| 3899 sync_info->SetString("summary", |
| 3900 ProfileSyncService::BuildSyncStatusSummaryText(status.summary)); |
| 3901 sync_info->SetString("sync url", service->sync_service_url().host()); |
| 3902 sync_info->SetBoolean("authenticated", status.authenticated); |
| 3903 sync_info->SetString("last synced", service->GetLastSyncedTimeString()); |
| 3904 ListValue* synced_datatype_list = new ListValue; |
| 3905 syncable::ModelTypeSet synced_datatypes; |
| 3906 service->GetPreferredDataTypes(&synced_datatypes); |
| 3907 for (syncable::ModelTypeSet::iterator it = synced_datatypes.begin(); |
| 3908 it != synced_datatypes.end(); ++it) { |
| 3909 synced_datatype_list->Append( |
| 3910 new StringValue(syncable::ModelTypeToString(*it))); |
| 3911 } |
| 3912 sync_info->Set("synced datatypes", synced_datatype_list); |
| 3913 } |
| 3914 return_value->Set("sync_info", sync_info); |
| 3915 reply.SendSuccess(return_value); |
| 3916 } |
| 3917 |
| 3918 // Sample json output: { "success": true } |
| 3919 void TestingAutomationProvider::AwaitSyncCycleCompletion( |
| 3920 Browser* browser, |
| 3921 DictionaryValue* args, |
| 3922 IPC::Message* reply_message) { |
| 3923 AutomationJSONReply reply(this, reply_message); |
| 3924 if (sync_waiter_.get() == NULL) { |
| 3925 sync_waiter_.reset( |
| 3926 ProfileSyncServiceHarness::CreateAndAttach(browser->profile())); |
| 3927 } |
| 3928 if (!sync_waiter_->IsSyncAlreadySetup()) { |
| 3929 reply.SendError("Not signed in to sync"); |
| 3930 return; |
| 3931 } |
| 3932 sync_waiter_->AwaitSyncCycleCompletion("Waiting for sync cycle"); |
| 3933 ProfileSyncService::Status status = sync_waiter_->GetStatus(); |
| 3934 if (status.summary == ProfileSyncService::Status::READY) { |
| 3935 scoped_ptr<DictionaryValue> return_value(new DictionaryValue); |
| 3936 return_value->SetBoolean("success", true); |
| 3937 reply.SendSuccess(return_value.get()); |
| 3938 } else { |
| 3939 reply.SendError("Wait for sync cycle was unsuccessful"); |
| 3940 } |
| 3941 } |
| 3942 |
| 3943 // Refer to EnableSyncForDatatypes() in chrome/test/pyautolib/pyauto.py for |
| 3944 // sample json input. Sample json output: { "success": true } |
| 3945 void TestingAutomationProvider::EnableSyncForDatatypes( |
| 3946 Browser* browser, |
| 3947 DictionaryValue* args, |
| 3948 IPC::Message* reply_message) { |
| 3949 AutomationJSONReply reply(this, reply_message); |
| 3950 if (sync_waiter_.get() == NULL) { |
| 3951 sync_waiter_.reset( |
| 3952 ProfileSyncServiceHarness::CreateAndAttach(browser->profile())); |
| 3953 } |
| 3954 if (!sync_waiter_->IsSyncAlreadySetup()) { |
| 3955 reply.SendError("Not signed in to sync"); |
| 3956 return; |
| 3957 } |
| 3958 ListValue* datatypes = NULL; |
| 3959 if (!args->GetList("datatypes", &datatypes)) { |
| 3960 reply.SendError("Invalid or missing args"); |
| 3961 return; |
| 3962 } |
| 3963 std::string first_datatype; |
| 3964 datatypes->GetString(0, &first_datatype); |
| 3965 if (first_datatype == "All") { |
| 3966 sync_waiter_->EnableSyncForAllDatatypes(); |
| 3967 } else { |
| 3968 int num_datatypes = datatypes->GetSize(); |
| 3969 for (int i = 0; i < num_datatypes; ++i) { |
| 3970 std::string datatype_string; |
| 3971 datatypes->GetString(i, &datatype_string); |
| 3972 syncable::ModelType datatype = |
| 3973 syncable::ModelTypeFromString(datatype_string); |
| 3974 if (datatype == syncable::UNSPECIFIED) { |
| 3975 AutomationJSONReply(this, reply_message).SendError(StringPrintf( |
| 3976 "Invalid datatype string: %s.", datatype_string.c_str())); |
| 3977 return; |
| 3978 } |
| 3979 sync_waiter_->EnableSyncForDatatype(datatype); |
| 3980 sync_waiter_->AwaitSyncCycleCompletion(StringPrintf( |
| 3981 "Enabling datatype: %s", datatype_string.c_str())); |
| 3982 } |
| 3983 } |
| 3984 ProfileSyncService::Status status = sync_waiter_->GetStatus(); |
| 3985 if (status.summary == ProfileSyncService::Status::READY || |
| 3986 status.summary == ProfileSyncService::Status::SYNCING) { |
| 3987 DictionaryValue* return_value = new DictionaryValue; |
| 3988 return_value->SetBoolean("success", true); |
| 3989 reply.SendSuccess(return_value); |
| 3990 } else { |
| 3991 reply.SendError("Enabling sync for given datatypes was unsuccessful"); |
| 3992 } |
| 3993 } |
| 3994 |
| 3995 // Refer to DisableSyncForDatatypes() in chrome/test/pyautolib/pyauto.py for |
| 3996 // sample json input. Sample json output: { "success": true } |
| 3997 void TestingAutomationProvider::DisableSyncForDatatypes( |
| 3998 Browser* browser, |
| 3999 DictionaryValue* args, |
| 4000 IPC::Message* reply_message) { |
| 4001 AutomationJSONReply reply(this, reply_message); |
| 4002 if (sync_waiter_.get() == NULL) { |
| 4003 sync_waiter_.reset( |
| 4004 ProfileSyncServiceHarness::CreateAndAttach(browser->profile())); |
| 4005 } |
| 4006 if (!sync_waiter_->IsSyncAlreadySetup()) { |
| 4007 reply.SendError("Not signed in to sync"); |
| 4008 return; |
| 4009 } |
| 4010 ListValue* datatypes = NULL; |
| 4011 if (!args->GetList("datatypes", &datatypes)) { |
| 4012 reply.SendError("Invalid or missing args"); |
| 4013 return; |
| 4014 } |
| 4015 std::string first_datatype; |
| 4016 datatypes->GetString(0, &first_datatype); |
| 4017 if (first_datatype == "All") { |
| 4018 sync_waiter_->DisableSyncForAllDatatypes(); |
| 4019 ProfileSyncService::Status status = sync_waiter_->GetStatus(); |
| 4020 if (status.summary != ProfileSyncService::Status::READY && |
| 4021 status.summary != ProfileSyncService::Status::SYNCING) { |
| 4022 DictionaryValue* return_value = new DictionaryValue; |
| 4023 return_value->SetBoolean("success", true); |
| 4024 reply.SendSuccess(return_value); |
| 4025 } else { |
| 4026 reply.SendError("Disabling all sync datatypes was unsuccessful"); |
| 4027 } |
| 4028 } else { |
| 4029 int num_datatypes = datatypes->GetSize(); |
| 4030 for (int i = 0; i < num_datatypes; i++) { |
| 4031 std::string datatype_string; |
| 4032 datatypes->GetString(i, &datatype_string); |
| 4033 syncable::ModelType datatype = |
| 4034 syncable::ModelTypeFromString(datatype_string); |
| 4035 if (datatype == syncable::UNSPECIFIED) { |
| 4036 AutomationJSONReply(this, reply_message).SendError(StringPrintf( |
| 4037 "Invalid datatype string: %s.", datatype_string.c_str())); |
| 4038 return; |
| 4039 } |
| 4040 sync_waiter_->DisableSyncForDatatype(datatype); |
| 4041 sync_waiter_->AwaitSyncCycleCompletion(StringPrintf( |
| 4042 "Disabling datatype: %s", datatype_string.c_str())); |
| 4043 } |
| 4044 ProfileSyncService::Status status = sync_waiter_->GetStatus(); |
| 4045 if (status.summary == ProfileSyncService::Status::READY || |
| 4046 status.summary == ProfileSyncService::Status::SYNCING) { |
| 4047 DictionaryValue* return_value = new DictionaryValue; |
| 4048 return_value->SetBoolean("success", true); |
| 4049 reply.SendSuccess(return_value); |
| 4050 } else { |
| 4051 reply.SendError("Disabling sync for given datatypes was unsuccessful"); |
| 4052 } |
| 4053 } |
| 4054 } |
| 4055 |
3833 /* static */ | 4056 /* static */ |
3834 ListValue* TestingAutomationProvider::GetListFromAutoFillProfiles( | 4057 ListValue* TestingAutomationProvider::GetListFromAutoFillProfiles( |
3835 const std::vector<AutoFillProfile*>& autofill_profiles) { | 4058 const std::vector<AutoFillProfile*>& autofill_profiles) { |
3836 ListValue* profiles = new ListValue; | 4059 ListValue* profiles = new ListValue; |
3837 | 4060 |
3838 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string | 4061 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string |
3839 = GetAutoFillFieldToStringMap(); | 4062 = GetAutoFillFieldToStringMap(); |
3840 | 4063 |
3841 // For each AutoFillProfile, transform it to a dictionary object to return. | 4064 // For each AutoFillProfile, transform it to a dictionary object to return. |
3842 for (std::vector<AutoFillProfile*>::const_iterator it = | 4065 for (std::vector<AutoFillProfile*>::const_iterator it = |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4212 DCHECK(type == NotificationType::SESSION_END); | 4435 DCHECK(type == NotificationType::SESSION_END); |
4213 // OnBrowserRemoved does a ReleaseLater. When session end is received we exit | 4436 // OnBrowserRemoved does a ReleaseLater. When session end is received we exit |
4214 // before the task runs resulting in this object not being deleted. This | 4437 // before the task runs resulting in this object not being deleted. This |
4215 // Release balance out the Release scheduled by OnBrowserRemoved. | 4438 // Release balance out the Release scheduled by OnBrowserRemoved. |
4216 Release(); | 4439 Release(); |
4217 } | 4440 } |
4218 | 4441 |
4219 void TestingAutomationProvider::OnRemoveProvider() { | 4442 void TestingAutomationProvider::OnRemoveProvider() { |
4220 AutomationProviderList::GetInstance()->RemoveProvider(this); | 4443 AutomationProviderList::GetInstance()->RemoveProvider(this); |
4221 } | 4444 } |
OLD | NEW |