blob: f73024f3651a953ceb8b72e4b9a62532e589beaf [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]c6cb1992009-04-13 16:45:2912#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2013#include "base/json/json_reader.h"
[email protected]59a611242010-04-02 02:24:0414#include "base/json/json_writer.h"
[email protected]bc1407f2009-09-29 00:33:3515#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3816#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4618#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5019#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2720#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3821#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5322#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1723#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2924#include "chrome/app/chrome_dll_resource.h"
[email protected]bcff05a2010-04-14 01:46:4325#include "chrome/app/chrome_version_info.h"
[email protected]0bfa713f2009-04-07 20:18:2826#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0927#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3328#include "chrome/browser/automation/automation_extension_function.h"
[email protected]790788ac2010-04-06 17:52:1929#include "chrome/browser/automation/automation_extension_tracker.h"
initial.commit09911bf2008-07-26 23:55:2930#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0831#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5532#include "chrome/browser/automation/extension_automation_constants.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]5c238752009-06-13 10:29:0737#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3938#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2039#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0540#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5841#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2942#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1243#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5944#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2245#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0546#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4047#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1948#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]d11c8e92009-10-20 23:26:4049#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5550#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1951#include "chrome/browser/extensions/extension_tabs_module.h"
52#include "chrome/browser/extensions/extension_toolbar_model.h"
53#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1654#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5855#include "chrome/browser/find_bar.h"
56#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2957#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0158#include "chrome/browser/host_content_settings_map.h"
[email protected]0ac83682010-01-22 17:46:2759#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0660#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0161#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5362#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2463#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0864#include "chrome/browser/pref_service.h"
[email protected]f83f9102010-05-04 17:01:0565#include "chrome/browser/printing/print_job.h"
[email protected]a7eee32f2009-05-22 18:08:1766#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0767#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2568#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0369#include "chrome/browser/ssl/ssl_manager.h"
70#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3471#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4572#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1773#include "chrome/common/automation_constants.h"
[email protected]a9ff2c02010-05-13 17:33:0574#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2975#include "chrome/common/chrome_paths.h"
[email protected]790788ac2010-04-06 17:52:1976#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1777#include "chrome/common/json_value_serializer.h"
[email protected]68d2a05f2010-05-07 21:39:5578#include "chrome/common/net/url_request_context_getter.h"
[email protected]1c58a5c2009-05-21 18:47:1479#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5780#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2581#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5682#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5783#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1784#include "net/proxy/proxy_service.h"
85#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2186#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:3087#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:2888#include "views/event.h"
[email protected]f7d48012010-05-06 08:17:0589#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:2990
[email protected]de246f52009-02-25 18:25:4591#if defined(OS_WIN)
[email protected]de246f52009-02-25 18:25:4592#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4593#endif // defined(OS_WIN)
94
[email protected]e1acf6f2008-10-27 20:43:3395using base::Time;
96
[email protected]cbab76d2008-10-13 22:42:4797class AutomationInterstitialPage : public InterstitialPage {
98 public:
[email protected]57c6a652009-05-04 07:58:3499 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47100 const GURL& url,
101 const std::string& contents)
102 : InterstitialPage(tab, true, url),
103 contents_(contents) {
104 }
105
106 virtual std::string GetHTMLContents() { return contents_; }
107
108 private:
109 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29110
[email protected]cbab76d2008-10-13 22:42:47111 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
112};
113
[email protected]c2cb8542009-08-20 21:16:51114class ClickTask : public Task {
115 public:
[email protected]fc2e0872009-08-21 22:14:41116 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51117 virtual ~ClickTask() {}
118
119 virtual void Run() {
120 ui_controls::MouseButton button = ui_controls::LEFT;
121 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
122 views::Event::EF_LEFT_BUTTON_DOWN) {
123 button = ui_controls::LEFT;
124 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
125 views::Event::EF_RIGHT_BUTTON_DOWN) {
126 button = ui_controls::RIGHT;
127 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
128 views::Event::EF_MIDDLE_BUTTON_DOWN) {
129 button = ui_controls::MIDDLE;
130 } else {
131 NOTREACHED();
132 }
133
[email protected]fc2e0872009-08-21 22:14:41134 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51135 }
136
137 private:
[email protected]c2cb8542009-08-20 21:16:51138 int flags_;
139
140 DISALLOW_COPY_AND_ASSIGN(ClickTask);
141};
[email protected]c2cb8542009-08-20 21:16:51142
initial.commit09911bf2008-07-26 23:55:29143AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57144 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56145 profile_(profile),
[email protected]cc824372010-03-31 15:33:01146 reply_message_(NULL),
147 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29148 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19149 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29150 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20151 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29152 autocomplete_edit_tracker_.reset(
153 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29154 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
155 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44156 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19157 extension_test_result_observer_.reset(
158 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13159 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29160}
161
162AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50163 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
164 port_containers_.end());
165 port_containers_.clear();
166
[email protected]0da050b92008-08-19 19:29:47167 // Make sure that any outstanding NotificationObservers also get destroyed.
168 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31169 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47170 while ((observer = it.GetNext()) != NULL)
171 delete observer;
[email protected]528211a2010-01-14 15:25:13172
173 if (channel_.get()) {
174 channel_->Close();
175 }
176 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29177}
178
[email protected]9a3a293b2009-06-04 22:28:16179void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06180 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57181 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06182 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
183 automation_resource_message_filter_,
184 g_browser_process->io_thread()->message_loop(),
185 true, g_browser_process->shutdown_event()));
[email protected]bcff05a2010-04-14 01:46:43186 scoped_ptr<FileVersionInfo> version_info(
187 chrome_app::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40188 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43189 if (version_info != NULL) {
190 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40191 }
[email protected]c6cb1992009-04-13 16:45:29192
193 // Send a hello message with our current automation protocol version.
194 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29195}
196
197void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
198 if (expected_tabs == 0) {
199 Send(new AutomationMsg_InitialLoadsComplete(0));
200 } else {
201 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
202 }
203}
204
205NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58206 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01207 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29208 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58209 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01210 number_of_navigations,
211 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29212
[email protected]71f65dd2009-02-11 19:14:56213 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29214 return observer;
215}
216
[email protected]faf2ee42010-05-11 14:26:17217void AutomationProvider::RemoveNavigationStatusListener(
218 NotificationObserver* obs) {
219 notification_observer_list_.RemoveObserver(obs);
220}
221
initial.commit09911bf2008-07-26 23:55:29222NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14223 Browser* parent,
224 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56225 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14226 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29227 notification_observer_list_.AddObserver(observer);
228
229 return observer;
230}
231
[email protected]faf2ee42010-05-11 14:26:17232void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
initial.commit09911bf2008-07-26 23:55:29233 notification_observer_list_.RemoveObserver(obs);
234}
235
236void AutomationProvider::AddLoginHandler(NavigationController* tab,
237 LoginHandler* handler) {
238 login_handler_map_[tab] = handler;
239}
240
241void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
242 DCHECK(login_handler_map_[tab]);
243 login_handler_map_.erase(tab);
244}
245
[email protected]f44265b2009-05-19 18:52:50246void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
247 int port_id = port->port_id();
248 DCHECK_NE(-1, port_id);
249 DCHECK(port_containers_.find(port_id) == port_containers_.end());
250
251 port_containers_[port_id] = port;
252}
253
254void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
255 int port_id = port->port_id();
256 DCHECK_NE(-1, port_id);
257
258 PortContainerMap::iterator it = port_containers_.find(port_id);
259 DCHECK(it != port_containers_.end());
260
261 if (it != port_containers_.end()) {
262 delete it->second;
263 port_containers_.erase(it);
264 }
265}
266
267ExtensionPortContainer* AutomationProvider::GetPortContainer(
268 int port_id) const {
269 PortContainerMap::const_iterator it = port_containers_.find(port_id);
270 if (it == port_containers_.end())
271 return NULL;
272
273 return it->second;
274}
275
initial.commit09911bf2008-07-26 23:55:29276int AutomationProvider::GetIndexForNavigationController(
277 const NavigationController* controller, const Browser* parent) const {
278 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12279 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29280}
281
[email protected]790788ac2010-04-06 17:52:19282int AutomationProvider::AddExtension(Extension* extension) {
283 DCHECK(extension);
284 return extension_tracker_->Add(extension);
285}
286
287Extension* AutomationProvider::GetExtension(int extension_handle) {
288 return extension_tracker_->GetResource(extension_handle);
289}
290
291Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
292 Extension* extension = extension_tracker_->GetResource(extension_handle);
293 ExtensionsService* service = profile_->GetExtensionsService();
294 if (extension && service &&
295 service->GetExtensionById(extension->id(), false))
296 return extension;
297 return NULL;
298}
299
300Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
301 Extension* extension = extension_tracker_->GetResource(extension_handle);
302 ExtensionsService* service = profile_->GetExtensionsService();
303 if (extension && service &&
304 service->GetExtensionById(extension->id(), true) &&
305 !service->GetExtensionById(extension->id(), false))
306 return extension;
307 return NULL;
308}
309
initial.commit09911bf2008-07-26 23:55:29310void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
311 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14312 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56313 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
314 CloseBrowserAsync)
315 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
316 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
317 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
318 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
319 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
320 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22321 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]1c58a5c2009-05-21 18:47:14322 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58323 IPC_MESSAGE_HANDLER_DELAY_REPLY(
324 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
325 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56326 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11327 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
328 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56329 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
330 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
331 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
332 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14333 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56334 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
335 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
336 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14337 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29338 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
339 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56340 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36341 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56342 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29343 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56344 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29345 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
346 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56347 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14348 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24349 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44350 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33351 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44352 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33353 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24354 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14355 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24356 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15357 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14358 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24359 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35360 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14361 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30362#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56363 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
364 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30365#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56366 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53367 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56368 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37369#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56370 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45371#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56372 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
373 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03374 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56375 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14376 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09377 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
378 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
379 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29380 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14381 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56382 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
383 ExecuteJavascript)
384 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29385 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14386 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
387 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56388 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
389 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14390 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17391 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14392 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14393 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53394 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
395 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14396 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56397 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14398 GetAutocompleteEditForBrowser)
399 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45400#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29401 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17402#endif
[email protected]71f65dd2009-02-11 19:14:56403 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29404 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26405 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
406 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56407 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14408 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56409 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14410 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17411#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29412 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
413 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17414#endif
[email protected]71f65dd2009-02-11 19:14:56415 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
416 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14417 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17418#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04419 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39420 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
421 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17422#endif
[email protected]1c58a5c2009-05-21 18:47:14423 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
424 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44425 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
426 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56427 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
428 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29429 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
430 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
431 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56432 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41433 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56434 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
435 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29436 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56437 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29438 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56439 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29440 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56441 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29442 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56443 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45444 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45445 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
446 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14447 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56448 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57449 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56450 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57451 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56452 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
453 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53454 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
455 GetBookmarksAsJSON)
456 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
457 WaitForBookmarkModelToLoad)
458 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
459 AddBookmarkGroup)
460 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
461 AddBookmarkURL)
462 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
463 ReparentBookmark)
464 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
465 SetBookmarkTitle)
466 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
467 SetBookmarkURL)
468 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
469 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04470 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
471 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18472 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
473 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
474 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56475 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42476 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56477 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
478 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14479 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56480 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44481 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56482 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20483 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14484 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56485 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16486 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56487 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16488 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56489 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16490 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14491 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20492 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
493 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14494 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]4cfc1d922009-11-08 14:02:58495#if defined(OS_WIN)
496 // Depends on ExternalTabContainer, so Windows-only
[email protected]b83e4602009-05-15 22:58:33497 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
498 SetEnableExtensionAutomation)
[email protected]4cfc1d922009-11-08 14:02:58499#endif
[email protected]59560e0b2009-06-04 03:30:22500 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13501 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19502 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
503 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
504 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
505 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
506 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
507 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52508 IPC_MESSAGE_HANDLER_DELAY_REPLY(
509 AutomationMsg_WaitForBrowserWindowCountToBecome,
510 WaitForBrowserWindowCountToBecome)
511 IPC_MESSAGE_HANDLER_DELAY_REPLY(
512 AutomationMsg_WaitForAppModalDialogToBeShown,
513 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26514 IPC_MESSAGE_HANDLER_DELAY_REPLY(
515 AutomationMsg_GoBackBlockUntilNavigationsComplete,
516 GoBackBlockUntilNavigationsComplete)
517 IPC_MESSAGE_HANDLER_DELAY_REPLY(
518 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
519 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14520#if defined(OS_WIN)
521 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
522#endif
[email protected]1bb5f892009-10-06 01:44:57523 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40524 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
525 InstallExtension)
526 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
527 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43528 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
529 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19530 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
531 WaitForExtensionTestResult)
532 IPC_MESSAGE_HANDLER_DELAY_REPLY(
533 AutomationMsg_InstallExtensionAndGetHandle,
534 InstallExtensionAndGetHandle)
535 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
536 UninstallExtension)
537 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
538 EnableExtension)
539 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
540 DisableExtension)
541 IPC_MESSAGE_HANDLER_DELAY_REPLY(
542 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
543 ExecuteExtensionActionInActiveTabAsync)
544 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
545 MoveExtensionBrowserAction)
546 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
547 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57548 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
549 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00550 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]2b19e2fe2010-02-16 02:24:18551#if defined(OS_WIN)
552 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
553#endif
[email protected]7dad3d5f2010-03-04 00:27:01554 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]566a0f762010-03-10 04:14:57555#if defined(OS_CHROMEOS)
556 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
557 LoginWithUserAndPass)
558#endif
[email protected]bc73b4e52010-03-26 04:16:20559 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]cc824372010-03-31 15:33:01560#if defined(TOOLKIT_VIEWS)
561 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
562 WaitForFocusedViewIDToChange)
563 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
564 StartTrackingPopupMenus)
565 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
566 WaitForPopupMenuToOpen)
567#endif
[email protected]d7e5525d2010-04-20 14:37:09568 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
initial.commit09911bf2008-07-26 23:55:29569 IPC_END_MESSAGE_MAP()
570}
571
[email protected]71f65dd2009-02-11 19:14:56572void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
573 *status = -1;
initial.commit09911bf2008-07-26 23:55:29574 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
575 Browser* browser = browser_tracker_->GetResource(handle);
576 if (at_index >= 0 && at_index < browser->tab_count()) {
577 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56578 *status = 0;
initial.commit09911bf2008-07-26 23:55:29579 }
580 }
initial.commit09911bf2008-07-26 23:55:29581}
582
[email protected]71f65dd2009-02-11 19:14:56583void AutomationProvider::AppendTab(int handle, const GURL& url,
584 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29585 int append_tab_response = -1; // -1 is the error code
586 NotificationObserver* observer = NULL;
587
588 if (browser_tracker_->ContainsHandle(handle)) {
589 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14590 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38591 TabContents* tab_contents = browser->AddTabWithURL(
592 url, GURL(), PageTransition::TYPED, -1, Browser::ADD_SELECTED, NULL,
593 std::string());
initial.commit09911bf2008-07-26 23:55:29594 if (tab_contents) {
595 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57596 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29597 }
598 }
599
600 if (append_tab_response < 0) {
601 // The append tab failed. Remove the TabStripObserver
602 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17603 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29604 delete observer;
605 }
606
[email protected]71f65dd2009-02-11 19:14:56607 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
608 append_tab_response);
609 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29610 }
611}
612
[email protected]71f65dd2009-02-11 19:14:56613void AutomationProvider::NavigateToURL(int handle, const GURL& url,
614 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58615 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
616}
617
618void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
619 int handle, const GURL& url, int number_of_navigations,
620 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29621 if (tab_tracker_->ContainsHandle(handle)) {
622 NavigationController* tab = tab_tracker_->GetResource(handle);
623
624 // Simulate what a user would do. Activate the tab and then navigate.
625 // We could allow navigating in a background tab in future.
626 Browser* browser = FindAndActivateTab(tab);
627
628 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01629 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
630 false);
[email protected]71f65dd2009-02-11 19:14:56631
initial.commit09911bf2008-07-26 23:55:29632 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50633 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29634 return;
635 }
636 }
[email protected]71f65dd2009-02-11 19:14:56637
638 AutomationMsg_NavigateToURL::WriteReplyParams(
639 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
640 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29641}
[email protected]2949e90d2009-08-21 15:32:52642
[email protected]c70f9b82010-04-21 07:31:11643void AutomationProvider::NavigationAsync(int handle,
644 const GURL& url,
645 bool* status) {
646 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
647}
648
649void AutomationProvider::NavigationAsyncWithDisposition(
650 int handle,
651 const GURL& url,
652 WindowOpenDisposition disposition,
653 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56654 *status = false;
initial.commit09911bf2008-07-26 23:55:29655
656 if (tab_tracker_->ContainsHandle(handle)) {
657 NavigationController* tab = tab_tracker_->GetResource(handle);
658
659 // Simulate what a user would do. Activate the tab and then navigate.
660 // We could allow navigating in a background tab in future.
661 Browser* browser = FindAndActivateTab(tab);
662
663 if (browser) {
664 // Don't add any listener unless a callback mechanism is desired.
665 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11666 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56667 *status = true;
initial.commit09911bf2008-07-26 23:55:29668 }
669 }
initial.commit09911bf2008-07-26 23:55:29670}
671
[email protected]71f65dd2009-02-11 19:14:56672void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29673 if (tab_tracker_->ContainsHandle(handle)) {
674 NavigationController* tab = tab_tracker_->GetResource(handle);
675 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14676 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01677 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29678 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29679 return;
680 }
681 }
[email protected]71f65dd2009-02-11 19:14:56682
683 AutomationMsg_GoBack::WriteReplyParams(
684 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
685 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29686}
687
[email protected]71f65dd2009-02-11 19:14:56688void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29689 if (tab_tracker_->ContainsHandle(handle)) {
690 NavigationController* tab = tab_tracker_->GetResource(handle);
691 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14692 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01693 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29694 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29695 return;
696 }
697 }
[email protected]71f65dd2009-02-11 19:14:56698
699 AutomationMsg_GoForward::WriteReplyParams(
700 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
701 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29702}
703
[email protected]71f65dd2009-02-11 19:14:56704void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29705 if (tab_tracker_->ContainsHandle(handle)) {
706 NavigationController* tab = tab_tracker_->GetResource(handle);
707 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14708 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01709 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29710 browser->Reload();
711 return;
712 }
713 }
[email protected]71f65dd2009-02-11 19:14:56714
715 AutomationMsg_Reload::WriteReplyParams(
716 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
717 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29718}
719
[email protected]71f65dd2009-02-11 19:14:56720void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29721 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56722 const std::wstring& password,
723 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29724 if (tab_tracker_->ContainsHandle(tab_handle)) {
725 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
726 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
727
728 if (iter != login_handler_map_.end()) {
729 // If auth is needed again after this, assume login has failed. This is
730 // not strictly correct, because a navigation can require both proxy and
731 // server auth, but it should be OK for now.
732 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01733 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29734 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52735 return;
initial.commit09911bf2008-07-26 23:55:29736 }
737 }
[email protected]de246f52009-02-25 18:25:45738
[email protected]457f5cf2009-08-18 16:37:52739 AutomationMsg_SetAuth::WriteReplyParams(
740 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
741 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29742}
743
[email protected]71f65dd2009-02-11 19:14:56744void AutomationProvider::CancelAuth(int tab_handle,
745 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29746 if (tab_tracker_->ContainsHandle(tab_handle)) {
747 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
748 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
749
750 if (iter != login_handler_map_.end()) {
751 // If auth is needed again after this, something is screwy.
752 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01753 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29754 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52755 return;
initial.commit09911bf2008-07-26 23:55:29756 }
757 }
[email protected]de246f52009-02-25 18:25:45758
[email protected]457f5cf2009-08-18 16:37:52759 AutomationMsg_CancelAuth::WriteReplyParams(
760 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
761 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29762}
763
[email protected]71f65dd2009-02-11 19:14:56764void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
765 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29766
767 if (tab_tracker_->ContainsHandle(tab_handle)) {
768 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
769 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
770
771 if (iter != login_handler_map_.end()) {
772 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56773 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29774 }
775 }
initial.commit09911bf2008-07-26 23:55:29776}
777
[email protected]71f65dd2009-02-11 19:14:56778void AutomationProvider::GetRedirectsFrom(int tab_handle,
779 const GURL& source_url,
780 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29781 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
782 if (tab_tracker_->ContainsHandle(tab_handle)) {
783 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
784 HistoryService* history_service =
785 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
786
787 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
788 "has no history service";
789 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56790 DCHECK(reply_message_ == NULL);
791 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29792 // Schedule a history query for redirects. The response will be sent
793 // asynchronously from the callback the history system uses to notify us
794 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29795 redirect_query_ = history_service->QueryRedirectsFrom(
796 source_url, &consumer_,
797 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
798 return; // Response will be sent when query completes.
799 }
800 }
801
802 // Send failure response.
[email protected]deb57402009-02-06 01:35:30803 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56804 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
805 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29806}
807
[email protected]71f65dd2009-02-11 19:14:56808void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
809 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29810 if (browser_tracker_->ContainsHandle(handle)) {
811 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56812 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29813 }
initial.commit09911bf2008-07-26 23:55:29814}
815
[email protected]202e7a72009-06-15 03:48:36816void AutomationProvider::GetBrowserLocale(string16* locale) {
817 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06818 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36819}
820
[email protected]71f65dd2009-02-11 19:14:56821void AutomationProvider::GetBrowserWindowCount(int* window_count) {
822 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29823}
824
[email protected]24497032009-05-01 17:00:29825void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
826 *window_count = static_cast<int>(
827 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
828}
829
[email protected]71f65dd2009-02-11 19:14:56830void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
831 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07832 AppModalDialog* dialog_delegate =
833 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50834 *showing_dialog = (dialog_delegate != NULL);
835 if (*showing_dialog)
836 *dialog_button = dialog_delegate->GetDialogButtons();
837 else
[email protected]478ff2ed2009-04-21 23:49:18838 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20839}
840
[email protected]71f65dd2009-02-11 19:14:56841void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
842 *success = false;
[email protected]fad84eab2008-12-05 00:37:20843
[email protected]1f460072009-05-28 17:02:07844 AppModalDialog* dialog_delegate =
845 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50846 if (dialog_delegate &&
847 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18848 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
849 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28850 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56851 *success = true;
[email protected]fad84eab2008-12-05 00:37:20852 }
[email protected]478ff2ed2009-04-21 23:49:18853 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
854 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56855 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28856 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56857 *success = true;
[email protected]fad84eab2008-12-05 00:37:20858 }
859 }
[email protected]c274acc2008-11-11 20:13:44860}
861
[email protected]fedaa7d2010-01-26 20:34:57862void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
863 if (browser_tracker_->ContainsHandle(handle)) {
864 Browser* browser = browser_tracker_->GetResource(handle);
865 browser->profile()->ShutdownSessionService();
866 *result = true;
867 } else {
868 *result = false;
869 }
870}
871
[email protected]71f65dd2009-02-11 19:14:56872void AutomationProvider::GetBrowserWindow(int index, int* handle) {
873 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29874 if (index >= 0) {
875 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29876 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29877 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56878 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29879 }
880 }
initial.commit09911bf2008-07-26 23:55:29881}
882
[email protected]24497032009-05-01 17:00:29883void AutomationProvider::FindNormalBrowserWindow(int* handle) {
884 *handle = 0;
885 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31886 Browser::TYPE_NORMAL,
887 false);
[email protected]24497032009-05-01 17:00:29888 if (browser)
889 *handle = browser_tracker_->Add(browser);
890}
891
[email protected]71f65dd2009-02-11 19:14:56892void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
893 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29894 Browser* browser = BrowserList::GetLastActive();
895 if (browser)
[email protected]71f65dd2009-02-11 19:14:56896 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29897}
898
[email protected]b2aa3ed72010-02-01 18:37:14899#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28900// TODO(estade): use this implementation for all platforms?
901void AutomationProvider::GetActiveWindow(int* handle) {
902 gfx::NativeWindow window =
903 BrowserList::GetLastActive()->window()->GetNativeHandle();
904 *handle = window_tracker_->Add(window);
905}
906#endif
907
[email protected]4f6381ee2009-04-16 02:46:33908void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
909 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56910 *success = false;
[email protected]4ae62752008-08-04 23:28:47911 if (browser_tracker_->ContainsHandle(handle)) {
912 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14913 if (browser->command_updater()->SupportsCommand(command) &&
914 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47915 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56916 *success = true;
[email protected]4ae62752008-08-04 23:28:47917 }
918 }
[email protected]4ae62752008-08-04 23:28:47919}
920
[email protected]4f6381ee2009-04-16 02:46:33921void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56922 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53923 // List of commands which just finish synchronously and don't require
924 // setting up an observer.
925 static const int kSynchronousCommands[] = {
926 IDC_HOME,
927 IDC_SELECT_NEXT_TAB,
928 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25929 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53930 };
[email protected]56e71b7c2009-03-27 03:05:56931 if (browser_tracker_->ContainsHandle(handle)) {
932 Browser* browser = browser_tracker_->GetResource(handle);
933 if (browser->command_updater()->SupportsCommand(command) &&
934 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53935 // First check if we can handle the command without using an observer.
936 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
937 if (command == kSynchronousCommands[i]) {
938 browser->ExecuteCommand(command);
939 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
940 true);
941 Send(reply_message);
942 return;
943 }
944 }
945
946 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42947 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
948 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27949 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42950 return;
951 }
[email protected]56e71b7c2009-03-27 03:05:56952 }
953 }
[email protected]49a14a82009-03-31 04:16:44954 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56955 Send(reply_message);
956}
957
[email protected]fc2e0872009-08-21 22:14:41958// This task just adds another task to the event queue. This is useful if
959// you want to ensure that any tasks added to the event queue after this one
960// have already been processed by the time |task| is run.
961class InvokeTaskLaterTask : public Task {
962 public:
963 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
964 virtual ~InvokeTaskLaterTask() {}
965
966 virtual void Run() {
967 MessageLoop::current()->PostTask(FROM_HERE, task_);
968 }
969
970 private:
971 Task* task_;
972
973 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
974};
975
initial.commit09911bf2008-07-26 23:55:29976void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
977 int handle,
[email protected]d1a5941e2009-08-13 23:34:24978 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29979 int flags) {
[email protected]b410bc32009-08-14 01:11:14980 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51981 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41982 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29983 }
984}
985
[email protected]60507b12009-11-02 23:51:35986void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
987 int handle,
988 const gfx::Point& location) {
989 if (window_tracker_->ContainsHandle(handle))
990 ui_controls::SendMouseMove(location.x(), location.y());
991}
992
initial.commit09911bf2008-07-26 23:55:29993void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
994 int handle,
[email protected]bc1407f2009-09-29 00:33:35995 int key,
initial.commit09911bf2008-07-26 23:55:29996 int flags) {
[email protected]b410bc32009-08-14 01:11:14997 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29998 return;
999
[email protected]b410bc32009-08-14 01:11:141000 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291001 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:351002 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:381003 ((flags & views::Event::EF_CONTROL_DOWN) ==
1004 views::Event::EF_CONTROL_DOWN),
1005 ((flags & views::Event::EF_SHIFT_DOWN) ==
1006 views::Event::EF_SHIFT_DOWN),
1007 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301008 views::Event::EF_ALT_DOWN),
1009 ((flags & views::Event::EF_COMMAND_DOWN) ==
1010 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291011}
initial.commit09911bf2008-07-26 23:55:291012
[email protected]71f65dd2009-02-11 19:14:561013void AutomationProvider::IsWindowActive(int handle, bool* success,
1014 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291015 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171016 *is_active =
1017 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561018 *success = true;
initial.commit09911bf2008-07-26 23:55:291019 } else {
[email protected]71f65dd2009-02-11 19:14:561020 *success = false;
1021 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291022 }
1023}
1024
[email protected]71f65dd2009-02-11 19:14:561025void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1026 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291027
1028 if (browser_tracker_->ContainsHandle(handle)) {
1029 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561030 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291031 }
initial.commit09911bf2008-07-26 23:55:291032}
1033
[email protected]982921f12009-10-27 21:43:531034void AutomationProvider::GetType(int handle, int* type_as_int) {
1035 *type_as_int = -1; // -1 is the error code
1036
1037 if (browser_tracker_->ContainsHandle(handle)) {
1038 Browser* browser = browser_tracker_->GetResource(handle);
1039 *type_as_int = static_cast<int>(browser->type());
1040 }
1041}
1042
[email protected]71f65dd2009-02-11 19:14:561043void AutomationProvider::GetTab(int win_handle, int tab_index,
1044 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561045 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291046 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1047 Browser* browser = browser_tracker_->GetResource(win_handle);
1048 if (tab_index < browser->tab_count()) {
1049 TabContents* tab_contents =
1050 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571051 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291052 }
1053 }
initial.commit09911bf2008-07-26 23:55:291054}
1055
[email protected]71f65dd2009-02-11 19:14:561056void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1057 std::wstring* title) {
1058 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291059 if (tab_tracker_->ContainsHandle(handle)) {
1060 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361061 NavigationEntry* entry = tab->GetActiveEntry();
1062 if (entry != NULL) {
1063 *title = UTF16ToWideHack(entry->title());
1064 } else {
1065 *title = std::wstring();
1066 }
[email protected]71f65dd2009-02-11 19:14:561067 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291068 }
initial.commit09911bf2008-07-26 23:55:291069}
1070
[email protected]77bc6732009-04-20 22:01:031071void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1072 *tabstrip_index = -1; // -1 is the error code
1073
1074 if (tab_tracker_->ContainsHandle(handle)) {
1075 NavigationController* tab = tab_tracker_->GetResource(handle);
1076 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121077 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031078 }
1079}
1080
initial.commit09911bf2008-07-26 23:55:291081void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1082 if (window_tracker_->ContainsHandle(handle)) {
1083 window_tracker_->Remove(window_tracker_->GetResource(handle));
1084 }
1085}
1086
1087void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161088 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571089 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291090}
1091
1092// TODO(brettw) change this to accept GURLs when history supports it
1093void AutomationProvider::OnRedirectQueryComplete(
1094 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371095 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291096 bool success,
[email protected]379c2b12009-07-01 21:50:331097 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291098 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561099 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291100
[email protected]deb57402009-02-06 01:35:301101 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101102 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291103 if (success) {
initial.commit09911bf2008-07-26 23:55:291104 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301105 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291106 }
1107
[email protected]4f3dc372009-02-24 00:10:291108 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301109
[email protected]71f65dd2009-02-11 19:14:561110 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231111 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561112 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291113}
1114
1115bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571116 DCHECK(channel_.get());
1117 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291118}
1119
1120Browser* AutomationProvider::FindAndActivateTab(
1121 NavigationController* controller) {
1122 int tab_index;
1123 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1124 if (browser)
1125 browser->SelectTabContentsAt(tab_index, true);
1126
1127 return browser;
1128}
1129
[email protected]71f65dd2009-02-11 19:14:561130void AutomationProvider::GetCookies(const GURL& url, int handle,
1131 int* value_size,
1132 std::string* value) {
1133 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291134 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1135 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311136
1137 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001138 scoped_refptr<URLRequestContextGetter> request_context =
1139 tab->tab_contents()->request_context();
1140 if (!request_context.get())
1141 request_context = tab->profile()->GetRequestContext();
1142
1143 net::CookieStore* cookie_store = request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311144
1145 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561146 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291147 }
initial.commit09911bf2008-07-26 23:55:291148}
1149
[email protected]71f65dd2009-02-11 19:14:561150void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291151 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561152 int handle,
1153 int* response_value) {
1154 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291155
1156 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1157 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311158
[email protected]dfa46e5f2009-11-17 18:48:431159 scoped_refptr<URLRequestContextGetter> request_context =
1160 tab->tab_contents()->request_context();
1161 if (!request_context.get())
1162 request_context = tab->profile()->GetRequestContext();
1163
[email protected]be180c802009-10-23 06:33:311164 // Since we are running on the UI thread don't call GetURLRequestContext().
1165 scoped_refptr<net::CookieStore> cookie_store =
[email protected]dfa46e5f2009-11-17 18:48:431166 request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311167
1168 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561169 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291170 }
initial.commit09911bf2008-07-26 23:55:291171}
1172
[email protected]5fa57942010-04-21 23:07:221173void AutomationProvider::DeleteCookie(const GURL& url,
1174 const std::string& cookie_name,
1175 int handle, bool* success) {
1176 *success = false;
1177 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1178 NavigationController* tab = tab_tracker_->GetResource(handle);
1179 net::CookieStore* cookie_store =
1180 tab->profile()->GetRequestContext()->GetCookieStore();
1181 cookie_store->DeleteCookie(url, cookie_name);
1182 *success = true;
1183 }
1184}
1185
[email protected]71f65dd2009-02-11 19:14:561186void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1187 *success = false;
initial.commit09911bf2008-07-26 23:55:291188 if (tab_tracker_->ContainsHandle(handle)) {
1189 NavigationController* tab = tab_tracker_->GetResource(handle);
1190 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541191 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561192 *success = true;
initial.commit09911bf2008-07-26 23:55:291193 }
initial.commit09911bf2008-07-26 23:55:291194}
1195
[email protected]71f65dd2009-02-11 19:14:561196void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1197 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291198
1199 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561200 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341201 TabContents* tab_contents =
1202 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161203 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1204 if (rph)
1205 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291206 }
initial.commit09911bf2008-07-26 23:55:291207}
1208
1209void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331210 NOTREACHED() << "This function has been deprecated. "
1211 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291212}
1213
[email protected]71f65dd2009-02-11 19:14:561214void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291215 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561216 const std::wstring& script,
1217 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291218 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341219 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1220 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571221 // Set the routing id of this message with the controller.
1222 // This routing id needs to be remembered for the reverse
1223 // communication while sending back the response of
1224 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331225 std::wstring set_automation_id;
1226 SStringPrintf(&set_automation_id,
1227 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561228 reply_message->routing_id());
1229
1230 DCHECK(reply_message_ == NULL);
1231 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291232
[email protected]57c6a652009-05-04 07:58:341233 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331234 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341235 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061236 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571237 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291238 }
1239
1240 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561241 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1242 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291243 }
1244}
1245
[email protected]71f65dd2009-02-11 19:14:561246void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1247 *visible = false;
[email protected]20e93d12008-08-28 16:31:571248
[email protected]59560e0b2009-06-04 03:30:221249 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251250#if defined(OS_CHROMEOS)
1251 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1252 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1253 for (BrowserList::const_iterator it = BrowserList::begin();
1254 it != BrowserList::end(); ++it) {
1255 if ((*it)->type() == Browser::TYPE_POPUP) {
1256 const GURL& url =
1257 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1258
1259 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1260 url.host() == chrome::kChromeUIFileBrowseHost) {
1261 *visible = true;
1262 break;
1263 }
1264 }
1265 }
1266#else
[email protected]59560e0b2009-06-04 03:30:221267 Browser* browser = browser_tracker_->GetResource(handle);
1268 if (browser) {
1269 *visible = browser->window()->IsDownloadShelfVisible();
1270 }
[email protected]f5bf8ccf2010-02-05 18:19:251271#endif
[email protected]59560e0b2009-06-04 03:30:221272 }
initial.commit09911bf2008-07-26 23:55:291273}
1274
[email protected]59560e0b2009-06-04 03:30:221275void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1276 if (browser_tracker_->ContainsHandle(handle)) {
1277 Browser* browser = browser_tracker_->GetResource(handle);
1278 if (browser) {
1279 if (visible)
1280 browser->window()->GetDownloadShelf()->Show();
1281 else
1282 browser->window()->GetDownloadShelf()->Close();
1283 }
1284 }
1285}
1286
[email protected]34930432009-11-09 00:12:091287void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1288 *visible = false;
1289
1290 if (browser_tracker_->ContainsHandle(handle)) {
1291 Browser* browser = browser_tracker_->GetResource(handle);
1292 if (browser)
1293 *visible = browser->window()->IsFullscreen();
1294 }
1295}
1296
1297void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1298 bool* visible) {
1299 *visible = false;
1300
1301 if (browser_tracker_->ContainsHandle(handle)) {
1302 Browser* browser = browser_tracker_->GetResource(handle);
1303 if (browser)
1304 *visible = browser->window()->IsFullscreenBubbleVisible();
1305 }
1306}
[email protected]59560e0b2009-06-04 03:30:221307
[email protected]71f65dd2009-02-11 19:14:561308void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1309 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291310 if (tab_tracker_->ContainsHandle(handle)) {
1311 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111312 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291313 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561314 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291315 }
1316 }
initial.commit09911bf2008-07-26 23:55:291317}
1318
initial.commit09911bf2008-07-26 23:55:291319void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561320 int handle, const std::wstring& find_request,
1321 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311322 NOTREACHED() << "This function has been deprecated."
1323 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561324 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311325 return;
1326}
1327
[email protected]4f999132009-03-31 18:08:401328void AutomationProvider::HandleFindRequest(
1329 int handle,
1330 const AutomationMsg_Find_Params& params,
1331 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291332 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561333 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1334 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291335 return;
1336 }
1337
1338 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111339 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291340
1341 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141342 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291343
[email protected]57c6a652009-05-04 07:58:341344 tab_contents->set_current_find_request_id(
1345 FindInPageNotificationObserver::kFindInPageRequestId);
1346 tab_contents->render_view_host()->StartFinding(
1347 FindInPageNotificationObserver::kFindInPageRequestId,
1348 params.search_string, params.forward, params.match_case,
1349 params.find_next);
initial.commit09911bf2008-07-26 23:55:291350}
1351
[email protected]5f8af2a2008-08-06 22:49:451352void AutomationProvider::HandleOpenFindInPageRequest(
1353 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291354 if (browser_tracker_->ContainsHandle(handle)) {
1355 Browser* browser = browser_tracker_->GetResource(handle);
1356 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451357 }
1358}
1359
[email protected]71f65dd2009-02-11 19:14:561360void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561361 *visible = false;
[email protected]855c0142009-09-28 22:35:241362 Browser* browser = browser_tracker_->GetResource(handle);
1363 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581364 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171365 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241366 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291367 }
[email protected]20e93d12008-08-28 16:31:571368}
1369
[email protected]71f65dd2009-02-11 19:14:561370void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1371 int* y) {
[email protected]9e0534b2008-10-21 15:03:011372 gfx::Point position(0, 0);
1373 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291374 if (browser_tracker_->ContainsHandle(handle)) {
1375 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581376 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171377 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581378 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291379 }
[email protected]20e93d12008-08-28 16:31:571380
[email protected]71f65dd2009-02-11 19:14:561381 *x = position.x();
1382 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571383}
1384
[email protected]4512cb52010-04-05 19:50:251385// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1386// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581387void AutomationProvider::GetBookmarkBarVisibility(int handle,
1388 bool* visible,
1389 bool* animating) {
1390 *visible = false;
1391 *animating = false;
1392
1393 if (browser_tracker_->ContainsHandle(handle)) {
1394 Browser* browser = browser_tracker_->GetResource(handle);
1395 if (browser) {
[email protected]ab6ca392010-04-07 00:44:131396#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1397 // IsBookmarkBarVisible() line looks correct but is not
1398 // consistent across platforms. Specifically, on Mac/Linux, it
1399 // returns false if the bar is hidden in a pref (even if visible
1400 // on the NTP). On ChromeOS, it returned true if on NTP
1401 // independent of the pref. Making the code more consistent
1402 // caused a perf bot regression on Windows (which shares views).
1403 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251404 *visible = browser->profile()->GetPrefs()->GetBoolean(
1405 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401406#else
1407 *visible = browser->window()->IsBookmarkBarVisible();
1408#endif
[email protected]c3240722010-03-05 21:52:581409 *animating = browser->window()->IsBookmarkBarAnimating();
1410 }
1411 }
1412}
1413
[email protected]6d8ffc9f2010-03-12 18:27:531414void AutomationProvider::GetBookmarksAsJSON(int handle,
1415 std::string* bookmarks_as_json,
1416 bool *success) {
1417 *success = false;
1418 if (browser_tracker_->ContainsHandle(handle)) {
1419 Browser* browser = browser_tracker_->GetResource(handle);
1420 if (browser) {
1421 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1422 return;
1423 }
1424 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1425 browser->profile(),
1426 browser->profile()->GetBookmarkModel());
1427 *success = storage->SerializeData(bookmarks_as_json);
1428 }
1429 }
1430}
1431
1432void AutomationProvider::WaitForBookmarkModelToLoad(
1433 int handle,
1434 IPC::Message* reply_message) {
1435 if (browser_tracker_->ContainsHandle(handle)) {
1436 Browser* browser = browser_tracker_->GetResource(handle);
1437 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1438 if (model->IsLoaded()) {
1439 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1440 reply_message, true);
1441 Send(reply_message);
1442 } else {
1443 // The observer will delete itself when done.
1444 new AutomationProviderBookmarkModelObserver(this, reply_message,
1445 model);
1446 }
1447 }
1448}
1449
1450void AutomationProvider::AddBookmarkGroup(int handle,
1451 int64 parent_id, int index,
1452 std::wstring title,
1453 bool* success) {
1454 if (browser_tracker_->ContainsHandle(handle)) {
1455 Browser* browser = browser_tracker_->GetResource(handle);
1456 if (browser) {
1457 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1458 if (!model->IsLoaded()) {
1459 *success = false;
1460 return;
1461 }
1462 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1463 DCHECK(parent);
1464 if (parent) {
1465 const BookmarkNode* child = model->AddGroup(parent, index,
1466 WideToUTF16(title));
1467 DCHECK(child);
1468 if (child)
1469 *success = true;
1470 }
1471 }
1472 }
1473 *success = false;
1474}
1475
1476void AutomationProvider::AddBookmarkURL(int handle,
1477 int64 parent_id, int index,
1478 std::wstring title, const GURL& url,
1479 bool* success) {
1480 if (browser_tracker_->ContainsHandle(handle)) {
1481 Browser* browser = browser_tracker_->GetResource(handle);
1482 if (browser) {
1483 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1484 if (!model->IsLoaded()) {
1485 *success = false;
1486 return;
1487 }
1488 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1489 DCHECK(parent);
1490 if (parent) {
1491 const BookmarkNode* child = model->AddURL(parent, index,
1492 WideToUTF16(title), url);
1493 DCHECK(child);
1494 if (child)
1495 *success = true;
1496 }
1497 }
1498 }
1499 *success = false;
1500}
1501
1502void AutomationProvider::ReparentBookmark(int handle,
1503 int64 id, int64 new_parent_id,
1504 int index,
1505 bool* success) {
1506 if (browser_tracker_->ContainsHandle(handle)) {
1507 Browser* browser = browser_tracker_->GetResource(handle);
1508 if (browser) {
1509 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1510 if (!model->IsLoaded()) {
1511 *success = false;
1512 return;
1513 }
1514 const BookmarkNode* node = model->GetNodeByID(id);
1515 DCHECK(node);
1516 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1517 DCHECK(new_parent);
1518 if (node && new_parent) {
1519 model->Move(node, new_parent, index);
1520 *success = true;
1521 }
1522 }
1523 }
1524 *success = false;
1525}
1526
1527void AutomationProvider::SetBookmarkTitle(int handle,
1528 int64 id, std::wstring title,
1529 bool* success) {
1530 if (browser_tracker_->ContainsHandle(handle)) {
1531 Browser* browser = browser_tracker_->GetResource(handle);
1532 if (browser) {
1533 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1534 if (!model->IsLoaded()) {
1535 *success = false;
1536 return;
1537 }
1538 const BookmarkNode* node = model->GetNodeByID(id);
1539 DCHECK(node);
1540 if (node) {
1541 model->SetTitle(node, WideToUTF16(title));
1542 *success = true;
1543 }
1544 }
1545 }
1546 *success = false;
1547}
1548
1549void AutomationProvider::SetBookmarkURL(int handle,
1550 int64 id, const GURL& url,
1551 bool* success) {
1552 if (browser_tracker_->ContainsHandle(handle)) {
1553 Browser* browser = browser_tracker_->GetResource(handle);
1554 if (browser) {
1555 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1556 if (!model->IsLoaded()) {
1557 *success = false;
1558 return;
1559 }
1560 const BookmarkNode* node = model->GetNodeByID(id);
1561 DCHECK(node);
1562 if (node) {
1563 model->SetURL(node, url);
1564 *success = true;
1565 }
1566 }
1567 }
1568 *success = false;
1569}
1570
1571void AutomationProvider::RemoveBookmark(int handle,
1572 int64 id,
1573 bool* success) {
1574 if (browser_tracker_->ContainsHandle(handle)) {
1575 Browser* browser = browser_tracker_->GetResource(handle);
1576 if (browser) {
1577 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1578 if (!model->IsLoaded()) {
1579 *success = false;
1580 return;
1581 }
1582 const BookmarkNode* node = model->GetNodeByID(id);
1583 DCHECK(node);
1584 if (node) {
1585 const BookmarkNode* parent = node->GetParent();
1586 DCHECK(parent);
1587 model->Remove(parent, parent->IndexOfChild(node));
1588 *success = true;
1589 }
1590 }
1591 }
1592 *success = false;
1593}
1594
[email protected]a9ff2c02010-05-13 17:33:051595// Sample json input: { "command": "GetBrowserInfo" }
1596// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1597// sample json output.
1598void AutomationProvider::GetBrowserInfo(DictionaryValue* args,
1599 IPC::Message* reply_message) {
1600 std::string json_return;
1601 bool reply_return = true;
1602
1603 DictionaryValue* properties = new DictionaryValue;
1604 properties->SetString(L"ChromeVersion", chrome::kChromeVersion);
1605 properties->SetString(L"BrowserProcessExecutableName",
1606 chrome::kBrowserProcessExecutableName);
1607 properties->SetString(L"HelperProcessExecutableName",
1608 chrome::kHelperProcessExecutableName);
1609 properties->SetString(L"BrowserProcessExecutablePath",
1610 chrome::kBrowserProcessExecutablePath);
1611 properties->SetString(L"HelperProcessExecutablePath",
1612 chrome::kHelperProcessExecutablePath);
1613#if defined(OS_WIN)
1614 properties->SetString(L"command_line_string",
1615 CommandLine::ForCurrentProcess()->command_line_string());
1616#elif defined(OS_POSIX)
1617 std::string command_line_string;
1618 const std::vector<std::string>& argv =
1619 CommandLine::ForCurrentProcess()->argv();
1620 for (uint i = 0; i < argv.size(); ++i)
1621 command_line_string += argv[i] + " ";
1622 properties->SetString(L"command_line_string", command_line_string);
1623#endif
1624
1625 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1626 return_value->Set(L"properties", properties);
1627
1628 base::JSONWriter::Write(return_value.get(), false, &json_return);
1629 AutomationMsg_SendJSONRequest::WriteReplyParams(
1630 reply_message, json_return, reply_return);
1631 Send(reply_message);
1632}
1633
[email protected]24e2b102010-04-29 17:56:471634// Sample json input: { "command": "GetHistoryInfo",
1635// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361636// Refer chrome/test/pyautolib/history_info.py for sample json output.
1637void AutomationProvider::GetHistoryInfo(
1638 DictionaryValue* args,
1639 IPC::Message* reply_message) {
1640 consumer_.CancelAllRequests();
1641
1642 std::wstring search_text;
1643 args->GetString(L"search_text", &search_text);
1644
1645 // Fetch history.
1646 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1647 history::QueryOptions options;
1648 // The observer owns itself. It deletes itself after it fetches history.
1649 AutomationProviderHistoryObserver* history_observer =
1650 new AutomationProviderHistoryObserver(this, reply_message);
1651 hs->QueryHistory(
1652 search_text,
1653 options,
1654 &consumer_,
1655 NewCallback(history_observer,
1656 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1657}
1658
[email protected]bbe6aa02010-05-07 17:27:291659// Sample json input: { "command": "AddHistoryItem",
1660// "item": { "URL": "http://www.google.com",
1661// "title": "Google", # optional
1662// "time": 12345 # optional (time_t)
1663// } }
1664// Refer chrome/test/pyautolib/pyauto.py for details on input.
1665void AutomationProvider::AddHistoryItem(
1666 DictionaryValue* args,
1667 IPC::Message* reply_message) {
1668 bool reply_return = true;
1669 std::string json_return = "{}";
1670
1671 DictionaryValue* item = NULL;
1672 args->GetDictionary(L"item", &item);
1673 string16 url_text;
1674 std::wstring title;
1675 base::Time time = base::Time::Now();
1676
1677 if (item->GetString("url", &url_text)) {
1678 GURL gurl(url_text);
1679 item->GetString(L"title", &title); // Don't care if it fails.
1680 int it;
1681 double dt;
1682 if (item->GetInteger(L"time", &it))
1683 time = base::Time::FromTimeT(it);
1684 else if (item->GetReal(L"time", &dt))
1685 time = base::Time::FromDoubleT(dt);
1686
1687 // Ideas for "dummy" values (e.g. id_scope) came from
1688 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
1689 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1690 const void* id_scope = reinterpret_cast<void*>(1);
1691 hs->AddPage(gurl, time,
1692 id_scope,
1693 0,
1694 GURL(),
1695 PageTransition::LINK,
1696 history::RedirectList(),
1697 false);
1698 if (title.length()) {
1699 // TODO(jrg): add a string16 interface for
1700 // HistoryService::SetPageTitle(), then use it.
1701 hs->SetPageTitle(gurl, title);
1702 }
1703 } else {
1704 json_return = "{\"error\": \"bad args (no URL in dict?).\"}";
1705 reply_return = false;
1706 }
1707
1708 AutomationMsg_SendJSONRequest::WriteReplyParams(
1709 reply_message, json_return, reply_return);
1710 Send(reply_message);
1711}
1712
[email protected]24e2b102010-04-29 17:56:471713// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361714// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]d4adc292010-04-15 18:06:391715void AutomationProvider::GetDownloadsInfo(
1716 DictionaryValue* args,
1717 IPC::Message* reply_message) {
1718 std::string json_return;
1719 bool reply_return = true;
1720 AutomationProviderDownloadManagerObserver observer;
1721 std::vector<DownloadItem*> downloads;
1722 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1723
1724 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471725 json_return = "{\"error\": \"no download manager\"}";
[email protected]d4adc292010-04-15 18:06:391726 reply_return = false;
1727 } else {
1728 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1729 // since that would be transient; a download might enter and empty out
1730 // the current download queue too soon to be noticed.
1731 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1732 downloads = observer.Downloads();
1733 }
1734
1735 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1736 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1737 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1738 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1739 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1740
1741 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1742 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1743 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1744 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1745 std::string("DANGEROUS_BUT_VALIDATED");
1746
1747 ListValue* list_of_downloads = new ListValue;
1748 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1749 it != downloads.end();
1750 it++) { // Fill info about each download item.
1751 DictionaryValue* dl_item_value = new DictionaryValue;
1752 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1753 dl_item_value->SetString(L"url", (*it)->url().spec());
1754 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1755 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1756 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1757 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1758 dl_item_value->SetBoolean(L"open_when_complete",
1759 (*it)->open_when_complete());
1760 dl_item_value->SetBoolean(L"is_extension_install",
1761 (*it)->is_extension_install());
1762 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1763 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1764 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1765 dl_item_value->SetString(L"safety_state",
1766 safety_state_to_string[(*it)->safety_state()]);
1767 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1768 list_of_downloads->Append(dl_item_value);
1769 }
1770 return_value->Set(L"downloads", list_of_downloads);
1771 base::JSONWriter::Write(return_value.get(), false, &json_return);
1772
1773 AutomationMsg_SendJSONRequest::WriteReplyParams(
1774 reply_message, json_return, reply_return);
1775 Send(reply_message);
1776 // All value objects allocated above are owned by |return_value|
1777 // and get freed by it.
1778}
1779
[email protected]59a611242010-04-02 02:24:041780void AutomationProvider::WaitForDownloadsToComplete(
1781 DictionaryValue* args,
1782 IPC::Message* reply_message) {
1783 std::string json_return;
1784 bool reply_return = true;
1785 AutomationProviderDownloadManagerObserver observer;
1786 std::vector<DownloadItem*> downloads;
1787
1788 // Look for a quick return.
1789 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471790 json_return = "{\"error\": \"no download manager\"}";
[email protected]59a611242010-04-02 02:24:041791 reply_return = false;
1792 } else {
1793 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
1794 FilePath());
1795 downloads = observer.Downloads();
1796 if (downloads.size() == 0) {
1797 json_return = "{}";
1798 }
1799 }
1800 if (!json_return.empty()) {
1801 AutomationMsg_SendJSONRequest::WriteReplyParams(
1802 reply_message, json_return, reply_return);
1803 Send(reply_message);
1804 }
1805
1806 // The observer owns itself. When the last observed item pings, it
1807 // deletes itself.
1808 AutomationProviderDownloadItemObserver* item_observer =
1809 new AutomationProviderDownloadItemObserver(
1810 this, reply_message, downloads.size());
1811 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
1812 i != downloads.end();
1813 i++) {
1814 (*i)->AddObserver(item_observer);
1815 }
1816}
1817
[email protected]24e2b102010-04-29 17:56:471818// Sample json input: { "command": "GetPrefsInfo" }
1819// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
1820void AutomationProvider::GetPrefsInfo(DictionaryValue* args,
1821 IPC::Message* reply_message) {
1822 std::string json_return;
1823 bool reply_return = true;
1824
1825 const PrefService::PreferenceSet& prefs =
1826 profile_->GetPrefs()->preference_set();
1827 DictionaryValue* items = new DictionaryValue;
1828 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
1829 it != prefs.end(); ++it) {
1830 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
1831 }
1832 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1833 return_value->Set(L"prefs", items); // return_value owns items.
1834
1835 base::JSONWriter::Write(return_value.get(), false, &json_return);
1836 AutomationMsg_SendJSONRequest::WriteReplyParams(
1837 reply_message, json_return, reply_return);
1838 Send(reply_message);
1839}
1840
1841// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
1842void AutomationProvider::SetPrefs(DictionaryValue* args,
1843 IPC::Message* reply_message) {
1844 bool reply_return = true;
1845 std::string json_return = "{}";
1846 std::wstring path;
1847 Value* val;
1848 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
1849 PrefService* pref_service = profile_->GetPrefs();
1850 const PrefService::Preference* pref =
1851 pref_service->FindPreference(path.c_str());
1852 if (!pref) { // Not a registered pref.
1853 json_return = "{\"error\": \"pref not registered.\"}";
1854 reply_return = false;
1855 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
1856 json_return = "{\"error\": \"pref is managed. cannot be changed.\"}";
1857 reply_return = false;
1858 } else { // Set the pref.
1859 pref_service->Set(path.c_str(), *val);
1860 }
1861 } else {
1862 json_return = "{\"error\": \"no pref path or value given.\"}";
1863 reply_return = false;
1864 }
1865
1866 AutomationMsg_SendJSONRequest::WriteReplyParams(
1867 reply_message, json_return, reply_return);
1868 Send(reply_message);
1869}
1870
[email protected]f7d48012010-05-06 08:17:051871// Sample json input: { "command": "GetPluginsInfo" }
1872// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
1873void AutomationProvider::GetPluginsInfo(DictionaryValue* args,
1874 IPC::Message* reply_message) {
1875 std::string json_return;
1876 bool reply_return = true;
1877
1878 std::vector<WebPluginInfo> plugins;
1879 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
1880 ListValue* items = new ListValue;
1881 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
1882 it != plugins.end();
1883 ++it) {
1884 DictionaryValue* item = new DictionaryValue;
1885 item->SetString(L"name", it->name);
1886 item->SetString(L"path", it->path.value());
1887 item->SetString(L"version", it->version);
1888 item->SetString(L"desc", it->desc);
1889 item->SetBoolean(L"enabled", it->enabled);
1890 // Add info about mime types.
1891 ListValue* mime_types = new ListValue();
1892 for (std::vector<WebPluginMimeType>::const_iterator type_it =
1893 it->mime_types.begin();
1894 type_it != it->mime_types.end();
1895 ++type_it) {
1896 DictionaryValue* mime_type = new DictionaryValue();
1897 mime_type->SetString(L"mimeType", type_it->mime_type);
1898 mime_type->SetString(L"description", type_it->description);
1899
1900 ListValue* file_extensions = new ListValue();
1901 for (std::vector<std::string>::const_iterator ext_it =
1902 type_it->file_extensions.begin();
1903 ext_it != type_it->file_extensions.end();
1904 ++ext_it) {
1905 file_extensions->Append(new StringValue(*ext_it));
1906 }
1907 mime_type->Set(L"fileExtensions", file_extensions);
1908
1909 mime_types->Append(mime_type);
1910 }
1911 item->Set(L"mimeTypes", mime_types);
1912 items->Append(item);
1913 }
1914 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1915 return_value->Set(L"plugins", items); // return_value owns items.
1916
1917 base::JSONWriter::Write(return_value.get(), false, &json_return);
1918 AutomationMsg_SendJSONRequest::WriteReplyParams(
1919 reply_message, json_return, reply_return);
1920 Send(reply_message);
1921}
1922
1923// Sample json input:
1924// { "command": "EnablePlugin",
1925// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
1926void AutomationProvider::EnablePlugin(DictionaryValue* args,
1927 IPC::Message* reply_message) {
1928 std::string json_return = "{}";
1929 bool reply_return = true;
1930 FilePath::StringType path;
1931 if (!args->GetString(L"path", &path)) {
1932 json_return = "{\"error\": \"path not specified.\"}";
1933 reply_return = false;
1934 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
1935 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
1936 " for path %s.\"}", path.c_str());
1937 reply_return = false;
1938 }
1939
1940 AutomationMsg_SendJSONRequest::WriteReplyParams(
1941 reply_message, json_return, reply_return);
1942 Send(reply_message);
1943}
1944
1945// Sample json input:
1946// { "command": "DisablePlugin",
1947// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
1948void AutomationProvider::DisablePlugin(DictionaryValue* args,
1949 IPC::Message* reply_message) {
1950 std::string json_return = "{}";
1951 bool reply_return = true;
1952 FilePath::StringType path;
1953 if (!args->GetString(L"path", &path)) {
1954 json_return = "{\"error\": \"path not specified.\"}";
1955 reply_return = false;
1956 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
1957 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
1958 " for path %s.\"}", path.c_str());
1959 reply_return = false;
1960 }
1961
1962 AutomationMsg_SendJSONRequest::WriteReplyParams(
1963 reply_message, json_return, reply_return);
1964 Send(reply_message);
1965}
1966
[email protected]59a611242010-04-02 02:24:041967void AutomationProvider::SendJSONRequest(
1968 int handle,
1969 std::string json_request,
1970 IPC::Message* reply_message) {
1971 Browser* browser = NULL;
1972 std::string error_string;
1973 scoped_ptr<Value> values;
1974
1975 // Basic error checking.
1976 if (browser_tracker_->ContainsHandle(handle)) {
1977 browser = browser_tracker_->GetResource(handle);
1978 }
1979 if (!browser) {
1980 error_string = "no browser object";
1981 } else {
1982 base::JSONReader reader;
1983 std::string error;
[email protected]ba399672010-04-06 15:42:391984 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
[email protected]59a611242010-04-02 02:24:041985 if (!error.empty()) {
1986 error_string = error;
1987 }
1988 }
1989
1990 // Make sure input is a dict with a string command.
1991 std::string command;
1992 DictionaryValue* dict_value = NULL;
1993 if (error_string.empty()) {
1994 if (values->GetType() != Value::TYPE_DICTIONARY) {
1995 error_string = "not a dict or no command key in dict";
1996 } else {
1997 // Ownership remains with "values" variable.
1998 dict_value = static_cast<DictionaryValue*>(values.get());
1999 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
2000 error_string = "no command key in dict or not a string command";
2001 }
2002 }
2003 }
2004
[email protected]24e2b102010-04-29 17:56:472005 // Map json commands to their handlers.
2006 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:052007 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
2008 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
2009 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
2010
[email protected]a9ff2c02010-05-13 17:33:052011 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
2012
[email protected]24e2b102010-04-29 17:56:472013 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:292014 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:052015
[email protected]24e2b102010-04-29 17:56:472016 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
2017 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:052018
2019 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:472020 handler_map["WaitForAllDownloadsToComplete"] =
2021 &AutomationProvider::WaitForDownloadsToComplete;
2022
[email protected]59a611242010-04-02 02:24:042023 if (error_string.empty()) {
[email protected]24e2b102010-04-29 17:56:472024 if (handler_map.find(std::string(command)) != handler_map.end()) {
2025 (this->*handler_map[command])(dict_value, reply_message);
[email protected]59a611242010-04-02 02:24:042026 return;
2027 } else {
[email protected]24e2b102010-04-29 17:56:472028 error_string = "Unknown command. Options: ";
2029 for (std::map<std::string, JsonHandler>::const_iterator it =
2030 handler_map.begin(); it != handler_map.end(); ++it) {
2031 error_string += it->first + ", ";
2032 }
[email protected]59a611242010-04-02 02:24:042033 }
2034 }
2035
2036 // If we hit an error, return info.
[email protected]24e2b102010-04-29 17:56:472037 // Return a dict of {"error", "descriptive_string_for_error"}.
[email protected]59a611242010-04-02 02:24:042038 // Else return an empty dict.
2039 std::string json_string;
2040 bool success = true;
2041 if (!error_string.empty()) {
2042 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
2043 dict->SetString(L"error", error_string);
2044 base::JSONWriter::Write(dict.get(), false, &json_string);
2045 success = false;
2046 } else {
2047 json_string = "{}";
2048 }
2049 AutomationMsg_SendJSONRequest::WriteReplyParams(
2050 reply_message, json_string, success);
2051 Send(reply_message);
2052}
2053
initial.commit09911bf2008-07-26 23:55:292054void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562055 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342056 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2057 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562058 DCHECK(reply_message_ == NULL);
2059 reply_message_ = reply_message;
2060
[email protected]d9f9b792009-06-24 13:17:122061 DevToolsManager::GetInstance()->InspectElement(
2062 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292063 } else {
[email protected]71f65dd2009-02-11 19:14:562064 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2065 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292066 }
2067}
2068
2069void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092070 if (reply_message_) {
2071 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2072 num_resources);
2073 Send(reply_message_);
2074 reply_message_ = NULL;
2075 }
initial.commit09911bf2008-07-26 23:55:292076}
2077
[email protected]a7eee32f2009-05-22 18:08:172078class SetProxyConfigTask : public Task {
2079 public:
[email protected]be180c802009-10-23 06:33:312080 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
2081 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:252082 : request_context_getter_(request_context_getter),
2083 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:172084 virtual void Run() {
2085 // First, deserialize the JSON string. If this fails, log and bail.
2086 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:392087 std::string error_msg;
2088 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:172089 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2090 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:392091 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:172092 return;
2093 }
2094
2095 scoped_ptr<DictionaryValue> dict(
2096 static_cast<DictionaryValue*>(root.release()));
2097 // Now put together a proxy configuration from the deserialized string.
2098 net::ProxyConfig pc;
2099 PopulateProxyConfig(*dict.get(), &pc);
2100
[email protected]be180c802009-10-23 06:33:312101 net::ProxyService* proxy_service =
2102 request_context_getter_->GetURLRequestContext()->proxy_service();
2103 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:172104 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2105 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:312106 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:172107 }
2108
2109 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2110 DCHECK(pc);
2111 bool no_proxy = false;
2112 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2113 // Make no changes to the ProxyConfig.
2114 return;
2115 }
2116 bool auto_config;
2117 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:482118 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:172119 }
2120 std::string pac_url;
2121 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:482122 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:172123 }
2124 std::string proxy_bypass_list;
2125 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:482126 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:172127 }
2128 std::string proxy_server;
2129 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:482130 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:172131 }
2132 }
2133
2134 private:
[email protected]be180c802009-10-23 06:33:312135 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:172136 std::string proxy_config_;
2137};
2138
2139
2140void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:312141 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
2142 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:172143 FilePath user_data_dir;
2144 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2145 ProfileManager* profile_manager = g_browser_process->profile_manager();
2146 DCHECK(profile_manager);
2147 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2148 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:312149 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:172150 }
[email protected]be180c802009-10-23 06:33:312151 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:172152
[email protected]fae20792009-10-28 20:31:582153 ChromeThread::PostTask(
2154 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:312155 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:172156}
2157
[email protected]4f3dc372009-02-24 00:10:292158void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:332159 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:292160 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292161 if (tab_tracker_->ContainsHandle(handle)) {
2162 NavigationController* tab = tab_tracker_->GetResource(handle);
2163 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2164 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:332165 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:292166 }
initial.commit09911bf2008-07-26 23:55:292167}
2168
[email protected]6a5670d22009-10-27 16:21:342169void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:142170 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:532171 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
2172 reply_message);
2173}
2174
2175void AutomationProvider::OpenNewBrowserWindowOfType(
2176 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:142177 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292178 // We may have no current browser windows open so don't rely on
2179 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:532180 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
2181 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:052182 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152183 if (show)
[email protected]15952e462008-11-14 00:29:052184 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292185}
2186
[email protected]71f65dd2009-02-11 19:14:562187void AutomationProvider::GetWindowForBrowser(int browser_handle,
2188 bool* success,
2189 int* handle) {
2190 *success = false;
2191 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292192
2193 if (browser_tracker_->ContainsHandle(browser_handle)) {
2194 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202195 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292196 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202197 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562198 *success = true;
initial.commit09911bf2008-07-26 23:55:292199 }
initial.commit09911bf2008-07-26 23:55:292200}
2201
2202void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562203 int browser_handle,
2204 bool* success,
2205 int* autocomplete_edit_handle) {
2206 *success = false;
2207 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292208
2209 if (browser_tracker_->ContainsHandle(browser_handle)) {
2210 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062211 LocationBar* loc_bar = browser->window()->GetLocationBar();
2212 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292213 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562214 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2215 *success = true;
initial.commit09911bf2008-07-26 23:55:292216 }
initial.commit09911bf2008-07-26 23:55:292217}
initial.commit09911bf2008-07-26 23:55:292218
[email protected]71f65dd2009-02-11 19:14:562219void AutomationProvider::ShowInterstitialPage(int tab_handle,
2220 const std::string& html_text,
2221 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292222 if (tab_tracker_->ContainsHandle(tab_handle)) {
2223 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112224 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402225
[email protected]7dad3d5f2010-03-04 00:27:012226 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:402227 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342228 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402229 GURL("about:interstitial"),
2230 html_text);
2231 interstitial->Show();
2232 return;
initial.commit09911bf2008-07-26 23:55:292233 }
[email protected]71f65dd2009-02-11 19:14:562234
[email protected]457f5cf2009-08-18 16:37:522235 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
2236 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562237 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292238}
2239
[email protected]71f65dd2009-02-11 19:14:562240void AutomationProvider::HideInterstitialPage(int tab_handle,
2241 bool* success) {
2242 *success = false;
[email protected]57c6a652009-05-04 07:58:342243 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2244 if (tab_contents && tab_contents->interstitial_page()) {
2245 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562246 *success = true;
initial.commit09911bf2008-07-26 23:55:292247 }
initial.commit09911bf2008-07-26 23:55:292248}
2249
[email protected]71f65dd2009-02-11 19:14:562250void AutomationProvider::CloseTab(int tab_handle,
2251 bool wait_until_closed,
2252 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292253 if (tab_tracker_->ContainsHandle(tab_handle)) {
2254 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2255 int index;
2256 Browser* browser = Browser::GetBrowserForController(controller, &index);
2257 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142258 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112259 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452260 return;
initial.commit09911bf2008-07-26 23:55:292261 }
[email protected]de246f52009-02-25 18:25:452262
2263 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:552264 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292265}
2266
[email protected]71f65dd2009-02-11 19:14:562267void AutomationProvider::CloseBrowser(int browser_handle,
2268 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292269 if (browser_tracker_->ContainsHandle(browser_handle)) {
2270 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562271 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562272 reply_message);
[email protected]f3e99e32008-07-30 04:48:392273 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292274 } else {
2275 NOTREACHED();
2276 }
2277}
2278
[email protected]71f65dd2009-02-11 19:14:562279void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2280 if (browser_tracker_->ContainsHandle(browser_handle)) {
2281 Browser* browser = browser_tracker_->GetResource(browser_handle);
2282 browser->window()->Close();
2283 } else {
2284 NOTREACHED();
2285 }
2286}
2287
[email protected]71f65dd2009-02-11 19:14:562288void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:572289 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:562290 AutomationMsg_NavigationResponseValues* status) {
2291 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:292292
2293 if (tab_tracker_->ContainsHandle(handle)) {
2294 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:572295 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:562296 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:292297 }
initial.commit09911bf2008-07-26 23:55:292298}
2299
[email protected]4150ef02009-08-19 23:14:262300void AutomationProvider::NavigateExternalTabAtIndex(
2301 int handle, int navigation_index,
2302 AutomationMsg_NavigationResponseValues* status) {
2303 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
2304
2305 if (tab_tracker_->ContainsHandle(handle)) {
2306 NavigationController* tab = tab_tracker_->GetResource(handle);
2307 tab->GoToIndex(navigation_index);
2308 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
2309 }
2310}
2311
[email protected]71f65dd2009-02-11 19:14:562312void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2313 IPC::Message* reply_message) {
2314 if (tab_tracker_->ContainsHandle(tab_handle)) {
2315 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2316 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:142317 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:562318 }
2319}
2320
[email protected]71f65dd2009-02-11 19:14:562321void AutomationProvider::GetSecurityState(int handle, bool* success,
2322 SecurityStyle* security_style,
2323 int* ssl_cert_status,
2324 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:292325 if (tab_tracker_->ContainsHandle(handle)) {
2326 NavigationController* tab = tab_tracker_->GetResource(handle);
2327 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562328 *success = true;
2329 *security_style = entry->ssl().security_style();
2330 *ssl_cert_status = entry->ssl().cert_status();
2331 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292332 } else {
[email protected]71f65dd2009-02-11 19:14:562333 *success = false;
2334 *security_style = SECURITY_STYLE_UNKNOWN;
2335 *ssl_cert_status = 0;
2336 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292337 }
2338}
2339
[email protected]71f65dd2009-02-11 19:14:562340void AutomationProvider::GetPageType(int handle, bool* success,
2341 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:292342 if (tab_tracker_->ContainsHandle(handle)) {
2343 NavigationController* tab = tab_tracker_->GetResource(handle);
2344 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562345 *page_type = entry->page_type();
2346 *success = true;
initial.commit09911bf2008-07-26 23:55:292347 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:342348 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:562349 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:342350 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:562351 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292352 } else {
[email protected]71f65dd2009-02-11 19:14:562353 *success = false;
2354 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292355 }
2356}
2357
[email protected]84abba62009-10-07 17:01:442358void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
2359 int* duration_ms) {
2360 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
2361 event_name);
2362}
2363
[email protected]71f65dd2009-02-11 19:14:562364void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
2365 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292366 if (tab_tracker_->ContainsHandle(handle)) {
2367 NavigationController* tab = tab_tracker_->GetResource(handle);
2368 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072369 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:402370 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:472371 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:342372 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:292373 if (ssl_blocking_page) {
2374 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:012375 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:562376 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:292377 return;
2378 }
2379 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:522380 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2381 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:562382 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292383 return;
2384 }
2385 }
2386 }
2387 // We failed.
[email protected]457f5cf2009-08-18 16:37:522388 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2389 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562390 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292391}
2392
[email protected]71f65dd2009-02-11 19:14:562393void AutomationProvider::BringBrowserToFront(int browser_handle,
2394 bool* success) {
initial.commit09911bf2008-07-26 23:55:292395 if (browser_tracker_->ContainsHandle(browser_handle)) {
2396 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:062397 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:562398 *success = true;
initial.commit09911bf2008-07-26 23:55:292399 } else {
[email protected]71f65dd2009-02-11 19:14:562400 *success = false;
initial.commit09911bf2008-07-26 23:55:292401 }
2402}
2403
[email protected]71f65dd2009-02-11 19:14:562404void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
2405 int message_num,
2406 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:292407 if (browser_tracker_->ContainsHandle(browser_handle)) {
2408 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562409 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:142410 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:292411 } else {
[email protected]71f65dd2009-02-11 19:14:562412 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:292413 }
2414}
2415
[email protected]71f65dd2009-02-11 19:14:562416void AutomationProvider::PrintNow(int tab_handle,
2417 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:572418 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:342419 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2420 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:292421 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572422 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:142423 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:342424 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:572425 return;
initial.commit09911bf2008-07-26 23:55:292426 }
[email protected]71f65dd2009-02-11 19:14:562427 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
2428 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292429}
[email protected]d301c952009-07-13 15:02:412430
[email protected]71f65dd2009-02-11 19:14:562431void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:112432 const FilePath& file_name,
2433 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:562434 int type,
2435 bool* success) {
initial.commit09911bf2008-07-26 23:55:292436 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562437 *success = false;
initial.commit09911bf2008-07-26 23:55:292438 return;
2439 }
2440
2441 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2442 Browser* browser = FindAndActivateTab(nav);
2443 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142444 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562445 *success = false;
initial.commit09911bf2008-07-26 23:55:292446 return;
2447 }
2448
initial.commit09911bf2008-07-26 23:55:292449 SavePackage::SavePackageType save_type =
2450 static_cast<SavePackage::SavePackageType>(type);
2451 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2452 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:342453 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292454
[email protected]71f65dd2009-02-11 19:14:562455 *success = true;
initial.commit09911bf2008-07-26 23:55:292456}
2457
[email protected]71f65dd2009-02-11 19:14:562458void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2459 bool* success,
2460 std::wstring* text) {
2461 *success = false;
initial.commit09911bf2008-07-26 23:55:292462 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562463 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522464 GetText();
[email protected]71f65dd2009-02-11 19:14:562465 *success = true;
initial.commit09911bf2008-07-26 23:55:292466 }
initial.commit09911bf2008-07-26 23:55:292467}
2468
[email protected]71f65dd2009-02-11 19:14:562469void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2470 const std::wstring& text,
2471 bool* success) {
2472 *success = false;
initial.commit09911bf2008-07-26 23:55:292473 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522474 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2475 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562476 *success = true;
initial.commit09911bf2008-07-26 23:55:292477 }
initial.commit09911bf2008-07-26 23:55:292478}
2479
2480void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562481 int autocomplete_edit_handle,
2482 bool* success,
2483 std::vector<AutocompleteMatchData>* matches) {
2484 *success = false;
initial.commit09911bf2008-07-26 23:55:292485 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272486 const AutocompleteResult& result = autocomplete_edit_tracker_->
2487 GetResource(autocomplete_edit_handle)->model()->result();
2488 for (AutocompleteResult::const_iterator i = result.begin();
2489 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562490 matches->push_back(AutocompleteMatchData(*i));
2491 *success = true;
initial.commit09911bf2008-07-26 23:55:292492 }
initial.commit09911bf2008-07-26 23:55:292493}
2494
2495void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562496 int autocomplete_edit_handle,
2497 bool* success,
2498 bool* query_in_progress) {
2499 *success = false;
2500 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292501 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562502 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522503 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562504 *success = true;
initial.commit09911bf2008-07-26 23:55:292505 }
initial.commit09911bf2008-07-26 23:55:292506}
2507
[email protected]63514af2010-03-30 17:17:232508#if !defined(OS_MACOSX)
[email protected]28790922009-03-09 19:48:372509void AutomationProvider::OnMessageFromExternalHost(int handle,
2510 const std::string& message,
2511 const std::string& origin,
2512 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:192513 RenderViewHost* view_host = GetViewForTab(handle);
2514 if (!view_host) {
2515 return;
[email protected]fa83e762008-08-15 21:41:392516 }
[email protected]f7a68432009-07-29 23:18:192517
2518 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
2519 view_host, message, origin, target)) {
2520 // Message was diverted.
2521 return;
2522 }
2523
2524 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
2525 origin, target, this, view_host, handle)) {
2526 // Message was diverted.
2527 return;
2528 }
2529
2530 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
2531 // Message was diverted.
2532 return;
2533 }
2534
2535 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:392536}
[email protected]a9024892009-06-16 23:13:552537
2538bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
2539 const std::string& message, const std::string& origin,
2540 const std::string& target) {
2541 if (target !=
2542 extension_automation_constants::kAutomationBrowserEventRequestTarget)
2543 return false;
2544
2545 if (origin != extension_automation_constants::kAutomationOrigin) {
2546 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
2547 return false;
2548 }
2549
2550 // The message is a JSON-encoded array with two elements, both strings. The
2551 // first is the name of the event to dispatch. The second is a JSON-encoding
2552 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:202553 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:552554 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
2555 LOG(WARNING) << "Invalid browser event specified through automation";
2556 return false;
2557 }
2558
2559 const ListValue* args = static_cast<const ListValue*>(message_value.get());
2560
2561 std::string event_name;
2562 if (!args->GetString(0, &event_name)) {
2563 LOG(WARNING) << "No browser event name specified through automation";
2564 return false;
2565 }
2566
2567 std::string json_args;
2568 if (!args->GetString(1, &json_args)) {
2569 LOG(WARNING) << "No browser event args specified through automation";
2570 return false;
2571 }
2572
[email protected]7120f132009-07-20 21:05:372573 if (profile()->GetExtensionMessageService()) {
[email protected]db7331a2010-02-25 22:10:502574 profile()->GetExtensionMessageService()->DispatchEventToRenderers(
[email protected]a807bbe2010-04-14 10:51:192575 event_name, json_args, profile()->IsOffTheRecord(), GURL());
[email protected]7120f132009-07-20 21:05:372576 }
[email protected]a9024892009-06-16 23:13:552577
2578 return true;
2579}
[email protected]5ae5bed2009-08-21 18:52:442580#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:392581
[email protected]57c6a652009-05-04 07:58:342582TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572583 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572584 if (tab_tracker_->ContainsHandle(handle)) {
2585 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342586 if (tab)
2587 *tab = nav_controller;
2588 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572589 }
[email protected]57c6a652009-05-04 07:58:342590 return NULL;
[email protected]20e93d12008-08-28 16:31:572591}
2592
initial.commit09911bf2008-07-26 23:55:292593TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2594 : AutomationProvider(profile) {
2595 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142596 registrar_.Add(this, NotificationType::SESSION_END,
2597 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292598}
2599
2600TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292601 BrowserList::RemoveObserver(this);
2602}
2603
2604void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:142605 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:292606 AutomationProvider::OnChannelError();
2607}
2608
2609void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2610 // For backwards compatibility with the testing automation interface, we
2611 // want the automation provider (and hence the process) to go away when the
2612 // last browser goes away.
2613 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292614 // If you change this, update Observer for NotificationType::SESSION_END
2615 // below.
[email protected]295039bd2008-08-15 04:32:572616 MessageLoop::current()->PostTask(FROM_HERE,
2617 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292618 }
2619}
2620
2621void TestingAutomationProvider::Observe(NotificationType type,
2622 const NotificationSource& source,
2623 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562624 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292625 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2626 // before the task runs resulting in this object not being deleted. This
2627 // Release balance out the Release scheduled by OnBrowserRemoving.
2628 Release();
2629}
[email protected]295039bd2008-08-15 04:32:572630
2631void TestingAutomationProvider::OnRemoveProvider() {
2632 AutomationProviderList::GetInstance()->RemoveProvider(this);
2633}
[email protected]8a3422c92008-09-24 17:42:422634
[email protected]816633a2009-11-11 21:48:182635void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:562636 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422637 if (tab_tracker_->ContainsHandle(handle)) {
2638 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342639 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112640 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422641 }
[email protected]8a3422c92008-09-24 17:42:422642}
2643
[email protected]816633a2009-11-11 21:48:182644void AutomationProvider::ClickInfoBarAccept(int handle,
2645 int info_bar_index,
2646 bool wait_for_navigation,
2647 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422648 bool success = false;
2649 if (tab_tracker_->ContainsHandle(handle)) {
2650 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2651 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112652 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422653 if (info_bar_index >= 0 && info_bar_index < count) {
2654 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:012655 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:422656 }
[email protected]eb9ba192008-12-02 02:41:342657 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112658 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342659 info_bar_index);
2660 if (delegate->AsConfirmInfoBarDelegate())
2661 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422662 success = true;
2663 }
2664 }
[email protected]4f3dc372009-02-24 00:10:292665 }
[email protected]58f622a62009-10-04 01:17:552666
2667 // This "!wait_for_navigation || !success condition" logic looks suspicious.
2668 // It will send a failure message when success is true but
2669 // |wait_for_navigation| is false.
2670 // TODO(phajdan.jr): investgate whether the reply param (currently
2671 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:422672 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:182673 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:522674 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:422675}
2676
[email protected]71f65dd2009-02-11 19:14:562677void AutomationProvider::GetLastNavigationTime(int handle,
2678 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422679 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562680 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422681}
2682
[email protected]71f65dd2009-02-11 19:14:562683void AutomationProvider::WaitForNavigation(int handle,
2684 int64 last_navigation_time,
2685 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:252686 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:422687 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:252688
[email protected]8a3422c92008-09-24 17:42:422689 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562690 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:522691 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
2692 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:552693 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:292694 return;
[email protected]8a3422c92008-09-24 17:42:422695 }
2696
[email protected]7dad3d5f2010-03-04 00:27:012697 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:422698}
2699
[email protected]71f65dd2009-02-11 19:14:562700void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162701 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562702 int value,
2703 bool* success) {
2704 *success = false;
[email protected]8a3422c92008-09-24 17:42:422705 if (browser_tracker_->ContainsHandle(handle)) {
2706 Browser* browser = browser_tracker_->GetResource(handle);
2707 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562708 *success = true;
[email protected]8a3422c92008-09-24 17:42:422709 }
[email protected]8a3422c92008-09-24 17:42:422710}
[email protected]97fa6ce32008-12-19 01:48:162711
[email protected]71f65dd2009-02-11 19:14:562712void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162713 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562714 const std::wstring& value,
2715 bool* success) {
2716 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162717 if (browser_tracker_->ContainsHandle(handle)) {
2718 Browser* browser = browser_tracker_->GetResource(handle);
2719 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562720 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162721 }
[email protected]97fa6ce32008-12-19 01:48:162722}
2723
[email protected]71f65dd2009-02-11 19:14:562724void AutomationProvider::GetBooleanPreference(int handle,
2725 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:362726 bool* success,
2727 bool* value) {
[email protected]71f65dd2009-02-11 19:14:562728 *success = false;
2729 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162730 if (browser_tracker_->ContainsHandle(handle)) {
2731 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562732 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2733 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162734 }
[email protected]97fa6ce32008-12-19 01:48:162735}
2736
[email protected]71f65dd2009-02-11 19:14:562737void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162738 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562739 bool value,
2740 bool* success) {
2741 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162742 if (browser_tracker_->ContainsHandle(handle)) {
2743 Browser* browser = browser_tracker_->GetResource(handle);
2744 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562745 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162746 }
[email protected]97fa6ce32008-12-19 01:48:162747}
2748
2749// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562750void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:402751 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162752 if (tab_tracker_->ContainsHandle(tab_handle)) {
2753 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2754 Browser* browser = FindAndActivateTab(nav);
2755 DCHECK(browser);
2756
[email protected]57c6a652009-05-04 07:58:342757 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2758 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162759 }
[email protected]97fa6ce32008-12-19 01:48:162760}
2761
[email protected]b8f48d12009-11-09 20:14:362762// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562763void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402764 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562765 bool* success) {
2766 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162767 if (tab_tracker_->ContainsHandle(tab_handle)) {
2768 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:192769 if (!nav)
2770 return;
[email protected]97fa6ce32008-12-19 01:48:162771 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:162772
[email protected]2f2afba2010-04-01 01:53:192773 // If the browser has UI, simulate what a user would do.
2774 // Activate the tab and then click the encoding menu.
2775 if (browser &&
2776 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162777 int selected_encoding_id =
2778 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2779 if (selected_encoding_id) {
2780 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562781 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162782 }
[email protected]2f2afba2010-04-01 01:53:192783 } else {
2784 // There is no UI, Chrome probably runs as Chrome-Frame mode.
2785 // Try to get TabContents and call its override_encoding method.
2786 TabContents* contents = nav->tab_contents();
2787 if (!contents)
2788 return;
2789 const std::string selected_encoding =
2790 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
2791 if (selected_encoding.empty())
2792 return;
2793 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:162794 }
2795 }
[email protected]97fa6ce32008-12-19 01:48:162796}
[email protected]5bcdb312009-01-07 21:43:202797
[email protected]4d434a1a2009-02-11 21:06:572798void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202799 SavePackage::SetShouldPromptUser(should_prompt);
2800}
[email protected]87eab222009-03-13 00:47:452801
[email protected]66ba4932009-06-04 19:22:132802void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2803 *count = -1; // -1 is the error code
2804 if (tab_tracker_->ContainsHandle(handle)) {
2805 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2806 TabContents* tab_contents = nav_controller->tab_contents();
2807 if (tab_contents) {
2808 BlockedPopupContainer* container =
2809 tab_contents->blocked_popup_container();
2810 if (container) {
2811 *count = static_cast<int>(container->GetBlockedPopupCount());
2812 } else {
2813 // If we don't have a container, we don't have any blocked popups to
2814 // contain!
2815 *count = 0;
2816 }
2817 }
2818 }
2819}
[email protected]f7a68432009-07-29 23:18:192820
2821void AutomationProvider::SelectAll(int tab_handle) {
2822 RenderViewHost* view = GetViewForTab(tab_handle);
2823 if (!view) {
2824 NOTREACHED();
2825 return;
2826 }
2827
2828 view->SelectAll();
2829}
2830
2831void AutomationProvider::Cut(int tab_handle) {
2832 RenderViewHost* view = GetViewForTab(tab_handle);
2833 if (!view) {
2834 NOTREACHED();
2835 return;
2836 }
2837
2838 view->Cut();
2839}
2840
2841void AutomationProvider::Copy(int tab_handle) {
2842 RenderViewHost* view = GetViewForTab(tab_handle);
2843 if (!view) {
2844 NOTREACHED();
2845 return;
2846 }
2847
2848 view->Copy();
2849}
2850
2851void AutomationProvider::Paste(int tab_handle) {
2852 RenderViewHost* view = GetViewForTab(tab_handle);
2853 if (!view) {
2854 NOTREACHED();
2855 return;
2856 }
2857
2858 view->Paste();
2859}
2860
2861void AutomationProvider::ReloadAsync(int tab_handle) {
2862 if (tab_tracker_->ContainsHandle(tab_handle)) {
2863 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2864 if (!tab) {
2865 NOTREACHED();
2866 return;
2867 }
2868
[email protected]106a0812010-03-18 00:15:122869 const bool check_for_repost = true;
2870 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:192871 }
2872}
2873
2874void AutomationProvider::StopAsync(int tab_handle) {
2875 RenderViewHost* view = GetViewForTab(tab_handle);
2876 if (!view) {
[email protected]8b2b3312009-09-14 18:38:362877 // We tolerate StopAsync being called even before a view has been created.
2878 // So just log a warning instead of a NOTREACHED().
2879 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:192880 return;
2881 }
2882
2883 view->Stop();
2884}
2885
[email protected]1bb5f892009-10-06 01:44:572886void AutomationProvider::OnSetPageFontSize(int tab_handle,
2887 int font_size) {
2888 AutomationPageFontSize automation_font_size =
2889 static_cast<AutomationPageFontSize>(font_size);
2890
2891 if (automation_font_size < SMALLEST_FONT ||
2892 automation_font_size > LARGEST_FONT) {
2893 DLOG(ERROR) << "Invalid font size specified : "
2894 << font_size;
2895 return;
2896 }
2897
2898 if (tab_tracker_->ContainsHandle(tab_handle)) {
2899 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2900 DCHECK(tab != NULL);
2901 if (tab && tab->tab_contents()) {
2902 DCHECK(tab->tab_contents()->profile() != NULL);
2903 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2904 prefs::kWebKitDefaultFontSize, font_size);
2905 }
2906 }
2907}
2908
[email protected]bc73b4e52010-03-26 04:16:202909void AutomationProvider::RemoveBrowsingData(int remove_mask) {
2910 BrowsingDataRemover* remover;
2911 remover = new BrowsingDataRemover(profile(),
2912 BrowsingDataRemover::EVERYTHING, // All time periods.
2913 base::Time());
2914 remover->Remove(remove_mask);
2915 // BrowsingDataRemover deletes itself.
2916}
[email protected]1bb5f892009-10-06 01:44:572917
[email protected]2949e90d2009-08-21 15:32:522918void AutomationProvider::WaitForBrowserWindowCountToBecome(
2919 int target_count, IPC::Message* reply_message) {
2920 if (static_cast<int>(BrowserList::size()) == target_count) {
2921 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2922 reply_message, true);
2923 Send(reply_message);
2924 return;
2925 }
2926
2927 // Set up an observer (it will delete itself).
2928 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2929}
2930
2931void AutomationProvider::WaitForAppModalDialogToBeShown(
2932 IPC::Message* reply_message) {
2933 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2934 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2935 reply_message, true);
2936 Send(reply_message);
2937 return;
2938 }
2939
2940 // Set up an observer (it will delete itself).
2941 new AppModalDialogShownObserver(this, reply_message);
2942}
2943
[email protected]1126a1d32009-08-26 15:39:262944void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2945 int handle, int number_of_navigations, IPC::Message* reply_message) {
2946 if (tab_tracker_->ContainsHandle(handle)) {
2947 NavigationController* tab = tab_tracker_->GetResource(handle);
2948 Browser* browser = FindAndActivateTab(tab);
2949 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:012950 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2951 false);
[email protected]1126a1d32009-08-26 15:39:262952 browser->GoBack(CURRENT_TAB);
2953 return;
2954 }
2955 }
2956
2957 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2958 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2959 Send(reply_message);
2960}
2961
2962void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2963 int handle, int number_of_navigations, IPC::Message* reply_message) {
2964 if (tab_tracker_->ContainsHandle(handle)) {
2965 NavigationController* tab = tab_tracker_->GetResource(handle);
2966 Browser* browser = FindAndActivateTab(tab);
2967 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:012968 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2969 false);
[email protected]1126a1d32009-08-26 15:39:262970 browser->GoForward(CURRENT_TAB);
2971 return;
2972 }
2973 }
2974
2975 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2976 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2977 Send(reply_message);
2978}
2979
[email protected]f7a68432009-07-29 23:18:192980RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2981 if (tab_tracker_->ContainsHandle(tab_handle)) {
2982 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2983 if (!tab) {
2984 NOTREACHED();
2985 return NULL;
2986 }
2987
2988 TabContents* tab_contents = tab->tab_contents();
2989 if (!tab_contents) {
2990 NOTREACHED();
2991 return NULL;
2992 }
2993
2994 RenderViewHost* view_host = tab_contents->render_view_host();
2995 return view_host;
2996 }
2997
2998 return NULL;
2999}
[email protected]675595f2009-08-26 22:32:043000
3001void AutomationProvider::GetBrowserForWindow(int window_handle,
3002 bool* success,
3003 int* browser_handle) {
3004 *success = false;
3005 *browser_handle = 0;
3006
3007 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
3008 if (!window)
3009 return;
3010
3011 BrowserList::const_iterator iter = BrowserList::begin();
3012 for (;iter != BrowserList::end(); ++iter) {
3013 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
3014 if (window == this_window) {
3015 // Add() returns the existing handle for the resource if any.
3016 *browser_handle = browser_tracker_->Add(*iter);
3017 *success = true;
3018 return;
3019 }
3020 }
3021}
[email protected]d11c8e92009-10-20 23:26:403022
3023void AutomationProvider::InstallExtension(const FilePath& crx_path,
3024 IPC::Message* reply_message) {
3025 ExtensionsService* service = profile_->GetExtensionsService();
3026 if (service) {
3027 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193028 new ExtensionInstallNotificationObserver(this,
3029 AutomationMsg_InstallExtension::ID,
3030 reply_message);
[email protected]d11c8e92009-10-20 23:26:403031
3032 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:163033 scoped_refptr<CrxInstaller> installer(
3034 new CrxInstaller(install_dir,
3035 service,
3036 NULL)); // silent install, no UI
3037 installer->set_allow_privilege_increase(true);
3038 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:403039 } else {
3040 AutomationMsg_InstallExtension::WriteReplyParams(
3041 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3042 Send(reply_message);
3043 }
3044}
3045
3046void AutomationProvider::LoadExpandedExtension(
3047 const FilePath& extension_dir,
3048 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:383049 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:403050 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193051 new ExtensionInstallNotificationObserver(
3052 this,
3053 AutomationMsg_LoadExpandedExtension::ID,
3054 reply_message);
[email protected]d11c8e92009-10-20 23:26:403055
3056 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:403057 } else {
3058 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
3059 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3060 Send(reply_message);
3061 }
3062}
[email protected]673fd2c02010-02-04 23:10:003063
[email protected]a1e62d12010-03-16 02:18:433064void AutomationProvider::GetEnabledExtensions(
3065 std::vector<FilePath>* result) {
3066 ExtensionsService* service = profile_->GetExtensionsService();
3067 DCHECK(service);
3068 if (service->extensions_enabled()) {
3069 const ExtensionList* extensions = service->extensions();
3070 DCHECK(extensions);
3071 for (size_t i = 0; i < extensions->size(); ++i) {
3072 Extension* extension = (*extensions)[i];
3073 DCHECK(extension);
[email protected]237f281672010-03-20 12:37:073074 if (extension->location() == Extension::INTERNAL) {
3075 result->push_back(extension->path());
3076 }
[email protected]a1e62d12010-03-16 02:18:433077 }
3078 }
3079}
3080
[email protected]790788ac2010-04-06 17:52:193081void AutomationProvider::WaitForExtensionTestResult(
3082 IPC::Message* reply_message) {
3083 DCHECK(reply_message_ == NULL);
3084 reply_message_ = reply_message;
3085 // Call MaybeSendResult, because the result might have come in before
3086 // we were waiting on it.
3087 extension_test_result_observer_->MaybeSendResult();
3088}
3089
3090void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:093091 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:193092 ExtensionsService* service = profile_->GetExtensionsService();
3093 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3094 if (service && manager) {
3095 // The observer will delete itself when done.
3096 new ExtensionReadyNotificationObserver(
3097 manager,
3098 this,
3099 AutomationMsg_InstallExtensionAndGetHandle::ID,
3100 reply_message);
3101
[email protected]d7e5525d2010-04-20 14:37:093102 ExtensionInstallUI* client =
3103 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:193104 scoped_refptr<CrxInstaller> installer(
3105 new CrxInstaller(service->install_directory(),
3106 service,
[email protected]d7e5525d2010-04-20 14:37:093107 client));
[email protected]790788ac2010-04-06 17:52:193108 installer->set_allow_privilege_increase(true);
3109 installer->InstallCrx(crx_path);
3110 } else {
3111 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
3112 reply_message, 0);
3113 Send(reply_message);
3114 }
3115}
3116
3117void AutomationProvider::UninstallExtension(int extension_handle,
3118 bool* success) {
3119 *success = false;
3120 Extension* extension = GetExtension(extension_handle);
3121 ExtensionsService* service = profile_->GetExtensionsService();
3122 if (extension && service) {
3123 ExtensionUnloadNotificationObserver observer;
3124 service->UninstallExtension(extension->id(), false);
3125 // The extension unload notification should have been sent synchronously
3126 // with the uninstall. Just to be safe, check that it was received.
3127 *success = observer.did_receive_unload_notification();
3128 }
3129}
3130
3131void AutomationProvider::EnableExtension(int extension_handle,
3132 IPC::Message* reply_message) {
3133 Extension* extension = GetDisabledExtension(extension_handle);
3134 ExtensionsService* service = profile_->GetExtensionsService();
3135 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3136 // Only enable if this extension is disabled.
3137 if (extension && service && manager) {
3138 // The observer will delete itself when done.
3139 new ExtensionReadyNotificationObserver(
3140 manager,
3141 this,
3142 AutomationMsg_EnableExtension::ID,
3143 reply_message);
3144 service->EnableExtension(extension->id());
3145 } else {
3146 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
3147 Send(reply_message);
3148 }
3149}
3150
3151void AutomationProvider::DisableExtension(int extension_handle,
3152 bool* success) {
3153 *success = false;
3154 Extension* extension = GetEnabledExtension(extension_handle);
3155 ExtensionsService* service = profile_->GetExtensionsService();
3156 if (extension && service) {
3157 ExtensionUnloadNotificationObserver observer;
3158 service->DisableExtension(extension->id());
3159 // The extension unload notification should have been sent synchronously
3160 // with the disable. Just to be safe, check that it was received.
3161 *success = observer.did_receive_unload_notification();
3162 }
3163}
3164
3165void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
3166 int extension_handle, int browser_handle,
3167 IPC::Message* reply_message) {
3168 bool success = false;
3169 Extension* extension = GetEnabledExtension(extension_handle);
3170 ExtensionsService* service = profile_->GetExtensionsService();
3171 ExtensionMessageService* message_service =
3172 profile_->GetExtensionMessageService();
3173 Browser* browser = browser_tracker_->GetResource(browser_handle);
3174 if (extension && service && message_service && browser) {
3175 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
3176 if (extension->page_action()) {
3177 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
3178 browser->profile(), extension->id(), "action", tab_id, "", 1);
3179 success = true;
3180 } else if (extension->browser_action()) {
3181 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
3182 browser->profile(), extension->id(), browser);
3183 success = true;
3184 }
3185 }
3186 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3187 reply_message, success);
3188 Send(reply_message);
3189}
3190
3191void AutomationProvider::MoveExtensionBrowserAction(
3192 int extension_handle, int index, bool* success) {
3193 *success = false;
3194 Extension* extension = GetEnabledExtension(extension_handle);
3195 ExtensionsService* service = profile_->GetExtensionsService();
3196 if (extension && service) {
3197 ExtensionToolbarModel* toolbar = service->toolbar_model();
3198 if (toolbar) {
3199 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3200 toolbar->MoveBrowserAction(extension, index);
3201 *success = true;
3202 } else {
3203 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3204 }
3205 }
3206 }
3207}
3208
3209void AutomationProvider::GetExtensionProperty(
3210 int extension_handle,
3211 AutomationMsg_ExtensionProperty type,
3212 bool* success,
3213 std::string* value) {
3214 *success = false;
3215 Extension* extension = GetExtension(extension_handle);
3216 ExtensionsService* service = profile_->GetExtensionsService();
3217 if (extension && service) {
3218 ExtensionToolbarModel* toolbar = service->toolbar_model();
3219 int found_index = -1;
3220 int index = 0;
3221 switch (type) {
3222 case AUTOMATION_MSG_EXTENSION_ID:
3223 *value = extension->id();
3224 *success = true;
3225 break;
3226 case AUTOMATION_MSG_EXTENSION_NAME:
3227 *value = extension->name();
3228 *success = true;
3229 break;
3230 case AUTOMATION_MSG_EXTENSION_VERSION:
3231 *value = extension->VersionString();
3232 *success = true;
3233 break;
3234 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3235 if (toolbar) {
3236 for (ExtensionList::const_iterator iter = toolbar->begin();
3237 iter != toolbar->end(); iter++) {
3238 // Skip this extension if we are in incognito mode
3239 // and it is not incognito-enabled.
3240 if (profile_->IsOffTheRecord() &&
3241 !service->IsIncognitoEnabled(*iter))
3242 continue;
3243 if (*iter == extension) {
3244 found_index = index;
3245 break;
3246 }
3247 index++;
3248 }
3249 *value = IntToString(found_index);
3250 *success = true;
3251 }
3252 break;
3253 default:
3254 LOG(WARNING) << "Trying to get undefined extension property";
3255 break;
3256 }
3257 }
3258}
3259
[email protected]673fd2c02010-02-04 23:10:003260void AutomationProvider::SaveAsAsync(int tab_handle) {
3261 NavigationController* tab = NULL;
3262 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3263 if (tab_contents)
3264 tab_contents->OnSavePage();
3265}
[email protected]7dad3d5f2010-03-04 00:27:013266
3267void AutomationProvider::SetContentSetting(
3268 int handle,
3269 const std::string& host,
3270 ContentSettingsType content_type,
3271 ContentSetting setting,
3272 bool* success) {
3273 *success = false;
3274 if (browser_tracker_->ContainsHandle(handle)) {
3275 Browser* browser = browser_tracker_->GetResource(handle);
3276 HostContentSettingsMap* map =
3277 browser->profile()->GetHostContentSettingsMap();
3278 if (host.empty()) {
3279 map->SetDefaultContentSetting(content_type, setting);
3280 } else {
[email protected]0314ae02010-04-08 09:18:293281 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3282 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013283 }
3284 *success = true;
3285 }
3286}
[email protected]cc824372010-03-31 15:33:013287
3288#if !defined(TOOLKIT_VIEWS)
3289void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3290 NOTIMPLEMENTED();
3291};
3292
3293void AutomationProvider::WaitForFocusedViewIDToChange(
3294 int handle, int previous_view_id, IPC::Message* reply_message) {
3295 NOTIMPLEMENTED();
3296}
3297
3298void AutomationProvider::StartTrackingPopupMenus(
3299 int browser_handle, bool* success) {
3300 NOTIMPLEMENTED();
3301}
3302
3303void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3304 NOTIMPLEMENTED();
3305}
3306#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:093307
3308void AutomationProvider::ResetToDefaultTheme() {
3309 profile_->ClearTheme();
3310}