blob: 3dccaaaa9806093704a49f1270f30631ab832d81 [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]790788ac2010-04-06 17:52:1928#include "chrome/browser/automation/automation_extension_tracker.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0830#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]f44265b2009-05-19 18:52:5031#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1332#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5333#include "chrome/browser/bookmarks/bookmark_model.h"
34#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]ef413ca2010-05-25 21:09:1435#include "chrome/browser/browser_list.h"
[email protected]5c238752009-06-13 10:29:0736#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3937#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2038#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0539#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5840#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2941#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1242#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5943#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2244#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0545#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4046#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1947#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]ef413ca2010-05-25 21:09:1448#include "chrome/browser/extensions/extension_host.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]4bdde602010-06-16 03:17:3592#include "chrome/browser/external_tab_container_win.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]71f65dd2009-02-11 19:14:56400 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14401 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56402 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14403 HideInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56404 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
405 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14406 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
407 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44408 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
409 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56410 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
411 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29412 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
413 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
414 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56415 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41416 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56417 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
418 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29419 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56420 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29421 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56422 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29423 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56424 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29425 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56426 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45427 HandleOpenFindInPageRequest)
[email protected]1c58a5c2009-05-21 18:47:14428 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56429 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57430 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56431 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57432 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56433 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
434 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53435 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
436 GetBookmarksAsJSON)
437 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
438 WaitForBookmarkModelToLoad)
439 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
440 AddBookmarkGroup)
441 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
442 AddBookmarkURL)
443 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
444 ReparentBookmark)
445 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
446 SetBookmarkTitle)
447 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
448 SetBookmarkURL)
449 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
450 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04451 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
452 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18453 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
454 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
455 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56456 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42457 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56458 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
459 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14460 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56461 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44462 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56463 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20464 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14465 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56466 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16467 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56468 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16469 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56470 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16471 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14472 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20473 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
474 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14475 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]59560e0b2009-06-04 03:30:22476 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13477 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19478 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
479 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
480 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
481 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
482 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
483 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52484 IPC_MESSAGE_HANDLER_DELAY_REPLY(
485 AutomationMsg_WaitForBrowserWindowCountToBecome,
486 WaitForBrowserWindowCountToBecome)
487 IPC_MESSAGE_HANDLER_DELAY_REPLY(
488 AutomationMsg_WaitForAppModalDialogToBeShown,
489 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26490 IPC_MESSAGE_HANDLER_DELAY_REPLY(
491 AutomationMsg_GoBackBlockUntilNavigationsComplete,
492 GoBackBlockUntilNavigationsComplete)
493 IPC_MESSAGE_HANDLER_DELAY_REPLY(
494 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
495 GoForwardBlockUntilNavigationsComplete)
[email protected]1bb5f892009-10-06 01:44:57496 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40497 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
498 InstallExtension)
499 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
500 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43501 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
502 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19503 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
504 WaitForExtensionTestResult)
505 IPC_MESSAGE_HANDLER_DELAY_REPLY(
506 AutomationMsg_InstallExtensionAndGetHandle,
507 InstallExtensionAndGetHandle)
508 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
509 UninstallExtension)
510 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
511 EnableExtension)
512 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
513 DisableExtension)
514 IPC_MESSAGE_HANDLER_DELAY_REPLY(
515 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
516 ExecuteExtensionActionInActiveTabAsync)
517 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
518 MoveExtensionBrowserAction)
519 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
520 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57521 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
522 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00523 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]7dad3d5f2010-03-04 00:27:01524 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]bc73b4e52010-03-26 04:16:20525 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]bdd5a9c92010-06-14 18:21:00526 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
[email protected]cc824372010-03-31 15:33:01527#if defined(TOOLKIT_VIEWS)
528 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
529 WaitForFocusedViewIDToChange)
530 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
531 StartTrackingPopupMenus)
532 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
533 WaitForPopupMenuToOpen)
[email protected]bdd5a9c92010-06-14 18:21:00534#endif // defined(TOOLKIT_VIEWS)
[email protected]52415f842010-06-10 21:51:52535#if defined(OS_WIN)
536 // These are for use with external tabs.
537 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
538 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
539 ProcessUnhandledAccelerator)
540 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
541 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
542 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
543 OnForwardContextMenuCommandToChrome)
544 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
545 NavigateInExternalTab)
546 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
547 NavigateExternalTabAtIndex)
548 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
549 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
550 SetEnableExtensionAutomation)
551 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
552 OnMessageFromExternalHost)
[email protected]bdd5a9c92010-06-14 18:21:00553 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
554#endif // defined(OS_WIN)
555#if defined(OS_CHROMEOS)
556 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
557 LoginWithUserAndPass)
558#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29559 IPC_END_MESSAGE_MAP()
560}
561
[email protected]71f65dd2009-02-11 19:14:56562void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
563 *status = -1;
initial.commit09911bf2008-07-26 23:55:29564 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
565 Browser* browser = browser_tracker_->GetResource(handle);
566 if (at_index >= 0 && at_index < browser->tab_count()) {
567 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56568 *status = 0;
initial.commit09911bf2008-07-26 23:55:29569 }
570 }
initial.commit09911bf2008-07-26 23:55:29571}
572
[email protected]71f65dd2009-02-11 19:14:56573void AutomationProvider::AppendTab(int handle, const GURL& url,
574 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29575 int append_tab_response = -1; // -1 is the error code
576 NotificationObserver* observer = NULL;
577
578 if (browser_tracker_->ContainsHandle(handle)) {
579 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14580 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38581 TabContents* tab_contents = browser->AddTabWithURL(
582 url, GURL(), PageTransition::TYPED, -1, Browser::ADD_SELECTED, NULL,
583 std::string());
initial.commit09911bf2008-07-26 23:55:29584 if (tab_contents) {
585 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57586 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29587 }
588 }
589
590 if (append_tab_response < 0) {
591 // The append tab failed. Remove the TabStripObserver
592 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17593 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29594 delete observer;
595 }
596
[email protected]71f65dd2009-02-11 19:14:56597 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
598 append_tab_response);
599 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29600 }
601}
602
[email protected]71f65dd2009-02-11 19:14:56603void AutomationProvider::NavigateToURL(int handle, const GURL& url,
604 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58605 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
606}
607
608void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
609 int handle, const GURL& url, int number_of_navigations,
610 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29611 if (tab_tracker_->ContainsHandle(handle)) {
612 NavigationController* tab = tab_tracker_->GetResource(handle);
613
614 // Simulate what a user would do. Activate the tab and then navigate.
615 // We could allow navigating in a background tab in future.
616 Browser* browser = FindAndActivateTab(tab);
617
618 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01619 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
620 false);
[email protected]71f65dd2009-02-11 19:14:56621
initial.commit09911bf2008-07-26 23:55:29622 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50623 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29624 return;
625 }
626 }
[email protected]71f65dd2009-02-11 19:14:56627
628 AutomationMsg_NavigateToURL::WriteReplyParams(
629 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
630 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29631}
[email protected]2949e90d2009-08-21 15:32:52632
[email protected]c70f9b82010-04-21 07:31:11633void AutomationProvider::NavigationAsync(int handle,
634 const GURL& url,
635 bool* status) {
636 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
637}
638
639void AutomationProvider::NavigationAsyncWithDisposition(
640 int handle,
641 const GURL& url,
642 WindowOpenDisposition disposition,
643 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56644 *status = false;
initial.commit09911bf2008-07-26 23:55:29645
646 if (tab_tracker_->ContainsHandle(handle)) {
647 NavigationController* tab = tab_tracker_->GetResource(handle);
648
649 // Simulate what a user would do. Activate the tab and then navigate.
650 // We could allow navigating in a background tab in future.
651 Browser* browser = FindAndActivateTab(tab);
652
653 if (browser) {
654 // Don't add any listener unless a callback mechanism is desired.
655 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11656 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56657 *status = true;
initial.commit09911bf2008-07-26 23:55:29658 }
659 }
initial.commit09911bf2008-07-26 23:55:29660}
661
[email protected]71f65dd2009-02-11 19:14:56662void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29663 if (tab_tracker_->ContainsHandle(handle)) {
664 NavigationController* tab = tab_tracker_->GetResource(handle);
665 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14666 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01667 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29668 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29669 return;
670 }
671 }
[email protected]71f65dd2009-02-11 19:14:56672
673 AutomationMsg_GoBack::WriteReplyParams(
674 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
675 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29676}
677
[email protected]71f65dd2009-02-11 19:14:56678void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29679 if (tab_tracker_->ContainsHandle(handle)) {
680 NavigationController* tab = tab_tracker_->GetResource(handle);
681 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14682 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01683 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29684 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29685 return;
686 }
687 }
[email protected]71f65dd2009-02-11 19:14:56688
689 AutomationMsg_GoForward::WriteReplyParams(
690 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
691 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29692}
693
[email protected]71f65dd2009-02-11 19:14:56694void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29695 if (tab_tracker_->ContainsHandle(handle)) {
696 NavigationController* tab = tab_tracker_->GetResource(handle);
697 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14698 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01699 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]cb84d642010-06-10 00:56:28700 browser->Reload(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29701 return;
702 }
703 }
[email protected]71f65dd2009-02-11 19:14:56704
705 AutomationMsg_Reload::WriteReplyParams(
706 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
707 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29708}
709
[email protected]71f65dd2009-02-11 19:14:56710void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29711 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56712 const std::wstring& password,
713 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29714 if (tab_tracker_->ContainsHandle(tab_handle)) {
715 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
716 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
717
718 if (iter != login_handler_map_.end()) {
719 // If auth is needed again after this, assume login has failed. This is
720 // not strictly correct, because a navigation can require both proxy and
721 // server auth, but it should be OK for now.
722 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01723 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29724 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52725 return;
initial.commit09911bf2008-07-26 23:55:29726 }
727 }
[email protected]de246f52009-02-25 18:25:45728
[email protected]457f5cf2009-08-18 16:37:52729 AutomationMsg_SetAuth::WriteReplyParams(
730 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
731 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29732}
733
[email protected]71f65dd2009-02-11 19:14:56734void AutomationProvider::CancelAuth(int tab_handle,
735 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29736 if (tab_tracker_->ContainsHandle(tab_handle)) {
737 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
738 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
739
740 if (iter != login_handler_map_.end()) {
741 // If auth is needed again after this, something is screwy.
742 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01743 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29744 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52745 return;
initial.commit09911bf2008-07-26 23:55:29746 }
747 }
[email protected]de246f52009-02-25 18:25:45748
[email protected]457f5cf2009-08-18 16:37:52749 AutomationMsg_CancelAuth::WriteReplyParams(
750 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
751 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29752}
753
[email protected]71f65dd2009-02-11 19:14:56754void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
755 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29756
757 if (tab_tracker_->ContainsHandle(tab_handle)) {
758 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
759 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
760
761 if (iter != login_handler_map_.end()) {
762 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56763 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29764 }
765 }
initial.commit09911bf2008-07-26 23:55:29766}
767
[email protected]71f65dd2009-02-11 19:14:56768void AutomationProvider::GetRedirectsFrom(int tab_handle,
769 const GURL& source_url,
770 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29771 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
772 if (tab_tracker_->ContainsHandle(tab_handle)) {
773 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
774 HistoryService* history_service =
775 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
776
777 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
778 "has no history service";
779 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56780 DCHECK(reply_message_ == NULL);
781 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29782 // Schedule a history query for redirects. The response will be sent
783 // asynchronously from the callback the history system uses to notify us
784 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29785 redirect_query_ = history_service->QueryRedirectsFrom(
786 source_url, &consumer_,
787 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
788 return; // Response will be sent when query completes.
789 }
790 }
791
792 // Send failure response.
[email protected]deb57402009-02-06 01:35:30793 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56794 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
795 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29796}
797
[email protected]71f65dd2009-02-11 19:14:56798void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
799 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29800 if (browser_tracker_->ContainsHandle(handle)) {
801 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56802 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29803 }
initial.commit09911bf2008-07-26 23:55:29804}
805
[email protected]202e7a72009-06-15 03:48:36806void AutomationProvider::GetBrowserLocale(string16* locale) {
807 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06808 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36809}
810
[email protected]71f65dd2009-02-11 19:14:56811void AutomationProvider::GetBrowserWindowCount(int* window_count) {
812 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29813}
814
[email protected]24497032009-05-01 17:00:29815void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
816 *window_count = static_cast<int>(
817 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
818}
819
[email protected]71f65dd2009-02-11 19:14:56820void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
821 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07822 AppModalDialog* dialog_delegate =
823 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50824 *showing_dialog = (dialog_delegate != NULL);
825 if (*showing_dialog)
826 *dialog_button = dialog_delegate->GetDialogButtons();
827 else
[email protected]478ff2ed2009-04-21 23:49:18828 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20829}
830
[email protected]71f65dd2009-02-11 19:14:56831void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
832 *success = false;
[email protected]fad84eab2008-12-05 00:37:20833
[email protected]1f460072009-05-28 17:02:07834 AppModalDialog* dialog_delegate =
835 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50836 if (dialog_delegate &&
837 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18838 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
839 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28840 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56841 *success = true;
[email protected]fad84eab2008-12-05 00:37:20842 }
[email protected]478ff2ed2009-04-21 23:49:18843 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
844 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56845 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28846 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56847 *success = true;
[email protected]fad84eab2008-12-05 00:37:20848 }
849 }
[email protected]c274acc2008-11-11 20:13:44850}
851
[email protected]fedaa7d2010-01-26 20:34:57852void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
853 if (browser_tracker_->ContainsHandle(handle)) {
854 Browser* browser = browser_tracker_->GetResource(handle);
855 browser->profile()->ShutdownSessionService();
856 *result = true;
857 } else {
858 *result = false;
859 }
860}
861
[email protected]71f65dd2009-02-11 19:14:56862void AutomationProvider::GetBrowserWindow(int index, int* handle) {
863 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29864 if (index >= 0) {
865 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]f07467d2010-06-16 14:28:30866 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {}
initial.commit09911bf2008-07-26 23:55:29867 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56868 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29869 }
870 }
initial.commit09911bf2008-07-26 23:55:29871}
872
[email protected]24497032009-05-01 17:00:29873void AutomationProvider::FindNormalBrowserWindow(int* handle) {
874 *handle = 0;
875 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31876 Browser::TYPE_NORMAL,
877 false);
[email protected]24497032009-05-01 17:00:29878 if (browser)
879 *handle = browser_tracker_->Add(browser);
880}
881
[email protected]71f65dd2009-02-11 19:14:56882void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
883 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29884 Browser* browser = BrowserList::GetLastActive();
885 if (browser)
[email protected]71f65dd2009-02-11 19:14:56886 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29887}
888
[email protected]b2aa3ed72010-02-01 18:37:14889#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28890// TODO(estade): use this implementation for all platforms?
891void AutomationProvider::GetActiveWindow(int* handle) {
892 gfx::NativeWindow window =
893 BrowserList::GetLastActive()->window()->GetNativeHandle();
894 *handle = window_tracker_->Add(window);
895}
896#endif
897
[email protected]4f6381ee2009-04-16 02:46:33898void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
899 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56900 *success = false;
[email protected]4ae62752008-08-04 23:28:47901 if (browser_tracker_->ContainsHandle(handle)) {
902 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14903 if (browser->command_updater()->SupportsCommand(command) &&
904 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47905 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56906 *success = true;
[email protected]4ae62752008-08-04 23:28:47907 }
908 }
[email protected]4ae62752008-08-04 23:28:47909}
910
[email protected]4f6381ee2009-04-16 02:46:33911void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56912 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53913 // List of commands which just finish synchronously and don't require
914 // setting up an observer.
915 static const int kSynchronousCommands[] = {
916 IDC_HOME,
917 IDC_SELECT_NEXT_TAB,
918 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25919 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53920 };
[email protected]56e71b7c2009-03-27 03:05:56921 if (browser_tracker_->ContainsHandle(handle)) {
922 Browser* browser = browser_tracker_->GetResource(handle);
923 if (browser->command_updater()->SupportsCommand(command) &&
924 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53925 // First check if we can handle the command without using an observer.
926 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
927 if (command == kSynchronousCommands[i]) {
928 browser->ExecuteCommand(command);
929 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
930 true);
931 Send(reply_message);
932 return;
933 }
934 }
935
936 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42937 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
938 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27939 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42940 return;
941 }
[email protected]56e71b7c2009-03-27 03:05:56942 }
943 }
[email protected]49a14a82009-03-31 04:16:44944 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56945 Send(reply_message);
946}
947
[email protected]fc2e0872009-08-21 22:14:41948// This task just adds another task to the event queue. This is useful if
949// you want to ensure that any tasks added to the event queue after this one
950// have already been processed by the time |task| is run.
951class InvokeTaskLaterTask : public Task {
952 public:
953 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
954 virtual ~InvokeTaskLaterTask() {}
955
956 virtual void Run() {
957 MessageLoop::current()->PostTask(FROM_HERE, task_);
958 }
959
960 private:
961 Task* task_;
962
963 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
964};
965
initial.commit09911bf2008-07-26 23:55:29966void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
967 int handle,
[email protected]d1a5941e2009-08-13 23:34:24968 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29969 int flags) {
[email protected]b410bc32009-08-14 01:11:14970 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51971 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41972 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29973 }
974}
975
[email protected]60507b12009-11-02 23:51:35976void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
977 int handle,
978 const gfx::Point& location) {
979 if (window_tracker_->ContainsHandle(handle))
980 ui_controls::SendMouseMove(location.x(), location.y());
981}
982
initial.commit09911bf2008-07-26 23:55:29983void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
984 int handle,
[email protected]bc1407f2009-09-29 00:33:35985 int key,
initial.commit09911bf2008-07-26 23:55:29986 int flags) {
[email protected]b410bc32009-08-14 01:11:14987 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29988 return;
989
[email protected]b410bc32009-08-14 01:11:14990 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29991 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35992 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38993 ((flags & views::Event::EF_CONTROL_DOWN) ==
994 views::Event::EF_CONTROL_DOWN),
995 ((flags & views::Event::EF_SHIFT_DOWN) ==
996 views::Event::EF_SHIFT_DOWN),
997 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:30998 views::Event::EF_ALT_DOWN),
999 ((flags & views::Event::EF_COMMAND_DOWN) ==
1000 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291001}
initial.commit09911bf2008-07-26 23:55:291002
[email protected]71f65dd2009-02-11 19:14:561003void AutomationProvider::IsWindowActive(int handle, bool* success,
1004 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291005 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171006 *is_active =
1007 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561008 *success = true;
initial.commit09911bf2008-07-26 23:55:291009 } else {
[email protected]71f65dd2009-02-11 19:14:561010 *success = false;
1011 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291012 }
1013}
1014
[email protected]71f65dd2009-02-11 19:14:561015void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1016 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291017
1018 if (browser_tracker_->ContainsHandle(handle)) {
1019 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561020 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291021 }
initial.commit09911bf2008-07-26 23:55:291022}
1023
[email protected]982921f12009-10-27 21:43:531024void AutomationProvider::GetType(int handle, int* type_as_int) {
1025 *type_as_int = -1; // -1 is the error code
1026
1027 if (browser_tracker_->ContainsHandle(handle)) {
1028 Browser* browser = browser_tracker_->GetResource(handle);
1029 *type_as_int = static_cast<int>(browser->type());
1030 }
1031}
1032
[email protected]71f65dd2009-02-11 19:14:561033void AutomationProvider::GetTab(int win_handle, int tab_index,
1034 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561035 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291036 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1037 Browser* browser = browser_tracker_->GetResource(win_handle);
1038 if (tab_index < browser->tab_count()) {
1039 TabContents* tab_contents =
1040 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571041 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291042 }
1043 }
initial.commit09911bf2008-07-26 23:55:291044}
1045
[email protected]71f65dd2009-02-11 19:14:561046void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1047 std::wstring* title) {
1048 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291049 if (tab_tracker_->ContainsHandle(handle)) {
1050 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361051 NavigationEntry* entry = tab->GetActiveEntry();
1052 if (entry != NULL) {
1053 *title = UTF16ToWideHack(entry->title());
1054 } else {
1055 *title = std::wstring();
1056 }
[email protected]71f65dd2009-02-11 19:14:561057 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291058 }
initial.commit09911bf2008-07-26 23:55:291059}
1060
[email protected]77bc6732009-04-20 22:01:031061void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1062 *tabstrip_index = -1; // -1 is the error code
1063
1064 if (tab_tracker_->ContainsHandle(handle)) {
1065 NavigationController* tab = tab_tracker_->GetResource(handle);
1066 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121067 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031068 }
1069}
1070
initial.commit09911bf2008-07-26 23:55:291071void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1072 if (window_tracker_->ContainsHandle(handle)) {
1073 window_tracker_->Remove(window_tracker_->GetResource(handle));
1074 }
1075}
1076
1077void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161078 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571079 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291080}
1081
1082// TODO(brettw) change this to accept GURLs when history supports it
1083void AutomationProvider::OnRedirectQueryComplete(
1084 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371085 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291086 bool success,
[email protected]379c2b12009-07-01 21:50:331087 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291088 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561089 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291090
[email protected]deb57402009-02-06 01:35:301091 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101092 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291093 if (success) {
initial.commit09911bf2008-07-26 23:55:291094 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301095 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291096 }
1097
[email protected]4f3dc372009-02-24 00:10:291098 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301099
[email protected]71f65dd2009-02-11 19:14:561100 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231101 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561102 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291103}
1104
1105bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571106 DCHECK(channel_.get());
1107 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291108}
1109
1110Browser* AutomationProvider::FindAndActivateTab(
1111 NavigationController* controller) {
1112 int tab_index;
1113 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1114 if (browser)
1115 browser->SelectTabContentsAt(tab_index, true);
1116
1117 return browser;
1118}
1119
[email protected]71f65dd2009-02-11 19:14:561120void AutomationProvider::GetCookies(const GURL& url, int handle,
1121 int* value_size,
1122 std::string* value) {
1123 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291124 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1125 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311126
1127 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001128 scoped_refptr<URLRequestContextGetter> request_context =
1129 tab->tab_contents()->request_context();
1130 if (!request_context.get())
1131 request_context = tab->profile()->GetRequestContext();
1132
[email protected]34d18e42010-06-21 16:04:501133 net::CookieStore* cookie_store = request_context->GetCookieStore();
1134
1135 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561136 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291137 }
initial.commit09911bf2008-07-26 23:55:291138}
1139
[email protected]71f65dd2009-02-11 19:14:561140void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291141 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561142 int handle,
1143 int* response_value) {
1144 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291145
1146 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1147 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311148
[email protected]dfa46e5f2009-11-17 18:48:431149 scoped_refptr<URLRequestContextGetter> request_context =
1150 tab->tab_contents()->request_context();
1151 if (!request_context.get())
1152 request_context = tab->profile()->GetRequestContext();
1153
[email protected]34d18e42010-06-21 16:04:501154 // Since we are running on the UI thread don't call GetURLRequestContext().
1155 scoped_refptr<net::CookieStore> cookie_store =
1156 request_context->GetCookieStore();
1157
1158 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561159 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291160 }
initial.commit09911bf2008-07-26 23:55:291161}
1162
[email protected]5fa57942010-04-21 23:07:221163void AutomationProvider::DeleteCookie(const GURL& url,
1164 const std::string& cookie_name,
1165 int handle, bool* success) {
1166 *success = false;
1167 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1168 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]34d18e42010-06-21 16:04:501169 net::CookieStore* cookie_store =
1170 tab->profile()->GetRequestContext()->GetCookieStore();
1171 cookie_store->DeleteCookie(url, cookie_name);
[email protected]5fa57942010-04-21 23:07:221172 *success = true;
1173 }
1174}
1175
[email protected]71f65dd2009-02-11 19:14:561176void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1177 *success = false;
initial.commit09911bf2008-07-26 23:55:291178 if (tab_tracker_->ContainsHandle(handle)) {
1179 NavigationController* tab = tab_tracker_->GetResource(handle);
1180 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541181 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561182 *success = true;
initial.commit09911bf2008-07-26 23:55:291183 }
initial.commit09911bf2008-07-26 23:55:291184}
1185
[email protected]71f65dd2009-02-11 19:14:561186void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1187 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291188
1189 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561190 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341191 TabContents* tab_contents =
1192 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161193 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1194 if (rph)
1195 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291196 }
initial.commit09911bf2008-07-26 23:55:291197}
1198
1199void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331200 NOTREACHED() << "This function has been deprecated. "
1201 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291202}
1203
[email protected]71f65dd2009-02-11 19:14:561204void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291205 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561206 const std::wstring& script,
1207 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291208 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341209 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1210 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571211 // Set the routing id of this message with the controller.
1212 // This routing id needs to be remembered for the reverse
1213 // communication while sending back the response of
1214 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331215 std::wstring set_automation_id;
1216 SStringPrintf(&set_automation_id,
1217 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561218 reply_message->routing_id());
1219
1220 DCHECK(reply_message_ == NULL);
1221 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291222
[email protected]57c6a652009-05-04 07:58:341223 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331224 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341225 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061226 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571227 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291228 }
1229
1230 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561231 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1232 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291233 }
1234}
1235
[email protected]71f65dd2009-02-11 19:14:561236void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1237 *visible = false;
[email protected]20e93d12008-08-28 16:31:571238
[email protected]59560e0b2009-06-04 03:30:221239 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251240#if defined(OS_CHROMEOS)
1241 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1242 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1243 for (BrowserList::const_iterator it = BrowserList::begin();
1244 it != BrowserList::end(); ++it) {
1245 if ((*it)->type() == Browser::TYPE_POPUP) {
1246 const GURL& url =
1247 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1248
1249 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1250 url.host() == chrome::kChromeUIFileBrowseHost) {
1251 *visible = true;
1252 break;
1253 }
1254 }
1255 }
1256#else
[email protected]59560e0b2009-06-04 03:30:221257 Browser* browser = browser_tracker_->GetResource(handle);
1258 if (browser) {
1259 *visible = browser->window()->IsDownloadShelfVisible();
1260 }
[email protected]f5bf8ccf2010-02-05 18:19:251261#endif
[email protected]59560e0b2009-06-04 03:30:221262 }
initial.commit09911bf2008-07-26 23:55:291263}
1264
[email protected]59560e0b2009-06-04 03:30:221265void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1266 if (browser_tracker_->ContainsHandle(handle)) {
1267 Browser* browser = browser_tracker_->GetResource(handle);
1268 if (browser) {
1269 if (visible)
1270 browser->window()->GetDownloadShelf()->Show();
1271 else
1272 browser->window()->GetDownloadShelf()->Close();
1273 }
1274 }
1275}
1276
[email protected]34930432009-11-09 00:12:091277void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1278 *visible = false;
1279
1280 if (browser_tracker_->ContainsHandle(handle)) {
1281 Browser* browser = browser_tracker_->GetResource(handle);
1282 if (browser)
1283 *visible = browser->window()->IsFullscreen();
1284 }
1285}
1286
1287void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1288 bool* visible) {
1289 *visible = false;
1290
1291 if (browser_tracker_->ContainsHandle(handle)) {
1292 Browser* browser = browser_tracker_->GetResource(handle);
1293 if (browser)
1294 *visible = browser->window()->IsFullscreenBubbleVisible();
1295 }
1296}
[email protected]59560e0b2009-06-04 03:30:221297
[email protected]71f65dd2009-02-11 19:14:561298void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1299 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291300 if (tab_tracker_->ContainsHandle(handle)) {
1301 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111302 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291303 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561304 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291305 }
1306 }
initial.commit09911bf2008-07-26 23:55:291307}
1308
initial.commit09911bf2008-07-26 23:55:291309void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561310 int handle, const std::wstring& find_request,
1311 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311312 NOTREACHED() << "This function has been deprecated."
1313 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561314 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311315 return;
1316}
1317
[email protected]4f999132009-03-31 18:08:401318void AutomationProvider::HandleFindRequest(
1319 int handle,
1320 const AutomationMsg_Find_Params& params,
1321 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291322 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561323 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1324 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291325 return;
1326 }
1327
1328 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111329 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291330
1331 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141332 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291333
[email protected]57c6a652009-05-04 07:58:341334 tab_contents->set_current_find_request_id(
1335 FindInPageNotificationObserver::kFindInPageRequestId);
1336 tab_contents->render_view_host()->StartFinding(
1337 FindInPageNotificationObserver::kFindInPageRequestId,
1338 params.search_string, params.forward, params.match_case,
1339 params.find_next);
initial.commit09911bf2008-07-26 23:55:291340}
1341
[email protected]5f8af2a2008-08-06 22:49:451342void AutomationProvider::HandleOpenFindInPageRequest(
1343 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291344 if (browser_tracker_->ContainsHandle(handle)) {
1345 Browser* browser = browser_tracker_->GetResource(handle);
1346 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451347 }
1348}
1349
[email protected]71f65dd2009-02-11 19:14:561350void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561351 *visible = false;
[email protected]855c0142009-09-28 22:35:241352 Browser* browser = browser_tracker_->GetResource(handle);
1353 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581354 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171355 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241356 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291357 }
[email protected]20e93d12008-08-28 16:31:571358}
1359
[email protected]71f65dd2009-02-11 19:14:561360void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1361 int* y) {
[email protected]9e0534b2008-10-21 15:03:011362 gfx::Point position(0, 0);
1363 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291364 if (browser_tracker_->ContainsHandle(handle)) {
1365 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581366 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171367 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581368 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291369 }
[email protected]20e93d12008-08-28 16:31:571370
[email protected]71f65dd2009-02-11 19:14:561371 *x = position.x();
1372 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571373}
1374
[email protected]4512cb52010-04-05 19:50:251375// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1376// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581377void AutomationProvider::GetBookmarkBarVisibility(int handle,
1378 bool* visible,
1379 bool* animating) {
1380 *visible = false;
1381 *animating = false;
1382
1383 if (browser_tracker_->ContainsHandle(handle)) {
1384 Browser* browser = browser_tracker_->GetResource(handle);
1385 if (browser) {
[email protected]472f099b2010-05-27 17:07:121386#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1387 // TODO(jrg): Was removed in rev43789 for perf. Need to investigate.
1388
[email protected]ab6ca392010-04-07 00:44:131389 // IsBookmarkBarVisible() line looks correct but is not
1390 // consistent across platforms. Specifically, on Mac/Linux, it
1391 // returns false if the bar is hidden in a pref (even if visible
1392 // on the NTP). On ChromeOS, it returned true if on NTP
1393 // independent of the pref. Making the code more consistent
1394 // caused a perf bot regression on Windows (which shares views).
1395 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251396 *visible = browser->profile()->GetPrefs()->GetBoolean(
1397 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401398#else
1399 *visible = browser->window()->IsBookmarkBarVisible();
1400#endif
[email protected]c3240722010-03-05 21:52:581401 *animating = browser->window()->IsBookmarkBarAnimating();
1402 }
1403 }
1404}
1405
[email protected]6d8ffc9f2010-03-12 18:27:531406void AutomationProvider::GetBookmarksAsJSON(int handle,
1407 std::string* bookmarks_as_json,
1408 bool *success) {
1409 *success = false;
1410 if (browser_tracker_->ContainsHandle(handle)) {
1411 Browser* browser = browser_tracker_->GetResource(handle);
1412 if (browser) {
1413 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1414 return;
1415 }
1416 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1417 browser->profile(),
1418 browser->profile()->GetBookmarkModel());
1419 *success = storage->SerializeData(bookmarks_as_json);
1420 }
1421 }
1422}
1423
1424void AutomationProvider::WaitForBookmarkModelToLoad(
1425 int handle,
1426 IPC::Message* reply_message) {
1427 if (browser_tracker_->ContainsHandle(handle)) {
1428 Browser* browser = browser_tracker_->GetResource(handle);
1429 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1430 if (model->IsLoaded()) {
1431 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1432 reply_message, true);
1433 Send(reply_message);
1434 } else {
1435 // The observer will delete itself when done.
1436 new AutomationProviderBookmarkModelObserver(this, reply_message,
1437 model);
1438 }
1439 }
1440}
1441
1442void AutomationProvider::AddBookmarkGroup(int handle,
1443 int64 parent_id, int index,
1444 std::wstring title,
1445 bool* success) {
1446 if (browser_tracker_->ContainsHandle(handle)) {
1447 Browser* browser = browser_tracker_->GetResource(handle);
1448 if (browser) {
1449 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1450 if (!model->IsLoaded()) {
1451 *success = false;
1452 return;
1453 }
1454 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1455 DCHECK(parent);
1456 if (parent) {
1457 const BookmarkNode* child = model->AddGroup(parent, index,
1458 WideToUTF16(title));
1459 DCHECK(child);
1460 if (child)
1461 *success = true;
1462 }
1463 }
1464 }
1465 *success = false;
1466}
1467
1468void AutomationProvider::AddBookmarkURL(int handle,
1469 int64 parent_id, int index,
1470 std::wstring title, const GURL& url,
1471 bool* success) {
1472 if (browser_tracker_->ContainsHandle(handle)) {
1473 Browser* browser = browser_tracker_->GetResource(handle);
1474 if (browser) {
1475 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1476 if (!model->IsLoaded()) {
1477 *success = false;
1478 return;
1479 }
1480 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1481 DCHECK(parent);
1482 if (parent) {
1483 const BookmarkNode* child = model->AddURL(parent, index,
1484 WideToUTF16(title), url);
1485 DCHECK(child);
1486 if (child)
1487 *success = true;
1488 }
1489 }
1490 }
1491 *success = false;
1492}
1493
1494void AutomationProvider::ReparentBookmark(int handle,
1495 int64 id, int64 new_parent_id,
1496 int index,
1497 bool* success) {
1498 if (browser_tracker_->ContainsHandle(handle)) {
1499 Browser* browser = browser_tracker_->GetResource(handle);
1500 if (browser) {
1501 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1502 if (!model->IsLoaded()) {
1503 *success = false;
1504 return;
1505 }
1506 const BookmarkNode* node = model->GetNodeByID(id);
1507 DCHECK(node);
1508 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1509 DCHECK(new_parent);
1510 if (node && new_parent) {
1511 model->Move(node, new_parent, index);
1512 *success = true;
1513 }
1514 }
1515 }
1516 *success = false;
1517}
1518
1519void AutomationProvider::SetBookmarkTitle(int handle,
1520 int64 id, std::wstring title,
1521 bool* success) {
1522 if (browser_tracker_->ContainsHandle(handle)) {
1523 Browser* browser = browser_tracker_->GetResource(handle);
1524 if (browser) {
1525 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1526 if (!model->IsLoaded()) {
1527 *success = false;
1528 return;
1529 }
1530 const BookmarkNode* node = model->GetNodeByID(id);
1531 DCHECK(node);
1532 if (node) {
1533 model->SetTitle(node, WideToUTF16(title));
1534 *success = true;
1535 }
1536 }
1537 }
1538 *success = false;
1539}
1540
1541void AutomationProvider::SetBookmarkURL(int handle,
1542 int64 id, const GURL& url,
1543 bool* success) {
1544 if (browser_tracker_->ContainsHandle(handle)) {
1545 Browser* browser = browser_tracker_->GetResource(handle);
1546 if (browser) {
1547 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1548 if (!model->IsLoaded()) {
1549 *success = false;
1550 return;
1551 }
1552 const BookmarkNode* node = model->GetNodeByID(id);
1553 DCHECK(node);
1554 if (node) {
1555 model->SetURL(node, url);
1556 *success = true;
1557 }
1558 }
1559 }
1560 *success = false;
1561}
1562
1563void AutomationProvider::RemoveBookmark(int handle,
1564 int64 id,
1565 bool* success) {
1566 if (browser_tracker_->ContainsHandle(handle)) {
1567 Browser* browser = browser_tracker_->GetResource(handle);
1568 if (browser) {
1569 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1570 if (!model->IsLoaded()) {
1571 *success = false;
1572 return;
1573 }
1574 const BookmarkNode* node = model->GetNodeByID(id);
1575 DCHECK(node);
1576 if (node) {
1577 const BookmarkNode* parent = node->GetParent();
1578 DCHECK(parent);
1579 model->Remove(parent, parent->IndexOfChild(node));
1580 *success = true;
1581 }
1582 }
1583 }
1584 *success = false;
1585}
1586
[email protected]ef413ca2010-05-25 21:09:141587// Sample json input: { "command": "SetWindowDimensions",
1588// "x": 20, # optional
1589// "y": 20, # optional
1590// "width": 800, # optional
1591// "height": 600 } # optional
1592void AutomationProvider::SetWindowDimensions(Browser* browser,
1593 DictionaryValue* args,
1594 IPC::Message* reply_message) {
1595 gfx::Rect rect = browser->window()->GetRestoredBounds();
1596 int x, y, width, height;
1597 if (args->GetInteger(L"x", &x))
1598 rect.set_x(x);
1599 if (args->GetInteger(L"y", &y))
1600 rect.set_y(y);
1601 if (args->GetInteger(L"width", &width))
1602 rect.set_width(width);
1603 if (args->GetInteger(L"height", &height))
1604 rect.set_height(height);
1605 browser->window()->SetBounds(rect);
1606 AutomationMsg_SendJSONRequest::WriteReplyParams(
1607 reply_message, std::string("{}"), true);
1608 Send(reply_message);
1609}
1610
[email protected]a9ff2c02010-05-13 17:33:051611// Sample json input: { "command": "GetBrowserInfo" }
1612// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1613// sample json output.
[email protected]53329582010-05-14 21:10:581614void AutomationProvider::GetBrowserInfo(Browser* browser,
1615 DictionaryValue* args,
[email protected]a9ff2c02010-05-13 17:33:051616 IPC::Message* reply_message) {
1617 std::string json_return;
1618 bool reply_return = true;
1619
1620 DictionaryValue* properties = new DictionaryValue;
1621 properties->SetString(L"ChromeVersion", chrome::kChromeVersion);
1622 properties->SetString(L"BrowserProcessExecutableName",
1623 chrome::kBrowserProcessExecutableName);
1624 properties->SetString(L"HelperProcessExecutableName",
1625 chrome::kHelperProcessExecutableName);
1626 properties->SetString(L"BrowserProcessExecutablePath",
1627 chrome::kBrowserProcessExecutablePath);
1628 properties->SetString(L"HelperProcessExecutablePath",
1629 chrome::kHelperProcessExecutablePath);
1630#if defined(OS_WIN)
1631 properties->SetString(L"command_line_string",
1632 CommandLine::ForCurrentProcess()->command_line_string());
1633#elif defined(OS_POSIX)
[email protected]ef413ca2010-05-25 21:09:141634 properties->SetString(L"command_line_string",
1635 JoinString(CommandLine::ForCurrentProcess()->argv(), ' '));
[email protected]a9ff2c02010-05-13 17:33:051636#endif
1637
1638 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1639 return_value->Set(L"properties", properties);
1640
[email protected]ef413ca2010-05-25 21:09:141641 return_value->SetInteger(L"browser_pid", base::GetCurrentProcId());
1642 // Add info about all windows in a list of dictionaries, one dictionary
1643 // item per window.
1644 ListValue* windows = new ListValue;
1645 int windex = 0;
1646 for (BrowserList::const_iterator it = BrowserList::begin();
1647 it != BrowserList::end();
1648 ++it, ++windex) {
1649 DictionaryValue* browser_item = new DictionaryValue;
1650 browser = *it;
1651 browser_item->SetInteger(L"index", windex);
1652 // Window properties
1653 gfx::Rect rect = browser->window()->GetRestoredBounds();
1654 browser_item->SetInteger(L"x", rect.x());
1655 browser_item->SetInteger(L"y", rect.y());
1656 browser_item->SetInteger(L"width", rect.width());
1657 browser_item->SetInteger(L"height", rect.height());
1658 browser_item->SetBoolean(L"fullscreen",
1659 browser->window()->IsFullscreen());
1660 browser_item->SetInteger(L"selected_tab", browser->selected_index());
1661 browser_item->SetBoolean(L"incognito",
1662 browser->profile()->IsOffTheRecord());
1663 // For each window, add info about all tabs in a list of dictionaries,
1664 // one dictionary item per tab.
1665 ListValue* tabs = new ListValue;
1666 for (int i = 0; i < browser->tab_count(); ++i) {
1667 TabContents* tc = browser->GetTabContentsAt(i);
1668 DictionaryValue* tab = new DictionaryValue;
1669 tab->SetInteger(L"index", i);
1670 tab->SetString(L"url", tc->GetURL().spec());
1671 tab->SetInteger(L"renderer_pid",
1672 base::GetProcId(tc->GetRenderProcessHost()->GetHandle()));
1673 tab->SetInteger(L"num_infobars", tc->infobar_delegate_count());
1674 tabs->Append(tab);
1675 }
1676 browser_item->Set(L"tabs", tabs);
1677
1678 windows->Append(browser_item);
1679 }
1680 return_value->Set(L"windows", windows);
1681
1682 return_value->SetString(L"child_process_path",
1683 ChildProcessHost::GetChildPath(true).value());
1684 // Child processes are the processes for plugins and other workers.
1685 // Add all child processes in a list of dictionaries, one dictionary item
1686 // per child process.
1687 ListValue* child_processes = new ListValue;
1688 for (ChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
1689 // Only add processes which are already started, since we need their handle.
1690 if ((*iter)->handle() != base::kNullProcessHandle) {
1691 ChildProcessInfo* info = *iter;
1692 DictionaryValue* item = new DictionaryValue;
1693 item->SetString(L"name", info->name());
1694 item->SetString(L"type",
1695 ChildProcessInfo::GetTypeNameInEnglish(info->type()));
1696 item->SetInteger(L"pid", base::GetProcId(info->handle()));
1697 child_processes->Append(item);
1698 }
1699 }
1700 return_value->Set(L"child_processes", child_processes);
1701
1702 // Add all extension processes in a list of dictionaries, one dictionary
1703 // item per extension process.
1704 ListValue* extension_processes = new ListValue;
1705 ProfileManager* profile_manager = g_browser_process->profile_manager();
1706 for (ProfileManager::const_iterator it = profile_manager->begin();
1707 it != profile_manager->end(); ++it) {
1708 ExtensionProcessManager* process_manager =
1709 (*it)->GetExtensionProcessManager();
1710 ExtensionProcessManager::const_iterator jt;
1711 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) {
1712 ExtensionHost* ex_host = *jt;
1713 // Don't add dead extension processes.
1714 if (!ex_host->IsRenderViewLive())
1715 continue;
1716 DictionaryValue* item = new DictionaryValue;
1717 item->SetString(L"name", ex_host->extension()->name());
1718 item->SetInteger(
1719 L"pid",
1720 base::GetProcId(ex_host->render_process_host()->GetHandle()));
1721 extension_processes->Append(item);
1722 }
1723 }
1724 return_value->Set(L"extension_processes", extension_processes);
1725
[email protected]a9ff2c02010-05-13 17:33:051726 base::JSONWriter::Write(return_value.get(), false, &json_return);
1727 AutomationMsg_SendJSONRequest::WriteReplyParams(
1728 reply_message, json_return, reply_return);
1729 Send(reply_message);
1730}
1731
[email protected]24e2b102010-04-29 17:56:471732// Sample json input: { "command": "GetHistoryInfo",
1733// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361734// Refer chrome/test/pyautolib/history_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581735void AutomationProvider::GetHistoryInfo(Browser* browser,
1736 DictionaryValue* args,
1737 IPC::Message* reply_message) {
[email protected]e6e376e2010-04-19 21:41:361738 consumer_.CancelAllRequests();
1739
1740 std::wstring search_text;
1741 args->GetString(L"search_text", &search_text);
1742
1743 // Fetch history.
1744 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1745 history::QueryOptions options;
1746 // The observer owns itself. It deletes itself after it fetches history.
1747 AutomationProviderHistoryObserver* history_observer =
1748 new AutomationProviderHistoryObserver(this, reply_message);
1749 hs->QueryHistory(
1750 search_text,
1751 options,
1752 &consumer_,
1753 NewCallback(history_observer,
1754 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1755}
1756
[email protected]bbe6aa02010-05-07 17:27:291757// Sample json input: { "command": "AddHistoryItem",
1758// "item": { "URL": "http://www.google.com",
1759// "title": "Google", # optional
1760// "time": 12345 # optional (time_t)
1761// } }
1762// Refer chrome/test/pyautolib/pyauto.py for details on input.
[email protected]53329582010-05-14 21:10:581763void AutomationProvider::AddHistoryItem(Browser* browser,
1764 DictionaryValue* args,
1765 IPC::Message* reply_message) {
[email protected]bbe6aa02010-05-07 17:27:291766 bool reply_return = true;
1767 std::string json_return = "{}";
1768
1769 DictionaryValue* item = NULL;
1770 args->GetDictionary(L"item", &item);
1771 string16 url_text;
1772 std::wstring title;
1773 base::Time time = base::Time::Now();
1774
1775 if (item->GetString("url", &url_text)) {
1776 GURL gurl(url_text);
1777 item->GetString(L"title", &title); // Don't care if it fails.
1778 int it;
1779 double dt;
1780 if (item->GetInteger(L"time", &it))
1781 time = base::Time::FromTimeT(it);
1782 else if (item->GetReal(L"time", &dt))
1783 time = base::Time::FromDoubleT(dt);
1784
1785 // Ideas for "dummy" values (e.g. id_scope) came from
1786 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
1787 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1788 const void* id_scope = reinterpret_cast<void*>(1);
1789 hs->AddPage(gurl, time,
1790 id_scope,
1791 0,
1792 GURL(),
1793 PageTransition::LINK,
1794 history::RedirectList(),
1795 false);
1796 if (title.length()) {
1797 // TODO(jrg): add a string16 interface for
1798 // HistoryService::SetPageTitle(), then use it.
1799 hs->SetPageTitle(gurl, title);
1800 }
1801 } else {
1802 json_return = "{\"error\": \"bad args (no URL in dict?).\"}";
1803 reply_return = false;
1804 }
1805
1806 AutomationMsg_SendJSONRequest::WriteReplyParams(
1807 reply_message, json_return, reply_return);
1808 Send(reply_message);
1809}
1810
[email protected]24e2b102010-04-29 17:56:471811// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361812// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581813void AutomationProvider::GetDownloadsInfo(Browser* browser,
1814 DictionaryValue* args,
1815 IPC::Message* reply_message) {
[email protected]d4adc292010-04-15 18:06:391816 std::string json_return;
1817 bool reply_return = true;
1818 AutomationProviderDownloadManagerObserver observer;
1819 std::vector<DownloadItem*> downloads;
1820 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1821
1822 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471823 json_return = "{\"error\": \"no download manager\"}";
[email protected]d4adc292010-04-15 18:06:391824 reply_return = false;
1825 } else {
1826 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1827 // since that would be transient; a download might enter and empty out
1828 // the current download queue too soon to be noticed.
1829 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1830 downloads = observer.Downloads();
1831 }
1832
1833 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1834 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1835 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1836 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1837 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1838
1839 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1840 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1841 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1842 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1843 std::string("DANGEROUS_BUT_VALIDATED");
1844
1845 ListValue* list_of_downloads = new ListValue;
1846 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1847 it != downloads.end();
1848 it++) { // Fill info about each download item.
1849 DictionaryValue* dl_item_value = new DictionaryValue;
1850 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1851 dl_item_value->SetString(L"url", (*it)->url().spec());
1852 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1853 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1854 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1855 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1856 dl_item_value->SetBoolean(L"open_when_complete",
1857 (*it)->open_when_complete());
1858 dl_item_value->SetBoolean(L"is_extension_install",
1859 (*it)->is_extension_install());
1860 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1861 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1862 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1863 dl_item_value->SetString(L"safety_state",
1864 safety_state_to_string[(*it)->safety_state()]);
1865 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1866 list_of_downloads->Append(dl_item_value);
1867 }
1868 return_value->Set(L"downloads", list_of_downloads);
1869 base::JSONWriter::Write(return_value.get(), false, &json_return);
1870
1871 AutomationMsg_SendJSONRequest::WriteReplyParams(
1872 reply_message, json_return, reply_return);
1873 Send(reply_message);
1874 // All value objects allocated above are owned by |return_value|
1875 // and get freed by it.
1876}
1877
[email protected]59a611242010-04-02 02:24:041878void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:581879 Browser* browser,
[email protected]59a611242010-04-02 02:24:041880 DictionaryValue* args,
1881 IPC::Message* reply_message) {
1882 std::string json_return;
1883 bool reply_return = true;
1884 AutomationProviderDownloadManagerObserver observer;
1885 std::vector<DownloadItem*> downloads;
1886
1887 // Look for a quick return.
1888 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471889 json_return = "{\"error\": \"no download manager\"}";
[email protected]59a611242010-04-02 02:24:041890 reply_return = false;
1891 } else {
1892 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
1893 FilePath());
1894 downloads = observer.Downloads();
1895 if (downloads.size() == 0) {
1896 json_return = "{}";
1897 }
1898 }
1899 if (!json_return.empty()) {
1900 AutomationMsg_SendJSONRequest::WriteReplyParams(
1901 reply_message, json_return, reply_return);
1902 Send(reply_message);
1903 }
1904
1905 // The observer owns itself. When the last observed item pings, it
1906 // deletes itself.
1907 AutomationProviderDownloadItemObserver* item_observer =
1908 new AutomationProviderDownloadItemObserver(
1909 this, reply_message, downloads.size());
1910 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
1911 i != downloads.end();
1912 i++) {
1913 (*i)->AddObserver(item_observer);
1914 }
1915}
1916
[email protected]24e2b102010-04-29 17:56:471917// Sample json input: { "command": "GetPrefsInfo" }
1918// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581919void AutomationProvider::GetPrefsInfo(Browser* browser,
1920 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:471921 IPC::Message* reply_message) {
1922 std::string json_return;
1923 bool reply_return = true;
1924
1925 const PrefService::PreferenceSet& prefs =
1926 profile_->GetPrefs()->preference_set();
1927 DictionaryValue* items = new DictionaryValue;
1928 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
1929 it != prefs.end(); ++it) {
1930 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
1931 }
1932 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1933 return_value->Set(L"prefs", items); // return_value owns items.
1934
1935 base::JSONWriter::Write(return_value.get(), false, &json_return);
1936 AutomationMsg_SendJSONRequest::WriteReplyParams(
1937 reply_message, json_return, reply_return);
1938 Send(reply_message);
1939}
1940
1941// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:581942void AutomationProvider::SetPrefs(Browser* browser,
1943 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:471944 IPC::Message* reply_message) {
1945 bool reply_return = true;
1946 std::string json_return = "{}";
1947 std::wstring path;
1948 Value* val;
1949 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
1950 PrefService* pref_service = profile_->GetPrefs();
1951 const PrefService::Preference* pref =
1952 pref_service->FindPreference(path.c_str());
1953 if (!pref) { // Not a registered pref.
1954 json_return = "{\"error\": \"pref not registered.\"}";
1955 reply_return = false;
1956 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
1957 json_return = "{\"error\": \"pref is managed. cannot be changed.\"}";
1958 reply_return = false;
1959 } else { // Set the pref.
1960 pref_service->Set(path.c_str(), *val);
1961 }
1962 } else {
1963 json_return = "{\"error\": \"no pref path or value given.\"}";
1964 reply_return = false;
1965 }
1966
1967 AutomationMsg_SendJSONRequest::WriteReplyParams(
1968 reply_message, json_return, reply_return);
1969 Send(reply_message);
1970}
1971
[email protected]53329582010-05-14 21:10:581972// Sample json input: { "command": "GetOmniboxInfo" }
1973// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
1974void AutomationProvider::GetOmniboxInfo(Browser* browser,
1975 DictionaryValue* args,
1976 IPC::Message* reply_message) {
1977 std::string json_return;
1978 bool reply_return = true;
1979 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1980
1981 LocationBar* loc_bar = browser->window()->GetLocationBar();
1982 AutocompleteEditView* edit_view = loc_bar->location_entry();
1983 AutocompleteEditModel* model = edit_view->model();
1984
1985 // Fill up matches.
1986 ListValue* matches = new ListValue;
1987 const AutocompleteResult& result = model->result();
1988 for (AutocompleteResult::const_iterator i = result.begin();
1989 i != result.end(); ++i) {
1990 const AutocompleteMatch& match = *i;
1991 DictionaryValue* item = new DictionaryValue; // owned by return_value
1992 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
1993 item->SetBoolean(L"starred", match.starred);
1994 item->SetString(L"destination_url", match.destination_url.spec());
1995 item->SetString(L"contents", match.contents);
1996 item->SetString(L"description", match.description);
1997 matches->Append(item);
1998 }
1999 return_value->Set(L"matches", matches);
2000
2001 // Fill up other properties.
2002 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2003 properties->SetBoolean(L"has_focus", model->has_focus());
2004 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2005 properties->SetString(L"keyword", model->keyword());
2006 properties->SetString(L"text", edit_view->GetText());
2007 return_value->Set(L"properties", properties);
2008
2009 base::JSONWriter::Write(return_value.get(), false, &json_return);
2010 AutomationMsg_SendJSONRequest::WriteReplyParams(
2011 reply_message, json_return, reply_return);
2012 Send(reply_message);
2013}
2014
2015// Sample json input: { "command": "SetOmniboxText",
2016// "text": "goog" }
2017void AutomationProvider::SetOmniboxText(Browser* browser,
2018 DictionaryValue* args,
2019 IPC::Message* reply_message) {
2020 std::string json_return = "{}";
2021 bool reply_return = true;
2022 std::wstring text;
2023
2024 if (!args->GetString(L"text", &text)) {
2025 json_return = "{\"error\": \"text missing\"}";
2026 reply_return = false;
2027 } else {
2028 browser->FocusLocationBar();
2029 LocationBar* loc_bar = browser->window()->GetLocationBar();
2030 AutocompleteEditView* edit_view = loc_bar->location_entry();
2031 edit_view->model()->OnSetFocus(false);
2032 edit_view->SetUserText(text);
2033 }
2034
2035 AutomationMsg_SendJSONRequest::WriteReplyParams(
2036 reply_message, json_return, reply_return);
2037 Send(reply_message);
2038}
2039
2040// Sample json input: { "command": "OmniboxMovePopupSelection",
2041// "count": 1 }
2042// Negative count implies up, positive implies down. Count values will be
2043// capped by the size of the popup list.
2044void AutomationProvider::OmniboxMovePopupSelection(
2045 Browser* browser,
2046 DictionaryValue* args,
2047 IPC::Message* reply_message) {
2048 std::string json_return = "{}";
2049 bool reply_return = true;
2050 int count;
2051
2052 if (!args->GetInteger(L"count", &count)) {
2053 json_return = "{\"error\": \"count missing\"}";
2054 reply_return = false;
2055 } else {
2056 LocationBar* loc_bar = browser->window()->GetLocationBar();
2057 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2058 model->OnUpOrDownKeyPressed(count);
2059 }
2060
2061 AutomationMsg_SendJSONRequest::WriteReplyParams(
2062 reply_message, json_return, reply_return);
2063 Send(reply_message);
2064}
2065
2066// Sample json input: { "command": "OmniboxAcceptInput" }
2067void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2068 DictionaryValue* args,
2069 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282070 NavigationController& controller =
2071 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122072 // Setup observer to wait until the selected item loads.
2073 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282074 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122075 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582076
2077 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582078}
2079
[email protected]a3cd5022010-06-16 18:25:292080// Sample json input: { "command": "GetInitialLoadTimes" }
2081// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2082void AutomationProvider::GetInitialLoadTimes(
2083 Browser*,
2084 DictionaryValue*,
2085 IPC::Message* reply_message) {
2086 scoped_ptr<DictionaryValue> return_value(
2087 initial_load_observer_->GetTimingInformation());
2088
2089 std::string json_return;
2090 base::JSONWriter::Write(return_value.get(), false, &json_return);
2091 AutomationMsg_SendJSONRequest::WriteReplyParams(
2092 reply_message, json_return, true);
2093 Send(reply_message);
2094}
2095
[email protected]f7d48012010-05-06 08:17:052096// Sample json input: { "command": "GetPluginsInfo" }
2097// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582098void AutomationProvider::GetPluginsInfo(Browser* browser,
2099 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052100 IPC::Message* reply_message) {
2101 std::string json_return;
2102 bool reply_return = true;
2103
2104 std::vector<WebPluginInfo> plugins;
2105 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2106 ListValue* items = new ListValue;
2107 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2108 it != plugins.end();
2109 ++it) {
2110 DictionaryValue* item = new DictionaryValue;
2111 item->SetString(L"name", it->name);
2112 item->SetString(L"path", it->path.value());
2113 item->SetString(L"version", it->version);
2114 item->SetString(L"desc", it->desc);
2115 item->SetBoolean(L"enabled", it->enabled);
2116 // Add info about mime types.
2117 ListValue* mime_types = new ListValue();
2118 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2119 it->mime_types.begin();
2120 type_it != it->mime_types.end();
2121 ++type_it) {
2122 DictionaryValue* mime_type = new DictionaryValue();
2123 mime_type->SetString(L"mimeType", type_it->mime_type);
2124 mime_type->SetString(L"description", type_it->description);
2125
2126 ListValue* file_extensions = new ListValue();
2127 for (std::vector<std::string>::const_iterator ext_it =
2128 type_it->file_extensions.begin();
2129 ext_it != type_it->file_extensions.end();
2130 ++ext_it) {
2131 file_extensions->Append(new StringValue(*ext_it));
2132 }
2133 mime_type->Set(L"fileExtensions", file_extensions);
2134
2135 mime_types->Append(mime_type);
2136 }
2137 item->Set(L"mimeTypes", mime_types);
2138 items->Append(item);
2139 }
2140 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2141 return_value->Set(L"plugins", items); // return_value owns items.
2142
2143 base::JSONWriter::Write(return_value.get(), false, &json_return);
2144 AutomationMsg_SendJSONRequest::WriteReplyParams(
2145 reply_message, json_return, reply_return);
2146 Send(reply_message);
2147}
2148
2149// Sample json input:
2150// { "command": "EnablePlugin",
2151// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582152void AutomationProvider::EnablePlugin(Browser* browser,
2153 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052154 IPC::Message* reply_message) {
2155 std::string json_return = "{}";
2156 bool reply_return = true;
2157 FilePath::StringType path;
2158 if (!args->GetString(L"path", &path)) {
2159 json_return = "{\"error\": \"path not specified.\"}";
2160 reply_return = false;
2161 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
2162 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2163 " for path %s.\"}", path.c_str());
2164 reply_return = false;
2165 }
2166
2167 AutomationMsg_SendJSONRequest::WriteReplyParams(
2168 reply_message, json_return, reply_return);
2169 Send(reply_message);
2170}
2171
2172// Sample json input:
2173// { "command": "DisablePlugin",
2174// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582175void AutomationProvider::DisablePlugin(Browser* browser,
2176 DictionaryValue* args,
2177 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052178 std::string json_return = "{}";
2179 bool reply_return = true;
2180 FilePath::StringType path;
2181 if (!args->GetString(L"path", &path)) {
2182 json_return = "{\"error\": \"path not specified.\"}";
2183 reply_return = false;
2184 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
2185 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2186 " for path %s.\"}", path.c_str());
2187 reply_return = false;
2188 }
2189
2190 AutomationMsg_SendJSONRequest::WriteReplyParams(
2191 reply_message, json_return, reply_return);
2192 Send(reply_message);
2193}
2194
[email protected]53329582010-05-14 21:10:582195void AutomationProvider::SendJSONRequest(int handle,
2196 std::string json_request,
2197 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:042198 Browser* browser = NULL;
2199 std::string error_string;
2200 scoped_ptr<Value> values;
2201
2202 // Basic error checking.
2203 if (browser_tracker_->ContainsHandle(handle)) {
2204 browser = browser_tracker_->GetResource(handle);
2205 }
2206 if (!browser) {
2207 error_string = "no browser object";
2208 } else {
2209 base::JSONReader reader;
2210 std::string error;
[email protected]ba399672010-04-06 15:42:392211 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
[email protected]59a611242010-04-02 02:24:042212 if (!error.empty()) {
2213 error_string = error;
2214 }
2215 }
2216
2217 // Make sure input is a dict with a string command.
2218 std::string command;
2219 DictionaryValue* dict_value = NULL;
2220 if (error_string.empty()) {
2221 if (values->GetType() != Value::TYPE_DICTIONARY) {
2222 error_string = "not a dict or no command key in dict";
2223 } else {
2224 // Ownership remains with "values" variable.
2225 dict_value = static_cast<DictionaryValue*>(values.get());
2226 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
2227 error_string = "no command key in dict or not a string command";
2228 }
2229 }
2230 }
2231
[email protected]24e2b102010-04-29 17:56:472232 // Map json commands to their handlers.
2233 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:052234 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
2235 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
2236 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
2237
[email protected]a9ff2c02010-05-13 17:33:052238 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
2239
[email protected]24e2b102010-04-29 17:56:472240 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:292241 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:052242
[email protected]53329582010-05-14 21:10:582243 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
2244 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
2245 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
2246 handler_map["OmniboxMovePopupSelection"] =
2247 &AutomationProvider::OmniboxMovePopupSelection;
2248
[email protected]24e2b102010-04-29 17:56:472249 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
2250 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:052251
[email protected]ef413ca2010-05-25 21:09:142252 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
2253
[email protected]f7d48012010-05-06 08:17:052254 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:472255 handler_map["WaitForAllDownloadsToComplete"] =
2256 &AutomationProvider::WaitForDownloadsToComplete;
2257
[email protected]a3cd5022010-06-16 18:25:292258 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
2259
[email protected]59a611242010-04-02 02:24:042260 if (error_string.empty()) {
[email protected]24e2b102010-04-29 17:56:472261 if (handler_map.find(std::string(command)) != handler_map.end()) {
[email protected]53329582010-05-14 21:10:582262 (this->*handler_map[command])(browser, dict_value, reply_message);
[email protected]59a611242010-04-02 02:24:042263 return;
2264 } else {
[email protected]24e2b102010-04-29 17:56:472265 error_string = "Unknown command. Options: ";
2266 for (std::map<std::string, JsonHandler>::const_iterator it =
2267 handler_map.begin(); it != handler_map.end(); ++it) {
2268 error_string += it->first + ", ";
2269 }
[email protected]59a611242010-04-02 02:24:042270 }
2271 }
2272
2273 // If we hit an error, return info.
[email protected]24e2b102010-04-29 17:56:472274 // Return a dict of {"error", "descriptive_string_for_error"}.
[email protected]59a611242010-04-02 02:24:042275 // Else return an empty dict.
2276 std::string json_string;
2277 bool success = true;
2278 if (!error_string.empty()) {
2279 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
2280 dict->SetString(L"error", error_string);
2281 base::JSONWriter::Write(dict.get(), false, &json_string);
2282 success = false;
2283 } else {
2284 json_string = "{}";
2285 }
2286 AutomationMsg_SendJSONRequest::WriteReplyParams(
2287 reply_message, json_string, success);
2288 Send(reply_message);
2289}
2290
initial.commit09911bf2008-07-26 23:55:292291void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562292 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342293 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2294 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562295 DCHECK(reply_message_ == NULL);
2296 reply_message_ = reply_message;
2297
[email protected]d9f9b792009-06-24 13:17:122298 DevToolsManager::GetInstance()->InspectElement(
2299 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292300 } else {
[email protected]71f65dd2009-02-11 19:14:562301 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2302 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292303 }
2304}
2305
2306void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092307 if (reply_message_) {
2308 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2309 num_resources);
2310 Send(reply_message_);
2311 reply_message_ = NULL;
2312 }
initial.commit09911bf2008-07-26 23:55:292313}
2314
[email protected]a7eee32f2009-05-22 18:08:172315class SetProxyConfigTask : public Task {
2316 public:
[email protected]be180c802009-10-23 06:33:312317 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
2318 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:252319 : request_context_getter_(request_context_getter),
2320 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:172321 virtual void Run() {
2322 // First, deserialize the JSON string. If this fails, log and bail.
2323 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:392324 std::string error_msg;
2325 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:172326 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2327 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:392328 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:172329 return;
2330 }
2331
2332 scoped_ptr<DictionaryValue> dict(
2333 static_cast<DictionaryValue*>(root.release()));
2334 // Now put together a proxy configuration from the deserialized string.
2335 net::ProxyConfig pc;
2336 PopulateProxyConfig(*dict.get(), &pc);
2337
[email protected]be180c802009-10-23 06:33:312338 net::ProxyService* proxy_service =
2339 request_context_getter_->GetURLRequestContext()->proxy_service();
2340 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:172341 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2342 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:312343 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:172344 }
2345
2346 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2347 DCHECK(pc);
2348 bool no_proxy = false;
2349 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2350 // Make no changes to the ProxyConfig.
2351 return;
2352 }
2353 bool auto_config;
2354 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:482355 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:172356 }
2357 std::string pac_url;
2358 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:482359 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:172360 }
2361 std::string proxy_bypass_list;
2362 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:482363 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:172364 }
2365 std::string proxy_server;
2366 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:482367 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:172368 }
2369 }
2370
2371 private:
[email protected]be180c802009-10-23 06:33:312372 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:172373 std::string proxy_config_;
2374};
2375
2376
2377void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:312378 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
2379 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:172380 FilePath user_data_dir;
2381 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2382 ProfileManager* profile_manager = g_browser_process->profile_manager();
2383 DCHECK(profile_manager);
2384 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2385 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:312386 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:172387 }
[email protected]be180c802009-10-23 06:33:312388 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:172389
[email protected]fae20792009-10-28 20:31:582390 ChromeThread::PostTask(
2391 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:312392 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:172393}
2394
[email protected]4f3dc372009-02-24 00:10:292395void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:332396 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:292397 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292398 if (tab_tracker_->ContainsHandle(handle)) {
2399 NavigationController* tab = tab_tracker_->GetResource(handle);
2400 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2401 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:332402 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:292403 }
initial.commit09911bf2008-07-26 23:55:292404}
2405
[email protected]6a5670d22009-10-27 16:21:342406void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:142407 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:532408 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
2409 reply_message);
2410}
2411
2412void AutomationProvider::OpenNewBrowserWindowOfType(
2413 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:142414 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292415 // We may have no current browser windows open so don't rely on
2416 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:532417 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
2418 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:052419 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152420 if (show)
[email protected]15952e462008-11-14 00:29:052421 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292422}
2423
[email protected]71f65dd2009-02-11 19:14:562424void AutomationProvider::GetWindowForBrowser(int browser_handle,
2425 bool* success,
2426 int* handle) {
2427 *success = false;
2428 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292429
2430 if (browser_tracker_->ContainsHandle(browser_handle)) {
2431 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202432 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292433 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202434 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562435 *success = true;
initial.commit09911bf2008-07-26 23:55:292436 }
initial.commit09911bf2008-07-26 23:55:292437}
2438
2439void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562440 int browser_handle,
2441 bool* success,
2442 int* autocomplete_edit_handle) {
2443 *success = false;
2444 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292445
2446 if (browser_tracker_->ContainsHandle(browser_handle)) {
2447 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062448 LocationBar* loc_bar = browser->window()->GetLocationBar();
2449 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292450 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562451 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2452 *success = true;
initial.commit09911bf2008-07-26 23:55:292453 }
initial.commit09911bf2008-07-26 23:55:292454}
initial.commit09911bf2008-07-26 23:55:292455
[email protected]71f65dd2009-02-11 19:14:562456void AutomationProvider::ShowInterstitialPage(int tab_handle,
2457 const std::string& html_text,
2458 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292459 if (tab_tracker_->ContainsHandle(tab_handle)) {
2460 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112461 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402462
[email protected]7dad3d5f2010-03-04 00:27:012463 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:402464 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342465 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402466 GURL("about:interstitial"),
2467 html_text);
2468 interstitial->Show();
2469 return;
initial.commit09911bf2008-07-26 23:55:292470 }
[email protected]71f65dd2009-02-11 19:14:562471
[email protected]457f5cf2009-08-18 16:37:522472 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
2473 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562474 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292475}
2476
[email protected]71f65dd2009-02-11 19:14:562477void AutomationProvider::HideInterstitialPage(int tab_handle,
2478 bool* success) {
2479 *success = false;
[email protected]57c6a652009-05-04 07:58:342480 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2481 if (tab_contents && tab_contents->interstitial_page()) {
2482 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562483 *success = true;
initial.commit09911bf2008-07-26 23:55:292484 }
initial.commit09911bf2008-07-26 23:55:292485}
2486
[email protected]71f65dd2009-02-11 19:14:562487void AutomationProvider::CloseTab(int tab_handle,
2488 bool wait_until_closed,
2489 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292490 if (tab_tracker_->ContainsHandle(tab_handle)) {
2491 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2492 int index;
2493 Browser* browser = Browser::GetBrowserForController(controller, &index);
2494 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142495 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112496 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452497 return;
initial.commit09911bf2008-07-26 23:55:292498 }
[email protected]de246f52009-02-25 18:25:452499
2500 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:552501 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292502}
2503
[email protected]71f65dd2009-02-11 19:14:562504void AutomationProvider::CloseBrowser(int browser_handle,
2505 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292506 if (browser_tracker_->ContainsHandle(browser_handle)) {
2507 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562508 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562509 reply_message);
[email protected]f3e99e32008-07-30 04:48:392510 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292511 } else {
2512 NOTREACHED();
2513 }
2514}
2515
[email protected]71f65dd2009-02-11 19:14:562516void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2517 if (browser_tracker_->ContainsHandle(browser_handle)) {
2518 Browser* browser = browser_tracker_->GetResource(browser_handle);
2519 browser->window()->Close();
2520 } else {
2521 NOTREACHED();
2522 }
2523}
2524
[email protected]71f65dd2009-02-11 19:14:562525void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2526 IPC::Message* reply_message) {
2527 if (tab_tracker_->ContainsHandle(tab_handle)) {
2528 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2529 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:142530 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:562531 }
2532}
2533
[email protected]71f65dd2009-02-11 19:14:562534void AutomationProvider::GetSecurityState(int handle, bool* success,
2535 SecurityStyle* security_style,
2536 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:482537 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:292538 if (tab_tracker_->ContainsHandle(handle)) {
2539 NavigationController* tab = tab_tracker_->GetResource(handle);
2540 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562541 *success = true;
2542 *security_style = entry->ssl().security_style();
2543 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:482544 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292545 } else {
[email protected]71f65dd2009-02-11 19:14:562546 *success = false;
2547 *security_style = SECURITY_STYLE_UNKNOWN;
2548 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:482549 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292550 }
2551}
2552
[email protected]71f65dd2009-02-11 19:14:562553void AutomationProvider::GetPageType(int handle, bool* success,
2554 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:292555 if (tab_tracker_->ContainsHandle(handle)) {
2556 NavigationController* tab = tab_tracker_->GetResource(handle);
2557 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562558 *page_type = entry->page_type();
2559 *success = true;
initial.commit09911bf2008-07-26 23:55:292560 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:342561 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:562562 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:342563 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:562564 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292565 } else {
[email protected]71f65dd2009-02-11 19:14:562566 *success = false;
2567 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292568 }
2569}
2570
[email protected]84abba62009-10-07 17:01:442571void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
2572 int* duration_ms) {
2573 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
2574 event_name);
2575}
2576
[email protected]71f65dd2009-02-11 19:14:562577void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
2578 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292579 if (tab_tracker_->ContainsHandle(handle)) {
2580 NavigationController* tab = tab_tracker_->GetResource(handle);
2581 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072582 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:402583 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:472584 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:342585 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:292586 if (ssl_blocking_page) {
2587 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:012588 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:562589 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:292590 return;
2591 }
2592 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:522593 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2594 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:562595 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292596 return;
2597 }
2598 }
2599 }
2600 // We failed.
[email protected]457f5cf2009-08-18 16:37:522601 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2602 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562603 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292604}
2605
[email protected]71f65dd2009-02-11 19:14:562606void AutomationProvider::BringBrowserToFront(int browser_handle,
2607 bool* success) {
initial.commit09911bf2008-07-26 23:55:292608 if (browser_tracker_->ContainsHandle(browser_handle)) {
2609 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:062610 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:562611 *success = true;
initial.commit09911bf2008-07-26 23:55:292612 } else {
[email protected]71f65dd2009-02-11 19:14:562613 *success = false;
initial.commit09911bf2008-07-26 23:55:292614 }
2615}
2616
[email protected]71f65dd2009-02-11 19:14:562617void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
2618 int message_num,
2619 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:292620 if (browser_tracker_->ContainsHandle(browser_handle)) {
2621 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562622 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:142623 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:292624 } else {
[email protected]71f65dd2009-02-11 19:14:562625 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:292626 }
2627}
2628
[email protected]71f65dd2009-02-11 19:14:562629void AutomationProvider::PrintNow(int tab_handle,
2630 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:572631 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:342632 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2633 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:292634 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572635 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:142636 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:342637 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:572638 return;
initial.commit09911bf2008-07-26 23:55:292639 }
[email protected]71f65dd2009-02-11 19:14:562640 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
2641 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292642}
[email protected]d301c952009-07-13 15:02:412643
[email protected]71f65dd2009-02-11 19:14:562644void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:112645 const FilePath& file_name,
2646 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:562647 int type,
2648 bool* success) {
initial.commit09911bf2008-07-26 23:55:292649 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562650 *success = false;
initial.commit09911bf2008-07-26 23:55:292651 return;
2652 }
2653
2654 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2655 Browser* browser = FindAndActivateTab(nav);
2656 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142657 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562658 *success = false;
initial.commit09911bf2008-07-26 23:55:292659 return;
2660 }
2661
initial.commit09911bf2008-07-26 23:55:292662 SavePackage::SavePackageType save_type =
2663 static_cast<SavePackage::SavePackageType>(type);
2664 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2665 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:342666 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292667
[email protected]71f65dd2009-02-11 19:14:562668 *success = true;
initial.commit09911bf2008-07-26 23:55:292669}
2670
[email protected]71f65dd2009-02-11 19:14:562671void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2672 bool* success,
2673 std::wstring* text) {
2674 *success = false;
initial.commit09911bf2008-07-26 23:55:292675 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562676 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522677 GetText();
[email protected]71f65dd2009-02-11 19:14:562678 *success = true;
initial.commit09911bf2008-07-26 23:55:292679 }
initial.commit09911bf2008-07-26 23:55:292680}
2681
[email protected]71f65dd2009-02-11 19:14:562682void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2683 const std::wstring& text,
2684 bool* success) {
2685 *success = false;
initial.commit09911bf2008-07-26 23:55:292686 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522687 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2688 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562689 *success = true;
initial.commit09911bf2008-07-26 23:55:292690 }
initial.commit09911bf2008-07-26 23:55:292691}
2692
2693void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562694 int autocomplete_edit_handle,
2695 bool* success,
2696 std::vector<AutocompleteMatchData>* matches) {
2697 *success = false;
initial.commit09911bf2008-07-26 23:55:292698 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272699 const AutocompleteResult& result = autocomplete_edit_tracker_->
2700 GetResource(autocomplete_edit_handle)->model()->result();
2701 for (AutocompleteResult::const_iterator i = result.begin();
2702 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562703 matches->push_back(AutocompleteMatchData(*i));
2704 *success = true;
initial.commit09911bf2008-07-26 23:55:292705 }
initial.commit09911bf2008-07-26 23:55:292706}
2707
2708void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562709 int autocomplete_edit_handle,
2710 bool* success,
2711 bool* query_in_progress) {
2712 *success = false;
2713 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292714 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562715 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522716 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562717 *success = true;
initial.commit09911bf2008-07-26 23:55:292718 }
initial.commit09911bf2008-07-26 23:55:292719}
2720
[email protected]63514af2010-03-30 17:17:232721#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:192722
[email protected]5ae5bed2009-08-21 18:52:442723#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:392724
[email protected]57c6a652009-05-04 07:58:342725TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572726 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572727 if (tab_tracker_->ContainsHandle(handle)) {
2728 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342729 if (tab)
2730 *tab = nav_controller;
2731 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572732 }
[email protected]57c6a652009-05-04 07:58:342733 return NULL;
[email protected]20e93d12008-08-28 16:31:572734}
2735
initial.commit09911bf2008-07-26 23:55:292736TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2737 : AutomationProvider(profile) {
2738 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142739 registrar_.Add(this, NotificationType::SESSION_END,
2740 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292741}
2742
2743TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292744 BrowserList::RemoveObserver(this);
2745}
2746
2747void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:142748 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:292749 AutomationProvider::OnChannelError();
2750}
2751
2752void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2753 // For backwards compatibility with the testing automation interface, we
2754 // want the automation provider (and hence the process) to go away when the
2755 // last browser goes away.
2756 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292757 // If you change this, update Observer for NotificationType::SESSION_END
2758 // below.
[email protected]295039bd2008-08-15 04:32:572759 MessageLoop::current()->PostTask(FROM_HERE,
2760 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292761 }
2762}
2763
2764void TestingAutomationProvider::Observe(NotificationType type,
2765 const NotificationSource& source,
2766 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562767 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292768 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2769 // before the task runs resulting in this object not being deleted. This
2770 // Release balance out the Release scheduled by OnBrowserRemoving.
2771 Release();
2772}
[email protected]295039bd2008-08-15 04:32:572773
2774void TestingAutomationProvider::OnRemoveProvider() {
2775 AutomationProviderList::GetInstance()->RemoveProvider(this);
2776}
[email protected]8a3422c92008-09-24 17:42:422777
[email protected]816633a2009-11-11 21:48:182778void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:562779 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422780 if (tab_tracker_->ContainsHandle(handle)) {
2781 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342782 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112783 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422784 }
[email protected]8a3422c92008-09-24 17:42:422785}
2786
[email protected]816633a2009-11-11 21:48:182787void AutomationProvider::ClickInfoBarAccept(int handle,
2788 int info_bar_index,
2789 bool wait_for_navigation,
2790 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422791 bool success = false;
2792 if (tab_tracker_->ContainsHandle(handle)) {
2793 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2794 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112795 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422796 if (info_bar_index >= 0 && info_bar_index < count) {
2797 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:012798 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:422799 }
[email protected]eb9ba192008-12-02 02:41:342800 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112801 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342802 info_bar_index);
2803 if (delegate->AsConfirmInfoBarDelegate())
2804 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422805 success = true;
2806 }
2807 }
[email protected]4f3dc372009-02-24 00:10:292808 }
[email protected]58f622a62009-10-04 01:17:552809
2810 // This "!wait_for_navigation || !success condition" logic looks suspicious.
2811 // It will send a failure message when success is true but
2812 // |wait_for_navigation| is false.
2813 // TODO(phajdan.jr): investgate whether the reply param (currently
2814 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:422815 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:182816 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:522817 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:422818}
2819
[email protected]71f65dd2009-02-11 19:14:562820void AutomationProvider::GetLastNavigationTime(int handle,
2821 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422822 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562823 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422824}
2825
[email protected]71f65dd2009-02-11 19:14:562826void AutomationProvider::WaitForNavigation(int handle,
2827 int64 last_navigation_time,
2828 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:252829 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:422830 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:252831
[email protected]8a3422c92008-09-24 17:42:422832 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562833 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:522834 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
2835 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:552836 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:292837 return;
[email protected]8a3422c92008-09-24 17:42:422838 }
2839
[email protected]7dad3d5f2010-03-04 00:27:012840 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:422841}
2842
[email protected]71f65dd2009-02-11 19:14:562843void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162844 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562845 int value,
2846 bool* success) {
2847 *success = false;
[email protected]8a3422c92008-09-24 17:42:422848 if (browser_tracker_->ContainsHandle(handle)) {
2849 Browser* browser = browser_tracker_->GetResource(handle);
2850 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562851 *success = true;
[email protected]8a3422c92008-09-24 17:42:422852 }
[email protected]8a3422c92008-09-24 17:42:422853}
[email protected]97fa6ce32008-12-19 01:48:162854
[email protected]71f65dd2009-02-11 19:14:562855void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162856 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562857 const std::wstring& value,
2858 bool* success) {
2859 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162860 if (browser_tracker_->ContainsHandle(handle)) {
2861 Browser* browser = browser_tracker_->GetResource(handle);
2862 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562863 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162864 }
[email protected]97fa6ce32008-12-19 01:48:162865}
2866
[email protected]71f65dd2009-02-11 19:14:562867void AutomationProvider::GetBooleanPreference(int handle,
2868 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:362869 bool* success,
2870 bool* value) {
[email protected]71f65dd2009-02-11 19:14:562871 *success = false;
2872 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162873 if (browser_tracker_->ContainsHandle(handle)) {
2874 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562875 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2876 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162877 }
[email protected]97fa6ce32008-12-19 01:48:162878}
2879
[email protected]71f65dd2009-02-11 19:14:562880void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162881 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562882 bool value,
2883 bool* success) {
2884 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162885 if (browser_tracker_->ContainsHandle(handle)) {
2886 Browser* browser = browser_tracker_->GetResource(handle);
2887 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562888 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162889 }
[email protected]97fa6ce32008-12-19 01:48:162890}
2891
2892// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562893void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:402894 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162895 if (tab_tracker_->ContainsHandle(tab_handle)) {
2896 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2897 Browser* browser = FindAndActivateTab(nav);
2898 DCHECK(browser);
2899
[email protected]57c6a652009-05-04 07:58:342900 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2901 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162902 }
[email protected]97fa6ce32008-12-19 01:48:162903}
2904
[email protected]b8f48d12009-11-09 20:14:362905// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562906void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402907 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562908 bool* success) {
2909 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162910 if (tab_tracker_->ContainsHandle(tab_handle)) {
2911 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:192912 if (!nav)
2913 return;
[email protected]97fa6ce32008-12-19 01:48:162914 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:162915
[email protected]2f2afba2010-04-01 01:53:192916 // If the browser has UI, simulate what a user would do.
2917 // Activate the tab and then click the encoding menu.
2918 if (browser &&
2919 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162920 int selected_encoding_id =
2921 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2922 if (selected_encoding_id) {
2923 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562924 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162925 }
[email protected]2f2afba2010-04-01 01:53:192926 } else {
2927 // There is no UI, Chrome probably runs as Chrome-Frame mode.
2928 // Try to get TabContents and call its override_encoding method.
2929 TabContents* contents = nav->tab_contents();
2930 if (!contents)
2931 return;
2932 const std::string selected_encoding =
2933 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
2934 if (selected_encoding.empty())
2935 return;
2936 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:162937 }
2938 }
[email protected]97fa6ce32008-12-19 01:48:162939}
[email protected]5bcdb312009-01-07 21:43:202940
[email protected]4d434a1a2009-02-11 21:06:572941void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202942 SavePackage::SetShouldPromptUser(should_prompt);
2943}
[email protected]87eab222009-03-13 00:47:452944
[email protected]66ba4932009-06-04 19:22:132945void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2946 *count = -1; // -1 is the error code
2947 if (tab_tracker_->ContainsHandle(handle)) {
2948 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2949 TabContents* tab_contents = nav_controller->tab_contents();
2950 if (tab_contents) {
2951 BlockedPopupContainer* container =
2952 tab_contents->blocked_popup_container();
2953 if (container) {
2954 *count = static_cast<int>(container->GetBlockedPopupCount());
2955 } else {
2956 // If we don't have a container, we don't have any blocked popups to
2957 // contain!
2958 *count = 0;
2959 }
2960 }
2961 }
2962}
[email protected]f7a68432009-07-29 23:18:192963
2964void AutomationProvider::SelectAll(int tab_handle) {
2965 RenderViewHost* view = GetViewForTab(tab_handle);
2966 if (!view) {
2967 NOTREACHED();
2968 return;
2969 }
2970
2971 view->SelectAll();
2972}
2973
2974void AutomationProvider::Cut(int tab_handle) {
2975 RenderViewHost* view = GetViewForTab(tab_handle);
2976 if (!view) {
2977 NOTREACHED();
2978 return;
2979 }
2980
2981 view->Cut();
2982}
2983
2984void AutomationProvider::Copy(int tab_handle) {
2985 RenderViewHost* view = GetViewForTab(tab_handle);
2986 if (!view) {
2987 NOTREACHED();
2988 return;
2989 }
2990
2991 view->Copy();
2992}
2993
2994void AutomationProvider::Paste(int tab_handle) {
2995 RenderViewHost* view = GetViewForTab(tab_handle);
2996 if (!view) {
2997 NOTREACHED();
2998 return;
2999 }
3000
3001 view->Paste();
3002}
3003
3004void AutomationProvider::ReloadAsync(int tab_handle) {
3005 if (tab_tracker_->ContainsHandle(tab_handle)) {
3006 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3007 if (!tab) {
3008 NOTREACHED();
3009 return;
3010 }
3011
[email protected]106a0812010-03-18 00:15:123012 const bool check_for_repost = true;
3013 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193014 }
3015}
3016
3017void AutomationProvider::StopAsync(int tab_handle) {
3018 RenderViewHost* view = GetViewForTab(tab_handle);
3019 if (!view) {
[email protected]8b2b3312009-09-14 18:38:363020 // We tolerate StopAsync being called even before a view has been created.
3021 // So just log a warning instead of a NOTREACHED().
3022 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:193023 return;
3024 }
3025
3026 view->Stop();
3027}
3028
[email protected]1bb5f892009-10-06 01:44:573029void AutomationProvider::OnSetPageFontSize(int tab_handle,
3030 int font_size) {
3031 AutomationPageFontSize automation_font_size =
3032 static_cast<AutomationPageFontSize>(font_size);
3033
3034 if (automation_font_size < SMALLEST_FONT ||
3035 automation_font_size > LARGEST_FONT) {
3036 DLOG(ERROR) << "Invalid font size specified : "
3037 << font_size;
3038 return;
3039 }
3040
3041 if (tab_tracker_->ContainsHandle(tab_handle)) {
3042 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3043 DCHECK(tab != NULL);
3044 if (tab && tab->tab_contents()) {
3045 DCHECK(tab->tab_contents()->profile() != NULL);
3046 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
3047 prefs::kWebKitDefaultFontSize, font_size);
3048 }
3049 }
3050}
3051
[email protected]bc73b4e52010-03-26 04:16:203052void AutomationProvider::RemoveBrowsingData(int remove_mask) {
3053 BrowsingDataRemover* remover;
3054 remover = new BrowsingDataRemover(profile(),
3055 BrowsingDataRemover::EVERYTHING, // All time periods.
3056 base::Time());
3057 remover->Remove(remove_mask);
3058 // BrowsingDataRemover deletes itself.
3059}
[email protected]1bb5f892009-10-06 01:44:573060
[email protected]2949e90d2009-08-21 15:32:523061void AutomationProvider::WaitForBrowserWindowCountToBecome(
3062 int target_count, IPC::Message* reply_message) {
3063 if (static_cast<int>(BrowserList::size()) == target_count) {
3064 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
3065 reply_message, true);
3066 Send(reply_message);
3067 return;
3068 }
3069
3070 // Set up an observer (it will delete itself).
3071 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
3072}
3073
3074void AutomationProvider::WaitForAppModalDialogToBeShown(
3075 IPC::Message* reply_message) {
3076 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
3077 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
3078 reply_message, true);
3079 Send(reply_message);
3080 return;
3081 }
3082
3083 // Set up an observer (it will delete itself).
3084 new AppModalDialogShownObserver(this, reply_message);
3085}
3086
[email protected]1126a1d32009-08-26 15:39:263087void AutomationProvider::GoBackBlockUntilNavigationsComplete(
3088 int handle, int number_of_navigations, IPC::Message* reply_message) {
3089 if (tab_tracker_->ContainsHandle(handle)) {
3090 NavigationController* tab = tab_tracker_->GetResource(handle);
3091 Browser* browser = FindAndActivateTab(tab);
3092 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:013093 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3094 false);
[email protected]1126a1d32009-08-26 15:39:263095 browser->GoBack(CURRENT_TAB);
3096 return;
3097 }
3098 }
3099
3100 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
3101 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3102 Send(reply_message);
3103}
3104
3105void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
3106 int handle, int number_of_navigations, IPC::Message* reply_message) {
3107 if (tab_tracker_->ContainsHandle(handle)) {
3108 NavigationController* tab = tab_tracker_->GetResource(handle);
3109 Browser* browser = FindAndActivateTab(tab);
3110 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:013111 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3112 false);
[email protected]1126a1d32009-08-26 15:39:263113 browser->GoForward(CURRENT_TAB);
3114 return;
3115 }
3116 }
3117
3118 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
3119 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3120 Send(reply_message);
3121}
3122
[email protected]f7a68432009-07-29 23:18:193123RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
3124 if (tab_tracker_->ContainsHandle(tab_handle)) {
3125 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3126 if (!tab) {
3127 NOTREACHED();
3128 return NULL;
3129 }
3130
3131 TabContents* tab_contents = tab->tab_contents();
3132 if (!tab_contents) {
3133 NOTREACHED();
3134 return NULL;
3135 }
3136
3137 RenderViewHost* view_host = tab_contents->render_view_host();
3138 return view_host;
3139 }
3140
3141 return NULL;
3142}
[email protected]675595f2009-08-26 22:32:043143
3144void AutomationProvider::GetBrowserForWindow(int window_handle,
3145 bool* success,
3146 int* browser_handle) {
3147 *success = false;
3148 *browser_handle = 0;
3149
3150 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
3151 if (!window)
3152 return;
3153
3154 BrowserList::const_iterator iter = BrowserList::begin();
3155 for (;iter != BrowserList::end(); ++iter) {
3156 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
3157 if (window == this_window) {
3158 // Add() returns the existing handle for the resource if any.
3159 *browser_handle = browser_tracker_->Add(*iter);
3160 *success = true;
3161 return;
3162 }
3163 }
3164}
[email protected]d11c8e92009-10-20 23:26:403165
3166void AutomationProvider::InstallExtension(const FilePath& crx_path,
3167 IPC::Message* reply_message) {
3168 ExtensionsService* service = profile_->GetExtensionsService();
3169 if (service) {
3170 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193171 new ExtensionInstallNotificationObserver(this,
3172 AutomationMsg_InstallExtension::ID,
3173 reply_message);
[email protected]d11c8e92009-10-20 23:26:403174
3175 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:163176 scoped_refptr<CrxInstaller> installer(
3177 new CrxInstaller(install_dir,
3178 service,
3179 NULL)); // silent install, no UI
3180 installer->set_allow_privilege_increase(true);
3181 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:403182 } else {
3183 AutomationMsg_InstallExtension::WriteReplyParams(
3184 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3185 Send(reply_message);
3186 }
3187}
3188
3189void AutomationProvider::LoadExpandedExtension(
3190 const FilePath& extension_dir,
3191 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:383192 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:403193 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193194 new ExtensionInstallNotificationObserver(
3195 this,
3196 AutomationMsg_LoadExpandedExtension::ID,
3197 reply_message);
[email protected]d11c8e92009-10-20 23:26:403198
3199 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:403200 } else {
3201 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
3202 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3203 Send(reply_message);
3204 }
3205}
[email protected]673fd2c02010-02-04 23:10:003206
[email protected]a1e62d12010-03-16 02:18:433207void AutomationProvider::GetEnabledExtensions(
3208 std::vector<FilePath>* result) {
3209 ExtensionsService* service = profile_->GetExtensionsService();
3210 DCHECK(service);
3211 if (service->extensions_enabled()) {
3212 const ExtensionList* extensions = service->extensions();
3213 DCHECK(extensions);
3214 for (size_t i = 0; i < extensions->size(); ++i) {
3215 Extension* extension = (*extensions)[i];
3216 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:123217 if (extension->location() == Extension::INTERNAL ||
3218 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:073219 result->push_back(extension->path());
3220 }
[email protected]a1e62d12010-03-16 02:18:433221 }
3222 }
3223}
3224
[email protected]790788ac2010-04-06 17:52:193225void AutomationProvider::WaitForExtensionTestResult(
3226 IPC::Message* reply_message) {
3227 DCHECK(reply_message_ == NULL);
3228 reply_message_ = reply_message;
3229 // Call MaybeSendResult, because the result might have come in before
3230 // we were waiting on it.
3231 extension_test_result_observer_->MaybeSendResult();
3232}
3233
3234void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:093235 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:193236 ExtensionsService* service = profile_->GetExtensionsService();
3237 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3238 if (service && manager) {
3239 // The observer will delete itself when done.
3240 new ExtensionReadyNotificationObserver(
3241 manager,
3242 this,
3243 AutomationMsg_InstallExtensionAndGetHandle::ID,
3244 reply_message);
3245
[email protected]d7e5525d2010-04-20 14:37:093246 ExtensionInstallUI* client =
3247 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:193248 scoped_refptr<CrxInstaller> installer(
3249 new CrxInstaller(service->install_directory(),
3250 service,
[email protected]d7e5525d2010-04-20 14:37:093251 client));
[email protected]790788ac2010-04-06 17:52:193252 installer->set_allow_privilege_increase(true);
3253 installer->InstallCrx(crx_path);
3254 } else {
3255 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
3256 reply_message, 0);
3257 Send(reply_message);
3258 }
3259}
3260
3261void AutomationProvider::UninstallExtension(int extension_handle,
3262 bool* success) {
3263 *success = false;
3264 Extension* extension = GetExtension(extension_handle);
3265 ExtensionsService* service = profile_->GetExtensionsService();
3266 if (extension && service) {
3267 ExtensionUnloadNotificationObserver observer;
3268 service->UninstallExtension(extension->id(), false);
3269 // The extension unload notification should have been sent synchronously
3270 // with the uninstall. Just to be safe, check that it was received.
3271 *success = observer.did_receive_unload_notification();
3272 }
3273}
3274
3275void AutomationProvider::EnableExtension(int extension_handle,
3276 IPC::Message* reply_message) {
3277 Extension* extension = GetDisabledExtension(extension_handle);
3278 ExtensionsService* service = profile_->GetExtensionsService();
3279 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3280 // Only enable if this extension is disabled.
3281 if (extension && service && manager) {
3282 // The observer will delete itself when done.
3283 new ExtensionReadyNotificationObserver(
3284 manager,
3285 this,
3286 AutomationMsg_EnableExtension::ID,
3287 reply_message);
3288 service->EnableExtension(extension->id());
3289 } else {
3290 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
3291 Send(reply_message);
3292 }
3293}
3294
3295void AutomationProvider::DisableExtension(int extension_handle,
3296 bool* success) {
3297 *success = false;
3298 Extension* extension = GetEnabledExtension(extension_handle);
3299 ExtensionsService* service = profile_->GetExtensionsService();
3300 if (extension && service) {
3301 ExtensionUnloadNotificationObserver observer;
3302 service->DisableExtension(extension->id());
3303 // The extension unload notification should have been sent synchronously
3304 // with the disable. Just to be safe, check that it was received.
3305 *success = observer.did_receive_unload_notification();
3306 }
3307}
3308
3309void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
3310 int extension_handle, int browser_handle,
3311 IPC::Message* reply_message) {
3312 bool success = false;
3313 Extension* extension = GetEnabledExtension(extension_handle);
3314 ExtensionsService* service = profile_->GetExtensionsService();
3315 ExtensionMessageService* message_service =
3316 profile_->GetExtensionMessageService();
3317 Browser* browser = browser_tracker_->GetResource(browser_handle);
3318 if (extension && service && message_service && browser) {
3319 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
3320 if (extension->page_action()) {
3321 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
3322 browser->profile(), extension->id(), "action", tab_id, "", 1);
3323 success = true;
3324 } else if (extension->browser_action()) {
3325 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
3326 browser->profile(), extension->id(), browser);
3327 success = true;
3328 }
3329 }
3330 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3331 reply_message, success);
3332 Send(reply_message);
3333}
3334
3335void AutomationProvider::MoveExtensionBrowserAction(
3336 int extension_handle, int index, bool* success) {
3337 *success = false;
3338 Extension* extension = GetEnabledExtension(extension_handle);
3339 ExtensionsService* service = profile_->GetExtensionsService();
3340 if (extension && service) {
3341 ExtensionToolbarModel* toolbar = service->toolbar_model();
3342 if (toolbar) {
3343 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3344 toolbar->MoveBrowserAction(extension, index);
3345 *success = true;
3346 } else {
3347 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3348 }
3349 }
3350 }
3351}
3352
3353void AutomationProvider::GetExtensionProperty(
3354 int extension_handle,
3355 AutomationMsg_ExtensionProperty type,
3356 bool* success,
3357 std::string* value) {
3358 *success = false;
3359 Extension* extension = GetExtension(extension_handle);
3360 ExtensionsService* service = profile_->GetExtensionsService();
3361 if (extension && service) {
3362 ExtensionToolbarModel* toolbar = service->toolbar_model();
3363 int found_index = -1;
3364 int index = 0;
3365 switch (type) {
3366 case AUTOMATION_MSG_EXTENSION_ID:
3367 *value = extension->id();
3368 *success = true;
3369 break;
3370 case AUTOMATION_MSG_EXTENSION_NAME:
3371 *value = extension->name();
3372 *success = true;
3373 break;
3374 case AUTOMATION_MSG_EXTENSION_VERSION:
3375 *value = extension->VersionString();
3376 *success = true;
3377 break;
3378 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3379 if (toolbar) {
3380 for (ExtensionList::const_iterator iter = toolbar->begin();
3381 iter != toolbar->end(); iter++) {
3382 // Skip this extension if we are in incognito mode
3383 // and it is not incognito-enabled.
3384 if (profile_->IsOffTheRecord() &&
3385 !service->IsIncognitoEnabled(*iter))
3386 continue;
3387 if (*iter == extension) {
3388 found_index = index;
3389 break;
3390 }
3391 index++;
3392 }
3393 *value = IntToString(found_index);
3394 *success = true;
3395 }
3396 break;
3397 default:
3398 LOG(WARNING) << "Trying to get undefined extension property";
3399 break;
3400 }
3401 }
3402}
3403
[email protected]673fd2c02010-02-04 23:10:003404void AutomationProvider::SaveAsAsync(int tab_handle) {
3405 NavigationController* tab = NULL;
3406 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3407 if (tab_contents)
3408 tab_contents->OnSavePage();
3409}
[email protected]7dad3d5f2010-03-04 00:27:013410
3411void AutomationProvider::SetContentSetting(
3412 int handle,
3413 const std::string& host,
3414 ContentSettingsType content_type,
3415 ContentSetting setting,
3416 bool* success) {
3417 *success = false;
3418 if (browser_tracker_->ContainsHandle(handle)) {
3419 Browser* browser = browser_tracker_->GetResource(handle);
3420 HostContentSettingsMap* map =
3421 browser->profile()->GetHostContentSettingsMap();
3422 if (host.empty()) {
3423 map->SetDefaultContentSetting(content_type, setting);
3424 } else {
[email protected]0314ae02010-04-08 09:18:293425 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3426 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013427 }
3428 *success = true;
3429 }
3430}
[email protected]cc824372010-03-31 15:33:013431
3432#if !defined(TOOLKIT_VIEWS)
3433void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3434 NOTIMPLEMENTED();
3435};
3436
3437void AutomationProvider::WaitForFocusedViewIDToChange(
3438 int handle, int previous_view_id, IPC::Message* reply_message) {
3439 NOTIMPLEMENTED();
3440}
3441
3442void AutomationProvider::StartTrackingPopupMenus(
3443 int browser_handle, bool* success) {
3444 NOTIMPLEMENTED();
3445}
3446
3447void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3448 NOTIMPLEMENTED();
3449}
3450#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:093451
3452void AutomationProvider::ResetToDefaultTheme() {
3453 profile_->ClearTheme();
3454}