blob: bba2ace58b1dfef469556416bfccc409bb505e7f [file] [log] [blame]
[email protected]3b5f7022010-03-25 20:37:401// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/automation/automation_provider.h"
6
[email protected]5ae5bed2009-08-21 18:52:447#include <set>
8
[email protected]37126212009-05-06 02:23:319#include "app/message_box_flags.h"
[email protected]2041cf342010-02-19 03:15:5910#include "base/callback.h"
[email protected]7060bb292010-06-24 00:52:4911#include "base/file_path.h"
[email protected]c6cb1992009-04-13 16:45:2912#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2013#include "base/json/json_reader.h"
[email protected]59a611242010-04-02 02:24:0414#include "base/json/json_writer.h"
[email protected]93364da2010-06-29 18:03:4415#include "base/json/string_escape.h"
[email protected]bc1407f2009-09-29 00:33:3516#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3817#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4619#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5020#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2721#include "base/string_util.h"
[email protected]9eaa18e2010-06-29 20:51:0122#include "base/task.h"
[email protected]5fac9622009-02-04 21:49:3823#include "base/thread.h"
[email protected]a872ea1f2010-08-11 04:45:3324#include "base/trace_event.h"
[email protected]528c56d2010-07-30 19:28:4425#include "base/string_number_conversions.h"
[email protected]6d8ffc9f2010-03-12 18:27:5326#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1727#include "base/values.h"
[email protected]9eaa18e2010-06-29 20:51:0128#include "base/waitable_event.h"
[email protected]4f3dc372009-02-24 00:10:2929#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2830#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0931#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]55846ad842010-07-09 18:22:5632#include "chrome/browser/autofill/autofill_manager.h"
[email protected]679082052010-07-21 21:30:1333#include "chrome/browser/automation/automation_autocomplete_edit_tracker.h"
34#include "chrome/browser/automation/automation_browser_tracker.h"
[email protected]790788ac2010-04-06 17:52:1935#include "chrome/browser/automation/automation_extension_tracker.h"
[email protected]7c983cc2010-07-16 11:33:3436#include "chrome/browser/automation/automation_provider_json.h"
initial.commit09911bf2008-07-26 23:55:2937#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0838#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]679082052010-07-21 21:30:1339#include "chrome/browser/automation/automation_resource_message_filter.h"
40#include "chrome/browser/automation/automation_tab_tracker.h"
41#include "chrome/browser/automation/automation_window_tracker.h"
[email protected]f44265b2009-05-19 18:52:5042#include "chrome/browser/automation/extension_port_container.h"
[email protected]12802702010-07-09 19:43:0943#include "chrome/browser/autocomplete/autocomplete_edit.h"
[email protected]66ba4932009-06-04 19:22:1344#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5345#include "chrome/browser/bookmarks/bookmark_model.h"
46#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]ef413ca2010-05-25 21:09:1447#include "chrome/browser/browser_list.h"
[email protected]5c238752009-06-13 10:29:0748#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3949#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2050#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0551#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5852#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2953#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1254#include "chrome/browser/debugger/devtools_manager.h"
[email protected]6c69796d2010-07-16 21:41:1655#include "chrome/browser/download/download_item.h"
[email protected]59560e0b2009-06-04 03:30:2256#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0557#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4058#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1959#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]ef413ca2010-05-25 21:09:1460#include "chrome/browser/extensions/extension_host.h"
[email protected]d11c8e92009-10-20 23:26:4061#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5562#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1963#include "chrome/browser/extensions/extension_tabs_module.h"
64#include "chrome/browser/extensions/extension_toolbar_model.h"
65#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1666#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5867#include "chrome/browser/find_bar.h"
68#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2969#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0170#include "chrome/browser/host_content_settings_map.h"
[email protected]c5aa5322010-07-15 19:00:0771#include "chrome/browser/importer/importer.h"
72#include "chrome/browser/importer/importer_data_types.h"
[email protected]0ac83682010-01-22 17:46:2773#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0674#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0175#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5376#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2477#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0878#include "chrome/browser/pref_service.h"
[email protected]f83f9102010-05-04 17:01:0579#include "chrome/browser/printing/print_job.h"
[email protected]a7eee32f2009-05-22 18:08:1780#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0781#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2582#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0383#include "chrome/browser/ssl/ssl_manager.h"
84#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]b5558cf22010-07-12 17:30:0685#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3486#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4587#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]38b5a3852010-07-21 06:49:5288#include "chrome/browser/translate/translate_infobar_delegate.h"
[email protected]a7eee32f2009-05-22 18:08:1789#include "chrome/common/automation_constants.h"
[email protected]a9ff2c02010-05-13 17:33:0590#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2991#include "chrome/common/chrome_paths.h"
[email protected]c984d9f2010-07-20 20:52:2092#include "chrome/common/chrome_switches.h"
[email protected]1eeb5e02010-07-20 23:02:1193#include "chrome/common/chrome_version_info.h"
[email protected]790788ac2010-04-06 17:52:1994#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1795#include "chrome/common/json_value_serializer.h"
[email protected]68d2a05f2010-05-07 21:39:5596#include "chrome/common/net/url_request_context_getter.h"
[email protected]1c58a5c2009-05-21 18:47:1497#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5798#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2599#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:56100#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:57101#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:17102#include "net/proxy/proxy_service.h"
103#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:21104#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:30105#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:28106#include "views/event.h"
[email protected]5bcfe1672010-07-16 20:51:57107#include "webkit/glue/password_form.h"
[email protected]f7d48012010-05-06 08:17:05108#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:29109
[email protected]de246f52009-02-25 18:25:45110#if defined(OS_WIN)
[email protected]4bdde602010-06-16 03:17:35111#include "chrome/browser/external_tab_container_win.h"
[email protected]de246f52009-02-25 18:25:45112#endif // defined(OS_WIN)
113
[email protected]e1acf6f2008-10-27 20:43:33114using base::Time;
115
[email protected]cbab76d2008-10-13 22:42:47116class AutomationInterstitialPage : public InterstitialPage {
117 public:
[email protected]57c6a652009-05-04 07:58:34118 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47119 const GURL& url,
120 const std::string& contents)
121 : InterstitialPage(tab, true, url),
122 contents_(contents) {
123 }
124
125 virtual std::string GetHTMLContents() { return contents_; }
126
127 private:
128 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29129
[email protected]cbab76d2008-10-13 22:42:47130 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
131};
132
[email protected]c2cb8542009-08-20 21:16:51133class ClickTask : public Task {
134 public:
[email protected]fc2e0872009-08-21 22:14:41135 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51136 virtual ~ClickTask() {}
137
138 virtual void Run() {
139 ui_controls::MouseButton button = ui_controls::LEFT;
140 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
141 views::Event::EF_LEFT_BUTTON_DOWN) {
142 button = ui_controls::LEFT;
143 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
144 views::Event::EF_RIGHT_BUTTON_DOWN) {
145 button = ui_controls::RIGHT;
146 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
147 views::Event::EF_MIDDLE_BUTTON_DOWN) {
148 button = ui_controls::MIDDLE;
149 } else {
150 NOTREACHED();
151 }
152
[email protected]fc2e0872009-08-21 22:14:41153 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51154 }
155
156 private:
[email protected]c2cb8542009-08-20 21:16:51157 int flags_;
158
159 DISALLOW_COPY_AND_ASSIGN(ClickTask);
160};
[email protected]c2cb8542009-08-20 21:16:51161
initial.commit09911bf2008-07-26 23:55:29162AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57163 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56164 profile_(profile),
[email protected]cc824372010-03-31 15:33:01165 reply_message_(NULL),
166 popup_menu_waiter_(NULL) {
[email protected]a872ea1f2010-08-11 04:45:33167 TRACE_EVENT_BEGIN("AutomationProvider::AutomationProvider", 0, "");
168
initial.commit09911bf2008-07-26 23:55:29169 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19170 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29171 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20172 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29173 autocomplete_edit_tracker_.reset(
174 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29175 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
176 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44177 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19178 extension_test_result_observer_.reset(
179 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13180 g_browser_process->AddRefModule();
[email protected]a872ea1f2010-08-11 04:45:33181
182 TRACE_EVENT_END("AutomationProvider::AutomationProvider", 0, "");
initial.commit09911bf2008-07-26 23:55:29183}
184
185AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50186 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
187 port_containers_.end());
188 port_containers_.clear();
189
[email protected]0da050b92008-08-19 19:29:47190 // Make sure that any outstanding NotificationObservers also get destroyed.
191 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31192 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47193 while ((observer = it.GetNext()) != NULL)
194 delete observer;
[email protected]528211a2010-01-14 15:25:13195
196 if (channel_.get()) {
197 channel_->Close();
198 }
199 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29200}
201
[email protected]9a3a293b2009-06-04 22:28:16202void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]a872ea1f2010-08-11 04:45:33203 TRACE_EVENT_BEGIN("AutomationProvider::ConnectToChannel", 0, "");
204
[email protected]2e4633c2009-07-09 16:58:06205 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57206 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06207 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
208 automation_resource_message_filter_,
209 g_browser_process->io_thread()->message_loop(),
210 true, g_browser_process->shutdown_event()));
[email protected]1eeb5e02010-07-20 23:02:11211 scoped_ptr<FileVersionInfo> version_info(chrome::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40212 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43213 if (version_info != NULL) {
214 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40215 }
[email protected]c6cb1992009-04-13 16:45:29216
217 // Send a hello message with our current automation protocol version.
218 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
[email protected]a872ea1f2010-08-11 04:45:33219
220 TRACE_EVENT_END("AutomationProvider::ConnectToChannel", 0, "");
initial.commit09911bf2008-07-26 23:55:29221}
222
223void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
224 if (expected_tabs == 0) {
225 Send(new AutomationMsg_InitialLoadsComplete(0));
226 } else {
227 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
228 }
229}
230
231NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58232 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01233 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29234 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58235 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01236 number_of_navigations,
237 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29238
[email protected]71f65dd2009-02-11 19:14:56239 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29240 return observer;
241}
242
[email protected]faf2ee42010-05-11 14:26:17243void AutomationProvider::RemoveNavigationStatusListener(
244 NotificationObserver* obs) {
245 notification_observer_list_.RemoveObserver(obs);
246}
247
initial.commit09911bf2008-07-26 23:55:29248NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14249 Browser* parent,
250 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56251 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14252 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29253 notification_observer_list_.AddObserver(observer);
254
255 return observer;
256}
257
[email protected]faf2ee42010-05-11 14:26:17258void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
initial.commit09911bf2008-07-26 23:55:29259 notification_observer_list_.RemoveObserver(obs);
260}
261
262void AutomationProvider::AddLoginHandler(NavigationController* tab,
263 LoginHandler* handler) {
264 login_handler_map_[tab] = handler;
265}
266
267void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
268 DCHECK(login_handler_map_[tab]);
269 login_handler_map_.erase(tab);
270}
271
[email protected]f44265b2009-05-19 18:52:50272void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
273 int port_id = port->port_id();
274 DCHECK_NE(-1, port_id);
275 DCHECK(port_containers_.find(port_id) == port_containers_.end());
276
277 port_containers_[port_id] = port;
278}
279
280void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
281 int port_id = port->port_id();
282 DCHECK_NE(-1, port_id);
283
284 PortContainerMap::iterator it = port_containers_.find(port_id);
285 DCHECK(it != port_containers_.end());
286
287 if (it != port_containers_.end()) {
288 delete it->second;
289 port_containers_.erase(it);
290 }
291}
292
293ExtensionPortContainer* AutomationProvider::GetPortContainer(
294 int port_id) const {
295 PortContainerMap::const_iterator it = port_containers_.find(port_id);
296 if (it == port_containers_.end())
297 return NULL;
298
299 return it->second;
300}
301
initial.commit09911bf2008-07-26 23:55:29302int AutomationProvider::GetIndexForNavigationController(
303 const NavigationController* controller, const Browser* parent) const {
304 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12305 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29306}
307
[email protected]790788ac2010-04-06 17:52:19308int AutomationProvider::AddExtension(Extension* extension) {
309 DCHECK(extension);
310 return extension_tracker_->Add(extension);
311}
312
313Extension* AutomationProvider::GetExtension(int extension_handle) {
314 return extension_tracker_->GetResource(extension_handle);
315}
316
317Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
318 Extension* extension = extension_tracker_->GetResource(extension_handle);
319 ExtensionsService* service = profile_->GetExtensionsService();
320 if (extension && service &&
321 service->GetExtensionById(extension->id(), false))
322 return extension;
323 return NULL;
324}
325
326Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
327 Extension* extension = extension_tracker_->GetResource(extension_handle);
328 ExtensionsService* service = profile_->GetExtensionsService();
329 if (extension && service &&
330 service->GetExtensionById(extension->id(), true) &&
331 !service->GetExtensionById(extension->id(), false))
332 return extension;
333 return NULL;
334}
335
initial.commit09911bf2008-07-26 23:55:29336void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
337 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14338 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56339 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
340 CloseBrowserAsync)
341 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
342 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
343 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
344 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
345 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
346 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22347 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]a503c97c2010-07-16 13:05:48348 IPC_MESSAGE_HANDLER(AutomationMsg_ShowCollectedCookiesDialog,
349 ShowCollectedCookiesDialog)
[email protected]1c58a5c2009-05-21 18:47:14350 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58351 IPC_MESSAGE_HANDLER_DELAY_REPLY(
352 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
353 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56354 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11355 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
356 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56357 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
358 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
359 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
360 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14361 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56362 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
363 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
364 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14365 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29366 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
367 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56368 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36369 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56370 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29371 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56372 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29373 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
374 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56375 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14376 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24377 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44378 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33379 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44380 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33381 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24382 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14383 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24384 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15385 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14386 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24387 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35388 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14389 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30390#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56391 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
392 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30393#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56394 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53395 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56396 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37397#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56398 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45399#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56400 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
401 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03402 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56403 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14404 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09405 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
406 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
407 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29408 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14409 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56410 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
411 ExecuteJavascript)
412 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29413 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14414 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
415 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56416 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
417 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14418 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17419 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14420 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14421 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53422 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
423 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14424 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56425 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14426 GetAutocompleteEditForBrowser)
427 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]71f65dd2009-02-11 19:14:56428 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14429 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56430 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14431 HideInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56432 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
433 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14434 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
435 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44436 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
437 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56438 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
439 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29440 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
[email protected]bdb7ff62010-07-20 01:56:52441 IPC_MESSAGE_HANDLER(AutomationMsg_IsMenuCommandEnabled,
442 IsMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56443 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41444 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56445 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
446 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29447 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56448 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29449 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56450 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29451 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56452 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29453 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56454 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45455 HandleOpenFindInPageRequest)
[email protected]1c58a5c2009-05-21 18:47:14456 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56457 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57458 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56459 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57460 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56461 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
462 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53463 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
464 GetBookmarksAsJSON)
465 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
466 WaitForBookmarkModelToLoad)
467 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
468 AddBookmarkGroup)
469 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
470 AddBookmarkURL)
471 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
472 ReparentBookmark)
473 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
474 SetBookmarkTitle)
475 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
476 SetBookmarkURL)
477 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
478 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04479 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
480 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18481 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
482 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
483 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56484 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42485 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56486 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
487 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14488 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56489 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44490 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56491 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20492 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14493 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56494 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16495 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56496 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16497 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56498 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16499 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14500 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20501 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
502 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14503 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]59560e0b2009-06-04 03:30:22504 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13505 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19506 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
507 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
508 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
509 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
510 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
511 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52512 IPC_MESSAGE_HANDLER_DELAY_REPLY(
513 AutomationMsg_WaitForBrowserWindowCountToBecome,
514 WaitForBrowserWindowCountToBecome)
515 IPC_MESSAGE_HANDLER_DELAY_REPLY(
516 AutomationMsg_WaitForAppModalDialogToBeShown,
517 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26518 IPC_MESSAGE_HANDLER_DELAY_REPLY(
519 AutomationMsg_GoBackBlockUntilNavigationsComplete,
520 GoBackBlockUntilNavigationsComplete)
521 IPC_MESSAGE_HANDLER_DELAY_REPLY(
522 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
523 GoForwardBlockUntilNavigationsComplete)
[email protected]1bb5f892009-10-06 01:44:57524 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40525 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
526 InstallExtension)
527 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
528 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43529 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
530 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19531 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
532 WaitForExtensionTestResult)
533 IPC_MESSAGE_HANDLER_DELAY_REPLY(
534 AutomationMsg_InstallExtensionAndGetHandle,
535 InstallExtensionAndGetHandle)
536 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
537 UninstallExtension)
538 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
539 EnableExtension)
540 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
541 DisableExtension)
542 IPC_MESSAGE_HANDLER_DELAY_REPLY(
543 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
544 ExecuteExtensionActionInActiveTabAsync)
545 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
546 MoveExtensionBrowserAction)
547 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
548 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57549 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
550 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00551 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]7dad3d5f2010-03-04 00:27:01552 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]bc73b4e52010-03-26 04:16:20553 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]bdd5a9c92010-06-14 18:21:00554 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
[email protected]cc824372010-03-31 15:33:01555#if defined(TOOLKIT_VIEWS)
556 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
557 WaitForFocusedViewIDToChange)
558 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
559 StartTrackingPopupMenus)
560 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
561 WaitForPopupMenuToOpen)
[email protected]bdd5a9c92010-06-14 18:21:00562#endif // defined(TOOLKIT_VIEWS)
[email protected]52415f842010-06-10 21:51:52563#if defined(OS_WIN)
564 // These are for use with external tabs.
565 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
566 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
567 ProcessUnhandledAccelerator)
568 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
569 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
570 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
571 OnForwardContextMenuCommandToChrome)
572 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
573 NavigateInExternalTab)
574 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
575 NavigateExternalTabAtIndex)
576 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
577 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
578 SetEnableExtensionAutomation)
579 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
580 OnMessageFromExternalHost)
[email protected]bdd5a9c92010-06-14 18:21:00581 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
[email protected]1f71d5882010-07-15 20:39:07582 IPC_MESSAGE_HANDLER(AutomationMsg_RunUnloadHandlers, OnRunUnloadHandlers)
[email protected]7f860dd82010-08-09 23:18:05583 IPC_MESSAGE_HANDLER(AutomationMsg_SetZoomLevel, OnSetZoomLevel)
[email protected]bdd5a9c92010-06-14 18:21:00584#endif // defined(OS_WIN)
585#if defined(OS_CHROMEOS)
586 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
587 LoginWithUserAndPass)
588#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29589 IPC_END_MESSAGE_MAP()
590}
591
[email protected]71f65dd2009-02-11 19:14:56592void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
593 *status = -1;
initial.commit09911bf2008-07-26 23:55:29594 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
595 Browser* browser = browser_tracker_->GetResource(handle);
596 if (at_index >= 0 && at_index < browser->tab_count()) {
597 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56598 *status = 0;
initial.commit09911bf2008-07-26 23:55:29599 }
600 }
initial.commit09911bf2008-07-26 23:55:29601}
602
[email protected]71f65dd2009-02-11 19:14:56603void AutomationProvider::AppendTab(int handle, const GURL& url,
604 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29605 int append_tab_response = -1; // -1 is the error code
606 NotificationObserver* observer = NULL;
607
608 if (browser_tracker_->ContainsHandle(handle)) {
609 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14610 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38611 TabContents* tab_contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39612 url, GURL(), PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED,
613 NULL, std::string());
initial.commit09911bf2008-07-26 23:55:29614 if (tab_contents) {
615 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57616 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29617 }
618 }
619
620 if (append_tab_response < 0) {
621 // The append tab failed. Remove the TabStripObserver
622 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17623 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29624 delete observer;
625 }
626
[email protected]71f65dd2009-02-11 19:14:56627 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
628 append_tab_response);
629 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29630 }
631}
632
[email protected]71f65dd2009-02-11 19:14:56633void AutomationProvider::NavigateToURL(int handle, const GURL& url,
634 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58635 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
636}
637
638void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
639 int handle, const GURL& url, int number_of_navigations,
640 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29641 if (tab_tracker_->ContainsHandle(handle)) {
642 NavigationController* tab = tab_tracker_->GetResource(handle);
643
644 // Simulate what a user would do. Activate the tab and then navigate.
645 // We could allow navigating in a background tab in future.
646 Browser* browser = FindAndActivateTab(tab);
647
648 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01649 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
650 false);
[email protected]71f65dd2009-02-11 19:14:56651
initial.commit09911bf2008-07-26 23:55:29652 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50653 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29654 return;
655 }
656 }
[email protected]71f65dd2009-02-11 19:14:56657
658 AutomationMsg_NavigateToURL::WriteReplyParams(
659 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
660 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29661}
[email protected]2949e90d2009-08-21 15:32:52662
[email protected]c70f9b82010-04-21 07:31:11663void AutomationProvider::NavigationAsync(int handle,
664 const GURL& url,
665 bool* status) {
666 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
667}
668
669void AutomationProvider::NavigationAsyncWithDisposition(
670 int handle,
671 const GURL& url,
672 WindowOpenDisposition disposition,
673 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56674 *status = false;
initial.commit09911bf2008-07-26 23:55:29675
676 if (tab_tracker_->ContainsHandle(handle)) {
677 NavigationController* tab = tab_tracker_->GetResource(handle);
678
679 // Simulate what a user would do. Activate the tab and then navigate.
680 // We could allow navigating in a background tab in future.
681 Browser* browser = FindAndActivateTab(tab);
682
683 if (browser) {
684 // Don't add any listener unless a callback mechanism is desired.
685 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11686 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56687 *status = true;
initial.commit09911bf2008-07-26 23:55:29688 }
689 }
initial.commit09911bf2008-07-26 23:55:29690}
691
[email protected]71f65dd2009-02-11 19:14:56692void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29693 if (tab_tracker_->ContainsHandle(handle)) {
694 NavigationController* tab = tab_tracker_->GetResource(handle);
695 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14696 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01697 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29698 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29699 return;
700 }
701 }
[email protected]71f65dd2009-02-11 19:14:56702
703 AutomationMsg_GoBack::WriteReplyParams(
704 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
705 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29706}
707
[email protected]71f65dd2009-02-11 19:14:56708void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29709 if (tab_tracker_->ContainsHandle(handle)) {
710 NavigationController* tab = tab_tracker_->GetResource(handle);
711 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14712 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01713 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29714 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29715 return;
716 }
717 }
[email protected]71f65dd2009-02-11 19:14:56718
719 AutomationMsg_GoForward::WriteReplyParams(
720 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
721 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29722}
723
[email protected]71f65dd2009-02-11 19:14:56724void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29725 if (tab_tracker_->ContainsHandle(handle)) {
726 NavigationController* tab = tab_tracker_->GetResource(handle);
727 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14728 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01729 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]cb84d642010-06-10 00:56:28730 browser->Reload(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29731 return;
732 }
733 }
[email protected]71f65dd2009-02-11 19:14:56734
735 AutomationMsg_Reload::WriteReplyParams(
736 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
737 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29738}
739
[email protected]71f65dd2009-02-11 19:14:56740void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29741 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56742 const std::wstring& password,
743 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29744 if (tab_tracker_->ContainsHandle(tab_handle)) {
745 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
746 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
747
748 if (iter != login_handler_map_.end()) {
749 // If auth is needed again after this, assume login has failed. This is
750 // not strictly correct, because a navigation can require both proxy and
751 // server auth, but it should be OK for now.
752 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01753 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29754 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52755 return;
initial.commit09911bf2008-07-26 23:55:29756 }
757 }
[email protected]de246f52009-02-25 18:25:45758
[email protected]457f5cf2009-08-18 16:37:52759 AutomationMsg_SetAuth::WriteReplyParams(
760 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
761 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29762}
763
[email protected]71f65dd2009-02-11 19:14:56764void AutomationProvider::CancelAuth(int tab_handle,
765 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29766 if (tab_tracker_->ContainsHandle(tab_handle)) {
767 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
768 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
769
770 if (iter != login_handler_map_.end()) {
771 // If auth is needed again after this, something is screwy.
772 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01773 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29774 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52775 return;
initial.commit09911bf2008-07-26 23:55:29776 }
777 }
[email protected]de246f52009-02-25 18:25:45778
[email protected]457f5cf2009-08-18 16:37:52779 AutomationMsg_CancelAuth::WriteReplyParams(
780 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
781 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29782}
783
[email protected]71f65dd2009-02-11 19:14:56784void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
785 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29786
787 if (tab_tracker_->ContainsHandle(tab_handle)) {
788 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
789 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
790
791 if (iter != login_handler_map_.end()) {
792 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56793 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29794 }
795 }
initial.commit09911bf2008-07-26 23:55:29796}
797
[email protected]71f65dd2009-02-11 19:14:56798void AutomationProvider::GetRedirectsFrom(int tab_handle,
799 const GURL& source_url,
800 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29801 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
802 if (tab_tracker_->ContainsHandle(tab_handle)) {
803 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
804 HistoryService* history_service =
805 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
806
807 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
808 "has no history service";
809 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56810 DCHECK(reply_message_ == NULL);
811 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29812 // Schedule a history query for redirects. The response will be sent
813 // asynchronously from the callback the history system uses to notify us
814 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29815 redirect_query_ = history_service->QueryRedirectsFrom(
816 source_url, &consumer_,
817 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
818 return; // Response will be sent when query completes.
819 }
820 }
821
822 // Send failure response.
[email protected]deb57402009-02-06 01:35:30823 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56824 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
825 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29826}
827
[email protected]71f65dd2009-02-11 19:14:56828void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
829 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29830 if (browser_tracker_->ContainsHandle(handle)) {
831 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56832 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29833 }
initial.commit09911bf2008-07-26 23:55:29834}
835
[email protected]202e7a72009-06-15 03:48:36836void AutomationProvider::GetBrowserLocale(string16* locale) {
837 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06838 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36839}
840
[email protected]71f65dd2009-02-11 19:14:56841void AutomationProvider::GetBrowserWindowCount(int* window_count) {
842 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29843}
844
[email protected]24497032009-05-01 17:00:29845void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
846 *window_count = static_cast<int>(
847 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
848}
849
[email protected]71f65dd2009-02-11 19:14:56850void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
851 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07852 AppModalDialog* dialog_delegate =
853 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50854 *showing_dialog = (dialog_delegate != NULL);
855 if (*showing_dialog)
856 *dialog_button = dialog_delegate->GetDialogButtons();
857 else
[email protected]478ff2ed2009-04-21 23:49:18858 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20859}
860
[email protected]71f65dd2009-02-11 19:14:56861void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
862 *success = false;
[email protected]fad84eab2008-12-05 00:37:20863
[email protected]1f460072009-05-28 17:02:07864 AppModalDialog* dialog_delegate =
865 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50866 if (dialog_delegate &&
867 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18868 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
869 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28870 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56871 *success = true;
[email protected]fad84eab2008-12-05 00:37:20872 }
[email protected]478ff2ed2009-04-21 23:49:18873 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
874 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56875 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28876 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56877 *success = true;
[email protected]fad84eab2008-12-05 00:37:20878 }
879 }
[email protected]c274acc2008-11-11 20:13:44880}
881
[email protected]fedaa7d2010-01-26 20:34:57882void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
883 if (browser_tracker_->ContainsHandle(handle)) {
884 Browser* browser = browser_tracker_->GetResource(handle);
885 browser->profile()->ShutdownSessionService();
886 *result = true;
887 } else {
888 *result = false;
889 }
890}
891
[email protected]71f65dd2009-02-11 19:14:56892void AutomationProvider::GetBrowserWindow(int index, int* handle) {
893 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29894 if (index >= 0) {
895 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]f07467d2010-06-16 14:28:30896 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {}
initial.commit09911bf2008-07-26 23:55:29897 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56898 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29899 }
900 }
initial.commit09911bf2008-07-26 23:55:29901}
902
[email protected]24497032009-05-01 17:00:29903void AutomationProvider::FindNormalBrowserWindow(int* handle) {
904 *handle = 0;
905 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31906 Browser::TYPE_NORMAL,
907 false);
[email protected]24497032009-05-01 17:00:29908 if (browser)
909 *handle = browser_tracker_->Add(browser);
910}
911
[email protected]71f65dd2009-02-11 19:14:56912void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
913 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29914 Browser* browser = BrowserList::GetLastActive();
915 if (browser)
[email protected]71f65dd2009-02-11 19:14:56916 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29917}
918
[email protected]b2aa3ed72010-02-01 18:37:14919#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28920// TODO(estade): use this implementation for all platforms?
921void AutomationProvider::GetActiveWindow(int* handle) {
922 gfx::NativeWindow window =
923 BrowserList::GetLastActive()->window()->GetNativeHandle();
924 *handle = window_tracker_->Add(window);
925}
926#endif
927
[email protected]4f6381ee2009-04-16 02:46:33928void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
929 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56930 *success = false;
[email protected]4ae62752008-08-04 23:28:47931 if (browser_tracker_->ContainsHandle(handle)) {
932 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14933 if (browser->command_updater()->SupportsCommand(command) &&
934 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47935 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56936 *success = true;
[email protected]4ae62752008-08-04 23:28:47937 }
938 }
[email protected]4ae62752008-08-04 23:28:47939}
940
[email protected]4f6381ee2009-04-16 02:46:33941void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56942 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53943 // List of commands which just finish synchronously and don't require
944 // setting up an observer.
945 static const int kSynchronousCommands[] = {
946 IDC_HOME,
947 IDC_SELECT_NEXT_TAB,
948 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25949 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53950 };
[email protected]56e71b7c2009-03-27 03:05:56951 if (browser_tracker_->ContainsHandle(handle)) {
952 Browser* browser = browser_tracker_->GetResource(handle);
953 if (browser->command_updater()->SupportsCommand(command) &&
954 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53955 // First check if we can handle the command without using an observer.
956 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
957 if (command == kSynchronousCommands[i]) {
958 browser->ExecuteCommand(command);
959 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
960 true);
961 Send(reply_message);
962 return;
963 }
964 }
965
966 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42967 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
968 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27969 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42970 return;
971 }
[email protected]56e71b7c2009-03-27 03:05:56972 }
973 }
[email protected]49a14a82009-03-31 04:16:44974 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56975 Send(reply_message);
976}
977
[email protected]fc2e0872009-08-21 22:14:41978// This task just adds another task to the event queue. This is useful if
979// you want to ensure that any tasks added to the event queue after this one
980// have already been processed by the time |task| is run.
981class InvokeTaskLaterTask : public Task {
982 public:
983 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
984 virtual ~InvokeTaskLaterTask() {}
985
986 virtual void Run() {
987 MessageLoop::current()->PostTask(FROM_HERE, task_);
988 }
989
990 private:
991 Task* task_;
992
993 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
994};
995
initial.commit09911bf2008-07-26 23:55:29996void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
997 int handle,
[email protected]d1a5941e2009-08-13 23:34:24998 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29999 int flags) {
[email protected]b410bc32009-08-14 01:11:141000 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:511001 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:411002 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:291003 }
1004}
1005
[email protected]60507b12009-11-02 23:51:351006void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
1007 int handle,
1008 const gfx::Point& location) {
1009 if (window_tracker_->ContainsHandle(handle))
1010 ui_controls::SendMouseMove(location.x(), location.y());
1011}
1012
initial.commit09911bf2008-07-26 23:55:291013void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
1014 int handle,
[email protected]bc1407f2009-09-29 00:33:351015 int key,
initial.commit09911bf2008-07-26 23:55:291016 int flags) {
[email protected]b410bc32009-08-14 01:11:141017 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:291018 return;
1019
[email protected]b410bc32009-08-14 01:11:141020 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291021 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:351022 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:381023 ((flags & views::Event::EF_CONTROL_DOWN) ==
1024 views::Event::EF_CONTROL_DOWN),
1025 ((flags & views::Event::EF_SHIFT_DOWN) ==
1026 views::Event::EF_SHIFT_DOWN),
1027 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301028 views::Event::EF_ALT_DOWN),
1029 ((flags & views::Event::EF_COMMAND_DOWN) ==
1030 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291031}
initial.commit09911bf2008-07-26 23:55:291032
[email protected]71f65dd2009-02-11 19:14:561033void AutomationProvider::IsWindowActive(int handle, bool* success,
1034 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291035 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171036 *is_active =
1037 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561038 *success = true;
initial.commit09911bf2008-07-26 23:55:291039 } else {
[email protected]71f65dd2009-02-11 19:14:561040 *success = false;
1041 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291042 }
1043}
1044
[email protected]71f65dd2009-02-11 19:14:561045void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1046 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291047
1048 if (browser_tracker_->ContainsHandle(handle)) {
1049 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561050 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291051 }
initial.commit09911bf2008-07-26 23:55:291052}
1053
[email protected]982921f12009-10-27 21:43:531054void AutomationProvider::GetType(int handle, int* type_as_int) {
1055 *type_as_int = -1; // -1 is the error code
1056
1057 if (browser_tracker_->ContainsHandle(handle)) {
1058 Browser* browser = browser_tracker_->GetResource(handle);
1059 *type_as_int = static_cast<int>(browser->type());
1060 }
1061}
1062
[email protected]71f65dd2009-02-11 19:14:561063void AutomationProvider::GetTab(int win_handle, int tab_index,
1064 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561065 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291066 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1067 Browser* browser = browser_tracker_->GetResource(win_handle);
1068 if (tab_index < browser->tab_count()) {
1069 TabContents* tab_contents =
1070 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571071 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291072 }
1073 }
initial.commit09911bf2008-07-26 23:55:291074}
1075
[email protected]71f65dd2009-02-11 19:14:561076void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1077 std::wstring* title) {
1078 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291079 if (tab_tracker_->ContainsHandle(handle)) {
1080 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361081 NavigationEntry* entry = tab->GetActiveEntry();
1082 if (entry != NULL) {
1083 *title = UTF16ToWideHack(entry->title());
1084 } else {
1085 *title = std::wstring();
1086 }
[email protected]71f65dd2009-02-11 19:14:561087 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291088 }
initial.commit09911bf2008-07-26 23:55:291089}
1090
[email protected]77bc6732009-04-20 22:01:031091void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1092 *tabstrip_index = -1; // -1 is the error code
1093
1094 if (tab_tracker_->ContainsHandle(handle)) {
1095 NavigationController* tab = tab_tracker_->GetResource(handle);
1096 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121097 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031098 }
1099}
1100
initial.commit09911bf2008-07-26 23:55:291101void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1102 if (window_tracker_->ContainsHandle(handle)) {
1103 window_tracker_->Remove(window_tracker_->GetResource(handle));
1104 }
1105}
1106
1107void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161108 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571109 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291110}
1111
1112// TODO(brettw) change this to accept GURLs when history supports it
1113void AutomationProvider::OnRedirectQueryComplete(
1114 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371115 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291116 bool success,
[email protected]379c2b12009-07-01 21:50:331117 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291118 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561119 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291120
[email protected]deb57402009-02-06 01:35:301121 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101122 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291123 if (success) {
initial.commit09911bf2008-07-26 23:55:291124 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301125 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291126 }
1127
[email protected]4f3dc372009-02-24 00:10:291128 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301129
[email protected]71f65dd2009-02-11 19:14:561130 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231131 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561132 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291133}
1134
1135bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571136 DCHECK(channel_.get());
1137 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291138}
1139
1140Browser* AutomationProvider::FindAndActivateTab(
1141 NavigationController* controller) {
1142 int tab_index;
1143 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1144 if (browser)
1145 browser->SelectTabContentsAt(tab_index, true);
1146
1147 return browser;
1148}
1149
[email protected]9eaa18e2010-06-29 20:51:011150namespace {
1151
1152class GetCookiesTask : public Task {
1153 public:
1154 GetCookiesTask(const GURL& url,
1155 URLRequestContextGetter* context_getter,
1156 base::WaitableEvent* event,
1157 std::string* cookies)
1158 : url_(url),
1159 context_getter_(context_getter),
1160 event_(event),
1161 cookies_(cookies) {}
1162
1163 virtual void Run() {
1164 *cookies_ = context_getter_->GetCookieStore()->GetCookies(url_);
1165 event_->Signal();
1166 }
1167
1168 private:
1169 const GURL& url_;
1170 URLRequestContextGetter* const context_getter_;
1171 base::WaitableEvent* const event_;
1172 std::string* const cookies_;
1173
1174 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
1175};
1176
1177std::string GetCookiesForURL(
1178 const GURL& url,
1179 URLRequestContextGetter* context_getter) {
1180 std::string cookies;
1181 base::WaitableEvent event(true /* manual reset */,
1182 false /* not initially signaled */);
1183 CHECK(ChromeThread::PostTask(
1184 ChromeThread::IO, FROM_HERE,
1185 new GetCookiesTask(url, context_getter, &event, &cookies)));
1186 event.Wait();
1187 return cookies;
1188}
1189
1190class SetCookieTask : public Task {
1191 public:
1192 SetCookieTask(const GURL& url,
1193 const std::string& value,
1194 URLRequestContextGetter* context_getter,
1195 base::WaitableEvent* event,
1196 bool* rv)
1197 : url_(url),
1198 value_(value),
1199 context_getter_(context_getter),
1200 event_(event),
1201 rv_(rv) {}
1202
1203 virtual void Run() {
1204 *rv_ = context_getter_->GetCookieStore()->SetCookie(url_, value_);
1205 event_->Signal();
1206 }
1207
1208 private:
1209 const GURL& url_;
1210 const std::string& value_;
1211 URLRequestContextGetter* const context_getter_;
1212 base::WaitableEvent* const event_;
1213 bool* const rv_;
1214
1215 DISALLOW_COPY_AND_ASSIGN(SetCookieTask);
1216};
1217
1218bool SetCookieForURL(
1219 const GURL& url,
1220 const std::string& value,
1221 URLRequestContextGetter* context_getter) {
1222 base::WaitableEvent event(true /* manual reset */,
1223 false /* not initially signaled */);
1224 bool rv = false;
1225 CHECK(ChromeThread::PostTask(
1226 ChromeThread::IO, FROM_HERE,
1227 new SetCookieTask(url, value, context_getter, &event, &rv)));
1228 event.Wait();
1229 return rv;
1230}
1231
1232class DeleteCookieTask : public Task {
1233 public:
1234 DeleteCookieTask(const GURL& url,
1235 const std::string& name,
1236 const scoped_refptr<URLRequestContextGetter>& context_getter)
1237 : url_(url),
1238 name_(name),
1239 context_getter_(context_getter) {}
1240
1241 virtual void Run() {
1242 net::CookieStore* cookie_store = context_getter_->GetCookieStore();
1243 cookie_store->DeleteCookie(url_, name_);
1244 }
1245
1246 private:
1247 const GURL url_;
1248 const std::string name_;
1249 const scoped_refptr<URLRequestContextGetter> context_getter_;
1250
1251 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
1252};
1253
1254} // namespace
1255
[email protected]71f65dd2009-02-11 19:14:561256void AutomationProvider::GetCookies(const GURL& url, int handle,
1257 int* value_size,
1258 std::string* value) {
1259 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291260 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1261 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311262
1263 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]674b3822010-08-04 04:02:511264 *value = GetCookiesForURL(url, tab->profile()->GetRequestContext());
[email protected]71f65dd2009-02-11 19:14:561265 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291266 }
initial.commit09911bf2008-07-26 23:55:291267}
1268
[email protected]71f65dd2009-02-11 19:14:561269void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291270 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561271 int handle,
1272 int* response_value) {
1273 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291274
1275 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1276 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311277
[email protected]674b3822010-08-04 04:02:511278 if (SetCookieForURL(url, value, tab->profile()->GetRequestContext()))
[email protected]71f65dd2009-02-11 19:14:561279 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291280 }
initial.commit09911bf2008-07-26 23:55:291281}
1282
[email protected]5fa57942010-04-21 23:07:221283void AutomationProvider::DeleteCookie(const GURL& url,
1284 const std::string& cookie_name,
1285 int handle, bool* success) {
1286 *success = false;
1287 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1288 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]9eaa18e2010-06-29 20:51:011289 ChromeThread::PostTask(
1290 ChromeThread::IO, FROM_HERE,
1291 new DeleteCookieTask(url, cookie_name,
1292 tab->profile()->GetRequestContext()));
[email protected]5fa57942010-04-21 23:07:221293 *success = true;
1294 }
1295}
1296
[email protected]a503c97c2010-07-16 13:05:481297void AutomationProvider::ShowCollectedCookiesDialog(
1298 int handle, bool* success) {
1299 *success = false;
1300 if (tab_tracker_->ContainsHandle(handle)) {
1301 TabContents* tab_contents =
1302 tab_tracker_->GetResource(handle)->tab_contents();
1303 tab_contents->delegate()->ShowCollectedCookiesDialog(tab_contents);
1304 *success = true;
1305 }
1306}
1307
[email protected]71f65dd2009-02-11 19:14:561308void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1309 *success = false;
initial.commit09911bf2008-07-26 23:55:291310 if (tab_tracker_->ContainsHandle(handle)) {
1311 NavigationController* tab = tab_tracker_->GetResource(handle);
1312 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541313 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561314 *success = true;
initial.commit09911bf2008-07-26 23:55:291315 }
initial.commit09911bf2008-07-26 23:55:291316}
1317
[email protected]71f65dd2009-02-11 19:14:561318void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1319 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291320
1321 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561322 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341323 TabContents* tab_contents =
1324 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161325 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1326 if (rph)
1327 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291328 }
initial.commit09911bf2008-07-26 23:55:291329}
1330
1331void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331332 NOTREACHED() << "This function has been deprecated. "
1333 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291334}
1335
[email protected]71f65dd2009-02-11 19:14:561336void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291337 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561338 const std::wstring& script,
1339 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291340 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341341 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1342 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571343 // Set the routing id of this message with the controller.
1344 // This routing id needs to be remembered for the reverse
1345 // communication while sending back the response of
1346 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331347 std::wstring set_automation_id;
1348 SStringPrintf(&set_automation_id,
1349 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561350 reply_message->routing_id());
1351
1352 DCHECK(reply_message_ == NULL);
1353 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291354
[email protected]57c6a652009-05-04 07:58:341355 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331356 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341357 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061358 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571359 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291360 }
1361
1362 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561363 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1364 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291365 }
1366}
1367
[email protected]71f65dd2009-02-11 19:14:561368void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1369 *visible = false;
[email protected]20e93d12008-08-28 16:31:571370
[email protected]59560e0b2009-06-04 03:30:221371 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251372#if defined(OS_CHROMEOS)
1373 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1374 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1375 for (BrowserList::const_iterator it = BrowserList::begin();
1376 it != BrowserList::end(); ++it) {
1377 if ((*it)->type() == Browser::TYPE_POPUP) {
1378 const GURL& url =
1379 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1380
1381 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1382 url.host() == chrome::kChromeUIFileBrowseHost) {
1383 *visible = true;
1384 break;
1385 }
1386 }
1387 }
1388#else
[email protected]59560e0b2009-06-04 03:30:221389 Browser* browser = browser_tracker_->GetResource(handle);
1390 if (browser) {
1391 *visible = browser->window()->IsDownloadShelfVisible();
1392 }
[email protected]f5bf8ccf2010-02-05 18:19:251393#endif
[email protected]59560e0b2009-06-04 03:30:221394 }
initial.commit09911bf2008-07-26 23:55:291395}
1396
[email protected]59560e0b2009-06-04 03:30:221397void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1398 if (browser_tracker_->ContainsHandle(handle)) {
1399 Browser* browser = browser_tracker_->GetResource(handle);
1400 if (browser) {
1401 if (visible)
1402 browser->window()->GetDownloadShelf()->Show();
1403 else
1404 browser->window()->GetDownloadShelf()->Close();
1405 }
1406 }
1407}
1408
[email protected]34930432009-11-09 00:12:091409void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1410 *visible = false;
1411
1412 if (browser_tracker_->ContainsHandle(handle)) {
1413 Browser* browser = browser_tracker_->GetResource(handle);
1414 if (browser)
1415 *visible = browser->window()->IsFullscreen();
1416 }
1417}
1418
1419void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1420 bool* visible) {
1421 *visible = false;
1422
1423 if (browser_tracker_->ContainsHandle(handle)) {
1424 Browser* browser = browser_tracker_->GetResource(handle);
1425 if (browser)
1426 *visible = browser->window()->IsFullscreenBubbleVisible();
1427 }
1428}
[email protected]59560e0b2009-06-04 03:30:221429
[email protected]71f65dd2009-02-11 19:14:561430void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1431 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291432 if (tab_tracker_->ContainsHandle(handle)) {
1433 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111434 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291435 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561436 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291437 }
1438 }
initial.commit09911bf2008-07-26 23:55:291439}
1440
initial.commit09911bf2008-07-26 23:55:291441void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561442 int handle, const std::wstring& find_request,
1443 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311444 NOTREACHED() << "This function has been deprecated."
1445 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561446 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311447 return;
1448}
1449
[email protected]4f999132009-03-31 18:08:401450void AutomationProvider::HandleFindRequest(
1451 int handle,
1452 const AutomationMsg_Find_Params& params,
1453 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291454 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561455 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1456 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291457 return;
1458 }
1459
1460 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111461 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291462
1463 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141464 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291465
[email protected]57c6a652009-05-04 07:58:341466 tab_contents->set_current_find_request_id(
1467 FindInPageNotificationObserver::kFindInPageRequestId);
1468 tab_contents->render_view_host()->StartFinding(
1469 FindInPageNotificationObserver::kFindInPageRequestId,
1470 params.search_string, params.forward, params.match_case,
1471 params.find_next);
initial.commit09911bf2008-07-26 23:55:291472}
1473
[email protected]5f8af2a2008-08-06 22:49:451474void AutomationProvider::HandleOpenFindInPageRequest(
1475 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291476 if (browser_tracker_->ContainsHandle(handle)) {
1477 Browser* browser = browser_tracker_->GetResource(handle);
1478 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451479 }
1480}
1481
[email protected]71f65dd2009-02-11 19:14:561482void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561483 *visible = false;
[email protected]855c0142009-09-28 22:35:241484 Browser* browser = browser_tracker_->GetResource(handle);
1485 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581486 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171487 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241488 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291489 }
[email protected]20e93d12008-08-28 16:31:571490}
1491
[email protected]71f65dd2009-02-11 19:14:561492void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1493 int* y) {
[email protected]9e0534b2008-10-21 15:03:011494 gfx::Point position(0, 0);
1495 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291496 if (browser_tracker_->ContainsHandle(handle)) {
1497 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581498 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171499 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581500 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291501 }
[email protected]20e93d12008-08-28 16:31:571502
[email protected]71f65dd2009-02-11 19:14:561503 *x = position.x();
1504 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571505}
1506
[email protected]4512cb52010-04-05 19:50:251507// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1508// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581509void AutomationProvider::GetBookmarkBarVisibility(int handle,
1510 bool* visible,
1511 bool* animating) {
1512 *visible = false;
1513 *animating = false;
1514
1515 if (browser_tracker_->ContainsHandle(handle)) {
1516 Browser* browser = browser_tracker_->GetResource(handle);
1517 if (browser) {
[email protected]472f099b2010-05-27 17:07:121518#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1519 // TODO(jrg): Was removed in rev43789 for perf. Need to investigate.
1520
[email protected]ab6ca392010-04-07 00:44:131521 // IsBookmarkBarVisible() line looks correct but is not
1522 // consistent across platforms. Specifically, on Mac/Linux, it
1523 // returns false if the bar is hidden in a pref (even if visible
1524 // on the NTP). On ChromeOS, it returned true if on NTP
1525 // independent of the pref. Making the code more consistent
1526 // caused a perf bot regression on Windows (which shares views).
1527 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251528 *visible = browser->profile()->GetPrefs()->GetBoolean(
1529 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401530#else
1531 *visible = browser->window()->IsBookmarkBarVisible();
1532#endif
[email protected]c3240722010-03-05 21:52:581533 *animating = browser->window()->IsBookmarkBarAnimating();
1534 }
1535 }
1536}
1537
[email protected]6d8ffc9f2010-03-12 18:27:531538void AutomationProvider::GetBookmarksAsJSON(int handle,
1539 std::string* bookmarks_as_json,
1540 bool *success) {
1541 *success = false;
1542 if (browser_tracker_->ContainsHandle(handle)) {
1543 Browser* browser = browser_tracker_->GetResource(handle);
1544 if (browser) {
1545 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1546 return;
1547 }
1548 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1549 browser->profile(),
1550 browser->profile()->GetBookmarkModel());
1551 *success = storage->SerializeData(bookmarks_as_json);
1552 }
1553 }
1554}
1555
1556void AutomationProvider::WaitForBookmarkModelToLoad(
1557 int handle,
1558 IPC::Message* reply_message) {
1559 if (browser_tracker_->ContainsHandle(handle)) {
1560 Browser* browser = browser_tracker_->GetResource(handle);
1561 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1562 if (model->IsLoaded()) {
1563 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1564 reply_message, true);
1565 Send(reply_message);
1566 } else {
1567 // The observer will delete itself when done.
1568 new AutomationProviderBookmarkModelObserver(this, reply_message,
1569 model);
1570 }
1571 }
1572}
1573
1574void AutomationProvider::AddBookmarkGroup(int handle,
1575 int64 parent_id, int index,
1576 std::wstring title,
1577 bool* success) {
1578 if (browser_tracker_->ContainsHandle(handle)) {
1579 Browser* browser = browser_tracker_->GetResource(handle);
1580 if (browser) {
1581 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1582 if (!model->IsLoaded()) {
1583 *success = false;
1584 return;
1585 }
1586 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1587 DCHECK(parent);
1588 if (parent) {
1589 const BookmarkNode* child = model->AddGroup(parent, index,
[email protected]ff4c1d82010-08-04 16:58:121590 WideToUTF16Hack(title));
[email protected]6d8ffc9f2010-03-12 18:27:531591 DCHECK(child);
1592 if (child)
1593 *success = true;
1594 }
1595 }
1596 }
1597 *success = false;
1598}
1599
1600void AutomationProvider::AddBookmarkURL(int handle,
1601 int64 parent_id, int index,
1602 std::wstring title, const GURL& url,
1603 bool* success) {
1604 if (browser_tracker_->ContainsHandle(handle)) {
1605 Browser* browser = browser_tracker_->GetResource(handle);
1606 if (browser) {
1607 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1608 if (!model->IsLoaded()) {
1609 *success = false;
1610 return;
1611 }
1612 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1613 DCHECK(parent);
1614 if (parent) {
1615 const BookmarkNode* child = model->AddURL(parent, index,
[email protected]ff4c1d82010-08-04 16:58:121616 WideToUTF16Hack(title), url);
[email protected]6d8ffc9f2010-03-12 18:27:531617 DCHECK(child);
1618 if (child)
1619 *success = true;
1620 }
1621 }
1622 }
1623 *success = false;
1624}
1625
1626void AutomationProvider::ReparentBookmark(int handle,
1627 int64 id, int64 new_parent_id,
1628 int index,
1629 bool* success) {
1630 if (browser_tracker_->ContainsHandle(handle)) {
1631 Browser* browser = browser_tracker_->GetResource(handle);
1632 if (browser) {
1633 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1634 if (!model->IsLoaded()) {
1635 *success = false;
1636 return;
1637 }
1638 const BookmarkNode* node = model->GetNodeByID(id);
1639 DCHECK(node);
1640 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1641 DCHECK(new_parent);
1642 if (node && new_parent) {
1643 model->Move(node, new_parent, index);
1644 *success = true;
1645 }
1646 }
1647 }
1648 *success = false;
1649}
1650
1651void AutomationProvider::SetBookmarkTitle(int handle,
1652 int64 id, std::wstring title,
1653 bool* success) {
1654 if (browser_tracker_->ContainsHandle(handle)) {
1655 Browser* browser = browser_tracker_->GetResource(handle);
1656 if (browser) {
1657 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1658 if (!model->IsLoaded()) {
1659 *success = false;
1660 return;
1661 }
1662 const BookmarkNode* node = model->GetNodeByID(id);
1663 DCHECK(node);
1664 if (node) {
[email protected]ff4c1d82010-08-04 16:58:121665 model->SetTitle(node, WideToUTF16Hack(title));
[email protected]6d8ffc9f2010-03-12 18:27:531666 *success = true;
1667 }
1668 }
1669 }
1670 *success = false;
1671}
1672
1673void AutomationProvider::SetBookmarkURL(int handle,
1674 int64 id, const GURL& url,
1675 bool* success) {
1676 if (browser_tracker_->ContainsHandle(handle)) {
1677 Browser* browser = browser_tracker_->GetResource(handle);
1678 if (browser) {
1679 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1680 if (!model->IsLoaded()) {
1681 *success = false;
1682 return;
1683 }
1684 const BookmarkNode* node = model->GetNodeByID(id);
1685 DCHECK(node);
1686 if (node) {
1687 model->SetURL(node, url);
1688 *success = true;
1689 }
1690 }
1691 }
1692 *success = false;
1693}
1694
1695void AutomationProvider::RemoveBookmark(int handle,
1696 int64 id,
1697 bool* success) {
1698 if (browser_tracker_->ContainsHandle(handle)) {
1699 Browser* browser = browser_tracker_->GetResource(handle);
1700 if (browser) {
1701 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1702 if (!model->IsLoaded()) {
1703 *success = false;
1704 return;
1705 }
1706 const BookmarkNode* node = model->GetNodeByID(id);
1707 DCHECK(node);
1708 if (node) {
1709 const BookmarkNode* parent = node->GetParent();
1710 DCHECK(parent);
1711 model->Remove(parent, parent->IndexOfChild(node));
1712 *success = true;
1713 }
1714 }
1715 }
1716 *success = false;
1717}
1718
[email protected]ef413ca2010-05-25 21:09:141719// Sample json input: { "command": "SetWindowDimensions",
1720// "x": 20, # optional
1721// "y": 20, # optional
1722// "width": 800, # optional
1723// "height": 600 } # optional
1724void AutomationProvider::SetWindowDimensions(Browser* browser,
1725 DictionaryValue* args,
1726 IPC::Message* reply_message) {
1727 gfx::Rect rect = browser->window()->GetRestoredBounds();
1728 int x, y, width, height;
[email protected]ff4c1d82010-08-04 16:58:121729 if (args->GetInteger("x", &x))
[email protected]ef413ca2010-05-25 21:09:141730 rect.set_x(x);
[email protected]ff4c1d82010-08-04 16:58:121731 if (args->GetInteger("y", &y))
[email protected]ef413ca2010-05-25 21:09:141732 rect.set_y(y);
[email protected]ff4c1d82010-08-04 16:58:121733 if (args->GetInteger("width", &width))
[email protected]ef413ca2010-05-25 21:09:141734 rect.set_width(width);
[email protected]ff4c1d82010-08-04 16:58:121735 if (args->GetInteger("height", &height))
[email protected]ef413ca2010-05-25 21:09:141736 rect.set_height(height);
1737 browser->window()->SetBounds(rect);
[email protected]7c983cc2010-07-16 11:33:341738 AutomationJSONReply(this, reply_message).SendSuccess(NULL);
[email protected]ef413ca2010-05-25 21:09:141739}
1740
[email protected]38b5a3852010-07-21 06:49:521741ListValue* AutomationProvider::GetInfobarsInfo(TabContents* tc) {
1742 // Each infobar may have different properties depending on the type.
1743 ListValue* infobars = new ListValue;
1744 for (int infobar_index = 0;
1745 infobar_index < tc->infobar_delegate_count();
1746 ++infobar_index) {
1747 DictionaryValue* infobar_item = new DictionaryValue;
1748 InfoBarDelegate* infobar = tc->GetInfoBarDelegateAt(infobar_index);
1749 if (infobar->AsConfirmInfoBarDelegate()) {
1750 // Also covers ThemeInstalledInfoBarDelegate and
1751 // CrashedExtensionInfoBarDelegate.
[email protected]ff4c1d82010-08-04 16:58:121752 infobar_item->SetString("type", "confirm_infobar");
[email protected]38b5a3852010-07-21 06:49:521753 ConfirmInfoBarDelegate* confirm_infobar =
1754 infobar->AsConfirmInfoBarDelegate();
[email protected]ff4c1d82010-08-04 16:58:121755 infobar_item->SetString("text",
1756 WideToUTF16Hack(confirm_infobar->GetMessageText()));
1757 infobar_item->SetString("link_text",
1758 WideToUTF16Hack(confirm_infobar->GetLinkText()));
[email protected]38b5a3852010-07-21 06:49:521759 ListValue* buttons_list = new ListValue;
1760 int buttons = confirm_infobar->GetButtons();
1761 if (ConfirmInfoBarDelegate::BUTTON_OK & buttons) {
1762 StringValue* button_label = new StringValue(
1763 confirm_infobar->GetButtonLabel(
1764 ConfirmInfoBarDelegate::BUTTON_OK));
1765 buttons_list->Append(button_label);
1766 }
1767 if (ConfirmInfoBarDelegate::BUTTON_CANCEL & buttons) {
1768 StringValue* button_label = new StringValue(
1769 confirm_infobar->GetButtonLabel(
1770 ConfirmInfoBarDelegate::BUTTON_CANCEL));
1771 buttons_list->Append(button_label);
1772 }
[email protected]ff4c1d82010-08-04 16:58:121773 infobar_item->Set("buttons", buttons_list);
[email protected]38b5a3852010-07-21 06:49:521774 } else if (infobar->AsAlertInfoBarDelegate()) {
[email protected]ff4c1d82010-08-04 16:58:121775 infobar_item->SetString("type", "alert_infobar");
[email protected]38b5a3852010-07-21 06:49:521776 AlertInfoBarDelegate* alert_infobar =
1777 infobar->AsAlertInfoBarDelegate();
[email protected]ff4c1d82010-08-04 16:58:121778 infobar_item->SetString("text",
1779 WideToUTF16Hack(alert_infobar->GetMessageText()));
[email protected]38b5a3852010-07-21 06:49:521780 } else if (infobar->AsLinkInfoBarDelegate()) {
[email protected]ff4c1d82010-08-04 16:58:121781 infobar_item->SetString("type", "link_infobar");
[email protected]38b5a3852010-07-21 06:49:521782 LinkInfoBarDelegate* link_infobar = infobar->AsLinkInfoBarDelegate();
[email protected]ff4c1d82010-08-04 16:58:121783 infobar_item->SetString("link_text",
1784 WideToUTF16Hack(link_infobar->GetLinkText()));
[email protected]38b5a3852010-07-21 06:49:521785 } else if (infobar->AsTranslateInfoBarDelegate()) {
[email protected]ff4c1d82010-08-04 16:58:121786 infobar_item->SetString("type", "translate_infobar");
[email protected]38b5a3852010-07-21 06:49:521787 TranslateInfoBarDelegate* translate_infobar =
1788 infobar->AsTranslateInfoBarDelegate();
[email protected]ff4c1d82010-08-04 16:58:121789 infobar_item->SetString("original_lang_code",
[email protected]38b5a3852010-07-21 06:49:521790 translate_infobar->GetOriginalLanguageCode());
[email protected]ff4c1d82010-08-04 16:58:121791 infobar_item->SetString("target_lang_code",
[email protected]38b5a3852010-07-21 06:49:521792 translate_infobar->GetTargetLanguageCode());
1793 } else if (infobar->AsExtensionInfoBarDelegate()) {
[email protected]ff4c1d82010-08-04 16:58:121794 infobar_item->SetString("type", "extension_infobar");
[email protected]38b5a3852010-07-21 06:49:521795 } else {
[email protected]ff4c1d82010-08-04 16:58:121796 infobar_item->SetString("type", "unknown_infobar");
[email protected]38b5a3852010-07-21 06:49:521797 }
1798 infobars->Append(infobar_item);
1799 }
1800 return infobars;
1801}
1802
1803// Sample json input: { "command": "WaitForInfobarCount",
1804// "count": COUNT,
1805// "tab_index": INDEX }
1806// Sample output: {}
1807void AutomationProvider::WaitForInfobarCount(Browser* browser,
1808 DictionaryValue* args,
1809 IPC::Message* reply_message) {
1810 int tab_index;
1811 int count;
[email protected]ff4c1d82010-08-04 16:58:121812 if (!args->GetInteger("count", &count) || count < 0 ||
1813 !args->GetInteger("tab_index", &tab_index) || tab_index < 0) {
[email protected]38b5a3852010-07-21 06:49:521814 AutomationJSONReply(this, reply_message).SendError(
1815 "Missing or invalid args: 'count', 'tab_index'.");
1816 return;
1817 }
1818
1819 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
1820 // Observer deletes itself.
1821 new WaitForInfobarCountObserver(this, reply_message, tab_contents, count);
1822}
1823
[email protected]e004a2d2010-07-22 04:55:281824// Sample json input: { "command": "PerformActionOnInfobar",
1825// "action": "dismiss",
1826// "infobar_index": 0,
1827// "tab_index": 0 }
1828// Sample output: {}
1829void AutomationProvider::PerformActionOnInfobar(Browser* browser,
1830 DictionaryValue* args,
1831 IPC::Message* reply_message) {
1832 AutomationJSONReply reply(this, reply_message);
1833 int tab_index;
1834 int infobar_index;
1835 std::string action;
[email protected]ff4c1d82010-08-04 16:58:121836 if (!args->GetInteger("tab_index", &tab_index) ||
1837 !args->GetInteger("infobar_index", &infobar_index) ||
1838 !args->GetString("action", &action)) {
[email protected]e004a2d2010-07-22 04:55:281839 reply.SendError("Invalid or missing args");
1840 return;
1841 }
1842 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
1843 if (!tab_contents) {
1844 reply.SendError(StringPrintf("No such tab at index %d", tab_index));
1845 return;
1846 }
1847 InfoBarDelegate* infobar = NULL;
1848 if (infobar_index < 0 ||
1849 infobar_index >= tab_contents->infobar_delegate_count() ||
1850 !(infobar = tab_contents->GetInfoBarDelegateAt(infobar_index))) {
1851 reply.SendError(StringPrintf("No such infobar at index %d", infobar_index));
1852 return;
1853 }
1854 if ("dismiss" == action) {
1855 infobar->InfoBarDismissed();
1856 tab_contents->RemoveInfoBar(infobar);
1857 reply.SendSuccess(NULL);
1858 return;
1859 }
1860 if ("accept" == action || "cancel" == action) {
1861 ConfirmInfoBarDelegate* confirm_infobar;
1862 if (!(confirm_infobar = infobar->AsConfirmInfoBarDelegate())) {
1863 reply.SendError("Not a confirm infobar");
1864 return;
1865 }
1866 if ("accept" == action) {
1867 if (confirm_infobar->Accept())
1868 tab_contents->RemoveInfoBar(infobar);
1869 } else if ("cancel" == action) {
1870 if (confirm_infobar->Cancel())
1871 tab_contents->RemoveInfoBar(infobar);
1872 }
1873 reply.SendSuccess(NULL);
1874 return;
1875 }
1876 reply.SendError("Invalid action");
1877}
1878
[email protected]26adfd312010-07-20 01:04:461879namespace {
1880
1881// Task to get info about BrowserChildProcessHost. Must run on IO thread to
1882// honor the semantics of BrowserChildProcessHost.
1883// Used by AutomationProvider::GetBrowserInfo().
1884class GetChildProcessHostInfoTask : public Task {
1885 public:
1886 GetChildProcessHostInfoTask(base::WaitableEvent* event,
1887 ListValue* child_processes)
1888 : event_(event),
1889 child_processes_(child_processes) {}
1890
1891 virtual void Run() {
1892 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
1893 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
1894 // Only add processes which are already started,
1895 // since we need their handle.
1896 if ((*iter)->handle() == base::kNullProcessHandle) {
1897 continue;
1898 }
1899 ChildProcessInfo* info = *iter;
1900 DictionaryValue* item = new DictionaryValue;
[email protected]ff4c1d82010-08-04 16:58:121901 item->SetString("name", WideToUTF16Hack(info->name()));
1902 item->SetString("type",
1903 WideToUTF16Hack(ChildProcessInfo::GetTypeNameInEnglish(
1904 info->type())));
1905 item->SetInteger("pid", base::GetProcId(info->handle()));
[email protected]26adfd312010-07-20 01:04:461906 child_processes_->Append(item);
1907 }
1908 event_->Signal();
1909 }
1910
1911 private:
1912 base::WaitableEvent* const event_; // weak
1913 ListValue* child_processes_;
1914
1915 DISALLOW_COPY_AND_ASSIGN(GetChildProcessHostInfoTask);
1916};
1917
1918} // namespace
1919
[email protected]a9ff2c02010-05-13 17:33:051920// Sample json input: { "command": "GetBrowserInfo" }
1921// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1922// sample json output.
[email protected]53329582010-05-14 21:10:581923void AutomationProvider::GetBrowserInfo(Browser* browser,
1924 DictionaryValue* args,
[email protected]a9ff2c02010-05-13 17:33:051925 IPC::Message* reply_message) {
[email protected]a9ff2c02010-05-13 17:33:051926 DictionaryValue* properties = new DictionaryValue;
[email protected]ff4c1d82010-08-04 16:58:121927 properties->SetString("ChromeVersion", chrome::kChromeVersion);
1928 properties->SetString("BrowserProcessExecutableName",
1929 WideToUTF16Hack(chrome::kBrowserProcessExecutableName));
1930 properties->SetString("HelperProcessExecutableName",
1931 WideToUTF16Hack(chrome::kHelperProcessExecutableName));
1932 properties->SetString("BrowserProcessExecutablePath",
1933 WideToUTF16Hack(chrome::kBrowserProcessExecutablePath));
1934 properties->SetString("HelperProcessExecutablePath",
[email protected]a9ff2c02010-05-13 17:33:051935 chrome::kHelperProcessExecutablePath);
[email protected]ff4c1d82010-08-04 16:58:121936 properties->SetString("command_line_string",
[email protected]a9ff2c02010-05-13 17:33:051937 CommandLine::ForCurrentProcess()->command_line_string());
[email protected]44eed9f2010-06-28 22:04:001938
1939 std::string branding;
1940#if defined(GOOGLE_CHROME_BUILD)
1941 branding = "Google Chrome";
1942#elif defined(CHROMIUM_BUILD)
1943 branding = "Chromium";
1944#else
1945 branding = "Unknown Branding";
[email protected]a9ff2c02010-05-13 17:33:051946#endif
[email protected]ff4c1d82010-08-04 16:58:121947 properties->SetString("branding", branding);
[email protected]a9ff2c02010-05-13 17:33:051948
1949 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]ff4c1d82010-08-04 16:58:121950 return_value->Set("properties", properties);
[email protected]a9ff2c02010-05-13 17:33:051951
[email protected]ff4c1d82010-08-04 16:58:121952 return_value->SetInteger("browser_pid", base::GetCurrentProcId());
[email protected]ef413ca2010-05-25 21:09:141953 // Add info about all windows in a list of dictionaries, one dictionary
1954 // item per window.
1955 ListValue* windows = new ListValue;
1956 int windex = 0;
1957 for (BrowserList::const_iterator it = BrowserList::begin();
1958 it != BrowserList::end();
1959 ++it, ++windex) {
1960 DictionaryValue* browser_item = new DictionaryValue;
1961 browser = *it;
[email protected]ff4c1d82010-08-04 16:58:121962 browser_item->SetInteger("index", windex);
[email protected]ef413ca2010-05-25 21:09:141963 // Window properties
1964 gfx::Rect rect = browser->window()->GetRestoredBounds();
[email protected]ff4c1d82010-08-04 16:58:121965 browser_item->SetInteger("x", rect.x());
1966 browser_item->SetInteger("y", rect.y());
1967 browser_item->SetInteger("width", rect.width());
1968 browser_item->SetInteger("height", rect.height());
1969 browser_item->SetBoolean("fullscreen",
[email protected]ef413ca2010-05-25 21:09:141970 browser->window()->IsFullscreen());
[email protected]ff4c1d82010-08-04 16:58:121971 browser_item->SetInteger("selected_tab", browser->selected_index());
1972 browser_item->SetBoolean("incognito",
[email protected]ef413ca2010-05-25 21:09:141973 browser->profile()->IsOffTheRecord());
1974 // For each window, add info about all tabs in a list of dictionaries,
1975 // one dictionary item per tab.
1976 ListValue* tabs = new ListValue;
1977 for (int i = 0; i < browser->tab_count(); ++i) {
1978 TabContents* tc = browser->GetTabContentsAt(i);
1979 DictionaryValue* tab = new DictionaryValue;
[email protected]ff4c1d82010-08-04 16:58:121980 tab->SetInteger("index", i);
1981 tab->SetString("url", tc->GetURL().spec());
1982 tab->SetInteger("renderer_pid",
[email protected]ef413ca2010-05-25 21:09:141983 base::GetProcId(tc->GetRenderProcessHost()->GetHandle()));
[email protected]ff4c1d82010-08-04 16:58:121984 tab->Set("infobars", GetInfobarsInfo(tc));
[email protected]ef413ca2010-05-25 21:09:141985 tabs->Append(tab);
1986 }
[email protected]ff4c1d82010-08-04 16:58:121987 browser_item->Set("tabs", tabs);
[email protected]ef413ca2010-05-25 21:09:141988
1989 windows->Append(browser_item);
1990 }
[email protected]ff4c1d82010-08-04 16:58:121991 return_value->Set("windows", windows);
[email protected]ef413ca2010-05-25 21:09:141992
[email protected]ff4c1d82010-08-04 16:58:121993 return_value->SetString("child_process_path",
[email protected]ef413ca2010-05-25 21:09:141994 ChildProcessHost::GetChildPath(true).value());
1995 // Child processes are the processes for plugins and other workers.
1996 // Add all child processes in a list of dictionaries, one dictionary item
1997 // per child process.
1998 ListValue* child_processes = new ListValue;
[email protected]26adfd312010-07-20 01:04:461999 base::WaitableEvent event(true /* manual reset */,
2000 false /* not initially signaled */);
2001 CHECK(ChromeThread::PostTask(
2002 ChromeThread::IO, FROM_HERE,
2003 new GetChildProcessHostInfoTask(&event, child_processes)));
2004 event.Wait();
[email protected]ff4c1d82010-08-04 16:58:122005 return_value->Set("child_processes", child_processes);
[email protected]ef413ca2010-05-25 21:09:142006
2007 // Add all extension processes in a list of dictionaries, one dictionary
2008 // item per extension process.
2009 ListValue* extension_processes = new ListValue;
2010 ProfileManager* profile_manager = g_browser_process->profile_manager();
2011 for (ProfileManager::const_iterator it = profile_manager->begin();
2012 it != profile_manager->end(); ++it) {
2013 ExtensionProcessManager* process_manager =
2014 (*it)->GetExtensionProcessManager();
2015 ExtensionProcessManager::const_iterator jt;
2016 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) {
2017 ExtensionHost* ex_host = *jt;
2018 // Don't add dead extension processes.
2019 if (!ex_host->IsRenderViewLive())
2020 continue;
2021 DictionaryValue* item = new DictionaryValue;
[email protected]ff4c1d82010-08-04 16:58:122022 item->SetString("name", ex_host->extension()->name());
[email protected]ef413ca2010-05-25 21:09:142023 item->SetInteger(
[email protected]ff4c1d82010-08-04 16:58:122024 "pid",
[email protected]ef413ca2010-05-25 21:09:142025 base::GetProcId(ex_host->render_process_host()->GetHandle()));
2026 extension_processes->Append(item);
2027 }
2028 }
[email protected]ff4c1d82010-08-04 16:58:122029 return_value->Set("extension_processes", extension_processes);
[email protected]7c983cc2010-07-16 11:33:342030 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]a9ff2c02010-05-13 17:33:052031}
2032
[email protected]24e2b102010-04-29 17:56:472033// Sample json input: { "command": "GetHistoryInfo",
2034// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:362035// Refer chrome/test/pyautolib/history_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582036void AutomationProvider::GetHistoryInfo(Browser* browser,
2037 DictionaryValue* args,
2038 IPC::Message* reply_message) {
[email protected]e6e376e2010-04-19 21:41:362039 consumer_.CancelAllRequests();
2040
[email protected]e53668962010-06-23 15:35:252041 string16 search_text;
2042 args->GetString("search_text", &search_text);
[email protected]e6e376e2010-04-19 21:41:362043
2044 // Fetch history.
2045 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
2046 history::QueryOptions options;
2047 // The observer owns itself. It deletes itself after it fetches history.
2048 AutomationProviderHistoryObserver* history_observer =
2049 new AutomationProviderHistoryObserver(this, reply_message);
2050 hs->QueryHistory(
2051 search_text,
2052 options,
2053 &consumer_,
2054 NewCallback(history_observer,
2055 &AutomationProviderHistoryObserver::HistoryQueryComplete));
2056}
2057
[email protected]bbe6aa02010-05-07 17:27:292058// Sample json input: { "command": "AddHistoryItem",
2059// "item": { "URL": "http://www.google.com",
2060// "title": "Google", # optional
2061// "time": 12345 # optional (time_t)
2062// } }
2063// Refer chrome/test/pyautolib/pyauto.py for details on input.
[email protected]53329582010-05-14 21:10:582064void AutomationProvider::AddHistoryItem(Browser* browser,
2065 DictionaryValue* args,
2066 IPC::Message* reply_message) {
[email protected]bbe6aa02010-05-07 17:27:292067 DictionaryValue* item = NULL;
[email protected]ff4c1d82010-08-04 16:58:122068 args->GetDictionary("item", &item);
[email protected]bbe6aa02010-05-07 17:27:292069 string16 url_text;
[email protected]e53668962010-06-23 15:35:252070 string16 title;
[email protected]bbe6aa02010-05-07 17:27:292071 base::Time time = base::Time::Now();
[email protected]7c983cc2010-07-16 11:33:342072 AutomationJSONReply reply(this, reply_message);
[email protected]bbe6aa02010-05-07 17:27:292073
[email protected]7c983cc2010-07-16 11:33:342074 if (!item->GetString("url", &url_text)) {
2075 reply.SendError("bad args (no URL in dict?)");
2076 return;
[email protected]bbe6aa02010-05-07 17:27:292077 }
[email protected]7c983cc2010-07-16 11:33:342078 GURL gurl(url_text);
2079 item->GetString("title", &title); // Don't care if it fails.
2080 int it;
2081 double dt;
[email protected]ff4c1d82010-08-04 16:58:122082 if (item->GetInteger("time", &it))
[email protected]7c983cc2010-07-16 11:33:342083 time = base::Time::FromTimeT(it);
[email protected]ff4c1d82010-08-04 16:58:122084 else if (item->GetReal("time", &dt))
[email protected]7c983cc2010-07-16 11:33:342085 time = base::Time::FromDoubleT(dt);
[email protected]f6ff0df2010-07-11 22:41:432086
[email protected]7c983cc2010-07-16 11:33:342087 // Ideas for "dummy" values (e.g. id_scope) came from
2088 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
2089 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
2090 const void* id_scope = reinterpret_cast<void*>(1);
2091 hs->AddPage(gurl, time,
2092 id_scope,
2093 0,
2094 GURL(),
2095 PageTransition::LINK,
2096 history::RedirectList(),
2097 false);
2098 if (title.length())
2099 hs->SetPageTitle(gurl, title);
2100 reply.SendSuccess(NULL);
[email protected]bbe6aa02010-05-07 17:27:292101}
2102
[email protected]24e2b102010-04-29 17:56:472103// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:362104// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582105void AutomationProvider::GetDownloadsInfo(Browser* browser,
2106 DictionaryValue* args,
2107 IPC::Message* reply_message) {
[email protected]d4adc292010-04-15 18:06:392108 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]7c983cc2010-07-16 11:33:342109 AutomationJSONReply reply(this, reply_message);
[email protected]d4adc292010-04-15 18:06:392110
2111 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:342112 reply.SendError("no download manager");
2113 return;
[email protected]d4adc292010-04-15 18:06:392114 }
[email protected]82f37b02010-07-29 22:04:572115
2116 std::vector<DownloadItem*> downloads;
2117 profile_->GetDownloadManager()->GetAllDownloads(FilePath(), &downloads);
[email protected]d4adc292010-04-15 18:06:392118
2119 std::map<DownloadItem::DownloadState, std::string> state_to_string;
2120 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
2121 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
2122 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
2123 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
2124
2125 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
2126 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
2127 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
2128 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
2129 std::string("DANGEROUS_BUT_VALIDATED");
2130
2131 ListValue* list_of_downloads = new ListValue;
2132 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
2133 it != downloads.end();
2134 it++) { // Fill info about each download item.
2135 DictionaryValue* dl_item_value = new DictionaryValue;
[email protected]ff4c1d82010-08-04 16:58:122136 dl_item_value->SetInteger("id", static_cast<int>((*it)->id()));
2137 dl_item_value->SetString("url", (*it)->url().spec());
2138 dl_item_value->SetString("referrer_url", (*it)->referrer_url().spec());
2139 dl_item_value->SetString("file_name", (*it)->GetFileName().value());
2140 dl_item_value->SetString("full_path", (*it)->full_path().value());
2141 dl_item_value->SetBoolean("is_paused", (*it)->is_paused());
2142 dl_item_value->SetBoolean("open_when_complete",
[email protected]d4adc292010-04-15 18:06:392143 (*it)->open_when_complete());
[email protected]ff4c1d82010-08-04 16:58:122144 dl_item_value->SetBoolean("is_extension_install",
[email protected]d4adc292010-04-15 18:06:392145 (*it)->is_extension_install());
[email protected]ff4c1d82010-08-04 16:58:122146 dl_item_value->SetBoolean("is_temporary", (*it)->is_temporary());
2147 dl_item_value->SetBoolean("is_otr", (*it)->is_otr()); // off-the-record
2148 dl_item_value->SetString("state", state_to_string[(*it)->state()]);
2149 dl_item_value->SetString("safety_state",
[email protected]d4adc292010-04-15 18:06:392150 safety_state_to_string[(*it)->safety_state()]);
[email protected]ff4c1d82010-08-04 16:58:122151 dl_item_value->SetInteger("PercentComplete", (*it)->PercentComplete());
[email protected]d4adc292010-04-15 18:06:392152 list_of_downloads->Append(dl_item_value);
2153 }
[email protected]ff4c1d82010-08-04 16:58:122154 return_value->Set("downloads", list_of_downloads);
[email protected]d4adc292010-04-15 18:06:392155
[email protected]7c983cc2010-07-16 11:33:342156 reply.SendSuccess(return_value.get());
[email protected]d4adc292010-04-15 18:06:392157 // All value objects allocated above are owned by |return_value|
2158 // and get freed by it.
2159}
2160
[email protected]59a611242010-04-02 02:24:042161void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:582162 Browser* browser,
[email protected]59a611242010-04-02 02:24:042163 DictionaryValue* args,
2164 IPC::Message* reply_message) {
[email protected]7c983cc2010-07-16 11:33:342165 AutomationJSONReply reply(this, reply_message);
[email protected]59a611242010-04-02 02:24:042166
2167 // Look for a quick return.
2168 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:342169 reply.SendSuccess(NULL); // No download manager.
2170 return;
[email protected]59a611242010-04-02 02:24:042171 }
[email protected]82f37b02010-07-29 22:04:572172 std::vector<DownloadItem*> downloads;
2173 profile_->GetDownloadManager()->GetCurrentDownloads(FilePath(), &downloads);
2174 if (downloads.empty()) {
[email protected]7c983cc2010-07-16 11:33:342175 reply.SendSuccess(NULL);
[email protected]f6ff0df2010-07-11 22:41:432176 return;
2177 }
[email protected]59a611242010-04-02 02:24:042178
2179 // The observer owns itself. When the last observed item pings, it
2180 // deletes itself.
2181 AutomationProviderDownloadItemObserver* item_observer =
2182 new AutomationProviderDownloadItemObserver(
2183 this, reply_message, downloads.size());
2184 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
2185 i != downloads.end();
2186 i++) {
2187 (*i)->AddObserver(item_observer);
2188 }
2189}
2190
[email protected]24e2b102010-04-29 17:56:472191// Sample json input: { "command": "GetPrefsInfo" }
2192// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582193void AutomationProvider::GetPrefsInfo(Browser* browser,
2194 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472195 IPC::Message* reply_message) {
[email protected]24e2b102010-04-29 17:56:472196 const PrefService::PreferenceSet& prefs =
2197 profile_->GetPrefs()->preference_set();
2198 DictionaryValue* items = new DictionaryValue;
2199 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
2200 it != prefs.end(); ++it) {
2201 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
2202 }
2203 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]ff4c1d82010-08-04 16:58:122204 return_value->Set("prefs", items); // return_value owns items.
[email protected]7c983cc2010-07-16 11:33:342205 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]24e2b102010-04-29 17:56:472206}
2207
2208// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:582209void AutomationProvider::SetPrefs(Browser* browser,
2210 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472211 IPC::Message* reply_message) {
[email protected]57ecc4b2010-08-11 03:02:512212 std::string path;
[email protected]24e2b102010-04-29 17:56:472213 Value* val;
[email protected]7c983cc2010-07-16 11:33:342214 AutomationJSONReply reply(this, reply_message);
[email protected]57ecc4b2010-08-11 03:02:512215 if (args->GetString("path", &path) && args->Get("value", &val)) {
[email protected]24e2b102010-04-29 17:56:472216 PrefService* pref_service = profile_->GetPrefs();
2217 const PrefService::Preference* pref =
2218 pref_service->FindPreference(path.c_str());
2219 if (!pref) { // Not a registered pref.
[email protected]7c983cc2010-07-16 11:33:342220 reply.SendError("pref not registered.");
2221 return;
[email protected]24e2b102010-04-29 17:56:472222 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
[email protected]7c983cc2010-07-16 11:33:342223 reply.SendError("pref is managed. cannot be changed.");
2224 return;
[email protected]24e2b102010-04-29 17:56:472225 } else { // Set the pref.
2226 pref_service->Set(path.c_str(), *val);
2227 }
2228 } else {
[email protected]7c983cc2010-07-16 11:33:342229 reply.SendError("no pref path or value given.");
2230 return;
[email protected]24e2b102010-04-29 17:56:472231 }
2232
[email protected]7c983cc2010-07-16 11:33:342233 reply.SendSuccess(NULL);
[email protected]24e2b102010-04-29 17:56:472234}
2235
[email protected]53329582010-05-14 21:10:582236// Sample json input: { "command": "GetOmniboxInfo" }
2237// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
2238void AutomationProvider::GetOmniboxInfo(Browser* browser,
2239 DictionaryValue* args,
2240 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582241 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2242
2243 LocationBar* loc_bar = browser->window()->GetLocationBar();
2244 AutocompleteEditView* edit_view = loc_bar->location_entry();
2245 AutocompleteEditModel* model = edit_view->model();
2246
2247 // Fill up matches.
2248 ListValue* matches = new ListValue;
2249 const AutocompleteResult& result = model->result();
2250 for (AutocompleteResult::const_iterator i = result.begin();
2251 i != result.end(); ++i) {
2252 const AutocompleteMatch& match = *i;
2253 DictionaryValue* item = new DictionaryValue; // owned by return_value
[email protected]ff4c1d82010-08-04 16:58:122254 item->SetString("type", AutocompleteMatch::TypeToString(match.type));
2255 item->SetBoolean("starred", match.starred);
2256 item->SetString("destination_url", match.destination_url.spec());
2257 item->SetString("contents", WideToUTF16Hack(match.contents));
2258 item->SetString("description", WideToUTF16Hack(match.description));
[email protected]53329582010-05-14 21:10:582259 matches->Append(item);
2260 }
[email protected]ff4c1d82010-08-04 16:58:122261 return_value->Set("matches", matches);
[email protected]53329582010-05-14 21:10:582262
2263 // Fill up other properties.
2264 DictionaryValue* properties = new DictionaryValue; // owned by return_value
[email protected]ff4c1d82010-08-04 16:58:122265 properties->SetBoolean("has_focus", model->has_focus());
2266 properties->SetBoolean("query_in_progress", model->query_in_progress());
2267 properties->SetString("keyword", WideToUTF16Hack(model->keyword()));
2268 properties->SetString("text", WideToUTF16Hack(edit_view->GetText()));
2269 return_value->Set("properties", properties);
[email protected]53329582010-05-14 21:10:582270
[email protected]7c983cc2010-07-16 11:33:342271 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]53329582010-05-14 21:10:582272}
2273
2274// Sample json input: { "command": "SetOmniboxText",
2275// "text": "goog" }
2276void AutomationProvider::SetOmniboxText(Browser* browser,
2277 DictionaryValue* args,
2278 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582279 std::wstring text;
[email protected]7c983cc2010-07-16 11:33:342280 AutomationJSONReply reply(this, reply_message);
[email protected]53329582010-05-14 21:10:582281 if (!args->GetString(L"text", &text)) {
[email protected]7c983cc2010-07-16 11:33:342282 reply.SendError("text missing");
2283 return;
[email protected]53329582010-05-14 21:10:582284 }
[email protected]7c983cc2010-07-16 11:33:342285 browser->FocusLocationBar();
2286 LocationBar* loc_bar = browser->window()->GetLocationBar();
2287 AutocompleteEditView* edit_view = loc_bar->location_entry();
2288 edit_view->model()->OnSetFocus(false);
2289 edit_view->SetUserText(text);
2290 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582291}
2292
2293// Sample json input: { "command": "OmniboxMovePopupSelection",
2294// "count": 1 }
2295// Negative count implies up, positive implies down. Count values will be
2296// capped by the size of the popup list.
2297void AutomationProvider::OmniboxMovePopupSelection(
2298 Browser* browser,
2299 DictionaryValue* args,
2300 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582301 int count;
[email protected]7c983cc2010-07-16 11:33:342302 AutomationJSONReply reply(this, reply_message);
[email protected]ff4c1d82010-08-04 16:58:122303 if (!args->GetInteger("count", &count)) {
[email protected]7c983cc2010-07-16 11:33:342304 reply.SendError("count missing");
2305 return;
[email protected]53329582010-05-14 21:10:582306 }
[email protected]7c983cc2010-07-16 11:33:342307 LocationBar* loc_bar = browser->window()->GetLocationBar();
2308 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2309 model->OnUpOrDownKeyPressed(count);
2310 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582311}
2312
2313// Sample json input: { "command": "OmniboxAcceptInput" }
2314void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2315 DictionaryValue* args,
2316 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282317 NavigationController& controller =
2318 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122319 // Setup observer to wait until the selected item loads.
2320 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282321 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122322 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582323
2324 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582325}
2326
[email protected]a3cd5022010-06-16 18:25:292327// Sample json input: { "command": "GetInitialLoadTimes" }
2328// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2329void AutomationProvider::GetInitialLoadTimes(
2330 Browser*,
2331 DictionaryValue*,
2332 IPC::Message* reply_message) {
2333 scoped_ptr<DictionaryValue> return_value(
2334 initial_load_observer_->GetTimingInformation());
[email protected]f6ff0df2010-07-11 22:41:432335
2336 std::string json_return;
2337 base::JSONWriter::Write(return_value.get(), false, &json_return);
2338 AutomationMsg_SendJSONRequest::WriteReplyParams(
2339 reply_message, json_return, true);
2340 Send(reply_message);
[email protected]a3cd5022010-06-16 18:25:292341}
2342
[email protected]f7d48012010-05-06 08:17:052343// Sample json input: { "command": "GetPluginsInfo" }
2344// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582345void AutomationProvider::GetPluginsInfo(Browser* browser,
2346 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052347 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052348 std::vector<WebPluginInfo> plugins;
2349 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2350 ListValue* items = new ListValue;
2351 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2352 it != plugins.end();
2353 ++it) {
2354 DictionaryValue* item = new DictionaryValue;
[email protected]ff4c1d82010-08-04 16:58:122355 item->SetString("name", it->name);
2356 item->SetString("path", it->path.value());
2357 item->SetString("version", it->version);
2358 item->SetString("desc", it->desc);
2359 item->SetBoolean("enabled", it->enabled);
[email protected]f7d48012010-05-06 08:17:052360 // Add info about mime types.
2361 ListValue* mime_types = new ListValue();
2362 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2363 it->mime_types.begin();
2364 type_it != it->mime_types.end();
2365 ++type_it) {
2366 DictionaryValue* mime_type = new DictionaryValue();
[email protected]ff4c1d82010-08-04 16:58:122367 mime_type->SetString("mimeType", type_it->mime_type);
2368 mime_type->SetString("description", type_it->description);
[email protected]f7d48012010-05-06 08:17:052369
2370 ListValue* file_extensions = new ListValue();
2371 for (std::vector<std::string>::const_iterator ext_it =
2372 type_it->file_extensions.begin();
2373 ext_it != type_it->file_extensions.end();
2374 ++ext_it) {
2375 file_extensions->Append(new StringValue(*ext_it));
2376 }
[email protected]ff4c1d82010-08-04 16:58:122377 mime_type->Set("fileExtensions", file_extensions);
[email protected]f7d48012010-05-06 08:17:052378
2379 mime_types->Append(mime_type);
2380 }
[email protected]ff4c1d82010-08-04 16:58:122381 item->Set("mimeTypes", mime_types);
[email protected]f7d48012010-05-06 08:17:052382 items->Append(item);
2383 }
2384 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]ff4c1d82010-08-04 16:58:122385 return_value->Set("plugins", items); // return_value owns items.
[email protected]f7d48012010-05-06 08:17:052386
[email protected]7c983cc2010-07-16 11:33:342387 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]f7d48012010-05-06 08:17:052388}
2389
2390// Sample json input:
2391// { "command": "EnablePlugin",
2392// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582393void AutomationProvider::EnablePlugin(Browser* browser,
2394 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052395 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052396 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342397 AutomationJSONReply reply(this, reply_message);
[email protected]ff4c1d82010-08-04 16:58:122398 if (!args->GetString("path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342399 reply.SendError("path not specified.");
2400 return;
[email protected]f6ff0df2010-07-11 22:41:432401 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342402 reply.SendError(StringPrintf("Could not enable plugin for path %s.",
2403 path.c_str()));
2404 return;
[email protected]f6ff0df2010-07-11 22:41:432405 }
[email protected]7c983cc2010-07-16 11:33:342406 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052407}
2408
2409// Sample json input:
2410// { "command": "DisablePlugin",
2411// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582412void AutomationProvider::DisablePlugin(Browser* browser,
2413 DictionaryValue* args,
2414 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052415 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342416 AutomationJSONReply reply(this, reply_message);
[email protected]ff4c1d82010-08-04 16:58:122417 if (!args->GetString("path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342418 reply.SendError("path not specified.");
2419 return;
[email protected]f6ff0df2010-07-11 22:41:432420 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342421 reply.SendError(StringPrintf("Could not disable plugin for path %s.",
2422 path.c_str()));
2423 return;
[email protected]f6ff0df2010-07-11 22:41:432424 }
[email protected]7c983cc2010-07-16 11:33:342425 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052426}
2427
[email protected]7060bb292010-06-24 00:52:492428// Sample json input:
2429// { "command": "SaveTabContents",
2430// "tab_index": 0,
2431// "filename": <a full pathname> }
2432// Sample json output:
2433// {}
2434void AutomationProvider::SaveTabContents(Browser* browser,
2435 DictionaryValue* args,
2436 IPC::Message* reply_message) {
[email protected]7060bb292010-06-24 00:52:492437 int tab_index = 0;
2438 FilePath::StringType filename;
2439 FilePath::StringType parent_directory;
2440 TabContents* tab_contents = NULL;
2441
[email protected]ff4c1d82010-08-04 16:58:122442 if (!args->GetInteger("tab_index", &tab_index) ||
2443 !args->GetString("filename", &filename)) {
[email protected]26adfd312010-07-20 01:04:462444 AutomationJSONReply(this, reply_message).SendError(
2445 "tab_index or filename param missing");
[email protected]7c983cc2010-07-16 11:33:342446 return;
[email protected]7060bb292010-06-24 00:52:492447 } else {
2448 tab_contents = browser->GetTabContentsAt(tab_index);
2449 if (!tab_contents) {
[email protected]26adfd312010-07-20 01:04:462450 AutomationJSONReply(this, reply_message).SendError(
2451 "no tab at tab_index");
[email protected]7060bb292010-06-24 00:52:492452 return;
2453 }
2454 }
[email protected]7c983cc2010-07-16 11:33:342455 // We're doing a SAVE_AS_ONLY_HTML so the the directory path isn't
2456 // used. Nevertheless, SavePackage requires it be valid. Sigh.
2457 parent_directory = FilePath(filename).DirName().value();
2458 if (!tab_contents->SavePage(FilePath(filename), FilePath(parent_directory),
2459 SavePackage::SAVE_AS_ONLY_HTML)) {
[email protected]26adfd312010-07-20 01:04:462460 AutomationJSONReply(this, reply_message).SendError(
2461 "Could not initiate SavePage");
[email protected]7c983cc2010-07-16 11:33:342462 return;
2463 }
2464 // The observer will delete itself when done.
2465 new SavePackageNotificationObserver(tab_contents->save_package(),
2466 this, reply_message);
[email protected]7060bb292010-06-24 00:52:492467}
2468
[email protected]c5aa5322010-07-15 19:00:072469// Refer to ImportSettings() in chrome/test/pyautolib/pyauto.py for sample
2470// json input.
2471// Sample json output: "{}"
2472void AutomationProvider::ImportSettings(Browser* browser,
2473 DictionaryValue* args,
2474 IPC::Message* reply_message) {
[email protected]c5aa5322010-07-15 19:00:072475 // Map from the json string passed over to the import item masks.
2476 std::map<std::string, ImportItem> string_to_import_item;
2477 string_to_import_item["HISTORY"] = importer::HISTORY;
2478 string_to_import_item["FAVORITES"] = importer::FAVORITES;
2479 string_to_import_item["COOKIES"] = importer::COOKIES;
2480 string_to_import_item["PASSWORDS"] = importer::PASSWORDS;
2481 string_to_import_item["SEARCH_ENGINES"] = importer::SEARCH_ENGINES;
2482 string_to_import_item["HOME_PAGE"] = importer::HOME_PAGE;
2483 string_to_import_item["ALL"] = importer::ALL;
2484
2485 std::wstring browser_name;
2486 int import_items = 0;
2487 ListValue* import_items_list = NULL;
2488 bool first_run;
2489
2490 if (!args->GetString(L"import_from", &browser_name) ||
[email protected]ff4c1d82010-08-04 16:58:122491 !args->GetBoolean("first_run", &first_run) ||
2492 !args->GetList("import_items", &import_items_list)) {
[email protected]7c983cc2010-07-16 11:33:342493 AutomationJSONReply(this, reply_message).SendError(
2494 "Incorrect type for one or more of the arguments.");
[email protected]c5aa5322010-07-15 19:00:072495 return;
2496 }
2497
2498 int num_items = import_items_list->GetSize();
2499 for (int i = 0; i < num_items; i++) {
2500 std::string item;
2501 import_items_list->GetString(i, &item);
2502 // If the provided string is not part of the map, error out.
2503 if (!ContainsKey(string_to_import_item, item)) {
[email protected]7c983cc2010-07-16 11:33:342504 AutomationJSONReply(this, reply_message).SendError(
2505 "Invalid item string found in import_items.");
[email protected]c5aa5322010-07-15 19:00:072506 return;
2507 }
2508 import_items |= string_to_import_item[item];
2509 }
2510
2511 ImporterHost* importer_host = new ImporterHost();
2512 // Get the correct ProfileInfo based on the browser they user provided.
2513 importer::ProfileInfo profile_info;
2514 int num_browsers = importer_host->GetAvailableProfileCount();
2515 int i = 0;
2516 for ( ; i < num_browsers; i++) {
2517 std::wstring name = importer_host->GetSourceProfileNameAt(i);
2518 if (name == browser_name) {
2519 profile_info = importer_host->GetSourceProfileInfoAt(i);
2520 break;
2521 }
2522 }
2523 // If we made it to the end of the loop, then the input was bad.
2524 if (i == num_browsers) {
[email protected]7c983cc2010-07-16 11:33:342525 AutomationJSONReply(this, reply_message).SendError(
2526 "Invalid browser name string found.");
[email protected]c5aa5322010-07-15 19:00:072527 return;
2528 }
2529
2530 Profile* profile = browser->profile();
2531
2532 importer_host->SetObserver(
2533 new AutomationProviderImportSettingsObserver(this, reply_message));
2534 importer_host->StartImportSettings(profile_info, profile, import_items,
2535 new ProfileWriter(profile), first_run);
2536}
2537
[email protected]95222282010-07-26 17:43:022538namespace {
2539
2540// Translates a dictionary password to a PasswordForm struct.
2541webkit_glue::PasswordForm GetPasswordFormFromDict(
2542 const DictionaryValue& password_dict) {
2543
2544 // If the time is specified, change time to the specified time.
2545 base::Time time = base::Time::Now();
2546 int it;
2547 double dt;
[email protected]ff4c1d82010-08-04 16:58:122548 if (password_dict.GetInteger("time", &it))
[email protected]95222282010-07-26 17:43:022549 time = base::Time::FromTimeT(it);
[email protected]ff4c1d82010-08-04 16:58:122550 else if (password_dict.GetReal("time", &dt))
[email protected]95222282010-07-26 17:43:022551 time = base::Time::FromDoubleT(dt);
2552
2553 std::string signon_realm;
2554 string16 username_value;
2555 string16 password_value;
2556 string16 origin_url_text;
2557 string16 username_element;
2558 string16 password_element;
2559 string16 submit_element;
2560 string16 action_target_text;
2561 bool blacklist = false;
2562 string16 old_password_element;
2563 string16 old_password_value;
2564
2565 // We don't care if any of these fail - they are either optional or checked
2566 // before this function is called.
[email protected]ff4c1d82010-08-04 16:58:122567 password_dict.GetString("signon_realm", &signon_realm);
[email protected]698f7f42010-08-04 19:35:332568 password_dict.GetString("username_value", &username_value);
2569 password_dict.GetString("password_value", &password_value);
2570 password_dict.GetString("origin_url", &origin_url_text);
2571 password_dict.GetString("username_element", &username_element);
2572 password_dict.GetString("password_element", &password_element);
2573 password_dict.GetString("submit_element", &submit_element);
2574 password_dict.GetString("action_target", &action_target_text);
[email protected]ff4c1d82010-08-04 16:58:122575 password_dict.GetBoolean("blacklist", &blacklist);
[email protected]95222282010-07-26 17:43:022576
2577 GURL origin_gurl(origin_url_text);
2578 GURL action_target(action_target_text);
2579
2580 webkit_glue::PasswordForm password_form;
2581 password_form.signon_realm = signon_realm;
2582 password_form.username_value = username_value;
2583 password_form.password_value = password_value;
2584 password_form.origin = origin_gurl;
2585 password_form.username_element = username_element;
2586 password_form.password_element = password_element;
2587 password_form.submit_element = submit_element;
2588 password_form.action = action_target;
2589 password_form.blacklisted_by_user = blacklist;
2590 password_form.date_created = time;
2591
2592 return password_form;
2593}
2594
2595} // namespace
2596
[email protected]5bcfe1672010-07-16 20:51:572597// See AddSavedPassword() in chrome/test/functional/pyauto.py for sample json
2598// input.
2599// Sample json output: { "password_added": true }
2600void AutomationProvider::AddSavedPassword(Browser* browser,
2601 DictionaryValue* args,
2602 IPC::Message* reply_message) {
[email protected]5bcfe1672010-07-16 20:51:572603 AutomationJSONReply reply(this, reply_message);
[email protected]95222282010-07-26 17:43:022604 DictionaryValue* password_dict = NULL;
[email protected]5bcfe1672010-07-16 20:51:572605
[email protected]ff4c1d82010-08-04 16:58:122606 if (!args->GetDictionary("password", &password_dict)) {
[email protected]95222282010-07-26 17:43:022607 reply.SendError("Password must be a dictionary.");
[email protected]5bcfe1672010-07-16 20:51:572608 return;
2609 }
2610
[email protected]95222282010-07-26 17:43:022611 // The signon realm is effectively the primary key and must be included.
2612 // Check here before calling GetPasswordFormFromDict.
[email protected]ff4c1d82010-08-04 16:58:122613 if (!password_dict->HasKey("signon_realm")) {
[email protected]95222282010-07-26 17:43:022614 reply.SendError("Password must include signon_realm.");
2615 return;
2616 }
2617 webkit_glue::PasswordForm new_password =
2618 GetPasswordFormFromDict(*password_dict);
[email protected]5bcfe1672010-07-16 20:51:572619
2620 Profile* profile = browser->profile();
2621 // Use IMPLICIT_ACCESS since new passwords aren't added off the record.
2622 PasswordStore* password_store =
2623 profile->GetPasswordStore(Profile::IMPLICIT_ACCESS);
2624
2625 // Set the return based on whether setting the password succeeded.
2626 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2627
2628 // It will be null if it's accessed in an incognito window.
2629 if (password_store != NULL) {
2630 password_store->AddLogin(new_password);
[email protected]ff4c1d82010-08-04 16:58:122631 return_value->SetBoolean("password_added", true);
[email protected]5bcfe1672010-07-16 20:51:572632 } else {
[email protected]ff4c1d82010-08-04 16:58:122633 return_value->SetBoolean("password_added", false);
[email protected]5bcfe1672010-07-16 20:51:572634 }
2635
2636 reply.SendSuccess(return_value.get());
2637}
2638
[email protected]95222282010-07-26 17:43:022639// See RemoveSavedPassword() in chrome/test/functional/pyauto.py for sample
2640// json input.
2641// Sample json output: {}
2642void AutomationProvider::RemoveSavedPassword(Browser* browser,
2643 DictionaryValue* args,
2644 IPC::Message* reply_message) {
2645 AutomationJSONReply reply(this, reply_message);
2646 DictionaryValue* password_dict = NULL;
2647
[email protected]ff4c1d82010-08-04 16:58:122648 if (!args->GetDictionary("password", &password_dict)) {
[email protected]95222282010-07-26 17:43:022649 reply.SendError("Password must be a dictionary.");
2650 return;
2651 }
2652
2653 // The signon realm is effectively the primary key and must be included.
2654 // Check here before calling GetPasswordFormFromDict.
[email protected]ff4c1d82010-08-04 16:58:122655 if (!password_dict->HasKey("signon_realm")) {
[email protected]95222282010-07-26 17:43:022656 reply.SendError("Password must include signon_realm.");
2657 return;
2658 }
2659 webkit_glue::PasswordForm to_remove =
2660 GetPasswordFormFromDict(*password_dict);
2661
2662 Profile* profile = browser->profile();
2663 // Use EXPLICIT_ACCESS since passwords can be removed off the record.
2664 PasswordStore* password_store =
2665 profile->GetPasswordStore(Profile::EXPLICIT_ACCESS);
2666
2667 password_store->RemoveLogin(to_remove);
2668 reply.SendSuccess(NULL);
2669}
2670
[email protected]5bcfe1672010-07-16 20:51:572671// Sample json input: { "command": "GetSavedPasswords" }
2672// Refer to GetSavedPasswords() in chrome/test/pyautolib/pyauto.py for sample
2673// json output.
2674void AutomationProvider::GetSavedPasswords(Browser* browser,
2675 DictionaryValue* args,
2676 IPC::Message* reply_message) {
2677 Profile* profile = browser->profile();
2678 // Use EXPLICIT_ACCESS since saved passwords can be retreived off the record.
2679 PasswordStore* password_store =
2680 profile->GetPasswordStore(Profile::EXPLICIT_ACCESS);
2681 password_store->GetAutofillableLogins(
2682 new AutomationProviderGetPasswordsObserver(this, reply_message));
2683 // Observer deletes itself after returning.
2684}
2685
[email protected]a0fc50d72010-07-14 21:14:192686// Refer to ClearBrowsingData() in chrome/test/pyautolib/pyauto.py for sample
2687// json input.
2688// Sample json output: {}
2689void AutomationProvider::ClearBrowsingData(Browser* browser,
2690 DictionaryValue* args,
2691 IPC::Message* reply_message) {
[email protected]a0fc50d72010-07-14 21:14:192692 std::map<std::string, BrowsingDataRemover::TimePeriod> string_to_time_period;
2693 string_to_time_period["LAST_HOUR"] = BrowsingDataRemover::LAST_HOUR;
2694 string_to_time_period["LAST_DAY"] = BrowsingDataRemover::LAST_DAY;
2695 string_to_time_period["LAST_WEEK"] = BrowsingDataRemover::LAST_WEEK;
2696 string_to_time_period["FOUR_WEEKS"] = BrowsingDataRemover::FOUR_WEEKS;
2697 string_to_time_period["EVERYTHING"] = BrowsingDataRemover::EVERYTHING;
2698
2699 std::map<std::string, int> string_to_mask_value;
2700 string_to_mask_value["HISTORY"] = BrowsingDataRemover::REMOVE_HISTORY;
2701 string_to_mask_value["DOWNLOADS"] = BrowsingDataRemover::REMOVE_DOWNLOADS;
2702 string_to_mask_value["COOKIES"] = BrowsingDataRemover::REMOVE_COOKIES;
2703 string_to_mask_value["PASSWORDS"] = BrowsingDataRemover::REMOVE_PASSWORDS;
2704 string_to_mask_value["FORM_DATA"] = BrowsingDataRemover::REMOVE_FORM_DATA;
2705 string_to_mask_value["CACHE"] = BrowsingDataRemover::REMOVE_CACHE;
2706
2707 std::string time_period;
2708 ListValue* to_remove;
[email protected]ff4c1d82010-08-04 16:58:122709 if (!args->GetString("time_period", &time_period) ||
2710 !args->GetList("to_remove", &to_remove)) {
[email protected]7c983cc2010-07-16 11:33:342711 AutomationJSONReply(this, reply_message).SendError(
2712 "time_period must be a string and to_remove a list.");
[email protected]a0fc50d72010-07-14 21:14:192713 return;
2714 }
2715
2716 int remove_mask = 0;
2717 int num_removals = to_remove->GetSize();
2718 for (int i = 0; i < num_removals; i++) {
2719 std::string removal;
2720 to_remove->GetString(i, &removal);
2721 // If the provided string is not part of the map, then error out.
2722 if (!ContainsKey(string_to_mask_value, removal)) {
[email protected]7c983cc2010-07-16 11:33:342723 AutomationJSONReply(this, reply_message).SendError(
2724 "Invalid browsing data string found in to_remove.");
[email protected]a0fc50d72010-07-14 21:14:192725 return;
2726 }
2727 remove_mask |= string_to_mask_value[removal];
2728 }
2729
2730 if (!ContainsKey(string_to_time_period, time_period)) {
[email protected]7c983cc2010-07-16 11:33:342731 AutomationJSONReply(this, reply_message).SendError(
2732 "Invalid string for time_period.");
[email protected]a0fc50d72010-07-14 21:14:192733 return;
2734 }
2735
2736 BrowsingDataRemover* remover = new BrowsingDataRemover(
2737 profile(), string_to_time_period[time_period], base::Time());
2738
2739 remover->AddObserver(
2740 new AutomationProviderBrowsingDataObserver(this, reply_message));
2741 remover->Remove(remove_mask);
2742 // BrowsingDataRemover deletes itself using DeleteTask.
2743 // The observer also deletes itself after sending the reply.
2744}
2745
[email protected]3dda5b02010-07-27 16:35:422746namespace {
2747
2748 // Get the TabContents from a dictionary of arguments.
2749 TabContents* GetTabContentsFromDict(const Browser* browser,
2750 const DictionaryValue* args,
2751 std::string* error_message) {
2752 int tab_index;
[email protected]ff4c1d82010-08-04 16:58:122753 if (!args->GetInteger("tab_index", &tab_index)) {
[email protected]3dda5b02010-07-27 16:35:422754 *error_message = "Must include tab_index.";
2755 return NULL;
2756 }
2757
2758 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2759 if (!tab_contents) {
2760 *error_message = StringPrintf("No tab at index %d.", tab_index);
2761 return NULL;
2762 }
2763 return tab_contents;
2764 }
2765
2766 // Get the TranslateInfoBarDelegate from TabContents.
2767 TranslateInfoBarDelegate* GetTranslateInfoBarDelegate(
2768 TabContents* tab_contents) {
2769 for (int i = 0; i < tab_contents->infobar_delegate_count(); i++) {
2770 InfoBarDelegate* infobar = tab_contents->GetInfoBarDelegateAt(i);
2771 if (infobar->AsTranslateInfoBarDelegate())
2772 return infobar->AsTranslateInfoBarDelegate();
2773 }
2774 // No translate infobar.
2775 return NULL;
2776 }
2777
2778} // namespace
2779
2780// See GetTranslateInfo() in chrome/test/pyautolib/pyauto.py for sample json
2781// input and output.
2782void AutomationProvider::GetTranslateInfo(Browser* browser,
2783 DictionaryValue* args,
2784 IPC::Message* reply_message) {
2785 std::string error_message;
2786 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2787 &error_message);
2788 if (!tab_contents) {
2789 AutomationJSONReply(this, reply_message).SendError(error_message);
2790 return;
2791 }
2792
2793 // Get the translate bar if there is one and pass it to the observer.
2794 // The observer will check for null and populate the information accordingly.
2795 TranslateInfoBarDelegate* translate_bar =
2796 GetTranslateInfoBarDelegate(tab_contents);
2797
2798 TabLanguageDeterminedObserver* observer = new TabLanguageDeterminedObserver(
2799 this, reply_message, tab_contents, translate_bar);
2800 // If the language for the page hasn't been loaded yet, then just make
2801 // the observer, otherwise call observe directly.
2802 std::string language = tab_contents->language_state().original_language();
2803 if (!language.empty()) {
2804 observer->Observe(NotificationType::TAB_LANGUAGE_DETERMINED,
2805 Source<TabContents>(tab_contents),
2806 Details<std::string>(&language));
2807 }
2808}
2809
2810// See SelectTranslateOption() in chrome/test/pyautolib/pyauto.py for sample
2811// json input.
2812// Sample json output: {}
2813void AutomationProvider::SelectTranslateOption(Browser* browser,
2814 DictionaryValue* args,
2815 IPC::Message* reply_message) {
2816 std::string option;
2817 std::string error_message;
2818 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2819 &error_message);
2820 if (!tab_contents) {
2821 AutomationJSONReply(this, reply_message).SendError(error_message);
2822 return;
2823 }
2824
2825 TranslateInfoBarDelegate* translate_bar =
2826 GetTranslateInfoBarDelegate(tab_contents);
2827 if (!translate_bar) {
2828 AutomationJSONReply(this, reply_message)
2829 .SendError("There is no translate bar open.");
2830 return;
2831 }
2832
[email protected]ff4c1d82010-08-04 16:58:122833 if (!args->GetString("option", &option)) {
[email protected]3dda5b02010-07-27 16:35:422834 AutomationJSONReply(this, reply_message).SendError("Must include option");
2835 return;
2836 }
2837
2838 if (option == "translate_page") {
2839 // Make a new notification observer which will send the reply.
2840 new PageTranslatedObserver(this, reply_message, tab_contents);
2841 translate_bar->Translate();
2842 return;
[email protected]da1913e2010-08-04 23:50:152843 } else if (option == "set_target_language") {
2844 string16 target_language;
2845 if (!args->GetString("target_language", &target_language)) {
2846 AutomationJSONReply(this, reply_message).
2847 SendError("Must include target_language string.");
2848 return;
2849 }
2850 // Get the target language index based off of the language name.
2851 int target_language_index = -1;
2852 for (int i = 0; i < translate_bar->GetLanguageCount(); i++) {
2853 if (translate_bar->GetLanguageDisplayableNameAt(i) == target_language) {
2854 target_language_index = i;
2855 break;
2856 }
2857 }
2858 if (target_language_index == -1) {
2859 AutomationJSONReply(this, reply_message)
2860 .SendError("Invalid target language string.");
2861 return;
2862 }
2863 // If the page has already been translated it will be translated again to
2864 // the new language. The observer will wait until the page has been
2865 // translated to reply.
2866 if (translate_bar->type() == TranslateInfoBarDelegate::AFTER_TRANSLATE) {
2867 new PageTranslatedObserver(this, reply_message, tab_contents);
2868 translate_bar->SetTargetLanguage(target_language_index);
2869 return;
2870 }
2871 // Otherwise just send the reply back immediately.
2872 translate_bar->SetTargetLanguage(target_language_index);
2873 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2874 return_value->SetBoolean("translation_success", true);
2875 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
2876 return;
[email protected]4a47d76122010-08-10 18:11:162877 } else if (option == "click_always_translate_lang_button") {
2878 if (!translate_bar->ShouldShowAlwaysTranslateButton()) {
2879 AutomationJSONReply(this, reply_message)
2880 .SendError("Always translate button not showing.");
2881 return;
2882 }
2883 // Clicking 'Always Translate' triggers a translation. The observer will
2884 // wait until the translation is complete before sending the reply.
2885 new PageTranslatedObserver(this, reply_message, tab_contents);
2886 translate_bar->AlwaysTranslatePageLanguage();
2887 return;
[email protected]3dda5b02010-07-27 16:35:422888 }
2889
2890 AutomationJSONReply reply(this, reply_message);
2891 if (option == "never_translate_language") {
2892 if (translate_bar->IsLanguageBlacklisted()) {
2893 reply.SendError("The language was already blacklisted.");
2894 return;
2895 }
2896 translate_bar->ToggleLanguageBlacklist();
2897 reply.SendSuccess(NULL);
2898 } else if (option == "never_translate_site") {
2899 if (translate_bar->IsSiteBlacklisted()) {
2900 reply.SendError("The site was already blacklisted.");
2901 return;
2902 }
2903 translate_bar->ToggleSiteBlacklist();
2904 reply.SendSuccess(NULL);
2905 } else if (option == "toggle_always_translate") {
2906 translate_bar->ToggleAlwaysTranslate();
2907 reply.SendSuccess(NULL);
2908 } else if (option == "revert_translation") {
2909 translate_bar->RevertTranslation();
2910 reply.SendSuccess(NULL);
[email protected]4a47d76122010-08-10 18:11:162911 } else if (option == "click_never_translate_lang_button") {
2912 if (!translate_bar->ShouldShowNeverTranslateButton()) {
2913 reply.SendError("Always translate button not showing.");
2914 return;
2915 }
2916 translate_bar->NeverTranslatePageLanguage();
2917 reply.SendSuccess(NULL);
2918 } else if (option == "decline_translation") {
2919 // This is the function called when an infobar is dismissed or when the
2920 // user clicks the 'Nope' translate button.
2921 translate_bar->TranslationDeclined();
2922 reply.SendSuccess(NULL);
[email protected]3dda5b02010-07-27 16:35:422923 } else {
2924 reply.SendError("Invalid string found for option.");
2925 }
2926}
2927
[email protected]f89ee5d2010-08-02 16:39:122928// See WaitUntilTranslateComplete() in chrome/test/pyautolib/pyauto.py for
2929// sample json input and output.
2930void AutomationProvider::WaitUntilTranslateComplete(
2931 Browser* browser, DictionaryValue* args, IPC::Message* reply_message) {
2932 std::string error_message;
2933 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2934 &error_message);
2935 if (!tab_contents) {
2936 AutomationJSONReply(this, reply_message).SendError(error_message);
2937 return;
2938 }
2939
[email protected]f89ee5d2010-08-02 16:39:122940 TranslateInfoBarDelegate* translate_bar =
2941 GetTranslateInfoBarDelegate(tab_contents);
2942 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]14a0aac2010-08-05 15:52:592943
[email protected]f89ee5d2010-08-02 16:39:122944 if (!translate_bar) {
[email protected]57ecc4b2010-08-11 03:02:512945 return_value->SetBoolean("translation_success", false);
[email protected]14a0aac2010-08-05 15:52:592946 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
2947 return;
[email protected]f89ee5d2010-08-02 16:39:122948 }
[email protected]14a0aac2010-08-05 15:52:592949
2950 // If the translation is still pending, the observer will wait
2951 // for it to finish and then reply.
2952 if (translate_bar->type() == TranslateInfoBarDelegate::TRANSLATING) {
2953 new PageTranslatedObserver(this, reply_message, tab_contents);
2954 return;
2955 }
2956
2957 // Otherwise send back the success or failure of the attempted translation.
2958 return_value->SetBoolean(
[email protected]57ecc4b2010-08-11 03:02:512959 "translation_success",
[email protected]14a0aac2010-08-05 15:52:592960 translate_bar->type() == TranslateInfoBarDelegate::AFTER_TRANSLATE);
2961 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]f89ee5d2010-08-02 16:39:122962}
2963
[email protected]4d1929f12010-07-10 00:09:032964// Sample json input: { "command": "GetThemeInfo" }
2965// Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output.
2966void AutomationProvider::GetThemeInfo(Browser* browser,
2967 DictionaryValue* args,
2968 IPC::Message* reply_message) {
2969 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2970 Extension* theme = browser->profile()->GetTheme();
2971 if (theme) {
[email protected]ff4c1d82010-08-04 16:58:122972 return_value->SetString("name", theme->name());
2973 return_value->Set("images", theme->GetThemeImages()->DeepCopy());
2974 return_value->Set("colors", theme->GetThemeColors()->DeepCopy());
2975 return_value->Set("tints", theme->GetThemeTints()->DeepCopy());
[email protected]4d1929f12010-07-10 00:09:032976 }
[email protected]7c983cc2010-07-16 11:33:342977 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]4d1929f12010-07-10 00:09:032978}
2979
[email protected]32a69cf2010-08-03 16:27:012980// Sample json input: { "command": "GetExtensionsInfo" }
2981// See GetExtensionsInfo() in chrome/test/pyautolib/pyauto.py for sample json
2982// output.
2983void AutomationProvider::GetExtensionsInfo(Browser* browser,
2984 DictionaryValue* args,
2985 IPC::Message* reply_message) {
2986 AutomationJSONReply reply(this, reply_message);
2987 ExtensionsService* service = profile()->GetExtensionsService();
2988 if (!service) {
2989 reply.SendError("No extensions service.");
2990 }
2991 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2992 ListValue* extensions_values = new ListValue;
2993 const ExtensionList* extensions = service->extensions();
2994 for (ExtensionList::const_iterator it = extensions->begin();
2995 it != extensions->end(); ++it) {
2996 const Extension* extension = *it;
2997 DictionaryValue* extension_value = new DictionaryValue;
[email protected]ff4c1d82010-08-04 16:58:122998 extension_value->SetString("id", extension->id());
2999 extension_value->SetString("version", extension->VersionString());
3000 extension_value->SetString("name", extension->name());
3001 extension_value->SetString("public_key", extension->public_key());
3002 extension_value->SetString("description", extension->description());
3003 extension_value->SetString("background_url",
[email protected]32a69cf2010-08-03 16:27:013004 extension->background_url().spec());
[email protected]ff4c1d82010-08-04 16:58:123005 extension_value->SetString("options_url",
[email protected]32a69cf2010-08-03 16:27:013006 extension->options_url().spec());
3007 extensions_values->Append(extension_value);
3008 }
[email protected]ff4c1d82010-08-04 16:58:123009 return_value->Set("extensions", extensions_values);
[email protected]32a69cf2010-08-03 16:27:013010 reply.SendSuccess(return_value.get());
3011}
3012
3013// See UninstallExtensionById() in chrome/test/pyautolib/pyauto.py for sample
3014// json input.
3015// Sample json output: {}
3016void AutomationProvider::UninstallExtensionById(Browser* browser,
3017 DictionaryValue* args,
3018 IPC::Message* reply_message) {
3019 AutomationJSONReply reply(this, reply_message);
3020 std::string id;
[email protected]ff4c1d82010-08-04 16:58:123021 if (!args->GetString("id", &id)) {
[email protected]32a69cf2010-08-03 16:27:013022 reply.SendError("Must include string id.");
3023 return;
3024 }
3025 ExtensionsService* service = profile()->GetExtensionsService();
3026 if (!service) {
3027 reply.SendError("No extensions service.");
3028 return;
3029 }
3030 ExtensionUnloadNotificationObserver observer;
3031 service->UninstallExtension(id, false);
3032 reply.SendSuccess(NULL);
3033}
3034
[email protected]55846ad842010-07-09 18:22:563035// Sample json input:
3036// { "command": "GetAutoFillProfile" }
3037// Refer to GetAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
3038// json output.
3039void AutomationProvider::GetAutoFillProfile(Browser* browser,
3040 DictionaryValue* args,
3041 IPC::Message* reply_message) {
[email protected]55846ad842010-07-09 18:22:563042 // Get the AutoFillProfiles currently in the database.
3043 int tab_index = 0;
[email protected]ff4c1d82010-08-04 16:58:123044 args->GetInteger("tab_index", &tab_index);
[email protected]55846ad842010-07-09 18:22:563045 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
[email protected]7c983cc2010-07-16 11:33:343046 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:563047
3048 if (tab_contents) {
3049 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
3050 ->GetPersonalDataManager();
3051 if (pdm) {
3052 std::vector<AutoFillProfile*> autofill_profiles = pdm->profiles();
3053 std::vector<CreditCard*> credit_cards = pdm->credit_cards();
3054
3055 ListValue* profiles = GetListFromAutoFillProfiles(autofill_profiles);
3056 ListValue* cards = GetListFromCreditCards(credit_cards);
3057
3058 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
3059
[email protected]ff4c1d82010-08-04 16:58:123060 return_value->Set("profiles", profiles);
3061 return_value->Set("credit_cards", cards);
[email protected]7c983cc2010-07-16 11:33:343062 reply.SendSuccess(return_value.get());
[email protected]55846ad842010-07-09 18:22:563063 } else {
[email protected]7c983cc2010-07-16 11:33:343064 reply.SendError("No PersonalDataManager.");
3065 return;
[email protected]55846ad842010-07-09 18:22:563066 }
3067 } else {
[email protected]7c983cc2010-07-16 11:33:343068 reply.SendError("No tab at that index.");
3069 return;
[email protected]55846ad842010-07-09 18:22:563070 }
[email protected]55846ad842010-07-09 18:22:563071}
3072
3073// Refer to FillAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
3074// json input.
3075// Sample json output: {}
3076void AutomationProvider::FillAutoFillProfile(Browser* browser,
3077 DictionaryValue* args,
3078 IPC::Message* reply_message) {
[email protected]7c983cc2010-07-16 11:33:343079 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:563080 ListValue* profiles = NULL;
3081 ListValue* cards = NULL;
[email protected]ff4c1d82010-08-04 16:58:123082 args->GetList("profiles", &profiles);
3083 args->GetList("credit_cards", &cards);
[email protected]7c983cc2010-07-16 11:33:343084 std::string error_mesg;
[email protected]55846ad842010-07-09 18:22:563085
3086 std::vector<AutoFillProfile> autofill_profiles;
3087 std::vector<CreditCard> credit_cards;
3088 // Create an AutoFillProfile for each of the dictionary profiles.
3089 if (profiles) {
[email protected]7c983cc2010-07-16 11:33:343090 autofill_profiles = GetAutoFillProfilesFromList(*profiles, &error_mesg);
[email protected]55846ad842010-07-09 18:22:563091 }
3092 // Create a CreditCard for each of the dictionary values.
3093 if (cards) {
[email protected]7c983cc2010-07-16 11:33:343094 credit_cards = GetCreditCardsFromList(*cards, &error_mesg);
3095 }
3096 if (!error_mesg.empty()) {
3097 reply.SendError(error_mesg);
3098 return;
[email protected]55846ad842010-07-09 18:22:563099 }
3100
3101 // Save the AutoFillProfiles.
3102 int tab_index = 0;
[email protected]ff4c1d82010-08-04 16:58:123103 args->GetInteger("tab_index", &tab_index);
[email protected]55846ad842010-07-09 18:22:563104 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
3105
3106 if (tab_contents) {
[email protected]ce6b0122010-07-19 19:07:303107 PersonalDataManager* pdm = tab_contents->profile()
[email protected]55846ad842010-07-09 18:22:563108 ->GetPersonalDataManager();
3109 if (pdm) {
3110 pdm->OnAutoFillDialogApply(profiles? &autofill_profiles : NULL,
3111 cards? &credit_cards : NULL);
3112 } else {
[email protected]7c983cc2010-07-16 11:33:343113 reply.SendError("No PersonalDataManager.");
3114 return;
[email protected]55846ad842010-07-09 18:22:563115 }
3116 } else {
[email protected]7c983cc2010-07-16 11:33:343117 reply.SendError("No tab at that index.");
3118 return;
[email protected]55846ad842010-07-09 18:22:563119 }
[email protected]7c983cc2010-07-16 11:33:343120 reply.SendSuccess(NULL);
[email protected]55846ad842010-07-09 18:22:563121}
3122
3123/* static */
3124ListValue* AutomationProvider::GetListFromAutoFillProfiles(
3125 std::vector<AutoFillProfile*> autofill_profiles) {
3126 ListValue* profiles = new ListValue;
3127
3128 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string
3129 = GetAutoFillFieldToStringMap();
3130
3131 // For each AutoFillProfile, transform it to a dictionary object to return.
3132 for (std::vector<AutoFillProfile*>::iterator it = autofill_profiles.begin();
3133 it != autofill_profiles.end(); ++it) {
3134 AutoFillProfile* profile = *it;
3135 DictionaryValue* profile_info = new DictionaryValue;
[email protected]ff4c1d82010-08-04 16:58:123136 profile_info->SetString("label", profile->Label());
[email protected]55846ad842010-07-09 18:22:563137 // For each of the types, if it has a value, add it to the dictionary.
3138 for (std::map<AutoFillFieldType, std::wstring>::iterator
3139 type_it = autofill_type_to_string.begin();
3140 type_it != autofill_type_to_string.end(); ++type_it) {
3141 string16 value = profile->GetFieldText(AutoFillType(type_it->first));
3142 if (value.length()) { // If there was something stored for that value.
[email protected]ff4c1d82010-08-04 16:58:123143 profile_info->SetString(type_it->second, value);
[email protected]55846ad842010-07-09 18:22:563144 }
3145 }
3146 profiles->Append(profile_info);
3147 }
3148 return profiles;
3149}
3150
3151/* static */
3152ListValue* AutomationProvider::GetListFromCreditCards(
3153 std::vector<CreditCard*> credit_cards) {
3154 ListValue* cards = new ListValue;
3155
3156 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
3157 GetCreditCardFieldToStringMap();
3158
3159 // For each AutoFillProfile, transform it to a dictionary object to return.
3160 for (std::vector<CreditCard*>::iterator it = credit_cards.begin();
3161 it != credit_cards.end(); ++it) {
3162 CreditCard* card = *it;
3163 DictionaryValue* card_info = new DictionaryValue;
[email protected]ff4c1d82010-08-04 16:58:123164 card_info->SetString("label", card->Label());
[email protected]55846ad842010-07-09 18:22:563165 // For each of the types, if it has a value, add it to the dictionary.
3166 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
3167 credit_card_type_to_string.begin();
3168 type_it != credit_card_type_to_string.end(); ++type_it) {
3169 string16 value = card->GetFieldText(AutoFillType(type_it->first));
3170 // If there was something stored for that value.
3171 if (value.length()) {
[email protected]ff4c1d82010-08-04 16:58:123172 card_info->SetString(type_it->second, value);
[email protected]55846ad842010-07-09 18:22:563173 }
3174 }
3175 cards->Append(card_info);
3176 }
3177 return cards;
3178}
3179
3180/* static */
3181std::vector<AutoFillProfile> AutomationProvider::GetAutoFillProfilesFromList(
[email protected]7c983cc2010-07-16 11:33:343182 const ListValue& profiles, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:563183 std::vector<AutoFillProfile> autofill_profiles;
3184 DictionaryValue* profile_info = NULL;
3185 string16 profile_label;
3186 string16 current_value;
3187
3188 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string =
3189 GetAutoFillFieldToStringMap();
3190
3191 int num_profiles = profiles.GetSize();
3192 for (int i = 0; i < num_profiles; i++) {
3193 profiles.GetDictionary(i, &profile_info);
3194 profile_info->GetString("label", &profile_label);
3195 // Choose an id of 0 so that a unique id will be created.
3196 AutoFillProfile profile(profile_label, 0);
3197 // Loop through the possible profile types and add those provided.
3198 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
3199 autofill_type_to_string.begin();
3200 type_it != autofill_type_to_string.end(); ++type_it) {
3201 if (profile_info->HasKey(type_it->second)) {
[email protected]698f7f42010-08-04 19:35:333202 if (profile_info->GetString(WideToUTF8(type_it->second),
3203 &current_value)) {
[email protected]55846ad842010-07-09 18:22:563204 profile.SetInfo(AutoFillType(type_it->first), current_value);
3205 } else {
[email protected]7c983cc2010-07-16 11:33:343206 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:563207 break;
3208 }
3209 }
3210 }
3211 autofill_profiles.push_back(profile);
3212 }
3213 return autofill_profiles;
3214}
3215
3216/* static */
3217std::vector<CreditCard> AutomationProvider::GetCreditCardsFromList(
[email protected]7c983cc2010-07-16 11:33:343218 const ListValue& cards, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:563219 std::vector<CreditCard> credit_cards;
3220 DictionaryValue* card_info = NULL;
3221 string16 card_label;
3222 string16 current_value;
3223
3224 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
3225 GetCreditCardFieldToStringMap();
3226
3227 int num_credit_cards = cards.GetSize();
3228 for (int i = 0; i < num_credit_cards; i++) {
3229 cards.GetDictionary(i, &card_info);
3230 card_info->GetString("label", &card_label);
3231 CreditCard card(card_label, 0);
3232 // Loop through the possible credit card fields and add those provided.
3233 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
3234 credit_card_type_to_string.begin();
3235 type_it != credit_card_type_to_string.end(); ++type_it) {
3236 if (card_info->HasKey(type_it->second)) {
[email protected]698f7f42010-08-04 19:35:333237 if (card_info->GetString(WideToUTF8(type_it->second), &current_value)) {
[email protected]55846ad842010-07-09 18:22:563238 card.SetInfo(AutoFillType(type_it->first), current_value);
3239 } else {
[email protected]7c983cc2010-07-16 11:33:343240 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:563241 break;
3242 }
3243 }
3244 }
3245 credit_cards.push_back(card);
3246 }
3247 return credit_cards;
3248}
3249
3250/* static */
3251std::map<AutoFillFieldType, std::wstring>
3252 AutomationProvider::GetAutoFillFieldToStringMap() {
3253 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string;
3254 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
3255 autofill_type_to_string[NAME_MIDDLE] = L"NAME_MIDDLE";
3256 autofill_type_to_string[NAME_LAST] = L"NAME_LAST";
3257 autofill_type_to_string[COMPANY_NAME] = L"COMPANY_NAME";
3258 autofill_type_to_string[EMAIL_ADDRESS] = L"EMAIL_ADDRESS";
3259 autofill_type_to_string[ADDRESS_HOME_LINE1] = L"ADDRESS_HOME_LINE1";
3260 autofill_type_to_string[ADDRESS_HOME_LINE2] = L"ADDRESS_HOME_LINE2";
3261 autofill_type_to_string[ADDRESS_HOME_CITY] = L"ADDRESS_HOME_CITY";
3262 autofill_type_to_string[ADDRESS_HOME_STATE] = L"ADDRESS_HOME_STATE";
3263 autofill_type_to_string[ADDRESS_HOME_ZIP] = L"ADDRESS_HOME_ZIP";
3264 autofill_type_to_string[ADDRESS_HOME_COUNTRY] = L"ADDRESS_HOME_COUNTRY";
3265 autofill_type_to_string[PHONE_HOME_NUMBER] = L"PHONE_HOME_NUMBER";
3266 autofill_type_to_string[PHONE_FAX_NUMBER] = L"PHONE_FAX_NUMBER";
3267 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
3268 return autofill_type_to_string;
3269}
3270
3271/* static */
3272std::map<AutoFillFieldType, std::wstring>
3273 AutomationProvider::GetCreditCardFieldToStringMap() {
3274 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string;
3275 credit_card_type_to_string[CREDIT_CARD_NAME] = L"CREDIT_CARD_NAME";
3276 credit_card_type_to_string[CREDIT_CARD_NUMBER] = L"CREDIT_CARD_NUMBER";
[email protected]55846ad842010-07-09 18:22:563277 credit_card_type_to_string[CREDIT_CARD_EXP_MONTH] = L"CREDIT_CARD_EXP_MONTH";
3278 credit_card_type_to_string[CREDIT_CARD_EXP_4_DIGIT_YEAR] =
3279 L"CREDIT_CARD_EXP_4_DIGIT_YEAR";
3280 return credit_card_type_to_string;
3281}
3282
[email protected]53329582010-05-14 21:10:583283void AutomationProvider::SendJSONRequest(int handle,
3284 std::string json_request,
3285 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:043286 Browser* browser = NULL;
[email protected]59a611242010-04-02 02:24:043287 scoped_ptr<Value> values;
3288
3289 // Basic error checking.
3290 if (browser_tracker_->ContainsHandle(handle)) {
3291 browser = browser_tracker_->GetResource(handle);
3292 }
3293 if (!browser) {
[email protected]7c983cc2010-07-16 11:33:343294 AutomationJSONReply(this, reply_message).SendError("no browser object");
3295 return;
3296 }
3297 base::JSONReader reader;
3298 std::string error;
3299 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
3300 if (!error.empty()) {
3301 AutomationJSONReply(this, reply_message).SendError(error);
3302 return;
[email protected]59a611242010-04-02 02:24:043303 }
3304
3305 // Make sure input is a dict with a string command.
3306 std::string command;
3307 DictionaryValue* dict_value = NULL;
[email protected]7c983cc2010-07-16 11:33:343308 if (values->GetType() != Value::TYPE_DICTIONARY) {
3309 AutomationJSONReply(this, reply_message).SendError("not a dict");
3310 return;
3311 }
3312 // Ownership remains with "values" variable.
3313 dict_value = static_cast<DictionaryValue*>(values.get());
3314 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
3315 AutomationJSONReply(this, reply_message).SendError(
3316 "no command key in dict or not a string command");
3317 return;
[email protected]59a611242010-04-02 02:24:043318 }
3319
[email protected]24e2b102010-04-29 17:56:473320 // Map json commands to their handlers.
3321 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:053322 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
3323 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
3324 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
3325
[email protected]a9ff2c02010-05-13 17:33:053326 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
[email protected]7c983cc2010-07-16 11:33:343327
[email protected]38b5a3852010-07-21 06:49:523328 handler_map["WaitForInfobarCount"] = &AutomationProvider::WaitForInfobarCount;
[email protected]e004a2d2010-07-22 04:55:283329 handler_map["PerformActionOnInfobar"] =
3330 &AutomationProvider::PerformActionOnInfobar;
[email protected]38b5a3852010-07-21 06:49:523331
[email protected]24e2b102010-04-29 17:56:473332 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:293333 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:053334
[email protected]53329582010-05-14 21:10:583335 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
3336 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
3337 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
3338 handler_map["OmniboxMovePopupSelection"] =
3339 &AutomationProvider::OmniboxMovePopupSelection;
3340
[email protected]24e2b102010-04-29 17:56:473341 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
3342 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:053343
[email protected]ef413ca2010-05-25 21:09:143344 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
3345
[email protected]f7d48012010-05-06 08:17:053346 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:473347 handler_map["WaitForAllDownloadsToComplete"] =
3348 &AutomationProvider::WaitForDownloadsToComplete;
3349
[email protected]a3cd5022010-06-16 18:25:293350 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
3351
[email protected]7060bb292010-06-24 00:52:493352 handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents;
3353
[email protected]c5aa5322010-07-15 19:00:073354 handler_map["ImportSettings"] = &AutomationProvider::ImportSettings;
3355
[email protected]5bcfe1672010-07-16 20:51:573356 handler_map["AddSavedPassword"] = &AutomationProvider::AddSavedPassword;
[email protected]95222282010-07-26 17:43:023357 handler_map["RemoveSavedPassword"] =
3358 &AutomationProvider::RemoveSavedPassword;
[email protected]5bcfe1672010-07-16 20:51:573359 handler_map["GetSavedPasswords"] = &AutomationProvider::GetSavedPasswords;
3360
[email protected]a0fc50d72010-07-14 21:14:193361 handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData;
3362
[email protected]4d1929f12010-07-10 00:09:033363 // SetTheme() implemented using InstallExtension().
3364 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo;
3365
[email protected]32a69cf2010-08-03 16:27:013366 // InstallExtension() present in pyauto.py.
3367 handler_map["GetExtensionsInfo"] = &AutomationProvider::GetExtensionsInfo;
3368 handler_map["UninstallExtensionById"] =
3369 &AutomationProvider::UninstallExtensionById;
3370
[email protected]3dda5b02010-07-27 16:35:423371 handler_map["SelectTranslateOption"] =
3372 &AutomationProvider::SelectTranslateOption;
3373 handler_map["GetTranslateInfo"] = &AutomationProvider::GetTranslateInfo;
[email protected]f89ee5d2010-08-02 16:39:123374 handler_map["WaitUntilTranslateComplete"] =
3375 &AutomationProvider::WaitUntilTranslateComplete;
[email protected]3dda5b02010-07-27 16:35:423376
[email protected]55846ad842010-07-09 18:22:563377 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile;
3378 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile;
3379
[email protected]7c983cc2010-07-16 11:33:343380 if (handler_map.find(std::string(command)) != handler_map.end()) {
3381 (this->*handler_map[command])(browser, dict_value, reply_message);
[email protected]1ac875d22010-07-16 09:57:583382 } else {
[email protected]7c983cc2010-07-16 11:33:343383 std::string error_string = "Unknown command. Options: ";
3384 for (std::map<std::string, JsonHandler>::const_iterator it =
3385 handler_map.begin(); it != handler_map.end(); ++it) {
3386 error_string += it->first + ", ";
3387 }
3388 AutomationJSONReply(this, reply_message).SendError(error_string);
[email protected]1ac875d22010-07-16 09:57:583389 }
[email protected]59a611242010-04-02 02:24:043390}
3391
initial.commit09911bf2008-07-26 23:55:293392void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:563393 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:343394 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
3395 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:563396 DCHECK(reply_message_ == NULL);
3397 reply_message_ = reply_message;
3398
[email protected]d9f9b792009-06-24 13:17:123399 DevToolsManager::GetInstance()->InspectElement(
3400 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:293401 } else {
[email protected]71f65dd2009-02-11 19:14:563402 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
3403 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293404 }
3405}
3406
3407void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:093408 if (reply_message_) {
3409 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
3410 num_resources);
3411 Send(reply_message_);
3412 reply_message_ = NULL;
3413 }
initial.commit09911bf2008-07-26 23:55:293414}
3415
[email protected]a7eee32f2009-05-22 18:08:173416class SetProxyConfigTask : public Task {
3417 public:
[email protected]be180c802009-10-23 06:33:313418 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
3419 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:253420 : request_context_getter_(request_context_getter),
3421 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:173422 virtual void Run() {
3423 // First, deserialize the JSON string. If this fails, log and bail.
3424 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:393425 std::string error_msg;
3426 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:173427 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
3428 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:393429 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:173430 return;
3431 }
3432
3433 scoped_ptr<DictionaryValue> dict(
3434 static_cast<DictionaryValue*>(root.release()));
3435 // Now put together a proxy configuration from the deserialized string.
3436 net::ProxyConfig pc;
3437 PopulateProxyConfig(*dict.get(), &pc);
3438
[email protected]be180c802009-10-23 06:33:313439 net::ProxyService* proxy_service =
3440 request_context_getter_->GetURLRequestContext()->proxy_service();
3441 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:173442 scoped_ptr<net::ProxyConfigService> proxy_config_service(
3443 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:313444 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:173445 }
3446
3447 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
3448 DCHECK(pc);
3449 bool no_proxy = false;
3450 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
3451 // Make no changes to the ProxyConfig.
3452 return;
3453 }
3454 bool auto_config;
3455 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:483456 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:173457 }
3458 std::string pac_url;
3459 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:483460 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:173461 }
3462 std::string proxy_bypass_list;
3463 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:483464 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:173465 }
3466 std::string proxy_server;
3467 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:483468 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:173469 }
3470 }
3471
3472 private:
[email protected]be180c802009-10-23 06:33:313473 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:173474 std::string proxy_config_;
3475};
3476
3477
3478void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:313479 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
3480 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:173481 FilePath user_data_dir;
3482 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
3483 ProfileManager* profile_manager = g_browser_process->profile_manager();
3484 DCHECK(profile_manager);
3485 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
3486 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:313487 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:173488 }
[email protected]be180c802009-10-23 06:33:313489 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:173490
[email protected]fae20792009-10-28 20:31:583491 ChromeThread::PostTask(
3492 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:313493 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:173494}
3495
[email protected]4f3dc372009-02-24 00:10:293496void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:333497 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:293498 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:293499 if (tab_tracker_->ContainsHandle(handle)) {
3500 NavigationController* tab = tab_tracker_->GetResource(handle);
3501 DownloadManager* dlm = tab->profile()->GetDownloadManager();
3502 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:333503 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:293504 }
initial.commit09911bf2008-07-26 23:55:293505}
3506
[email protected]6a5670d22009-10-27 16:21:343507void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:143508 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:533509 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
3510 reply_message);
3511}
3512
3513void AutomationProvider::OpenNewBrowserWindowOfType(
3514 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:143515 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:293516 // We may have no current browser windows open so don't rely on
3517 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:533518 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
3519 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:053520 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:153521 if (show)
[email protected]15952e462008-11-14 00:29:053522 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:293523}
3524
[email protected]71f65dd2009-02-11 19:14:563525void AutomationProvider::GetWindowForBrowser(int browser_handle,
3526 bool* success,
3527 int* handle) {
3528 *success = false;
3529 *handle = 0;
initial.commit09911bf2008-07-26 23:55:293530
3531 if (browser_tracker_->ContainsHandle(browser_handle)) {
3532 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:203533 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:293534 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:203535 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:563536 *success = true;
initial.commit09911bf2008-07-26 23:55:293537 }
initial.commit09911bf2008-07-26 23:55:293538}
3539
3540void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:563541 int browser_handle,
3542 bool* success,
3543 int* autocomplete_edit_handle) {
3544 *success = false;
3545 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:293546
3547 if (browser_tracker_->ContainsHandle(browser_handle)) {
3548 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:063549 LocationBar* loc_bar = browser->window()->GetLocationBar();
3550 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:293551 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:563552 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
3553 *success = true;
initial.commit09911bf2008-07-26 23:55:293554 }
initial.commit09911bf2008-07-26 23:55:293555}
initial.commit09911bf2008-07-26 23:55:293556
[email protected]71f65dd2009-02-11 19:14:563557void AutomationProvider::ShowInterstitialPage(int tab_handle,
3558 const std::string& html_text,
3559 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293560 if (tab_tracker_->ContainsHandle(tab_handle)) {
3561 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:113562 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:403563
[email protected]7dad3d5f2010-03-04 00:27:013564 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:403565 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:343566 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:403567 GURL("about:interstitial"),
3568 html_text);
3569 interstitial->Show();
3570 return;
initial.commit09911bf2008-07-26 23:55:293571 }
[email protected]71f65dd2009-02-11 19:14:563572
[email protected]457f5cf2009-08-18 16:37:523573 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
3574 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563575 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293576}
3577
[email protected]71f65dd2009-02-11 19:14:563578void AutomationProvider::HideInterstitialPage(int tab_handle,
3579 bool* success) {
3580 *success = false;
[email protected]57c6a652009-05-04 07:58:343581 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
3582 if (tab_contents && tab_contents->interstitial_page()) {
3583 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:563584 *success = true;
initial.commit09911bf2008-07-26 23:55:293585 }
initial.commit09911bf2008-07-26 23:55:293586}
3587
[email protected]71f65dd2009-02-11 19:14:563588void AutomationProvider::CloseTab(int tab_handle,
3589 bool wait_until_closed,
3590 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293591 if (tab_tracker_->ContainsHandle(tab_handle)) {
3592 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
3593 int index;
3594 Browser* browser = Browser::GetBrowserForController(controller, &index);
3595 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:143596 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:113597 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:453598 return;
initial.commit09911bf2008-07-26 23:55:293599 }
[email protected]de246f52009-02-25 18:25:453600
3601 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:553602 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293603}
3604
[email protected]71f65dd2009-02-11 19:14:563605void AutomationProvider::CloseBrowser(int browser_handle,
3606 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293607 if (browser_tracker_->ContainsHandle(browser_handle)) {
3608 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563609 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:563610 reply_message);
[email protected]f3e99e32008-07-30 04:48:393611 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:293612 } else {
3613 NOTREACHED();
3614 }
3615}
3616
[email protected]71f65dd2009-02-11 19:14:563617void AutomationProvider::CloseBrowserAsync(int browser_handle) {
3618 if (browser_tracker_->ContainsHandle(browser_handle)) {
3619 Browser* browser = browser_tracker_->GetResource(browser_handle);
3620 browser->window()->Close();
3621 } else {
3622 NOTREACHED();
3623 }
3624}
3625
[email protected]71f65dd2009-02-11 19:14:563626void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
3627 IPC::Message* reply_message) {
3628 if (tab_tracker_->ContainsHandle(tab_handle)) {
3629 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3630 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:143631 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:563632 }
3633}
3634
[email protected]71f65dd2009-02-11 19:14:563635void AutomationProvider::GetSecurityState(int handle, bool* success,
3636 SecurityStyle* security_style,
3637 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:483638 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:293639 if (tab_tracker_->ContainsHandle(handle)) {
3640 NavigationController* tab = tab_tracker_->GetResource(handle);
3641 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563642 *success = true;
3643 *security_style = entry->ssl().security_style();
3644 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:483645 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:293646 } else {
[email protected]71f65dd2009-02-11 19:14:563647 *success = false;
3648 *security_style = SECURITY_STYLE_UNKNOWN;
3649 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:483650 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:293651 }
3652}
3653
[email protected]71f65dd2009-02-11 19:14:563654void AutomationProvider::GetPageType(int handle, bool* success,
3655 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:293656 if (tab_tracker_->ContainsHandle(handle)) {
3657 NavigationController* tab = tab_tracker_->GetResource(handle);
3658 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563659 *page_type = entry->page_type();
3660 *success = true;
initial.commit09911bf2008-07-26 23:55:293661 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:343662 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:563663 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:343664 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:563665 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293666 } else {
[email protected]71f65dd2009-02-11 19:14:563667 *success = false;
3668 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293669 }
3670}
3671
[email protected]84abba62009-10-07 17:01:443672void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
3673 int* duration_ms) {
3674 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
3675 event_name);
3676}
3677
[email protected]71f65dd2009-02-11 19:14:563678void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
3679 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293680 if (tab_tracker_->ContainsHandle(handle)) {
3681 NavigationController* tab = tab_tracker_->GetResource(handle);
3682 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:073683 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:403684 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:473685 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:343686 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:293687 if (ssl_blocking_page) {
3688 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:013689 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:563690 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:293691 return;
3692 }
3693 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:523694 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3695 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:563696 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293697 return;
3698 }
3699 }
3700 }
3701 // We failed.
[email protected]457f5cf2009-08-18 16:37:523702 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3703 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563704 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293705}
3706
[email protected]71f65dd2009-02-11 19:14:563707void AutomationProvider::BringBrowserToFront(int browser_handle,
3708 bool* success) {
initial.commit09911bf2008-07-26 23:55:293709 if (browser_tracker_->ContainsHandle(browser_handle)) {
3710 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:063711 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:563712 *success = true;
initial.commit09911bf2008-07-26 23:55:293713 } else {
[email protected]71f65dd2009-02-11 19:14:563714 *success = false;
initial.commit09911bf2008-07-26 23:55:293715 }
3716}
3717
[email protected]bdb7ff62010-07-20 01:56:523718void AutomationProvider::IsMenuCommandEnabled(int browser_handle,
3719 int message_num,
3720 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:293721 if (browser_tracker_->ContainsHandle(browser_handle)) {
3722 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563723 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:143724 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:293725 } else {
[email protected]71f65dd2009-02-11 19:14:563726 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:293727 }
3728}
3729
[email protected]71f65dd2009-02-11 19:14:563730void AutomationProvider::PrintNow(int tab_handle,
3731 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:573732 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:343733 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3734 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:293735 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:573736 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:143737 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:343738 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:573739 return;
initial.commit09911bf2008-07-26 23:55:293740 }
[email protected]71f65dd2009-02-11 19:14:563741 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
3742 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293743}
[email protected]d301c952009-07-13 15:02:413744
[email protected]71f65dd2009-02-11 19:14:563745void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:113746 const FilePath& file_name,
3747 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:563748 int type,
3749 bool* success) {
initial.commit09911bf2008-07-26 23:55:293750 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:563751 *success = false;
initial.commit09911bf2008-07-26 23:55:293752 return;
3753 }
3754
3755 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3756 Browser* browser = FindAndActivateTab(nav);
3757 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:143758 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:563759 *success = false;
initial.commit09911bf2008-07-26 23:55:293760 return;
3761 }
3762
initial.commit09911bf2008-07-26 23:55:293763 SavePackage::SavePackageType save_type =
3764 static_cast<SavePackage::SavePackageType>(type);
3765 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
3766 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:343767 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:293768
[email protected]71f65dd2009-02-11 19:14:563769 *success = true;
initial.commit09911bf2008-07-26 23:55:293770}
3771
[email protected]71f65dd2009-02-11 19:14:563772void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
3773 bool* success,
3774 std::wstring* text) {
3775 *success = false;
initial.commit09911bf2008-07-26 23:55:293776 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563777 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:523778 GetText();
[email protected]71f65dd2009-02-11 19:14:563779 *success = true;
initial.commit09911bf2008-07-26 23:55:293780 }
initial.commit09911bf2008-07-26 23:55:293781}
3782
[email protected]71f65dd2009-02-11 19:14:563783void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
3784 const std::wstring& text,
3785 bool* success) {
3786 *success = false;
initial.commit09911bf2008-07-26 23:55:293787 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:523788 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
3789 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:563790 *success = true;
initial.commit09911bf2008-07-26 23:55:293791 }
initial.commit09911bf2008-07-26 23:55:293792}
3793
3794void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:563795 int autocomplete_edit_handle,
3796 bool* success,
3797 std::vector<AutocompleteMatchData>* matches) {
3798 *success = false;
initial.commit09911bf2008-07-26 23:55:293799 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:273800 const AutocompleteResult& result = autocomplete_edit_tracker_->
3801 GetResource(autocomplete_edit_handle)->model()->result();
3802 for (AutocompleteResult::const_iterator i = result.begin();
3803 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:563804 matches->push_back(AutocompleteMatchData(*i));
3805 *success = true;
initial.commit09911bf2008-07-26 23:55:293806 }
initial.commit09911bf2008-07-26 23:55:293807}
3808
3809void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:563810 int autocomplete_edit_handle,
3811 bool* success,
3812 bool* query_in_progress) {
3813 *success = false;
3814 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:293815 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563816 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:523817 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:563818 *success = true;
initial.commit09911bf2008-07-26 23:55:293819 }
initial.commit09911bf2008-07-26 23:55:293820}
3821
[email protected]63514af2010-03-30 17:17:233822#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:193823
[email protected]5ae5bed2009-08-21 18:52:443824#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:393825
[email protected]57c6a652009-05-04 07:58:343826TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:573827 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:573828 if (tab_tracker_->ContainsHandle(handle)) {
3829 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:343830 if (tab)
3831 *tab = nav_controller;
3832 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:573833 }
[email protected]57c6a652009-05-04 07:58:343834 return NULL;
[email protected]20e93d12008-08-28 16:31:573835}
3836
initial.commit09911bf2008-07-26 23:55:293837TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
3838 : AutomationProvider(profile) {
3839 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:143840 registrar_.Add(this, NotificationType::SESSION_END,
3841 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:293842}
3843
3844TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:293845 BrowserList::RemoveObserver(this);
3846}
3847
3848void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:143849 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:293850 AutomationProvider::OnChannelError();
3851}
3852
[email protected]679082052010-07-21 21:30:133853void TestingAutomationProvider::OnBrowserAdded(const Browser* browser) {
3854}
3855
initial.commit09911bf2008-07-26 23:55:293856void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
3857 // For backwards compatibility with the testing automation interface, we
3858 // want the automation provider (and hence the process) to go away when the
3859 // last browser goes away.
[email protected]c984d9f2010-07-20 20:52:203860 if (BrowserList::size() == 1 && !CommandLine::ForCurrentProcess()->HasSwitch(
3861 switches::kKeepAliveForTest)) {
[email protected]4f3dc372009-02-24 00:10:293862 // If you change this, update Observer for NotificationType::SESSION_END
3863 // below.
[email protected]295039bd2008-08-15 04:32:573864 MessageLoop::current()->PostTask(FROM_HERE,
3865 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:293866 }
3867}
3868
3869void TestingAutomationProvider::Observe(NotificationType type,
3870 const NotificationSource& source,
3871 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:563872 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:293873 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
3874 // before the task runs resulting in this object not being deleted. This
3875 // Release balance out the Release scheduled by OnBrowserRemoving.
3876 Release();
3877}
[email protected]295039bd2008-08-15 04:32:573878
3879void TestingAutomationProvider::OnRemoveProvider() {
3880 AutomationProviderList::GetInstance()->RemoveProvider(this);
3881}
[email protected]8a3422c92008-09-24 17:42:423882
[email protected]816633a2009-11-11 21:48:183883void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:563884 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:423885 if (tab_tracker_->ContainsHandle(handle)) {
3886 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:343887 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:113888 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423889 }
[email protected]8a3422c92008-09-24 17:42:423890}
3891
[email protected]816633a2009-11-11 21:48:183892void AutomationProvider::ClickInfoBarAccept(int handle,
3893 int info_bar_index,
3894 bool wait_for_navigation,
3895 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:423896 bool success = false;
3897 if (tab_tracker_->ContainsHandle(handle)) {
3898 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3899 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:113900 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423901 if (info_bar_index >= 0 && info_bar_index < count) {
3902 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:013903 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:423904 }
[email protected]eb9ba192008-12-02 02:41:343905 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:113906 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:343907 info_bar_index);
3908 if (delegate->AsConfirmInfoBarDelegate())
3909 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:423910 success = true;
3911 }
3912 }
[email protected]4f3dc372009-02-24 00:10:293913 }
[email protected]58f622a62009-10-04 01:17:553914
3915 // This "!wait_for_navigation || !success condition" logic looks suspicious.
3916 // It will send a failure message when success is true but
3917 // |wait_for_navigation| is false.
3918 // TODO(phajdan.jr): investgate whether the reply param (currently
3919 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:423920 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:183921 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:523922 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:423923}
3924
[email protected]71f65dd2009-02-11 19:14:563925void AutomationProvider::GetLastNavigationTime(int handle,
3926 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:423927 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:563928 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:423929}
3930
[email protected]71f65dd2009-02-11 19:14:563931void AutomationProvider::WaitForNavigation(int handle,
3932 int64 last_navigation_time,
3933 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:253934 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:423935 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:253936
[email protected]8a3422c92008-09-24 17:42:423937 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:563938 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:523939 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
3940 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:553941 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:293942 return;
[email protected]8a3422c92008-09-24 17:42:423943 }
3944
[email protected]7dad3d5f2010-03-04 00:27:013945 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:423946}
3947
[email protected]71f65dd2009-02-11 19:14:563948void AutomationProvider::SetIntPreference(int handle,
[email protected]57ecc4b2010-08-11 03:02:513949 const std::string& name,
[email protected]71f65dd2009-02-11 19:14:563950 int value,
3951 bool* success) {
3952 *success = false;
[email protected]8a3422c92008-09-24 17:42:423953 if (browser_tracker_->ContainsHandle(handle)) {
3954 Browser* browser = browser_tracker_->GetResource(handle);
3955 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563956 *success = true;
[email protected]8a3422c92008-09-24 17:42:423957 }
[email protected]8a3422c92008-09-24 17:42:423958}
[email protected]97fa6ce32008-12-19 01:48:163959
[email protected]71f65dd2009-02-11 19:14:563960void AutomationProvider::SetStringPreference(int handle,
[email protected]57ecc4b2010-08-11 03:02:513961 const std::string& name,
[email protected]ddd231e2010-06-29 20:35:193962 const std::string& value,
[email protected]71f65dd2009-02-11 19:14:563963 bool* success) {
3964 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163965 if (browser_tracker_->ContainsHandle(handle)) {
3966 Browser* browser = browser_tracker_->GetResource(handle);
3967 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563968 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163969 }
[email protected]97fa6ce32008-12-19 01:48:163970}
3971
[email protected]71f65dd2009-02-11 19:14:563972void AutomationProvider::GetBooleanPreference(int handle,
[email protected]57ecc4b2010-08-11 03:02:513973 const std::string& name,
[email protected]b8f48d12009-11-09 20:14:363974 bool* success,
3975 bool* value) {
[email protected]71f65dd2009-02-11 19:14:563976 *success = false;
3977 *value = false;
[email protected]97fa6ce32008-12-19 01:48:163978 if (browser_tracker_->ContainsHandle(handle)) {
3979 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:563980 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
3981 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163982 }
[email protected]97fa6ce32008-12-19 01:48:163983}
3984
[email protected]71f65dd2009-02-11 19:14:563985void AutomationProvider::SetBooleanPreference(int handle,
[email protected]57ecc4b2010-08-11 03:02:513986 const std::string& name,
[email protected]71f65dd2009-02-11 19:14:563987 bool value,
3988 bool* success) {
3989 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163990 if (browser_tracker_->ContainsHandle(handle)) {
3991 Browser* browser = browser_tracker_->GetResource(handle);
3992 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563993 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163994 }
[email protected]97fa6ce32008-12-19 01:48:163995}
3996
3997// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563998void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:403999 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:164000 if (tab_tracker_->ContainsHandle(tab_handle)) {
4001 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
4002 Browser* browser = FindAndActivateTab(nav);
4003 DCHECK(browser);
4004
[email protected]57c6a652009-05-04 07:58:344005 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
4006 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:164007 }
[email protected]97fa6ce32008-12-19 01:48:164008}
4009
[email protected]b8f48d12009-11-09 20:14:364010// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:564011void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:404012 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:564013 bool* success) {
4014 *success = false;
[email protected]97fa6ce32008-12-19 01:48:164015 if (tab_tracker_->ContainsHandle(tab_handle)) {
4016 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:194017 if (!nav)
4018 return;
[email protected]97fa6ce32008-12-19 01:48:164019 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:164020
[email protected]2f2afba2010-04-01 01:53:194021 // If the browser has UI, simulate what a user would do.
4022 // Activate the tab and then click the encoding menu.
4023 if (browser &&
4024 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:164025 int selected_encoding_id =
4026 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
4027 if (selected_encoding_id) {
4028 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:564029 *success = true;
[email protected]97fa6ce32008-12-19 01:48:164030 }
[email protected]2f2afba2010-04-01 01:53:194031 } else {
4032 // There is no UI, Chrome probably runs as Chrome-Frame mode.
4033 // Try to get TabContents and call its override_encoding method.
4034 TabContents* contents = nav->tab_contents();
4035 if (!contents)
4036 return;
4037 const std::string selected_encoding =
4038 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
4039 if (selected_encoding.empty())
4040 return;
4041 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:164042 }
4043 }
[email protected]97fa6ce32008-12-19 01:48:164044}
[email protected]5bcdb312009-01-07 21:43:204045
[email protected]4d434a1a2009-02-11 21:06:574046void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:204047 SavePackage::SetShouldPromptUser(should_prompt);
4048}
[email protected]87eab222009-03-13 00:47:454049
[email protected]66ba4932009-06-04 19:22:134050void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
4051 *count = -1; // -1 is the error code
4052 if (tab_tracker_->ContainsHandle(handle)) {
4053 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
4054 TabContents* tab_contents = nav_controller->tab_contents();
4055 if (tab_contents) {
4056 BlockedPopupContainer* container =
4057 tab_contents->blocked_popup_container();
4058 if (container) {
4059 *count = static_cast<int>(container->GetBlockedPopupCount());
4060 } else {
4061 // If we don't have a container, we don't have any blocked popups to
4062 // contain!
4063 *count = 0;
4064 }
4065 }
4066 }
4067}
[email protected]f7a68432009-07-29 23:18:194068
4069void AutomationProvider::SelectAll(int tab_handle) {
4070 RenderViewHost* view = GetViewForTab(tab_handle);
4071 if (!view) {
4072 NOTREACHED();
4073 return;
4074 }
4075
4076 view->SelectAll();
4077}
4078
4079void AutomationProvider::Cut(int tab_handle) {
4080 RenderViewHost* view = GetViewForTab(tab_handle);
4081 if (!view) {
4082 NOTREACHED();
4083 return;
4084 }
4085
4086 view->Cut();
4087}
4088
4089void AutomationProvider::Copy(int tab_handle) {
4090 RenderViewHost* view = GetViewForTab(tab_handle);
4091 if (!view) {
4092 NOTREACHED();
4093 return;
4094 }
4095
4096 view->Copy();
4097}
4098
4099void AutomationProvider::Paste(int tab_handle) {
4100 RenderViewHost* view = GetViewForTab(tab_handle);
4101 if (!view) {
4102 NOTREACHED();
4103 return;
4104 }
4105
4106 view->Paste();
4107}
4108
4109void AutomationProvider::ReloadAsync(int tab_handle) {
4110 if (tab_tracker_->ContainsHandle(tab_handle)) {
4111 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
4112 if (!tab) {
4113 NOTREACHED();
4114 return;
4115 }
4116
[email protected]106a0812010-03-18 00:15:124117 const bool check_for_repost = true;
4118 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:194119 }
4120}
4121
4122void AutomationProvider::StopAsync(int tab_handle) {
4123 RenderViewHost* view = GetViewForTab(tab_handle);
4124 if (!view) {
[email protected]8b2b3312009-09-14 18:38:364125 // We tolerate StopAsync being called even before a view has been created.
4126 // So just log a warning instead of a NOTREACHED().
4127 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:194128 return;
4129 }
4130
4131 view->Stop();
4132}
4133
[email protected]1bb5f892009-10-06 01:44:574134void AutomationProvider::OnSetPageFontSize(int tab_handle,
4135 int font_size) {
4136 AutomationPageFontSize automation_font_size =
4137 static_cast<AutomationPageFontSize>(font_size);
4138
4139 if (automation_font_size < SMALLEST_FONT ||
4140 automation_font_size > LARGEST_FONT) {
4141 DLOG(ERROR) << "Invalid font size specified : "
4142 << font_size;
4143 return;
4144 }
4145
4146 if (tab_tracker_->ContainsHandle(tab_handle)) {
4147 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
4148 DCHECK(tab != NULL);
4149 if (tab && tab->tab_contents()) {
4150 DCHECK(tab->tab_contents()->profile() != NULL);
4151 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
4152 prefs::kWebKitDefaultFontSize, font_size);
4153 }
4154 }
4155}
4156
[email protected]bc73b4e52010-03-26 04:16:204157void AutomationProvider::RemoveBrowsingData(int remove_mask) {
4158 BrowsingDataRemover* remover;
4159 remover = new BrowsingDataRemover(profile(),
4160 BrowsingDataRemover::EVERYTHING, // All time periods.
4161 base::Time());
4162 remover->Remove(remove_mask);
4163 // BrowsingDataRemover deletes itself.
4164}
[email protected]1bb5f892009-10-06 01:44:574165
[email protected]2949e90d2009-08-21 15:32:524166void AutomationProvider::WaitForBrowserWindowCountToBecome(
4167 int target_count, IPC::Message* reply_message) {
4168 if (static_cast<int>(BrowserList::size()) == target_count) {
4169 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
4170 reply_message, true);
4171 Send(reply_message);
4172 return;
4173 }
4174
4175 // Set up an observer (it will delete itself).
4176 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
4177}
4178
4179void AutomationProvider::WaitForAppModalDialogToBeShown(
4180 IPC::Message* reply_message) {
4181 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
4182 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
4183 reply_message, true);
4184 Send(reply_message);
4185 return;
4186 }
4187
4188 // Set up an observer (it will delete itself).
4189 new AppModalDialogShownObserver(this, reply_message);
4190}
4191
[email protected]1126a1d32009-08-26 15:39:264192void AutomationProvider::GoBackBlockUntilNavigationsComplete(
4193 int handle, int number_of_navigations, IPC::Message* reply_message) {
4194 if (tab_tracker_->ContainsHandle(handle)) {
4195 NavigationController* tab = tab_tracker_->GetResource(handle);
4196 Browser* browser = FindAndActivateTab(tab);
4197 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:014198 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
4199 false);
[email protected]1126a1d32009-08-26 15:39:264200 browser->GoBack(CURRENT_TAB);
4201 return;
4202 }
4203 }
4204
4205 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
4206 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
4207 Send(reply_message);
4208}
4209
4210void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
4211 int handle, int number_of_navigations, IPC::Message* reply_message) {
4212 if (tab_tracker_->ContainsHandle(handle)) {
4213 NavigationController* tab = tab_tracker_->GetResource(handle);
4214 Browser* browser = FindAndActivateTab(tab);
4215 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:014216 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
4217 false);
[email protected]1126a1d32009-08-26 15:39:264218 browser->GoForward(CURRENT_TAB);
4219 return;
4220 }
4221 }
4222
4223 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
4224 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
4225 Send(reply_message);
4226}
4227
[email protected]f7a68432009-07-29 23:18:194228RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
4229 if (tab_tracker_->ContainsHandle(tab_handle)) {
4230 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
4231 if (!tab) {
4232 NOTREACHED();
4233 return NULL;
4234 }
4235
4236 TabContents* tab_contents = tab->tab_contents();
4237 if (!tab_contents) {
4238 NOTREACHED();
4239 return NULL;
4240 }
4241
4242 RenderViewHost* view_host = tab_contents->render_view_host();
4243 return view_host;
4244 }
4245
4246 return NULL;
4247}
[email protected]675595f2009-08-26 22:32:044248
4249void AutomationProvider::GetBrowserForWindow(int window_handle,
4250 bool* success,
4251 int* browser_handle) {
4252 *success = false;
4253 *browser_handle = 0;
4254
4255 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
4256 if (!window)
4257 return;
4258
4259 BrowserList::const_iterator iter = BrowserList::begin();
4260 for (;iter != BrowserList::end(); ++iter) {
4261 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
4262 if (window == this_window) {
4263 // Add() returns the existing handle for the resource if any.
4264 *browser_handle = browser_tracker_->Add(*iter);
4265 *success = true;
4266 return;
4267 }
4268 }
4269}
[email protected]d11c8e92009-10-20 23:26:404270
4271void AutomationProvider::InstallExtension(const FilePath& crx_path,
4272 IPC::Message* reply_message) {
4273 ExtensionsService* service = profile_->GetExtensionsService();
4274 if (service) {
4275 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:194276 new ExtensionInstallNotificationObserver(this,
4277 AutomationMsg_InstallExtension::ID,
4278 reply_message);
[email protected]d11c8e92009-10-20 23:26:404279
4280 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:164281 scoped_refptr<CrxInstaller> installer(
4282 new CrxInstaller(install_dir,
4283 service,
4284 NULL)); // silent install, no UI
4285 installer->set_allow_privilege_increase(true);
4286 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:404287 } else {
4288 AutomationMsg_InstallExtension::WriteReplyParams(
4289 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
4290 Send(reply_message);
4291 }
4292}
4293
4294void AutomationProvider::LoadExpandedExtension(
4295 const FilePath& extension_dir,
4296 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:384297 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:404298 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:194299 new ExtensionInstallNotificationObserver(
4300 this,
4301 AutomationMsg_LoadExpandedExtension::ID,
4302 reply_message);
[email protected]d11c8e92009-10-20 23:26:404303
4304 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:404305 } else {
4306 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
4307 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
4308 Send(reply_message);
4309 }
4310}
[email protected]673fd2c02010-02-04 23:10:004311
[email protected]a1e62d12010-03-16 02:18:434312void AutomationProvider::GetEnabledExtensions(
4313 std::vector<FilePath>* result) {
4314 ExtensionsService* service = profile_->GetExtensionsService();
4315 DCHECK(service);
4316 if (service->extensions_enabled()) {
4317 const ExtensionList* extensions = service->extensions();
4318 DCHECK(extensions);
4319 for (size_t i = 0; i < extensions->size(); ++i) {
4320 Extension* extension = (*extensions)[i];
4321 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:124322 if (extension->location() == Extension::INTERNAL ||
4323 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:074324 result->push_back(extension->path());
4325 }
[email protected]a1e62d12010-03-16 02:18:434326 }
4327 }
4328}
4329
[email protected]790788ac2010-04-06 17:52:194330void AutomationProvider::WaitForExtensionTestResult(
4331 IPC::Message* reply_message) {
4332 DCHECK(reply_message_ == NULL);
4333 reply_message_ = reply_message;
4334 // Call MaybeSendResult, because the result might have come in before
4335 // we were waiting on it.
4336 extension_test_result_observer_->MaybeSendResult();
4337}
4338
4339void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:094340 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:194341 ExtensionsService* service = profile_->GetExtensionsService();
4342 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
4343 if (service && manager) {
4344 // The observer will delete itself when done.
4345 new ExtensionReadyNotificationObserver(
4346 manager,
4347 this,
4348 AutomationMsg_InstallExtensionAndGetHandle::ID,
4349 reply_message);
4350
[email protected]d7e5525d2010-04-20 14:37:094351 ExtensionInstallUI* client =
4352 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:194353 scoped_refptr<CrxInstaller> installer(
4354 new CrxInstaller(service->install_directory(),
4355 service,
[email protected]d7e5525d2010-04-20 14:37:094356 client));
[email protected]790788ac2010-04-06 17:52:194357 installer->set_allow_privilege_increase(true);
4358 installer->InstallCrx(crx_path);
4359 } else {
4360 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
4361 reply_message, 0);
4362 Send(reply_message);
4363 }
4364}
4365
4366void AutomationProvider::UninstallExtension(int extension_handle,
4367 bool* success) {
4368 *success = false;
4369 Extension* extension = GetExtension(extension_handle);
4370 ExtensionsService* service = profile_->GetExtensionsService();
4371 if (extension && service) {
4372 ExtensionUnloadNotificationObserver observer;
4373 service->UninstallExtension(extension->id(), false);
4374 // The extension unload notification should have been sent synchronously
4375 // with the uninstall. Just to be safe, check that it was received.
4376 *success = observer.did_receive_unload_notification();
4377 }
4378}
4379
4380void AutomationProvider::EnableExtension(int extension_handle,
4381 IPC::Message* reply_message) {
4382 Extension* extension = GetDisabledExtension(extension_handle);
4383 ExtensionsService* service = profile_->GetExtensionsService();
4384 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
4385 // Only enable if this extension is disabled.
4386 if (extension && service && manager) {
4387 // The observer will delete itself when done.
4388 new ExtensionReadyNotificationObserver(
4389 manager,
4390 this,
4391 AutomationMsg_EnableExtension::ID,
4392 reply_message);
4393 service->EnableExtension(extension->id());
4394 } else {
4395 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
4396 Send(reply_message);
4397 }
4398}
4399
4400void AutomationProvider::DisableExtension(int extension_handle,
4401 bool* success) {
4402 *success = false;
4403 Extension* extension = GetEnabledExtension(extension_handle);
4404 ExtensionsService* service = profile_->GetExtensionsService();
4405 if (extension && service) {
4406 ExtensionUnloadNotificationObserver observer;
4407 service->DisableExtension(extension->id());
4408 // The extension unload notification should have been sent synchronously
4409 // with the disable. Just to be safe, check that it was received.
4410 *success = observer.did_receive_unload_notification();
4411 }
4412}
4413
4414void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
4415 int extension_handle, int browser_handle,
4416 IPC::Message* reply_message) {
4417 bool success = false;
4418 Extension* extension = GetEnabledExtension(extension_handle);
4419 ExtensionsService* service = profile_->GetExtensionsService();
4420 ExtensionMessageService* message_service =
4421 profile_->GetExtensionMessageService();
4422 Browser* browser = browser_tracker_->GetResource(browser_handle);
4423 if (extension && service && message_service && browser) {
4424 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
4425 if (extension->page_action()) {
4426 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
4427 browser->profile(), extension->id(), "action", tab_id, "", 1);
4428 success = true;
4429 } else if (extension->browser_action()) {
4430 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
4431 browser->profile(), extension->id(), browser);
4432 success = true;
4433 }
4434 }
4435 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
4436 reply_message, success);
4437 Send(reply_message);
4438}
4439
4440void AutomationProvider::MoveExtensionBrowserAction(
4441 int extension_handle, int index, bool* success) {
4442 *success = false;
4443 Extension* extension = GetEnabledExtension(extension_handle);
4444 ExtensionsService* service = profile_->GetExtensionsService();
4445 if (extension && service) {
4446 ExtensionToolbarModel* toolbar = service->toolbar_model();
4447 if (toolbar) {
4448 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
4449 toolbar->MoveBrowserAction(extension, index);
4450 *success = true;
4451 } else {
4452 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
4453 }
4454 }
4455 }
4456}
4457
4458void AutomationProvider::GetExtensionProperty(
4459 int extension_handle,
4460 AutomationMsg_ExtensionProperty type,
4461 bool* success,
4462 std::string* value) {
4463 *success = false;
4464 Extension* extension = GetExtension(extension_handle);
4465 ExtensionsService* service = profile_->GetExtensionsService();
4466 if (extension && service) {
4467 ExtensionToolbarModel* toolbar = service->toolbar_model();
4468 int found_index = -1;
4469 int index = 0;
4470 switch (type) {
4471 case AUTOMATION_MSG_EXTENSION_ID:
4472 *value = extension->id();
4473 *success = true;
4474 break;
4475 case AUTOMATION_MSG_EXTENSION_NAME:
4476 *value = extension->name();
4477 *success = true;
4478 break;
4479 case AUTOMATION_MSG_EXTENSION_VERSION:
4480 *value = extension->VersionString();
4481 *success = true;
4482 break;
4483 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
4484 if (toolbar) {
4485 for (ExtensionList::const_iterator iter = toolbar->begin();
4486 iter != toolbar->end(); iter++) {
4487 // Skip this extension if we are in incognito mode
4488 // and it is not incognito-enabled.
4489 if (profile_->IsOffTheRecord() &&
4490 !service->IsIncognitoEnabled(*iter))
4491 continue;
4492 if (*iter == extension) {
4493 found_index = index;
4494 break;
4495 }
4496 index++;
4497 }
[email protected]528c56d2010-07-30 19:28:444498 *value = base::IntToString(found_index);
[email protected]790788ac2010-04-06 17:52:194499 *success = true;
4500 }
4501 break;
4502 default:
4503 LOG(WARNING) << "Trying to get undefined extension property";
4504 break;
4505 }
4506 }
4507}
4508
[email protected]673fd2c02010-02-04 23:10:004509void AutomationProvider::SaveAsAsync(int tab_handle) {
4510 NavigationController* tab = NULL;
4511 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
4512 if (tab_contents)
4513 tab_contents->OnSavePage();
4514}
[email protected]7dad3d5f2010-03-04 00:27:014515
4516void AutomationProvider::SetContentSetting(
4517 int handle,
4518 const std::string& host,
4519 ContentSettingsType content_type,
4520 ContentSetting setting,
4521 bool* success) {
4522 *success = false;
4523 if (browser_tracker_->ContainsHandle(handle)) {
4524 Browser* browser = browser_tracker_->GetResource(handle);
4525 HostContentSettingsMap* map =
4526 browser->profile()->GetHostContentSettingsMap();
4527 if (host.empty()) {
4528 map->SetDefaultContentSetting(content_type, setting);
4529 } else {
[email protected]0314ae02010-04-08 09:18:294530 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
[email protected]ca352452010-08-06 11:14:094531 content_type, "", setting);
[email protected]7dad3d5f2010-03-04 00:27:014532 }
4533 *success = true;
4534 }
4535}
[email protected]cc824372010-03-31 15:33:014536
4537#if !defined(TOOLKIT_VIEWS)
4538void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
4539 NOTIMPLEMENTED();
4540};
4541
4542void AutomationProvider::WaitForFocusedViewIDToChange(
4543 int handle, int previous_view_id, IPC::Message* reply_message) {
4544 NOTIMPLEMENTED();
4545}
4546
4547void AutomationProvider::StartTrackingPopupMenus(
4548 int browser_handle, bool* success) {
4549 NOTIMPLEMENTED();
4550}
4551
4552void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
4553 NOTIMPLEMENTED();
4554}
4555#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:094556
4557void AutomationProvider::ResetToDefaultTheme() {
4558 profile_->ClearTheme();
4559}