blob: fb8c7a18a7a63f1fc8a1f111779d54181da06d78 [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]202e7a72009-06-15 03:48:369#include "app/l10n_util.h"
[email protected]37126212009-05-06 02:23:3110#include "app/message_box_flags.h"
[email protected]2041cf342010-02-19 03:15:5911#include "base/callback.h"
[email protected]7060bb292010-06-24 00:52:4912#include "base/file_path.h"
[email protected]c6cb1992009-04-13 16:45:2913#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2014#include "base/json/json_reader.h"
[email protected]59a611242010-04-02 02:24:0415#include "base/json/json_writer.h"
[email protected]93364da2010-06-29 18:03:4416#include "base/json/string_escape.h"
[email protected]bc1407f2009-09-29 00:33:3517#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3818#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4620#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5021#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2722#include "base/string_util.h"
[email protected]9eaa18e2010-06-29 20:51:0123#include "base/task.h"
[email protected]5fac9622009-02-04 21:49:3824#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5325#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1726#include "base/values.h"
[email protected]9eaa18e2010-06-29 20:51:0127#include "base/waitable_event.h"
[email protected]4f3dc372009-02-24 00:10:2928#include "chrome/app/chrome_dll_resource.h"
[email protected]bcff05a2010-04-14 01:46:4329#include "chrome/app/chrome_version_info.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]790788ac2010-04-06 17:52:1933#include "chrome/browser/automation/automation_extension_tracker.h"
initial.commit09911bf2008-07-26 23:55:2934#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0835#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]f44265b2009-05-19 18:52:5036#include "chrome/browser/automation/extension_port_container.h"
[email protected]12802702010-07-09 19:43:0937#include "chrome/browser/autocomplete/autocomplete_edit.h"
[email protected]66ba4932009-06-04 19:22:1338#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5339#include "chrome/browser/bookmarks/bookmark_model.h"
40#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]ef413ca2010-05-25 21:09:1441#include "chrome/browser/browser_list.h"
[email protected]5c238752009-06-13 10:29:0742#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3943#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2044#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0545#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5846#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2947#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1248#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5949#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2250#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0551#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4052#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1953#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]ef413ca2010-05-25 21:09:1454#include "chrome/browser/extensions/extension_host.h"
[email protected]d11c8e92009-10-20 23:26:4055#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5556#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1957#include "chrome/browser/extensions/extension_tabs_module.h"
58#include "chrome/browser/extensions/extension_toolbar_model.h"
59#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1660#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5861#include "chrome/browser/find_bar.h"
62#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2963#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0164#include "chrome/browser/host_content_settings_map.h"
[email protected]c5aa5322010-07-15 19:00:0765#include "chrome/browser/importer/importer.h"
66#include "chrome/browser/importer/importer_data_types.h"
[email protected]0ac83682010-01-22 17:46:2767#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0668#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0169#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5370#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2471#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0872#include "chrome/browser/pref_service.h"
[email protected]f83f9102010-05-04 17:01:0573#include "chrome/browser/printing/print_job.h"
[email protected]a7eee32f2009-05-22 18:08:1774#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0775#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2576#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0377#include "chrome/browser/ssl/ssl_manager.h"
78#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]b5558cf22010-07-12 17:30:0679#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3480#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4581#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1782#include "chrome/common/automation_constants.h"
[email protected]a9ff2c02010-05-13 17:33:0583#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2984#include "chrome/common/chrome_paths.h"
[email protected]790788ac2010-04-06 17:52:1985#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1786#include "chrome/common/json_value_serializer.h"
[email protected]68d2a05f2010-05-07 21:39:5587#include "chrome/common/net/url_request_context_getter.h"
[email protected]1c58a5c2009-05-21 18:47:1488#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5789#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2590#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5691#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5792#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1793#include "net/proxy/proxy_service.h"
94#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2195#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:3096#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:2897#include "views/event.h"
[email protected]f7d48012010-05-06 08:17:0598#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:2999
[email protected]de246f52009-02-25 18:25:45100#if defined(OS_WIN)
[email protected]4bdde602010-06-16 03:17:35101#include "chrome/browser/external_tab_container_win.h"
[email protected]de246f52009-02-25 18:25:45102#endif // defined(OS_WIN)
103
[email protected]e1acf6f2008-10-27 20:43:33104using base::Time;
105
[email protected]cbab76d2008-10-13 22:42:47106class AutomationInterstitialPage : public InterstitialPage {
107 public:
[email protected]57c6a652009-05-04 07:58:34108 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47109 const GURL& url,
110 const std::string& contents)
111 : InterstitialPage(tab, true, url),
112 contents_(contents) {
113 }
114
115 virtual std::string GetHTMLContents() { return contents_; }
116
117 private:
118 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29119
[email protected]cbab76d2008-10-13 22:42:47120 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
121};
122
[email protected]c2cb8542009-08-20 21:16:51123class ClickTask : public Task {
124 public:
[email protected]fc2e0872009-08-21 22:14:41125 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51126 virtual ~ClickTask() {}
127
128 virtual void Run() {
129 ui_controls::MouseButton button = ui_controls::LEFT;
130 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
131 views::Event::EF_LEFT_BUTTON_DOWN) {
132 button = ui_controls::LEFT;
133 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
134 views::Event::EF_RIGHT_BUTTON_DOWN) {
135 button = ui_controls::RIGHT;
136 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
137 views::Event::EF_MIDDLE_BUTTON_DOWN) {
138 button = ui_controls::MIDDLE;
139 } else {
140 NOTREACHED();
141 }
142
[email protected]fc2e0872009-08-21 22:14:41143 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51144 }
145
146 private:
[email protected]c2cb8542009-08-20 21:16:51147 int flags_;
148
149 DISALLOW_COPY_AND_ASSIGN(ClickTask);
150};
[email protected]c2cb8542009-08-20 21:16:51151
initial.commit09911bf2008-07-26 23:55:29152AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57153 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56154 profile_(profile),
[email protected]cc824372010-03-31 15:33:01155 reply_message_(NULL),
156 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29157 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19158 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29159 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20160 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29161 autocomplete_edit_tracker_.reset(
162 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29163 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
164 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44165 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19166 extension_test_result_observer_.reset(
167 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13168 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29169}
170
171AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50172 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
173 port_containers_.end());
174 port_containers_.clear();
175
[email protected]0da050b92008-08-19 19:29:47176 // Make sure that any outstanding NotificationObservers also get destroyed.
177 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31178 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47179 while ((observer = it.GetNext()) != NULL)
180 delete observer;
[email protected]528211a2010-01-14 15:25:13181
182 if (channel_.get()) {
183 channel_->Close();
184 }
185 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29186}
187
[email protected]9a3a293b2009-06-04 22:28:16188void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06189 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57190 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06191 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
192 automation_resource_message_filter_,
193 g_browser_process->io_thread()->message_loop(),
194 true, g_browser_process->shutdown_event()));
[email protected]bcff05a2010-04-14 01:46:43195 scoped_ptr<FileVersionInfo> version_info(
196 chrome_app::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40197 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43198 if (version_info != NULL) {
199 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40200 }
[email protected]c6cb1992009-04-13 16:45:29201
202 // Send a hello message with our current automation protocol version.
203 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29204}
205
206void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
207 if (expected_tabs == 0) {
208 Send(new AutomationMsg_InitialLoadsComplete(0));
209 } else {
210 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
211 }
212}
213
214NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58215 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01216 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29217 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58218 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01219 number_of_navigations,
220 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29221
[email protected]71f65dd2009-02-11 19:14:56222 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29223 return observer;
224}
225
[email protected]faf2ee42010-05-11 14:26:17226void AutomationProvider::RemoveNavigationStatusListener(
227 NotificationObserver* obs) {
228 notification_observer_list_.RemoveObserver(obs);
229}
230
initial.commit09911bf2008-07-26 23:55:29231NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14232 Browser* parent,
233 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56234 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14235 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29236 notification_observer_list_.AddObserver(observer);
237
238 return observer;
239}
240
[email protected]faf2ee42010-05-11 14:26:17241void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
initial.commit09911bf2008-07-26 23:55:29242 notification_observer_list_.RemoveObserver(obs);
243}
244
245void AutomationProvider::AddLoginHandler(NavigationController* tab,
246 LoginHandler* handler) {
247 login_handler_map_[tab] = handler;
248}
249
250void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
251 DCHECK(login_handler_map_[tab]);
252 login_handler_map_.erase(tab);
253}
254
[email protected]f44265b2009-05-19 18:52:50255void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
256 int port_id = port->port_id();
257 DCHECK_NE(-1, port_id);
258 DCHECK(port_containers_.find(port_id) == port_containers_.end());
259
260 port_containers_[port_id] = port;
261}
262
263void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
264 int port_id = port->port_id();
265 DCHECK_NE(-1, port_id);
266
267 PortContainerMap::iterator it = port_containers_.find(port_id);
268 DCHECK(it != port_containers_.end());
269
270 if (it != port_containers_.end()) {
271 delete it->second;
272 port_containers_.erase(it);
273 }
274}
275
276ExtensionPortContainer* AutomationProvider::GetPortContainer(
277 int port_id) const {
278 PortContainerMap::const_iterator it = port_containers_.find(port_id);
279 if (it == port_containers_.end())
280 return NULL;
281
282 return it->second;
283}
284
initial.commit09911bf2008-07-26 23:55:29285int AutomationProvider::GetIndexForNavigationController(
286 const NavigationController* controller, const Browser* parent) const {
287 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12288 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29289}
290
[email protected]790788ac2010-04-06 17:52:19291int AutomationProvider::AddExtension(Extension* extension) {
292 DCHECK(extension);
293 return extension_tracker_->Add(extension);
294}
295
296Extension* AutomationProvider::GetExtension(int extension_handle) {
297 return extension_tracker_->GetResource(extension_handle);
298}
299
300Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
301 Extension* extension = extension_tracker_->GetResource(extension_handle);
302 ExtensionsService* service = profile_->GetExtensionsService();
303 if (extension && service &&
304 service->GetExtensionById(extension->id(), false))
305 return extension;
306 return NULL;
307}
308
309Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
310 Extension* extension = extension_tracker_->GetResource(extension_handle);
311 ExtensionsService* service = profile_->GetExtensionsService();
312 if (extension && service &&
313 service->GetExtensionById(extension->id(), true) &&
314 !service->GetExtensionById(extension->id(), false))
315 return extension;
316 return NULL;
317}
318
initial.commit09911bf2008-07-26 23:55:29319void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
320 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14321 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56322 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
323 CloseBrowserAsync)
324 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
325 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
326 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
327 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
328 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
329 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22330 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]1c58a5c2009-05-21 18:47:14331 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58332 IPC_MESSAGE_HANDLER_DELAY_REPLY(
333 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
334 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56335 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11336 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
337 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56338 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
339 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
340 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
341 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14342 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56343 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
344 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
345 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14346 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29347 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
348 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56349 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36350 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56351 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29352 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56353 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29354 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
355 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56356 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14357 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24358 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44359 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33360 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44361 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33362 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24363 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14364 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24365 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15366 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14367 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24368 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35369 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14370 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30371#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56372 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
373 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30374#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56375 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53376 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56377 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37378#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56379 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45380#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56381 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
382 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03383 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56384 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14385 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09386 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
387 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
388 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29389 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14390 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56391 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
392 ExecuteJavascript)
393 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29394 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14395 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
396 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56397 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
398 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14399 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17400 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14401 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14402 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53403 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
404 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14405 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56406 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14407 GetAutocompleteEditForBrowser)
408 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]71f65dd2009-02-11 19:14:56409 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14410 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56411 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14412 HideInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56413 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
414 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14415 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
416 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44417 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
418 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56419 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
420 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29421 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
422 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
423 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56424 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41425 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56426 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
427 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29428 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56429 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29430 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56431 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29432 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56433 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29434 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56435 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45436 HandleOpenFindInPageRequest)
[email protected]1c58a5c2009-05-21 18:47:14437 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56438 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57439 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56440 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57441 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56442 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
443 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53444 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
445 GetBookmarksAsJSON)
446 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
447 WaitForBookmarkModelToLoad)
448 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
449 AddBookmarkGroup)
450 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
451 AddBookmarkURL)
452 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
453 ReparentBookmark)
454 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
455 SetBookmarkTitle)
456 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
457 SetBookmarkURL)
458 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
459 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04460 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
461 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18462 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
463 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
464 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56465 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42466 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56467 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
468 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14469 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56470 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44471 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56472 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20473 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14474 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56475 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16476 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56477 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16478 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56479 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16480 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14481 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20482 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
483 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14484 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]59560e0b2009-06-04 03:30:22485 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13486 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19487 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
488 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
489 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
490 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
491 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
492 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52493 IPC_MESSAGE_HANDLER_DELAY_REPLY(
494 AutomationMsg_WaitForBrowserWindowCountToBecome,
495 WaitForBrowserWindowCountToBecome)
496 IPC_MESSAGE_HANDLER_DELAY_REPLY(
497 AutomationMsg_WaitForAppModalDialogToBeShown,
498 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26499 IPC_MESSAGE_HANDLER_DELAY_REPLY(
500 AutomationMsg_GoBackBlockUntilNavigationsComplete,
501 GoBackBlockUntilNavigationsComplete)
502 IPC_MESSAGE_HANDLER_DELAY_REPLY(
503 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
504 GoForwardBlockUntilNavigationsComplete)
[email protected]1bb5f892009-10-06 01:44:57505 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40506 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
507 InstallExtension)
508 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
509 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43510 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
511 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19512 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
513 WaitForExtensionTestResult)
514 IPC_MESSAGE_HANDLER_DELAY_REPLY(
515 AutomationMsg_InstallExtensionAndGetHandle,
516 InstallExtensionAndGetHandle)
517 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
518 UninstallExtension)
519 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
520 EnableExtension)
521 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
522 DisableExtension)
523 IPC_MESSAGE_HANDLER_DELAY_REPLY(
524 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
525 ExecuteExtensionActionInActiveTabAsync)
526 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
527 MoveExtensionBrowserAction)
528 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
529 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57530 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
531 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00532 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]7dad3d5f2010-03-04 00:27:01533 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]bc73b4e52010-03-26 04:16:20534 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]bdd5a9c92010-06-14 18:21:00535 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
[email protected]cc824372010-03-31 15:33:01536#if defined(TOOLKIT_VIEWS)
537 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
538 WaitForFocusedViewIDToChange)
539 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
540 StartTrackingPopupMenus)
541 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
542 WaitForPopupMenuToOpen)
[email protected]bdd5a9c92010-06-14 18:21:00543#endif // defined(TOOLKIT_VIEWS)
[email protected]52415f842010-06-10 21:51:52544#if defined(OS_WIN)
545 // These are for use with external tabs.
546 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
547 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
548 ProcessUnhandledAccelerator)
549 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
550 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
551 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
552 OnForwardContextMenuCommandToChrome)
553 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
554 NavigateInExternalTab)
555 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
556 NavigateExternalTabAtIndex)
557 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
558 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
559 SetEnableExtensionAutomation)
560 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
561 OnMessageFromExternalHost)
[email protected]bdd5a9c92010-06-14 18:21:00562 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
563#endif // defined(OS_WIN)
564#if defined(OS_CHROMEOS)
565 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
566 LoginWithUserAndPass)
567#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29568 IPC_END_MESSAGE_MAP()
569}
570
[email protected]71f65dd2009-02-11 19:14:56571void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
572 *status = -1;
initial.commit09911bf2008-07-26 23:55:29573 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
574 Browser* browser = browser_tracker_->GetResource(handle);
575 if (at_index >= 0 && at_index < browser->tab_count()) {
576 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56577 *status = 0;
initial.commit09911bf2008-07-26 23:55:29578 }
579 }
initial.commit09911bf2008-07-26 23:55:29580}
581
[email protected]71f65dd2009-02-11 19:14:56582void AutomationProvider::AppendTab(int handle, const GURL& url,
583 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29584 int append_tab_response = -1; // -1 is the error code
585 NotificationObserver* observer = NULL;
586
587 if (browser_tracker_->ContainsHandle(handle)) {
588 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14589 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38590 TabContents* tab_contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39591 url, GURL(), PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED,
592 NULL, std::string());
initial.commit09911bf2008-07-26 23:55:29593 if (tab_contents) {
594 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57595 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29596 }
597 }
598
599 if (append_tab_response < 0) {
600 // The append tab failed. Remove the TabStripObserver
601 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17602 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29603 delete observer;
604 }
605
[email protected]71f65dd2009-02-11 19:14:56606 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
607 append_tab_response);
608 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29609 }
610}
611
[email protected]71f65dd2009-02-11 19:14:56612void AutomationProvider::NavigateToURL(int handle, const GURL& url,
613 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58614 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
615}
616
617void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
618 int handle, const GURL& url, int number_of_navigations,
619 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29620 if (tab_tracker_->ContainsHandle(handle)) {
621 NavigationController* tab = tab_tracker_->GetResource(handle);
622
623 // Simulate what a user would do. Activate the tab and then navigate.
624 // We could allow navigating in a background tab in future.
625 Browser* browser = FindAndActivateTab(tab);
626
627 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01628 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
629 false);
[email protected]71f65dd2009-02-11 19:14:56630
initial.commit09911bf2008-07-26 23:55:29631 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50632 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29633 return;
634 }
635 }
[email protected]71f65dd2009-02-11 19:14:56636
637 AutomationMsg_NavigateToURL::WriteReplyParams(
638 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
639 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29640}
[email protected]2949e90d2009-08-21 15:32:52641
[email protected]c70f9b82010-04-21 07:31:11642void AutomationProvider::NavigationAsync(int handle,
643 const GURL& url,
644 bool* status) {
645 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
646}
647
648void AutomationProvider::NavigationAsyncWithDisposition(
649 int handle,
650 const GURL& url,
651 WindowOpenDisposition disposition,
652 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56653 *status = false;
initial.commit09911bf2008-07-26 23:55:29654
655 if (tab_tracker_->ContainsHandle(handle)) {
656 NavigationController* tab = tab_tracker_->GetResource(handle);
657
658 // Simulate what a user would do. Activate the tab and then navigate.
659 // We could allow navigating in a background tab in future.
660 Browser* browser = FindAndActivateTab(tab);
661
662 if (browser) {
663 // Don't add any listener unless a callback mechanism is desired.
664 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11665 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56666 *status = true;
initial.commit09911bf2008-07-26 23:55:29667 }
668 }
initial.commit09911bf2008-07-26 23:55:29669}
670
[email protected]71f65dd2009-02-11 19:14:56671void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29672 if (tab_tracker_->ContainsHandle(handle)) {
673 NavigationController* tab = tab_tracker_->GetResource(handle);
674 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14675 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01676 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29677 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29678 return;
679 }
680 }
[email protected]71f65dd2009-02-11 19:14:56681
682 AutomationMsg_GoBack::WriteReplyParams(
683 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
684 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29685}
686
[email protected]71f65dd2009-02-11 19:14:56687void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29688 if (tab_tracker_->ContainsHandle(handle)) {
689 NavigationController* tab = tab_tracker_->GetResource(handle);
690 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14691 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01692 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29693 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29694 return;
695 }
696 }
[email protected]71f65dd2009-02-11 19:14:56697
698 AutomationMsg_GoForward::WriteReplyParams(
699 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
700 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29701}
702
[email protected]71f65dd2009-02-11 19:14:56703void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29704 if (tab_tracker_->ContainsHandle(handle)) {
705 NavigationController* tab = tab_tracker_->GetResource(handle);
706 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14707 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01708 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]cb84d642010-06-10 00:56:28709 browser->Reload(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29710 return;
711 }
712 }
[email protected]71f65dd2009-02-11 19:14:56713
714 AutomationMsg_Reload::WriteReplyParams(
715 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
716 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29717}
718
[email protected]71f65dd2009-02-11 19:14:56719void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29720 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56721 const std::wstring& password,
722 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29723 if (tab_tracker_->ContainsHandle(tab_handle)) {
724 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
725 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
726
727 if (iter != login_handler_map_.end()) {
728 // If auth is needed again after this, assume login has failed. This is
729 // not strictly correct, because a navigation can require both proxy and
730 // server auth, but it should be OK for now.
731 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01732 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29733 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52734 return;
initial.commit09911bf2008-07-26 23:55:29735 }
736 }
[email protected]de246f52009-02-25 18:25:45737
[email protected]457f5cf2009-08-18 16:37:52738 AutomationMsg_SetAuth::WriteReplyParams(
739 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
740 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29741}
742
[email protected]71f65dd2009-02-11 19:14:56743void AutomationProvider::CancelAuth(int tab_handle,
744 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29745 if (tab_tracker_->ContainsHandle(tab_handle)) {
746 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
747 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
748
749 if (iter != login_handler_map_.end()) {
750 // If auth is needed again after this, something is screwy.
751 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01752 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29753 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52754 return;
initial.commit09911bf2008-07-26 23:55:29755 }
756 }
[email protected]de246f52009-02-25 18:25:45757
[email protected]457f5cf2009-08-18 16:37:52758 AutomationMsg_CancelAuth::WriteReplyParams(
759 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
760 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29761}
762
[email protected]71f65dd2009-02-11 19:14:56763void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
764 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29765
766 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 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56772 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29773 }
774 }
initial.commit09911bf2008-07-26 23:55:29775}
776
[email protected]71f65dd2009-02-11 19:14:56777void AutomationProvider::GetRedirectsFrom(int tab_handle,
778 const GURL& source_url,
779 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29780 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
781 if (tab_tracker_->ContainsHandle(tab_handle)) {
782 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
783 HistoryService* history_service =
784 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
785
786 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
787 "has no history service";
788 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56789 DCHECK(reply_message_ == NULL);
790 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29791 // Schedule a history query for redirects. The response will be sent
792 // asynchronously from the callback the history system uses to notify us
793 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29794 redirect_query_ = history_service->QueryRedirectsFrom(
795 source_url, &consumer_,
796 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
797 return; // Response will be sent when query completes.
798 }
799 }
800
801 // Send failure response.
[email protected]deb57402009-02-06 01:35:30802 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56803 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
804 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29805}
806
[email protected]71f65dd2009-02-11 19:14:56807void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
808 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29809 if (browser_tracker_->ContainsHandle(handle)) {
810 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56811 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29812 }
initial.commit09911bf2008-07-26 23:55:29813}
814
[email protected]202e7a72009-06-15 03:48:36815void AutomationProvider::GetBrowserLocale(string16* locale) {
816 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06817 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36818}
819
[email protected]71f65dd2009-02-11 19:14:56820void AutomationProvider::GetBrowserWindowCount(int* window_count) {
821 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29822}
823
[email protected]24497032009-05-01 17:00:29824void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
825 *window_count = static_cast<int>(
826 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
827}
828
[email protected]71f65dd2009-02-11 19:14:56829void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
830 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07831 AppModalDialog* dialog_delegate =
832 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50833 *showing_dialog = (dialog_delegate != NULL);
834 if (*showing_dialog)
835 *dialog_button = dialog_delegate->GetDialogButtons();
836 else
[email protected]478ff2ed2009-04-21 23:49:18837 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20838}
839
[email protected]71f65dd2009-02-11 19:14:56840void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
841 *success = false;
[email protected]fad84eab2008-12-05 00:37:20842
[email protected]1f460072009-05-28 17:02:07843 AppModalDialog* dialog_delegate =
844 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50845 if (dialog_delegate &&
846 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18847 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
848 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28849 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56850 *success = true;
[email protected]fad84eab2008-12-05 00:37:20851 }
[email protected]478ff2ed2009-04-21 23:49:18852 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
853 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56854 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28855 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56856 *success = true;
[email protected]fad84eab2008-12-05 00:37:20857 }
858 }
[email protected]c274acc2008-11-11 20:13:44859}
860
[email protected]fedaa7d2010-01-26 20:34:57861void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
862 if (browser_tracker_->ContainsHandle(handle)) {
863 Browser* browser = browser_tracker_->GetResource(handle);
864 browser->profile()->ShutdownSessionService();
865 *result = true;
866 } else {
867 *result = false;
868 }
869}
870
[email protected]71f65dd2009-02-11 19:14:56871void AutomationProvider::GetBrowserWindow(int index, int* handle) {
872 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29873 if (index >= 0) {
874 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]f07467d2010-06-16 14:28:30875 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {}
initial.commit09911bf2008-07-26 23:55:29876 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56877 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29878 }
879 }
initial.commit09911bf2008-07-26 23:55:29880}
881
[email protected]24497032009-05-01 17:00:29882void AutomationProvider::FindNormalBrowserWindow(int* handle) {
883 *handle = 0;
884 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31885 Browser::TYPE_NORMAL,
886 false);
[email protected]24497032009-05-01 17:00:29887 if (browser)
888 *handle = browser_tracker_->Add(browser);
889}
890
[email protected]71f65dd2009-02-11 19:14:56891void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
892 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29893 Browser* browser = BrowserList::GetLastActive();
894 if (browser)
[email protected]71f65dd2009-02-11 19:14:56895 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29896}
897
[email protected]b2aa3ed72010-02-01 18:37:14898#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28899// TODO(estade): use this implementation for all platforms?
900void AutomationProvider::GetActiveWindow(int* handle) {
901 gfx::NativeWindow window =
902 BrowserList::GetLastActive()->window()->GetNativeHandle();
903 *handle = window_tracker_->Add(window);
904}
905#endif
906
[email protected]4f6381ee2009-04-16 02:46:33907void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
908 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56909 *success = false;
[email protected]4ae62752008-08-04 23:28:47910 if (browser_tracker_->ContainsHandle(handle)) {
911 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14912 if (browser->command_updater()->SupportsCommand(command) &&
913 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47914 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56915 *success = true;
[email protected]4ae62752008-08-04 23:28:47916 }
917 }
[email protected]4ae62752008-08-04 23:28:47918}
919
[email protected]4f6381ee2009-04-16 02:46:33920void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56921 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53922 // List of commands which just finish synchronously and don't require
923 // setting up an observer.
924 static const int kSynchronousCommands[] = {
925 IDC_HOME,
926 IDC_SELECT_NEXT_TAB,
927 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25928 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53929 };
[email protected]56e71b7c2009-03-27 03:05:56930 if (browser_tracker_->ContainsHandle(handle)) {
931 Browser* browser = browser_tracker_->GetResource(handle);
932 if (browser->command_updater()->SupportsCommand(command) &&
933 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53934 // First check if we can handle the command without using an observer.
935 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
936 if (command == kSynchronousCommands[i]) {
937 browser->ExecuteCommand(command);
938 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
939 true);
940 Send(reply_message);
941 return;
942 }
943 }
944
945 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42946 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
947 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27948 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42949 return;
950 }
[email protected]56e71b7c2009-03-27 03:05:56951 }
952 }
[email protected]49a14a82009-03-31 04:16:44953 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56954 Send(reply_message);
955}
956
[email protected]fc2e0872009-08-21 22:14:41957// This task just adds another task to the event queue. This is useful if
958// you want to ensure that any tasks added to the event queue after this one
959// have already been processed by the time |task| is run.
960class InvokeTaskLaterTask : public Task {
961 public:
962 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
963 virtual ~InvokeTaskLaterTask() {}
964
965 virtual void Run() {
966 MessageLoop::current()->PostTask(FROM_HERE, task_);
967 }
968
969 private:
970 Task* task_;
971
972 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
973};
974
initial.commit09911bf2008-07-26 23:55:29975void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
976 int handle,
[email protected]d1a5941e2009-08-13 23:34:24977 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29978 int flags) {
[email protected]b410bc32009-08-14 01:11:14979 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51980 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41981 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29982 }
983}
984
[email protected]60507b12009-11-02 23:51:35985void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
986 int handle,
987 const gfx::Point& location) {
988 if (window_tracker_->ContainsHandle(handle))
989 ui_controls::SendMouseMove(location.x(), location.y());
990}
991
initial.commit09911bf2008-07-26 23:55:29992void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
993 int handle,
[email protected]bc1407f2009-09-29 00:33:35994 int key,
initial.commit09911bf2008-07-26 23:55:29995 int flags) {
[email protected]b410bc32009-08-14 01:11:14996 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29997 return;
998
[email protected]b410bc32009-08-14 01:11:14999 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291000 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:351001 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:381002 ((flags & views::Event::EF_CONTROL_DOWN) ==
1003 views::Event::EF_CONTROL_DOWN),
1004 ((flags & views::Event::EF_SHIFT_DOWN) ==
1005 views::Event::EF_SHIFT_DOWN),
1006 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301007 views::Event::EF_ALT_DOWN),
1008 ((flags & views::Event::EF_COMMAND_DOWN) ==
1009 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291010}
initial.commit09911bf2008-07-26 23:55:291011
[email protected]71f65dd2009-02-11 19:14:561012void AutomationProvider::IsWindowActive(int handle, bool* success,
1013 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291014 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171015 *is_active =
1016 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561017 *success = true;
initial.commit09911bf2008-07-26 23:55:291018 } else {
[email protected]71f65dd2009-02-11 19:14:561019 *success = false;
1020 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291021 }
1022}
1023
[email protected]71f65dd2009-02-11 19:14:561024void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1025 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291026
1027 if (browser_tracker_->ContainsHandle(handle)) {
1028 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561029 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291030 }
initial.commit09911bf2008-07-26 23:55:291031}
1032
[email protected]982921f12009-10-27 21:43:531033void AutomationProvider::GetType(int handle, int* type_as_int) {
1034 *type_as_int = -1; // -1 is the error code
1035
1036 if (browser_tracker_->ContainsHandle(handle)) {
1037 Browser* browser = browser_tracker_->GetResource(handle);
1038 *type_as_int = static_cast<int>(browser->type());
1039 }
1040}
1041
[email protected]71f65dd2009-02-11 19:14:561042void AutomationProvider::GetTab(int win_handle, int tab_index,
1043 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561044 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291045 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1046 Browser* browser = browser_tracker_->GetResource(win_handle);
1047 if (tab_index < browser->tab_count()) {
1048 TabContents* tab_contents =
1049 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571050 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291051 }
1052 }
initial.commit09911bf2008-07-26 23:55:291053}
1054
[email protected]71f65dd2009-02-11 19:14:561055void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1056 std::wstring* title) {
1057 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291058 if (tab_tracker_->ContainsHandle(handle)) {
1059 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361060 NavigationEntry* entry = tab->GetActiveEntry();
1061 if (entry != NULL) {
1062 *title = UTF16ToWideHack(entry->title());
1063 } else {
1064 *title = std::wstring();
1065 }
[email protected]71f65dd2009-02-11 19:14:561066 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291067 }
initial.commit09911bf2008-07-26 23:55:291068}
1069
[email protected]77bc6732009-04-20 22:01:031070void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1071 *tabstrip_index = -1; // -1 is the error code
1072
1073 if (tab_tracker_->ContainsHandle(handle)) {
1074 NavigationController* tab = tab_tracker_->GetResource(handle);
1075 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121076 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031077 }
1078}
1079
initial.commit09911bf2008-07-26 23:55:291080void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1081 if (window_tracker_->ContainsHandle(handle)) {
1082 window_tracker_->Remove(window_tracker_->GetResource(handle));
1083 }
1084}
1085
1086void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161087 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571088 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291089}
1090
1091// TODO(brettw) change this to accept GURLs when history supports it
1092void AutomationProvider::OnRedirectQueryComplete(
1093 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371094 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291095 bool success,
[email protected]379c2b12009-07-01 21:50:331096 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291097 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561098 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291099
[email protected]deb57402009-02-06 01:35:301100 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101101 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291102 if (success) {
initial.commit09911bf2008-07-26 23:55:291103 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301104 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291105 }
1106
[email protected]4f3dc372009-02-24 00:10:291107 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301108
[email protected]71f65dd2009-02-11 19:14:561109 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231110 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561111 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291112}
1113
1114bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571115 DCHECK(channel_.get());
1116 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291117}
1118
1119Browser* AutomationProvider::FindAndActivateTab(
1120 NavigationController* controller) {
1121 int tab_index;
1122 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1123 if (browser)
1124 browser->SelectTabContentsAt(tab_index, true);
1125
1126 return browser;
1127}
1128
[email protected]9eaa18e2010-06-29 20:51:011129namespace {
1130
1131class GetCookiesTask : public Task {
1132 public:
1133 GetCookiesTask(const GURL& url,
1134 URLRequestContextGetter* context_getter,
1135 base::WaitableEvent* event,
1136 std::string* cookies)
1137 : url_(url),
1138 context_getter_(context_getter),
1139 event_(event),
1140 cookies_(cookies) {}
1141
1142 virtual void Run() {
1143 *cookies_ = context_getter_->GetCookieStore()->GetCookies(url_);
1144 event_->Signal();
1145 }
1146
1147 private:
1148 const GURL& url_;
1149 URLRequestContextGetter* const context_getter_;
1150 base::WaitableEvent* const event_;
1151 std::string* const cookies_;
1152
1153 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
1154};
1155
1156std::string GetCookiesForURL(
1157 const GURL& url,
1158 URLRequestContextGetter* context_getter) {
1159 std::string cookies;
1160 base::WaitableEvent event(true /* manual reset */,
1161 false /* not initially signaled */);
1162 CHECK(ChromeThread::PostTask(
1163 ChromeThread::IO, FROM_HERE,
1164 new GetCookiesTask(url, context_getter, &event, &cookies)));
1165 event.Wait();
1166 return cookies;
1167}
1168
1169class SetCookieTask : public Task {
1170 public:
1171 SetCookieTask(const GURL& url,
1172 const std::string& value,
1173 URLRequestContextGetter* context_getter,
1174 base::WaitableEvent* event,
1175 bool* rv)
1176 : url_(url),
1177 value_(value),
1178 context_getter_(context_getter),
1179 event_(event),
1180 rv_(rv) {}
1181
1182 virtual void Run() {
1183 *rv_ = context_getter_->GetCookieStore()->SetCookie(url_, value_);
1184 event_->Signal();
1185 }
1186
1187 private:
1188 const GURL& url_;
1189 const std::string& value_;
1190 URLRequestContextGetter* const context_getter_;
1191 base::WaitableEvent* const event_;
1192 bool* const rv_;
1193
1194 DISALLOW_COPY_AND_ASSIGN(SetCookieTask);
1195};
1196
1197bool SetCookieForURL(
1198 const GURL& url,
1199 const std::string& value,
1200 URLRequestContextGetter* context_getter) {
1201 base::WaitableEvent event(true /* manual reset */,
1202 false /* not initially signaled */);
1203 bool rv = false;
1204 CHECK(ChromeThread::PostTask(
1205 ChromeThread::IO, FROM_HERE,
1206 new SetCookieTask(url, value, context_getter, &event, &rv)));
1207 event.Wait();
1208 return rv;
1209}
1210
1211class DeleteCookieTask : public Task {
1212 public:
1213 DeleteCookieTask(const GURL& url,
1214 const std::string& name,
1215 const scoped_refptr<URLRequestContextGetter>& context_getter)
1216 : url_(url),
1217 name_(name),
1218 context_getter_(context_getter) {}
1219
1220 virtual void Run() {
1221 net::CookieStore* cookie_store = context_getter_->GetCookieStore();
1222 cookie_store->DeleteCookie(url_, name_);
1223 }
1224
1225 private:
1226 const GURL url_;
1227 const std::string name_;
1228 const scoped_refptr<URLRequestContextGetter> context_getter_;
1229
1230 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
1231};
1232
1233} // namespace
1234
[email protected]71f65dd2009-02-11 19:14:561235void AutomationProvider::GetCookies(const GURL& url, int handle,
1236 int* value_size,
1237 std::string* value) {
1238 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291239 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1240 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311241
1242 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001243 scoped_refptr<URLRequestContextGetter> request_context =
1244 tab->tab_contents()->request_context();
1245 if (!request_context.get())
1246 request_context = tab->profile()->GetRequestContext();
1247
[email protected]9eaa18e2010-06-29 20:51:011248 *value = GetCookiesForURL(url, request_context.get());
[email protected]71f65dd2009-02-11 19:14:561249 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291250 }
initial.commit09911bf2008-07-26 23:55:291251}
1252
[email protected]71f65dd2009-02-11 19:14:561253void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291254 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561255 int handle,
1256 int* response_value) {
1257 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291258
1259 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1260 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311261
[email protected]dfa46e5f2009-11-17 18:48:431262 scoped_refptr<URLRequestContextGetter> request_context =
1263 tab->tab_contents()->request_context();
1264 if (!request_context.get())
1265 request_context = tab->profile()->GetRequestContext();
1266
[email protected]9eaa18e2010-06-29 20:51:011267 if (SetCookieForURL(url, value, request_context.get()))
[email protected]71f65dd2009-02-11 19:14:561268 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291269 }
initial.commit09911bf2008-07-26 23:55:291270}
1271
[email protected]5fa57942010-04-21 23:07:221272void AutomationProvider::DeleteCookie(const GURL& url,
1273 const std::string& cookie_name,
1274 int handle, bool* success) {
1275 *success = false;
1276 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1277 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]9eaa18e2010-06-29 20:51:011278 ChromeThread::PostTask(
1279 ChromeThread::IO, FROM_HERE,
1280 new DeleteCookieTask(url, cookie_name,
1281 tab->profile()->GetRequestContext()));
[email protected]5fa57942010-04-21 23:07:221282 *success = true;
1283 }
1284}
1285
[email protected]71f65dd2009-02-11 19:14:561286void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1287 *success = false;
initial.commit09911bf2008-07-26 23:55:291288 if (tab_tracker_->ContainsHandle(handle)) {
1289 NavigationController* tab = tab_tracker_->GetResource(handle);
1290 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541291 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561292 *success = true;
initial.commit09911bf2008-07-26 23:55:291293 }
initial.commit09911bf2008-07-26 23:55:291294}
1295
[email protected]71f65dd2009-02-11 19:14:561296void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1297 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291298
1299 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561300 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341301 TabContents* tab_contents =
1302 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161303 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1304 if (rph)
1305 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291306 }
initial.commit09911bf2008-07-26 23:55:291307}
1308
1309void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331310 NOTREACHED() << "This function has been deprecated. "
1311 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291312}
1313
[email protected]71f65dd2009-02-11 19:14:561314void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291315 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561316 const std::wstring& script,
1317 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291318 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341319 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1320 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571321 // Set the routing id of this message with the controller.
1322 // This routing id needs to be remembered for the reverse
1323 // communication while sending back the response of
1324 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331325 std::wstring set_automation_id;
1326 SStringPrintf(&set_automation_id,
1327 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561328 reply_message->routing_id());
1329
1330 DCHECK(reply_message_ == NULL);
1331 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291332
[email protected]57c6a652009-05-04 07:58:341333 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331334 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341335 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061336 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571337 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291338 }
1339
1340 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561341 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1342 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291343 }
1344}
1345
[email protected]71f65dd2009-02-11 19:14:561346void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1347 *visible = false;
[email protected]20e93d12008-08-28 16:31:571348
[email protected]59560e0b2009-06-04 03:30:221349 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251350#if defined(OS_CHROMEOS)
1351 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1352 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1353 for (BrowserList::const_iterator it = BrowserList::begin();
1354 it != BrowserList::end(); ++it) {
1355 if ((*it)->type() == Browser::TYPE_POPUP) {
1356 const GURL& url =
1357 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1358
1359 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1360 url.host() == chrome::kChromeUIFileBrowseHost) {
1361 *visible = true;
1362 break;
1363 }
1364 }
1365 }
1366#else
[email protected]59560e0b2009-06-04 03:30:221367 Browser* browser = browser_tracker_->GetResource(handle);
1368 if (browser) {
1369 *visible = browser->window()->IsDownloadShelfVisible();
1370 }
[email protected]f5bf8ccf2010-02-05 18:19:251371#endif
[email protected]59560e0b2009-06-04 03:30:221372 }
initial.commit09911bf2008-07-26 23:55:291373}
1374
[email protected]59560e0b2009-06-04 03:30:221375void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1376 if (browser_tracker_->ContainsHandle(handle)) {
1377 Browser* browser = browser_tracker_->GetResource(handle);
1378 if (browser) {
1379 if (visible)
1380 browser->window()->GetDownloadShelf()->Show();
1381 else
1382 browser->window()->GetDownloadShelf()->Close();
1383 }
1384 }
1385}
1386
[email protected]34930432009-11-09 00:12:091387void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1388 *visible = false;
1389
1390 if (browser_tracker_->ContainsHandle(handle)) {
1391 Browser* browser = browser_tracker_->GetResource(handle);
1392 if (browser)
1393 *visible = browser->window()->IsFullscreen();
1394 }
1395}
1396
1397void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1398 bool* visible) {
1399 *visible = false;
1400
1401 if (browser_tracker_->ContainsHandle(handle)) {
1402 Browser* browser = browser_tracker_->GetResource(handle);
1403 if (browser)
1404 *visible = browser->window()->IsFullscreenBubbleVisible();
1405 }
1406}
[email protected]59560e0b2009-06-04 03:30:221407
[email protected]71f65dd2009-02-11 19:14:561408void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1409 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291410 if (tab_tracker_->ContainsHandle(handle)) {
1411 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111412 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291413 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561414 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291415 }
1416 }
initial.commit09911bf2008-07-26 23:55:291417}
1418
initial.commit09911bf2008-07-26 23:55:291419void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561420 int handle, const std::wstring& find_request,
1421 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311422 NOTREACHED() << "This function has been deprecated."
1423 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561424 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311425 return;
1426}
1427
[email protected]4f999132009-03-31 18:08:401428void AutomationProvider::HandleFindRequest(
1429 int handle,
1430 const AutomationMsg_Find_Params& params,
1431 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291432 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561433 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1434 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291435 return;
1436 }
1437
1438 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111439 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291440
1441 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141442 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291443
[email protected]57c6a652009-05-04 07:58:341444 tab_contents->set_current_find_request_id(
1445 FindInPageNotificationObserver::kFindInPageRequestId);
1446 tab_contents->render_view_host()->StartFinding(
1447 FindInPageNotificationObserver::kFindInPageRequestId,
1448 params.search_string, params.forward, params.match_case,
1449 params.find_next);
initial.commit09911bf2008-07-26 23:55:291450}
1451
[email protected]5f8af2a2008-08-06 22:49:451452void AutomationProvider::HandleOpenFindInPageRequest(
1453 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291454 if (browser_tracker_->ContainsHandle(handle)) {
1455 Browser* browser = browser_tracker_->GetResource(handle);
1456 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451457 }
1458}
1459
[email protected]71f65dd2009-02-11 19:14:561460void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561461 *visible = false;
[email protected]855c0142009-09-28 22:35:241462 Browser* browser = browser_tracker_->GetResource(handle);
1463 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581464 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171465 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241466 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291467 }
[email protected]20e93d12008-08-28 16:31:571468}
1469
[email protected]71f65dd2009-02-11 19:14:561470void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1471 int* y) {
[email protected]9e0534b2008-10-21 15:03:011472 gfx::Point position(0, 0);
1473 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291474 if (browser_tracker_->ContainsHandle(handle)) {
1475 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581476 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171477 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581478 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291479 }
[email protected]20e93d12008-08-28 16:31:571480
[email protected]71f65dd2009-02-11 19:14:561481 *x = position.x();
1482 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571483}
1484
[email protected]4512cb52010-04-05 19:50:251485// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1486// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581487void AutomationProvider::GetBookmarkBarVisibility(int handle,
1488 bool* visible,
1489 bool* animating) {
1490 *visible = false;
1491 *animating = false;
1492
1493 if (browser_tracker_->ContainsHandle(handle)) {
1494 Browser* browser = browser_tracker_->GetResource(handle);
1495 if (browser) {
[email protected]472f099b2010-05-27 17:07:121496#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1497 // TODO(jrg): Was removed in rev43789 for perf. Need to investigate.
1498
[email protected]ab6ca392010-04-07 00:44:131499 // IsBookmarkBarVisible() line looks correct but is not
1500 // consistent across platforms. Specifically, on Mac/Linux, it
1501 // returns false if the bar is hidden in a pref (even if visible
1502 // on the NTP). On ChromeOS, it returned true if on NTP
1503 // independent of the pref. Making the code more consistent
1504 // caused a perf bot regression on Windows (which shares views).
1505 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251506 *visible = browser->profile()->GetPrefs()->GetBoolean(
1507 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401508#else
1509 *visible = browser->window()->IsBookmarkBarVisible();
1510#endif
[email protected]c3240722010-03-05 21:52:581511 *animating = browser->window()->IsBookmarkBarAnimating();
1512 }
1513 }
1514}
1515
[email protected]6d8ffc9f2010-03-12 18:27:531516void AutomationProvider::GetBookmarksAsJSON(int handle,
1517 std::string* bookmarks_as_json,
1518 bool *success) {
1519 *success = false;
1520 if (browser_tracker_->ContainsHandle(handle)) {
1521 Browser* browser = browser_tracker_->GetResource(handle);
1522 if (browser) {
1523 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1524 return;
1525 }
1526 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1527 browser->profile(),
1528 browser->profile()->GetBookmarkModel());
1529 *success = storage->SerializeData(bookmarks_as_json);
1530 }
1531 }
1532}
1533
1534void AutomationProvider::WaitForBookmarkModelToLoad(
1535 int handle,
1536 IPC::Message* reply_message) {
1537 if (browser_tracker_->ContainsHandle(handle)) {
1538 Browser* browser = browser_tracker_->GetResource(handle);
1539 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1540 if (model->IsLoaded()) {
1541 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1542 reply_message, true);
1543 Send(reply_message);
1544 } else {
1545 // The observer will delete itself when done.
1546 new AutomationProviderBookmarkModelObserver(this, reply_message,
1547 model);
1548 }
1549 }
1550}
1551
1552void AutomationProvider::AddBookmarkGroup(int handle,
1553 int64 parent_id, int index,
1554 std::wstring title,
1555 bool* success) {
1556 if (browser_tracker_->ContainsHandle(handle)) {
1557 Browser* browser = browser_tracker_->GetResource(handle);
1558 if (browser) {
1559 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1560 if (!model->IsLoaded()) {
1561 *success = false;
1562 return;
1563 }
1564 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1565 DCHECK(parent);
1566 if (parent) {
1567 const BookmarkNode* child = model->AddGroup(parent, index,
1568 WideToUTF16(title));
1569 DCHECK(child);
1570 if (child)
1571 *success = true;
1572 }
1573 }
1574 }
1575 *success = false;
1576}
1577
1578void AutomationProvider::AddBookmarkURL(int handle,
1579 int64 parent_id, int index,
1580 std::wstring title, const GURL& url,
1581 bool* success) {
1582 if (browser_tracker_->ContainsHandle(handle)) {
1583 Browser* browser = browser_tracker_->GetResource(handle);
1584 if (browser) {
1585 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1586 if (!model->IsLoaded()) {
1587 *success = false;
1588 return;
1589 }
1590 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1591 DCHECK(parent);
1592 if (parent) {
1593 const BookmarkNode* child = model->AddURL(parent, index,
1594 WideToUTF16(title), url);
1595 DCHECK(child);
1596 if (child)
1597 *success = true;
1598 }
1599 }
1600 }
1601 *success = false;
1602}
1603
1604void AutomationProvider::ReparentBookmark(int handle,
1605 int64 id, int64 new_parent_id,
1606 int index,
1607 bool* success) {
1608 if (browser_tracker_->ContainsHandle(handle)) {
1609 Browser* browser = browser_tracker_->GetResource(handle);
1610 if (browser) {
1611 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1612 if (!model->IsLoaded()) {
1613 *success = false;
1614 return;
1615 }
1616 const BookmarkNode* node = model->GetNodeByID(id);
1617 DCHECK(node);
1618 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1619 DCHECK(new_parent);
1620 if (node && new_parent) {
1621 model->Move(node, new_parent, index);
1622 *success = true;
1623 }
1624 }
1625 }
1626 *success = false;
1627}
1628
1629void AutomationProvider::SetBookmarkTitle(int handle,
1630 int64 id, std::wstring title,
1631 bool* success) {
1632 if (browser_tracker_->ContainsHandle(handle)) {
1633 Browser* browser = browser_tracker_->GetResource(handle);
1634 if (browser) {
1635 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1636 if (!model->IsLoaded()) {
1637 *success = false;
1638 return;
1639 }
1640 const BookmarkNode* node = model->GetNodeByID(id);
1641 DCHECK(node);
1642 if (node) {
1643 model->SetTitle(node, WideToUTF16(title));
1644 *success = true;
1645 }
1646 }
1647 }
1648 *success = false;
1649}
1650
1651void AutomationProvider::SetBookmarkURL(int handle,
1652 int64 id, const GURL& url,
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) {
1665 model->SetURL(node, url);
1666 *success = true;
1667 }
1668 }
1669 }
1670 *success = false;
1671}
1672
1673void AutomationProvider::RemoveBookmark(int handle,
1674 int64 id,
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 const BookmarkNode* parent = node->GetParent();
1688 DCHECK(parent);
1689 model->Remove(parent, parent->IndexOfChild(node));
1690 *success = true;
1691 }
1692 }
1693 }
1694 *success = false;
1695}
1696
[email protected]ef413ca2010-05-25 21:09:141697// Sample json input: { "command": "SetWindowDimensions",
1698// "x": 20, # optional
1699// "y": 20, # optional
1700// "width": 800, # optional
1701// "height": 600 } # optional
1702void AutomationProvider::SetWindowDimensions(Browser* browser,
1703 DictionaryValue* args,
1704 IPC::Message* reply_message) {
1705 gfx::Rect rect = browser->window()->GetRestoredBounds();
1706 int x, y, width, height;
1707 if (args->GetInteger(L"x", &x))
1708 rect.set_x(x);
1709 if (args->GetInteger(L"y", &y))
1710 rect.set_y(y);
1711 if (args->GetInteger(L"width", &width))
1712 rect.set_width(width);
1713 if (args->GetInteger(L"height", &height))
1714 rect.set_height(height);
1715 browser->window()->SetBounds(rect);
[email protected]f6ff0df2010-07-11 22:41:431716 AutomationMsg_SendJSONRequest::WriteReplyParams(
1717 reply_message, std::string("{}"), true);
1718 Send(reply_message);
[email protected]ef413ca2010-05-25 21:09:141719}
1720
[email protected]a9ff2c02010-05-13 17:33:051721// Sample json input: { "command": "GetBrowserInfo" }
1722// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1723// sample json output.
[email protected]53329582010-05-14 21:10:581724void AutomationProvider::GetBrowserInfo(Browser* browser,
1725 DictionaryValue* args,
[email protected]a9ff2c02010-05-13 17:33:051726 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:431727 std::string json_return;
1728 bool reply_return = true;
1729
[email protected]a9ff2c02010-05-13 17:33:051730 DictionaryValue* properties = new DictionaryValue;
1731 properties->SetString(L"ChromeVersion", chrome::kChromeVersion);
1732 properties->SetString(L"BrowserProcessExecutableName",
1733 chrome::kBrowserProcessExecutableName);
1734 properties->SetString(L"HelperProcessExecutableName",
1735 chrome::kHelperProcessExecutableName);
1736 properties->SetString(L"BrowserProcessExecutablePath",
1737 chrome::kBrowserProcessExecutablePath);
1738 properties->SetString(L"HelperProcessExecutablePath",
1739 chrome::kHelperProcessExecutablePath);
[email protected]a9ff2c02010-05-13 17:33:051740 properties->SetString(L"command_line_string",
1741 CommandLine::ForCurrentProcess()->command_line_string());
[email protected]44eed9f2010-06-28 22:04:001742
1743 std::string branding;
1744#if defined(GOOGLE_CHROME_BUILD)
1745 branding = "Google Chrome";
1746#elif defined(CHROMIUM_BUILD)
1747 branding = "Chromium";
1748#else
1749 branding = "Unknown Branding";
[email protected]a9ff2c02010-05-13 17:33:051750#endif
[email protected]44eed9f2010-06-28 22:04:001751 properties->SetString(L"branding", branding);
[email protected]a9ff2c02010-05-13 17:33:051752
1753 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1754 return_value->Set(L"properties", properties);
1755
[email protected]ef413ca2010-05-25 21:09:141756 return_value->SetInteger(L"browser_pid", base::GetCurrentProcId());
1757 // Add info about all windows in a list of dictionaries, one dictionary
1758 // item per window.
1759 ListValue* windows = new ListValue;
1760 int windex = 0;
1761 for (BrowserList::const_iterator it = BrowserList::begin();
1762 it != BrowserList::end();
1763 ++it, ++windex) {
1764 DictionaryValue* browser_item = new DictionaryValue;
1765 browser = *it;
1766 browser_item->SetInteger(L"index", windex);
1767 // Window properties
1768 gfx::Rect rect = browser->window()->GetRestoredBounds();
1769 browser_item->SetInteger(L"x", rect.x());
1770 browser_item->SetInteger(L"y", rect.y());
1771 browser_item->SetInteger(L"width", rect.width());
1772 browser_item->SetInteger(L"height", rect.height());
1773 browser_item->SetBoolean(L"fullscreen",
1774 browser->window()->IsFullscreen());
1775 browser_item->SetInteger(L"selected_tab", browser->selected_index());
1776 browser_item->SetBoolean(L"incognito",
1777 browser->profile()->IsOffTheRecord());
1778 // For each window, add info about all tabs in a list of dictionaries,
1779 // one dictionary item per tab.
1780 ListValue* tabs = new ListValue;
1781 for (int i = 0; i < browser->tab_count(); ++i) {
1782 TabContents* tc = browser->GetTabContentsAt(i);
1783 DictionaryValue* tab = new DictionaryValue;
1784 tab->SetInteger(L"index", i);
1785 tab->SetString(L"url", tc->GetURL().spec());
1786 tab->SetInteger(L"renderer_pid",
1787 base::GetProcId(tc->GetRenderProcessHost()->GetHandle()));
1788 tab->SetInteger(L"num_infobars", tc->infobar_delegate_count());
1789 tabs->Append(tab);
1790 }
1791 browser_item->Set(L"tabs", tabs);
1792
1793 windows->Append(browser_item);
1794 }
1795 return_value->Set(L"windows", windows);
1796
1797 return_value->SetString(L"child_process_path",
1798 ChildProcessHost::GetChildPath(true).value());
1799 // Child processes are the processes for plugins and other workers.
1800 // Add all child processes in a list of dictionaries, one dictionary item
1801 // per child process.
1802 ListValue* child_processes = new ListValue;
[email protected]d27893f62010-07-03 05:47:421803 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
[email protected]ef413ca2010-05-25 21:09:141804 // Only add processes which are already started, since we need their handle.
1805 if ((*iter)->handle() != base::kNullProcessHandle) {
1806 ChildProcessInfo* info = *iter;
1807 DictionaryValue* item = new DictionaryValue;
1808 item->SetString(L"name", info->name());
1809 item->SetString(L"type",
1810 ChildProcessInfo::GetTypeNameInEnglish(info->type()));
1811 item->SetInteger(L"pid", base::GetProcId(info->handle()));
1812 child_processes->Append(item);
1813 }
1814 }
1815 return_value->Set(L"child_processes", child_processes);
1816
1817 // Add all extension processes in a list of dictionaries, one dictionary
1818 // item per extension process.
1819 ListValue* extension_processes = new ListValue;
1820 ProfileManager* profile_manager = g_browser_process->profile_manager();
1821 for (ProfileManager::const_iterator it = profile_manager->begin();
1822 it != profile_manager->end(); ++it) {
1823 ExtensionProcessManager* process_manager =
1824 (*it)->GetExtensionProcessManager();
1825 ExtensionProcessManager::const_iterator jt;
1826 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) {
1827 ExtensionHost* ex_host = *jt;
1828 // Don't add dead extension processes.
1829 if (!ex_host->IsRenderViewLive())
1830 continue;
1831 DictionaryValue* item = new DictionaryValue;
1832 item->SetString(L"name", ex_host->extension()->name());
1833 item->SetInteger(
1834 L"pid",
1835 base::GetProcId(ex_host->render_process_host()->GetHandle()));
1836 extension_processes->Append(item);
1837 }
1838 }
1839 return_value->Set(L"extension_processes", extension_processes);
[email protected]f6ff0df2010-07-11 22:41:431840
1841 base::JSONWriter::Write(return_value.get(), false, &json_return);
1842 AutomationMsg_SendJSONRequest::WriteReplyParams(
1843 reply_message, json_return, reply_return);
1844 Send(reply_message);
[email protected]a9ff2c02010-05-13 17:33:051845}
1846
[email protected]24e2b102010-04-29 17:56:471847// Sample json input: { "command": "GetHistoryInfo",
1848// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361849// Refer chrome/test/pyautolib/history_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581850void AutomationProvider::GetHistoryInfo(Browser* browser,
1851 DictionaryValue* args,
1852 IPC::Message* reply_message) {
[email protected]e6e376e2010-04-19 21:41:361853 consumer_.CancelAllRequests();
1854
[email protected]e53668962010-06-23 15:35:251855 string16 search_text;
1856 args->GetString("search_text", &search_text);
[email protected]e6e376e2010-04-19 21:41:361857
1858 // Fetch history.
1859 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1860 history::QueryOptions options;
1861 // The observer owns itself. It deletes itself after it fetches history.
1862 AutomationProviderHistoryObserver* history_observer =
1863 new AutomationProviderHistoryObserver(this, reply_message);
1864 hs->QueryHistory(
1865 search_text,
1866 options,
1867 &consumer_,
1868 NewCallback(history_observer,
1869 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1870}
1871
[email protected]bbe6aa02010-05-07 17:27:291872// Sample json input: { "command": "AddHistoryItem",
1873// "item": { "URL": "http://www.google.com",
1874// "title": "Google", # optional
1875// "time": 12345 # optional (time_t)
1876// } }
1877// Refer chrome/test/pyautolib/pyauto.py for details on input.
[email protected]53329582010-05-14 21:10:581878void AutomationProvider::AddHistoryItem(Browser* browser,
1879 DictionaryValue* args,
1880 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:431881 bool reply_return = true;
1882 std::string json_return = "{}";
1883
[email protected]bbe6aa02010-05-07 17:27:291884 DictionaryValue* item = NULL;
1885 args->GetDictionary(L"item", &item);
1886 string16 url_text;
[email protected]e53668962010-06-23 15:35:251887 string16 title;
[email protected]bbe6aa02010-05-07 17:27:291888 base::Time time = base::Time::Now();
1889
1890 if (item->GetString("url", &url_text)) {
1891 GURL gurl(url_text);
[email protected]e53668962010-06-23 15:35:251892 item->GetString("title", &title); // Don't care if it fails.
[email protected]bbe6aa02010-05-07 17:27:291893 int it;
1894 double dt;
1895 if (item->GetInteger(L"time", &it))
1896 time = base::Time::FromTimeT(it);
1897 else if (item->GetReal(L"time", &dt))
1898 time = base::Time::FromDoubleT(dt);
1899
1900 // Ideas for "dummy" values (e.g. id_scope) came from
1901 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
1902 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1903 const void* id_scope = reinterpret_cast<void*>(1);
1904 hs->AddPage(gurl, time,
1905 id_scope,
1906 0,
1907 GURL(),
1908 PageTransition::LINK,
1909 history::RedirectList(),
1910 false);
[email protected]e53668962010-06-23 15:35:251911 if (title.length())
[email protected]bbe6aa02010-05-07 17:27:291912 hs->SetPageTitle(gurl, title);
[email protected]bbe6aa02010-05-07 17:27:291913 } else {
[email protected]f6ff0df2010-07-11 22:41:431914 json_return = JSONErrorString("bad args (no URL in dict?)");
1915 reply_return = false;
[email protected]bbe6aa02010-05-07 17:27:291916 }
[email protected]f6ff0df2010-07-11 22:41:431917
1918 AutomationMsg_SendJSONRequest::WriteReplyParams(
1919 reply_message, json_return, reply_return);
1920 Send(reply_message);
[email protected]bbe6aa02010-05-07 17:27:291921}
1922
[email protected]24e2b102010-04-29 17:56:471923// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361924// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581925void AutomationProvider::GetDownloadsInfo(Browser* browser,
1926 DictionaryValue* args,
1927 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:431928 std::string json_return;
1929 bool reply_return = true;
[email protected]d4adc292010-04-15 18:06:391930 AutomationProviderDownloadManagerObserver observer;
1931 std::vector<DownloadItem*> downloads;
1932 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1933
1934 if (!profile_->HasCreatedDownloadManager()) {
[email protected]f6ff0df2010-07-11 22:41:431935 json_return = JSONErrorString("no download manager");
1936 reply_return = false;
[email protected]d4adc292010-04-15 18:06:391937 } else {
1938 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1939 // since that would be transient; a download might enter and empty out
1940 // the current download queue too soon to be noticed.
1941 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1942 downloads = observer.Downloads();
1943 }
1944
1945 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1946 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1947 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1948 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1949 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1950
1951 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1952 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1953 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1954 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1955 std::string("DANGEROUS_BUT_VALIDATED");
1956
1957 ListValue* list_of_downloads = new ListValue;
1958 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1959 it != downloads.end();
1960 it++) { // Fill info about each download item.
1961 DictionaryValue* dl_item_value = new DictionaryValue;
1962 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1963 dl_item_value->SetString(L"url", (*it)->url().spec());
1964 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1965 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1966 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1967 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1968 dl_item_value->SetBoolean(L"open_when_complete",
1969 (*it)->open_when_complete());
1970 dl_item_value->SetBoolean(L"is_extension_install",
1971 (*it)->is_extension_install());
1972 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1973 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1974 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1975 dl_item_value->SetString(L"safety_state",
1976 safety_state_to_string[(*it)->safety_state()]);
1977 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1978 list_of_downloads->Append(dl_item_value);
1979 }
1980 return_value->Set(L"downloads", list_of_downloads);
[email protected]f6ff0df2010-07-11 22:41:431981 base::JSONWriter::Write(return_value.get(), false, &json_return);
[email protected]d4adc292010-04-15 18:06:391982
[email protected]f6ff0df2010-07-11 22:41:431983 AutomationMsg_SendJSONRequest::WriteReplyParams(
1984 reply_message, json_return, reply_return);
1985 Send(reply_message);
[email protected]d4adc292010-04-15 18:06:391986 // All value objects allocated above are owned by |return_value|
1987 // and get freed by it.
1988}
1989
[email protected]59a611242010-04-02 02:24:041990void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:581991 Browser* browser,
[email protected]59a611242010-04-02 02:24:041992 DictionaryValue* args,
1993 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:431994 std::string json_return;
1995 bool reply_return = true;
[email protected]59a611242010-04-02 02:24:041996 AutomationProviderDownloadManagerObserver observer;
1997 std::vector<DownloadItem*> downloads;
1998
1999 // Look for a quick return.
2000 if (!profile_->HasCreatedDownloadManager()) {
[email protected]f6ff0df2010-07-11 22:41:432001 json_return = JSONErrorString("no download manager");
2002 reply_return = false;
[email protected]59a611242010-04-02 02:24:042003 } else {
[email protected]f6ff0df2010-07-11 22:41:432004 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
2005 FilePath());
[email protected]59a611242010-04-02 02:24:042006 downloads = observer.Downloads();
2007 if (downloads.size() == 0) {
[email protected]f6ff0df2010-07-11 22:41:432008 json_return = "{}";
[email protected]59a611242010-04-02 02:24:042009 }
2010 }
[email protected]f6ff0df2010-07-11 22:41:432011 if (!json_return.empty()) {
2012 AutomationMsg_SendJSONRequest::WriteReplyParams(
2013 reply_message, json_return, reply_return);
2014 Send(reply_message);
2015 return;
2016 }
[email protected]59a611242010-04-02 02:24:042017
2018 // The observer owns itself. When the last observed item pings, it
2019 // deletes itself.
2020 AutomationProviderDownloadItemObserver* item_observer =
2021 new AutomationProviderDownloadItemObserver(
2022 this, reply_message, downloads.size());
2023 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
2024 i != downloads.end();
2025 i++) {
2026 (*i)->AddObserver(item_observer);
2027 }
2028}
2029
[email protected]24e2b102010-04-29 17:56:472030// Sample json input: { "command": "GetPrefsInfo" }
2031// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582032void AutomationProvider::GetPrefsInfo(Browser* browser,
2033 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472034 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432035 std::string json_return;
2036 bool reply_return = true;
2037
[email protected]24e2b102010-04-29 17:56:472038 const PrefService::PreferenceSet& prefs =
2039 profile_->GetPrefs()->preference_set();
2040 DictionaryValue* items = new DictionaryValue;
2041 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
2042 it != prefs.end(); ++it) {
2043 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
2044 }
2045 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2046 return_value->Set(L"prefs", items); // return_value owns items.
[email protected]f6ff0df2010-07-11 22:41:432047
2048 base::JSONWriter::Write(return_value.get(), false, &json_return);
2049 AutomationMsg_SendJSONRequest::WriteReplyParams(
2050 reply_message, json_return, reply_return);
2051 Send(reply_message);
[email protected]24e2b102010-04-29 17:56:472052}
2053
2054// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:582055void AutomationProvider::SetPrefs(Browser* browser,
2056 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472057 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432058 bool reply_return = true;
2059 std::string json_return = "{}";
[email protected]24e2b102010-04-29 17:56:472060 std::wstring path;
2061 Value* val;
2062 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
2063 PrefService* pref_service = profile_->GetPrefs();
2064 const PrefService::Preference* pref =
2065 pref_service->FindPreference(path.c_str());
2066 if (!pref) { // Not a registered pref.
[email protected]f6ff0df2010-07-11 22:41:432067 json_return = JSONErrorString("pref not registered.");
2068 reply_return = false;
[email protected]24e2b102010-04-29 17:56:472069 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
[email protected]f6ff0df2010-07-11 22:41:432070 json_return = JSONErrorString("pref is managed. cannot be changed.");
2071 reply_return = false;
[email protected]24e2b102010-04-29 17:56:472072 } else { // Set the pref.
2073 pref_service->Set(path.c_str(), *val);
2074 }
2075 } else {
[email protected]f6ff0df2010-07-11 22:41:432076 json_return = JSONErrorString("no pref path or value given.");
2077 reply_return = false;
[email protected]24e2b102010-04-29 17:56:472078 }
2079
[email protected]f6ff0df2010-07-11 22:41:432080 AutomationMsg_SendJSONRequest::WriteReplyParams(
2081 reply_message, json_return, reply_return);
2082 Send(reply_message);
[email protected]24e2b102010-04-29 17:56:472083}
2084
[email protected]53329582010-05-14 21:10:582085// Sample json input: { "command": "GetOmniboxInfo" }
2086// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
2087void AutomationProvider::GetOmniboxInfo(Browser* browser,
2088 DictionaryValue* args,
2089 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432090 std::string json_return;
2091 bool reply_return = true;
[email protected]53329582010-05-14 21:10:582092 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2093
2094 LocationBar* loc_bar = browser->window()->GetLocationBar();
2095 AutocompleteEditView* edit_view = loc_bar->location_entry();
2096 AutocompleteEditModel* model = edit_view->model();
2097
2098 // Fill up matches.
2099 ListValue* matches = new ListValue;
2100 const AutocompleteResult& result = model->result();
2101 for (AutocompleteResult::const_iterator i = result.begin();
2102 i != result.end(); ++i) {
2103 const AutocompleteMatch& match = *i;
2104 DictionaryValue* item = new DictionaryValue; // owned by return_value
2105 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
2106 item->SetBoolean(L"starred", match.starred);
2107 item->SetString(L"destination_url", match.destination_url.spec());
2108 item->SetString(L"contents", match.contents);
2109 item->SetString(L"description", match.description);
2110 matches->Append(item);
2111 }
2112 return_value->Set(L"matches", matches);
2113
2114 // Fill up other properties.
2115 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2116 properties->SetBoolean(L"has_focus", model->has_focus());
2117 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2118 properties->SetString(L"keyword", model->keyword());
2119 properties->SetString(L"text", edit_view->GetText());
2120 return_value->Set(L"properties", properties);
2121
[email protected]f6ff0df2010-07-11 22:41:432122 base::JSONWriter::Write(return_value.get(), false, &json_return);
2123 AutomationMsg_SendJSONRequest::WriteReplyParams(
2124 reply_message, json_return, reply_return);
2125 Send(reply_message);
[email protected]53329582010-05-14 21:10:582126}
2127
2128// Sample json input: { "command": "SetOmniboxText",
2129// "text": "goog" }
2130void AutomationProvider::SetOmniboxText(Browser* browser,
2131 DictionaryValue* args,
2132 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432133 std::string json_return = "{}";
2134 bool reply_return = true;
[email protected]53329582010-05-14 21:10:582135 std::wstring text;
[email protected]f6ff0df2010-07-11 22:41:432136
[email protected]53329582010-05-14 21:10:582137 if (!args->GetString(L"text", &text)) {
[email protected]f6ff0df2010-07-11 22:41:432138 json_return = JSONErrorString("text missing");
2139 reply_return = false;
[email protected]53329582010-05-14 21:10:582140 } else {
2141 browser->FocusLocationBar();
2142 LocationBar* loc_bar = browser->window()->GetLocationBar();
2143 AutocompleteEditView* edit_view = loc_bar->location_entry();
2144 edit_view->model()->OnSetFocus(false);
2145 edit_view->SetUserText(text);
2146 }
2147
[email protected]f6ff0df2010-07-11 22:41:432148 AutomationMsg_SendJSONRequest::WriteReplyParams(
2149 reply_message, json_return, reply_return);
2150 Send(reply_message);
[email protected]53329582010-05-14 21:10:582151}
2152
2153// Sample json input: { "command": "OmniboxMovePopupSelection",
2154// "count": 1 }
2155// Negative count implies up, positive implies down. Count values will be
2156// capped by the size of the popup list.
2157void AutomationProvider::OmniboxMovePopupSelection(
2158 Browser* browser,
2159 DictionaryValue* args,
2160 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432161 std::string json_return = "{}";
2162 bool reply_return = true;
[email protected]53329582010-05-14 21:10:582163 int count;
[email protected]f6ff0df2010-07-11 22:41:432164
[email protected]53329582010-05-14 21:10:582165 if (!args->GetInteger(L"count", &count)) {
[email protected]f6ff0df2010-07-11 22:41:432166 json_return = JSONErrorString("count missing");
2167 reply_return = false;
[email protected]53329582010-05-14 21:10:582168 } else {
2169 LocationBar* loc_bar = browser->window()->GetLocationBar();
2170 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2171 model->OnUpOrDownKeyPressed(count);
2172 }
2173
[email protected]f6ff0df2010-07-11 22:41:432174 AutomationMsg_SendJSONRequest::WriteReplyParams(
2175 reply_message, json_return, reply_return);
2176 Send(reply_message);
[email protected]53329582010-05-14 21:10:582177}
2178
2179// Sample json input: { "command": "OmniboxAcceptInput" }
2180void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2181 DictionaryValue* args,
2182 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282183 NavigationController& controller =
2184 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122185 // Setup observer to wait until the selected item loads.
2186 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282187 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122188 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582189
2190 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582191}
2192
[email protected]a3cd5022010-06-16 18:25:292193// Sample json input: { "command": "GetInitialLoadTimes" }
2194// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2195void AutomationProvider::GetInitialLoadTimes(
2196 Browser*,
2197 DictionaryValue*,
2198 IPC::Message* reply_message) {
2199 scoped_ptr<DictionaryValue> return_value(
2200 initial_load_observer_->GetTimingInformation());
[email protected]f6ff0df2010-07-11 22:41:432201
2202 std::string json_return;
2203 base::JSONWriter::Write(return_value.get(), false, &json_return);
2204 AutomationMsg_SendJSONRequest::WriteReplyParams(
2205 reply_message, json_return, true);
2206 Send(reply_message);
[email protected]a3cd5022010-06-16 18:25:292207}
2208
[email protected]f7d48012010-05-06 08:17:052209// Sample json input: { "command": "GetPluginsInfo" }
2210// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582211void AutomationProvider::GetPluginsInfo(Browser* browser,
2212 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052213 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432214 std::string json_return;
2215 bool reply_return = true;
2216
[email protected]f7d48012010-05-06 08:17:052217 std::vector<WebPluginInfo> plugins;
2218 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2219 ListValue* items = new ListValue;
2220 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2221 it != plugins.end();
2222 ++it) {
2223 DictionaryValue* item = new DictionaryValue;
[email protected]c9d811372010-06-23 21:44:572224 item->SetStringFromUTF16(L"name", it->name);
[email protected]f7d48012010-05-06 08:17:052225 item->SetString(L"path", it->path.value());
[email protected]c9d811372010-06-23 21:44:572226 item->SetStringFromUTF16(L"version", it->version);
2227 item->SetStringFromUTF16(L"desc", it->desc);
[email protected]f7d48012010-05-06 08:17:052228 item->SetBoolean(L"enabled", it->enabled);
2229 // Add info about mime types.
2230 ListValue* mime_types = new ListValue();
2231 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2232 it->mime_types.begin();
2233 type_it != it->mime_types.end();
2234 ++type_it) {
2235 DictionaryValue* mime_type = new DictionaryValue();
2236 mime_type->SetString(L"mimeType", type_it->mime_type);
[email protected]c9d811372010-06-23 21:44:572237 mime_type->SetStringFromUTF16(L"description", type_it->description);
[email protected]f7d48012010-05-06 08:17:052238
2239 ListValue* file_extensions = new ListValue();
2240 for (std::vector<std::string>::const_iterator ext_it =
2241 type_it->file_extensions.begin();
2242 ext_it != type_it->file_extensions.end();
2243 ++ext_it) {
2244 file_extensions->Append(new StringValue(*ext_it));
2245 }
2246 mime_type->Set(L"fileExtensions", file_extensions);
2247
2248 mime_types->Append(mime_type);
2249 }
2250 item->Set(L"mimeTypes", mime_types);
2251 items->Append(item);
2252 }
2253 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2254 return_value->Set(L"plugins", items); // return_value owns items.
2255
[email protected]f6ff0df2010-07-11 22:41:432256 base::JSONWriter::Write(return_value.get(), false, &json_return);
2257 AutomationMsg_SendJSONRequest::WriteReplyParams(
2258 reply_message, json_return, reply_return);
2259 Send(reply_message);
[email protected]f7d48012010-05-06 08:17:052260}
2261
2262// Sample json input:
2263// { "command": "EnablePlugin",
2264// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582265void AutomationProvider::EnablePlugin(Browser* browser,
2266 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052267 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432268 std::string json_return = "{}";
2269 bool reply_return = true;
[email protected]f7d48012010-05-06 08:17:052270 FilePath::StringType path;
[email protected]f6ff0df2010-07-11 22:41:432271 if (!args->GetString(L"path", &path)) {
2272 json_return = JSONErrorString("path not specified.");
2273 reply_return = false;
2274 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
2275 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2276 " for path %s.\"}", path.c_str());
2277 reply_return = false;
2278 }
2279
2280 AutomationMsg_SendJSONRequest::WriteReplyParams(
2281 reply_message, json_return, reply_return);
2282 Send(reply_message);
[email protected]f7d48012010-05-06 08:17:052283}
2284
2285// Sample json input:
2286// { "command": "DisablePlugin",
2287// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582288void AutomationProvider::DisablePlugin(Browser* browser,
2289 DictionaryValue* args,
2290 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432291 std::string json_return = "{}";
2292 bool reply_return = true;
[email protected]f7d48012010-05-06 08:17:052293 FilePath::StringType path;
[email protected]f6ff0df2010-07-11 22:41:432294 if (!args->GetString(L"path", &path)) {
2295 json_return = JSONErrorString("path not specified.");
2296 reply_return = false;
2297 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
2298 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2299 " for path %s.\"}", path.c_str());
2300 reply_return = false;
2301 }
2302
2303 AutomationMsg_SendJSONRequest::WriteReplyParams(
2304 reply_message, json_return, reply_return);
2305 Send(reply_message);
[email protected]f7d48012010-05-06 08:17:052306}
2307
[email protected]7060bb292010-06-24 00:52:492308// Sample json input:
2309// { "command": "SaveTabContents",
2310// "tab_index": 0,
2311// "filename": <a full pathname> }
2312// Sample json output:
2313// {}
2314void AutomationProvider::SaveTabContents(Browser* browser,
2315 DictionaryValue* args,
2316 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432317 std::string json_return;
[email protected]7060bb292010-06-24 00:52:492318 int tab_index = 0;
2319 FilePath::StringType filename;
2320 FilePath::StringType parent_directory;
2321 TabContents* tab_contents = NULL;
2322
2323 if (!args->GetInteger(L"tab_index", &tab_index) ||
2324 !args->GetString(L"filename", &filename)) {
[email protected]f6ff0df2010-07-11 22:41:432325 json_return = JSONErrorString("tab_index or filename param missing");
[email protected]7060bb292010-06-24 00:52:492326 } else {
2327 tab_contents = browser->GetTabContentsAt(tab_index);
2328 if (!tab_contents) {
[email protected]f6ff0df2010-07-11 22:41:432329 json_return = JSONErrorString("no tab at tab_index");
2330 }
2331 }
2332 if (tab_contents) {
2333 // We're doing a SAVE_AS_ONLY_HTML so the the directory path isn't
2334 // used. Nevertheless, SavePackage requires it be valid. Sigh.
2335 parent_directory = FilePath(filename).DirName().value();
2336 if (!tab_contents->SavePage(FilePath(filename), FilePath(parent_directory),
2337 SavePackage::SAVE_AS_ONLY_HTML)) {
2338 json_return = JSONErrorString("Could not initiate SavePage");
2339 } else {
2340 // The observer will delete itself when done.
2341 new SavePackageNotificationObserver(tab_contents->save_package(),
2342 this, reply_message);
[email protected]7060bb292010-06-24 00:52:492343 return;
2344 }
2345 }
[email protected]f6ff0df2010-07-11 22:41:432346
2347 // If we get here, error.
2348 DCHECK(!json_return.empty());
2349 AutomationMsg_SendJSONRequest::WriteReplyParams(
2350 reply_message, json_return, false);
2351 Send(reply_message);
[email protected]7060bb292010-06-24 00:52:492352}
2353
[email protected]c5aa5322010-07-15 19:00:072354// Refer to ImportSettings() in chrome/test/pyautolib/pyauto.py for sample
2355// json input.
2356// Sample json output: "{}"
2357void AutomationProvider::ImportSettings(Browser* browser,
2358 DictionaryValue* args,
2359 IPC::Message* reply_message) {
2360 std::string json_return = "{}";
2361
2362 // Map from the json string passed over to the import item masks.
2363 std::map<std::string, ImportItem> string_to_import_item;
2364 string_to_import_item["HISTORY"] = importer::HISTORY;
2365 string_to_import_item["FAVORITES"] = importer::FAVORITES;
2366 string_to_import_item["COOKIES"] = importer::COOKIES;
2367 string_to_import_item["PASSWORDS"] = importer::PASSWORDS;
2368 string_to_import_item["SEARCH_ENGINES"] = importer::SEARCH_ENGINES;
2369 string_to_import_item["HOME_PAGE"] = importer::HOME_PAGE;
2370 string_to_import_item["ALL"] = importer::ALL;
2371
2372 std::wstring browser_name;
2373 int import_items = 0;
2374 ListValue* import_items_list = NULL;
2375 bool first_run;
2376
2377 if (!args->GetString(L"import_from", &browser_name) ||
2378 !args->GetBoolean(L"first_run", &first_run) ||
2379 !args->GetList(L"import_items", &import_items_list)) {
2380 std::string json_return =
2381 JSONErrorString("Incorrect type for one or more of the arguments.");
2382 AutomationMsg_SendJSONRequest::WriteReplyParams(
2383 reply_message, json_return, false);
2384 Send(reply_message);
2385 return;
2386 }
2387
2388 int num_items = import_items_list->GetSize();
2389 for (int i = 0; i < num_items; i++) {
2390 std::string item;
2391 import_items_list->GetString(i, &item);
2392 // If the provided string is not part of the map, error out.
2393 if (!ContainsKey(string_to_import_item, item)) {
2394 json_return =
2395 JSONErrorString("Invalid item string found in import_items.");
2396 AutomationMsg_SendJSONRequest::WriteReplyParams(
2397 reply_message, json_return, false);
2398 Send(reply_message);
2399 return;
2400 }
2401 import_items |= string_to_import_item[item];
2402 }
2403
2404 ImporterHost* importer_host = new ImporterHost();
2405 // Get the correct ProfileInfo based on the browser they user provided.
2406 importer::ProfileInfo profile_info;
2407 int num_browsers = importer_host->GetAvailableProfileCount();
2408 int i = 0;
2409 for ( ; i < num_browsers; i++) {
2410 std::wstring name = importer_host->GetSourceProfileNameAt(i);
2411 if (name == browser_name) {
2412 profile_info = importer_host->GetSourceProfileInfoAt(i);
2413 break;
2414 }
2415 }
2416 // If we made it to the end of the loop, then the input was bad.
2417 if (i == num_browsers) {
2418 json_return =
2419 JSONErrorString("Invalid browser name string found.");
2420 AutomationMsg_SendJSONRequest::WriteReplyParams(
2421 reply_message, json_return, false);
2422 Send(reply_message);
2423 return;
2424 }
2425
2426 Profile* profile = browser->profile();
2427
2428 importer_host->SetObserver(
2429 new AutomationProviderImportSettingsObserver(this, reply_message));
2430 importer_host->StartImportSettings(profile_info, profile, import_items,
2431 new ProfileWriter(profile), first_run);
2432}
2433
[email protected]a0fc50d72010-07-14 21:14:192434// Refer to ClearBrowsingData() in chrome/test/pyautolib/pyauto.py for sample
2435// json input.
2436// Sample json output: {}
2437void AutomationProvider::ClearBrowsingData(Browser* browser,
2438 DictionaryValue* args,
2439 IPC::Message* reply_message) {
2440 std::string json_return = "{}";
2441
2442 std::map<std::string, BrowsingDataRemover::TimePeriod> string_to_time_period;
2443 string_to_time_period["LAST_HOUR"] = BrowsingDataRemover::LAST_HOUR;
2444 string_to_time_period["LAST_DAY"] = BrowsingDataRemover::LAST_DAY;
2445 string_to_time_period["LAST_WEEK"] = BrowsingDataRemover::LAST_WEEK;
2446 string_to_time_period["FOUR_WEEKS"] = BrowsingDataRemover::FOUR_WEEKS;
2447 string_to_time_period["EVERYTHING"] = BrowsingDataRemover::EVERYTHING;
2448
2449 std::map<std::string, int> string_to_mask_value;
2450 string_to_mask_value["HISTORY"] = BrowsingDataRemover::REMOVE_HISTORY;
2451 string_to_mask_value["DOWNLOADS"] = BrowsingDataRemover::REMOVE_DOWNLOADS;
2452 string_to_mask_value["COOKIES"] = BrowsingDataRemover::REMOVE_COOKIES;
2453 string_to_mask_value["PASSWORDS"] = BrowsingDataRemover::REMOVE_PASSWORDS;
2454 string_to_mask_value["FORM_DATA"] = BrowsingDataRemover::REMOVE_FORM_DATA;
2455 string_to_mask_value["CACHE"] = BrowsingDataRemover::REMOVE_CACHE;
2456
2457 std::string time_period;
2458 ListValue* to_remove;
2459 if (!args->GetString(L"time_period", &time_period) ||
2460 !args->GetList(L"to_remove", &to_remove)) {
2461 // TODO(nirnimesh): Here and below refactor returns with pending CL.
2462 std::string json_return =
2463 JSONErrorString("time_period must be a string and to_remove a list.");
2464 AutomationMsg_SendJSONRequest::WriteReplyParams(
2465 reply_message, json_return, false);
2466 Send(reply_message);
2467 return;
2468 }
2469
2470 int remove_mask = 0;
2471 int num_removals = to_remove->GetSize();
2472 for (int i = 0; i < num_removals; i++) {
2473 std::string removal;
2474 to_remove->GetString(i, &removal);
2475 // If the provided string is not part of the map, then error out.
2476 if (!ContainsKey(string_to_mask_value, removal)) {
2477 std::string json_return =
2478 JSONErrorString("Invalid browsing data string found in to_remove.");
2479 AutomationMsg_SendJSONRequest::WriteReplyParams(
2480 reply_message, json_return, false);
2481 Send(reply_message);
2482 return;
2483 }
2484 remove_mask |= string_to_mask_value[removal];
2485 }
2486
2487 if (!ContainsKey(string_to_time_period, time_period)) {
2488 std::string json_return =
2489 JSONErrorString("Invalid string for time_period.");
2490 AutomationMsg_SendJSONRequest::WriteReplyParams(
2491 reply_message, json_return, false);
2492 Send(reply_message);
2493 return;
2494 }
2495
2496 BrowsingDataRemover* remover = new BrowsingDataRemover(
2497 profile(), string_to_time_period[time_period], base::Time());
2498
2499 remover->AddObserver(
2500 new AutomationProviderBrowsingDataObserver(this, reply_message));
2501 remover->Remove(remove_mask);
2502 // BrowsingDataRemover deletes itself using DeleteTask.
2503 // The observer also deletes itself after sending the reply.
2504}
2505
[email protected]4d1929f12010-07-10 00:09:032506// Sample json input: { "command": "GetThemeInfo" }
2507// Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output.
2508void AutomationProvider::GetThemeInfo(Browser* browser,
2509 DictionaryValue* args,
2510 IPC::Message* reply_message) {
2511 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2512 Extension* theme = browser->profile()->GetTheme();
2513 if (theme) {
2514 return_value->SetString(L"name", theme->name());
2515 return_value->Set(L"images", theme->GetThemeImages()->DeepCopy());
2516 return_value->Set(L"colors", theme->GetThemeColors()->DeepCopy());
2517 return_value->Set(L"tints", theme->GetThemeTints()->DeepCopy());
2518 }
[email protected]f6ff0df2010-07-11 22:41:432519
2520 std::string json_return;
2521 base::JSONWriter::Write(return_value.get(), false, &json_return);
2522 AutomationMsg_SendJSONRequest::WriteReplyParams(
2523 reply_message, json_return, true);
2524 Send(reply_message);
[email protected]4d1929f12010-07-10 00:09:032525}
2526
[email protected]55846ad842010-07-09 18:22:562527// Sample json input:
2528// { "command": "GetAutoFillProfile" }
2529// Refer to GetAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2530// json output.
2531void AutomationProvider::GetAutoFillProfile(Browser* browser,
2532 DictionaryValue* args,
2533 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432534 std::string json_return;
2535 bool reply_return = true;
2536
[email protected]55846ad842010-07-09 18:22:562537 // Get the AutoFillProfiles currently in the database.
2538 int tab_index = 0;
2539 args->GetInteger(L"tab_index", &tab_index);
2540 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2541
2542 if (tab_contents) {
2543 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2544 ->GetPersonalDataManager();
2545 if (pdm) {
2546 std::vector<AutoFillProfile*> autofill_profiles = pdm->profiles();
2547 std::vector<CreditCard*> credit_cards = pdm->credit_cards();
2548
2549 ListValue* profiles = GetListFromAutoFillProfiles(autofill_profiles);
2550 ListValue* cards = GetListFromCreditCards(credit_cards);
2551
2552 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2553
2554 return_value->Set(L"profiles", profiles);
2555 return_value->Set(L"credit_cards", cards);
[email protected]f6ff0df2010-07-11 22:41:432556
2557 base::JSONWriter::Write(return_value.get(), false, &json_return);
[email protected]55846ad842010-07-09 18:22:562558 } else {
[email protected]f6ff0df2010-07-11 22:41:432559 json_return = JSONErrorString("No PersonalDataManager.");
2560 reply_return = false;
[email protected]55846ad842010-07-09 18:22:562561 }
2562 } else {
[email protected]f6ff0df2010-07-11 22:41:432563 json_return = JSONErrorString("No tab at that index.");
2564 reply_return = false;
[email protected]55846ad842010-07-09 18:22:562565 }
[email protected]f6ff0df2010-07-11 22:41:432566
2567 AutomationMsg_SendJSONRequest::WriteReplyParams(reply_message, json_return,
2568 reply_return);
2569 Send(reply_message);
[email protected]55846ad842010-07-09 18:22:562570}
2571
2572// Refer to FillAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2573// json input.
2574// Sample json output: {}
2575void AutomationProvider::FillAutoFillProfile(Browser* browser,
2576 DictionaryValue* args,
2577 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432578 std::string json_return = "{}";
2579 bool reply_return = true;
2580
[email protected]55846ad842010-07-09 18:22:562581 ListValue* profiles = NULL;
2582 ListValue* cards = NULL;
2583 args->GetList(L"profiles", &profiles);
2584 args->GetList(L"credit_cards", &cards);
2585
2586 std::vector<AutoFillProfile> autofill_profiles;
2587 std::vector<CreditCard> credit_cards;
2588 // Create an AutoFillProfile for each of the dictionary profiles.
2589 if (profiles) {
[email protected]f6ff0df2010-07-11 22:41:432590 autofill_profiles = GetAutoFillProfilesFromList(*profiles, &json_return);
[email protected]55846ad842010-07-09 18:22:562591 }
2592 // Create a CreditCard for each of the dictionary values.
2593 if (cards) {
[email protected]f6ff0df2010-07-11 22:41:432594 credit_cards = GetCreditCardsFromList(*cards, &json_return);
[email protected]55846ad842010-07-09 18:22:562595 }
2596
2597 // Save the AutoFillProfiles.
2598 int tab_index = 0;
2599 args->GetInteger(L"tab_index", &tab_index);
2600 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2601
2602 if (tab_contents) {
2603 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2604 ->GetPersonalDataManager();
2605 if (pdm) {
2606 pdm->OnAutoFillDialogApply(profiles? &autofill_profiles : NULL,
2607 cards? &credit_cards : NULL);
2608 } else {
[email protected]f6ff0df2010-07-11 22:41:432609 json_return = JSONErrorString("No PersonalDataManager.");
2610 reply_return = false;
[email protected]55846ad842010-07-09 18:22:562611 }
2612 } else {
[email protected]f6ff0df2010-07-11 22:41:432613 json_return = JSONErrorString("No tab at that index.");
2614 reply_return = false;
[email protected]55846ad842010-07-09 18:22:562615 }
[email protected]f6ff0df2010-07-11 22:41:432616 AutomationMsg_SendJSONRequest::WriteReplyParams(
2617 reply_message, json_return, reply_return);
2618 Send(reply_message);
[email protected]55846ad842010-07-09 18:22:562619}
2620
2621/* static */
2622ListValue* AutomationProvider::GetListFromAutoFillProfiles(
2623 std::vector<AutoFillProfile*> autofill_profiles) {
2624 ListValue* profiles = new ListValue;
2625
2626 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string
2627 = GetAutoFillFieldToStringMap();
2628
2629 // For each AutoFillProfile, transform it to a dictionary object to return.
2630 for (std::vector<AutoFillProfile*>::iterator it = autofill_profiles.begin();
2631 it != autofill_profiles.end(); ++it) {
2632 AutoFillProfile* profile = *it;
2633 DictionaryValue* profile_info = new DictionaryValue;
2634 profile_info->SetStringFromUTF16(L"label", profile->Label());
2635 // For each of the types, if it has a value, add it to the dictionary.
2636 for (std::map<AutoFillFieldType, std::wstring>::iterator
2637 type_it = autofill_type_to_string.begin();
2638 type_it != autofill_type_to_string.end(); ++type_it) {
2639 string16 value = profile->GetFieldText(AutoFillType(type_it->first));
2640 if (value.length()) { // If there was something stored for that value.
2641 profile_info->SetStringFromUTF16(type_it->second, value);
2642 }
2643 }
2644 profiles->Append(profile_info);
2645 }
2646 return profiles;
2647}
2648
2649/* static */
2650ListValue* AutomationProvider::GetListFromCreditCards(
2651 std::vector<CreditCard*> credit_cards) {
2652 ListValue* cards = new ListValue;
2653
2654 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2655 GetCreditCardFieldToStringMap();
2656
2657 // For each AutoFillProfile, transform it to a dictionary object to return.
2658 for (std::vector<CreditCard*>::iterator it = credit_cards.begin();
2659 it != credit_cards.end(); ++it) {
2660 CreditCard* card = *it;
2661 DictionaryValue* card_info = new DictionaryValue;
2662 card_info->SetStringFromUTF16(L"label", card->Label());
2663 // For each of the types, if it has a value, add it to the dictionary.
2664 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2665 credit_card_type_to_string.begin();
2666 type_it != credit_card_type_to_string.end(); ++type_it) {
2667 string16 value = card->GetFieldText(AutoFillType(type_it->first));
2668 // If there was something stored for that value.
2669 if (value.length()) {
2670 card_info->SetStringFromUTF16(type_it->second, value);
2671 }
2672 }
2673 cards->Append(card_info);
2674 }
2675 return cards;
2676}
2677
2678/* static */
2679std::vector<AutoFillProfile> AutomationProvider::GetAutoFillProfilesFromList(
[email protected]f6ff0df2010-07-11 22:41:432680 const ListValue& profiles, std::string* json_return) {
[email protected]55846ad842010-07-09 18:22:562681 std::vector<AutoFillProfile> autofill_profiles;
2682 DictionaryValue* profile_info = NULL;
2683 string16 profile_label;
2684 string16 current_value;
2685
2686 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string =
2687 GetAutoFillFieldToStringMap();
2688
2689 int num_profiles = profiles.GetSize();
2690 for (int i = 0; i < num_profiles; i++) {
2691 profiles.GetDictionary(i, &profile_info);
2692 profile_info->GetString("label", &profile_label);
2693 // Choose an id of 0 so that a unique id will be created.
2694 AutoFillProfile profile(profile_label, 0);
2695 // Loop through the possible profile types and add those provided.
2696 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2697 autofill_type_to_string.begin();
2698 type_it != autofill_type_to_string.end(); ++type_it) {
2699 if (profile_info->HasKey(type_it->second)) {
2700 if (profile_info->GetStringAsUTF16(type_it->second, &current_value)) {
2701 profile.SetInfo(AutoFillType(type_it->first), current_value);
2702 } else {
[email protected]f6ff0df2010-07-11 22:41:432703 *json_return = JSONErrorString("All values must be strings");
[email protected]55846ad842010-07-09 18:22:562704 break;
2705 }
2706 }
2707 }
2708 autofill_profiles.push_back(profile);
2709 }
2710 return autofill_profiles;
2711}
2712
2713/* static */
2714std::vector<CreditCard> AutomationProvider::GetCreditCardsFromList(
[email protected]f6ff0df2010-07-11 22:41:432715 const ListValue& cards, std::string* json_return) {
[email protected]55846ad842010-07-09 18:22:562716 std::vector<CreditCard> credit_cards;
2717 DictionaryValue* card_info = NULL;
2718 string16 card_label;
2719 string16 current_value;
2720
2721 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2722 GetCreditCardFieldToStringMap();
2723
2724 int num_credit_cards = cards.GetSize();
2725 for (int i = 0; i < num_credit_cards; i++) {
2726 cards.GetDictionary(i, &card_info);
2727 card_info->GetString("label", &card_label);
2728 CreditCard card(card_label, 0);
2729 // Loop through the possible credit card fields and add those provided.
2730 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2731 credit_card_type_to_string.begin();
2732 type_it != credit_card_type_to_string.end(); ++type_it) {
2733 if (card_info->HasKey(type_it->second)) {
2734 if (card_info->GetStringAsUTF16(type_it->second, &current_value)) {
2735 card.SetInfo(AutoFillType(type_it->first), current_value);
2736 } else {
[email protected]f6ff0df2010-07-11 22:41:432737 *json_return = JSONErrorString("All values must be strings");
[email protected]55846ad842010-07-09 18:22:562738 break;
2739 }
2740 }
2741 }
2742 credit_cards.push_back(card);
2743 }
2744 return credit_cards;
2745}
2746
2747/* static */
2748std::map<AutoFillFieldType, std::wstring>
2749 AutomationProvider::GetAutoFillFieldToStringMap() {
2750 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string;
2751 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2752 autofill_type_to_string[NAME_MIDDLE] = L"NAME_MIDDLE";
2753 autofill_type_to_string[NAME_LAST] = L"NAME_LAST";
2754 autofill_type_to_string[COMPANY_NAME] = L"COMPANY_NAME";
2755 autofill_type_to_string[EMAIL_ADDRESS] = L"EMAIL_ADDRESS";
2756 autofill_type_to_string[ADDRESS_HOME_LINE1] = L"ADDRESS_HOME_LINE1";
2757 autofill_type_to_string[ADDRESS_HOME_LINE2] = L"ADDRESS_HOME_LINE2";
2758 autofill_type_to_string[ADDRESS_HOME_CITY] = L"ADDRESS_HOME_CITY";
2759 autofill_type_to_string[ADDRESS_HOME_STATE] = L"ADDRESS_HOME_STATE";
2760 autofill_type_to_string[ADDRESS_HOME_ZIP] = L"ADDRESS_HOME_ZIP";
2761 autofill_type_to_string[ADDRESS_HOME_COUNTRY] = L"ADDRESS_HOME_COUNTRY";
2762 autofill_type_to_string[PHONE_HOME_NUMBER] = L"PHONE_HOME_NUMBER";
2763 autofill_type_to_string[PHONE_FAX_NUMBER] = L"PHONE_FAX_NUMBER";
2764 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2765 return autofill_type_to_string;
2766}
2767
2768/* static */
2769std::map<AutoFillFieldType, std::wstring>
2770 AutomationProvider::GetCreditCardFieldToStringMap() {
2771 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string;
2772 credit_card_type_to_string[CREDIT_CARD_NAME] = L"CREDIT_CARD_NAME";
2773 credit_card_type_to_string[CREDIT_CARD_NUMBER] = L"CREDIT_CARD_NUMBER";
2774 credit_card_type_to_string[CREDIT_CARD_TYPE] = L"CREDIT_CARD_TYPE";
2775 credit_card_type_to_string[CREDIT_CARD_EXP_MONTH] = L"CREDIT_CARD_EXP_MONTH";
2776 credit_card_type_to_string[CREDIT_CARD_EXP_4_DIGIT_YEAR] =
2777 L"CREDIT_CARD_EXP_4_DIGIT_YEAR";
2778 return credit_card_type_to_string;
2779}
2780
[email protected]f6ff0df2010-07-11 22:41:432781/* static */
2782std::string AutomationProvider::JSONErrorString(const std::string& err) {
2783 std::string prefix = "{\"error\": \"";
2784 std::string no_quote_err;
2785 std::string suffix = "\"}";
2786
2787 base::JsonDoubleQuote(err, false, &no_quote_err);
2788 return prefix + no_quote_err + suffix;
2789}
2790
[email protected]53329582010-05-14 21:10:582791void AutomationProvider::SendJSONRequest(int handle,
2792 std::string json_request,
2793 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:042794 Browser* browser = NULL;
[email protected]f6ff0df2010-07-11 22:41:432795 std::string error_string;
[email protected]59a611242010-04-02 02:24:042796 scoped_ptr<Value> values;
2797
2798 // Basic error checking.
2799 if (browser_tracker_->ContainsHandle(handle)) {
2800 browser = browser_tracker_->GetResource(handle);
2801 }
2802 if (!browser) {
[email protected]f6ff0df2010-07-11 22:41:432803 error_string = "no browser object";
[email protected]59a611242010-04-02 02:24:042804 } else {
2805 base::JSONReader reader;
2806 std::string error;
[email protected]ba399672010-04-06 15:42:392807 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
[email protected]59a611242010-04-02 02:24:042808 if (!error.empty()) {
[email protected]f6ff0df2010-07-11 22:41:432809 error_string = error;
[email protected]59a611242010-04-02 02:24:042810 }
2811 }
2812
2813 // Make sure input is a dict with a string command.
2814 std::string command;
2815 DictionaryValue* dict_value = NULL;
[email protected]f6ff0df2010-07-11 22:41:432816 if (error_string.empty()) {
2817 if (values->GetType() != Value::TYPE_DICTIONARY) {
2818 error_string = "not a dict or no command key in dict";
2819 } else {
2820 // Ownership remains with "values" variable.
2821 dict_value = static_cast<DictionaryValue*>(values.get());
2822 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
2823 error_string = "no command key in dict or not a string command";
2824 }
[email protected]59a611242010-04-02 02:24:042825 }
2826 }
2827
[email protected]24e2b102010-04-29 17:56:472828 // Map json commands to their handlers.
2829 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:052830 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
2831 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
2832 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
2833
[email protected]a9ff2c02010-05-13 17:33:052834 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
[email protected]24e2b102010-04-29 17:56:472835 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:292836 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:052837
[email protected]53329582010-05-14 21:10:582838 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
2839 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
2840 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
2841 handler_map["OmniboxMovePopupSelection"] =
2842 &AutomationProvider::OmniboxMovePopupSelection;
2843
[email protected]24e2b102010-04-29 17:56:472844 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
2845 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:052846
[email protected]ef413ca2010-05-25 21:09:142847 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
2848
[email protected]f7d48012010-05-06 08:17:052849 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:472850 handler_map["WaitForAllDownloadsToComplete"] =
2851 &AutomationProvider::WaitForDownloadsToComplete;
2852
[email protected]a3cd5022010-06-16 18:25:292853 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
2854
[email protected]7060bb292010-06-24 00:52:492855 handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents;
2856
[email protected]c5aa5322010-07-15 19:00:072857 handler_map["ImportSettings"] = &AutomationProvider::ImportSettings;
2858
[email protected]a0fc50d72010-07-14 21:14:192859 handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData;
2860
[email protected]4d1929f12010-07-10 00:09:032861 // SetTheme() implemented using InstallExtension().
2862 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo;
2863
[email protected]55846ad842010-07-09 18:22:562864 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile;
2865 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile;
2866
[email protected]f6ff0df2010-07-11 22:41:432867 if (error_string.empty()) {
2868 if (handler_map.find(std::string(command)) != handler_map.end()) {
2869 (this->*handler_map[command])(browser, dict_value, reply_message);
2870 return;
2871 } else {
2872 error_string = "Unknown command. Options: ";
2873 for (std::map<std::string, JsonHandler>::const_iterator it =
2874 handler_map.begin(); it != handler_map.end(); ++it) {
2875 error_string += it->first + ", ";
2876 }
[email protected]d1c458a22010-07-11 06:48:402877 }
[email protected]59a611242010-04-02 02:24:042878 }
[email protected]f6ff0df2010-07-11 22:41:432879
2880 // If we hit an error, return info.
2881 // Return a dict of {"error", "descriptive_string_for_error"}.
2882 // Else return an empty dict.
2883 std::string json_string;
2884 bool success = true;
2885 if (!error_string.empty()) {
2886 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
2887 dict->SetString(L"error", error_string);
2888 base::JSONWriter::Write(dict.get(), false, &json_string);
2889 success = false;
2890 } else {
2891 json_string = "{}";
2892 }
2893 AutomationMsg_SendJSONRequest::WriteReplyParams(
2894 reply_message, json_string, success);
2895 Send(reply_message);
[email protected]59a611242010-04-02 02:24:042896}
2897
initial.commit09911bf2008-07-26 23:55:292898void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562899 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342900 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2901 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562902 DCHECK(reply_message_ == NULL);
2903 reply_message_ = reply_message;
2904
[email protected]d9f9b792009-06-24 13:17:122905 DevToolsManager::GetInstance()->InspectElement(
2906 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292907 } else {
[email protected]71f65dd2009-02-11 19:14:562908 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2909 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292910 }
2911}
2912
2913void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092914 if (reply_message_) {
2915 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2916 num_resources);
2917 Send(reply_message_);
2918 reply_message_ = NULL;
2919 }
initial.commit09911bf2008-07-26 23:55:292920}
2921
[email protected]a7eee32f2009-05-22 18:08:172922class SetProxyConfigTask : public Task {
2923 public:
[email protected]be180c802009-10-23 06:33:312924 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
2925 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:252926 : request_context_getter_(request_context_getter),
2927 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:172928 virtual void Run() {
2929 // First, deserialize the JSON string. If this fails, log and bail.
2930 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:392931 std::string error_msg;
2932 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:172933 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2934 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:392935 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:172936 return;
2937 }
2938
2939 scoped_ptr<DictionaryValue> dict(
2940 static_cast<DictionaryValue*>(root.release()));
2941 // Now put together a proxy configuration from the deserialized string.
2942 net::ProxyConfig pc;
2943 PopulateProxyConfig(*dict.get(), &pc);
2944
[email protected]be180c802009-10-23 06:33:312945 net::ProxyService* proxy_service =
2946 request_context_getter_->GetURLRequestContext()->proxy_service();
2947 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:172948 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2949 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:312950 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:172951 }
2952
2953 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2954 DCHECK(pc);
2955 bool no_proxy = false;
2956 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2957 // Make no changes to the ProxyConfig.
2958 return;
2959 }
2960 bool auto_config;
2961 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:482962 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:172963 }
2964 std::string pac_url;
2965 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:482966 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:172967 }
2968 std::string proxy_bypass_list;
2969 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:482970 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:172971 }
2972 std::string proxy_server;
2973 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:482974 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:172975 }
2976 }
2977
2978 private:
[email protected]be180c802009-10-23 06:33:312979 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:172980 std::string proxy_config_;
2981};
2982
2983
2984void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:312985 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
2986 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:172987 FilePath user_data_dir;
2988 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2989 ProfileManager* profile_manager = g_browser_process->profile_manager();
2990 DCHECK(profile_manager);
2991 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2992 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:312993 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:172994 }
[email protected]be180c802009-10-23 06:33:312995 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:172996
[email protected]fae20792009-10-28 20:31:582997 ChromeThread::PostTask(
2998 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:312999 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:173000}
3001
[email protected]4f3dc372009-02-24 00:10:293002void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:333003 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:293004 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:293005 if (tab_tracker_->ContainsHandle(handle)) {
3006 NavigationController* tab = tab_tracker_->GetResource(handle);
3007 DownloadManager* dlm = tab->profile()->GetDownloadManager();
3008 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:333009 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:293010 }
initial.commit09911bf2008-07-26 23:55:293011}
3012
[email protected]6a5670d22009-10-27 16:21:343013void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:143014 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:533015 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
3016 reply_message);
3017}
3018
3019void AutomationProvider::OpenNewBrowserWindowOfType(
3020 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:143021 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:293022 // We may have no current browser windows open so don't rely on
3023 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:533024 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
3025 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:053026 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:153027 if (show)
[email protected]15952e462008-11-14 00:29:053028 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:293029}
3030
[email protected]71f65dd2009-02-11 19:14:563031void AutomationProvider::GetWindowForBrowser(int browser_handle,
3032 bool* success,
3033 int* handle) {
3034 *success = false;
3035 *handle = 0;
initial.commit09911bf2008-07-26 23:55:293036
3037 if (browser_tracker_->ContainsHandle(browser_handle)) {
3038 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:203039 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:293040 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:203041 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:563042 *success = true;
initial.commit09911bf2008-07-26 23:55:293043 }
initial.commit09911bf2008-07-26 23:55:293044}
3045
3046void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:563047 int browser_handle,
3048 bool* success,
3049 int* autocomplete_edit_handle) {
3050 *success = false;
3051 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:293052
3053 if (browser_tracker_->ContainsHandle(browser_handle)) {
3054 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:063055 LocationBar* loc_bar = browser->window()->GetLocationBar();
3056 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:293057 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:563058 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
3059 *success = true;
initial.commit09911bf2008-07-26 23:55:293060 }
initial.commit09911bf2008-07-26 23:55:293061}
initial.commit09911bf2008-07-26 23:55:293062
[email protected]71f65dd2009-02-11 19:14:563063void AutomationProvider::ShowInterstitialPage(int tab_handle,
3064 const std::string& html_text,
3065 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293066 if (tab_tracker_->ContainsHandle(tab_handle)) {
3067 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:113068 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:403069
[email protected]7dad3d5f2010-03-04 00:27:013070 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:403071 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:343072 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:403073 GURL("about:interstitial"),
3074 html_text);
3075 interstitial->Show();
3076 return;
initial.commit09911bf2008-07-26 23:55:293077 }
[email protected]71f65dd2009-02-11 19:14:563078
[email protected]457f5cf2009-08-18 16:37:523079 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
3080 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563081 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293082}
3083
[email protected]71f65dd2009-02-11 19:14:563084void AutomationProvider::HideInterstitialPage(int tab_handle,
3085 bool* success) {
3086 *success = false;
[email protected]57c6a652009-05-04 07:58:343087 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
3088 if (tab_contents && tab_contents->interstitial_page()) {
3089 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:563090 *success = true;
initial.commit09911bf2008-07-26 23:55:293091 }
initial.commit09911bf2008-07-26 23:55:293092}
3093
[email protected]71f65dd2009-02-11 19:14:563094void AutomationProvider::CloseTab(int tab_handle,
3095 bool wait_until_closed,
3096 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293097 if (tab_tracker_->ContainsHandle(tab_handle)) {
3098 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
3099 int index;
3100 Browser* browser = Browser::GetBrowserForController(controller, &index);
3101 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:143102 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:113103 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:453104 return;
initial.commit09911bf2008-07-26 23:55:293105 }
[email protected]de246f52009-02-25 18:25:453106
3107 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:553108 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293109}
3110
[email protected]71f65dd2009-02-11 19:14:563111void AutomationProvider::CloseBrowser(int browser_handle,
3112 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293113 if (browser_tracker_->ContainsHandle(browser_handle)) {
3114 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563115 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:563116 reply_message);
[email protected]f3e99e32008-07-30 04:48:393117 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:293118 } else {
3119 NOTREACHED();
3120 }
3121}
3122
[email protected]71f65dd2009-02-11 19:14:563123void AutomationProvider::CloseBrowserAsync(int browser_handle) {
3124 if (browser_tracker_->ContainsHandle(browser_handle)) {
3125 Browser* browser = browser_tracker_->GetResource(browser_handle);
3126 browser->window()->Close();
3127 } else {
3128 NOTREACHED();
3129 }
3130}
3131
[email protected]71f65dd2009-02-11 19:14:563132void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
3133 IPC::Message* reply_message) {
3134 if (tab_tracker_->ContainsHandle(tab_handle)) {
3135 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3136 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:143137 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:563138 }
3139}
3140
[email protected]71f65dd2009-02-11 19:14:563141void AutomationProvider::GetSecurityState(int handle, bool* success,
3142 SecurityStyle* security_style,
3143 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:483144 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:293145 if (tab_tracker_->ContainsHandle(handle)) {
3146 NavigationController* tab = tab_tracker_->GetResource(handle);
3147 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563148 *success = true;
3149 *security_style = entry->ssl().security_style();
3150 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:483151 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:293152 } else {
[email protected]71f65dd2009-02-11 19:14:563153 *success = false;
3154 *security_style = SECURITY_STYLE_UNKNOWN;
3155 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:483156 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:293157 }
3158}
3159
[email protected]71f65dd2009-02-11 19:14:563160void AutomationProvider::GetPageType(int handle, bool* success,
3161 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:293162 if (tab_tracker_->ContainsHandle(handle)) {
3163 NavigationController* tab = tab_tracker_->GetResource(handle);
3164 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563165 *page_type = entry->page_type();
3166 *success = true;
initial.commit09911bf2008-07-26 23:55:293167 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:343168 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:563169 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:343170 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:563171 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293172 } else {
[email protected]71f65dd2009-02-11 19:14:563173 *success = false;
3174 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293175 }
3176}
3177
[email protected]84abba62009-10-07 17:01:443178void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
3179 int* duration_ms) {
3180 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
3181 event_name);
3182}
3183
[email protected]71f65dd2009-02-11 19:14:563184void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
3185 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293186 if (tab_tracker_->ContainsHandle(handle)) {
3187 NavigationController* tab = tab_tracker_->GetResource(handle);
3188 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:073189 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:403190 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:473191 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:343192 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:293193 if (ssl_blocking_page) {
3194 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:013195 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:563196 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:293197 return;
3198 }
3199 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:523200 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3201 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:563202 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293203 return;
3204 }
3205 }
3206 }
3207 // We failed.
[email protected]457f5cf2009-08-18 16:37:523208 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3209 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563210 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293211}
3212
[email protected]71f65dd2009-02-11 19:14:563213void AutomationProvider::BringBrowserToFront(int browser_handle,
3214 bool* success) {
initial.commit09911bf2008-07-26 23:55:293215 if (browser_tracker_->ContainsHandle(browser_handle)) {
3216 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:063217 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:563218 *success = true;
initial.commit09911bf2008-07-26 23:55:293219 } else {
[email protected]71f65dd2009-02-11 19:14:563220 *success = false;
initial.commit09911bf2008-07-26 23:55:293221 }
3222}
3223
[email protected]71f65dd2009-02-11 19:14:563224void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
3225 int message_num,
3226 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:293227 if (browser_tracker_->ContainsHandle(browser_handle)) {
3228 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563229 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:143230 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:293231 } else {
[email protected]71f65dd2009-02-11 19:14:563232 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:293233 }
3234}
3235
[email protected]71f65dd2009-02-11 19:14:563236void AutomationProvider::PrintNow(int tab_handle,
3237 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:573238 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:343239 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3240 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:293241 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:573242 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:143243 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:343244 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:573245 return;
initial.commit09911bf2008-07-26 23:55:293246 }
[email protected]71f65dd2009-02-11 19:14:563247 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
3248 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293249}
[email protected]d301c952009-07-13 15:02:413250
[email protected]71f65dd2009-02-11 19:14:563251void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:113252 const FilePath& file_name,
3253 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:563254 int type,
3255 bool* success) {
initial.commit09911bf2008-07-26 23:55:293256 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:563257 *success = false;
initial.commit09911bf2008-07-26 23:55:293258 return;
3259 }
3260
3261 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3262 Browser* browser = FindAndActivateTab(nav);
3263 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:143264 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:563265 *success = false;
initial.commit09911bf2008-07-26 23:55:293266 return;
3267 }
3268
initial.commit09911bf2008-07-26 23:55:293269 SavePackage::SavePackageType save_type =
3270 static_cast<SavePackage::SavePackageType>(type);
3271 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
3272 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:343273 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:293274
[email protected]71f65dd2009-02-11 19:14:563275 *success = true;
initial.commit09911bf2008-07-26 23:55:293276}
3277
[email protected]71f65dd2009-02-11 19:14:563278void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
3279 bool* success,
3280 std::wstring* text) {
3281 *success = false;
initial.commit09911bf2008-07-26 23:55:293282 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563283 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:523284 GetText();
[email protected]71f65dd2009-02-11 19:14:563285 *success = true;
initial.commit09911bf2008-07-26 23:55:293286 }
initial.commit09911bf2008-07-26 23:55:293287}
3288
[email protected]71f65dd2009-02-11 19:14:563289void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
3290 const std::wstring& text,
3291 bool* success) {
3292 *success = false;
initial.commit09911bf2008-07-26 23:55:293293 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:523294 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
3295 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:563296 *success = true;
initial.commit09911bf2008-07-26 23:55:293297 }
initial.commit09911bf2008-07-26 23:55:293298}
3299
3300void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:563301 int autocomplete_edit_handle,
3302 bool* success,
3303 std::vector<AutocompleteMatchData>* matches) {
3304 *success = false;
initial.commit09911bf2008-07-26 23:55:293305 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:273306 const AutocompleteResult& result = autocomplete_edit_tracker_->
3307 GetResource(autocomplete_edit_handle)->model()->result();
3308 for (AutocompleteResult::const_iterator i = result.begin();
3309 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:563310 matches->push_back(AutocompleteMatchData(*i));
3311 *success = true;
initial.commit09911bf2008-07-26 23:55:293312 }
initial.commit09911bf2008-07-26 23:55:293313}
3314
3315void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:563316 int autocomplete_edit_handle,
3317 bool* success,
3318 bool* query_in_progress) {
3319 *success = false;
3320 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:293321 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563322 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:523323 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:563324 *success = true;
initial.commit09911bf2008-07-26 23:55:293325 }
initial.commit09911bf2008-07-26 23:55:293326}
3327
[email protected]63514af2010-03-30 17:17:233328#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:193329
[email protected]5ae5bed2009-08-21 18:52:443330#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:393331
[email protected]57c6a652009-05-04 07:58:343332TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:573333 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:573334 if (tab_tracker_->ContainsHandle(handle)) {
3335 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:343336 if (tab)
3337 *tab = nav_controller;
3338 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:573339 }
[email protected]57c6a652009-05-04 07:58:343340 return NULL;
[email protected]20e93d12008-08-28 16:31:573341}
3342
initial.commit09911bf2008-07-26 23:55:293343TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
3344 : AutomationProvider(profile) {
3345 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:143346 registrar_.Add(this, NotificationType::SESSION_END,
3347 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:293348}
3349
3350TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:293351 BrowserList::RemoveObserver(this);
3352}
3353
3354void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:143355 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:293356 AutomationProvider::OnChannelError();
3357}
3358
3359void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
3360 // For backwards compatibility with the testing automation interface, we
3361 // want the automation provider (and hence the process) to go away when the
3362 // last browser goes away.
3363 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:293364 // If you change this, update Observer for NotificationType::SESSION_END
3365 // below.
[email protected]295039bd2008-08-15 04:32:573366 MessageLoop::current()->PostTask(FROM_HERE,
3367 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:293368 }
3369}
3370
3371void TestingAutomationProvider::Observe(NotificationType type,
3372 const NotificationSource& source,
3373 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:563374 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:293375 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
3376 // before the task runs resulting in this object not being deleted. This
3377 // Release balance out the Release scheduled by OnBrowserRemoving.
3378 Release();
3379}
[email protected]295039bd2008-08-15 04:32:573380
3381void TestingAutomationProvider::OnRemoveProvider() {
3382 AutomationProviderList::GetInstance()->RemoveProvider(this);
3383}
[email protected]8a3422c92008-09-24 17:42:423384
[email protected]816633a2009-11-11 21:48:183385void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:563386 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:423387 if (tab_tracker_->ContainsHandle(handle)) {
3388 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:343389 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:113390 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423391 }
[email protected]8a3422c92008-09-24 17:42:423392}
3393
[email protected]816633a2009-11-11 21:48:183394void AutomationProvider::ClickInfoBarAccept(int handle,
3395 int info_bar_index,
3396 bool wait_for_navigation,
3397 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:423398 bool success = false;
3399 if (tab_tracker_->ContainsHandle(handle)) {
3400 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3401 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:113402 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423403 if (info_bar_index >= 0 && info_bar_index < count) {
3404 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:013405 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:423406 }
[email protected]eb9ba192008-12-02 02:41:343407 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:113408 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:343409 info_bar_index);
3410 if (delegate->AsConfirmInfoBarDelegate())
3411 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:423412 success = true;
3413 }
3414 }
[email protected]4f3dc372009-02-24 00:10:293415 }
[email protected]58f622a62009-10-04 01:17:553416
3417 // This "!wait_for_navigation || !success condition" logic looks suspicious.
3418 // It will send a failure message when success is true but
3419 // |wait_for_navigation| is false.
3420 // TODO(phajdan.jr): investgate whether the reply param (currently
3421 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:423422 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:183423 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:523424 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:423425}
3426
[email protected]71f65dd2009-02-11 19:14:563427void AutomationProvider::GetLastNavigationTime(int handle,
3428 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:423429 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:563430 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:423431}
3432
[email protected]71f65dd2009-02-11 19:14:563433void AutomationProvider::WaitForNavigation(int handle,
3434 int64 last_navigation_time,
3435 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:253436 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:423437 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:253438
[email protected]8a3422c92008-09-24 17:42:423439 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:563440 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:523441 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
3442 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:553443 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:293444 return;
[email protected]8a3422c92008-09-24 17:42:423445 }
3446
[email protected]7dad3d5f2010-03-04 00:27:013447 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:423448}
3449
[email protected]71f65dd2009-02-11 19:14:563450void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163451 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563452 int value,
3453 bool* success) {
3454 *success = false;
[email protected]8a3422c92008-09-24 17:42:423455 if (browser_tracker_->ContainsHandle(handle)) {
3456 Browser* browser = browser_tracker_->GetResource(handle);
3457 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563458 *success = true;
[email protected]8a3422c92008-09-24 17:42:423459 }
[email protected]8a3422c92008-09-24 17:42:423460}
[email protected]97fa6ce32008-12-19 01:48:163461
[email protected]71f65dd2009-02-11 19:14:563462void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163463 const std::wstring& name,
[email protected]ddd231e2010-06-29 20:35:193464 const std::string& value,
[email protected]71f65dd2009-02-11 19:14:563465 bool* success) {
3466 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163467 if (browser_tracker_->ContainsHandle(handle)) {
3468 Browser* browser = browser_tracker_->GetResource(handle);
3469 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563470 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163471 }
[email protected]97fa6ce32008-12-19 01:48:163472}
3473
[email protected]71f65dd2009-02-11 19:14:563474void AutomationProvider::GetBooleanPreference(int handle,
3475 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:363476 bool* success,
3477 bool* value) {
[email protected]71f65dd2009-02-11 19:14:563478 *success = false;
3479 *value = false;
[email protected]97fa6ce32008-12-19 01:48:163480 if (browser_tracker_->ContainsHandle(handle)) {
3481 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:563482 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
3483 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163484 }
[email protected]97fa6ce32008-12-19 01:48:163485}
3486
[email protected]71f65dd2009-02-11 19:14:563487void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163488 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563489 bool value,
3490 bool* success) {
3491 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163492 if (browser_tracker_->ContainsHandle(handle)) {
3493 Browser* browser = browser_tracker_->GetResource(handle);
3494 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563495 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163496 }
[email protected]97fa6ce32008-12-19 01:48:163497}
3498
3499// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563500void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:403501 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:163502 if (tab_tracker_->ContainsHandle(tab_handle)) {
3503 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3504 Browser* browser = FindAndActivateTab(nav);
3505 DCHECK(browser);
3506
[email protected]57c6a652009-05-04 07:58:343507 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
3508 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:163509 }
[email protected]97fa6ce32008-12-19 01:48:163510}
3511
[email protected]b8f48d12009-11-09 20:14:363512// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563513void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:403514 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:563515 bool* success) {
3516 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163517 if (tab_tracker_->ContainsHandle(tab_handle)) {
3518 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:193519 if (!nav)
3520 return;
[email protected]97fa6ce32008-12-19 01:48:163521 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:163522
[email protected]2f2afba2010-04-01 01:53:193523 // If the browser has UI, simulate what a user would do.
3524 // Activate the tab and then click the encoding menu.
3525 if (browser &&
3526 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:163527 int selected_encoding_id =
3528 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
3529 if (selected_encoding_id) {
3530 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:563531 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163532 }
[email protected]2f2afba2010-04-01 01:53:193533 } else {
3534 // There is no UI, Chrome probably runs as Chrome-Frame mode.
3535 // Try to get TabContents and call its override_encoding method.
3536 TabContents* contents = nav->tab_contents();
3537 if (!contents)
3538 return;
3539 const std::string selected_encoding =
3540 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
3541 if (selected_encoding.empty())
3542 return;
3543 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:163544 }
3545 }
[email protected]97fa6ce32008-12-19 01:48:163546}
[email protected]5bcdb312009-01-07 21:43:203547
[email protected]4d434a1a2009-02-11 21:06:573548void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:203549 SavePackage::SetShouldPromptUser(should_prompt);
3550}
[email protected]87eab222009-03-13 00:47:453551
[email protected]66ba4932009-06-04 19:22:133552void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
3553 *count = -1; // -1 is the error code
3554 if (tab_tracker_->ContainsHandle(handle)) {
3555 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3556 TabContents* tab_contents = nav_controller->tab_contents();
3557 if (tab_contents) {
3558 BlockedPopupContainer* container =
3559 tab_contents->blocked_popup_container();
3560 if (container) {
3561 *count = static_cast<int>(container->GetBlockedPopupCount());
3562 } else {
3563 // If we don't have a container, we don't have any blocked popups to
3564 // contain!
3565 *count = 0;
3566 }
3567 }
3568 }
3569}
[email protected]f7a68432009-07-29 23:18:193570
3571void AutomationProvider::SelectAll(int tab_handle) {
3572 RenderViewHost* view = GetViewForTab(tab_handle);
3573 if (!view) {
3574 NOTREACHED();
3575 return;
3576 }
3577
3578 view->SelectAll();
3579}
3580
3581void AutomationProvider::Cut(int tab_handle) {
3582 RenderViewHost* view = GetViewForTab(tab_handle);
3583 if (!view) {
3584 NOTREACHED();
3585 return;
3586 }
3587
3588 view->Cut();
3589}
3590
3591void AutomationProvider::Copy(int tab_handle) {
3592 RenderViewHost* view = GetViewForTab(tab_handle);
3593 if (!view) {
3594 NOTREACHED();
3595 return;
3596 }
3597
3598 view->Copy();
3599}
3600
3601void AutomationProvider::Paste(int tab_handle) {
3602 RenderViewHost* view = GetViewForTab(tab_handle);
3603 if (!view) {
3604 NOTREACHED();
3605 return;
3606 }
3607
3608 view->Paste();
3609}
3610
3611void AutomationProvider::ReloadAsync(int tab_handle) {
3612 if (tab_tracker_->ContainsHandle(tab_handle)) {
3613 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3614 if (!tab) {
3615 NOTREACHED();
3616 return;
3617 }
3618
[email protected]106a0812010-03-18 00:15:123619 const bool check_for_repost = true;
3620 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193621 }
3622}
3623
3624void AutomationProvider::StopAsync(int tab_handle) {
3625 RenderViewHost* view = GetViewForTab(tab_handle);
3626 if (!view) {
[email protected]8b2b3312009-09-14 18:38:363627 // We tolerate StopAsync being called even before a view has been created.
3628 // So just log a warning instead of a NOTREACHED().
3629 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:193630 return;
3631 }
3632
3633 view->Stop();
3634}
3635
[email protected]1bb5f892009-10-06 01:44:573636void AutomationProvider::OnSetPageFontSize(int tab_handle,
3637 int font_size) {
3638 AutomationPageFontSize automation_font_size =
3639 static_cast<AutomationPageFontSize>(font_size);
3640
3641 if (automation_font_size < SMALLEST_FONT ||
3642 automation_font_size > LARGEST_FONT) {
3643 DLOG(ERROR) << "Invalid font size specified : "
3644 << font_size;
3645 return;
3646 }
3647
3648 if (tab_tracker_->ContainsHandle(tab_handle)) {
3649 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3650 DCHECK(tab != NULL);
3651 if (tab && tab->tab_contents()) {
3652 DCHECK(tab->tab_contents()->profile() != NULL);
3653 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
3654 prefs::kWebKitDefaultFontSize, font_size);
3655 }
3656 }
3657}
3658
[email protected]bc73b4e52010-03-26 04:16:203659void AutomationProvider::RemoveBrowsingData(int remove_mask) {
3660 BrowsingDataRemover* remover;
3661 remover = new BrowsingDataRemover(profile(),
3662 BrowsingDataRemover::EVERYTHING, // All time periods.
3663 base::Time());
3664 remover->Remove(remove_mask);
3665 // BrowsingDataRemover deletes itself.
3666}
[email protected]1bb5f892009-10-06 01:44:573667
[email protected]2949e90d2009-08-21 15:32:523668void AutomationProvider::WaitForBrowserWindowCountToBecome(
3669 int target_count, IPC::Message* reply_message) {
3670 if (static_cast<int>(BrowserList::size()) == target_count) {
3671 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
3672 reply_message, true);
3673 Send(reply_message);
3674 return;
3675 }
3676
3677 // Set up an observer (it will delete itself).
3678 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
3679}
3680
3681void AutomationProvider::WaitForAppModalDialogToBeShown(
3682 IPC::Message* reply_message) {
3683 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
3684 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
3685 reply_message, true);
3686 Send(reply_message);
3687 return;
3688 }
3689
3690 // Set up an observer (it will delete itself).
3691 new AppModalDialogShownObserver(this, reply_message);
3692}
3693
[email protected]1126a1d32009-08-26 15:39:263694void AutomationProvider::GoBackBlockUntilNavigationsComplete(
3695 int handle, int number_of_navigations, IPC::Message* reply_message) {
3696 if (tab_tracker_->ContainsHandle(handle)) {
3697 NavigationController* tab = tab_tracker_->GetResource(handle);
3698 Browser* browser = FindAndActivateTab(tab);
3699 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:013700 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3701 false);
[email protected]1126a1d32009-08-26 15:39:263702 browser->GoBack(CURRENT_TAB);
3703 return;
3704 }
3705 }
3706
3707 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
3708 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3709 Send(reply_message);
3710}
3711
3712void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
3713 int handle, int number_of_navigations, IPC::Message* reply_message) {
3714 if (tab_tracker_->ContainsHandle(handle)) {
3715 NavigationController* tab = tab_tracker_->GetResource(handle);
3716 Browser* browser = FindAndActivateTab(tab);
3717 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:013718 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3719 false);
[email protected]1126a1d32009-08-26 15:39:263720 browser->GoForward(CURRENT_TAB);
3721 return;
3722 }
3723 }
3724
3725 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
3726 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3727 Send(reply_message);
3728}
3729
[email protected]f7a68432009-07-29 23:18:193730RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
3731 if (tab_tracker_->ContainsHandle(tab_handle)) {
3732 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3733 if (!tab) {
3734 NOTREACHED();
3735 return NULL;
3736 }
3737
3738 TabContents* tab_contents = tab->tab_contents();
3739 if (!tab_contents) {
3740 NOTREACHED();
3741 return NULL;
3742 }
3743
3744 RenderViewHost* view_host = tab_contents->render_view_host();
3745 return view_host;
3746 }
3747
3748 return NULL;
3749}
[email protected]675595f2009-08-26 22:32:043750
3751void AutomationProvider::GetBrowserForWindow(int window_handle,
3752 bool* success,
3753 int* browser_handle) {
3754 *success = false;
3755 *browser_handle = 0;
3756
3757 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
3758 if (!window)
3759 return;
3760
3761 BrowserList::const_iterator iter = BrowserList::begin();
3762 for (;iter != BrowserList::end(); ++iter) {
3763 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
3764 if (window == this_window) {
3765 // Add() returns the existing handle for the resource if any.
3766 *browser_handle = browser_tracker_->Add(*iter);
3767 *success = true;
3768 return;
3769 }
3770 }
3771}
[email protected]d11c8e92009-10-20 23:26:403772
3773void AutomationProvider::InstallExtension(const FilePath& crx_path,
3774 IPC::Message* reply_message) {
3775 ExtensionsService* service = profile_->GetExtensionsService();
3776 if (service) {
3777 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193778 new ExtensionInstallNotificationObserver(this,
3779 AutomationMsg_InstallExtension::ID,
3780 reply_message);
[email protected]d11c8e92009-10-20 23:26:403781
3782 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:163783 scoped_refptr<CrxInstaller> installer(
3784 new CrxInstaller(install_dir,
3785 service,
3786 NULL)); // silent install, no UI
3787 installer->set_allow_privilege_increase(true);
3788 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:403789 } else {
3790 AutomationMsg_InstallExtension::WriteReplyParams(
3791 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3792 Send(reply_message);
3793 }
3794}
3795
3796void AutomationProvider::LoadExpandedExtension(
3797 const FilePath& extension_dir,
3798 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:383799 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:403800 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193801 new ExtensionInstallNotificationObserver(
3802 this,
3803 AutomationMsg_LoadExpandedExtension::ID,
3804 reply_message);
[email protected]d11c8e92009-10-20 23:26:403805
3806 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:403807 } else {
3808 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
3809 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3810 Send(reply_message);
3811 }
3812}
[email protected]673fd2c02010-02-04 23:10:003813
[email protected]a1e62d12010-03-16 02:18:433814void AutomationProvider::GetEnabledExtensions(
3815 std::vector<FilePath>* result) {
3816 ExtensionsService* service = profile_->GetExtensionsService();
3817 DCHECK(service);
3818 if (service->extensions_enabled()) {
3819 const ExtensionList* extensions = service->extensions();
3820 DCHECK(extensions);
3821 for (size_t i = 0; i < extensions->size(); ++i) {
3822 Extension* extension = (*extensions)[i];
3823 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:123824 if (extension->location() == Extension::INTERNAL ||
3825 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:073826 result->push_back(extension->path());
3827 }
[email protected]a1e62d12010-03-16 02:18:433828 }
3829 }
3830}
3831
[email protected]790788ac2010-04-06 17:52:193832void AutomationProvider::WaitForExtensionTestResult(
3833 IPC::Message* reply_message) {
3834 DCHECK(reply_message_ == NULL);
3835 reply_message_ = reply_message;
3836 // Call MaybeSendResult, because the result might have come in before
3837 // we were waiting on it.
3838 extension_test_result_observer_->MaybeSendResult();
3839}
3840
3841void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:093842 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:193843 ExtensionsService* service = profile_->GetExtensionsService();
3844 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3845 if (service && manager) {
3846 // The observer will delete itself when done.
3847 new ExtensionReadyNotificationObserver(
3848 manager,
3849 this,
3850 AutomationMsg_InstallExtensionAndGetHandle::ID,
3851 reply_message);
3852
[email protected]d7e5525d2010-04-20 14:37:093853 ExtensionInstallUI* client =
3854 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:193855 scoped_refptr<CrxInstaller> installer(
3856 new CrxInstaller(service->install_directory(),
3857 service,
[email protected]d7e5525d2010-04-20 14:37:093858 client));
[email protected]790788ac2010-04-06 17:52:193859 installer->set_allow_privilege_increase(true);
3860 installer->InstallCrx(crx_path);
3861 } else {
3862 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
3863 reply_message, 0);
3864 Send(reply_message);
3865 }
3866}
3867
3868void AutomationProvider::UninstallExtension(int extension_handle,
3869 bool* success) {
3870 *success = false;
3871 Extension* extension = GetExtension(extension_handle);
3872 ExtensionsService* service = profile_->GetExtensionsService();
3873 if (extension && service) {
3874 ExtensionUnloadNotificationObserver observer;
3875 service->UninstallExtension(extension->id(), false);
3876 // The extension unload notification should have been sent synchronously
3877 // with the uninstall. Just to be safe, check that it was received.
3878 *success = observer.did_receive_unload_notification();
3879 }
3880}
3881
3882void AutomationProvider::EnableExtension(int extension_handle,
3883 IPC::Message* reply_message) {
3884 Extension* extension = GetDisabledExtension(extension_handle);
3885 ExtensionsService* service = profile_->GetExtensionsService();
3886 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3887 // Only enable if this extension is disabled.
3888 if (extension && service && manager) {
3889 // The observer will delete itself when done.
3890 new ExtensionReadyNotificationObserver(
3891 manager,
3892 this,
3893 AutomationMsg_EnableExtension::ID,
3894 reply_message);
3895 service->EnableExtension(extension->id());
3896 } else {
3897 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
3898 Send(reply_message);
3899 }
3900}
3901
3902void AutomationProvider::DisableExtension(int extension_handle,
3903 bool* success) {
3904 *success = false;
3905 Extension* extension = GetEnabledExtension(extension_handle);
3906 ExtensionsService* service = profile_->GetExtensionsService();
3907 if (extension && service) {
3908 ExtensionUnloadNotificationObserver observer;
3909 service->DisableExtension(extension->id());
3910 // The extension unload notification should have been sent synchronously
3911 // with the disable. Just to be safe, check that it was received.
3912 *success = observer.did_receive_unload_notification();
3913 }
3914}
3915
3916void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
3917 int extension_handle, int browser_handle,
3918 IPC::Message* reply_message) {
3919 bool success = false;
3920 Extension* extension = GetEnabledExtension(extension_handle);
3921 ExtensionsService* service = profile_->GetExtensionsService();
3922 ExtensionMessageService* message_service =
3923 profile_->GetExtensionMessageService();
3924 Browser* browser = browser_tracker_->GetResource(browser_handle);
3925 if (extension && service && message_service && browser) {
3926 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
3927 if (extension->page_action()) {
3928 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
3929 browser->profile(), extension->id(), "action", tab_id, "", 1);
3930 success = true;
3931 } else if (extension->browser_action()) {
3932 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
3933 browser->profile(), extension->id(), browser);
3934 success = true;
3935 }
3936 }
3937 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3938 reply_message, success);
3939 Send(reply_message);
3940}
3941
3942void AutomationProvider::MoveExtensionBrowserAction(
3943 int extension_handle, int index, bool* success) {
3944 *success = false;
3945 Extension* extension = GetEnabledExtension(extension_handle);
3946 ExtensionsService* service = profile_->GetExtensionsService();
3947 if (extension && service) {
3948 ExtensionToolbarModel* toolbar = service->toolbar_model();
3949 if (toolbar) {
3950 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3951 toolbar->MoveBrowserAction(extension, index);
3952 *success = true;
3953 } else {
3954 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3955 }
3956 }
3957 }
3958}
3959
3960void AutomationProvider::GetExtensionProperty(
3961 int extension_handle,
3962 AutomationMsg_ExtensionProperty type,
3963 bool* success,
3964 std::string* value) {
3965 *success = false;
3966 Extension* extension = GetExtension(extension_handle);
3967 ExtensionsService* service = profile_->GetExtensionsService();
3968 if (extension && service) {
3969 ExtensionToolbarModel* toolbar = service->toolbar_model();
3970 int found_index = -1;
3971 int index = 0;
3972 switch (type) {
3973 case AUTOMATION_MSG_EXTENSION_ID:
3974 *value = extension->id();
3975 *success = true;
3976 break;
3977 case AUTOMATION_MSG_EXTENSION_NAME:
3978 *value = extension->name();
3979 *success = true;
3980 break;
3981 case AUTOMATION_MSG_EXTENSION_VERSION:
3982 *value = extension->VersionString();
3983 *success = true;
3984 break;
3985 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3986 if (toolbar) {
3987 for (ExtensionList::const_iterator iter = toolbar->begin();
3988 iter != toolbar->end(); iter++) {
3989 // Skip this extension if we are in incognito mode
3990 // and it is not incognito-enabled.
3991 if (profile_->IsOffTheRecord() &&
3992 !service->IsIncognitoEnabled(*iter))
3993 continue;
3994 if (*iter == extension) {
3995 found_index = index;
3996 break;
3997 }
3998 index++;
3999 }
4000 *value = IntToString(found_index);
4001 *success = true;
4002 }
4003 break;
4004 default:
4005 LOG(WARNING) << "Trying to get undefined extension property";
4006 break;
4007 }
4008 }
4009}
4010
[email protected]673fd2c02010-02-04 23:10:004011void AutomationProvider::SaveAsAsync(int tab_handle) {
4012 NavigationController* tab = NULL;
4013 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
4014 if (tab_contents)
4015 tab_contents->OnSavePage();
4016}
[email protected]7dad3d5f2010-03-04 00:27:014017
4018void AutomationProvider::SetContentSetting(
4019 int handle,
4020 const std::string& host,
4021 ContentSettingsType content_type,
4022 ContentSetting setting,
4023 bool* success) {
4024 *success = false;
4025 if (browser_tracker_->ContainsHandle(handle)) {
4026 Browser* browser = browser_tracker_->GetResource(handle);
4027 HostContentSettingsMap* map =
4028 browser->profile()->GetHostContentSettingsMap();
4029 if (host.empty()) {
4030 map->SetDefaultContentSetting(content_type, setting);
4031 } else {
[email protected]0314ae02010-04-08 09:18:294032 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
4033 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:014034 }
4035 *success = true;
4036 }
4037}
[email protected]cc824372010-03-31 15:33:014038
4039#if !defined(TOOLKIT_VIEWS)
4040void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
4041 NOTIMPLEMENTED();
4042};
4043
4044void AutomationProvider::WaitForFocusedViewIDToChange(
4045 int handle, int previous_view_id, IPC::Message* reply_message) {
4046 NOTIMPLEMENTED();
4047}
4048
4049void AutomationProvider::StartTrackingPopupMenus(
4050 int browser_handle, bool* success) {
4051 NOTIMPLEMENTED();
4052}
4053
4054void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
4055 NOTIMPLEMENTED();
4056}
4057#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:094058
4059void AutomationProvider::ResetToDefaultTheme() {
4060 profile_->ClearTheme();
4061}