blob: 07c37d390497d4a7bf1421576acc1d46741b20ab [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]5fac9622009-02-04 21:49:3823#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5324#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1725#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2926#include "chrome/app/chrome_dll_resource.h"
[email protected]bcff05a2010-04-14 01:46:4327#include "chrome/app/chrome_version_info.h"
[email protected]0bfa713f2009-04-07 20:18:2828#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0929#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]790788ac2010-04-06 17:52:1930#include "chrome/browser/automation/automation_extension_tracker.h"
initial.commit09911bf2008-07-26 23:55:2931#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0832#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]f44265b2009-05-19 18:52:5033#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1334#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5335#include "chrome/browser/bookmarks/bookmark_model.h"
36#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]ef413ca2010-05-25 21:09:1437#include "chrome/browser/browser_list.h"
[email protected]5c238752009-06-13 10:29:0738#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3939#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2040#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0541#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5842#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2943#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1244#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5945#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2246#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0547#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4048#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1949#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]ef413ca2010-05-25 21:09:1450#include "chrome/browser/extensions/extension_host.h"
[email protected]d11c8e92009-10-20 23:26:4051#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5552#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1953#include "chrome/browser/extensions/extension_tabs_module.h"
54#include "chrome/browser/extensions/extension_toolbar_model.h"
55#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1656#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5857#include "chrome/browser/find_bar.h"
58#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2959#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0160#include "chrome/browser/host_content_settings_map.h"
[email protected]0ac83682010-01-22 17:46:2761#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0662#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0163#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5364#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2465#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0866#include "chrome/browser/pref_service.h"
[email protected]f83f9102010-05-04 17:01:0567#include "chrome/browser/printing/print_job.h"
[email protected]a7eee32f2009-05-22 18:08:1768#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0769#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2570#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0371#include "chrome/browser/ssl/ssl_manager.h"
72#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3473#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4574#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1775#include "chrome/common/automation_constants.h"
[email protected]a9ff2c02010-05-13 17:33:0576#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2977#include "chrome/common/chrome_paths.h"
[email protected]790788ac2010-04-06 17:52:1978#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1779#include "chrome/common/json_value_serializer.h"
[email protected]68d2a05f2010-05-07 21:39:5580#include "chrome/common/net/url_request_context_getter.h"
[email protected]1c58a5c2009-05-21 18:47:1481#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5782#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2583#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5684#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5785#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1786#include "net/proxy/proxy_service.h"
87#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2188#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:3089#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:2890#include "views/event.h"
[email protected]f7d48012010-05-06 08:17:0591#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:2992
[email protected]de246f52009-02-25 18:25:4593#if defined(OS_WIN)
[email protected]4bdde602010-06-16 03:17:3594#include "chrome/browser/external_tab_container_win.h"
[email protected]de246f52009-02-25 18:25:4595#endif // defined(OS_WIN)
96
[email protected]e1acf6f2008-10-27 20:43:3397using base::Time;
98
[email protected]cbab76d2008-10-13 22:42:4799class AutomationInterstitialPage : public InterstitialPage {
100 public:
[email protected]57c6a652009-05-04 07:58:34101 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47102 const GURL& url,
103 const std::string& contents)
104 : InterstitialPage(tab, true, url),
105 contents_(contents) {
106 }
107
108 virtual std::string GetHTMLContents() { return contents_; }
109
110 private:
111 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29112
[email protected]cbab76d2008-10-13 22:42:47113 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
114};
115
[email protected]c2cb8542009-08-20 21:16:51116class ClickTask : public Task {
117 public:
[email protected]fc2e0872009-08-21 22:14:41118 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51119 virtual ~ClickTask() {}
120
121 virtual void Run() {
122 ui_controls::MouseButton button = ui_controls::LEFT;
123 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
124 views::Event::EF_LEFT_BUTTON_DOWN) {
125 button = ui_controls::LEFT;
126 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
127 views::Event::EF_RIGHT_BUTTON_DOWN) {
128 button = ui_controls::RIGHT;
129 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
130 views::Event::EF_MIDDLE_BUTTON_DOWN) {
131 button = ui_controls::MIDDLE;
132 } else {
133 NOTREACHED();
134 }
135
[email protected]fc2e0872009-08-21 22:14:41136 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51137 }
138
139 private:
[email protected]c2cb8542009-08-20 21:16:51140 int flags_;
141
142 DISALLOW_COPY_AND_ASSIGN(ClickTask);
143};
[email protected]c2cb8542009-08-20 21:16:51144
initial.commit09911bf2008-07-26 23:55:29145AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57146 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56147 profile_(profile),
[email protected]cc824372010-03-31 15:33:01148 reply_message_(NULL),
149 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29150 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19151 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29152 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20153 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29154 autocomplete_edit_tracker_.reset(
155 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29156 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
157 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44158 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19159 extension_test_result_observer_.reset(
160 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13161 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29162}
163
164AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50165 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
166 port_containers_.end());
167 port_containers_.clear();
168
[email protected]0da050b92008-08-19 19:29:47169 // Make sure that any outstanding NotificationObservers also get destroyed.
170 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31171 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47172 while ((observer = it.GetNext()) != NULL)
173 delete observer;
[email protected]528211a2010-01-14 15:25:13174
175 if (channel_.get()) {
176 channel_->Close();
177 }
178 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29179}
180
[email protected]9a3a293b2009-06-04 22:28:16181void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06182 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57183 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06184 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
185 automation_resource_message_filter_,
186 g_browser_process->io_thread()->message_loop(),
187 true, g_browser_process->shutdown_event()));
[email protected]bcff05a2010-04-14 01:46:43188 scoped_ptr<FileVersionInfo> version_info(
189 chrome_app::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40190 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43191 if (version_info != NULL) {
192 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40193 }
[email protected]c6cb1992009-04-13 16:45:29194
195 // Send a hello message with our current automation protocol version.
196 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29197}
198
199void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
200 if (expected_tabs == 0) {
201 Send(new AutomationMsg_InitialLoadsComplete(0));
202 } else {
203 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
204 }
205}
206
207NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58208 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01209 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29210 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58211 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01212 number_of_navigations,
213 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29214
[email protected]71f65dd2009-02-11 19:14:56215 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29216 return observer;
217}
218
[email protected]faf2ee42010-05-11 14:26:17219void AutomationProvider::RemoveNavigationStatusListener(
220 NotificationObserver* obs) {
221 notification_observer_list_.RemoveObserver(obs);
222}
223
initial.commit09911bf2008-07-26 23:55:29224NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14225 Browser* parent,
226 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56227 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14228 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29229 notification_observer_list_.AddObserver(observer);
230
231 return observer;
232}
233
[email protected]faf2ee42010-05-11 14:26:17234void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
initial.commit09911bf2008-07-26 23:55:29235 notification_observer_list_.RemoveObserver(obs);
236}
237
238void AutomationProvider::AddLoginHandler(NavigationController* tab,
239 LoginHandler* handler) {
240 login_handler_map_[tab] = handler;
241}
242
243void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
244 DCHECK(login_handler_map_[tab]);
245 login_handler_map_.erase(tab);
246}
247
[email protected]f44265b2009-05-19 18:52:50248void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
249 int port_id = port->port_id();
250 DCHECK_NE(-1, port_id);
251 DCHECK(port_containers_.find(port_id) == port_containers_.end());
252
253 port_containers_[port_id] = port;
254}
255
256void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
257 int port_id = port->port_id();
258 DCHECK_NE(-1, port_id);
259
260 PortContainerMap::iterator it = port_containers_.find(port_id);
261 DCHECK(it != port_containers_.end());
262
263 if (it != port_containers_.end()) {
264 delete it->second;
265 port_containers_.erase(it);
266 }
267}
268
269ExtensionPortContainer* AutomationProvider::GetPortContainer(
270 int port_id) const {
271 PortContainerMap::const_iterator it = port_containers_.find(port_id);
272 if (it == port_containers_.end())
273 return NULL;
274
275 return it->second;
276}
277
initial.commit09911bf2008-07-26 23:55:29278int AutomationProvider::GetIndexForNavigationController(
279 const NavigationController* controller, const Browser* parent) const {
280 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12281 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29282}
283
[email protected]790788ac2010-04-06 17:52:19284int AutomationProvider::AddExtension(Extension* extension) {
285 DCHECK(extension);
286 return extension_tracker_->Add(extension);
287}
288
289Extension* AutomationProvider::GetExtension(int extension_handle) {
290 return extension_tracker_->GetResource(extension_handle);
291}
292
293Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
294 Extension* extension = extension_tracker_->GetResource(extension_handle);
295 ExtensionsService* service = profile_->GetExtensionsService();
296 if (extension && service &&
297 service->GetExtensionById(extension->id(), false))
298 return extension;
299 return NULL;
300}
301
302Extension* AutomationProvider::GetDisabledExtension(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(), true) &&
307 !service->GetExtensionById(extension->id(), false))
308 return extension;
309 return NULL;
310}
311
initial.commit09911bf2008-07-26 23:55:29312void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
313 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14314 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56315 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
316 CloseBrowserAsync)
317 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
318 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
319 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
320 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
321 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
322 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22323 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]1c58a5c2009-05-21 18:47:14324 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58325 IPC_MESSAGE_HANDLER_DELAY_REPLY(
326 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
327 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56328 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11329 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
330 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56331 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
332 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
333 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
334 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14335 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56336 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
337 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
338 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14339 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29340 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
341 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56342 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36343 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56344 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29345 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56346 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29347 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
348 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56349 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14350 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24351 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44352 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33353 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44354 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33355 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24356 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14357 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24358 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15359 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14360 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24361 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35362 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14363 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30364#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56365 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
366 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30367#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56368 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53369 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56370 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37371#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56372 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45373#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56374 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
375 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03376 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56377 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14378 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09379 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
380 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
381 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29382 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14383 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56384 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
385 ExecuteJavascript)
386 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29387 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14388 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
389 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56390 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
391 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14392 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17393 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14394 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14395 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53396 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
397 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14398 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56399 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14400 GetAutocompleteEditForBrowser)
401 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]71f65dd2009-02-11 19:14:56402 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14403 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56404 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14405 HideInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56406 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
407 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14408 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
409 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44410 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
411 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56412 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
413 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29414 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
415 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
416 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56417 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41418 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56419 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
420 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29421 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56422 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29423 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56424 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29425 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56426 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29427 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56428 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45429 HandleOpenFindInPageRequest)
[email protected]1c58a5c2009-05-21 18:47:14430 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56431 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57432 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56433 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57434 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56435 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
436 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53437 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
438 GetBookmarksAsJSON)
439 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
440 WaitForBookmarkModelToLoad)
441 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
442 AddBookmarkGroup)
443 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
444 AddBookmarkURL)
445 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
446 ReparentBookmark)
447 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
448 SetBookmarkTitle)
449 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
450 SetBookmarkURL)
451 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
452 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04453 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
454 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18455 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
456 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
457 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56458 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42459 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56460 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
461 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14462 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56463 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44464 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56465 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20466 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14467 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56468 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16469 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56470 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16471 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56472 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16473 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14474 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20475 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
476 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14477 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]59560e0b2009-06-04 03:30:22478 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13479 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19480 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
481 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
482 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
483 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
484 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
485 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52486 IPC_MESSAGE_HANDLER_DELAY_REPLY(
487 AutomationMsg_WaitForBrowserWindowCountToBecome,
488 WaitForBrowserWindowCountToBecome)
489 IPC_MESSAGE_HANDLER_DELAY_REPLY(
490 AutomationMsg_WaitForAppModalDialogToBeShown,
491 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26492 IPC_MESSAGE_HANDLER_DELAY_REPLY(
493 AutomationMsg_GoBackBlockUntilNavigationsComplete,
494 GoBackBlockUntilNavigationsComplete)
495 IPC_MESSAGE_HANDLER_DELAY_REPLY(
496 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
497 GoForwardBlockUntilNavigationsComplete)
[email protected]1bb5f892009-10-06 01:44:57498 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40499 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
500 InstallExtension)
501 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
502 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43503 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
504 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19505 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
506 WaitForExtensionTestResult)
507 IPC_MESSAGE_HANDLER_DELAY_REPLY(
508 AutomationMsg_InstallExtensionAndGetHandle,
509 InstallExtensionAndGetHandle)
510 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
511 UninstallExtension)
512 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
513 EnableExtension)
514 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
515 DisableExtension)
516 IPC_MESSAGE_HANDLER_DELAY_REPLY(
517 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
518 ExecuteExtensionActionInActiveTabAsync)
519 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
520 MoveExtensionBrowserAction)
521 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
522 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57523 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
524 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00525 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]7dad3d5f2010-03-04 00:27:01526 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]bc73b4e52010-03-26 04:16:20527 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]bdd5a9c92010-06-14 18:21:00528 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
[email protected]cc824372010-03-31 15:33:01529#if defined(TOOLKIT_VIEWS)
530 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
531 WaitForFocusedViewIDToChange)
532 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
533 StartTrackingPopupMenus)
534 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
535 WaitForPopupMenuToOpen)
[email protected]bdd5a9c92010-06-14 18:21:00536#endif // defined(TOOLKIT_VIEWS)
[email protected]52415f842010-06-10 21:51:52537#if defined(OS_WIN)
538 // These are for use with external tabs.
539 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
540 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
541 ProcessUnhandledAccelerator)
542 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
543 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
544 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
545 OnForwardContextMenuCommandToChrome)
546 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
547 NavigateInExternalTab)
548 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
549 NavigateExternalTabAtIndex)
550 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
551 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
552 SetEnableExtensionAutomation)
553 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
554 OnMessageFromExternalHost)
[email protected]bdd5a9c92010-06-14 18:21:00555 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
556#endif // defined(OS_WIN)
557#if defined(OS_CHROMEOS)
558 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
559 LoginWithUserAndPass)
560#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29561 IPC_END_MESSAGE_MAP()
562}
563
[email protected]71f65dd2009-02-11 19:14:56564void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
565 *status = -1;
initial.commit09911bf2008-07-26 23:55:29566 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
567 Browser* browser = browser_tracker_->GetResource(handle);
568 if (at_index >= 0 && at_index < browser->tab_count()) {
569 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56570 *status = 0;
initial.commit09911bf2008-07-26 23:55:29571 }
572 }
initial.commit09911bf2008-07-26 23:55:29573}
574
[email protected]71f65dd2009-02-11 19:14:56575void AutomationProvider::AppendTab(int handle, const GURL& url,
576 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29577 int append_tab_response = -1; // -1 is the error code
578 NotificationObserver* observer = NULL;
579
580 if (browser_tracker_->ContainsHandle(handle)) {
581 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14582 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38583 TabContents* tab_contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39584 url, GURL(), PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED,
585 NULL, std::string());
initial.commit09911bf2008-07-26 23:55:29586 if (tab_contents) {
587 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57588 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29589 }
590 }
591
592 if (append_tab_response < 0) {
593 // The append tab failed. Remove the TabStripObserver
594 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17595 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29596 delete observer;
597 }
598
[email protected]71f65dd2009-02-11 19:14:56599 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
600 append_tab_response);
601 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29602 }
603}
604
[email protected]71f65dd2009-02-11 19:14:56605void AutomationProvider::NavigateToURL(int handle, const GURL& url,
606 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58607 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
608}
609
610void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
611 int handle, const GURL& url, int number_of_navigations,
612 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29613 if (tab_tracker_->ContainsHandle(handle)) {
614 NavigationController* tab = tab_tracker_->GetResource(handle);
615
616 // Simulate what a user would do. Activate the tab and then navigate.
617 // We could allow navigating in a background tab in future.
618 Browser* browser = FindAndActivateTab(tab);
619
620 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01621 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
622 false);
[email protected]71f65dd2009-02-11 19:14:56623
initial.commit09911bf2008-07-26 23:55:29624 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50625 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29626 return;
627 }
628 }
[email protected]71f65dd2009-02-11 19:14:56629
630 AutomationMsg_NavigateToURL::WriteReplyParams(
631 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
632 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29633}
[email protected]2949e90d2009-08-21 15:32:52634
[email protected]c70f9b82010-04-21 07:31:11635void AutomationProvider::NavigationAsync(int handle,
636 const GURL& url,
637 bool* status) {
638 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
639}
640
641void AutomationProvider::NavigationAsyncWithDisposition(
642 int handle,
643 const GURL& url,
644 WindowOpenDisposition disposition,
645 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56646 *status = false;
initial.commit09911bf2008-07-26 23:55:29647
648 if (tab_tracker_->ContainsHandle(handle)) {
649 NavigationController* tab = tab_tracker_->GetResource(handle);
650
651 // Simulate what a user would do. Activate the tab and then navigate.
652 // We could allow navigating in a background tab in future.
653 Browser* browser = FindAndActivateTab(tab);
654
655 if (browser) {
656 // Don't add any listener unless a callback mechanism is desired.
657 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11658 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56659 *status = true;
initial.commit09911bf2008-07-26 23:55:29660 }
661 }
initial.commit09911bf2008-07-26 23:55:29662}
663
[email protected]71f65dd2009-02-11 19:14:56664void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29665 if (tab_tracker_->ContainsHandle(handle)) {
666 NavigationController* tab = tab_tracker_->GetResource(handle);
667 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14668 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01669 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29670 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29671 return;
672 }
673 }
[email protected]71f65dd2009-02-11 19:14:56674
675 AutomationMsg_GoBack::WriteReplyParams(
676 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
677 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29678}
679
[email protected]71f65dd2009-02-11 19:14:56680void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29681 if (tab_tracker_->ContainsHandle(handle)) {
682 NavigationController* tab = tab_tracker_->GetResource(handle);
683 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14684 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01685 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29686 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29687 return;
688 }
689 }
[email protected]71f65dd2009-02-11 19:14:56690
691 AutomationMsg_GoForward::WriteReplyParams(
692 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
693 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29694}
695
[email protected]71f65dd2009-02-11 19:14:56696void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29697 if (tab_tracker_->ContainsHandle(handle)) {
698 NavigationController* tab = tab_tracker_->GetResource(handle);
699 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14700 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01701 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]cb84d642010-06-10 00:56:28702 browser->Reload(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29703 return;
704 }
705 }
[email protected]71f65dd2009-02-11 19:14:56706
707 AutomationMsg_Reload::WriteReplyParams(
708 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
709 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29710}
711
[email protected]71f65dd2009-02-11 19:14:56712void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29713 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56714 const std::wstring& password,
715 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29716 if (tab_tracker_->ContainsHandle(tab_handle)) {
717 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
718 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
719
720 if (iter != login_handler_map_.end()) {
721 // If auth is needed again after this, assume login has failed. This is
722 // not strictly correct, because a navigation can require both proxy and
723 // server auth, but it should be OK for now.
724 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01725 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29726 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52727 return;
initial.commit09911bf2008-07-26 23:55:29728 }
729 }
[email protected]de246f52009-02-25 18:25:45730
[email protected]457f5cf2009-08-18 16:37:52731 AutomationMsg_SetAuth::WriteReplyParams(
732 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
733 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29734}
735
[email protected]71f65dd2009-02-11 19:14:56736void AutomationProvider::CancelAuth(int tab_handle,
737 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29738 if (tab_tracker_->ContainsHandle(tab_handle)) {
739 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
740 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
741
742 if (iter != login_handler_map_.end()) {
743 // If auth is needed again after this, something is screwy.
744 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01745 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29746 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52747 return;
initial.commit09911bf2008-07-26 23:55:29748 }
749 }
[email protected]de246f52009-02-25 18:25:45750
[email protected]457f5cf2009-08-18 16:37:52751 AutomationMsg_CancelAuth::WriteReplyParams(
752 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
753 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29754}
755
[email protected]71f65dd2009-02-11 19:14:56756void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
757 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29758
759 if (tab_tracker_->ContainsHandle(tab_handle)) {
760 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
761 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
762
763 if (iter != login_handler_map_.end()) {
764 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56765 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29766 }
767 }
initial.commit09911bf2008-07-26 23:55:29768}
769
[email protected]71f65dd2009-02-11 19:14:56770void AutomationProvider::GetRedirectsFrom(int tab_handle,
771 const GURL& source_url,
772 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29773 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
774 if (tab_tracker_->ContainsHandle(tab_handle)) {
775 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
776 HistoryService* history_service =
777 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
778
779 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
780 "has no history service";
781 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56782 DCHECK(reply_message_ == NULL);
783 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29784 // Schedule a history query for redirects. The response will be sent
785 // asynchronously from the callback the history system uses to notify us
786 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29787 redirect_query_ = history_service->QueryRedirectsFrom(
788 source_url, &consumer_,
789 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
790 return; // Response will be sent when query completes.
791 }
792 }
793
794 // Send failure response.
[email protected]deb57402009-02-06 01:35:30795 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56796 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
797 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29798}
799
[email protected]71f65dd2009-02-11 19:14:56800void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
801 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29802 if (browser_tracker_->ContainsHandle(handle)) {
803 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56804 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29805 }
initial.commit09911bf2008-07-26 23:55:29806}
807
[email protected]202e7a72009-06-15 03:48:36808void AutomationProvider::GetBrowserLocale(string16* locale) {
809 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06810 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36811}
812
[email protected]71f65dd2009-02-11 19:14:56813void AutomationProvider::GetBrowserWindowCount(int* window_count) {
814 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29815}
816
[email protected]24497032009-05-01 17:00:29817void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
818 *window_count = static_cast<int>(
819 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
820}
821
[email protected]71f65dd2009-02-11 19:14:56822void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
823 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07824 AppModalDialog* dialog_delegate =
825 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50826 *showing_dialog = (dialog_delegate != NULL);
827 if (*showing_dialog)
828 *dialog_button = dialog_delegate->GetDialogButtons();
829 else
[email protected]478ff2ed2009-04-21 23:49:18830 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20831}
832
[email protected]71f65dd2009-02-11 19:14:56833void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
834 *success = false;
[email protected]fad84eab2008-12-05 00:37:20835
[email protected]1f460072009-05-28 17:02:07836 AppModalDialog* dialog_delegate =
837 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50838 if (dialog_delegate &&
839 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18840 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
841 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28842 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56843 *success = true;
[email protected]fad84eab2008-12-05 00:37:20844 }
[email protected]478ff2ed2009-04-21 23:49:18845 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
846 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56847 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28848 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56849 *success = true;
[email protected]fad84eab2008-12-05 00:37:20850 }
851 }
[email protected]c274acc2008-11-11 20:13:44852}
853
[email protected]fedaa7d2010-01-26 20:34:57854void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
855 if (browser_tracker_->ContainsHandle(handle)) {
856 Browser* browser = browser_tracker_->GetResource(handle);
857 browser->profile()->ShutdownSessionService();
858 *result = true;
859 } else {
860 *result = false;
861 }
862}
863
[email protected]71f65dd2009-02-11 19:14:56864void AutomationProvider::GetBrowserWindow(int index, int* handle) {
865 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29866 if (index >= 0) {
867 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]f07467d2010-06-16 14:28:30868 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {}
initial.commit09911bf2008-07-26 23:55:29869 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56870 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29871 }
872 }
initial.commit09911bf2008-07-26 23:55:29873}
874
[email protected]24497032009-05-01 17:00:29875void AutomationProvider::FindNormalBrowserWindow(int* handle) {
876 *handle = 0;
877 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31878 Browser::TYPE_NORMAL,
879 false);
[email protected]24497032009-05-01 17:00:29880 if (browser)
881 *handle = browser_tracker_->Add(browser);
882}
883
[email protected]71f65dd2009-02-11 19:14:56884void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
885 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29886 Browser* browser = BrowserList::GetLastActive();
887 if (browser)
[email protected]71f65dd2009-02-11 19:14:56888 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29889}
890
[email protected]b2aa3ed72010-02-01 18:37:14891#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28892// TODO(estade): use this implementation for all platforms?
893void AutomationProvider::GetActiveWindow(int* handle) {
894 gfx::NativeWindow window =
895 BrowserList::GetLastActive()->window()->GetNativeHandle();
896 *handle = window_tracker_->Add(window);
897}
898#endif
899
[email protected]4f6381ee2009-04-16 02:46:33900void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
901 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56902 *success = false;
[email protected]4ae62752008-08-04 23:28:47903 if (browser_tracker_->ContainsHandle(handle)) {
904 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14905 if (browser->command_updater()->SupportsCommand(command) &&
906 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47907 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56908 *success = true;
[email protected]4ae62752008-08-04 23:28:47909 }
910 }
[email protected]4ae62752008-08-04 23:28:47911}
912
[email protected]4f6381ee2009-04-16 02:46:33913void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56914 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53915 // List of commands which just finish synchronously and don't require
916 // setting up an observer.
917 static const int kSynchronousCommands[] = {
918 IDC_HOME,
919 IDC_SELECT_NEXT_TAB,
920 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25921 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53922 };
[email protected]56e71b7c2009-03-27 03:05:56923 if (browser_tracker_->ContainsHandle(handle)) {
924 Browser* browser = browser_tracker_->GetResource(handle);
925 if (browser->command_updater()->SupportsCommand(command) &&
926 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53927 // First check if we can handle the command without using an observer.
928 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
929 if (command == kSynchronousCommands[i]) {
930 browser->ExecuteCommand(command);
931 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
932 true);
933 Send(reply_message);
934 return;
935 }
936 }
937
938 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42939 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
940 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27941 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42942 return;
943 }
[email protected]56e71b7c2009-03-27 03:05:56944 }
945 }
[email protected]49a14a82009-03-31 04:16:44946 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56947 Send(reply_message);
948}
949
[email protected]fc2e0872009-08-21 22:14:41950// This task just adds another task to the event queue. This is useful if
951// you want to ensure that any tasks added to the event queue after this one
952// have already been processed by the time |task| is run.
953class InvokeTaskLaterTask : public Task {
954 public:
955 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
956 virtual ~InvokeTaskLaterTask() {}
957
958 virtual void Run() {
959 MessageLoop::current()->PostTask(FROM_HERE, task_);
960 }
961
962 private:
963 Task* task_;
964
965 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
966};
967
initial.commit09911bf2008-07-26 23:55:29968void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
969 int handle,
[email protected]d1a5941e2009-08-13 23:34:24970 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29971 int flags) {
[email protected]b410bc32009-08-14 01:11:14972 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51973 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41974 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29975 }
976}
977
[email protected]60507b12009-11-02 23:51:35978void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
979 int handle,
980 const gfx::Point& location) {
981 if (window_tracker_->ContainsHandle(handle))
982 ui_controls::SendMouseMove(location.x(), location.y());
983}
984
initial.commit09911bf2008-07-26 23:55:29985void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
986 int handle,
[email protected]bc1407f2009-09-29 00:33:35987 int key,
initial.commit09911bf2008-07-26 23:55:29988 int flags) {
[email protected]b410bc32009-08-14 01:11:14989 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29990 return;
991
[email protected]b410bc32009-08-14 01:11:14992 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29993 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35994 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38995 ((flags & views::Event::EF_CONTROL_DOWN) ==
996 views::Event::EF_CONTROL_DOWN),
997 ((flags & views::Event::EF_SHIFT_DOWN) ==
998 views::Event::EF_SHIFT_DOWN),
999 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301000 views::Event::EF_ALT_DOWN),
1001 ((flags & views::Event::EF_COMMAND_DOWN) ==
1002 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291003}
initial.commit09911bf2008-07-26 23:55:291004
[email protected]71f65dd2009-02-11 19:14:561005void AutomationProvider::IsWindowActive(int handle, bool* success,
1006 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291007 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171008 *is_active =
1009 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561010 *success = true;
initial.commit09911bf2008-07-26 23:55:291011 } else {
[email protected]71f65dd2009-02-11 19:14:561012 *success = false;
1013 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291014 }
1015}
1016
[email protected]71f65dd2009-02-11 19:14:561017void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1018 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291019
1020 if (browser_tracker_->ContainsHandle(handle)) {
1021 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561022 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291023 }
initial.commit09911bf2008-07-26 23:55:291024}
1025
[email protected]982921f12009-10-27 21:43:531026void AutomationProvider::GetType(int handle, int* type_as_int) {
1027 *type_as_int = -1; // -1 is the error code
1028
1029 if (browser_tracker_->ContainsHandle(handle)) {
1030 Browser* browser = browser_tracker_->GetResource(handle);
1031 *type_as_int = static_cast<int>(browser->type());
1032 }
1033}
1034
[email protected]71f65dd2009-02-11 19:14:561035void AutomationProvider::GetTab(int win_handle, int tab_index,
1036 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561037 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291038 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1039 Browser* browser = browser_tracker_->GetResource(win_handle);
1040 if (tab_index < browser->tab_count()) {
1041 TabContents* tab_contents =
1042 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571043 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291044 }
1045 }
initial.commit09911bf2008-07-26 23:55:291046}
1047
[email protected]71f65dd2009-02-11 19:14:561048void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1049 std::wstring* title) {
1050 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291051 if (tab_tracker_->ContainsHandle(handle)) {
1052 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361053 NavigationEntry* entry = tab->GetActiveEntry();
1054 if (entry != NULL) {
1055 *title = UTF16ToWideHack(entry->title());
1056 } else {
1057 *title = std::wstring();
1058 }
[email protected]71f65dd2009-02-11 19:14:561059 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291060 }
initial.commit09911bf2008-07-26 23:55:291061}
1062
[email protected]77bc6732009-04-20 22:01:031063void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1064 *tabstrip_index = -1; // -1 is the error code
1065
1066 if (tab_tracker_->ContainsHandle(handle)) {
1067 NavigationController* tab = tab_tracker_->GetResource(handle);
1068 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121069 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031070 }
1071}
1072
initial.commit09911bf2008-07-26 23:55:291073void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1074 if (window_tracker_->ContainsHandle(handle)) {
1075 window_tracker_->Remove(window_tracker_->GetResource(handle));
1076 }
1077}
1078
1079void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161080 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571081 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291082}
1083
1084// TODO(brettw) change this to accept GURLs when history supports it
1085void AutomationProvider::OnRedirectQueryComplete(
1086 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371087 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291088 bool success,
[email protected]379c2b12009-07-01 21:50:331089 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291090 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561091 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291092
[email protected]deb57402009-02-06 01:35:301093 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101094 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291095 if (success) {
initial.commit09911bf2008-07-26 23:55:291096 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301097 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291098 }
1099
[email protected]4f3dc372009-02-24 00:10:291100 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301101
[email protected]71f65dd2009-02-11 19:14:561102 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231103 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561104 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291105}
1106
1107bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571108 DCHECK(channel_.get());
1109 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291110}
1111
1112Browser* AutomationProvider::FindAndActivateTab(
1113 NavigationController* controller) {
1114 int tab_index;
1115 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1116 if (browser)
1117 browser->SelectTabContentsAt(tab_index, true);
1118
1119 return browser;
1120}
1121
[email protected]71f65dd2009-02-11 19:14:561122void AutomationProvider::GetCookies(const GURL& url, int handle,
1123 int* value_size,
1124 std::string* value) {
1125 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291126 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1127 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311128
1129 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001130 scoped_refptr<URLRequestContextGetter> request_context =
1131 tab->tab_contents()->request_context();
1132 if (!request_context.get())
1133 request_context = tab->profile()->GetRequestContext();
1134
[email protected]34d18e42010-06-21 16:04:501135 net::CookieStore* cookie_store = request_context->GetCookieStore();
1136
1137 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561138 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291139 }
initial.commit09911bf2008-07-26 23:55:291140}
1141
[email protected]71f65dd2009-02-11 19:14:561142void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291143 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561144 int handle,
1145 int* response_value) {
1146 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291147
1148 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1149 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311150
[email protected]dfa46e5f2009-11-17 18:48:431151 scoped_refptr<URLRequestContextGetter> request_context =
1152 tab->tab_contents()->request_context();
1153 if (!request_context.get())
1154 request_context = tab->profile()->GetRequestContext();
1155
[email protected]34d18e42010-06-21 16:04:501156 // Since we are running on the UI thread don't call GetURLRequestContext().
1157 scoped_refptr<net::CookieStore> cookie_store =
1158 request_context->GetCookieStore();
1159
1160 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561161 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291162 }
initial.commit09911bf2008-07-26 23:55:291163}
1164
[email protected]5fa57942010-04-21 23:07:221165void AutomationProvider::DeleteCookie(const GURL& url,
1166 const std::string& cookie_name,
1167 int handle, bool* success) {
1168 *success = false;
1169 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1170 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]34d18e42010-06-21 16:04:501171 net::CookieStore* cookie_store =
1172 tab->profile()->GetRequestContext()->GetCookieStore();
1173 cookie_store->DeleteCookie(url, cookie_name);
[email protected]5fa57942010-04-21 23:07:221174 *success = true;
1175 }
1176}
1177
[email protected]71f65dd2009-02-11 19:14:561178void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1179 *success = false;
initial.commit09911bf2008-07-26 23:55:291180 if (tab_tracker_->ContainsHandle(handle)) {
1181 NavigationController* tab = tab_tracker_->GetResource(handle);
1182 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541183 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561184 *success = true;
initial.commit09911bf2008-07-26 23:55:291185 }
initial.commit09911bf2008-07-26 23:55:291186}
1187
[email protected]71f65dd2009-02-11 19:14:561188void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1189 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291190
1191 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561192 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341193 TabContents* tab_contents =
1194 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161195 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1196 if (rph)
1197 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291198 }
initial.commit09911bf2008-07-26 23:55:291199}
1200
1201void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331202 NOTREACHED() << "This function has been deprecated. "
1203 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291204}
1205
[email protected]71f65dd2009-02-11 19:14:561206void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291207 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561208 const std::wstring& script,
1209 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291210 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341211 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1212 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571213 // Set the routing id of this message with the controller.
1214 // This routing id needs to be remembered for the reverse
1215 // communication while sending back the response of
1216 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331217 std::wstring set_automation_id;
1218 SStringPrintf(&set_automation_id,
1219 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561220 reply_message->routing_id());
1221
1222 DCHECK(reply_message_ == NULL);
1223 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291224
[email protected]57c6a652009-05-04 07:58:341225 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331226 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341227 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061228 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571229 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291230 }
1231
1232 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561233 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1234 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291235 }
1236}
1237
[email protected]71f65dd2009-02-11 19:14:561238void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1239 *visible = false;
[email protected]20e93d12008-08-28 16:31:571240
[email protected]59560e0b2009-06-04 03:30:221241 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251242#if defined(OS_CHROMEOS)
1243 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1244 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1245 for (BrowserList::const_iterator it = BrowserList::begin();
1246 it != BrowserList::end(); ++it) {
1247 if ((*it)->type() == Browser::TYPE_POPUP) {
1248 const GURL& url =
1249 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1250
1251 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1252 url.host() == chrome::kChromeUIFileBrowseHost) {
1253 *visible = true;
1254 break;
1255 }
1256 }
1257 }
1258#else
[email protected]59560e0b2009-06-04 03:30:221259 Browser* browser = browser_tracker_->GetResource(handle);
1260 if (browser) {
1261 *visible = browser->window()->IsDownloadShelfVisible();
1262 }
[email protected]f5bf8ccf2010-02-05 18:19:251263#endif
[email protected]59560e0b2009-06-04 03:30:221264 }
initial.commit09911bf2008-07-26 23:55:291265}
1266
[email protected]59560e0b2009-06-04 03:30:221267void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1268 if (browser_tracker_->ContainsHandle(handle)) {
1269 Browser* browser = browser_tracker_->GetResource(handle);
1270 if (browser) {
1271 if (visible)
1272 browser->window()->GetDownloadShelf()->Show();
1273 else
1274 browser->window()->GetDownloadShelf()->Close();
1275 }
1276 }
1277}
1278
[email protected]34930432009-11-09 00:12:091279void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1280 *visible = false;
1281
1282 if (browser_tracker_->ContainsHandle(handle)) {
1283 Browser* browser = browser_tracker_->GetResource(handle);
1284 if (browser)
1285 *visible = browser->window()->IsFullscreen();
1286 }
1287}
1288
1289void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1290 bool* visible) {
1291 *visible = false;
1292
1293 if (browser_tracker_->ContainsHandle(handle)) {
1294 Browser* browser = browser_tracker_->GetResource(handle);
1295 if (browser)
1296 *visible = browser->window()->IsFullscreenBubbleVisible();
1297 }
1298}
[email protected]59560e0b2009-06-04 03:30:221299
[email protected]71f65dd2009-02-11 19:14:561300void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1301 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291302 if (tab_tracker_->ContainsHandle(handle)) {
1303 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111304 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291305 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561306 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291307 }
1308 }
initial.commit09911bf2008-07-26 23:55:291309}
1310
initial.commit09911bf2008-07-26 23:55:291311void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561312 int handle, const std::wstring& find_request,
1313 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311314 NOTREACHED() << "This function has been deprecated."
1315 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561316 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311317 return;
1318}
1319
[email protected]4f999132009-03-31 18:08:401320void AutomationProvider::HandleFindRequest(
1321 int handle,
1322 const AutomationMsg_Find_Params& params,
1323 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291324 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561325 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1326 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291327 return;
1328 }
1329
1330 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111331 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291332
1333 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141334 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291335
[email protected]57c6a652009-05-04 07:58:341336 tab_contents->set_current_find_request_id(
1337 FindInPageNotificationObserver::kFindInPageRequestId);
1338 tab_contents->render_view_host()->StartFinding(
1339 FindInPageNotificationObserver::kFindInPageRequestId,
1340 params.search_string, params.forward, params.match_case,
1341 params.find_next);
initial.commit09911bf2008-07-26 23:55:291342}
1343
[email protected]5f8af2a2008-08-06 22:49:451344void AutomationProvider::HandleOpenFindInPageRequest(
1345 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291346 if (browser_tracker_->ContainsHandle(handle)) {
1347 Browser* browser = browser_tracker_->GetResource(handle);
1348 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451349 }
1350}
1351
[email protected]71f65dd2009-02-11 19:14:561352void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561353 *visible = false;
[email protected]855c0142009-09-28 22:35:241354 Browser* browser = browser_tracker_->GetResource(handle);
1355 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581356 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171357 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241358 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291359 }
[email protected]20e93d12008-08-28 16:31:571360}
1361
[email protected]71f65dd2009-02-11 19:14:561362void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1363 int* y) {
[email protected]9e0534b2008-10-21 15:03:011364 gfx::Point position(0, 0);
1365 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291366 if (browser_tracker_->ContainsHandle(handle)) {
1367 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581368 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171369 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581370 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291371 }
[email protected]20e93d12008-08-28 16:31:571372
[email protected]71f65dd2009-02-11 19:14:561373 *x = position.x();
1374 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571375}
1376
[email protected]4512cb52010-04-05 19:50:251377// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1378// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581379void AutomationProvider::GetBookmarkBarVisibility(int handle,
1380 bool* visible,
1381 bool* animating) {
1382 *visible = false;
1383 *animating = false;
1384
1385 if (browser_tracker_->ContainsHandle(handle)) {
1386 Browser* browser = browser_tracker_->GetResource(handle);
1387 if (browser) {
[email protected]472f099b2010-05-27 17:07:121388#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1389 // TODO(jrg): Was removed in rev43789 for perf. Need to investigate.
1390
[email protected]ab6ca392010-04-07 00:44:131391 // IsBookmarkBarVisible() line looks correct but is not
1392 // consistent across platforms. Specifically, on Mac/Linux, it
1393 // returns false if the bar is hidden in a pref (even if visible
1394 // on the NTP). On ChromeOS, it returned true if on NTP
1395 // independent of the pref. Making the code more consistent
1396 // caused a perf bot regression on Windows (which shares views).
1397 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251398 *visible = browser->profile()->GetPrefs()->GetBoolean(
1399 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401400#else
1401 *visible = browser->window()->IsBookmarkBarVisible();
1402#endif
[email protected]c3240722010-03-05 21:52:581403 *animating = browser->window()->IsBookmarkBarAnimating();
1404 }
1405 }
1406}
1407
[email protected]6d8ffc9f2010-03-12 18:27:531408void AutomationProvider::GetBookmarksAsJSON(int handle,
1409 std::string* bookmarks_as_json,
1410 bool *success) {
1411 *success = false;
1412 if (browser_tracker_->ContainsHandle(handle)) {
1413 Browser* browser = browser_tracker_->GetResource(handle);
1414 if (browser) {
1415 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1416 return;
1417 }
1418 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1419 browser->profile(),
1420 browser->profile()->GetBookmarkModel());
1421 *success = storage->SerializeData(bookmarks_as_json);
1422 }
1423 }
1424}
1425
1426void AutomationProvider::WaitForBookmarkModelToLoad(
1427 int handle,
1428 IPC::Message* reply_message) {
1429 if (browser_tracker_->ContainsHandle(handle)) {
1430 Browser* browser = browser_tracker_->GetResource(handle);
1431 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1432 if (model->IsLoaded()) {
1433 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1434 reply_message, true);
1435 Send(reply_message);
1436 } else {
1437 // The observer will delete itself when done.
1438 new AutomationProviderBookmarkModelObserver(this, reply_message,
1439 model);
1440 }
1441 }
1442}
1443
1444void AutomationProvider::AddBookmarkGroup(int handle,
1445 int64 parent_id, int index,
1446 std::wstring title,
1447 bool* success) {
1448 if (browser_tracker_->ContainsHandle(handle)) {
1449 Browser* browser = browser_tracker_->GetResource(handle);
1450 if (browser) {
1451 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1452 if (!model->IsLoaded()) {
1453 *success = false;
1454 return;
1455 }
1456 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1457 DCHECK(parent);
1458 if (parent) {
1459 const BookmarkNode* child = model->AddGroup(parent, index,
1460 WideToUTF16(title));
1461 DCHECK(child);
1462 if (child)
1463 *success = true;
1464 }
1465 }
1466 }
1467 *success = false;
1468}
1469
1470void AutomationProvider::AddBookmarkURL(int handle,
1471 int64 parent_id, int index,
1472 std::wstring title, const GURL& url,
1473 bool* success) {
1474 if (browser_tracker_->ContainsHandle(handle)) {
1475 Browser* browser = browser_tracker_->GetResource(handle);
1476 if (browser) {
1477 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1478 if (!model->IsLoaded()) {
1479 *success = false;
1480 return;
1481 }
1482 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1483 DCHECK(parent);
1484 if (parent) {
1485 const BookmarkNode* child = model->AddURL(parent, index,
1486 WideToUTF16(title), url);
1487 DCHECK(child);
1488 if (child)
1489 *success = true;
1490 }
1491 }
1492 }
1493 *success = false;
1494}
1495
1496void AutomationProvider::ReparentBookmark(int handle,
1497 int64 id, int64 new_parent_id,
1498 int index,
1499 bool* success) {
1500 if (browser_tracker_->ContainsHandle(handle)) {
1501 Browser* browser = browser_tracker_->GetResource(handle);
1502 if (browser) {
1503 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1504 if (!model->IsLoaded()) {
1505 *success = false;
1506 return;
1507 }
1508 const BookmarkNode* node = model->GetNodeByID(id);
1509 DCHECK(node);
1510 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1511 DCHECK(new_parent);
1512 if (node && new_parent) {
1513 model->Move(node, new_parent, index);
1514 *success = true;
1515 }
1516 }
1517 }
1518 *success = false;
1519}
1520
1521void AutomationProvider::SetBookmarkTitle(int handle,
1522 int64 id, std::wstring title,
1523 bool* success) {
1524 if (browser_tracker_->ContainsHandle(handle)) {
1525 Browser* browser = browser_tracker_->GetResource(handle);
1526 if (browser) {
1527 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1528 if (!model->IsLoaded()) {
1529 *success = false;
1530 return;
1531 }
1532 const BookmarkNode* node = model->GetNodeByID(id);
1533 DCHECK(node);
1534 if (node) {
1535 model->SetTitle(node, WideToUTF16(title));
1536 *success = true;
1537 }
1538 }
1539 }
1540 *success = false;
1541}
1542
1543void AutomationProvider::SetBookmarkURL(int handle,
1544 int64 id, const GURL& url,
1545 bool* success) {
1546 if (browser_tracker_->ContainsHandle(handle)) {
1547 Browser* browser = browser_tracker_->GetResource(handle);
1548 if (browser) {
1549 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1550 if (!model->IsLoaded()) {
1551 *success = false;
1552 return;
1553 }
1554 const BookmarkNode* node = model->GetNodeByID(id);
1555 DCHECK(node);
1556 if (node) {
1557 model->SetURL(node, url);
1558 *success = true;
1559 }
1560 }
1561 }
1562 *success = false;
1563}
1564
1565void AutomationProvider::RemoveBookmark(int handle,
1566 int64 id,
1567 bool* success) {
1568 if (browser_tracker_->ContainsHandle(handle)) {
1569 Browser* browser = browser_tracker_->GetResource(handle);
1570 if (browser) {
1571 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1572 if (!model->IsLoaded()) {
1573 *success = false;
1574 return;
1575 }
1576 const BookmarkNode* node = model->GetNodeByID(id);
1577 DCHECK(node);
1578 if (node) {
1579 const BookmarkNode* parent = node->GetParent();
1580 DCHECK(parent);
1581 model->Remove(parent, parent->IndexOfChild(node));
1582 *success = true;
1583 }
1584 }
1585 }
1586 *success = false;
1587}
1588
[email protected]ef413ca2010-05-25 21:09:141589// Sample json input: { "command": "SetWindowDimensions",
1590// "x": 20, # optional
1591// "y": 20, # optional
1592// "width": 800, # optional
1593// "height": 600 } # optional
1594void AutomationProvider::SetWindowDimensions(Browser* browser,
1595 DictionaryValue* args,
1596 IPC::Message* reply_message) {
1597 gfx::Rect rect = browser->window()->GetRestoredBounds();
1598 int x, y, width, height;
1599 if (args->GetInteger(L"x", &x))
1600 rect.set_x(x);
1601 if (args->GetInteger(L"y", &y))
1602 rect.set_y(y);
1603 if (args->GetInteger(L"width", &width))
1604 rect.set_width(width);
1605 if (args->GetInteger(L"height", &height))
1606 rect.set_height(height);
1607 browser->window()->SetBounds(rect);
1608 AutomationMsg_SendJSONRequest::WriteReplyParams(
1609 reply_message, std::string("{}"), true);
1610 Send(reply_message);
1611}
1612
[email protected]a9ff2c02010-05-13 17:33:051613// Sample json input: { "command": "GetBrowserInfo" }
1614// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1615// sample json output.
[email protected]53329582010-05-14 21:10:581616void AutomationProvider::GetBrowserInfo(Browser* browser,
1617 DictionaryValue* args,
[email protected]a9ff2c02010-05-13 17:33:051618 IPC::Message* reply_message) {
1619 std::string json_return;
1620 bool reply_return = true;
1621
1622 DictionaryValue* properties = new DictionaryValue;
1623 properties->SetString(L"ChromeVersion", chrome::kChromeVersion);
1624 properties->SetString(L"BrowserProcessExecutableName",
1625 chrome::kBrowserProcessExecutableName);
1626 properties->SetString(L"HelperProcessExecutableName",
1627 chrome::kHelperProcessExecutableName);
1628 properties->SetString(L"BrowserProcessExecutablePath",
1629 chrome::kBrowserProcessExecutablePath);
1630 properties->SetString(L"HelperProcessExecutablePath",
1631 chrome::kHelperProcessExecutablePath);
[email protected]a9ff2c02010-05-13 17:33:051632 properties->SetString(L"command_line_string",
1633 CommandLine::ForCurrentProcess()->command_line_string());
[email protected]44eed9f2010-06-28 22:04:001634
1635 std::string branding;
1636#if defined(GOOGLE_CHROME_BUILD)
1637 branding = "Google Chrome";
1638#elif defined(CHROMIUM_BUILD)
1639 branding = "Chromium";
1640#else
1641 branding = "Unknown Branding";
[email protected]a9ff2c02010-05-13 17:33:051642#endif
[email protected]44eed9f2010-06-28 22:04:001643 properties->SetString(L"branding", branding);
[email protected]a9ff2c02010-05-13 17:33:051644
1645 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1646 return_value->Set(L"properties", properties);
1647
[email protected]ef413ca2010-05-25 21:09:141648 return_value->SetInteger(L"browser_pid", base::GetCurrentProcId());
1649 // Add info about all windows in a list of dictionaries, one dictionary
1650 // item per window.
1651 ListValue* windows = new ListValue;
1652 int windex = 0;
1653 for (BrowserList::const_iterator it = BrowserList::begin();
1654 it != BrowserList::end();
1655 ++it, ++windex) {
1656 DictionaryValue* browser_item = new DictionaryValue;
1657 browser = *it;
1658 browser_item->SetInteger(L"index", windex);
1659 // Window properties
1660 gfx::Rect rect = browser->window()->GetRestoredBounds();
1661 browser_item->SetInteger(L"x", rect.x());
1662 browser_item->SetInteger(L"y", rect.y());
1663 browser_item->SetInteger(L"width", rect.width());
1664 browser_item->SetInteger(L"height", rect.height());
1665 browser_item->SetBoolean(L"fullscreen",
1666 browser->window()->IsFullscreen());
1667 browser_item->SetInteger(L"selected_tab", browser->selected_index());
1668 browser_item->SetBoolean(L"incognito",
1669 browser->profile()->IsOffTheRecord());
1670 // For each window, add info about all tabs in a list of dictionaries,
1671 // one dictionary item per tab.
1672 ListValue* tabs = new ListValue;
1673 for (int i = 0; i < browser->tab_count(); ++i) {
1674 TabContents* tc = browser->GetTabContentsAt(i);
1675 DictionaryValue* tab = new DictionaryValue;
1676 tab->SetInteger(L"index", i);
1677 tab->SetString(L"url", tc->GetURL().spec());
1678 tab->SetInteger(L"renderer_pid",
1679 base::GetProcId(tc->GetRenderProcessHost()->GetHandle()));
1680 tab->SetInteger(L"num_infobars", tc->infobar_delegate_count());
1681 tabs->Append(tab);
1682 }
1683 browser_item->Set(L"tabs", tabs);
1684
1685 windows->Append(browser_item);
1686 }
1687 return_value->Set(L"windows", windows);
1688
1689 return_value->SetString(L"child_process_path",
1690 ChildProcessHost::GetChildPath(true).value());
1691 // Child processes are the processes for plugins and other workers.
1692 // Add all child processes in a list of dictionaries, one dictionary item
1693 // per child process.
1694 ListValue* child_processes = new ListValue;
1695 for (ChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
1696 // Only add processes which are already started, since we need their handle.
1697 if ((*iter)->handle() != base::kNullProcessHandle) {
1698 ChildProcessInfo* info = *iter;
1699 DictionaryValue* item = new DictionaryValue;
1700 item->SetString(L"name", info->name());
1701 item->SetString(L"type",
1702 ChildProcessInfo::GetTypeNameInEnglish(info->type()));
1703 item->SetInteger(L"pid", base::GetProcId(info->handle()));
1704 child_processes->Append(item);
1705 }
1706 }
1707 return_value->Set(L"child_processes", child_processes);
1708
1709 // Add all extension processes in a list of dictionaries, one dictionary
1710 // item per extension process.
1711 ListValue* extension_processes = new ListValue;
1712 ProfileManager* profile_manager = g_browser_process->profile_manager();
1713 for (ProfileManager::const_iterator it = profile_manager->begin();
1714 it != profile_manager->end(); ++it) {
1715 ExtensionProcessManager* process_manager =
1716 (*it)->GetExtensionProcessManager();
1717 ExtensionProcessManager::const_iterator jt;
1718 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) {
1719 ExtensionHost* ex_host = *jt;
1720 // Don't add dead extension processes.
1721 if (!ex_host->IsRenderViewLive())
1722 continue;
1723 DictionaryValue* item = new DictionaryValue;
1724 item->SetString(L"name", ex_host->extension()->name());
1725 item->SetInteger(
1726 L"pid",
1727 base::GetProcId(ex_host->render_process_host()->GetHandle()));
1728 extension_processes->Append(item);
1729 }
1730 }
1731 return_value->Set(L"extension_processes", extension_processes);
1732
[email protected]a9ff2c02010-05-13 17:33:051733 base::JSONWriter::Write(return_value.get(), false, &json_return);
1734 AutomationMsg_SendJSONRequest::WriteReplyParams(
1735 reply_message, json_return, reply_return);
1736 Send(reply_message);
1737}
1738
[email protected]24e2b102010-04-29 17:56:471739// Sample json input: { "command": "GetHistoryInfo",
1740// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361741// Refer chrome/test/pyautolib/history_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581742void AutomationProvider::GetHistoryInfo(Browser* browser,
1743 DictionaryValue* args,
1744 IPC::Message* reply_message) {
[email protected]e6e376e2010-04-19 21:41:361745 consumer_.CancelAllRequests();
1746
[email protected]e53668962010-06-23 15:35:251747 string16 search_text;
1748 args->GetString("search_text", &search_text);
[email protected]e6e376e2010-04-19 21:41:361749
1750 // Fetch history.
1751 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1752 history::QueryOptions options;
1753 // The observer owns itself. It deletes itself after it fetches history.
1754 AutomationProviderHistoryObserver* history_observer =
1755 new AutomationProviderHistoryObserver(this, reply_message);
1756 hs->QueryHistory(
1757 search_text,
1758 options,
1759 &consumer_,
1760 NewCallback(history_observer,
1761 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1762}
1763
[email protected]bbe6aa02010-05-07 17:27:291764// Sample json input: { "command": "AddHistoryItem",
1765// "item": { "URL": "http://www.google.com",
1766// "title": "Google", # optional
1767// "time": 12345 # optional (time_t)
1768// } }
1769// Refer chrome/test/pyautolib/pyauto.py for details on input.
[email protected]53329582010-05-14 21:10:581770void AutomationProvider::AddHistoryItem(Browser* browser,
1771 DictionaryValue* args,
1772 IPC::Message* reply_message) {
[email protected]bbe6aa02010-05-07 17:27:291773 bool reply_return = true;
1774 std::string json_return = "{}";
1775
1776 DictionaryValue* item = NULL;
1777 args->GetDictionary(L"item", &item);
1778 string16 url_text;
[email protected]e53668962010-06-23 15:35:251779 string16 title;
[email protected]bbe6aa02010-05-07 17:27:291780 base::Time time = base::Time::Now();
1781
1782 if (item->GetString("url", &url_text)) {
1783 GURL gurl(url_text);
[email protected]e53668962010-06-23 15:35:251784 item->GetString("title", &title); // Don't care if it fails.
[email protected]bbe6aa02010-05-07 17:27:291785 int it;
1786 double dt;
1787 if (item->GetInteger(L"time", &it))
1788 time = base::Time::FromTimeT(it);
1789 else if (item->GetReal(L"time", &dt))
1790 time = base::Time::FromDoubleT(dt);
1791
1792 // Ideas for "dummy" values (e.g. id_scope) came from
1793 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
1794 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1795 const void* id_scope = reinterpret_cast<void*>(1);
1796 hs->AddPage(gurl, time,
1797 id_scope,
1798 0,
1799 GURL(),
1800 PageTransition::LINK,
1801 history::RedirectList(),
1802 false);
[email protected]e53668962010-06-23 15:35:251803 if (title.length())
[email protected]bbe6aa02010-05-07 17:27:291804 hs->SetPageTitle(gurl, title);
[email protected]bbe6aa02010-05-07 17:27:291805 } else {
[email protected]7060bb292010-06-24 00:52:491806 json_return = JSONErrorString("bad args (no URL in dict?)");
[email protected]bbe6aa02010-05-07 17:27:291807 reply_return = false;
1808 }
1809
1810 AutomationMsg_SendJSONRequest::WriteReplyParams(
1811 reply_message, json_return, reply_return);
1812 Send(reply_message);
1813}
1814
[email protected]24e2b102010-04-29 17:56:471815// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361816// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581817void AutomationProvider::GetDownloadsInfo(Browser* browser,
1818 DictionaryValue* args,
1819 IPC::Message* reply_message) {
[email protected]d4adc292010-04-15 18:06:391820 std::string json_return;
1821 bool reply_return = true;
1822 AutomationProviderDownloadManagerObserver observer;
1823 std::vector<DownloadItem*> downloads;
1824 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1825
1826 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7060bb292010-06-24 00:52:491827 json_return = JSONErrorString("no download manager");
[email protected]d4adc292010-04-15 18:06:391828 reply_return = false;
1829 } else {
1830 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1831 // since that would be transient; a download might enter and empty out
1832 // the current download queue too soon to be noticed.
1833 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1834 downloads = observer.Downloads();
1835 }
1836
1837 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1838 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1839 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1840 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1841 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1842
1843 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1844 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1845 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1846 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1847 std::string("DANGEROUS_BUT_VALIDATED");
1848
1849 ListValue* list_of_downloads = new ListValue;
1850 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1851 it != downloads.end();
1852 it++) { // Fill info about each download item.
1853 DictionaryValue* dl_item_value = new DictionaryValue;
1854 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1855 dl_item_value->SetString(L"url", (*it)->url().spec());
1856 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1857 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1858 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1859 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1860 dl_item_value->SetBoolean(L"open_when_complete",
1861 (*it)->open_when_complete());
1862 dl_item_value->SetBoolean(L"is_extension_install",
1863 (*it)->is_extension_install());
1864 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1865 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1866 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1867 dl_item_value->SetString(L"safety_state",
1868 safety_state_to_string[(*it)->safety_state()]);
1869 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1870 list_of_downloads->Append(dl_item_value);
1871 }
1872 return_value->Set(L"downloads", list_of_downloads);
1873 base::JSONWriter::Write(return_value.get(), false, &json_return);
1874
1875 AutomationMsg_SendJSONRequest::WriteReplyParams(
1876 reply_message, json_return, reply_return);
1877 Send(reply_message);
1878 // All value objects allocated above are owned by |return_value|
1879 // and get freed by it.
1880}
1881
[email protected]59a611242010-04-02 02:24:041882void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:581883 Browser* browser,
[email protected]59a611242010-04-02 02:24:041884 DictionaryValue* args,
1885 IPC::Message* reply_message) {
1886 std::string json_return;
1887 bool reply_return = true;
1888 AutomationProviderDownloadManagerObserver observer;
1889 std::vector<DownloadItem*> downloads;
1890
1891 // Look for a quick return.
1892 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7060bb292010-06-24 00:52:491893 json_return = JSONErrorString("no download manager");
[email protected]59a611242010-04-02 02:24:041894 reply_return = false;
1895 } else {
1896 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
1897 FilePath());
1898 downloads = observer.Downloads();
1899 if (downloads.size() == 0) {
1900 json_return = "{}";
1901 }
1902 }
1903 if (!json_return.empty()) {
1904 AutomationMsg_SendJSONRequest::WriteReplyParams(
1905 reply_message, json_return, reply_return);
1906 Send(reply_message);
1907 }
1908
1909 // The observer owns itself. When the last observed item pings, it
1910 // deletes itself.
1911 AutomationProviderDownloadItemObserver* item_observer =
1912 new AutomationProviderDownloadItemObserver(
1913 this, reply_message, downloads.size());
1914 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
1915 i != downloads.end();
1916 i++) {
1917 (*i)->AddObserver(item_observer);
1918 }
1919}
1920
[email protected]24e2b102010-04-29 17:56:471921// Sample json input: { "command": "GetPrefsInfo" }
1922// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581923void AutomationProvider::GetPrefsInfo(Browser* browser,
1924 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:471925 IPC::Message* reply_message) {
1926 std::string json_return;
1927 bool reply_return = true;
1928
1929 const PrefService::PreferenceSet& prefs =
1930 profile_->GetPrefs()->preference_set();
1931 DictionaryValue* items = new DictionaryValue;
1932 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
1933 it != prefs.end(); ++it) {
1934 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
1935 }
1936 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1937 return_value->Set(L"prefs", items); // return_value owns items.
1938
1939 base::JSONWriter::Write(return_value.get(), false, &json_return);
1940 AutomationMsg_SendJSONRequest::WriteReplyParams(
1941 reply_message, json_return, reply_return);
1942 Send(reply_message);
1943}
1944
1945// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:581946void AutomationProvider::SetPrefs(Browser* browser,
1947 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:471948 IPC::Message* reply_message) {
1949 bool reply_return = true;
1950 std::string json_return = "{}";
1951 std::wstring path;
1952 Value* val;
1953 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
1954 PrefService* pref_service = profile_->GetPrefs();
1955 const PrefService::Preference* pref =
1956 pref_service->FindPreference(path.c_str());
1957 if (!pref) { // Not a registered pref.
[email protected]7060bb292010-06-24 00:52:491958 json_return = JSONErrorString("pref not registered.");
[email protected]24e2b102010-04-29 17:56:471959 reply_return = false;
1960 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
[email protected]7060bb292010-06-24 00:52:491961 json_return = JSONErrorString("pref is managed. cannot be changed.");
[email protected]24e2b102010-04-29 17:56:471962 reply_return = false;
1963 } else { // Set the pref.
1964 pref_service->Set(path.c_str(), *val);
1965 }
1966 } else {
[email protected]7060bb292010-06-24 00:52:491967 json_return = JSONErrorString("no pref path or value given.");
[email protected]24e2b102010-04-29 17:56:471968 reply_return = false;
1969 }
1970
1971 AutomationMsg_SendJSONRequest::WriteReplyParams(
1972 reply_message, json_return, reply_return);
1973 Send(reply_message);
1974}
1975
[email protected]53329582010-05-14 21:10:581976// Sample json input: { "command": "GetOmniboxInfo" }
1977// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
1978void AutomationProvider::GetOmniboxInfo(Browser* browser,
1979 DictionaryValue* args,
1980 IPC::Message* reply_message) {
1981 std::string json_return;
1982 bool reply_return = true;
1983 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1984
1985 LocationBar* loc_bar = browser->window()->GetLocationBar();
1986 AutocompleteEditView* edit_view = loc_bar->location_entry();
1987 AutocompleteEditModel* model = edit_view->model();
1988
1989 // Fill up matches.
1990 ListValue* matches = new ListValue;
1991 const AutocompleteResult& result = model->result();
1992 for (AutocompleteResult::const_iterator i = result.begin();
1993 i != result.end(); ++i) {
1994 const AutocompleteMatch& match = *i;
1995 DictionaryValue* item = new DictionaryValue; // owned by return_value
1996 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
1997 item->SetBoolean(L"starred", match.starred);
1998 item->SetString(L"destination_url", match.destination_url.spec());
1999 item->SetString(L"contents", match.contents);
2000 item->SetString(L"description", match.description);
2001 matches->Append(item);
2002 }
2003 return_value->Set(L"matches", matches);
2004
2005 // Fill up other properties.
2006 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2007 properties->SetBoolean(L"has_focus", model->has_focus());
2008 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2009 properties->SetString(L"keyword", model->keyword());
2010 properties->SetString(L"text", edit_view->GetText());
2011 return_value->Set(L"properties", properties);
2012
2013 base::JSONWriter::Write(return_value.get(), false, &json_return);
2014 AutomationMsg_SendJSONRequest::WriteReplyParams(
2015 reply_message, json_return, reply_return);
2016 Send(reply_message);
2017}
2018
2019// Sample json input: { "command": "SetOmniboxText",
2020// "text": "goog" }
2021void AutomationProvider::SetOmniboxText(Browser* browser,
2022 DictionaryValue* args,
2023 IPC::Message* reply_message) {
2024 std::string json_return = "{}";
2025 bool reply_return = true;
2026 std::wstring text;
2027
2028 if (!args->GetString(L"text", &text)) {
[email protected]7060bb292010-06-24 00:52:492029 json_return = JSONErrorString("text missing");
[email protected]53329582010-05-14 21:10:582030 reply_return = false;
2031 } else {
2032 browser->FocusLocationBar();
2033 LocationBar* loc_bar = browser->window()->GetLocationBar();
2034 AutocompleteEditView* edit_view = loc_bar->location_entry();
2035 edit_view->model()->OnSetFocus(false);
2036 edit_view->SetUserText(text);
2037 }
2038
2039 AutomationMsg_SendJSONRequest::WriteReplyParams(
2040 reply_message, json_return, reply_return);
2041 Send(reply_message);
2042}
2043
2044// Sample json input: { "command": "OmniboxMovePopupSelection",
2045// "count": 1 }
2046// Negative count implies up, positive implies down. Count values will be
2047// capped by the size of the popup list.
2048void AutomationProvider::OmniboxMovePopupSelection(
2049 Browser* browser,
2050 DictionaryValue* args,
2051 IPC::Message* reply_message) {
2052 std::string json_return = "{}";
2053 bool reply_return = true;
2054 int count;
2055
2056 if (!args->GetInteger(L"count", &count)) {
[email protected]7060bb292010-06-24 00:52:492057 json_return = JSONErrorString("count missing");
[email protected]53329582010-05-14 21:10:582058 reply_return = false;
2059 } else {
2060 LocationBar* loc_bar = browser->window()->GetLocationBar();
2061 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2062 model->OnUpOrDownKeyPressed(count);
2063 }
2064
2065 AutomationMsg_SendJSONRequest::WriteReplyParams(
2066 reply_message, json_return, reply_return);
2067 Send(reply_message);
2068}
2069
2070// Sample json input: { "command": "OmniboxAcceptInput" }
2071void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2072 DictionaryValue* args,
2073 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282074 NavigationController& controller =
2075 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122076 // Setup observer to wait until the selected item loads.
2077 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282078 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122079 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582080
2081 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582082}
2083
[email protected]a3cd5022010-06-16 18:25:292084// Sample json input: { "command": "GetInitialLoadTimes" }
2085// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2086void AutomationProvider::GetInitialLoadTimes(
2087 Browser*,
2088 DictionaryValue*,
2089 IPC::Message* reply_message) {
2090 scoped_ptr<DictionaryValue> return_value(
2091 initial_load_observer_->GetTimingInformation());
2092
2093 std::string json_return;
2094 base::JSONWriter::Write(return_value.get(), false, &json_return);
2095 AutomationMsg_SendJSONRequest::WriteReplyParams(
2096 reply_message, json_return, true);
2097 Send(reply_message);
2098}
2099
[email protected]f7d48012010-05-06 08:17:052100// Sample json input: { "command": "GetPluginsInfo" }
2101// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582102void AutomationProvider::GetPluginsInfo(Browser* browser,
2103 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052104 IPC::Message* reply_message) {
2105 std::string json_return;
2106 bool reply_return = true;
2107
2108 std::vector<WebPluginInfo> plugins;
2109 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2110 ListValue* items = new ListValue;
2111 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2112 it != plugins.end();
2113 ++it) {
2114 DictionaryValue* item = new DictionaryValue;
[email protected]c9d811372010-06-23 21:44:572115 item->SetStringFromUTF16(L"name", it->name);
[email protected]f7d48012010-05-06 08:17:052116 item->SetString(L"path", it->path.value());
[email protected]c9d811372010-06-23 21:44:572117 item->SetStringFromUTF16(L"version", it->version);
2118 item->SetStringFromUTF16(L"desc", it->desc);
[email protected]f7d48012010-05-06 08:17:052119 item->SetBoolean(L"enabled", it->enabled);
2120 // Add info about mime types.
2121 ListValue* mime_types = new ListValue();
2122 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2123 it->mime_types.begin();
2124 type_it != it->mime_types.end();
2125 ++type_it) {
2126 DictionaryValue* mime_type = new DictionaryValue();
2127 mime_type->SetString(L"mimeType", type_it->mime_type);
[email protected]c9d811372010-06-23 21:44:572128 mime_type->SetStringFromUTF16(L"description", type_it->description);
[email protected]f7d48012010-05-06 08:17:052129
2130 ListValue* file_extensions = new ListValue();
2131 for (std::vector<std::string>::const_iterator ext_it =
2132 type_it->file_extensions.begin();
2133 ext_it != type_it->file_extensions.end();
2134 ++ext_it) {
2135 file_extensions->Append(new StringValue(*ext_it));
2136 }
2137 mime_type->Set(L"fileExtensions", file_extensions);
2138
2139 mime_types->Append(mime_type);
2140 }
2141 item->Set(L"mimeTypes", mime_types);
2142 items->Append(item);
2143 }
2144 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2145 return_value->Set(L"plugins", items); // return_value owns items.
2146
2147 base::JSONWriter::Write(return_value.get(), false, &json_return);
2148 AutomationMsg_SendJSONRequest::WriteReplyParams(
2149 reply_message, json_return, reply_return);
2150 Send(reply_message);
2151}
2152
2153// Sample json input:
2154// { "command": "EnablePlugin",
2155// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582156void AutomationProvider::EnablePlugin(Browser* browser,
2157 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052158 IPC::Message* reply_message) {
2159 std::string json_return = "{}";
2160 bool reply_return = true;
2161 FilePath::StringType path;
2162 if (!args->GetString(L"path", &path)) {
[email protected]7060bb292010-06-24 00:52:492163 json_return = JSONErrorString("path not specified.");
[email protected]f7d48012010-05-06 08:17:052164 reply_return = false;
2165 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
2166 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2167 " for path %s.\"}", path.c_str());
2168 reply_return = false;
2169 }
2170
2171 AutomationMsg_SendJSONRequest::WriteReplyParams(
2172 reply_message, json_return, reply_return);
2173 Send(reply_message);
2174}
2175
2176// Sample json input:
2177// { "command": "DisablePlugin",
2178// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582179void AutomationProvider::DisablePlugin(Browser* browser,
2180 DictionaryValue* args,
2181 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052182 std::string json_return = "{}";
2183 bool reply_return = true;
2184 FilePath::StringType path;
2185 if (!args->GetString(L"path", &path)) {
[email protected]7060bb292010-06-24 00:52:492186 json_return = JSONErrorString("path not specified.");
[email protected]f7d48012010-05-06 08:17:052187 reply_return = false;
2188 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
2189 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2190 " for path %s.\"}", path.c_str());
2191 reply_return = false;
2192 }
2193
2194 AutomationMsg_SendJSONRequest::WriteReplyParams(
2195 reply_message, json_return, reply_return);
2196 Send(reply_message);
2197}
2198
[email protected]7060bb292010-06-24 00:52:492199// Sample json input:
2200// { "command": "SaveTabContents",
2201// "tab_index": 0,
2202// "filename": <a full pathname> }
2203// Sample json output:
2204// {}
2205void AutomationProvider::SaveTabContents(Browser* browser,
2206 DictionaryValue* args,
2207 IPC::Message* reply_message) {
2208 std::string json_return;
2209 int tab_index = 0;
2210 FilePath::StringType filename;
2211 FilePath::StringType parent_directory;
2212 TabContents* tab_contents = NULL;
2213
2214 if (!args->GetInteger(L"tab_index", &tab_index) ||
2215 !args->GetString(L"filename", &filename)) {
2216 json_return = JSONErrorString("tab_index or filename param missing");
2217 } else {
2218 tab_contents = browser->GetTabContentsAt(tab_index);
2219 if (!tab_contents) {
2220 json_return = JSONErrorString("no tab at tab_index");
2221 }
2222 }
2223 if (tab_contents) {
2224 // We're doing a SAVE_AS_ONLY_HTML so the the directory path isn't
2225 // used. Nevertheless, SavePackage requires it be valid. Sigh.
2226 parent_directory = FilePath(filename).DirName().value();
2227 if (!tab_contents->SavePage(FilePath(filename), FilePath(parent_directory),
2228 SavePackage::SAVE_AS_ONLY_HTML)) {
2229 json_return = JSONErrorString("Could not initiate SavePage");
2230 } else {
2231 // The observer will delete itself when done.
2232 new SavePackageNotificationObserver(tab_contents->save_package(),
2233 this, reply_message);
2234 return;
2235 }
2236 }
2237
[email protected]93364da2010-06-29 18:03:442238 // If we get here, error.
[email protected]7060bb292010-06-24 00:52:492239 DCHECK(!json_return.empty());
2240 AutomationMsg_SendJSONRequest::WriteReplyParams(
2241 reply_message, json_return, false);
2242 Send(reply_message);
2243}
2244
[email protected]93364da2010-06-29 18:03:442245/* static */
2246std::string AutomationProvider::JSONErrorString(const std::string& err) {
2247 std::string prefix = "{\"error\": \"";
2248 std::string no_quote_err;
2249 std::string suffix = "\"}";
2250
2251 base::JsonDoubleQuote(err, false, &no_quote_err);
2252 return prefix + no_quote_err + suffix;
2253}
2254
[email protected]53329582010-05-14 21:10:582255void AutomationProvider::SendJSONRequest(int handle,
2256 std::string json_request,
2257 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:042258 Browser* browser = NULL;
2259 std::string error_string;
2260 scoped_ptr<Value> values;
2261
2262 // Basic error checking.
2263 if (browser_tracker_->ContainsHandle(handle)) {
2264 browser = browser_tracker_->GetResource(handle);
2265 }
2266 if (!browser) {
2267 error_string = "no browser object";
2268 } else {
2269 base::JSONReader reader;
2270 std::string error;
[email protected]ba399672010-04-06 15:42:392271 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
[email protected]59a611242010-04-02 02:24:042272 if (!error.empty()) {
2273 error_string = error;
2274 }
2275 }
2276
2277 // Make sure input is a dict with a string command.
2278 std::string command;
2279 DictionaryValue* dict_value = NULL;
2280 if (error_string.empty()) {
2281 if (values->GetType() != Value::TYPE_DICTIONARY) {
2282 error_string = "not a dict or no command key in dict";
2283 } else {
2284 // Ownership remains with "values" variable.
2285 dict_value = static_cast<DictionaryValue*>(values.get());
2286 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
2287 error_string = "no command key in dict or not a string command";
2288 }
2289 }
2290 }
2291
[email protected]24e2b102010-04-29 17:56:472292 // Map json commands to their handlers.
2293 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:052294 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
2295 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
2296 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
2297
[email protected]a9ff2c02010-05-13 17:33:052298 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
2299
[email protected]24e2b102010-04-29 17:56:472300 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:292301 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:052302
[email protected]53329582010-05-14 21:10:582303 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
2304 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
2305 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
2306 handler_map["OmniboxMovePopupSelection"] =
2307 &AutomationProvider::OmniboxMovePopupSelection;
2308
[email protected]24e2b102010-04-29 17:56:472309 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
2310 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:052311
[email protected]ef413ca2010-05-25 21:09:142312 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
2313
[email protected]f7d48012010-05-06 08:17:052314 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:472315 handler_map["WaitForAllDownloadsToComplete"] =
2316 &AutomationProvider::WaitForDownloadsToComplete;
2317
[email protected]a3cd5022010-06-16 18:25:292318 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
2319
[email protected]7060bb292010-06-24 00:52:492320 handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents;
2321
[email protected]59a611242010-04-02 02:24:042322 if (error_string.empty()) {
[email protected]24e2b102010-04-29 17:56:472323 if (handler_map.find(std::string(command)) != handler_map.end()) {
[email protected]53329582010-05-14 21:10:582324 (this->*handler_map[command])(browser, dict_value, reply_message);
[email protected]59a611242010-04-02 02:24:042325 return;
2326 } else {
[email protected]24e2b102010-04-29 17:56:472327 error_string = "Unknown command. Options: ";
2328 for (std::map<std::string, JsonHandler>::const_iterator it =
2329 handler_map.begin(); it != handler_map.end(); ++it) {
2330 error_string += it->first + ", ";
2331 }
[email protected]59a611242010-04-02 02:24:042332 }
2333 }
2334
2335 // If we hit an error, return info.
[email protected]24e2b102010-04-29 17:56:472336 // Return a dict of {"error", "descriptive_string_for_error"}.
[email protected]59a611242010-04-02 02:24:042337 // Else return an empty dict.
2338 std::string json_string;
2339 bool success = true;
2340 if (!error_string.empty()) {
2341 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
2342 dict->SetString(L"error", error_string);
2343 base::JSONWriter::Write(dict.get(), false, &json_string);
2344 success = false;
2345 } else {
2346 json_string = "{}";
2347 }
2348 AutomationMsg_SendJSONRequest::WriteReplyParams(
2349 reply_message, json_string, success);
2350 Send(reply_message);
2351}
2352
initial.commit09911bf2008-07-26 23:55:292353void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562354 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342355 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2356 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562357 DCHECK(reply_message_ == NULL);
2358 reply_message_ = reply_message;
2359
[email protected]d9f9b792009-06-24 13:17:122360 DevToolsManager::GetInstance()->InspectElement(
2361 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292362 } else {
[email protected]71f65dd2009-02-11 19:14:562363 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2364 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292365 }
2366}
2367
2368void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092369 if (reply_message_) {
2370 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2371 num_resources);
2372 Send(reply_message_);
2373 reply_message_ = NULL;
2374 }
initial.commit09911bf2008-07-26 23:55:292375}
2376
[email protected]a7eee32f2009-05-22 18:08:172377class SetProxyConfigTask : public Task {
2378 public:
[email protected]be180c802009-10-23 06:33:312379 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
2380 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:252381 : request_context_getter_(request_context_getter),
2382 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:172383 virtual void Run() {
2384 // First, deserialize the JSON string. If this fails, log and bail.
2385 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:392386 std::string error_msg;
2387 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:172388 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2389 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:392390 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:172391 return;
2392 }
2393
2394 scoped_ptr<DictionaryValue> dict(
2395 static_cast<DictionaryValue*>(root.release()));
2396 // Now put together a proxy configuration from the deserialized string.
2397 net::ProxyConfig pc;
2398 PopulateProxyConfig(*dict.get(), &pc);
2399
[email protected]be180c802009-10-23 06:33:312400 net::ProxyService* proxy_service =
2401 request_context_getter_->GetURLRequestContext()->proxy_service();
2402 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:172403 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2404 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:312405 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:172406 }
2407
2408 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2409 DCHECK(pc);
2410 bool no_proxy = false;
2411 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2412 // Make no changes to the ProxyConfig.
2413 return;
2414 }
2415 bool auto_config;
2416 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:482417 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:172418 }
2419 std::string pac_url;
2420 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:482421 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:172422 }
2423 std::string proxy_bypass_list;
2424 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:482425 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:172426 }
2427 std::string proxy_server;
2428 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:482429 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:172430 }
2431 }
2432
2433 private:
[email protected]be180c802009-10-23 06:33:312434 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:172435 std::string proxy_config_;
2436};
2437
2438
2439void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:312440 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
2441 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:172442 FilePath user_data_dir;
2443 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2444 ProfileManager* profile_manager = g_browser_process->profile_manager();
2445 DCHECK(profile_manager);
2446 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2447 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:312448 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:172449 }
[email protected]be180c802009-10-23 06:33:312450 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:172451
[email protected]fae20792009-10-28 20:31:582452 ChromeThread::PostTask(
2453 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:312454 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:172455}
2456
[email protected]4f3dc372009-02-24 00:10:292457void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:332458 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:292459 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292460 if (tab_tracker_->ContainsHandle(handle)) {
2461 NavigationController* tab = tab_tracker_->GetResource(handle);
2462 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2463 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:332464 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:292465 }
initial.commit09911bf2008-07-26 23:55:292466}
2467
[email protected]6a5670d22009-10-27 16:21:342468void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:142469 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:532470 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
2471 reply_message);
2472}
2473
2474void AutomationProvider::OpenNewBrowserWindowOfType(
2475 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:142476 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292477 // We may have no current browser windows open so don't rely on
2478 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:532479 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
2480 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:052481 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152482 if (show)
[email protected]15952e462008-11-14 00:29:052483 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292484}
2485
[email protected]71f65dd2009-02-11 19:14:562486void AutomationProvider::GetWindowForBrowser(int browser_handle,
2487 bool* success,
2488 int* handle) {
2489 *success = false;
2490 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292491
2492 if (browser_tracker_->ContainsHandle(browser_handle)) {
2493 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202494 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292495 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202496 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562497 *success = true;
initial.commit09911bf2008-07-26 23:55:292498 }
initial.commit09911bf2008-07-26 23:55:292499}
2500
2501void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562502 int browser_handle,
2503 bool* success,
2504 int* autocomplete_edit_handle) {
2505 *success = false;
2506 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292507
2508 if (browser_tracker_->ContainsHandle(browser_handle)) {
2509 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062510 LocationBar* loc_bar = browser->window()->GetLocationBar();
2511 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292512 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562513 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2514 *success = true;
initial.commit09911bf2008-07-26 23:55:292515 }
initial.commit09911bf2008-07-26 23:55:292516}
initial.commit09911bf2008-07-26 23:55:292517
[email protected]71f65dd2009-02-11 19:14:562518void AutomationProvider::ShowInterstitialPage(int tab_handle,
2519 const std::string& html_text,
2520 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292521 if (tab_tracker_->ContainsHandle(tab_handle)) {
2522 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112523 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402524
[email protected]7dad3d5f2010-03-04 00:27:012525 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:402526 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342527 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402528 GURL("about:interstitial"),
2529 html_text);
2530 interstitial->Show();
2531 return;
initial.commit09911bf2008-07-26 23:55:292532 }
[email protected]71f65dd2009-02-11 19:14:562533
[email protected]457f5cf2009-08-18 16:37:522534 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
2535 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562536 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292537}
2538
[email protected]71f65dd2009-02-11 19:14:562539void AutomationProvider::HideInterstitialPage(int tab_handle,
2540 bool* success) {
2541 *success = false;
[email protected]57c6a652009-05-04 07:58:342542 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2543 if (tab_contents && tab_contents->interstitial_page()) {
2544 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562545 *success = true;
initial.commit09911bf2008-07-26 23:55:292546 }
initial.commit09911bf2008-07-26 23:55:292547}
2548
[email protected]71f65dd2009-02-11 19:14:562549void AutomationProvider::CloseTab(int tab_handle,
2550 bool wait_until_closed,
2551 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292552 if (tab_tracker_->ContainsHandle(tab_handle)) {
2553 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2554 int index;
2555 Browser* browser = Browser::GetBrowserForController(controller, &index);
2556 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142557 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112558 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452559 return;
initial.commit09911bf2008-07-26 23:55:292560 }
[email protected]de246f52009-02-25 18:25:452561
2562 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:552563 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292564}
2565
[email protected]71f65dd2009-02-11 19:14:562566void AutomationProvider::CloseBrowser(int browser_handle,
2567 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292568 if (browser_tracker_->ContainsHandle(browser_handle)) {
2569 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562570 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562571 reply_message);
[email protected]f3e99e32008-07-30 04:48:392572 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292573 } else {
2574 NOTREACHED();
2575 }
2576}
2577
[email protected]71f65dd2009-02-11 19:14:562578void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2579 if (browser_tracker_->ContainsHandle(browser_handle)) {
2580 Browser* browser = browser_tracker_->GetResource(browser_handle);
2581 browser->window()->Close();
2582 } else {
2583 NOTREACHED();
2584 }
2585}
2586
[email protected]71f65dd2009-02-11 19:14:562587void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2588 IPC::Message* reply_message) {
2589 if (tab_tracker_->ContainsHandle(tab_handle)) {
2590 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2591 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:142592 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:562593 }
2594}
2595
[email protected]71f65dd2009-02-11 19:14:562596void AutomationProvider::GetSecurityState(int handle, bool* success,
2597 SecurityStyle* security_style,
2598 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:482599 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:292600 if (tab_tracker_->ContainsHandle(handle)) {
2601 NavigationController* tab = tab_tracker_->GetResource(handle);
2602 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562603 *success = true;
2604 *security_style = entry->ssl().security_style();
2605 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:482606 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292607 } else {
[email protected]71f65dd2009-02-11 19:14:562608 *success = false;
2609 *security_style = SECURITY_STYLE_UNKNOWN;
2610 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:482611 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292612 }
2613}
2614
[email protected]71f65dd2009-02-11 19:14:562615void AutomationProvider::GetPageType(int handle, bool* success,
2616 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:292617 if (tab_tracker_->ContainsHandle(handle)) {
2618 NavigationController* tab = tab_tracker_->GetResource(handle);
2619 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562620 *page_type = entry->page_type();
2621 *success = true;
initial.commit09911bf2008-07-26 23:55:292622 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:342623 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:562624 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:342625 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:562626 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292627 } else {
[email protected]71f65dd2009-02-11 19:14:562628 *success = false;
2629 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292630 }
2631}
2632
[email protected]84abba62009-10-07 17:01:442633void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
2634 int* duration_ms) {
2635 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
2636 event_name);
2637}
2638
[email protected]71f65dd2009-02-11 19:14:562639void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
2640 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292641 if (tab_tracker_->ContainsHandle(handle)) {
2642 NavigationController* tab = tab_tracker_->GetResource(handle);
2643 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072644 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:402645 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:472646 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:342647 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:292648 if (ssl_blocking_page) {
2649 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:012650 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:562651 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:292652 return;
2653 }
2654 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:522655 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2656 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:562657 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292658 return;
2659 }
2660 }
2661 }
2662 // We failed.
[email protected]457f5cf2009-08-18 16:37:522663 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2664 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562665 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292666}
2667
[email protected]71f65dd2009-02-11 19:14:562668void AutomationProvider::BringBrowserToFront(int browser_handle,
2669 bool* success) {
initial.commit09911bf2008-07-26 23:55:292670 if (browser_tracker_->ContainsHandle(browser_handle)) {
2671 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:062672 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:562673 *success = true;
initial.commit09911bf2008-07-26 23:55:292674 } else {
[email protected]71f65dd2009-02-11 19:14:562675 *success = false;
initial.commit09911bf2008-07-26 23:55:292676 }
2677}
2678
[email protected]71f65dd2009-02-11 19:14:562679void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
2680 int message_num,
2681 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:292682 if (browser_tracker_->ContainsHandle(browser_handle)) {
2683 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562684 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:142685 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:292686 } else {
[email protected]71f65dd2009-02-11 19:14:562687 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:292688 }
2689}
2690
[email protected]71f65dd2009-02-11 19:14:562691void AutomationProvider::PrintNow(int tab_handle,
2692 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:572693 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:342694 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2695 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:292696 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572697 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:142698 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:342699 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:572700 return;
initial.commit09911bf2008-07-26 23:55:292701 }
[email protected]71f65dd2009-02-11 19:14:562702 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
2703 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292704}
[email protected]d301c952009-07-13 15:02:412705
[email protected]71f65dd2009-02-11 19:14:562706void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:112707 const FilePath& file_name,
2708 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:562709 int type,
2710 bool* success) {
initial.commit09911bf2008-07-26 23:55:292711 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562712 *success = false;
initial.commit09911bf2008-07-26 23:55:292713 return;
2714 }
2715
2716 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2717 Browser* browser = FindAndActivateTab(nav);
2718 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142719 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562720 *success = false;
initial.commit09911bf2008-07-26 23:55:292721 return;
2722 }
2723
initial.commit09911bf2008-07-26 23:55:292724 SavePackage::SavePackageType save_type =
2725 static_cast<SavePackage::SavePackageType>(type);
2726 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2727 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:342728 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292729
[email protected]71f65dd2009-02-11 19:14:562730 *success = true;
initial.commit09911bf2008-07-26 23:55:292731}
2732
[email protected]71f65dd2009-02-11 19:14:562733void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2734 bool* success,
2735 std::wstring* text) {
2736 *success = false;
initial.commit09911bf2008-07-26 23:55:292737 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562738 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522739 GetText();
[email protected]71f65dd2009-02-11 19:14:562740 *success = true;
initial.commit09911bf2008-07-26 23:55:292741 }
initial.commit09911bf2008-07-26 23:55:292742}
2743
[email protected]71f65dd2009-02-11 19:14:562744void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2745 const std::wstring& text,
2746 bool* success) {
2747 *success = false;
initial.commit09911bf2008-07-26 23:55:292748 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522749 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2750 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562751 *success = true;
initial.commit09911bf2008-07-26 23:55:292752 }
initial.commit09911bf2008-07-26 23:55:292753}
2754
2755void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562756 int autocomplete_edit_handle,
2757 bool* success,
2758 std::vector<AutocompleteMatchData>* matches) {
2759 *success = false;
initial.commit09911bf2008-07-26 23:55:292760 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272761 const AutocompleteResult& result = autocomplete_edit_tracker_->
2762 GetResource(autocomplete_edit_handle)->model()->result();
2763 for (AutocompleteResult::const_iterator i = result.begin();
2764 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562765 matches->push_back(AutocompleteMatchData(*i));
2766 *success = true;
initial.commit09911bf2008-07-26 23:55:292767 }
initial.commit09911bf2008-07-26 23:55:292768}
2769
2770void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562771 int autocomplete_edit_handle,
2772 bool* success,
2773 bool* query_in_progress) {
2774 *success = false;
2775 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292776 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562777 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522778 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562779 *success = true;
initial.commit09911bf2008-07-26 23:55:292780 }
initial.commit09911bf2008-07-26 23:55:292781}
2782
[email protected]63514af2010-03-30 17:17:232783#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:192784
[email protected]5ae5bed2009-08-21 18:52:442785#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:392786
[email protected]57c6a652009-05-04 07:58:342787TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572788 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572789 if (tab_tracker_->ContainsHandle(handle)) {
2790 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342791 if (tab)
2792 *tab = nav_controller;
2793 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572794 }
[email protected]57c6a652009-05-04 07:58:342795 return NULL;
[email protected]20e93d12008-08-28 16:31:572796}
2797
initial.commit09911bf2008-07-26 23:55:292798TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2799 : AutomationProvider(profile) {
2800 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142801 registrar_.Add(this, NotificationType::SESSION_END,
2802 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292803}
2804
2805TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292806 BrowserList::RemoveObserver(this);
2807}
2808
2809void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:142810 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:292811 AutomationProvider::OnChannelError();
2812}
2813
2814void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2815 // For backwards compatibility with the testing automation interface, we
2816 // want the automation provider (and hence the process) to go away when the
2817 // last browser goes away.
2818 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292819 // If you change this, update Observer for NotificationType::SESSION_END
2820 // below.
[email protected]295039bd2008-08-15 04:32:572821 MessageLoop::current()->PostTask(FROM_HERE,
2822 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292823 }
2824}
2825
2826void TestingAutomationProvider::Observe(NotificationType type,
2827 const NotificationSource& source,
2828 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562829 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292830 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2831 // before the task runs resulting in this object not being deleted. This
2832 // Release balance out the Release scheduled by OnBrowserRemoving.
2833 Release();
2834}
[email protected]295039bd2008-08-15 04:32:572835
2836void TestingAutomationProvider::OnRemoveProvider() {
2837 AutomationProviderList::GetInstance()->RemoveProvider(this);
2838}
[email protected]8a3422c92008-09-24 17:42:422839
[email protected]816633a2009-11-11 21:48:182840void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:562841 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422842 if (tab_tracker_->ContainsHandle(handle)) {
2843 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342844 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112845 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422846 }
[email protected]8a3422c92008-09-24 17:42:422847}
2848
[email protected]816633a2009-11-11 21:48:182849void AutomationProvider::ClickInfoBarAccept(int handle,
2850 int info_bar_index,
2851 bool wait_for_navigation,
2852 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422853 bool success = false;
2854 if (tab_tracker_->ContainsHandle(handle)) {
2855 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2856 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112857 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422858 if (info_bar_index >= 0 && info_bar_index < count) {
2859 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:012860 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:422861 }
[email protected]eb9ba192008-12-02 02:41:342862 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112863 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342864 info_bar_index);
2865 if (delegate->AsConfirmInfoBarDelegate())
2866 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422867 success = true;
2868 }
2869 }
[email protected]4f3dc372009-02-24 00:10:292870 }
[email protected]58f622a62009-10-04 01:17:552871
2872 // This "!wait_for_navigation || !success condition" logic looks suspicious.
2873 // It will send a failure message when success is true but
2874 // |wait_for_navigation| is false.
2875 // TODO(phajdan.jr): investgate whether the reply param (currently
2876 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:422877 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:182878 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:522879 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:422880}
2881
[email protected]71f65dd2009-02-11 19:14:562882void AutomationProvider::GetLastNavigationTime(int handle,
2883 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422884 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562885 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422886}
2887
[email protected]71f65dd2009-02-11 19:14:562888void AutomationProvider::WaitForNavigation(int handle,
2889 int64 last_navigation_time,
2890 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:252891 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:422892 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:252893
[email protected]8a3422c92008-09-24 17:42:422894 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562895 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:522896 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
2897 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:552898 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:292899 return;
[email protected]8a3422c92008-09-24 17:42:422900 }
2901
[email protected]7dad3d5f2010-03-04 00:27:012902 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:422903}
2904
[email protected]71f65dd2009-02-11 19:14:562905void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162906 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562907 int value,
2908 bool* success) {
2909 *success = false;
[email protected]8a3422c92008-09-24 17:42:422910 if (browser_tracker_->ContainsHandle(handle)) {
2911 Browser* browser = browser_tracker_->GetResource(handle);
2912 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562913 *success = true;
[email protected]8a3422c92008-09-24 17:42:422914 }
[email protected]8a3422c92008-09-24 17:42:422915}
[email protected]97fa6ce32008-12-19 01:48:162916
[email protected]71f65dd2009-02-11 19:14:562917void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162918 const std::wstring& name,
[email protected]ddd231e2010-06-29 20:35:192919 const std::string& value,
[email protected]71f65dd2009-02-11 19:14:562920 bool* success) {
2921 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162922 if (browser_tracker_->ContainsHandle(handle)) {
2923 Browser* browser = browser_tracker_->GetResource(handle);
2924 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562925 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162926 }
[email protected]97fa6ce32008-12-19 01:48:162927}
2928
[email protected]71f65dd2009-02-11 19:14:562929void AutomationProvider::GetBooleanPreference(int handle,
2930 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:362931 bool* success,
2932 bool* value) {
[email protected]71f65dd2009-02-11 19:14:562933 *success = false;
2934 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162935 if (browser_tracker_->ContainsHandle(handle)) {
2936 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562937 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2938 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162939 }
[email protected]97fa6ce32008-12-19 01:48:162940}
2941
[email protected]71f65dd2009-02-11 19:14:562942void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162943 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562944 bool value,
2945 bool* success) {
2946 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162947 if (browser_tracker_->ContainsHandle(handle)) {
2948 Browser* browser = browser_tracker_->GetResource(handle);
2949 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562950 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162951 }
[email protected]97fa6ce32008-12-19 01:48:162952}
2953
2954// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562955void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:402956 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162957 if (tab_tracker_->ContainsHandle(tab_handle)) {
2958 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2959 Browser* browser = FindAndActivateTab(nav);
2960 DCHECK(browser);
2961
[email protected]57c6a652009-05-04 07:58:342962 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2963 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162964 }
[email protected]97fa6ce32008-12-19 01:48:162965}
2966
[email protected]b8f48d12009-11-09 20:14:362967// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562968void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402969 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562970 bool* success) {
2971 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162972 if (tab_tracker_->ContainsHandle(tab_handle)) {
2973 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:192974 if (!nav)
2975 return;
[email protected]97fa6ce32008-12-19 01:48:162976 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:162977
[email protected]2f2afba2010-04-01 01:53:192978 // If the browser has UI, simulate what a user would do.
2979 // Activate the tab and then click the encoding menu.
2980 if (browser &&
2981 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162982 int selected_encoding_id =
2983 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2984 if (selected_encoding_id) {
2985 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562986 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162987 }
[email protected]2f2afba2010-04-01 01:53:192988 } else {
2989 // There is no UI, Chrome probably runs as Chrome-Frame mode.
2990 // Try to get TabContents and call its override_encoding method.
2991 TabContents* contents = nav->tab_contents();
2992 if (!contents)
2993 return;
2994 const std::string selected_encoding =
2995 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
2996 if (selected_encoding.empty())
2997 return;
2998 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:162999 }
3000 }
[email protected]97fa6ce32008-12-19 01:48:163001}
[email protected]5bcdb312009-01-07 21:43:203002
[email protected]4d434a1a2009-02-11 21:06:573003void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:203004 SavePackage::SetShouldPromptUser(should_prompt);
3005}
[email protected]87eab222009-03-13 00:47:453006
[email protected]66ba4932009-06-04 19:22:133007void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
3008 *count = -1; // -1 is the error code
3009 if (tab_tracker_->ContainsHandle(handle)) {
3010 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3011 TabContents* tab_contents = nav_controller->tab_contents();
3012 if (tab_contents) {
3013 BlockedPopupContainer* container =
3014 tab_contents->blocked_popup_container();
3015 if (container) {
3016 *count = static_cast<int>(container->GetBlockedPopupCount());
3017 } else {
3018 // If we don't have a container, we don't have any blocked popups to
3019 // contain!
3020 *count = 0;
3021 }
3022 }
3023 }
3024}
[email protected]f7a68432009-07-29 23:18:193025
3026void AutomationProvider::SelectAll(int tab_handle) {
3027 RenderViewHost* view = GetViewForTab(tab_handle);
3028 if (!view) {
3029 NOTREACHED();
3030 return;
3031 }
3032
3033 view->SelectAll();
3034}
3035
3036void AutomationProvider::Cut(int tab_handle) {
3037 RenderViewHost* view = GetViewForTab(tab_handle);
3038 if (!view) {
3039 NOTREACHED();
3040 return;
3041 }
3042
3043 view->Cut();
3044}
3045
3046void AutomationProvider::Copy(int tab_handle) {
3047 RenderViewHost* view = GetViewForTab(tab_handle);
3048 if (!view) {
3049 NOTREACHED();
3050 return;
3051 }
3052
3053 view->Copy();
3054}
3055
3056void AutomationProvider::Paste(int tab_handle) {
3057 RenderViewHost* view = GetViewForTab(tab_handle);
3058 if (!view) {
3059 NOTREACHED();
3060 return;
3061 }
3062
3063 view->Paste();
3064}
3065
3066void AutomationProvider::ReloadAsync(int tab_handle) {
3067 if (tab_tracker_->ContainsHandle(tab_handle)) {
3068 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3069 if (!tab) {
3070 NOTREACHED();
3071 return;
3072 }
3073
[email protected]106a0812010-03-18 00:15:123074 const bool check_for_repost = true;
3075 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193076 }
3077}
3078
3079void AutomationProvider::StopAsync(int tab_handle) {
3080 RenderViewHost* view = GetViewForTab(tab_handle);
3081 if (!view) {
[email protected]8b2b3312009-09-14 18:38:363082 // We tolerate StopAsync being called even before a view has been created.
3083 // So just log a warning instead of a NOTREACHED().
3084 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:193085 return;
3086 }
3087
3088 view->Stop();
3089}
3090
[email protected]1bb5f892009-10-06 01:44:573091void AutomationProvider::OnSetPageFontSize(int tab_handle,
3092 int font_size) {
3093 AutomationPageFontSize automation_font_size =
3094 static_cast<AutomationPageFontSize>(font_size);
3095
3096 if (automation_font_size < SMALLEST_FONT ||
3097 automation_font_size > LARGEST_FONT) {
3098 DLOG(ERROR) << "Invalid font size specified : "
3099 << font_size;
3100 return;
3101 }
3102
3103 if (tab_tracker_->ContainsHandle(tab_handle)) {
3104 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3105 DCHECK(tab != NULL);
3106 if (tab && tab->tab_contents()) {
3107 DCHECK(tab->tab_contents()->profile() != NULL);
3108 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
3109 prefs::kWebKitDefaultFontSize, font_size);
3110 }
3111 }
3112}
3113
[email protected]bc73b4e52010-03-26 04:16:203114void AutomationProvider::RemoveBrowsingData(int remove_mask) {
3115 BrowsingDataRemover* remover;
3116 remover = new BrowsingDataRemover(profile(),
3117 BrowsingDataRemover::EVERYTHING, // All time periods.
3118 base::Time());
3119 remover->Remove(remove_mask);
3120 // BrowsingDataRemover deletes itself.
3121}
[email protected]1bb5f892009-10-06 01:44:573122
[email protected]2949e90d2009-08-21 15:32:523123void AutomationProvider::WaitForBrowserWindowCountToBecome(
3124 int target_count, IPC::Message* reply_message) {
3125 if (static_cast<int>(BrowserList::size()) == target_count) {
3126 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
3127 reply_message, true);
3128 Send(reply_message);
3129 return;
3130 }
3131
3132 // Set up an observer (it will delete itself).
3133 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
3134}
3135
3136void AutomationProvider::WaitForAppModalDialogToBeShown(
3137 IPC::Message* reply_message) {
3138 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
3139 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
3140 reply_message, true);
3141 Send(reply_message);
3142 return;
3143 }
3144
3145 // Set up an observer (it will delete itself).
3146 new AppModalDialogShownObserver(this, reply_message);
3147}
3148
[email protected]1126a1d32009-08-26 15:39:263149void AutomationProvider::GoBackBlockUntilNavigationsComplete(
3150 int handle, int number_of_navigations, IPC::Message* reply_message) {
3151 if (tab_tracker_->ContainsHandle(handle)) {
3152 NavigationController* tab = tab_tracker_->GetResource(handle);
3153 Browser* browser = FindAndActivateTab(tab);
3154 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:013155 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3156 false);
[email protected]1126a1d32009-08-26 15:39:263157 browser->GoBack(CURRENT_TAB);
3158 return;
3159 }
3160 }
3161
3162 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
3163 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3164 Send(reply_message);
3165}
3166
3167void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
3168 int handle, int number_of_navigations, IPC::Message* reply_message) {
3169 if (tab_tracker_->ContainsHandle(handle)) {
3170 NavigationController* tab = tab_tracker_->GetResource(handle);
3171 Browser* browser = FindAndActivateTab(tab);
3172 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:013173 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3174 false);
[email protected]1126a1d32009-08-26 15:39:263175 browser->GoForward(CURRENT_TAB);
3176 return;
3177 }
3178 }
3179
3180 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
3181 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3182 Send(reply_message);
3183}
3184
[email protected]f7a68432009-07-29 23:18:193185RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
3186 if (tab_tracker_->ContainsHandle(tab_handle)) {
3187 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3188 if (!tab) {
3189 NOTREACHED();
3190 return NULL;
3191 }
3192
3193 TabContents* tab_contents = tab->tab_contents();
3194 if (!tab_contents) {
3195 NOTREACHED();
3196 return NULL;
3197 }
3198
3199 RenderViewHost* view_host = tab_contents->render_view_host();
3200 return view_host;
3201 }
3202
3203 return NULL;
3204}
[email protected]675595f2009-08-26 22:32:043205
3206void AutomationProvider::GetBrowserForWindow(int window_handle,
3207 bool* success,
3208 int* browser_handle) {
3209 *success = false;
3210 *browser_handle = 0;
3211
3212 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
3213 if (!window)
3214 return;
3215
3216 BrowserList::const_iterator iter = BrowserList::begin();
3217 for (;iter != BrowserList::end(); ++iter) {
3218 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
3219 if (window == this_window) {
3220 // Add() returns the existing handle for the resource if any.
3221 *browser_handle = browser_tracker_->Add(*iter);
3222 *success = true;
3223 return;
3224 }
3225 }
3226}
[email protected]d11c8e92009-10-20 23:26:403227
3228void AutomationProvider::InstallExtension(const FilePath& crx_path,
3229 IPC::Message* reply_message) {
3230 ExtensionsService* service = profile_->GetExtensionsService();
3231 if (service) {
3232 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193233 new ExtensionInstallNotificationObserver(this,
3234 AutomationMsg_InstallExtension::ID,
3235 reply_message);
[email protected]d11c8e92009-10-20 23:26:403236
3237 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:163238 scoped_refptr<CrxInstaller> installer(
3239 new CrxInstaller(install_dir,
3240 service,
3241 NULL)); // silent install, no UI
3242 installer->set_allow_privilege_increase(true);
3243 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:403244 } else {
3245 AutomationMsg_InstallExtension::WriteReplyParams(
3246 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3247 Send(reply_message);
3248 }
3249}
3250
3251void AutomationProvider::LoadExpandedExtension(
3252 const FilePath& extension_dir,
3253 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:383254 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:403255 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193256 new ExtensionInstallNotificationObserver(
3257 this,
3258 AutomationMsg_LoadExpandedExtension::ID,
3259 reply_message);
[email protected]d11c8e92009-10-20 23:26:403260
3261 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:403262 } else {
3263 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
3264 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3265 Send(reply_message);
3266 }
3267}
[email protected]673fd2c02010-02-04 23:10:003268
[email protected]a1e62d12010-03-16 02:18:433269void AutomationProvider::GetEnabledExtensions(
3270 std::vector<FilePath>* result) {
3271 ExtensionsService* service = profile_->GetExtensionsService();
3272 DCHECK(service);
3273 if (service->extensions_enabled()) {
3274 const ExtensionList* extensions = service->extensions();
3275 DCHECK(extensions);
3276 for (size_t i = 0; i < extensions->size(); ++i) {
3277 Extension* extension = (*extensions)[i];
3278 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:123279 if (extension->location() == Extension::INTERNAL ||
3280 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:073281 result->push_back(extension->path());
3282 }
[email protected]a1e62d12010-03-16 02:18:433283 }
3284 }
3285}
3286
[email protected]790788ac2010-04-06 17:52:193287void AutomationProvider::WaitForExtensionTestResult(
3288 IPC::Message* reply_message) {
3289 DCHECK(reply_message_ == NULL);
3290 reply_message_ = reply_message;
3291 // Call MaybeSendResult, because the result might have come in before
3292 // we were waiting on it.
3293 extension_test_result_observer_->MaybeSendResult();
3294}
3295
3296void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:093297 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:193298 ExtensionsService* service = profile_->GetExtensionsService();
3299 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3300 if (service && manager) {
3301 // The observer will delete itself when done.
3302 new ExtensionReadyNotificationObserver(
3303 manager,
3304 this,
3305 AutomationMsg_InstallExtensionAndGetHandle::ID,
3306 reply_message);
3307
[email protected]d7e5525d2010-04-20 14:37:093308 ExtensionInstallUI* client =
3309 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:193310 scoped_refptr<CrxInstaller> installer(
3311 new CrxInstaller(service->install_directory(),
3312 service,
[email protected]d7e5525d2010-04-20 14:37:093313 client));
[email protected]790788ac2010-04-06 17:52:193314 installer->set_allow_privilege_increase(true);
3315 installer->InstallCrx(crx_path);
3316 } else {
3317 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
3318 reply_message, 0);
3319 Send(reply_message);
3320 }
3321}
3322
3323void AutomationProvider::UninstallExtension(int extension_handle,
3324 bool* success) {
3325 *success = false;
3326 Extension* extension = GetExtension(extension_handle);
3327 ExtensionsService* service = profile_->GetExtensionsService();
3328 if (extension && service) {
3329 ExtensionUnloadNotificationObserver observer;
3330 service->UninstallExtension(extension->id(), false);
3331 // The extension unload notification should have been sent synchronously
3332 // with the uninstall. Just to be safe, check that it was received.
3333 *success = observer.did_receive_unload_notification();
3334 }
3335}
3336
3337void AutomationProvider::EnableExtension(int extension_handle,
3338 IPC::Message* reply_message) {
3339 Extension* extension = GetDisabledExtension(extension_handle);
3340 ExtensionsService* service = profile_->GetExtensionsService();
3341 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3342 // Only enable if this extension is disabled.
3343 if (extension && service && manager) {
3344 // The observer will delete itself when done.
3345 new ExtensionReadyNotificationObserver(
3346 manager,
3347 this,
3348 AutomationMsg_EnableExtension::ID,
3349 reply_message);
3350 service->EnableExtension(extension->id());
3351 } else {
3352 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
3353 Send(reply_message);
3354 }
3355}
3356
3357void AutomationProvider::DisableExtension(int extension_handle,
3358 bool* success) {
3359 *success = false;
3360 Extension* extension = GetEnabledExtension(extension_handle);
3361 ExtensionsService* service = profile_->GetExtensionsService();
3362 if (extension && service) {
3363 ExtensionUnloadNotificationObserver observer;
3364 service->DisableExtension(extension->id());
3365 // The extension unload notification should have been sent synchronously
3366 // with the disable. Just to be safe, check that it was received.
3367 *success = observer.did_receive_unload_notification();
3368 }
3369}
3370
3371void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
3372 int extension_handle, int browser_handle,
3373 IPC::Message* reply_message) {
3374 bool success = false;
3375 Extension* extension = GetEnabledExtension(extension_handle);
3376 ExtensionsService* service = profile_->GetExtensionsService();
3377 ExtensionMessageService* message_service =
3378 profile_->GetExtensionMessageService();
3379 Browser* browser = browser_tracker_->GetResource(browser_handle);
3380 if (extension && service && message_service && browser) {
3381 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
3382 if (extension->page_action()) {
3383 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
3384 browser->profile(), extension->id(), "action", tab_id, "", 1);
3385 success = true;
3386 } else if (extension->browser_action()) {
3387 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
3388 browser->profile(), extension->id(), browser);
3389 success = true;
3390 }
3391 }
3392 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3393 reply_message, success);
3394 Send(reply_message);
3395}
3396
3397void AutomationProvider::MoveExtensionBrowserAction(
3398 int extension_handle, int index, bool* success) {
3399 *success = false;
3400 Extension* extension = GetEnabledExtension(extension_handle);
3401 ExtensionsService* service = profile_->GetExtensionsService();
3402 if (extension && service) {
3403 ExtensionToolbarModel* toolbar = service->toolbar_model();
3404 if (toolbar) {
3405 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3406 toolbar->MoveBrowserAction(extension, index);
3407 *success = true;
3408 } else {
3409 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3410 }
3411 }
3412 }
3413}
3414
3415void AutomationProvider::GetExtensionProperty(
3416 int extension_handle,
3417 AutomationMsg_ExtensionProperty type,
3418 bool* success,
3419 std::string* value) {
3420 *success = false;
3421 Extension* extension = GetExtension(extension_handle);
3422 ExtensionsService* service = profile_->GetExtensionsService();
3423 if (extension && service) {
3424 ExtensionToolbarModel* toolbar = service->toolbar_model();
3425 int found_index = -1;
3426 int index = 0;
3427 switch (type) {
3428 case AUTOMATION_MSG_EXTENSION_ID:
3429 *value = extension->id();
3430 *success = true;
3431 break;
3432 case AUTOMATION_MSG_EXTENSION_NAME:
3433 *value = extension->name();
3434 *success = true;
3435 break;
3436 case AUTOMATION_MSG_EXTENSION_VERSION:
3437 *value = extension->VersionString();
3438 *success = true;
3439 break;
3440 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3441 if (toolbar) {
3442 for (ExtensionList::const_iterator iter = toolbar->begin();
3443 iter != toolbar->end(); iter++) {
3444 // Skip this extension if we are in incognito mode
3445 // and it is not incognito-enabled.
3446 if (profile_->IsOffTheRecord() &&
3447 !service->IsIncognitoEnabled(*iter))
3448 continue;
3449 if (*iter == extension) {
3450 found_index = index;
3451 break;
3452 }
3453 index++;
3454 }
3455 *value = IntToString(found_index);
3456 *success = true;
3457 }
3458 break;
3459 default:
3460 LOG(WARNING) << "Trying to get undefined extension property";
3461 break;
3462 }
3463 }
3464}
3465
[email protected]673fd2c02010-02-04 23:10:003466void AutomationProvider::SaveAsAsync(int tab_handle) {
3467 NavigationController* tab = NULL;
3468 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3469 if (tab_contents)
3470 tab_contents->OnSavePage();
3471}
[email protected]7dad3d5f2010-03-04 00:27:013472
3473void AutomationProvider::SetContentSetting(
3474 int handle,
3475 const std::string& host,
3476 ContentSettingsType content_type,
3477 ContentSetting setting,
3478 bool* success) {
3479 *success = false;
3480 if (browser_tracker_->ContainsHandle(handle)) {
3481 Browser* browser = browser_tracker_->GetResource(handle);
3482 HostContentSettingsMap* map =
3483 browser->profile()->GetHostContentSettingsMap();
3484 if (host.empty()) {
3485 map->SetDefaultContentSetting(content_type, setting);
3486 } else {
[email protected]0314ae02010-04-08 09:18:293487 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3488 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013489 }
3490 *success = true;
3491 }
3492}
[email protected]cc824372010-03-31 15:33:013493
3494#if !defined(TOOLKIT_VIEWS)
3495void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3496 NOTIMPLEMENTED();
3497};
3498
3499void AutomationProvider::WaitForFocusedViewIDToChange(
3500 int handle, int previous_view_id, IPC::Message* reply_message) {
3501 NOTIMPLEMENTED();
3502}
3503
3504void AutomationProvider::StartTrackingPopupMenus(
3505 int browser_handle, bool* success) {
3506 NOTIMPLEMENTED();
3507}
3508
3509void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3510 NOTIMPLEMENTED();
3511}
3512#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:093513
3514void AutomationProvider::ResetToDefaultTheme() {
3515 profile_->ClearTheme();
3516}