blob: 0597f694a06911e325a20e809005ee585a439aa6 [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"
[email protected]7c983cc2010-07-16 11:33:3434#include "chrome/browser/automation/automation_provider_json.h"
initial.commit09911bf2008-07-26 23:55:2935#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0836#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]f44265b2009-05-19 18:52:5037#include "chrome/browser/automation/extension_port_container.h"
[email protected]12802702010-07-09 19:43:0938#include "chrome/browser/autocomplete/autocomplete_edit.h"
[email protected]66ba4932009-06-04 19:22:1339#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5340#include "chrome/browser/bookmarks/bookmark_model.h"
41#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]ef413ca2010-05-25 21:09:1442#include "chrome/browser/browser_list.h"
[email protected]5c238752009-06-13 10:29:0743#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3944#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2045#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0546#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5847#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2948#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1249#include "chrome/browser/debugger/devtools_manager.h"
[email protected]6c69796d2010-07-16 21:41:1650#include "chrome/browser/download/download_item.h"
[email protected]59560e0b2009-06-04 03:30:2251#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0552#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4053#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1954#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]ef413ca2010-05-25 21:09:1455#include "chrome/browser/extensions/extension_host.h"
[email protected]d11c8e92009-10-20 23:26:4056#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5557#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1958#include "chrome/browser/extensions/extension_tabs_module.h"
59#include "chrome/browser/extensions/extension_toolbar_model.h"
60#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1661#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5862#include "chrome/browser/find_bar.h"
63#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2964#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0165#include "chrome/browser/host_content_settings_map.h"
[email protected]c5aa5322010-07-15 19:00:0766#include "chrome/browser/importer/importer.h"
67#include "chrome/browser/importer/importer_data_types.h"
[email protected]0ac83682010-01-22 17:46:2768#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0669#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0170#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5371#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2472#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0873#include "chrome/browser/pref_service.h"
[email protected]f83f9102010-05-04 17:01:0574#include "chrome/browser/printing/print_job.h"
[email protected]a7eee32f2009-05-22 18:08:1775#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0776#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2577#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0378#include "chrome/browser/ssl/ssl_manager.h"
79#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]b5558cf22010-07-12 17:30:0680#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3481#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4582#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1783#include "chrome/common/automation_constants.h"
[email protected]a9ff2c02010-05-13 17:33:0584#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2985#include "chrome/common/chrome_paths.h"
[email protected]790788ac2010-04-06 17:52:1986#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1787#include "chrome/common/json_value_serializer.h"
[email protected]68d2a05f2010-05-07 21:39:5588#include "chrome/common/net/url_request_context_getter.h"
[email protected]1c58a5c2009-05-21 18:47:1489#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5790#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2591#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5692#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5793#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1794#include "net/proxy/proxy_service.h"
95#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2196#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:3097#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:2898#include "views/event.h"
[email protected]5bcfe1672010-07-16 20:51:5799#include "webkit/glue/password_form.h"
[email protected]f7d48012010-05-06 08:17:05100#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:29101
[email protected]de246f52009-02-25 18:25:45102#if defined(OS_WIN)
[email protected]4bdde602010-06-16 03:17:35103#include "chrome/browser/external_tab_container_win.h"
[email protected]de246f52009-02-25 18:25:45104#endif // defined(OS_WIN)
105
[email protected]e1acf6f2008-10-27 20:43:33106using base::Time;
107
[email protected]cbab76d2008-10-13 22:42:47108class AutomationInterstitialPage : public InterstitialPage {
109 public:
[email protected]57c6a652009-05-04 07:58:34110 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47111 const GURL& url,
112 const std::string& contents)
113 : InterstitialPage(tab, true, url),
114 contents_(contents) {
115 }
116
117 virtual std::string GetHTMLContents() { return contents_; }
118
119 private:
120 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29121
[email protected]cbab76d2008-10-13 22:42:47122 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
123};
124
[email protected]c2cb8542009-08-20 21:16:51125class ClickTask : public Task {
126 public:
[email protected]fc2e0872009-08-21 22:14:41127 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51128 virtual ~ClickTask() {}
129
130 virtual void Run() {
131 ui_controls::MouseButton button = ui_controls::LEFT;
132 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
133 views::Event::EF_LEFT_BUTTON_DOWN) {
134 button = ui_controls::LEFT;
135 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
136 views::Event::EF_RIGHT_BUTTON_DOWN) {
137 button = ui_controls::RIGHT;
138 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
139 views::Event::EF_MIDDLE_BUTTON_DOWN) {
140 button = ui_controls::MIDDLE;
141 } else {
142 NOTREACHED();
143 }
144
[email protected]fc2e0872009-08-21 22:14:41145 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51146 }
147
148 private:
[email protected]c2cb8542009-08-20 21:16:51149 int flags_;
150
151 DISALLOW_COPY_AND_ASSIGN(ClickTask);
152};
[email protected]c2cb8542009-08-20 21:16:51153
initial.commit09911bf2008-07-26 23:55:29154AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57155 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56156 profile_(profile),
[email protected]cc824372010-03-31 15:33:01157 reply_message_(NULL),
158 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29159 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19160 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29161 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20162 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29163 autocomplete_edit_tracker_.reset(
164 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29165 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
166 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44167 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19168 extension_test_result_observer_.reset(
169 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13170 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29171}
172
173AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50174 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
175 port_containers_.end());
176 port_containers_.clear();
177
[email protected]0da050b92008-08-19 19:29:47178 // Make sure that any outstanding NotificationObservers also get destroyed.
179 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31180 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47181 while ((observer = it.GetNext()) != NULL)
182 delete observer;
[email protected]528211a2010-01-14 15:25:13183
184 if (channel_.get()) {
185 channel_->Close();
186 }
187 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29188}
189
[email protected]9a3a293b2009-06-04 22:28:16190void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06191 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57192 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06193 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
194 automation_resource_message_filter_,
195 g_browser_process->io_thread()->message_loop(),
196 true, g_browser_process->shutdown_event()));
[email protected]bcff05a2010-04-14 01:46:43197 scoped_ptr<FileVersionInfo> version_info(
198 chrome_app::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40199 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43200 if (version_info != NULL) {
201 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40202 }
[email protected]c6cb1992009-04-13 16:45:29203
204 // Send a hello message with our current automation protocol version.
205 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29206}
207
208void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
209 if (expected_tabs == 0) {
210 Send(new AutomationMsg_InitialLoadsComplete(0));
211 } else {
212 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
213 }
214}
215
216NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58217 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01218 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29219 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58220 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01221 number_of_navigations,
222 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29223
[email protected]71f65dd2009-02-11 19:14:56224 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29225 return observer;
226}
227
[email protected]faf2ee42010-05-11 14:26:17228void AutomationProvider::RemoveNavigationStatusListener(
229 NotificationObserver* obs) {
230 notification_observer_list_.RemoveObserver(obs);
231}
232
initial.commit09911bf2008-07-26 23:55:29233NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14234 Browser* parent,
235 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56236 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14237 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29238 notification_observer_list_.AddObserver(observer);
239
240 return observer;
241}
242
[email protected]faf2ee42010-05-11 14:26:17243void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
initial.commit09911bf2008-07-26 23:55:29244 notification_observer_list_.RemoveObserver(obs);
245}
246
247void AutomationProvider::AddLoginHandler(NavigationController* tab,
248 LoginHandler* handler) {
249 login_handler_map_[tab] = handler;
250}
251
252void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
253 DCHECK(login_handler_map_[tab]);
254 login_handler_map_.erase(tab);
255}
256
[email protected]f44265b2009-05-19 18:52:50257void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
258 int port_id = port->port_id();
259 DCHECK_NE(-1, port_id);
260 DCHECK(port_containers_.find(port_id) == port_containers_.end());
261
262 port_containers_[port_id] = port;
263}
264
265void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
266 int port_id = port->port_id();
267 DCHECK_NE(-1, port_id);
268
269 PortContainerMap::iterator it = port_containers_.find(port_id);
270 DCHECK(it != port_containers_.end());
271
272 if (it != port_containers_.end()) {
273 delete it->second;
274 port_containers_.erase(it);
275 }
276}
277
278ExtensionPortContainer* AutomationProvider::GetPortContainer(
279 int port_id) const {
280 PortContainerMap::const_iterator it = port_containers_.find(port_id);
281 if (it == port_containers_.end())
282 return NULL;
283
284 return it->second;
285}
286
initial.commit09911bf2008-07-26 23:55:29287int AutomationProvider::GetIndexForNavigationController(
288 const NavigationController* controller, const Browser* parent) const {
289 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12290 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29291}
292
[email protected]790788ac2010-04-06 17:52:19293int AutomationProvider::AddExtension(Extension* extension) {
294 DCHECK(extension);
295 return extension_tracker_->Add(extension);
296}
297
298Extension* AutomationProvider::GetExtension(int extension_handle) {
299 return extension_tracker_->GetResource(extension_handle);
300}
301
302Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
303 Extension* extension = extension_tracker_->GetResource(extension_handle);
304 ExtensionsService* service = profile_->GetExtensionsService();
305 if (extension && service &&
306 service->GetExtensionById(extension->id(), false))
307 return extension;
308 return NULL;
309}
310
311Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
312 Extension* extension = extension_tracker_->GetResource(extension_handle);
313 ExtensionsService* service = profile_->GetExtensionsService();
314 if (extension && service &&
315 service->GetExtensionById(extension->id(), true) &&
316 !service->GetExtensionById(extension->id(), false))
317 return extension;
318 return NULL;
319}
320
initial.commit09911bf2008-07-26 23:55:29321void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
322 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14323 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56324 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
325 CloseBrowserAsync)
326 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
327 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
328 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
329 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
330 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
331 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22332 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]a503c97c2010-07-16 13:05:48333 IPC_MESSAGE_HANDLER(AutomationMsg_ShowCollectedCookiesDialog,
334 ShowCollectedCookiesDialog)
[email protected]1c58a5c2009-05-21 18:47:14335 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58336 IPC_MESSAGE_HANDLER_DELAY_REPLY(
337 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
338 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56339 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11340 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
341 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56342 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
343 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
344 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
345 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14346 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56347 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
348 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
349 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14350 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29351 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
352 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56353 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36354 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56355 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29356 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56357 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29358 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
359 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56360 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14361 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24362 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44363 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33364 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44365 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33366 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24367 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14368 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24369 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15370 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14371 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24372 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35373 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14374 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30375#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56376 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
377 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30378#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56379 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53380 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56381 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37382#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56383 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45384#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56385 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
386 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03387 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56388 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14389 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09390 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
391 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
392 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29393 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14394 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56395 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
396 ExecuteJavascript)
397 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29398 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14399 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
400 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56401 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
402 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14403 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17404 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14405 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14406 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53407 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
408 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14409 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56410 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14411 GetAutocompleteEditForBrowser)
412 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]71f65dd2009-02-11 19:14:56413 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14414 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56415 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14416 HideInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56417 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
418 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14419 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
420 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44421 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
422 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56423 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
424 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29425 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
426 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
427 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56428 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41429 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56430 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
431 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29432 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56433 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29434 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56435 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29436 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56437 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29438 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56439 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45440 HandleOpenFindInPageRequest)
[email protected]1c58a5c2009-05-21 18:47:14441 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56442 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57443 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56444 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57445 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56446 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
447 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53448 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
449 GetBookmarksAsJSON)
450 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
451 WaitForBookmarkModelToLoad)
452 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
453 AddBookmarkGroup)
454 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
455 AddBookmarkURL)
456 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
457 ReparentBookmark)
458 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
459 SetBookmarkTitle)
460 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
461 SetBookmarkURL)
462 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
463 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04464 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
465 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18466 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
467 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
468 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56469 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42470 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56471 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
472 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14473 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56474 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44475 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56476 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20477 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14478 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56479 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16480 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56481 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16482 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56483 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16484 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14485 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20486 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
487 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14488 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]59560e0b2009-06-04 03:30:22489 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13490 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19491 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
492 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
493 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
494 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
495 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
496 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52497 IPC_MESSAGE_HANDLER_DELAY_REPLY(
498 AutomationMsg_WaitForBrowserWindowCountToBecome,
499 WaitForBrowserWindowCountToBecome)
500 IPC_MESSAGE_HANDLER_DELAY_REPLY(
501 AutomationMsg_WaitForAppModalDialogToBeShown,
502 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26503 IPC_MESSAGE_HANDLER_DELAY_REPLY(
504 AutomationMsg_GoBackBlockUntilNavigationsComplete,
505 GoBackBlockUntilNavigationsComplete)
506 IPC_MESSAGE_HANDLER_DELAY_REPLY(
507 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
508 GoForwardBlockUntilNavigationsComplete)
[email protected]1bb5f892009-10-06 01:44:57509 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40510 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
511 InstallExtension)
512 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
513 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43514 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
515 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19516 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
517 WaitForExtensionTestResult)
518 IPC_MESSAGE_HANDLER_DELAY_REPLY(
519 AutomationMsg_InstallExtensionAndGetHandle,
520 InstallExtensionAndGetHandle)
521 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
522 UninstallExtension)
523 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
524 EnableExtension)
525 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
526 DisableExtension)
527 IPC_MESSAGE_HANDLER_DELAY_REPLY(
528 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
529 ExecuteExtensionActionInActiveTabAsync)
530 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
531 MoveExtensionBrowserAction)
532 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
533 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57534 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
535 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00536 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]7dad3d5f2010-03-04 00:27:01537 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]bc73b4e52010-03-26 04:16:20538 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]bdd5a9c92010-06-14 18:21:00539 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
[email protected]cc824372010-03-31 15:33:01540#if defined(TOOLKIT_VIEWS)
541 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
542 WaitForFocusedViewIDToChange)
543 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
544 StartTrackingPopupMenus)
545 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
546 WaitForPopupMenuToOpen)
[email protected]bdd5a9c92010-06-14 18:21:00547#endif // defined(TOOLKIT_VIEWS)
[email protected]52415f842010-06-10 21:51:52548#if defined(OS_WIN)
549 // These are for use with external tabs.
550 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
551 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
552 ProcessUnhandledAccelerator)
553 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
554 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
555 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
556 OnForwardContextMenuCommandToChrome)
557 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
558 NavigateInExternalTab)
559 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
560 NavigateExternalTabAtIndex)
561 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
562 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
563 SetEnableExtensionAutomation)
564 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
565 OnMessageFromExternalHost)
[email protected]bdd5a9c92010-06-14 18:21:00566 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
[email protected]1f71d5882010-07-15 20:39:07567 IPC_MESSAGE_HANDLER(AutomationMsg_RunUnloadHandlers, OnRunUnloadHandlers)
[email protected]bdd5a9c92010-06-14 18:21:00568#endif // defined(OS_WIN)
569#if defined(OS_CHROMEOS)
570 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
571 LoginWithUserAndPass)
572#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29573 IPC_END_MESSAGE_MAP()
574}
575
[email protected]71f65dd2009-02-11 19:14:56576void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
577 *status = -1;
initial.commit09911bf2008-07-26 23:55:29578 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
579 Browser* browser = browser_tracker_->GetResource(handle);
580 if (at_index >= 0 && at_index < browser->tab_count()) {
581 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56582 *status = 0;
initial.commit09911bf2008-07-26 23:55:29583 }
584 }
initial.commit09911bf2008-07-26 23:55:29585}
586
[email protected]71f65dd2009-02-11 19:14:56587void AutomationProvider::AppendTab(int handle, const GURL& url,
588 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29589 int append_tab_response = -1; // -1 is the error code
590 NotificationObserver* observer = NULL;
591
592 if (browser_tracker_->ContainsHandle(handle)) {
593 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14594 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38595 TabContents* tab_contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39596 url, GURL(), PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED,
597 NULL, std::string());
initial.commit09911bf2008-07-26 23:55:29598 if (tab_contents) {
599 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57600 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29601 }
602 }
603
604 if (append_tab_response < 0) {
605 // The append tab failed. Remove the TabStripObserver
606 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17607 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29608 delete observer;
609 }
610
[email protected]71f65dd2009-02-11 19:14:56611 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
612 append_tab_response);
613 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29614 }
615}
616
[email protected]71f65dd2009-02-11 19:14:56617void AutomationProvider::NavigateToURL(int handle, const GURL& url,
618 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58619 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
620}
621
622void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
623 int handle, const GURL& url, int number_of_navigations,
624 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29625 if (tab_tracker_->ContainsHandle(handle)) {
626 NavigationController* tab = tab_tracker_->GetResource(handle);
627
628 // Simulate what a user would do. Activate the tab and then navigate.
629 // We could allow navigating in a background tab in future.
630 Browser* browser = FindAndActivateTab(tab);
631
632 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01633 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
634 false);
[email protected]71f65dd2009-02-11 19:14:56635
initial.commit09911bf2008-07-26 23:55:29636 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50637 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29638 return;
639 }
640 }
[email protected]71f65dd2009-02-11 19:14:56641
642 AutomationMsg_NavigateToURL::WriteReplyParams(
643 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
644 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29645}
[email protected]2949e90d2009-08-21 15:32:52646
[email protected]c70f9b82010-04-21 07:31:11647void AutomationProvider::NavigationAsync(int handle,
648 const GURL& url,
649 bool* status) {
650 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
651}
652
653void AutomationProvider::NavigationAsyncWithDisposition(
654 int handle,
655 const GURL& url,
656 WindowOpenDisposition disposition,
657 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56658 *status = false;
initial.commit09911bf2008-07-26 23:55:29659
660 if (tab_tracker_->ContainsHandle(handle)) {
661 NavigationController* tab = tab_tracker_->GetResource(handle);
662
663 // Simulate what a user would do. Activate the tab and then navigate.
664 // We could allow navigating in a background tab in future.
665 Browser* browser = FindAndActivateTab(tab);
666
667 if (browser) {
668 // Don't add any listener unless a callback mechanism is desired.
669 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11670 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56671 *status = true;
initial.commit09911bf2008-07-26 23:55:29672 }
673 }
initial.commit09911bf2008-07-26 23:55:29674}
675
[email protected]71f65dd2009-02-11 19:14:56676void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29677 if (tab_tracker_->ContainsHandle(handle)) {
678 NavigationController* tab = tab_tracker_->GetResource(handle);
679 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14680 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01681 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29682 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29683 return;
684 }
685 }
[email protected]71f65dd2009-02-11 19:14:56686
687 AutomationMsg_GoBack::WriteReplyParams(
688 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
689 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29690}
691
[email protected]71f65dd2009-02-11 19:14:56692void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29693 if (tab_tracker_->ContainsHandle(handle)) {
694 NavigationController* tab = tab_tracker_->GetResource(handle);
695 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14696 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01697 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29698 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29699 return;
700 }
701 }
[email protected]71f65dd2009-02-11 19:14:56702
703 AutomationMsg_GoForward::WriteReplyParams(
704 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
705 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29706}
707
[email protected]71f65dd2009-02-11 19:14:56708void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29709 if (tab_tracker_->ContainsHandle(handle)) {
710 NavigationController* tab = tab_tracker_->GetResource(handle);
711 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14712 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01713 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]cb84d642010-06-10 00:56:28714 browser->Reload(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29715 return;
716 }
717 }
[email protected]71f65dd2009-02-11 19:14:56718
719 AutomationMsg_Reload::WriteReplyParams(
720 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
721 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29722}
723
[email protected]71f65dd2009-02-11 19:14:56724void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29725 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56726 const std::wstring& password,
727 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29728 if (tab_tracker_->ContainsHandle(tab_handle)) {
729 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
730 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
731
732 if (iter != login_handler_map_.end()) {
733 // If auth is needed again after this, assume login has failed. This is
734 // not strictly correct, because a navigation can require both proxy and
735 // server auth, but it should be OK for now.
736 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01737 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29738 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52739 return;
initial.commit09911bf2008-07-26 23:55:29740 }
741 }
[email protected]de246f52009-02-25 18:25:45742
[email protected]457f5cf2009-08-18 16:37:52743 AutomationMsg_SetAuth::WriteReplyParams(
744 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
745 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29746}
747
[email protected]71f65dd2009-02-11 19:14:56748void AutomationProvider::CancelAuth(int tab_handle,
749 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29750 if (tab_tracker_->ContainsHandle(tab_handle)) {
751 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
752 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
753
754 if (iter != login_handler_map_.end()) {
755 // If auth is needed again after this, something is screwy.
756 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01757 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29758 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52759 return;
initial.commit09911bf2008-07-26 23:55:29760 }
761 }
[email protected]de246f52009-02-25 18:25:45762
[email protected]457f5cf2009-08-18 16:37:52763 AutomationMsg_CancelAuth::WriteReplyParams(
764 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
765 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29766}
767
[email protected]71f65dd2009-02-11 19:14:56768void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
769 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29770
771 if (tab_tracker_->ContainsHandle(tab_handle)) {
772 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
773 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
774
775 if (iter != login_handler_map_.end()) {
776 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56777 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29778 }
779 }
initial.commit09911bf2008-07-26 23:55:29780}
781
[email protected]71f65dd2009-02-11 19:14:56782void AutomationProvider::GetRedirectsFrom(int tab_handle,
783 const GURL& source_url,
784 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29785 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
786 if (tab_tracker_->ContainsHandle(tab_handle)) {
787 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
788 HistoryService* history_service =
789 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
790
791 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
792 "has no history service";
793 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56794 DCHECK(reply_message_ == NULL);
795 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29796 // Schedule a history query for redirects. The response will be sent
797 // asynchronously from the callback the history system uses to notify us
798 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29799 redirect_query_ = history_service->QueryRedirectsFrom(
800 source_url, &consumer_,
801 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
802 return; // Response will be sent when query completes.
803 }
804 }
805
806 // Send failure response.
[email protected]deb57402009-02-06 01:35:30807 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56808 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
809 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29810}
811
[email protected]71f65dd2009-02-11 19:14:56812void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
813 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29814 if (browser_tracker_->ContainsHandle(handle)) {
815 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56816 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29817 }
initial.commit09911bf2008-07-26 23:55:29818}
819
[email protected]202e7a72009-06-15 03:48:36820void AutomationProvider::GetBrowserLocale(string16* locale) {
821 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06822 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36823}
824
[email protected]71f65dd2009-02-11 19:14:56825void AutomationProvider::GetBrowserWindowCount(int* window_count) {
826 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29827}
828
[email protected]24497032009-05-01 17:00:29829void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
830 *window_count = static_cast<int>(
831 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
832}
833
[email protected]71f65dd2009-02-11 19:14:56834void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
835 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07836 AppModalDialog* dialog_delegate =
837 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50838 *showing_dialog = (dialog_delegate != NULL);
839 if (*showing_dialog)
840 *dialog_button = dialog_delegate->GetDialogButtons();
841 else
[email protected]478ff2ed2009-04-21 23:49:18842 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20843}
844
[email protected]71f65dd2009-02-11 19:14:56845void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
846 *success = false;
[email protected]fad84eab2008-12-05 00:37:20847
[email protected]1f460072009-05-28 17:02:07848 AppModalDialog* dialog_delegate =
849 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50850 if (dialog_delegate &&
851 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18852 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
853 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28854 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56855 *success = true;
[email protected]fad84eab2008-12-05 00:37:20856 }
[email protected]478ff2ed2009-04-21 23:49:18857 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
858 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56859 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28860 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56861 *success = true;
[email protected]fad84eab2008-12-05 00:37:20862 }
863 }
[email protected]c274acc2008-11-11 20:13:44864}
865
[email protected]fedaa7d2010-01-26 20:34:57866void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
867 if (browser_tracker_->ContainsHandle(handle)) {
868 Browser* browser = browser_tracker_->GetResource(handle);
869 browser->profile()->ShutdownSessionService();
870 *result = true;
871 } else {
872 *result = false;
873 }
874}
875
[email protected]71f65dd2009-02-11 19:14:56876void AutomationProvider::GetBrowserWindow(int index, int* handle) {
877 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29878 if (index >= 0) {
879 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]f07467d2010-06-16 14:28:30880 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {}
initial.commit09911bf2008-07-26 23:55:29881 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56882 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29883 }
884 }
initial.commit09911bf2008-07-26 23:55:29885}
886
[email protected]24497032009-05-01 17:00:29887void AutomationProvider::FindNormalBrowserWindow(int* handle) {
888 *handle = 0;
889 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31890 Browser::TYPE_NORMAL,
891 false);
[email protected]24497032009-05-01 17:00:29892 if (browser)
893 *handle = browser_tracker_->Add(browser);
894}
895
[email protected]71f65dd2009-02-11 19:14:56896void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
897 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29898 Browser* browser = BrowserList::GetLastActive();
899 if (browser)
[email protected]71f65dd2009-02-11 19:14:56900 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29901}
902
[email protected]b2aa3ed72010-02-01 18:37:14903#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28904// TODO(estade): use this implementation for all platforms?
905void AutomationProvider::GetActiveWindow(int* handle) {
906 gfx::NativeWindow window =
907 BrowserList::GetLastActive()->window()->GetNativeHandle();
908 *handle = window_tracker_->Add(window);
909}
910#endif
911
[email protected]4f6381ee2009-04-16 02:46:33912void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
913 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56914 *success = false;
[email protected]4ae62752008-08-04 23:28:47915 if (browser_tracker_->ContainsHandle(handle)) {
916 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14917 if (browser->command_updater()->SupportsCommand(command) &&
918 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47919 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56920 *success = true;
[email protected]4ae62752008-08-04 23:28:47921 }
922 }
[email protected]4ae62752008-08-04 23:28:47923}
924
[email protected]4f6381ee2009-04-16 02:46:33925void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56926 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53927 // List of commands which just finish synchronously and don't require
928 // setting up an observer.
929 static const int kSynchronousCommands[] = {
930 IDC_HOME,
931 IDC_SELECT_NEXT_TAB,
932 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25933 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53934 };
[email protected]56e71b7c2009-03-27 03:05:56935 if (browser_tracker_->ContainsHandle(handle)) {
936 Browser* browser = browser_tracker_->GetResource(handle);
937 if (browser->command_updater()->SupportsCommand(command) &&
938 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53939 // First check if we can handle the command without using an observer.
940 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
941 if (command == kSynchronousCommands[i]) {
942 browser->ExecuteCommand(command);
943 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
944 true);
945 Send(reply_message);
946 return;
947 }
948 }
949
950 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42951 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
952 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27953 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42954 return;
955 }
[email protected]56e71b7c2009-03-27 03:05:56956 }
957 }
[email protected]49a14a82009-03-31 04:16:44958 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56959 Send(reply_message);
960}
961
[email protected]fc2e0872009-08-21 22:14:41962// This task just adds another task to the event queue. This is useful if
963// you want to ensure that any tasks added to the event queue after this one
964// have already been processed by the time |task| is run.
965class InvokeTaskLaterTask : public Task {
966 public:
967 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
968 virtual ~InvokeTaskLaterTask() {}
969
970 virtual void Run() {
971 MessageLoop::current()->PostTask(FROM_HERE, task_);
972 }
973
974 private:
975 Task* task_;
976
977 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
978};
979
initial.commit09911bf2008-07-26 23:55:29980void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
981 int handle,
[email protected]d1a5941e2009-08-13 23:34:24982 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29983 int flags) {
[email protected]b410bc32009-08-14 01:11:14984 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51985 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41986 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29987 }
988}
989
[email protected]60507b12009-11-02 23:51:35990void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
991 int handle,
992 const gfx::Point& location) {
993 if (window_tracker_->ContainsHandle(handle))
994 ui_controls::SendMouseMove(location.x(), location.y());
995}
996
initial.commit09911bf2008-07-26 23:55:29997void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
998 int handle,
[email protected]bc1407f2009-09-29 00:33:35999 int key,
initial.commit09911bf2008-07-26 23:55:291000 int flags) {
[email protected]b410bc32009-08-14 01:11:141001 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:291002 return;
1003
[email protected]b410bc32009-08-14 01:11:141004 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291005 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:351006 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:381007 ((flags & views::Event::EF_CONTROL_DOWN) ==
1008 views::Event::EF_CONTROL_DOWN),
1009 ((flags & views::Event::EF_SHIFT_DOWN) ==
1010 views::Event::EF_SHIFT_DOWN),
1011 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301012 views::Event::EF_ALT_DOWN),
1013 ((flags & views::Event::EF_COMMAND_DOWN) ==
1014 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291015}
initial.commit09911bf2008-07-26 23:55:291016
[email protected]71f65dd2009-02-11 19:14:561017void AutomationProvider::IsWindowActive(int handle, bool* success,
1018 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291019 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171020 *is_active =
1021 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561022 *success = true;
initial.commit09911bf2008-07-26 23:55:291023 } else {
[email protected]71f65dd2009-02-11 19:14:561024 *success = false;
1025 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291026 }
1027}
1028
[email protected]71f65dd2009-02-11 19:14:561029void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1030 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291031
1032 if (browser_tracker_->ContainsHandle(handle)) {
1033 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561034 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291035 }
initial.commit09911bf2008-07-26 23:55:291036}
1037
[email protected]982921f12009-10-27 21:43:531038void AutomationProvider::GetType(int handle, int* type_as_int) {
1039 *type_as_int = -1; // -1 is the error code
1040
1041 if (browser_tracker_->ContainsHandle(handle)) {
1042 Browser* browser = browser_tracker_->GetResource(handle);
1043 *type_as_int = static_cast<int>(browser->type());
1044 }
1045}
1046
[email protected]71f65dd2009-02-11 19:14:561047void AutomationProvider::GetTab(int win_handle, int tab_index,
1048 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561049 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291050 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1051 Browser* browser = browser_tracker_->GetResource(win_handle);
1052 if (tab_index < browser->tab_count()) {
1053 TabContents* tab_contents =
1054 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571055 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291056 }
1057 }
initial.commit09911bf2008-07-26 23:55:291058}
1059
[email protected]71f65dd2009-02-11 19:14:561060void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1061 std::wstring* title) {
1062 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291063 if (tab_tracker_->ContainsHandle(handle)) {
1064 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361065 NavigationEntry* entry = tab->GetActiveEntry();
1066 if (entry != NULL) {
1067 *title = UTF16ToWideHack(entry->title());
1068 } else {
1069 *title = std::wstring();
1070 }
[email protected]71f65dd2009-02-11 19:14:561071 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291072 }
initial.commit09911bf2008-07-26 23:55:291073}
1074
[email protected]77bc6732009-04-20 22:01:031075void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1076 *tabstrip_index = -1; // -1 is the error code
1077
1078 if (tab_tracker_->ContainsHandle(handle)) {
1079 NavigationController* tab = tab_tracker_->GetResource(handle);
1080 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121081 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031082 }
1083}
1084
initial.commit09911bf2008-07-26 23:55:291085void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1086 if (window_tracker_->ContainsHandle(handle)) {
1087 window_tracker_->Remove(window_tracker_->GetResource(handle));
1088 }
1089}
1090
1091void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161092 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571093 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291094}
1095
1096// TODO(brettw) change this to accept GURLs when history supports it
1097void AutomationProvider::OnRedirectQueryComplete(
1098 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371099 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291100 bool success,
[email protected]379c2b12009-07-01 21:50:331101 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291102 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561103 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291104
[email protected]deb57402009-02-06 01:35:301105 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101106 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291107 if (success) {
initial.commit09911bf2008-07-26 23:55:291108 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301109 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291110 }
1111
[email protected]4f3dc372009-02-24 00:10:291112 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301113
[email protected]71f65dd2009-02-11 19:14:561114 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231115 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561116 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291117}
1118
1119bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571120 DCHECK(channel_.get());
1121 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291122}
1123
1124Browser* AutomationProvider::FindAndActivateTab(
1125 NavigationController* controller) {
1126 int tab_index;
1127 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1128 if (browser)
1129 browser->SelectTabContentsAt(tab_index, true);
1130
1131 return browser;
1132}
1133
[email protected]9eaa18e2010-06-29 20:51:011134namespace {
1135
1136class GetCookiesTask : public Task {
1137 public:
1138 GetCookiesTask(const GURL& url,
1139 URLRequestContextGetter* context_getter,
1140 base::WaitableEvent* event,
1141 std::string* cookies)
1142 : url_(url),
1143 context_getter_(context_getter),
1144 event_(event),
1145 cookies_(cookies) {}
1146
1147 virtual void Run() {
1148 *cookies_ = context_getter_->GetCookieStore()->GetCookies(url_);
1149 event_->Signal();
1150 }
1151
1152 private:
1153 const GURL& url_;
1154 URLRequestContextGetter* const context_getter_;
1155 base::WaitableEvent* const event_;
1156 std::string* const cookies_;
1157
1158 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
1159};
1160
1161std::string GetCookiesForURL(
1162 const GURL& url,
1163 URLRequestContextGetter* context_getter) {
1164 std::string cookies;
1165 base::WaitableEvent event(true /* manual reset */,
1166 false /* not initially signaled */);
1167 CHECK(ChromeThread::PostTask(
1168 ChromeThread::IO, FROM_HERE,
1169 new GetCookiesTask(url, context_getter, &event, &cookies)));
1170 event.Wait();
1171 return cookies;
1172}
1173
1174class SetCookieTask : public Task {
1175 public:
1176 SetCookieTask(const GURL& url,
1177 const std::string& value,
1178 URLRequestContextGetter* context_getter,
1179 base::WaitableEvent* event,
1180 bool* rv)
1181 : url_(url),
1182 value_(value),
1183 context_getter_(context_getter),
1184 event_(event),
1185 rv_(rv) {}
1186
1187 virtual void Run() {
1188 *rv_ = context_getter_->GetCookieStore()->SetCookie(url_, value_);
1189 event_->Signal();
1190 }
1191
1192 private:
1193 const GURL& url_;
1194 const std::string& value_;
1195 URLRequestContextGetter* const context_getter_;
1196 base::WaitableEvent* const event_;
1197 bool* const rv_;
1198
1199 DISALLOW_COPY_AND_ASSIGN(SetCookieTask);
1200};
1201
1202bool SetCookieForURL(
1203 const GURL& url,
1204 const std::string& value,
1205 URLRequestContextGetter* context_getter) {
1206 base::WaitableEvent event(true /* manual reset */,
1207 false /* not initially signaled */);
1208 bool rv = false;
1209 CHECK(ChromeThread::PostTask(
1210 ChromeThread::IO, FROM_HERE,
1211 new SetCookieTask(url, value, context_getter, &event, &rv)));
1212 event.Wait();
1213 return rv;
1214}
1215
1216class DeleteCookieTask : public Task {
1217 public:
1218 DeleteCookieTask(const GURL& url,
1219 const std::string& name,
1220 const scoped_refptr<URLRequestContextGetter>& context_getter)
1221 : url_(url),
1222 name_(name),
1223 context_getter_(context_getter) {}
1224
1225 virtual void Run() {
1226 net::CookieStore* cookie_store = context_getter_->GetCookieStore();
1227 cookie_store->DeleteCookie(url_, name_);
1228 }
1229
1230 private:
1231 const GURL url_;
1232 const std::string name_;
1233 const scoped_refptr<URLRequestContextGetter> context_getter_;
1234
1235 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
1236};
1237
1238} // namespace
1239
[email protected]71f65dd2009-02-11 19:14:561240void AutomationProvider::GetCookies(const GURL& url, int handle,
1241 int* value_size,
1242 std::string* value) {
1243 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291244 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1245 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311246
1247 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001248 scoped_refptr<URLRequestContextGetter> request_context =
1249 tab->tab_contents()->request_context();
1250 if (!request_context.get())
1251 request_context = tab->profile()->GetRequestContext();
1252
[email protected]9eaa18e2010-06-29 20:51:011253 *value = GetCookiesForURL(url, request_context.get());
[email protected]71f65dd2009-02-11 19:14:561254 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291255 }
initial.commit09911bf2008-07-26 23:55:291256}
1257
[email protected]71f65dd2009-02-11 19:14:561258void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291259 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561260 int handle,
1261 int* response_value) {
1262 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291263
1264 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1265 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311266
[email protected]dfa46e5f2009-11-17 18:48:431267 scoped_refptr<URLRequestContextGetter> request_context =
1268 tab->tab_contents()->request_context();
1269 if (!request_context.get())
1270 request_context = tab->profile()->GetRequestContext();
1271
[email protected]9eaa18e2010-06-29 20:51:011272 if (SetCookieForURL(url, value, request_context.get()))
[email protected]71f65dd2009-02-11 19:14:561273 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291274 }
initial.commit09911bf2008-07-26 23:55:291275}
1276
[email protected]5fa57942010-04-21 23:07:221277void AutomationProvider::DeleteCookie(const GURL& url,
1278 const std::string& cookie_name,
1279 int handle, bool* success) {
1280 *success = false;
1281 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1282 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]9eaa18e2010-06-29 20:51:011283 ChromeThread::PostTask(
1284 ChromeThread::IO, FROM_HERE,
1285 new DeleteCookieTask(url, cookie_name,
1286 tab->profile()->GetRequestContext()));
[email protected]5fa57942010-04-21 23:07:221287 *success = true;
1288 }
1289}
1290
[email protected]a503c97c2010-07-16 13:05:481291void AutomationProvider::ShowCollectedCookiesDialog(
1292 int handle, bool* success) {
1293 *success = false;
1294 if (tab_tracker_->ContainsHandle(handle)) {
1295 TabContents* tab_contents =
1296 tab_tracker_->GetResource(handle)->tab_contents();
1297 tab_contents->delegate()->ShowCollectedCookiesDialog(tab_contents);
1298 *success = true;
1299 }
1300}
1301
[email protected]71f65dd2009-02-11 19:14:561302void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1303 *success = false;
initial.commit09911bf2008-07-26 23:55:291304 if (tab_tracker_->ContainsHandle(handle)) {
1305 NavigationController* tab = tab_tracker_->GetResource(handle);
1306 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541307 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561308 *success = true;
initial.commit09911bf2008-07-26 23:55:291309 }
initial.commit09911bf2008-07-26 23:55:291310}
1311
[email protected]71f65dd2009-02-11 19:14:561312void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1313 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291314
1315 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561316 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341317 TabContents* tab_contents =
1318 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161319 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1320 if (rph)
1321 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291322 }
initial.commit09911bf2008-07-26 23:55:291323}
1324
1325void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331326 NOTREACHED() << "This function has been deprecated. "
1327 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291328}
1329
[email protected]71f65dd2009-02-11 19:14:561330void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291331 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561332 const std::wstring& script,
1333 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291334 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341335 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1336 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571337 // Set the routing id of this message with the controller.
1338 // This routing id needs to be remembered for the reverse
1339 // communication while sending back the response of
1340 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331341 std::wstring set_automation_id;
1342 SStringPrintf(&set_automation_id,
1343 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561344 reply_message->routing_id());
1345
1346 DCHECK(reply_message_ == NULL);
1347 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291348
[email protected]57c6a652009-05-04 07:58:341349 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331350 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341351 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061352 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571353 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291354 }
1355
1356 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561357 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1358 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291359 }
1360}
1361
[email protected]71f65dd2009-02-11 19:14:561362void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1363 *visible = false;
[email protected]20e93d12008-08-28 16:31:571364
[email protected]59560e0b2009-06-04 03:30:221365 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251366#if defined(OS_CHROMEOS)
1367 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1368 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1369 for (BrowserList::const_iterator it = BrowserList::begin();
1370 it != BrowserList::end(); ++it) {
1371 if ((*it)->type() == Browser::TYPE_POPUP) {
1372 const GURL& url =
1373 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1374
1375 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1376 url.host() == chrome::kChromeUIFileBrowseHost) {
1377 *visible = true;
1378 break;
1379 }
1380 }
1381 }
1382#else
[email protected]59560e0b2009-06-04 03:30:221383 Browser* browser = browser_tracker_->GetResource(handle);
1384 if (browser) {
1385 *visible = browser->window()->IsDownloadShelfVisible();
1386 }
[email protected]f5bf8ccf2010-02-05 18:19:251387#endif
[email protected]59560e0b2009-06-04 03:30:221388 }
initial.commit09911bf2008-07-26 23:55:291389}
1390
[email protected]59560e0b2009-06-04 03:30:221391void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1392 if (browser_tracker_->ContainsHandle(handle)) {
1393 Browser* browser = browser_tracker_->GetResource(handle);
1394 if (browser) {
1395 if (visible)
1396 browser->window()->GetDownloadShelf()->Show();
1397 else
1398 browser->window()->GetDownloadShelf()->Close();
1399 }
1400 }
1401}
1402
[email protected]34930432009-11-09 00:12:091403void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1404 *visible = false;
1405
1406 if (browser_tracker_->ContainsHandle(handle)) {
1407 Browser* browser = browser_tracker_->GetResource(handle);
1408 if (browser)
1409 *visible = browser->window()->IsFullscreen();
1410 }
1411}
1412
1413void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1414 bool* visible) {
1415 *visible = false;
1416
1417 if (browser_tracker_->ContainsHandle(handle)) {
1418 Browser* browser = browser_tracker_->GetResource(handle);
1419 if (browser)
1420 *visible = browser->window()->IsFullscreenBubbleVisible();
1421 }
1422}
[email protected]59560e0b2009-06-04 03:30:221423
[email protected]71f65dd2009-02-11 19:14:561424void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1425 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291426 if (tab_tracker_->ContainsHandle(handle)) {
1427 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111428 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291429 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561430 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291431 }
1432 }
initial.commit09911bf2008-07-26 23:55:291433}
1434
initial.commit09911bf2008-07-26 23:55:291435void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561436 int handle, const std::wstring& find_request,
1437 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311438 NOTREACHED() << "This function has been deprecated."
1439 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561440 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311441 return;
1442}
1443
[email protected]4f999132009-03-31 18:08:401444void AutomationProvider::HandleFindRequest(
1445 int handle,
1446 const AutomationMsg_Find_Params& params,
1447 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291448 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561449 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1450 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291451 return;
1452 }
1453
1454 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111455 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291456
1457 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141458 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291459
[email protected]57c6a652009-05-04 07:58:341460 tab_contents->set_current_find_request_id(
1461 FindInPageNotificationObserver::kFindInPageRequestId);
1462 tab_contents->render_view_host()->StartFinding(
1463 FindInPageNotificationObserver::kFindInPageRequestId,
1464 params.search_string, params.forward, params.match_case,
1465 params.find_next);
initial.commit09911bf2008-07-26 23:55:291466}
1467
[email protected]5f8af2a2008-08-06 22:49:451468void AutomationProvider::HandleOpenFindInPageRequest(
1469 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291470 if (browser_tracker_->ContainsHandle(handle)) {
1471 Browser* browser = browser_tracker_->GetResource(handle);
1472 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451473 }
1474}
1475
[email protected]71f65dd2009-02-11 19:14:561476void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561477 *visible = false;
[email protected]855c0142009-09-28 22:35:241478 Browser* browser = browser_tracker_->GetResource(handle);
1479 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581480 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171481 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241482 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291483 }
[email protected]20e93d12008-08-28 16:31:571484}
1485
[email protected]71f65dd2009-02-11 19:14:561486void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1487 int* y) {
[email protected]9e0534b2008-10-21 15:03:011488 gfx::Point position(0, 0);
1489 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291490 if (browser_tracker_->ContainsHandle(handle)) {
1491 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581492 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171493 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581494 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291495 }
[email protected]20e93d12008-08-28 16:31:571496
[email protected]71f65dd2009-02-11 19:14:561497 *x = position.x();
1498 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571499}
1500
[email protected]4512cb52010-04-05 19:50:251501// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1502// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581503void AutomationProvider::GetBookmarkBarVisibility(int handle,
1504 bool* visible,
1505 bool* animating) {
1506 *visible = false;
1507 *animating = false;
1508
1509 if (browser_tracker_->ContainsHandle(handle)) {
1510 Browser* browser = browser_tracker_->GetResource(handle);
1511 if (browser) {
[email protected]472f099b2010-05-27 17:07:121512#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1513 // TODO(jrg): Was removed in rev43789 for perf. Need to investigate.
1514
[email protected]ab6ca392010-04-07 00:44:131515 // IsBookmarkBarVisible() line looks correct but is not
1516 // consistent across platforms. Specifically, on Mac/Linux, it
1517 // returns false if the bar is hidden in a pref (even if visible
1518 // on the NTP). On ChromeOS, it returned true if on NTP
1519 // independent of the pref. Making the code more consistent
1520 // caused a perf bot regression on Windows (which shares views).
1521 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251522 *visible = browser->profile()->GetPrefs()->GetBoolean(
1523 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401524#else
1525 *visible = browser->window()->IsBookmarkBarVisible();
1526#endif
[email protected]c3240722010-03-05 21:52:581527 *animating = browser->window()->IsBookmarkBarAnimating();
1528 }
1529 }
1530}
1531
[email protected]6d8ffc9f2010-03-12 18:27:531532void AutomationProvider::GetBookmarksAsJSON(int handle,
1533 std::string* bookmarks_as_json,
1534 bool *success) {
1535 *success = false;
1536 if (browser_tracker_->ContainsHandle(handle)) {
1537 Browser* browser = browser_tracker_->GetResource(handle);
1538 if (browser) {
1539 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1540 return;
1541 }
1542 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1543 browser->profile(),
1544 browser->profile()->GetBookmarkModel());
1545 *success = storage->SerializeData(bookmarks_as_json);
1546 }
1547 }
1548}
1549
1550void AutomationProvider::WaitForBookmarkModelToLoad(
1551 int handle,
1552 IPC::Message* reply_message) {
1553 if (browser_tracker_->ContainsHandle(handle)) {
1554 Browser* browser = browser_tracker_->GetResource(handle);
1555 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1556 if (model->IsLoaded()) {
1557 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1558 reply_message, true);
1559 Send(reply_message);
1560 } else {
1561 // The observer will delete itself when done.
1562 new AutomationProviderBookmarkModelObserver(this, reply_message,
1563 model);
1564 }
1565 }
1566}
1567
1568void AutomationProvider::AddBookmarkGroup(int handle,
1569 int64 parent_id, int index,
1570 std::wstring title,
1571 bool* success) {
1572 if (browser_tracker_->ContainsHandle(handle)) {
1573 Browser* browser = browser_tracker_->GetResource(handle);
1574 if (browser) {
1575 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1576 if (!model->IsLoaded()) {
1577 *success = false;
1578 return;
1579 }
1580 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1581 DCHECK(parent);
1582 if (parent) {
1583 const BookmarkNode* child = model->AddGroup(parent, index,
1584 WideToUTF16(title));
1585 DCHECK(child);
1586 if (child)
1587 *success = true;
1588 }
1589 }
1590 }
1591 *success = false;
1592}
1593
1594void AutomationProvider::AddBookmarkURL(int handle,
1595 int64 parent_id, int index,
1596 std::wstring title, const GURL& url,
1597 bool* success) {
1598 if (browser_tracker_->ContainsHandle(handle)) {
1599 Browser* browser = browser_tracker_->GetResource(handle);
1600 if (browser) {
1601 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1602 if (!model->IsLoaded()) {
1603 *success = false;
1604 return;
1605 }
1606 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1607 DCHECK(parent);
1608 if (parent) {
1609 const BookmarkNode* child = model->AddURL(parent, index,
1610 WideToUTF16(title), url);
1611 DCHECK(child);
1612 if (child)
1613 *success = true;
1614 }
1615 }
1616 }
1617 *success = false;
1618}
1619
1620void AutomationProvider::ReparentBookmark(int handle,
1621 int64 id, int64 new_parent_id,
1622 int index,
1623 bool* success) {
1624 if (browser_tracker_->ContainsHandle(handle)) {
1625 Browser* browser = browser_tracker_->GetResource(handle);
1626 if (browser) {
1627 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1628 if (!model->IsLoaded()) {
1629 *success = false;
1630 return;
1631 }
1632 const BookmarkNode* node = model->GetNodeByID(id);
1633 DCHECK(node);
1634 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1635 DCHECK(new_parent);
1636 if (node && new_parent) {
1637 model->Move(node, new_parent, index);
1638 *success = true;
1639 }
1640 }
1641 }
1642 *success = false;
1643}
1644
1645void AutomationProvider::SetBookmarkTitle(int handle,
1646 int64 id, std::wstring title,
1647 bool* success) {
1648 if (browser_tracker_->ContainsHandle(handle)) {
1649 Browser* browser = browser_tracker_->GetResource(handle);
1650 if (browser) {
1651 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1652 if (!model->IsLoaded()) {
1653 *success = false;
1654 return;
1655 }
1656 const BookmarkNode* node = model->GetNodeByID(id);
1657 DCHECK(node);
1658 if (node) {
1659 model->SetTitle(node, WideToUTF16(title));
1660 *success = true;
1661 }
1662 }
1663 }
1664 *success = false;
1665}
1666
1667void AutomationProvider::SetBookmarkURL(int handle,
1668 int64 id, const GURL& url,
1669 bool* success) {
1670 if (browser_tracker_->ContainsHandle(handle)) {
1671 Browser* browser = browser_tracker_->GetResource(handle);
1672 if (browser) {
1673 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1674 if (!model->IsLoaded()) {
1675 *success = false;
1676 return;
1677 }
1678 const BookmarkNode* node = model->GetNodeByID(id);
1679 DCHECK(node);
1680 if (node) {
1681 model->SetURL(node, url);
1682 *success = true;
1683 }
1684 }
1685 }
1686 *success = false;
1687}
1688
1689void AutomationProvider::RemoveBookmark(int handle,
1690 int64 id,
1691 bool* success) {
1692 if (browser_tracker_->ContainsHandle(handle)) {
1693 Browser* browser = browser_tracker_->GetResource(handle);
1694 if (browser) {
1695 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1696 if (!model->IsLoaded()) {
1697 *success = false;
1698 return;
1699 }
1700 const BookmarkNode* node = model->GetNodeByID(id);
1701 DCHECK(node);
1702 if (node) {
1703 const BookmarkNode* parent = node->GetParent();
1704 DCHECK(parent);
1705 model->Remove(parent, parent->IndexOfChild(node));
1706 *success = true;
1707 }
1708 }
1709 }
1710 *success = false;
1711}
1712
[email protected]ef413ca2010-05-25 21:09:141713// Sample json input: { "command": "SetWindowDimensions",
1714// "x": 20, # optional
1715// "y": 20, # optional
1716// "width": 800, # optional
1717// "height": 600 } # optional
1718void AutomationProvider::SetWindowDimensions(Browser* browser,
1719 DictionaryValue* args,
1720 IPC::Message* reply_message) {
1721 gfx::Rect rect = browser->window()->GetRestoredBounds();
1722 int x, y, width, height;
1723 if (args->GetInteger(L"x", &x))
1724 rect.set_x(x);
1725 if (args->GetInteger(L"y", &y))
1726 rect.set_y(y);
1727 if (args->GetInteger(L"width", &width))
1728 rect.set_width(width);
1729 if (args->GetInteger(L"height", &height))
1730 rect.set_height(height);
1731 browser->window()->SetBounds(rect);
[email protected]7c983cc2010-07-16 11:33:341732 AutomationJSONReply(this, reply_message).SendSuccess(NULL);
[email protected]ef413ca2010-05-25 21:09:141733}
1734
[email protected]a9ff2c02010-05-13 17:33:051735// Sample json input: { "command": "GetBrowserInfo" }
1736// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1737// sample json output.
[email protected]53329582010-05-14 21:10:581738void AutomationProvider::GetBrowserInfo(Browser* browser,
1739 DictionaryValue* args,
[email protected]a9ff2c02010-05-13 17:33:051740 IPC::Message* reply_message) {
[email protected]a9ff2c02010-05-13 17:33:051741 DictionaryValue* properties = new DictionaryValue;
1742 properties->SetString(L"ChromeVersion", chrome::kChromeVersion);
1743 properties->SetString(L"BrowserProcessExecutableName",
1744 chrome::kBrowserProcessExecutableName);
1745 properties->SetString(L"HelperProcessExecutableName",
1746 chrome::kHelperProcessExecutableName);
1747 properties->SetString(L"BrowserProcessExecutablePath",
1748 chrome::kBrowserProcessExecutablePath);
1749 properties->SetString(L"HelperProcessExecutablePath",
1750 chrome::kHelperProcessExecutablePath);
[email protected]a9ff2c02010-05-13 17:33:051751 properties->SetString(L"command_line_string",
1752 CommandLine::ForCurrentProcess()->command_line_string());
[email protected]44eed9f2010-06-28 22:04:001753
1754 std::string branding;
1755#if defined(GOOGLE_CHROME_BUILD)
1756 branding = "Google Chrome";
1757#elif defined(CHROMIUM_BUILD)
1758 branding = "Chromium";
1759#else
1760 branding = "Unknown Branding";
[email protected]a9ff2c02010-05-13 17:33:051761#endif
[email protected]44eed9f2010-06-28 22:04:001762 properties->SetString(L"branding", branding);
[email protected]a9ff2c02010-05-13 17:33:051763
1764 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1765 return_value->Set(L"properties", properties);
1766
[email protected]ef413ca2010-05-25 21:09:141767 return_value->SetInteger(L"browser_pid", base::GetCurrentProcId());
1768 // Add info about all windows in a list of dictionaries, one dictionary
1769 // item per window.
1770 ListValue* windows = new ListValue;
1771 int windex = 0;
1772 for (BrowserList::const_iterator it = BrowserList::begin();
1773 it != BrowserList::end();
1774 ++it, ++windex) {
1775 DictionaryValue* browser_item = new DictionaryValue;
1776 browser = *it;
1777 browser_item->SetInteger(L"index", windex);
1778 // Window properties
1779 gfx::Rect rect = browser->window()->GetRestoredBounds();
1780 browser_item->SetInteger(L"x", rect.x());
1781 browser_item->SetInteger(L"y", rect.y());
1782 browser_item->SetInteger(L"width", rect.width());
1783 browser_item->SetInteger(L"height", rect.height());
1784 browser_item->SetBoolean(L"fullscreen",
1785 browser->window()->IsFullscreen());
1786 browser_item->SetInteger(L"selected_tab", browser->selected_index());
1787 browser_item->SetBoolean(L"incognito",
1788 browser->profile()->IsOffTheRecord());
1789 // For each window, add info about all tabs in a list of dictionaries,
1790 // one dictionary item per tab.
1791 ListValue* tabs = new ListValue;
1792 for (int i = 0; i < browser->tab_count(); ++i) {
1793 TabContents* tc = browser->GetTabContentsAt(i);
1794 DictionaryValue* tab = new DictionaryValue;
1795 tab->SetInteger(L"index", i);
1796 tab->SetString(L"url", tc->GetURL().spec());
1797 tab->SetInteger(L"renderer_pid",
1798 base::GetProcId(tc->GetRenderProcessHost()->GetHandle()));
1799 tab->SetInteger(L"num_infobars", tc->infobar_delegate_count());
1800 tabs->Append(tab);
1801 }
1802 browser_item->Set(L"tabs", tabs);
1803
1804 windows->Append(browser_item);
1805 }
1806 return_value->Set(L"windows", windows);
1807
1808 return_value->SetString(L"child_process_path",
1809 ChildProcessHost::GetChildPath(true).value());
1810 // Child processes are the processes for plugins and other workers.
1811 // Add all child processes in a list of dictionaries, one dictionary item
1812 // per child process.
1813 ListValue* child_processes = new ListValue;
[email protected]d27893f62010-07-03 05:47:421814 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
[email protected]ef413ca2010-05-25 21:09:141815 // Only add processes which are already started, since we need their handle.
1816 if ((*iter)->handle() != base::kNullProcessHandle) {
1817 ChildProcessInfo* info = *iter;
1818 DictionaryValue* item = new DictionaryValue;
1819 item->SetString(L"name", info->name());
1820 item->SetString(L"type",
1821 ChildProcessInfo::GetTypeNameInEnglish(info->type()));
1822 item->SetInteger(L"pid", base::GetProcId(info->handle()));
1823 child_processes->Append(item);
1824 }
1825 }
1826 return_value->Set(L"child_processes", child_processes);
1827
1828 // Add all extension processes in a list of dictionaries, one dictionary
1829 // item per extension process.
1830 ListValue* extension_processes = new ListValue;
1831 ProfileManager* profile_manager = g_browser_process->profile_manager();
1832 for (ProfileManager::const_iterator it = profile_manager->begin();
1833 it != profile_manager->end(); ++it) {
1834 ExtensionProcessManager* process_manager =
1835 (*it)->GetExtensionProcessManager();
1836 ExtensionProcessManager::const_iterator jt;
1837 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) {
1838 ExtensionHost* ex_host = *jt;
1839 // Don't add dead extension processes.
1840 if (!ex_host->IsRenderViewLive())
1841 continue;
1842 DictionaryValue* item = new DictionaryValue;
1843 item->SetString(L"name", ex_host->extension()->name());
1844 item->SetInteger(
1845 L"pid",
1846 base::GetProcId(ex_host->render_process_host()->GetHandle()));
1847 extension_processes->Append(item);
1848 }
1849 }
1850 return_value->Set(L"extension_processes", extension_processes);
[email protected]7c983cc2010-07-16 11:33:341851 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]a9ff2c02010-05-13 17:33:051852}
1853
[email protected]24e2b102010-04-29 17:56:471854// Sample json input: { "command": "GetHistoryInfo",
1855// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361856// Refer chrome/test/pyautolib/history_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581857void AutomationProvider::GetHistoryInfo(Browser* browser,
1858 DictionaryValue* args,
1859 IPC::Message* reply_message) {
[email protected]e6e376e2010-04-19 21:41:361860 consumer_.CancelAllRequests();
1861
[email protected]e53668962010-06-23 15:35:251862 string16 search_text;
1863 args->GetString("search_text", &search_text);
[email protected]e6e376e2010-04-19 21:41:361864
1865 // Fetch history.
1866 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1867 history::QueryOptions options;
1868 // The observer owns itself. It deletes itself after it fetches history.
1869 AutomationProviderHistoryObserver* history_observer =
1870 new AutomationProviderHistoryObserver(this, reply_message);
1871 hs->QueryHistory(
1872 search_text,
1873 options,
1874 &consumer_,
1875 NewCallback(history_observer,
1876 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1877}
1878
[email protected]bbe6aa02010-05-07 17:27:291879// Sample json input: { "command": "AddHistoryItem",
1880// "item": { "URL": "http://www.google.com",
1881// "title": "Google", # optional
1882// "time": 12345 # optional (time_t)
1883// } }
1884// Refer chrome/test/pyautolib/pyauto.py for details on input.
[email protected]53329582010-05-14 21:10:581885void AutomationProvider::AddHistoryItem(Browser* browser,
1886 DictionaryValue* args,
1887 IPC::Message* reply_message) {
[email protected]bbe6aa02010-05-07 17:27:291888 DictionaryValue* item = NULL;
1889 args->GetDictionary(L"item", &item);
1890 string16 url_text;
[email protected]e53668962010-06-23 15:35:251891 string16 title;
[email protected]bbe6aa02010-05-07 17:27:291892 base::Time time = base::Time::Now();
[email protected]7c983cc2010-07-16 11:33:341893 AutomationJSONReply reply(this, reply_message);
[email protected]bbe6aa02010-05-07 17:27:291894
[email protected]7c983cc2010-07-16 11:33:341895 if (!item->GetString("url", &url_text)) {
1896 reply.SendError("bad args (no URL in dict?)");
1897 return;
[email protected]bbe6aa02010-05-07 17:27:291898 }
[email protected]7c983cc2010-07-16 11:33:341899 GURL gurl(url_text);
1900 item->GetString("title", &title); // Don't care if it fails.
1901 int it;
1902 double dt;
1903 if (item->GetInteger(L"time", &it))
1904 time = base::Time::FromTimeT(it);
1905 else if (item->GetReal(L"time", &dt))
1906 time = base::Time::FromDoubleT(dt);
[email protected]f6ff0df2010-07-11 22:41:431907
[email protected]7c983cc2010-07-16 11:33:341908 // Ideas for "dummy" values (e.g. id_scope) came from
1909 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
1910 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1911 const void* id_scope = reinterpret_cast<void*>(1);
1912 hs->AddPage(gurl, time,
1913 id_scope,
1914 0,
1915 GURL(),
1916 PageTransition::LINK,
1917 history::RedirectList(),
1918 false);
1919 if (title.length())
1920 hs->SetPageTitle(gurl, title);
1921 reply.SendSuccess(NULL);
[email protected]bbe6aa02010-05-07 17:27:291922}
1923
[email protected]24e2b102010-04-29 17:56:471924// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361925// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581926void AutomationProvider::GetDownloadsInfo(Browser* browser,
1927 DictionaryValue* args,
1928 IPC::Message* reply_message) {
[email protected]d4adc292010-04-15 18:06:391929 AutomationProviderDownloadManagerObserver observer;
1930 std::vector<DownloadItem*> downloads;
1931 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]7c983cc2010-07-16 11:33:341932 AutomationJSONReply reply(this, reply_message);
[email protected]d4adc292010-04-15 18:06:391933
1934 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:341935 reply.SendError("no download manager");
1936 return;
[email protected]d4adc292010-04-15 18:06:391937 }
[email protected]7c983cc2010-07-16 11:33:341938 // 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();
[email protected]d4adc292010-04-15 18:06:391943
1944 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1945 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1946 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1947 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1948 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1949
1950 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1951 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1952 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1953 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1954 std::string("DANGEROUS_BUT_VALIDATED");
1955
1956 ListValue* list_of_downloads = new ListValue;
1957 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1958 it != downloads.end();
1959 it++) { // Fill info about each download item.
1960 DictionaryValue* dl_item_value = new DictionaryValue;
1961 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1962 dl_item_value->SetString(L"url", (*it)->url().spec());
1963 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1964 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1965 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1966 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1967 dl_item_value->SetBoolean(L"open_when_complete",
1968 (*it)->open_when_complete());
1969 dl_item_value->SetBoolean(L"is_extension_install",
1970 (*it)->is_extension_install());
1971 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1972 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1973 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1974 dl_item_value->SetString(L"safety_state",
1975 safety_state_to_string[(*it)->safety_state()]);
1976 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1977 list_of_downloads->Append(dl_item_value);
1978 }
1979 return_value->Set(L"downloads", list_of_downloads);
[email protected]d4adc292010-04-15 18:06:391980
[email protected]7c983cc2010-07-16 11:33:341981 reply.SendSuccess(return_value.get());
[email protected]d4adc292010-04-15 18:06:391982 // All value objects allocated above are owned by |return_value|
1983 // and get freed by it.
1984}
1985
[email protected]59a611242010-04-02 02:24:041986void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:581987 Browser* browser,
[email protected]59a611242010-04-02 02:24:041988 DictionaryValue* args,
1989 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:041990 AutomationProviderDownloadManagerObserver observer;
1991 std::vector<DownloadItem*> downloads;
[email protected]7c983cc2010-07-16 11:33:341992 AutomationJSONReply reply(this, reply_message);
[email protected]59a611242010-04-02 02:24:041993
1994 // Look for a quick return.
1995 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:341996 reply.SendSuccess(NULL); // No download manager.
1997 return;
[email protected]59a611242010-04-02 02:24:041998 }
[email protected]7c983cc2010-07-16 11:33:341999 profile_->GetDownloadManager()->GetCurrentDownloads(&observer, FilePath());
2000 downloads = observer.Downloads();
2001 if (downloads.size() == 0) {
2002 reply.SendSuccess(NULL);
[email protected]f6ff0df2010-07-11 22:41:432003 return;
2004 }
[email protected]59a611242010-04-02 02:24:042005
2006 // The observer owns itself. When the last observed item pings, it
2007 // deletes itself.
2008 AutomationProviderDownloadItemObserver* item_observer =
2009 new AutomationProviderDownloadItemObserver(
2010 this, reply_message, downloads.size());
2011 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
2012 i != downloads.end();
2013 i++) {
2014 (*i)->AddObserver(item_observer);
2015 }
2016}
2017
[email protected]24e2b102010-04-29 17:56:472018// Sample json input: { "command": "GetPrefsInfo" }
2019// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582020void AutomationProvider::GetPrefsInfo(Browser* browser,
2021 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472022 IPC::Message* reply_message) {
[email protected]24e2b102010-04-29 17:56:472023 const PrefService::PreferenceSet& prefs =
2024 profile_->GetPrefs()->preference_set();
2025 DictionaryValue* items = new DictionaryValue;
2026 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
2027 it != prefs.end(); ++it) {
2028 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
2029 }
2030 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2031 return_value->Set(L"prefs", items); // return_value owns items.
[email protected]7c983cc2010-07-16 11:33:342032 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]24e2b102010-04-29 17:56:472033}
2034
2035// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:582036void AutomationProvider::SetPrefs(Browser* browser,
2037 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472038 IPC::Message* reply_message) {
[email protected]24e2b102010-04-29 17:56:472039 std::wstring path;
2040 Value* val;
[email protected]7c983cc2010-07-16 11:33:342041 AutomationJSONReply reply(this, reply_message);
[email protected]24e2b102010-04-29 17:56:472042 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
2043 PrefService* pref_service = profile_->GetPrefs();
2044 const PrefService::Preference* pref =
2045 pref_service->FindPreference(path.c_str());
2046 if (!pref) { // Not a registered pref.
[email protected]7c983cc2010-07-16 11:33:342047 reply.SendError("pref not registered.");
2048 return;
[email protected]24e2b102010-04-29 17:56:472049 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
[email protected]7c983cc2010-07-16 11:33:342050 reply.SendError("pref is managed. cannot be changed.");
2051 return;
[email protected]24e2b102010-04-29 17:56:472052 } else { // Set the pref.
2053 pref_service->Set(path.c_str(), *val);
2054 }
2055 } else {
[email protected]7c983cc2010-07-16 11:33:342056 reply.SendError("no pref path or value given.");
2057 return;
[email protected]24e2b102010-04-29 17:56:472058 }
2059
[email protected]7c983cc2010-07-16 11:33:342060 reply.SendSuccess(NULL);
[email protected]24e2b102010-04-29 17:56:472061}
2062
[email protected]53329582010-05-14 21:10:582063// Sample json input: { "command": "GetOmniboxInfo" }
2064// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
2065void AutomationProvider::GetOmniboxInfo(Browser* browser,
2066 DictionaryValue* args,
2067 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582068 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2069
2070 LocationBar* loc_bar = browser->window()->GetLocationBar();
2071 AutocompleteEditView* edit_view = loc_bar->location_entry();
2072 AutocompleteEditModel* model = edit_view->model();
2073
2074 // Fill up matches.
2075 ListValue* matches = new ListValue;
2076 const AutocompleteResult& result = model->result();
2077 for (AutocompleteResult::const_iterator i = result.begin();
2078 i != result.end(); ++i) {
2079 const AutocompleteMatch& match = *i;
2080 DictionaryValue* item = new DictionaryValue; // owned by return_value
2081 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
2082 item->SetBoolean(L"starred", match.starred);
2083 item->SetString(L"destination_url", match.destination_url.spec());
2084 item->SetString(L"contents", match.contents);
2085 item->SetString(L"description", match.description);
2086 matches->Append(item);
2087 }
2088 return_value->Set(L"matches", matches);
2089
2090 // Fill up other properties.
2091 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2092 properties->SetBoolean(L"has_focus", model->has_focus());
2093 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2094 properties->SetString(L"keyword", model->keyword());
2095 properties->SetString(L"text", edit_view->GetText());
2096 return_value->Set(L"properties", properties);
2097
[email protected]7c983cc2010-07-16 11:33:342098 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]53329582010-05-14 21:10:582099}
2100
2101// Sample json input: { "command": "SetOmniboxText",
2102// "text": "goog" }
2103void AutomationProvider::SetOmniboxText(Browser* browser,
2104 DictionaryValue* args,
2105 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582106 std::wstring text;
[email protected]7c983cc2010-07-16 11:33:342107 AutomationJSONReply reply(this, reply_message);
[email protected]53329582010-05-14 21:10:582108 if (!args->GetString(L"text", &text)) {
[email protected]7c983cc2010-07-16 11:33:342109 reply.SendError("text missing");
2110 return;
[email protected]53329582010-05-14 21:10:582111 }
[email protected]7c983cc2010-07-16 11:33:342112 browser->FocusLocationBar();
2113 LocationBar* loc_bar = browser->window()->GetLocationBar();
2114 AutocompleteEditView* edit_view = loc_bar->location_entry();
2115 edit_view->model()->OnSetFocus(false);
2116 edit_view->SetUserText(text);
2117 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582118}
2119
2120// Sample json input: { "command": "OmniboxMovePopupSelection",
2121// "count": 1 }
2122// Negative count implies up, positive implies down. Count values will be
2123// capped by the size of the popup list.
2124void AutomationProvider::OmniboxMovePopupSelection(
2125 Browser* browser,
2126 DictionaryValue* args,
2127 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582128 int count;
[email protected]7c983cc2010-07-16 11:33:342129 AutomationJSONReply reply(this, reply_message);
[email protected]53329582010-05-14 21:10:582130 if (!args->GetInteger(L"count", &count)) {
[email protected]7c983cc2010-07-16 11:33:342131 reply.SendError("count missing");
2132 return;
[email protected]53329582010-05-14 21:10:582133 }
[email protected]7c983cc2010-07-16 11:33:342134 LocationBar* loc_bar = browser->window()->GetLocationBar();
2135 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2136 model->OnUpOrDownKeyPressed(count);
2137 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582138}
2139
2140// Sample json input: { "command": "OmniboxAcceptInput" }
2141void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2142 DictionaryValue* args,
2143 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282144 NavigationController& controller =
2145 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122146 // Setup observer to wait until the selected item loads.
2147 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282148 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122149 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582150
2151 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582152}
2153
[email protected]a3cd5022010-06-16 18:25:292154// Sample json input: { "command": "GetInitialLoadTimes" }
2155// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2156void AutomationProvider::GetInitialLoadTimes(
2157 Browser*,
2158 DictionaryValue*,
2159 IPC::Message* reply_message) {
2160 scoped_ptr<DictionaryValue> return_value(
2161 initial_load_observer_->GetTimingInformation());
[email protected]f6ff0df2010-07-11 22:41:432162
2163 std::string json_return;
2164 base::JSONWriter::Write(return_value.get(), false, &json_return);
2165 AutomationMsg_SendJSONRequest::WriteReplyParams(
2166 reply_message, json_return, true);
2167 Send(reply_message);
[email protected]a3cd5022010-06-16 18:25:292168}
2169
[email protected]f7d48012010-05-06 08:17:052170// Sample json input: { "command": "GetPluginsInfo" }
2171// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582172void AutomationProvider::GetPluginsInfo(Browser* browser,
2173 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052174 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052175 std::vector<WebPluginInfo> plugins;
2176 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2177 ListValue* items = new ListValue;
2178 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2179 it != plugins.end();
2180 ++it) {
2181 DictionaryValue* item = new DictionaryValue;
[email protected]c9d811372010-06-23 21:44:572182 item->SetStringFromUTF16(L"name", it->name);
[email protected]f7d48012010-05-06 08:17:052183 item->SetString(L"path", it->path.value());
[email protected]c9d811372010-06-23 21:44:572184 item->SetStringFromUTF16(L"version", it->version);
2185 item->SetStringFromUTF16(L"desc", it->desc);
[email protected]f7d48012010-05-06 08:17:052186 item->SetBoolean(L"enabled", it->enabled);
2187 // Add info about mime types.
2188 ListValue* mime_types = new ListValue();
2189 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2190 it->mime_types.begin();
2191 type_it != it->mime_types.end();
2192 ++type_it) {
2193 DictionaryValue* mime_type = new DictionaryValue();
2194 mime_type->SetString(L"mimeType", type_it->mime_type);
[email protected]c9d811372010-06-23 21:44:572195 mime_type->SetStringFromUTF16(L"description", type_it->description);
[email protected]f7d48012010-05-06 08:17:052196
2197 ListValue* file_extensions = new ListValue();
2198 for (std::vector<std::string>::const_iterator ext_it =
2199 type_it->file_extensions.begin();
2200 ext_it != type_it->file_extensions.end();
2201 ++ext_it) {
2202 file_extensions->Append(new StringValue(*ext_it));
2203 }
2204 mime_type->Set(L"fileExtensions", file_extensions);
2205
2206 mime_types->Append(mime_type);
2207 }
2208 item->Set(L"mimeTypes", mime_types);
2209 items->Append(item);
2210 }
2211 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2212 return_value->Set(L"plugins", items); // return_value owns items.
2213
[email protected]7c983cc2010-07-16 11:33:342214 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]f7d48012010-05-06 08:17:052215}
2216
2217// Sample json input:
2218// { "command": "EnablePlugin",
2219// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582220void AutomationProvider::EnablePlugin(Browser* browser,
2221 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052222 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052223 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342224 AutomationJSONReply reply(this, reply_message);
[email protected]f6ff0df2010-07-11 22:41:432225 if (!args->GetString(L"path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342226 reply.SendError("path not specified.");
2227 return;
[email protected]f6ff0df2010-07-11 22:41:432228 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342229 reply.SendError(StringPrintf("Could not enable plugin for path %s.",
2230 path.c_str()));
2231 return;
[email protected]f6ff0df2010-07-11 22:41:432232 }
[email protected]7c983cc2010-07-16 11:33:342233 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052234}
2235
2236// Sample json input:
2237// { "command": "DisablePlugin",
2238// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582239void AutomationProvider::DisablePlugin(Browser* browser,
2240 DictionaryValue* args,
2241 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052242 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342243 AutomationJSONReply reply(this, reply_message);
[email protected]f6ff0df2010-07-11 22:41:432244 if (!args->GetString(L"path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342245 reply.SendError("path not specified.");
2246 return;
[email protected]f6ff0df2010-07-11 22:41:432247 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342248 reply.SendError(StringPrintf("Could not disable plugin for path %s.",
2249 path.c_str()));
2250 return;
[email protected]f6ff0df2010-07-11 22:41:432251 }
[email protected]7c983cc2010-07-16 11:33:342252 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052253}
2254
[email protected]7060bb292010-06-24 00:52:492255// Sample json input:
2256// { "command": "SaveTabContents",
2257// "tab_index": 0,
2258// "filename": <a full pathname> }
2259// Sample json output:
2260// {}
2261void AutomationProvider::SaveTabContents(Browser* browser,
2262 DictionaryValue* args,
2263 IPC::Message* reply_message) {
[email protected]7060bb292010-06-24 00:52:492264 int tab_index = 0;
2265 FilePath::StringType filename;
2266 FilePath::StringType parent_directory;
2267 TabContents* tab_contents = NULL;
[email protected]7c983cc2010-07-16 11:33:342268 AutomationJSONReply reply(this, reply_message);
[email protected]7060bb292010-06-24 00:52:492269
2270 if (!args->GetInteger(L"tab_index", &tab_index) ||
2271 !args->GetString(L"filename", &filename)) {
[email protected]7c983cc2010-07-16 11:33:342272 reply.SendError("tab_index or filename param missing");
2273 return;
[email protected]7060bb292010-06-24 00:52:492274 } else {
2275 tab_contents = browser->GetTabContentsAt(tab_index);
2276 if (!tab_contents) {
[email protected]7c983cc2010-07-16 11:33:342277 reply.SendError("no tab at tab_index");
[email protected]7060bb292010-06-24 00:52:492278 return;
2279 }
2280 }
[email protected]7c983cc2010-07-16 11:33:342281 // We're doing a SAVE_AS_ONLY_HTML so the the directory path isn't
2282 // used. Nevertheless, SavePackage requires it be valid. Sigh.
2283 parent_directory = FilePath(filename).DirName().value();
2284 if (!tab_contents->SavePage(FilePath(filename), FilePath(parent_directory),
2285 SavePackage::SAVE_AS_ONLY_HTML)) {
2286 reply.SendError("Could not initiate SavePage");
2287 return;
2288 }
2289 // The observer will delete itself when done.
2290 new SavePackageNotificationObserver(tab_contents->save_package(),
2291 this, reply_message);
[email protected]7060bb292010-06-24 00:52:492292}
2293
[email protected]c5aa5322010-07-15 19:00:072294// Refer to ImportSettings() in chrome/test/pyautolib/pyauto.py for sample
2295// json input.
2296// Sample json output: "{}"
2297void AutomationProvider::ImportSettings(Browser* browser,
2298 DictionaryValue* args,
2299 IPC::Message* reply_message) {
[email protected]c5aa5322010-07-15 19:00:072300 // Map from the json string passed over to the import item masks.
2301 std::map<std::string, ImportItem> string_to_import_item;
2302 string_to_import_item["HISTORY"] = importer::HISTORY;
2303 string_to_import_item["FAVORITES"] = importer::FAVORITES;
2304 string_to_import_item["COOKIES"] = importer::COOKIES;
2305 string_to_import_item["PASSWORDS"] = importer::PASSWORDS;
2306 string_to_import_item["SEARCH_ENGINES"] = importer::SEARCH_ENGINES;
2307 string_to_import_item["HOME_PAGE"] = importer::HOME_PAGE;
2308 string_to_import_item["ALL"] = importer::ALL;
2309
2310 std::wstring browser_name;
2311 int import_items = 0;
2312 ListValue* import_items_list = NULL;
2313 bool first_run;
2314
2315 if (!args->GetString(L"import_from", &browser_name) ||
2316 !args->GetBoolean(L"first_run", &first_run) ||
2317 !args->GetList(L"import_items", &import_items_list)) {
[email protected]7c983cc2010-07-16 11:33:342318 AutomationJSONReply(this, reply_message).SendError(
2319 "Incorrect type for one or more of the arguments.");
[email protected]c5aa5322010-07-15 19:00:072320 return;
2321 }
2322
2323 int num_items = import_items_list->GetSize();
2324 for (int i = 0; i < num_items; i++) {
2325 std::string item;
2326 import_items_list->GetString(i, &item);
2327 // If the provided string is not part of the map, error out.
2328 if (!ContainsKey(string_to_import_item, item)) {
[email protected]7c983cc2010-07-16 11:33:342329 AutomationJSONReply(this, reply_message).SendError(
2330 "Invalid item string found in import_items.");
[email protected]c5aa5322010-07-15 19:00:072331 return;
2332 }
2333 import_items |= string_to_import_item[item];
2334 }
2335
2336 ImporterHost* importer_host = new ImporterHost();
2337 // Get the correct ProfileInfo based on the browser they user provided.
2338 importer::ProfileInfo profile_info;
2339 int num_browsers = importer_host->GetAvailableProfileCount();
2340 int i = 0;
2341 for ( ; i < num_browsers; i++) {
2342 std::wstring name = importer_host->GetSourceProfileNameAt(i);
2343 if (name == browser_name) {
2344 profile_info = importer_host->GetSourceProfileInfoAt(i);
2345 break;
2346 }
2347 }
2348 // If we made it to the end of the loop, then the input was bad.
2349 if (i == num_browsers) {
[email protected]7c983cc2010-07-16 11:33:342350 AutomationJSONReply(this, reply_message).SendError(
2351 "Invalid browser name string found.");
[email protected]c5aa5322010-07-15 19:00:072352 return;
2353 }
2354
2355 Profile* profile = browser->profile();
2356
2357 importer_host->SetObserver(
2358 new AutomationProviderImportSettingsObserver(this, reply_message));
2359 importer_host->StartImportSettings(profile_info, profile, import_items,
2360 new ProfileWriter(profile), first_run);
2361}
2362
[email protected]5bcfe1672010-07-16 20:51:572363// See AddSavedPassword() in chrome/test/functional/pyauto.py for sample json
2364// input.
2365// Sample json output: { "password_added": true }
2366void AutomationProvider::AddSavedPassword(Browser* browser,
2367 DictionaryValue* args,
2368 IPC::Message* reply_message) {
2369 string16 username;
2370 string16 password;
2371 base::Time time = base::Time::Now();
2372 AutomationJSONReply reply(this, reply_message);
2373
2374 if (!args->GetStringAsUTF16(L"password", &password) ||
2375 !args->GetStringAsUTF16(L"username", &username)) {
2376 reply.SendError("Username and password must be strings.");
2377 return;
2378 }
2379
2380 // If the time is specified, change time to the specified time.
2381 int it;
2382 double dt;
2383 if (args->GetInteger(L"time", &it))
2384 time = base::Time::FromTimeT(it);
2385 else if (args->GetReal(L"time", &dt))
2386 time = base::Time::FromDoubleT(dt);
2387
2388 webkit_glue::PasswordForm new_password;
2389 new_password.username_value = username;
2390 new_password.password_value = password;
2391 new_password.date_created = time;
2392
2393 Profile* profile = browser->profile();
2394 // Use IMPLICIT_ACCESS since new passwords aren't added off the record.
2395 PasswordStore* password_store =
2396 profile->GetPasswordStore(Profile::IMPLICIT_ACCESS);
2397
2398 // Set the return based on whether setting the password succeeded.
2399 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2400
2401 // It will be null if it's accessed in an incognito window.
2402 if (password_store != NULL) {
2403 password_store->AddLogin(new_password);
2404 return_value->SetBoolean(L"password_added", true);
2405 } else {
2406 return_value->SetBoolean(L"password_added", false);
2407 }
2408
2409 reply.SendSuccess(return_value.get());
2410}
2411
2412// Sample json input: { "command": "GetSavedPasswords" }
2413// Refer to GetSavedPasswords() in chrome/test/pyautolib/pyauto.py for sample
2414// json output.
2415void AutomationProvider::GetSavedPasswords(Browser* browser,
2416 DictionaryValue* args,
2417 IPC::Message* reply_message) {
2418 Profile* profile = browser->profile();
2419 // Use EXPLICIT_ACCESS since saved passwords can be retreived off the record.
2420 PasswordStore* password_store =
2421 profile->GetPasswordStore(Profile::EXPLICIT_ACCESS);
2422 password_store->GetAutofillableLogins(
2423 new AutomationProviderGetPasswordsObserver(this, reply_message));
2424 // Observer deletes itself after returning.
2425}
2426
[email protected]a0fc50d72010-07-14 21:14:192427// Refer to ClearBrowsingData() in chrome/test/pyautolib/pyauto.py for sample
2428// json input.
2429// Sample json output: {}
2430void AutomationProvider::ClearBrowsingData(Browser* browser,
2431 DictionaryValue* args,
2432 IPC::Message* reply_message) {
[email protected]a0fc50d72010-07-14 21:14:192433 std::map<std::string, BrowsingDataRemover::TimePeriod> string_to_time_period;
2434 string_to_time_period["LAST_HOUR"] = BrowsingDataRemover::LAST_HOUR;
2435 string_to_time_period["LAST_DAY"] = BrowsingDataRemover::LAST_DAY;
2436 string_to_time_period["LAST_WEEK"] = BrowsingDataRemover::LAST_WEEK;
2437 string_to_time_period["FOUR_WEEKS"] = BrowsingDataRemover::FOUR_WEEKS;
2438 string_to_time_period["EVERYTHING"] = BrowsingDataRemover::EVERYTHING;
2439
2440 std::map<std::string, int> string_to_mask_value;
2441 string_to_mask_value["HISTORY"] = BrowsingDataRemover::REMOVE_HISTORY;
2442 string_to_mask_value["DOWNLOADS"] = BrowsingDataRemover::REMOVE_DOWNLOADS;
2443 string_to_mask_value["COOKIES"] = BrowsingDataRemover::REMOVE_COOKIES;
2444 string_to_mask_value["PASSWORDS"] = BrowsingDataRemover::REMOVE_PASSWORDS;
2445 string_to_mask_value["FORM_DATA"] = BrowsingDataRemover::REMOVE_FORM_DATA;
2446 string_to_mask_value["CACHE"] = BrowsingDataRemover::REMOVE_CACHE;
2447
2448 std::string time_period;
2449 ListValue* to_remove;
2450 if (!args->GetString(L"time_period", &time_period) ||
2451 !args->GetList(L"to_remove", &to_remove)) {
[email protected]7c983cc2010-07-16 11:33:342452 AutomationJSONReply(this, reply_message).SendError(
2453 "time_period must be a string and to_remove a list.");
[email protected]a0fc50d72010-07-14 21:14:192454 return;
2455 }
2456
2457 int remove_mask = 0;
2458 int num_removals = to_remove->GetSize();
2459 for (int i = 0; i < num_removals; i++) {
2460 std::string removal;
2461 to_remove->GetString(i, &removal);
2462 // If the provided string is not part of the map, then error out.
2463 if (!ContainsKey(string_to_mask_value, removal)) {
[email protected]7c983cc2010-07-16 11:33:342464 AutomationJSONReply(this, reply_message).SendError(
2465 "Invalid browsing data string found in to_remove.");
[email protected]a0fc50d72010-07-14 21:14:192466 return;
2467 }
2468 remove_mask |= string_to_mask_value[removal];
2469 }
2470
2471 if (!ContainsKey(string_to_time_period, time_period)) {
[email protected]7c983cc2010-07-16 11:33:342472 AutomationJSONReply(this, reply_message).SendError(
2473 "Invalid string for time_period.");
[email protected]a0fc50d72010-07-14 21:14:192474 return;
2475 }
2476
2477 BrowsingDataRemover* remover = new BrowsingDataRemover(
2478 profile(), string_to_time_period[time_period], base::Time());
2479
2480 remover->AddObserver(
2481 new AutomationProviderBrowsingDataObserver(this, reply_message));
2482 remover->Remove(remove_mask);
2483 // BrowsingDataRemover deletes itself using DeleteTask.
2484 // The observer also deletes itself after sending the reply.
2485}
2486
[email protected]4d1929f12010-07-10 00:09:032487// Sample json input: { "command": "GetThemeInfo" }
2488// Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output.
2489void AutomationProvider::GetThemeInfo(Browser* browser,
2490 DictionaryValue* args,
2491 IPC::Message* reply_message) {
2492 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2493 Extension* theme = browser->profile()->GetTheme();
2494 if (theme) {
2495 return_value->SetString(L"name", theme->name());
2496 return_value->Set(L"images", theme->GetThemeImages()->DeepCopy());
2497 return_value->Set(L"colors", theme->GetThemeColors()->DeepCopy());
2498 return_value->Set(L"tints", theme->GetThemeTints()->DeepCopy());
2499 }
[email protected]7c983cc2010-07-16 11:33:342500 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]4d1929f12010-07-10 00:09:032501}
2502
[email protected]55846ad842010-07-09 18:22:562503// Sample json input:
2504// { "command": "GetAutoFillProfile" }
2505// Refer to GetAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2506// json output.
2507void AutomationProvider::GetAutoFillProfile(Browser* browser,
2508 DictionaryValue* args,
2509 IPC::Message* reply_message) {
[email protected]55846ad842010-07-09 18:22:562510 // Get the AutoFillProfiles currently in the database.
2511 int tab_index = 0;
2512 args->GetInteger(L"tab_index", &tab_index);
2513 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
[email protected]7c983cc2010-07-16 11:33:342514 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:562515
2516 if (tab_contents) {
2517 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2518 ->GetPersonalDataManager();
2519 if (pdm) {
2520 std::vector<AutoFillProfile*> autofill_profiles = pdm->profiles();
2521 std::vector<CreditCard*> credit_cards = pdm->credit_cards();
2522
2523 ListValue* profiles = GetListFromAutoFillProfiles(autofill_profiles);
2524 ListValue* cards = GetListFromCreditCards(credit_cards);
2525
2526 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2527
2528 return_value->Set(L"profiles", profiles);
2529 return_value->Set(L"credit_cards", cards);
[email protected]7c983cc2010-07-16 11:33:342530 reply.SendSuccess(return_value.get());
[email protected]55846ad842010-07-09 18:22:562531 } else {
[email protected]7c983cc2010-07-16 11:33:342532 reply.SendError("No PersonalDataManager.");
2533 return;
[email protected]55846ad842010-07-09 18:22:562534 }
2535 } else {
[email protected]7c983cc2010-07-16 11:33:342536 reply.SendError("No tab at that index.");
2537 return;
[email protected]55846ad842010-07-09 18:22:562538 }
[email protected]55846ad842010-07-09 18:22:562539}
2540
2541// Refer to FillAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2542// json input.
2543// Sample json output: {}
2544void AutomationProvider::FillAutoFillProfile(Browser* browser,
2545 DictionaryValue* args,
2546 IPC::Message* reply_message) {
[email protected]7c983cc2010-07-16 11:33:342547 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:562548 ListValue* profiles = NULL;
2549 ListValue* cards = NULL;
2550 args->GetList(L"profiles", &profiles);
2551 args->GetList(L"credit_cards", &cards);
[email protected]7c983cc2010-07-16 11:33:342552 std::string error_mesg;
[email protected]55846ad842010-07-09 18:22:562553
2554 std::vector<AutoFillProfile> autofill_profiles;
2555 std::vector<CreditCard> credit_cards;
2556 // Create an AutoFillProfile for each of the dictionary profiles.
2557 if (profiles) {
[email protected]7c983cc2010-07-16 11:33:342558 autofill_profiles = GetAutoFillProfilesFromList(*profiles, &error_mesg);
[email protected]55846ad842010-07-09 18:22:562559 }
2560 // Create a CreditCard for each of the dictionary values.
2561 if (cards) {
[email protected]7c983cc2010-07-16 11:33:342562 credit_cards = GetCreditCardsFromList(*cards, &error_mesg);
2563 }
2564 if (!error_mesg.empty()) {
2565 reply.SendError(error_mesg);
2566 return;
[email protected]55846ad842010-07-09 18:22:562567 }
2568
2569 // Save the AutoFillProfiles.
2570 int tab_index = 0;
2571 args->GetInteger(L"tab_index", &tab_index);
2572 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2573
2574 if (tab_contents) {
2575 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2576 ->GetPersonalDataManager();
2577 if (pdm) {
2578 pdm->OnAutoFillDialogApply(profiles? &autofill_profiles : NULL,
2579 cards? &credit_cards : NULL);
2580 } else {
[email protected]7c983cc2010-07-16 11:33:342581 reply.SendError("No PersonalDataManager.");
2582 return;
[email protected]55846ad842010-07-09 18:22:562583 }
2584 } else {
[email protected]7c983cc2010-07-16 11:33:342585 reply.SendError("No tab at that index.");
2586 return;
[email protected]55846ad842010-07-09 18:22:562587 }
[email protected]7c983cc2010-07-16 11:33:342588 reply.SendSuccess(NULL);
[email protected]55846ad842010-07-09 18:22:562589}
2590
2591/* static */
2592ListValue* AutomationProvider::GetListFromAutoFillProfiles(
2593 std::vector<AutoFillProfile*> autofill_profiles) {
2594 ListValue* profiles = new ListValue;
2595
2596 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string
2597 = GetAutoFillFieldToStringMap();
2598
2599 // For each AutoFillProfile, transform it to a dictionary object to return.
2600 for (std::vector<AutoFillProfile*>::iterator it = autofill_profiles.begin();
2601 it != autofill_profiles.end(); ++it) {
2602 AutoFillProfile* profile = *it;
2603 DictionaryValue* profile_info = new DictionaryValue;
2604 profile_info->SetStringFromUTF16(L"label", profile->Label());
2605 // For each of the types, if it has a value, add it to the dictionary.
2606 for (std::map<AutoFillFieldType, std::wstring>::iterator
2607 type_it = autofill_type_to_string.begin();
2608 type_it != autofill_type_to_string.end(); ++type_it) {
2609 string16 value = profile->GetFieldText(AutoFillType(type_it->first));
2610 if (value.length()) { // If there was something stored for that value.
2611 profile_info->SetStringFromUTF16(type_it->second, value);
2612 }
2613 }
2614 profiles->Append(profile_info);
2615 }
2616 return profiles;
2617}
2618
2619/* static */
2620ListValue* AutomationProvider::GetListFromCreditCards(
2621 std::vector<CreditCard*> credit_cards) {
2622 ListValue* cards = new ListValue;
2623
2624 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2625 GetCreditCardFieldToStringMap();
2626
2627 // For each AutoFillProfile, transform it to a dictionary object to return.
2628 for (std::vector<CreditCard*>::iterator it = credit_cards.begin();
2629 it != credit_cards.end(); ++it) {
2630 CreditCard* card = *it;
2631 DictionaryValue* card_info = new DictionaryValue;
2632 card_info->SetStringFromUTF16(L"label", card->Label());
2633 // For each of the types, if it has a value, add it to the dictionary.
2634 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2635 credit_card_type_to_string.begin();
2636 type_it != credit_card_type_to_string.end(); ++type_it) {
2637 string16 value = card->GetFieldText(AutoFillType(type_it->first));
2638 // If there was something stored for that value.
2639 if (value.length()) {
2640 card_info->SetStringFromUTF16(type_it->second, value);
2641 }
2642 }
2643 cards->Append(card_info);
2644 }
2645 return cards;
2646}
2647
2648/* static */
2649std::vector<AutoFillProfile> AutomationProvider::GetAutoFillProfilesFromList(
[email protected]7c983cc2010-07-16 11:33:342650 const ListValue& profiles, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:562651 std::vector<AutoFillProfile> autofill_profiles;
2652 DictionaryValue* profile_info = NULL;
2653 string16 profile_label;
2654 string16 current_value;
2655
2656 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string =
2657 GetAutoFillFieldToStringMap();
2658
2659 int num_profiles = profiles.GetSize();
2660 for (int i = 0; i < num_profiles; i++) {
2661 profiles.GetDictionary(i, &profile_info);
2662 profile_info->GetString("label", &profile_label);
2663 // Choose an id of 0 so that a unique id will be created.
2664 AutoFillProfile profile(profile_label, 0);
2665 // Loop through the possible profile types and add those provided.
2666 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2667 autofill_type_to_string.begin();
2668 type_it != autofill_type_to_string.end(); ++type_it) {
2669 if (profile_info->HasKey(type_it->second)) {
2670 if (profile_info->GetStringAsUTF16(type_it->second, &current_value)) {
2671 profile.SetInfo(AutoFillType(type_it->first), current_value);
2672 } else {
[email protected]7c983cc2010-07-16 11:33:342673 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:562674 break;
2675 }
2676 }
2677 }
2678 autofill_profiles.push_back(profile);
2679 }
2680 return autofill_profiles;
2681}
2682
2683/* static */
2684std::vector<CreditCard> AutomationProvider::GetCreditCardsFromList(
[email protected]7c983cc2010-07-16 11:33:342685 const ListValue& cards, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:562686 std::vector<CreditCard> credit_cards;
2687 DictionaryValue* card_info = NULL;
2688 string16 card_label;
2689 string16 current_value;
2690
2691 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2692 GetCreditCardFieldToStringMap();
2693
2694 int num_credit_cards = cards.GetSize();
2695 for (int i = 0; i < num_credit_cards; i++) {
2696 cards.GetDictionary(i, &card_info);
2697 card_info->GetString("label", &card_label);
2698 CreditCard card(card_label, 0);
2699 // Loop through the possible credit card fields and add those provided.
2700 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2701 credit_card_type_to_string.begin();
2702 type_it != credit_card_type_to_string.end(); ++type_it) {
2703 if (card_info->HasKey(type_it->second)) {
2704 if (card_info->GetStringAsUTF16(type_it->second, &current_value)) {
2705 card.SetInfo(AutoFillType(type_it->first), current_value);
2706 } else {
[email protected]7c983cc2010-07-16 11:33:342707 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:562708 break;
2709 }
2710 }
2711 }
2712 credit_cards.push_back(card);
2713 }
2714 return credit_cards;
2715}
2716
2717/* static */
2718std::map<AutoFillFieldType, std::wstring>
2719 AutomationProvider::GetAutoFillFieldToStringMap() {
2720 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string;
2721 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2722 autofill_type_to_string[NAME_MIDDLE] = L"NAME_MIDDLE";
2723 autofill_type_to_string[NAME_LAST] = L"NAME_LAST";
2724 autofill_type_to_string[COMPANY_NAME] = L"COMPANY_NAME";
2725 autofill_type_to_string[EMAIL_ADDRESS] = L"EMAIL_ADDRESS";
2726 autofill_type_to_string[ADDRESS_HOME_LINE1] = L"ADDRESS_HOME_LINE1";
2727 autofill_type_to_string[ADDRESS_HOME_LINE2] = L"ADDRESS_HOME_LINE2";
2728 autofill_type_to_string[ADDRESS_HOME_CITY] = L"ADDRESS_HOME_CITY";
2729 autofill_type_to_string[ADDRESS_HOME_STATE] = L"ADDRESS_HOME_STATE";
2730 autofill_type_to_string[ADDRESS_HOME_ZIP] = L"ADDRESS_HOME_ZIP";
2731 autofill_type_to_string[ADDRESS_HOME_COUNTRY] = L"ADDRESS_HOME_COUNTRY";
2732 autofill_type_to_string[PHONE_HOME_NUMBER] = L"PHONE_HOME_NUMBER";
2733 autofill_type_to_string[PHONE_FAX_NUMBER] = L"PHONE_FAX_NUMBER";
2734 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2735 return autofill_type_to_string;
2736}
2737
2738/* static */
2739std::map<AutoFillFieldType, std::wstring>
2740 AutomationProvider::GetCreditCardFieldToStringMap() {
2741 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string;
2742 credit_card_type_to_string[CREDIT_CARD_NAME] = L"CREDIT_CARD_NAME";
2743 credit_card_type_to_string[CREDIT_CARD_NUMBER] = L"CREDIT_CARD_NUMBER";
2744 credit_card_type_to_string[CREDIT_CARD_TYPE] = L"CREDIT_CARD_TYPE";
2745 credit_card_type_to_string[CREDIT_CARD_EXP_MONTH] = L"CREDIT_CARD_EXP_MONTH";
2746 credit_card_type_to_string[CREDIT_CARD_EXP_4_DIGIT_YEAR] =
2747 L"CREDIT_CARD_EXP_4_DIGIT_YEAR";
2748 return credit_card_type_to_string;
2749}
2750
[email protected]53329582010-05-14 21:10:582751void AutomationProvider::SendJSONRequest(int handle,
2752 std::string json_request,
2753 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:042754 Browser* browser = NULL;
[email protected]59a611242010-04-02 02:24:042755 scoped_ptr<Value> values;
2756
2757 // Basic error checking.
2758 if (browser_tracker_->ContainsHandle(handle)) {
2759 browser = browser_tracker_->GetResource(handle);
2760 }
2761 if (!browser) {
[email protected]7c983cc2010-07-16 11:33:342762 AutomationJSONReply(this, reply_message).SendError("no browser object");
2763 return;
2764 }
2765 base::JSONReader reader;
2766 std::string error;
2767 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
2768 if (!error.empty()) {
2769 AutomationJSONReply(this, reply_message).SendError(error);
2770 return;
[email protected]59a611242010-04-02 02:24:042771 }
2772
2773 // Make sure input is a dict with a string command.
2774 std::string command;
2775 DictionaryValue* dict_value = NULL;
[email protected]7c983cc2010-07-16 11:33:342776 if (values->GetType() != Value::TYPE_DICTIONARY) {
2777 AutomationJSONReply(this, reply_message).SendError("not a dict");
2778 return;
2779 }
2780 // Ownership remains with "values" variable.
2781 dict_value = static_cast<DictionaryValue*>(values.get());
2782 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
2783 AutomationJSONReply(this, reply_message).SendError(
2784 "no command key in dict or not a string command");
2785 return;
[email protected]59a611242010-04-02 02:24:042786 }
2787
[email protected]24e2b102010-04-29 17:56:472788 // Map json commands to their handlers.
2789 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:052790 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
2791 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
2792 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
2793
[email protected]a9ff2c02010-05-13 17:33:052794 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
[email protected]7c983cc2010-07-16 11:33:342795
[email protected]24e2b102010-04-29 17:56:472796 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:292797 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:052798
[email protected]53329582010-05-14 21:10:582799 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
2800 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
2801 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
2802 handler_map["OmniboxMovePopupSelection"] =
2803 &AutomationProvider::OmniboxMovePopupSelection;
2804
[email protected]24e2b102010-04-29 17:56:472805 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
2806 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:052807
[email protected]ef413ca2010-05-25 21:09:142808 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
2809
[email protected]f7d48012010-05-06 08:17:052810 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:472811 handler_map["WaitForAllDownloadsToComplete"] =
2812 &AutomationProvider::WaitForDownloadsToComplete;
2813
[email protected]a3cd5022010-06-16 18:25:292814 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
2815
[email protected]7060bb292010-06-24 00:52:492816 handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents;
2817
[email protected]c5aa5322010-07-15 19:00:072818 handler_map["ImportSettings"] = &AutomationProvider::ImportSettings;
2819
[email protected]5bcfe1672010-07-16 20:51:572820 handler_map["AddSavedPassword"] = &AutomationProvider::AddSavedPassword;
2821 handler_map["GetSavedPasswords"] = &AutomationProvider::GetSavedPasswords;
2822
[email protected]a0fc50d72010-07-14 21:14:192823 handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData;
2824
[email protected]4d1929f12010-07-10 00:09:032825 // SetTheme() implemented using InstallExtension().
2826 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo;
2827
[email protected]55846ad842010-07-09 18:22:562828 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile;
2829 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile;
2830
[email protected]7c983cc2010-07-16 11:33:342831 if (handler_map.find(std::string(command)) != handler_map.end()) {
2832 (this->*handler_map[command])(browser, dict_value, reply_message);
[email protected]1ac875d22010-07-16 09:57:582833 } else {
[email protected]7c983cc2010-07-16 11:33:342834 std::string error_string = "Unknown command. Options: ";
2835 for (std::map<std::string, JsonHandler>::const_iterator it =
2836 handler_map.begin(); it != handler_map.end(); ++it) {
2837 error_string += it->first + ", ";
2838 }
2839 AutomationJSONReply(this, reply_message).SendError(error_string);
[email protected]1ac875d22010-07-16 09:57:582840 }
[email protected]59a611242010-04-02 02:24:042841}
2842
initial.commit09911bf2008-07-26 23:55:292843void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562844 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342845 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2846 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562847 DCHECK(reply_message_ == NULL);
2848 reply_message_ = reply_message;
2849
[email protected]d9f9b792009-06-24 13:17:122850 DevToolsManager::GetInstance()->InspectElement(
2851 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292852 } else {
[email protected]71f65dd2009-02-11 19:14:562853 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2854 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292855 }
2856}
2857
2858void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092859 if (reply_message_) {
2860 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2861 num_resources);
2862 Send(reply_message_);
2863 reply_message_ = NULL;
2864 }
initial.commit09911bf2008-07-26 23:55:292865}
2866
[email protected]a7eee32f2009-05-22 18:08:172867class SetProxyConfigTask : public Task {
2868 public:
[email protected]be180c802009-10-23 06:33:312869 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
2870 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:252871 : request_context_getter_(request_context_getter),
2872 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:172873 virtual void Run() {
2874 // First, deserialize the JSON string. If this fails, log and bail.
2875 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:392876 std::string error_msg;
2877 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:172878 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2879 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:392880 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:172881 return;
2882 }
2883
2884 scoped_ptr<DictionaryValue> dict(
2885 static_cast<DictionaryValue*>(root.release()));
2886 // Now put together a proxy configuration from the deserialized string.
2887 net::ProxyConfig pc;
2888 PopulateProxyConfig(*dict.get(), &pc);
2889
[email protected]be180c802009-10-23 06:33:312890 net::ProxyService* proxy_service =
2891 request_context_getter_->GetURLRequestContext()->proxy_service();
2892 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:172893 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2894 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:312895 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:172896 }
2897
2898 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2899 DCHECK(pc);
2900 bool no_proxy = false;
2901 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2902 // Make no changes to the ProxyConfig.
2903 return;
2904 }
2905 bool auto_config;
2906 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:482907 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:172908 }
2909 std::string pac_url;
2910 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:482911 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:172912 }
2913 std::string proxy_bypass_list;
2914 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:482915 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:172916 }
2917 std::string proxy_server;
2918 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:482919 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:172920 }
2921 }
2922
2923 private:
[email protected]be180c802009-10-23 06:33:312924 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:172925 std::string proxy_config_;
2926};
2927
2928
2929void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:312930 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
2931 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:172932 FilePath user_data_dir;
2933 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2934 ProfileManager* profile_manager = g_browser_process->profile_manager();
2935 DCHECK(profile_manager);
2936 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2937 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:312938 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:172939 }
[email protected]be180c802009-10-23 06:33:312940 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:172941
[email protected]fae20792009-10-28 20:31:582942 ChromeThread::PostTask(
2943 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:312944 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:172945}
2946
[email protected]4f3dc372009-02-24 00:10:292947void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:332948 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:292949 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292950 if (tab_tracker_->ContainsHandle(handle)) {
2951 NavigationController* tab = tab_tracker_->GetResource(handle);
2952 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2953 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:332954 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:292955 }
initial.commit09911bf2008-07-26 23:55:292956}
2957
[email protected]6a5670d22009-10-27 16:21:342958void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:142959 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:532960 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
2961 reply_message);
2962}
2963
2964void AutomationProvider::OpenNewBrowserWindowOfType(
2965 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:142966 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292967 // We may have no current browser windows open so don't rely on
2968 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:532969 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
2970 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:052971 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152972 if (show)
[email protected]15952e462008-11-14 00:29:052973 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292974}
2975
[email protected]71f65dd2009-02-11 19:14:562976void AutomationProvider::GetWindowForBrowser(int browser_handle,
2977 bool* success,
2978 int* handle) {
2979 *success = false;
2980 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292981
2982 if (browser_tracker_->ContainsHandle(browser_handle)) {
2983 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202984 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292985 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202986 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562987 *success = true;
initial.commit09911bf2008-07-26 23:55:292988 }
initial.commit09911bf2008-07-26 23:55:292989}
2990
2991void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562992 int browser_handle,
2993 bool* success,
2994 int* autocomplete_edit_handle) {
2995 *success = false;
2996 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292997
2998 if (browser_tracker_->ContainsHandle(browser_handle)) {
2999 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:063000 LocationBar* loc_bar = browser->window()->GetLocationBar();
3001 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:293002 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:563003 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
3004 *success = true;
initial.commit09911bf2008-07-26 23:55:293005 }
initial.commit09911bf2008-07-26 23:55:293006}
initial.commit09911bf2008-07-26 23:55:293007
[email protected]71f65dd2009-02-11 19:14:563008void AutomationProvider::ShowInterstitialPage(int tab_handle,
3009 const std::string& html_text,
3010 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293011 if (tab_tracker_->ContainsHandle(tab_handle)) {
3012 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:113013 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:403014
[email protected]7dad3d5f2010-03-04 00:27:013015 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:403016 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:343017 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:403018 GURL("about:interstitial"),
3019 html_text);
3020 interstitial->Show();
3021 return;
initial.commit09911bf2008-07-26 23:55:293022 }
[email protected]71f65dd2009-02-11 19:14:563023
[email protected]457f5cf2009-08-18 16:37:523024 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
3025 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563026 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293027}
3028
[email protected]71f65dd2009-02-11 19:14:563029void AutomationProvider::HideInterstitialPage(int tab_handle,
3030 bool* success) {
3031 *success = false;
[email protected]57c6a652009-05-04 07:58:343032 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
3033 if (tab_contents && tab_contents->interstitial_page()) {
3034 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:563035 *success = true;
initial.commit09911bf2008-07-26 23:55:293036 }
initial.commit09911bf2008-07-26 23:55:293037}
3038
[email protected]71f65dd2009-02-11 19:14:563039void AutomationProvider::CloseTab(int tab_handle,
3040 bool wait_until_closed,
3041 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293042 if (tab_tracker_->ContainsHandle(tab_handle)) {
3043 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
3044 int index;
3045 Browser* browser = Browser::GetBrowserForController(controller, &index);
3046 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:143047 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:113048 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:453049 return;
initial.commit09911bf2008-07-26 23:55:293050 }
[email protected]de246f52009-02-25 18:25:453051
3052 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:553053 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293054}
3055
[email protected]71f65dd2009-02-11 19:14:563056void AutomationProvider::CloseBrowser(int browser_handle,
3057 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293058 if (browser_tracker_->ContainsHandle(browser_handle)) {
3059 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563060 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:563061 reply_message);
[email protected]f3e99e32008-07-30 04:48:393062 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:293063 } else {
3064 NOTREACHED();
3065 }
3066}
3067
[email protected]71f65dd2009-02-11 19:14:563068void AutomationProvider::CloseBrowserAsync(int browser_handle) {
3069 if (browser_tracker_->ContainsHandle(browser_handle)) {
3070 Browser* browser = browser_tracker_->GetResource(browser_handle);
3071 browser->window()->Close();
3072 } else {
3073 NOTREACHED();
3074 }
3075}
3076
[email protected]71f65dd2009-02-11 19:14:563077void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
3078 IPC::Message* reply_message) {
3079 if (tab_tracker_->ContainsHandle(tab_handle)) {
3080 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3081 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:143082 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:563083 }
3084}
3085
[email protected]71f65dd2009-02-11 19:14:563086void AutomationProvider::GetSecurityState(int handle, bool* success,
3087 SecurityStyle* security_style,
3088 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:483089 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:293090 if (tab_tracker_->ContainsHandle(handle)) {
3091 NavigationController* tab = tab_tracker_->GetResource(handle);
3092 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563093 *success = true;
3094 *security_style = entry->ssl().security_style();
3095 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:483096 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:293097 } else {
[email protected]71f65dd2009-02-11 19:14:563098 *success = false;
3099 *security_style = SECURITY_STYLE_UNKNOWN;
3100 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:483101 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:293102 }
3103}
3104
[email protected]71f65dd2009-02-11 19:14:563105void AutomationProvider::GetPageType(int handle, bool* success,
3106 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:293107 if (tab_tracker_->ContainsHandle(handle)) {
3108 NavigationController* tab = tab_tracker_->GetResource(handle);
3109 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563110 *page_type = entry->page_type();
3111 *success = true;
initial.commit09911bf2008-07-26 23:55:293112 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:343113 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:563114 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:343115 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:563116 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293117 } else {
[email protected]71f65dd2009-02-11 19:14:563118 *success = false;
3119 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293120 }
3121}
3122
[email protected]84abba62009-10-07 17:01:443123void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
3124 int* duration_ms) {
3125 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
3126 event_name);
3127}
3128
[email protected]71f65dd2009-02-11 19:14:563129void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
3130 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293131 if (tab_tracker_->ContainsHandle(handle)) {
3132 NavigationController* tab = tab_tracker_->GetResource(handle);
3133 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:073134 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:403135 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:473136 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:343137 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:293138 if (ssl_blocking_page) {
3139 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:013140 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:563141 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:293142 return;
3143 }
3144 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:523145 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3146 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:563147 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293148 return;
3149 }
3150 }
3151 }
3152 // We failed.
[email protected]457f5cf2009-08-18 16:37:523153 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3154 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563155 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293156}
3157
[email protected]71f65dd2009-02-11 19:14:563158void AutomationProvider::BringBrowserToFront(int browser_handle,
3159 bool* success) {
initial.commit09911bf2008-07-26 23:55:293160 if (browser_tracker_->ContainsHandle(browser_handle)) {
3161 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:063162 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:563163 *success = true;
initial.commit09911bf2008-07-26 23:55:293164 } else {
[email protected]71f65dd2009-02-11 19:14:563165 *success = false;
initial.commit09911bf2008-07-26 23:55:293166 }
3167}
3168
[email protected]71f65dd2009-02-11 19:14:563169void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
3170 int message_num,
3171 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:293172 if (browser_tracker_->ContainsHandle(browser_handle)) {
3173 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563174 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:143175 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:293176 } else {
[email protected]71f65dd2009-02-11 19:14:563177 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:293178 }
3179}
3180
[email protected]71f65dd2009-02-11 19:14:563181void AutomationProvider::PrintNow(int tab_handle,
3182 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:573183 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:343184 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3185 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:293186 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:573187 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:143188 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:343189 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:573190 return;
initial.commit09911bf2008-07-26 23:55:293191 }
[email protected]71f65dd2009-02-11 19:14:563192 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
3193 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293194}
[email protected]d301c952009-07-13 15:02:413195
[email protected]71f65dd2009-02-11 19:14:563196void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:113197 const FilePath& file_name,
3198 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:563199 int type,
3200 bool* success) {
initial.commit09911bf2008-07-26 23:55:293201 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:563202 *success = false;
initial.commit09911bf2008-07-26 23:55:293203 return;
3204 }
3205
3206 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3207 Browser* browser = FindAndActivateTab(nav);
3208 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:143209 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:563210 *success = false;
initial.commit09911bf2008-07-26 23:55:293211 return;
3212 }
3213
initial.commit09911bf2008-07-26 23:55:293214 SavePackage::SavePackageType save_type =
3215 static_cast<SavePackage::SavePackageType>(type);
3216 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
3217 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:343218 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:293219
[email protected]71f65dd2009-02-11 19:14:563220 *success = true;
initial.commit09911bf2008-07-26 23:55:293221}
3222
[email protected]71f65dd2009-02-11 19:14:563223void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
3224 bool* success,
3225 std::wstring* text) {
3226 *success = false;
initial.commit09911bf2008-07-26 23:55:293227 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563228 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:523229 GetText();
[email protected]71f65dd2009-02-11 19:14:563230 *success = true;
initial.commit09911bf2008-07-26 23:55:293231 }
initial.commit09911bf2008-07-26 23:55:293232}
3233
[email protected]71f65dd2009-02-11 19:14:563234void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
3235 const std::wstring& text,
3236 bool* success) {
3237 *success = false;
initial.commit09911bf2008-07-26 23:55:293238 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:523239 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
3240 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:563241 *success = true;
initial.commit09911bf2008-07-26 23:55:293242 }
initial.commit09911bf2008-07-26 23:55:293243}
3244
3245void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:563246 int autocomplete_edit_handle,
3247 bool* success,
3248 std::vector<AutocompleteMatchData>* matches) {
3249 *success = false;
initial.commit09911bf2008-07-26 23:55:293250 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:273251 const AutocompleteResult& result = autocomplete_edit_tracker_->
3252 GetResource(autocomplete_edit_handle)->model()->result();
3253 for (AutocompleteResult::const_iterator i = result.begin();
3254 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:563255 matches->push_back(AutocompleteMatchData(*i));
3256 *success = true;
initial.commit09911bf2008-07-26 23:55:293257 }
initial.commit09911bf2008-07-26 23:55:293258}
3259
3260void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:563261 int autocomplete_edit_handle,
3262 bool* success,
3263 bool* query_in_progress) {
3264 *success = false;
3265 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:293266 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563267 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:523268 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:563269 *success = true;
initial.commit09911bf2008-07-26 23:55:293270 }
initial.commit09911bf2008-07-26 23:55:293271}
3272
[email protected]63514af2010-03-30 17:17:233273#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:193274
[email protected]5ae5bed2009-08-21 18:52:443275#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:393276
[email protected]57c6a652009-05-04 07:58:343277TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:573278 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:573279 if (tab_tracker_->ContainsHandle(handle)) {
3280 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:343281 if (tab)
3282 *tab = nav_controller;
3283 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:573284 }
[email protected]57c6a652009-05-04 07:58:343285 return NULL;
[email protected]20e93d12008-08-28 16:31:573286}
3287
initial.commit09911bf2008-07-26 23:55:293288TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
3289 : AutomationProvider(profile) {
3290 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:143291 registrar_.Add(this, NotificationType::SESSION_END,
3292 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:293293}
3294
3295TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:293296 BrowserList::RemoveObserver(this);
3297}
3298
3299void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:143300 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:293301 AutomationProvider::OnChannelError();
3302}
3303
3304void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
3305 // For backwards compatibility with the testing automation interface, we
3306 // want the automation provider (and hence the process) to go away when the
3307 // last browser goes away.
3308 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:293309 // If you change this, update Observer for NotificationType::SESSION_END
3310 // below.
[email protected]295039bd2008-08-15 04:32:573311 MessageLoop::current()->PostTask(FROM_HERE,
3312 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:293313 }
3314}
3315
3316void TestingAutomationProvider::Observe(NotificationType type,
3317 const NotificationSource& source,
3318 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:563319 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:293320 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
3321 // before the task runs resulting in this object not being deleted. This
3322 // Release balance out the Release scheduled by OnBrowserRemoving.
3323 Release();
3324}
[email protected]295039bd2008-08-15 04:32:573325
3326void TestingAutomationProvider::OnRemoveProvider() {
3327 AutomationProviderList::GetInstance()->RemoveProvider(this);
3328}
[email protected]8a3422c92008-09-24 17:42:423329
[email protected]816633a2009-11-11 21:48:183330void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:563331 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:423332 if (tab_tracker_->ContainsHandle(handle)) {
3333 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:343334 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:113335 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423336 }
[email protected]8a3422c92008-09-24 17:42:423337}
3338
[email protected]816633a2009-11-11 21:48:183339void AutomationProvider::ClickInfoBarAccept(int handle,
3340 int info_bar_index,
3341 bool wait_for_navigation,
3342 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:423343 bool success = false;
3344 if (tab_tracker_->ContainsHandle(handle)) {
3345 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3346 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:113347 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423348 if (info_bar_index >= 0 && info_bar_index < count) {
3349 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:013350 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:423351 }
[email protected]eb9ba192008-12-02 02:41:343352 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:113353 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:343354 info_bar_index);
3355 if (delegate->AsConfirmInfoBarDelegate())
3356 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:423357 success = true;
3358 }
3359 }
[email protected]4f3dc372009-02-24 00:10:293360 }
[email protected]58f622a62009-10-04 01:17:553361
3362 // This "!wait_for_navigation || !success condition" logic looks suspicious.
3363 // It will send a failure message when success is true but
3364 // |wait_for_navigation| is false.
3365 // TODO(phajdan.jr): investgate whether the reply param (currently
3366 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:423367 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:183368 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:523369 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:423370}
3371
[email protected]71f65dd2009-02-11 19:14:563372void AutomationProvider::GetLastNavigationTime(int handle,
3373 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:423374 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:563375 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:423376}
3377
[email protected]71f65dd2009-02-11 19:14:563378void AutomationProvider::WaitForNavigation(int handle,
3379 int64 last_navigation_time,
3380 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:253381 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:423382 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:253383
[email protected]8a3422c92008-09-24 17:42:423384 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:563385 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:523386 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
3387 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:553388 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:293389 return;
[email protected]8a3422c92008-09-24 17:42:423390 }
3391
[email protected]7dad3d5f2010-03-04 00:27:013392 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:423393}
3394
[email protected]71f65dd2009-02-11 19:14:563395void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163396 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563397 int value,
3398 bool* success) {
3399 *success = false;
[email protected]8a3422c92008-09-24 17:42:423400 if (browser_tracker_->ContainsHandle(handle)) {
3401 Browser* browser = browser_tracker_->GetResource(handle);
3402 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563403 *success = true;
[email protected]8a3422c92008-09-24 17:42:423404 }
[email protected]8a3422c92008-09-24 17:42:423405}
[email protected]97fa6ce32008-12-19 01:48:163406
[email protected]71f65dd2009-02-11 19:14:563407void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163408 const std::wstring& name,
[email protected]ddd231e2010-06-29 20:35:193409 const std::string& value,
[email protected]71f65dd2009-02-11 19:14:563410 bool* success) {
3411 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163412 if (browser_tracker_->ContainsHandle(handle)) {
3413 Browser* browser = browser_tracker_->GetResource(handle);
3414 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563415 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163416 }
[email protected]97fa6ce32008-12-19 01:48:163417}
3418
[email protected]71f65dd2009-02-11 19:14:563419void AutomationProvider::GetBooleanPreference(int handle,
3420 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:363421 bool* success,
3422 bool* value) {
[email protected]71f65dd2009-02-11 19:14:563423 *success = false;
3424 *value = false;
[email protected]97fa6ce32008-12-19 01:48:163425 if (browser_tracker_->ContainsHandle(handle)) {
3426 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:563427 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
3428 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163429 }
[email protected]97fa6ce32008-12-19 01:48:163430}
3431
[email protected]71f65dd2009-02-11 19:14:563432void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163433 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563434 bool value,
3435 bool* success) {
3436 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163437 if (browser_tracker_->ContainsHandle(handle)) {
3438 Browser* browser = browser_tracker_->GetResource(handle);
3439 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563440 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163441 }
[email protected]97fa6ce32008-12-19 01:48:163442}
3443
3444// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563445void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:403446 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:163447 if (tab_tracker_->ContainsHandle(tab_handle)) {
3448 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3449 Browser* browser = FindAndActivateTab(nav);
3450 DCHECK(browser);
3451
[email protected]57c6a652009-05-04 07:58:343452 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
3453 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:163454 }
[email protected]97fa6ce32008-12-19 01:48:163455}
3456
[email protected]b8f48d12009-11-09 20:14:363457// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563458void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:403459 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:563460 bool* success) {
3461 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163462 if (tab_tracker_->ContainsHandle(tab_handle)) {
3463 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:193464 if (!nav)
3465 return;
[email protected]97fa6ce32008-12-19 01:48:163466 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:163467
[email protected]2f2afba2010-04-01 01:53:193468 // If the browser has UI, simulate what a user would do.
3469 // Activate the tab and then click the encoding menu.
3470 if (browser &&
3471 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:163472 int selected_encoding_id =
3473 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
3474 if (selected_encoding_id) {
3475 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:563476 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163477 }
[email protected]2f2afba2010-04-01 01:53:193478 } else {
3479 // There is no UI, Chrome probably runs as Chrome-Frame mode.
3480 // Try to get TabContents and call its override_encoding method.
3481 TabContents* contents = nav->tab_contents();
3482 if (!contents)
3483 return;
3484 const std::string selected_encoding =
3485 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
3486 if (selected_encoding.empty())
3487 return;
3488 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:163489 }
3490 }
[email protected]97fa6ce32008-12-19 01:48:163491}
[email protected]5bcdb312009-01-07 21:43:203492
[email protected]4d434a1a2009-02-11 21:06:573493void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:203494 SavePackage::SetShouldPromptUser(should_prompt);
3495}
[email protected]87eab222009-03-13 00:47:453496
[email protected]66ba4932009-06-04 19:22:133497void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
3498 *count = -1; // -1 is the error code
3499 if (tab_tracker_->ContainsHandle(handle)) {
3500 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3501 TabContents* tab_contents = nav_controller->tab_contents();
3502 if (tab_contents) {
3503 BlockedPopupContainer* container =
3504 tab_contents->blocked_popup_container();
3505 if (container) {
3506 *count = static_cast<int>(container->GetBlockedPopupCount());
3507 } else {
3508 // If we don't have a container, we don't have any blocked popups to
3509 // contain!
3510 *count = 0;
3511 }
3512 }
3513 }
3514}
[email protected]f7a68432009-07-29 23:18:193515
3516void AutomationProvider::SelectAll(int tab_handle) {
3517 RenderViewHost* view = GetViewForTab(tab_handle);
3518 if (!view) {
3519 NOTREACHED();
3520 return;
3521 }
3522
3523 view->SelectAll();
3524}
3525
3526void AutomationProvider::Cut(int tab_handle) {
3527 RenderViewHost* view = GetViewForTab(tab_handle);
3528 if (!view) {
3529 NOTREACHED();
3530 return;
3531 }
3532
3533 view->Cut();
3534}
3535
3536void AutomationProvider::Copy(int tab_handle) {
3537 RenderViewHost* view = GetViewForTab(tab_handle);
3538 if (!view) {
3539 NOTREACHED();
3540 return;
3541 }
3542
3543 view->Copy();
3544}
3545
3546void AutomationProvider::Paste(int tab_handle) {
3547 RenderViewHost* view = GetViewForTab(tab_handle);
3548 if (!view) {
3549 NOTREACHED();
3550 return;
3551 }
3552
3553 view->Paste();
3554}
3555
3556void AutomationProvider::ReloadAsync(int tab_handle) {
3557 if (tab_tracker_->ContainsHandle(tab_handle)) {
3558 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3559 if (!tab) {
3560 NOTREACHED();
3561 return;
3562 }
3563
[email protected]106a0812010-03-18 00:15:123564 const bool check_for_repost = true;
3565 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193566 }
3567}
3568
3569void AutomationProvider::StopAsync(int tab_handle) {
3570 RenderViewHost* view = GetViewForTab(tab_handle);
3571 if (!view) {
[email protected]8b2b3312009-09-14 18:38:363572 // We tolerate StopAsync being called even before a view has been created.
3573 // So just log a warning instead of a NOTREACHED().
3574 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:193575 return;
3576 }
3577
3578 view->Stop();
3579}
3580
[email protected]1bb5f892009-10-06 01:44:573581void AutomationProvider::OnSetPageFontSize(int tab_handle,
3582 int font_size) {
3583 AutomationPageFontSize automation_font_size =
3584 static_cast<AutomationPageFontSize>(font_size);
3585
3586 if (automation_font_size < SMALLEST_FONT ||
3587 automation_font_size > LARGEST_FONT) {
3588 DLOG(ERROR) << "Invalid font size specified : "
3589 << font_size;
3590 return;
3591 }
3592
3593 if (tab_tracker_->ContainsHandle(tab_handle)) {
3594 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3595 DCHECK(tab != NULL);
3596 if (tab && tab->tab_contents()) {
3597 DCHECK(tab->tab_contents()->profile() != NULL);
3598 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
3599 prefs::kWebKitDefaultFontSize, font_size);
3600 }
3601 }
3602}
3603
[email protected]bc73b4e52010-03-26 04:16:203604void AutomationProvider::RemoveBrowsingData(int remove_mask) {
3605 BrowsingDataRemover* remover;
3606 remover = new BrowsingDataRemover(profile(),
3607 BrowsingDataRemover::EVERYTHING, // All time periods.
3608 base::Time());
3609 remover->Remove(remove_mask);
3610 // BrowsingDataRemover deletes itself.
3611}
[email protected]1bb5f892009-10-06 01:44:573612
[email protected]2949e90d2009-08-21 15:32:523613void AutomationProvider::WaitForBrowserWindowCountToBecome(
3614 int target_count, IPC::Message* reply_message) {
3615 if (static_cast<int>(BrowserList::size()) == target_count) {
3616 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
3617 reply_message, true);
3618 Send(reply_message);
3619 return;
3620 }
3621
3622 // Set up an observer (it will delete itself).
3623 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
3624}
3625
3626void AutomationProvider::WaitForAppModalDialogToBeShown(
3627 IPC::Message* reply_message) {
3628 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
3629 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
3630 reply_message, true);
3631 Send(reply_message);
3632 return;
3633 }
3634
3635 // Set up an observer (it will delete itself).
3636 new AppModalDialogShownObserver(this, reply_message);
3637}
3638
[email protected]1126a1d32009-08-26 15:39:263639void AutomationProvider::GoBackBlockUntilNavigationsComplete(
3640 int handle, int number_of_navigations, IPC::Message* reply_message) {
3641 if (tab_tracker_->ContainsHandle(handle)) {
3642 NavigationController* tab = tab_tracker_->GetResource(handle);
3643 Browser* browser = FindAndActivateTab(tab);
3644 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:013645 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3646 false);
[email protected]1126a1d32009-08-26 15:39:263647 browser->GoBack(CURRENT_TAB);
3648 return;
3649 }
3650 }
3651
3652 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
3653 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3654 Send(reply_message);
3655}
3656
3657void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
3658 int handle, int number_of_navigations, IPC::Message* reply_message) {
3659 if (tab_tracker_->ContainsHandle(handle)) {
3660 NavigationController* tab = tab_tracker_->GetResource(handle);
3661 Browser* browser = FindAndActivateTab(tab);
3662 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:013663 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3664 false);
[email protected]1126a1d32009-08-26 15:39:263665 browser->GoForward(CURRENT_TAB);
3666 return;
3667 }
3668 }
3669
3670 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
3671 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3672 Send(reply_message);
3673}
3674
[email protected]f7a68432009-07-29 23:18:193675RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
3676 if (tab_tracker_->ContainsHandle(tab_handle)) {
3677 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3678 if (!tab) {
3679 NOTREACHED();
3680 return NULL;
3681 }
3682
3683 TabContents* tab_contents = tab->tab_contents();
3684 if (!tab_contents) {
3685 NOTREACHED();
3686 return NULL;
3687 }
3688
3689 RenderViewHost* view_host = tab_contents->render_view_host();
3690 return view_host;
3691 }
3692
3693 return NULL;
3694}
[email protected]675595f2009-08-26 22:32:043695
3696void AutomationProvider::GetBrowserForWindow(int window_handle,
3697 bool* success,
3698 int* browser_handle) {
3699 *success = false;
3700 *browser_handle = 0;
3701
3702 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
3703 if (!window)
3704 return;
3705
3706 BrowserList::const_iterator iter = BrowserList::begin();
3707 for (;iter != BrowserList::end(); ++iter) {
3708 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
3709 if (window == this_window) {
3710 // Add() returns the existing handle for the resource if any.
3711 *browser_handle = browser_tracker_->Add(*iter);
3712 *success = true;
3713 return;
3714 }
3715 }
3716}
[email protected]d11c8e92009-10-20 23:26:403717
3718void AutomationProvider::InstallExtension(const FilePath& crx_path,
3719 IPC::Message* reply_message) {
3720 ExtensionsService* service = profile_->GetExtensionsService();
3721 if (service) {
3722 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193723 new ExtensionInstallNotificationObserver(this,
3724 AutomationMsg_InstallExtension::ID,
3725 reply_message);
[email protected]d11c8e92009-10-20 23:26:403726
3727 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:163728 scoped_refptr<CrxInstaller> installer(
3729 new CrxInstaller(install_dir,
3730 service,
3731 NULL)); // silent install, no UI
3732 installer->set_allow_privilege_increase(true);
3733 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:403734 } else {
3735 AutomationMsg_InstallExtension::WriteReplyParams(
3736 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3737 Send(reply_message);
3738 }
3739}
3740
3741void AutomationProvider::LoadExpandedExtension(
3742 const FilePath& extension_dir,
3743 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:383744 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:403745 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193746 new ExtensionInstallNotificationObserver(
3747 this,
3748 AutomationMsg_LoadExpandedExtension::ID,
3749 reply_message);
[email protected]d11c8e92009-10-20 23:26:403750
3751 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:403752 } else {
3753 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
3754 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3755 Send(reply_message);
3756 }
3757}
[email protected]673fd2c02010-02-04 23:10:003758
[email protected]a1e62d12010-03-16 02:18:433759void AutomationProvider::GetEnabledExtensions(
3760 std::vector<FilePath>* result) {
3761 ExtensionsService* service = profile_->GetExtensionsService();
3762 DCHECK(service);
3763 if (service->extensions_enabled()) {
3764 const ExtensionList* extensions = service->extensions();
3765 DCHECK(extensions);
3766 for (size_t i = 0; i < extensions->size(); ++i) {
3767 Extension* extension = (*extensions)[i];
3768 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:123769 if (extension->location() == Extension::INTERNAL ||
3770 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:073771 result->push_back(extension->path());
3772 }
[email protected]a1e62d12010-03-16 02:18:433773 }
3774 }
3775}
3776
[email protected]790788ac2010-04-06 17:52:193777void AutomationProvider::WaitForExtensionTestResult(
3778 IPC::Message* reply_message) {
3779 DCHECK(reply_message_ == NULL);
3780 reply_message_ = reply_message;
3781 // Call MaybeSendResult, because the result might have come in before
3782 // we were waiting on it.
3783 extension_test_result_observer_->MaybeSendResult();
3784}
3785
3786void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:093787 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:193788 ExtensionsService* service = profile_->GetExtensionsService();
3789 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3790 if (service && manager) {
3791 // The observer will delete itself when done.
3792 new ExtensionReadyNotificationObserver(
3793 manager,
3794 this,
3795 AutomationMsg_InstallExtensionAndGetHandle::ID,
3796 reply_message);
3797
[email protected]d7e5525d2010-04-20 14:37:093798 ExtensionInstallUI* client =
3799 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:193800 scoped_refptr<CrxInstaller> installer(
3801 new CrxInstaller(service->install_directory(),
3802 service,
[email protected]d7e5525d2010-04-20 14:37:093803 client));
[email protected]790788ac2010-04-06 17:52:193804 installer->set_allow_privilege_increase(true);
3805 installer->InstallCrx(crx_path);
3806 } else {
3807 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
3808 reply_message, 0);
3809 Send(reply_message);
3810 }
3811}
3812
3813void AutomationProvider::UninstallExtension(int extension_handle,
3814 bool* success) {
3815 *success = false;
3816 Extension* extension = GetExtension(extension_handle);
3817 ExtensionsService* service = profile_->GetExtensionsService();
3818 if (extension && service) {
3819 ExtensionUnloadNotificationObserver observer;
3820 service->UninstallExtension(extension->id(), false);
3821 // The extension unload notification should have been sent synchronously
3822 // with the uninstall. Just to be safe, check that it was received.
3823 *success = observer.did_receive_unload_notification();
3824 }
3825}
3826
3827void AutomationProvider::EnableExtension(int extension_handle,
3828 IPC::Message* reply_message) {
3829 Extension* extension = GetDisabledExtension(extension_handle);
3830 ExtensionsService* service = profile_->GetExtensionsService();
3831 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3832 // Only enable if this extension is disabled.
3833 if (extension && service && manager) {
3834 // The observer will delete itself when done.
3835 new ExtensionReadyNotificationObserver(
3836 manager,
3837 this,
3838 AutomationMsg_EnableExtension::ID,
3839 reply_message);
3840 service->EnableExtension(extension->id());
3841 } else {
3842 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
3843 Send(reply_message);
3844 }
3845}
3846
3847void AutomationProvider::DisableExtension(int extension_handle,
3848 bool* success) {
3849 *success = false;
3850 Extension* extension = GetEnabledExtension(extension_handle);
3851 ExtensionsService* service = profile_->GetExtensionsService();
3852 if (extension && service) {
3853 ExtensionUnloadNotificationObserver observer;
3854 service->DisableExtension(extension->id());
3855 // The extension unload notification should have been sent synchronously
3856 // with the disable. Just to be safe, check that it was received.
3857 *success = observer.did_receive_unload_notification();
3858 }
3859}
3860
3861void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
3862 int extension_handle, int browser_handle,
3863 IPC::Message* reply_message) {
3864 bool success = false;
3865 Extension* extension = GetEnabledExtension(extension_handle);
3866 ExtensionsService* service = profile_->GetExtensionsService();
3867 ExtensionMessageService* message_service =
3868 profile_->GetExtensionMessageService();
3869 Browser* browser = browser_tracker_->GetResource(browser_handle);
3870 if (extension && service && message_service && browser) {
3871 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
3872 if (extension->page_action()) {
3873 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
3874 browser->profile(), extension->id(), "action", tab_id, "", 1);
3875 success = true;
3876 } else if (extension->browser_action()) {
3877 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
3878 browser->profile(), extension->id(), browser);
3879 success = true;
3880 }
3881 }
3882 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3883 reply_message, success);
3884 Send(reply_message);
3885}
3886
3887void AutomationProvider::MoveExtensionBrowserAction(
3888 int extension_handle, int index, bool* success) {
3889 *success = false;
3890 Extension* extension = GetEnabledExtension(extension_handle);
3891 ExtensionsService* service = profile_->GetExtensionsService();
3892 if (extension && service) {
3893 ExtensionToolbarModel* toolbar = service->toolbar_model();
3894 if (toolbar) {
3895 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3896 toolbar->MoveBrowserAction(extension, index);
3897 *success = true;
3898 } else {
3899 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3900 }
3901 }
3902 }
3903}
3904
3905void AutomationProvider::GetExtensionProperty(
3906 int extension_handle,
3907 AutomationMsg_ExtensionProperty type,
3908 bool* success,
3909 std::string* value) {
3910 *success = false;
3911 Extension* extension = GetExtension(extension_handle);
3912 ExtensionsService* service = profile_->GetExtensionsService();
3913 if (extension && service) {
3914 ExtensionToolbarModel* toolbar = service->toolbar_model();
3915 int found_index = -1;
3916 int index = 0;
3917 switch (type) {
3918 case AUTOMATION_MSG_EXTENSION_ID:
3919 *value = extension->id();
3920 *success = true;
3921 break;
3922 case AUTOMATION_MSG_EXTENSION_NAME:
3923 *value = extension->name();
3924 *success = true;
3925 break;
3926 case AUTOMATION_MSG_EXTENSION_VERSION:
3927 *value = extension->VersionString();
3928 *success = true;
3929 break;
3930 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3931 if (toolbar) {
3932 for (ExtensionList::const_iterator iter = toolbar->begin();
3933 iter != toolbar->end(); iter++) {
3934 // Skip this extension if we are in incognito mode
3935 // and it is not incognito-enabled.
3936 if (profile_->IsOffTheRecord() &&
3937 !service->IsIncognitoEnabled(*iter))
3938 continue;
3939 if (*iter == extension) {
3940 found_index = index;
3941 break;
3942 }
3943 index++;
3944 }
3945 *value = IntToString(found_index);
3946 *success = true;
3947 }
3948 break;
3949 default:
3950 LOG(WARNING) << "Trying to get undefined extension property";
3951 break;
3952 }
3953 }
3954}
3955
[email protected]673fd2c02010-02-04 23:10:003956void AutomationProvider::SaveAsAsync(int tab_handle) {
3957 NavigationController* tab = NULL;
3958 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3959 if (tab_contents)
3960 tab_contents->OnSavePage();
3961}
[email protected]7dad3d5f2010-03-04 00:27:013962
3963void AutomationProvider::SetContentSetting(
3964 int handle,
3965 const std::string& host,
3966 ContentSettingsType content_type,
3967 ContentSetting setting,
3968 bool* success) {
3969 *success = false;
3970 if (browser_tracker_->ContainsHandle(handle)) {
3971 Browser* browser = browser_tracker_->GetResource(handle);
3972 HostContentSettingsMap* map =
3973 browser->profile()->GetHostContentSettingsMap();
3974 if (host.empty()) {
3975 map->SetDefaultContentSetting(content_type, setting);
3976 } else {
[email protected]0314ae02010-04-08 09:18:293977 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3978 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013979 }
3980 *success = true;
3981 }
3982}
[email protected]cc824372010-03-31 15:33:013983
3984#if !defined(TOOLKIT_VIEWS)
3985void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3986 NOTIMPLEMENTED();
3987};
3988
3989void AutomationProvider::WaitForFocusedViewIDToChange(
3990 int handle, int previous_view_id, IPC::Message* reply_message) {
3991 NOTIMPLEMENTED();
3992}
3993
3994void AutomationProvider::StartTrackingPopupMenus(
3995 int browser_handle, bool* success) {
3996 NOTIMPLEMENTED();
3997}
3998
3999void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
4000 NOTIMPLEMENTED();
4001}
4002#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:094003
4004void AutomationProvider::ResetToDefaultTheme() {
4005 profile_->ClearTheme();
4006}