| OLD | NEW |
| 1 // Copyright 2012 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 "chrome/browser/automation/testing_automation_provider.h" | 5 #include "chrome/browser/automation/testing_automation_provider.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 #include "chrome/browser/prefs/pref_service.h" | 77 #include "chrome/browser/prefs/pref_service.h" |
| 78 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 78 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 79 #include "chrome/browser/profiles/profile.h" | 79 #include "chrome/browser/profiles/profile.h" |
| 80 #include "chrome/browser/profiles/profile_info_cache.h" | 80 #include "chrome/browser/profiles/profile_info_cache.h" |
| 81 #include "chrome/browser/profiles/profile_manager.h" | 81 #include "chrome/browser/profiles/profile_manager.h" |
| 82 #include "chrome/browser/search_engines/template_url.h" | 82 #include "chrome/browser/search_engines/template_url.h" |
| 83 #include "chrome/browser/search_engines/template_url_service.h" | 83 #include "chrome/browser/search_engines/template_url_service.h" |
| 84 #include "chrome/browser/search_engines/template_url_service_factory.h" | 84 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 85 #include "chrome/browser/sessions/session_service_factory.h" | 85 #include "chrome/browser/sessions/session_service_factory.h" |
| 86 #include "chrome/browser/sessions/session_tab_helper.h" | 86 #include "chrome/browser/sessions/session_tab_helper.h" |
| 87 #include "chrome/browser/sync/profile_sync_service.h" | |
| 88 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 89 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" | 87 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" |
| 90 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" | 88 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" |
| 91 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" | 89 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
| 92 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" | 90 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" |
| 93 #include "chrome/browser/ui/bookmarks/bookmark_bar.h" | 91 #include "chrome/browser/ui/bookmarks/bookmark_bar.h" |
| 94 #include "chrome/browser/ui/browser_commands.h" | 92 #include "chrome/browser/ui/browser_commands.h" |
| 95 #include "chrome/browser/ui/browser_finder.h" | 93 #include "chrome/browser/ui/browser_finder.h" |
| 96 #include "chrome/browser/ui/browser_instant_controller.h" | 94 #include "chrome/browser/ui/browser_instant_controller.h" |
| 97 #include "chrome/browser/ui/browser_list.h" | 95 #include "chrome/browser/ui/browser_list.h" |
| 98 #include "chrome/browser/ui/browser_tabstrip.h" | 96 #include "chrome/browser/ui/browser_tabstrip.h" |
| (...skipping 1804 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1903 | 1901 |
| 1904 browser_handler_map_["FindInPage"] = &TestingAutomationProvider::FindInPage; | 1902 browser_handler_map_["FindInPage"] = &TestingAutomationProvider::FindInPage; |
| 1905 | 1903 |
| 1906 browser_handler_map_["GetAllNotifications"] = | 1904 browser_handler_map_["GetAllNotifications"] = |
| 1907 &TestingAutomationProvider::GetAllNotifications; | 1905 &TestingAutomationProvider::GetAllNotifications; |
| 1908 browser_handler_map_["CloseNotification"] = | 1906 browser_handler_map_["CloseNotification"] = |
| 1909 &TestingAutomationProvider::CloseNotification; | 1907 &TestingAutomationProvider::CloseNotification; |
| 1910 browser_handler_map_["WaitForNotificationCount"] = | 1908 browser_handler_map_["WaitForNotificationCount"] = |
| 1911 &TestingAutomationProvider::WaitForNotificationCount; | 1909 &TestingAutomationProvider::WaitForNotificationCount; |
| 1912 | 1910 |
| 1913 browser_handler_map_["SignInToSync"] = | |
| 1914 &TestingAutomationProvider::SignInToSync; | |
| 1915 browser_handler_map_["GetSyncInfo"] = | |
| 1916 &TestingAutomationProvider::GetSyncInfo; | |
| 1917 browser_handler_map_["AwaitFullSyncCompletion"] = | |
| 1918 &TestingAutomationProvider::AwaitFullSyncCompletion; | |
| 1919 browser_handler_map_["AwaitSyncRestart"] = | |
| 1920 &TestingAutomationProvider::AwaitSyncRestart; | |
| 1921 browser_handler_map_["EnableSyncForDatatypes"] = | |
| 1922 &TestingAutomationProvider::EnableSyncForDatatypes; | |
| 1923 browser_handler_map_["DisableSyncForDatatypes"] = | |
| 1924 &TestingAutomationProvider::DisableSyncForDatatypes; | |
| 1925 | |
| 1926 browser_handler_map_["GetNTPInfo"] = | 1911 browser_handler_map_["GetNTPInfo"] = |
| 1927 &TestingAutomationProvider::GetNTPInfo; | 1912 &TestingAutomationProvider::GetNTPInfo; |
| 1928 browser_handler_map_["RemoveNTPMostVisitedThumbnail"] = | 1913 browser_handler_map_["RemoveNTPMostVisitedThumbnail"] = |
| 1929 &TestingAutomationProvider::RemoveNTPMostVisitedThumbnail; | 1914 &TestingAutomationProvider::RemoveNTPMostVisitedThumbnail; |
| 1930 browser_handler_map_["RestoreAllNTPMostVisitedThumbnails"] = | 1915 browser_handler_map_["RestoreAllNTPMostVisitedThumbnails"] = |
| 1931 &TestingAutomationProvider::RestoreAllNTPMostVisitedThumbnails; | 1916 &TestingAutomationProvider::RestoreAllNTPMostVisitedThumbnails; |
| 1932 | 1917 |
| 1933 browser_handler_map_["KillRendererProcess"] = | 1918 browser_handler_map_["KillRendererProcess"] = |
| 1934 &TestingAutomationProvider::KillRendererProcess; | 1919 &TestingAutomationProvider::KillRendererProcess; |
| 1935 | 1920 |
| (...skipping 2235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4171 if (!args->GetString("switch", &switch_name) || | 4156 if (!args->GetString("switch", &switch_name) || |
| 4172 !args->GetString("value", &switch_value)) { | 4157 !args->GetString("value", &switch_value)) { |
| 4173 reply.SendError("Missing or invalid command line switch"); | 4158 reply.SendError("Missing or invalid command line switch"); |
| 4174 return; | 4159 return; |
| 4175 } | 4160 } |
| 4176 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_name, | 4161 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_name, |
| 4177 switch_value); | 4162 switch_value); |
| 4178 reply.SendSuccess(NULL); | 4163 reply.SendSuccess(NULL); |
| 4179 } | 4164 } |
| 4180 | 4165 |
| 4181 // Sample json output: { "success": true } | |
| 4182 void TestingAutomationProvider::SignInToSync(Browser* browser, | |
| 4183 DictionaryValue* args, | |
| 4184 IPC::Message* reply_message) { | |
| 4185 AutomationJSONReply reply(this, reply_message); | |
| 4186 std::string username; | |
| 4187 std::string password; | |
| 4188 if (!args->GetString("username", &username) || | |
| 4189 !args->GetString("password", &password)) { | |
| 4190 reply.SendError("Invalid or missing args"); | |
| 4191 return; | |
| 4192 } | |
| 4193 if (sync_waiter_.get() == NULL) { | |
| 4194 sync_waiter_.reset(new ProfileSyncServiceHarness( | |
| 4195 browser->profile(), username, password)); | |
| 4196 } else { | |
| 4197 sync_waiter_->SetCredentials(username, password); | |
| 4198 } | |
| 4199 if (sync_waiter_->SetupSync()) { | |
| 4200 scoped_ptr<DictionaryValue> return_value(new DictionaryValue); | |
| 4201 return_value->SetBoolean("success", true); | |
| 4202 reply.SendSuccess(return_value.get()); | |
| 4203 } else { | |
| 4204 reply.SendError("Signing in to sync was unsuccessful"); | |
| 4205 } | |
| 4206 } | |
| 4207 | |
| 4208 // Sample json output: | |
| 4209 // {u'summary': u'SYNC DISABLED'} | |
| 4210 // | |
| 4211 // { u'last synced': u'Just now', | |
| 4212 // u'sync url': u'clients4.google.com', | |
| 4213 // u'updates received': 42, | |
| 4214 // u'synced datatypes': [ u'Bookmarks', | |
| 4215 // u'Preferences', | |
| 4216 // u'Passwords', | |
| 4217 // u'Autofill', | |
| 4218 // u'Themes', | |
| 4219 // u'Extensions', | |
| 4220 // u'Apps']} | |
| 4221 void TestingAutomationProvider::GetSyncInfo(Browser* browser, | |
| 4222 DictionaryValue* args, | |
| 4223 IPC::Message* reply_message) { | |
| 4224 AutomationJSONReply reply(this, reply_message); | |
| 4225 DictionaryValue* sync_info = new DictionaryValue; | |
| 4226 scoped_ptr<DictionaryValue> return_value(new DictionaryValue); | |
| 4227 if (sync_waiter_.get() == NULL) { | |
| 4228 sync_waiter_.reset( | |
| 4229 ProfileSyncServiceHarness::CreateAndAttach(browser->profile())); | |
| 4230 } | |
| 4231 if (!sync_waiter_->IsSyncAlreadySetup()) { | |
| 4232 sync_info->SetString("summary", "SYNC DISABLED"); | |
| 4233 } else { | |
| 4234 ProfileSyncService* service = sync_waiter_->service(); | |
| 4235 ProfileSyncService::Status status = sync_waiter_->GetStatus(); | |
| 4236 sync_info->SetString("sync url", service->sync_service_url().host()); | |
| 4237 sync_info->SetString("last synced", service->GetLastSyncedTimeString()); | |
| 4238 sync_info->SetInteger("updates received", status.updates_received); | |
| 4239 ListValue* synced_datatype_list = new ListValue; | |
| 4240 const syncer::ModelTypeSet synced_datatypes = | |
| 4241 service->GetPreferredDataTypes(); | |
| 4242 for (syncer::ModelTypeSet::Iterator it = synced_datatypes.First(); | |
| 4243 it.Good(); it.Inc()) { | |
| 4244 synced_datatype_list->Append( | |
| 4245 new StringValue(syncer::ModelTypeToString(it.Get()))); | |
| 4246 } | |
| 4247 sync_info->Set("synced datatypes", synced_datatype_list); | |
| 4248 } | |
| 4249 return_value->Set("sync_info", sync_info); | |
| 4250 reply.SendSuccess(return_value.get()); | |
| 4251 } | |
| 4252 | |
| 4253 // Sample json output: { "success": true } | |
| 4254 void TestingAutomationProvider::AwaitFullSyncCompletion( | |
| 4255 Browser* browser, | |
| 4256 DictionaryValue* args, | |
| 4257 IPC::Message* reply_message) { | |
| 4258 AutomationJSONReply reply(this, reply_message); | |
| 4259 if (sync_waiter_.get() == NULL) { | |
| 4260 sync_waiter_.reset( | |
| 4261 ProfileSyncServiceHarness::CreateAndAttach(browser->profile())); | |
| 4262 } | |
| 4263 if (!sync_waiter_->IsSyncAlreadySetup()) { | |
| 4264 reply.SendError("Not signed in to sync"); | |
| 4265 return; | |
| 4266 } | |
| 4267 // Ensure that the profile sync service and sync backend host are initialized | |
| 4268 // before waiting for sync cycle completion. In cases where the browser is | |
| 4269 // restarted with sync enabled, these operations may still be in flight. | |
| 4270 if (ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
| 4271 browser->profile()) == NULL) { | |
| 4272 reply.SendError("ProfileSyncService initialization failed."); | |
| 4273 return; | |
| 4274 } | |
| 4275 if (!sync_waiter_->service()->sync_initialized() && | |
| 4276 !sync_waiter_->AwaitBackendInitialized()) { | |
| 4277 reply.SendError("Sync backend host initialization failed."); | |
| 4278 return; | |
| 4279 } | |
| 4280 if (!sync_waiter_->AwaitFullSyncCompletion("Waiting for sync cycle")) { | |
| 4281 reply.SendError("Sync cycle did not complete."); | |
| 4282 return; | |
| 4283 } | |
| 4284 scoped_ptr<DictionaryValue> return_value(new DictionaryValue); | |
| 4285 return_value->SetBoolean("success", true); | |
| 4286 reply.SendSuccess(return_value.get()); | |
| 4287 } | |
| 4288 | |
| 4289 // Sample json output: { "success": true } | |
| 4290 void TestingAutomationProvider::AwaitSyncRestart( | |
| 4291 Browser* browser, | |
| 4292 DictionaryValue* args, | |
| 4293 IPC::Message* reply_message) { | |
| 4294 AutomationJSONReply reply(this, reply_message); | |
| 4295 if (sync_waiter_.get() == NULL) { | |
| 4296 sync_waiter_.reset( | |
| 4297 ProfileSyncServiceHarness::CreateAndAttach(browser->profile())); | |
| 4298 } | |
| 4299 if (!sync_waiter_->IsSyncAlreadySetup()) { | |
| 4300 reply.SendError("Not signed in to sync"); | |
| 4301 return; | |
| 4302 } | |
| 4303 if (ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
| 4304 browser->profile()) == NULL) { | |
| 4305 reply.SendError("ProfileSyncService initialization failed."); | |
| 4306 return; | |
| 4307 } | |
| 4308 if (!sync_waiter_->AwaitSyncRestart()) { | |
| 4309 reply.SendError("Sync did not successfully restart."); | |
| 4310 return; | |
| 4311 } | |
| 4312 scoped_ptr<DictionaryValue> return_value(new DictionaryValue); | |
| 4313 return_value->SetBoolean("success", true); | |
| 4314 reply.SendSuccess(return_value.get()); | |
| 4315 } | |
| 4316 | |
| 4317 // Refer to EnableSyncForDatatypes() in chrome/test/pyautolib/pyauto.py for | |
| 4318 // sample json input. Sample json output: { "success": true } | |
| 4319 void TestingAutomationProvider::EnableSyncForDatatypes( | |
| 4320 Browser* browser, | |
| 4321 DictionaryValue* args, | |
| 4322 IPC::Message* reply_message) { | |
| 4323 AutomationJSONReply reply(this, reply_message); | |
| 4324 if (sync_waiter_.get() == NULL) { | |
| 4325 sync_waiter_.reset( | |
| 4326 ProfileSyncServiceHarness::CreateAndAttach(browser->profile())); | |
| 4327 } | |
| 4328 if (!sync_waiter_->IsSyncAlreadySetup()) { | |
| 4329 reply.SendError("Not signed in to sync"); | |
| 4330 return; | |
| 4331 } | |
| 4332 ListValue* datatypes = NULL; | |
| 4333 if (!args->GetList("datatypes", &datatypes)) { | |
| 4334 reply.SendError("Invalid or missing args"); | |
| 4335 return; | |
| 4336 } | |
| 4337 std::string first_datatype; | |
| 4338 datatypes->GetString(0, &first_datatype); | |
| 4339 if (first_datatype == "All") { | |
| 4340 sync_waiter_->EnableSyncForAllDatatypes(); | |
| 4341 } else { | |
| 4342 int num_datatypes = datatypes->GetSize(); | |
| 4343 for (int i = 0; i < num_datatypes; ++i) { | |
| 4344 std::string datatype_string; | |
| 4345 datatypes->GetString(i, &datatype_string); | |
| 4346 syncer::ModelType datatype = | |
| 4347 syncer::ModelTypeFromString(datatype_string); | |
| 4348 if (datatype == syncer::UNSPECIFIED) { | |
| 4349 AutomationJSONReply(this, reply_message).SendError(StringPrintf( | |
| 4350 "Invalid datatype string: %s.", datatype_string.c_str())); | |
| 4351 return; | |
| 4352 } | |
| 4353 sync_waiter_->EnableSyncForDatatype(datatype); | |
| 4354 sync_waiter_->AwaitFullSyncCompletion(StringPrintf( | |
| 4355 "Enabling datatype: %s", datatype_string.c_str())); | |
| 4356 } | |
| 4357 } | |
| 4358 scoped_ptr<DictionaryValue> return_value(new DictionaryValue); | |
| 4359 return_value->SetBoolean("success", true); | |
| 4360 reply.SendSuccess(return_value.get()); | |
| 4361 } | |
| 4362 | |
| 4363 // Refer to DisableSyncForDatatypes() in chrome/test/pyautolib/pyauto.py for | |
| 4364 // sample json input. Sample json output: { "success": true } | |
| 4365 void TestingAutomationProvider::DisableSyncForDatatypes( | |
| 4366 Browser* browser, | |
| 4367 DictionaryValue* args, | |
| 4368 IPC::Message* reply_message) { | |
| 4369 AutomationJSONReply reply(this, reply_message); | |
| 4370 if (sync_waiter_.get() == NULL) { | |
| 4371 sync_waiter_.reset( | |
| 4372 ProfileSyncServiceHarness::CreateAndAttach(browser->profile())); | |
| 4373 } | |
| 4374 if (!sync_waiter_->IsSyncAlreadySetup()) { | |
| 4375 reply.SendError("Not signed in to sync"); | |
| 4376 return; | |
| 4377 } | |
| 4378 ListValue* datatypes = NULL; | |
| 4379 if (!args->GetList("datatypes", &datatypes)) { | |
| 4380 reply.SendError("Invalid or missing args"); | |
| 4381 return; | |
| 4382 } | |
| 4383 std::string first_datatype; | |
| 4384 if (!datatypes->GetString(0, &first_datatype)) { | |
| 4385 reply.SendError("Invalid or missing string"); | |
| 4386 return; | |
| 4387 } | |
| 4388 if (first_datatype == "All") { | |
| 4389 sync_waiter_->DisableSyncForAllDatatypes(); | |
| 4390 } else { | |
| 4391 int num_datatypes = datatypes->GetSize(); | |
| 4392 for (int i = 0; i < num_datatypes; i++) { | |
| 4393 std::string datatype_string; | |
| 4394 datatypes->GetString(i, &datatype_string); | |
| 4395 syncer::ModelType datatype = | |
| 4396 syncer::ModelTypeFromString(datatype_string); | |
| 4397 if (datatype == syncer::UNSPECIFIED) { | |
| 4398 AutomationJSONReply(this, reply_message).SendError(StringPrintf( | |
| 4399 "Invalid datatype string: %s.", datatype_string.c_str())); | |
| 4400 return; | |
| 4401 } | |
| 4402 sync_waiter_->DisableSyncForDatatype(datatype); | |
| 4403 sync_waiter_->AwaitFullSyncCompletion(StringPrintf( | |
| 4404 "Disabling datatype: %s", datatype_string.c_str())); | |
| 4405 } | |
| 4406 scoped_ptr<DictionaryValue> return_value(new DictionaryValue); | |
| 4407 return_value->SetBoolean("success", true); | |
| 4408 reply.SendSuccess(return_value.get()); | |
| 4409 } | |
| 4410 } | |
| 4411 | |
| 4412 // Refer to GetAllNotifications() in chrome/test/pyautolib/pyauto.py for | 4166 // Refer to GetAllNotifications() in chrome/test/pyautolib/pyauto.py for |
| 4413 // sample json input/output. | 4167 // sample json input/output. |
| 4414 void TestingAutomationProvider::GetAllNotifications( | 4168 void TestingAutomationProvider::GetAllNotifications( |
| 4415 Browser* browser, | 4169 Browser* browser, |
| 4416 DictionaryValue* args, | 4170 DictionaryValue* args, |
| 4417 IPC::Message* reply_message) { | 4171 IPC::Message* reply_message) { |
| 4418 new GetAllNotificationsObserver(this, reply_message); | 4172 new GetAllNotificationsObserver(this, reply_message); |
| 4419 } | 4173 } |
| 4420 | 4174 |
| 4421 // Refer to CloseNotification() in chrome/test/pyautolib/pyauto.py for | 4175 // Refer to CloseNotification() in chrome/test/pyautolib/pyauto.py for |
| (...skipping 1785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6207 if (g_browser_process) | 5961 if (g_browser_process) |
| 6208 g_browser_process->GetAutomationProviderList()->RemoveProvider(this); | 5962 g_browser_process->GetAutomationProviderList()->RemoveProvider(this); |
| 6209 } | 5963 } |
| 6210 | 5964 |
| 6211 void TestingAutomationProvider::EnsureTabSelected(Browser* browser, | 5965 void TestingAutomationProvider::EnsureTabSelected(Browser* browser, |
| 6212 WebContents* tab) { | 5966 WebContents* tab) { |
| 6213 TabStripModel* tab_strip = browser->tab_strip_model(); | 5967 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 6214 if (tab_strip->GetActiveWebContents() != tab) | 5968 if (tab_strip->GetActiveWebContents() != tab) |
| 6215 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(tab), true); | 5969 tab_strip->ActivateTabAt(tab_strip->GetIndexOfWebContents(tab), true); |
| 6216 } | 5970 } |
| OLD | NEW |