blob: 8c4b5489270d206bb76dd13f8107eb904832ca95 [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
[email protected]e53668962010-06-23 15:35:251740 string16 search_text;
1741 args->GetString("search_text", &search_text);
[email protected]e6e376e2010-04-19 21:41:361742
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;
[email protected]e53668962010-06-23 15:35:251772 string16 title;
[email protected]bbe6aa02010-05-07 17:27:291773 base::Time time = base::Time::Now();
1774
1775 if (item->GetString("url", &url_text)) {
1776 GURL gurl(url_text);
[email protected]e53668962010-06-23 15:35:251777 item->GetString("title", &title); // Don't care if it fails.
[email protected]bbe6aa02010-05-07 17:27:291778 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);
[email protected]e53668962010-06-23 15:35:251796 if (title.length())
[email protected]bbe6aa02010-05-07 17:27:291797 hs->SetPageTitle(gurl, title);
[email protected]bbe6aa02010-05-07 17:27:291798 } else {
1799 json_return = "{\"error\": \"bad args (no URL in dict?).\"}";
1800 reply_return = false;
1801 }
1802
1803 AutomationMsg_SendJSONRequest::WriteReplyParams(
1804 reply_message, json_return, reply_return);
1805 Send(reply_message);
1806}
1807
[email protected]24e2b102010-04-29 17:56:471808// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361809// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581810void AutomationProvider::GetDownloadsInfo(Browser* browser,
1811 DictionaryValue* args,
1812 IPC::Message* reply_message) {
[email protected]d4adc292010-04-15 18:06:391813 std::string json_return;
1814 bool reply_return = true;
1815 AutomationProviderDownloadManagerObserver observer;
1816 std::vector<DownloadItem*> downloads;
1817 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1818
1819 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471820 json_return = "{\"error\": \"no download manager\"}";
[email protected]d4adc292010-04-15 18:06:391821 reply_return = false;
1822 } else {
1823 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1824 // since that would be transient; a download might enter and empty out
1825 // the current download queue too soon to be noticed.
1826 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1827 downloads = observer.Downloads();
1828 }
1829
1830 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1831 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1832 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1833 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1834 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1835
1836 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1837 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1838 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1839 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1840 std::string("DANGEROUS_BUT_VALIDATED");
1841
1842 ListValue* list_of_downloads = new ListValue;
1843 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1844 it != downloads.end();
1845 it++) { // Fill info about each download item.
1846 DictionaryValue* dl_item_value = new DictionaryValue;
1847 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1848 dl_item_value->SetString(L"url", (*it)->url().spec());
1849 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1850 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1851 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1852 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1853 dl_item_value->SetBoolean(L"open_when_complete",
1854 (*it)->open_when_complete());
1855 dl_item_value->SetBoolean(L"is_extension_install",
1856 (*it)->is_extension_install());
1857 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1858 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1859 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1860 dl_item_value->SetString(L"safety_state",
1861 safety_state_to_string[(*it)->safety_state()]);
1862 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1863 list_of_downloads->Append(dl_item_value);
1864 }
1865 return_value->Set(L"downloads", list_of_downloads);
1866 base::JSONWriter::Write(return_value.get(), false, &json_return);
1867
1868 AutomationMsg_SendJSONRequest::WriteReplyParams(
1869 reply_message, json_return, reply_return);
1870 Send(reply_message);
1871 // All value objects allocated above are owned by |return_value|
1872 // and get freed by it.
1873}
1874
[email protected]59a611242010-04-02 02:24:041875void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:581876 Browser* browser,
[email protected]59a611242010-04-02 02:24:041877 DictionaryValue* args,
1878 IPC::Message* reply_message) {
1879 std::string json_return;
1880 bool reply_return = true;
1881 AutomationProviderDownloadManagerObserver observer;
1882 std::vector<DownloadItem*> downloads;
1883
1884 // Look for a quick return.
1885 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471886 json_return = "{\"error\": \"no download manager\"}";
[email protected]59a611242010-04-02 02:24:041887 reply_return = false;
1888 } else {
1889 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
1890 FilePath());
1891 downloads = observer.Downloads();
1892 if (downloads.size() == 0) {
1893 json_return = "{}";
1894 }
1895 }
1896 if (!json_return.empty()) {
1897 AutomationMsg_SendJSONRequest::WriteReplyParams(
1898 reply_message, json_return, reply_return);
1899 Send(reply_message);
1900 }
1901
1902 // The observer owns itself. When the last observed item pings, it
1903 // deletes itself.
1904 AutomationProviderDownloadItemObserver* item_observer =
1905 new AutomationProviderDownloadItemObserver(
1906 this, reply_message, downloads.size());
1907 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
1908 i != downloads.end();
1909 i++) {
1910 (*i)->AddObserver(item_observer);
1911 }
1912}
1913
[email protected]24e2b102010-04-29 17:56:471914// Sample json input: { "command": "GetPrefsInfo" }
1915// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581916void AutomationProvider::GetPrefsInfo(Browser* browser,
1917 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:471918 IPC::Message* reply_message) {
1919 std::string json_return;
1920 bool reply_return = true;
1921
1922 const PrefService::PreferenceSet& prefs =
1923 profile_->GetPrefs()->preference_set();
1924 DictionaryValue* items = new DictionaryValue;
1925 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
1926 it != prefs.end(); ++it) {
1927 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
1928 }
1929 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1930 return_value->Set(L"prefs", items); // return_value owns items.
1931
1932 base::JSONWriter::Write(return_value.get(), false, &json_return);
1933 AutomationMsg_SendJSONRequest::WriteReplyParams(
1934 reply_message, json_return, reply_return);
1935 Send(reply_message);
1936}
1937
1938// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:581939void AutomationProvider::SetPrefs(Browser* browser,
1940 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:471941 IPC::Message* reply_message) {
1942 bool reply_return = true;
1943 std::string json_return = "{}";
1944 std::wstring path;
1945 Value* val;
1946 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
1947 PrefService* pref_service = profile_->GetPrefs();
1948 const PrefService::Preference* pref =
1949 pref_service->FindPreference(path.c_str());
1950 if (!pref) { // Not a registered pref.
1951 json_return = "{\"error\": \"pref not registered.\"}";
1952 reply_return = false;
1953 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
1954 json_return = "{\"error\": \"pref is managed. cannot be changed.\"}";
1955 reply_return = false;
1956 } else { // Set the pref.
1957 pref_service->Set(path.c_str(), *val);
1958 }
1959 } else {
1960 json_return = "{\"error\": \"no pref path or value given.\"}";
1961 reply_return = false;
1962 }
1963
1964 AutomationMsg_SendJSONRequest::WriteReplyParams(
1965 reply_message, json_return, reply_return);
1966 Send(reply_message);
1967}
1968
[email protected]53329582010-05-14 21:10:581969// Sample json input: { "command": "GetOmniboxInfo" }
1970// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
1971void AutomationProvider::GetOmniboxInfo(Browser* browser,
1972 DictionaryValue* args,
1973 IPC::Message* reply_message) {
1974 std::string json_return;
1975 bool reply_return = true;
1976 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1977
1978 LocationBar* loc_bar = browser->window()->GetLocationBar();
1979 AutocompleteEditView* edit_view = loc_bar->location_entry();
1980 AutocompleteEditModel* model = edit_view->model();
1981
1982 // Fill up matches.
1983 ListValue* matches = new ListValue;
1984 const AutocompleteResult& result = model->result();
1985 for (AutocompleteResult::const_iterator i = result.begin();
1986 i != result.end(); ++i) {
1987 const AutocompleteMatch& match = *i;
1988 DictionaryValue* item = new DictionaryValue; // owned by return_value
1989 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
1990 item->SetBoolean(L"starred", match.starred);
1991 item->SetString(L"destination_url", match.destination_url.spec());
1992 item->SetString(L"contents", match.contents);
1993 item->SetString(L"description", match.description);
1994 matches->Append(item);
1995 }
1996 return_value->Set(L"matches", matches);
1997
1998 // Fill up other properties.
1999 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2000 properties->SetBoolean(L"has_focus", model->has_focus());
2001 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2002 properties->SetString(L"keyword", model->keyword());
2003 properties->SetString(L"text", edit_view->GetText());
2004 return_value->Set(L"properties", properties);
2005
2006 base::JSONWriter::Write(return_value.get(), false, &json_return);
2007 AutomationMsg_SendJSONRequest::WriteReplyParams(
2008 reply_message, json_return, reply_return);
2009 Send(reply_message);
2010}
2011
2012// Sample json input: { "command": "SetOmniboxText",
2013// "text": "goog" }
2014void AutomationProvider::SetOmniboxText(Browser* browser,
2015 DictionaryValue* args,
2016 IPC::Message* reply_message) {
2017 std::string json_return = "{}";
2018 bool reply_return = true;
2019 std::wstring text;
2020
2021 if (!args->GetString(L"text", &text)) {
2022 json_return = "{\"error\": \"text missing\"}";
2023 reply_return = false;
2024 } else {
2025 browser->FocusLocationBar();
2026 LocationBar* loc_bar = browser->window()->GetLocationBar();
2027 AutocompleteEditView* edit_view = loc_bar->location_entry();
2028 edit_view->model()->OnSetFocus(false);
2029 edit_view->SetUserText(text);
2030 }
2031
2032 AutomationMsg_SendJSONRequest::WriteReplyParams(
2033 reply_message, json_return, reply_return);
2034 Send(reply_message);
2035}
2036
2037// Sample json input: { "command": "OmniboxMovePopupSelection",
2038// "count": 1 }
2039// Negative count implies up, positive implies down. Count values will be
2040// capped by the size of the popup list.
2041void AutomationProvider::OmniboxMovePopupSelection(
2042 Browser* browser,
2043 DictionaryValue* args,
2044 IPC::Message* reply_message) {
2045 std::string json_return = "{}";
2046 bool reply_return = true;
2047 int count;
2048
2049 if (!args->GetInteger(L"count", &count)) {
2050 json_return = "{\"error\": \"count missing\"}";
2051 reply_return = false;
2052 } else {
2053 LocationBar* loc_bar = browser->window()->GetLocationBar();
2054 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2055 model->OnUpOrDownKeyPressed(count);
2056 }
2057
2058 AutomationMsg_SendJSONRequest::WriteReplyParams(
2059 reply_message, json_return, reply_return);
2060 Send(reply_message);
2061}
2062
2063// Sample json input: { "command": "OmniboxAcceptInput" }
2064void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2065 DictionaryValue* args,
2066 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282067 NavigationController& controller =
2068 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122069 // Setup observer to wait until the selected item loads.
2070 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282071 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122072 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582073
2074 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582075}
2076
[email protected]a3cd5022010-06-16 18:25:292077// Sample json input: { "command": "GetInitialLoadTimes" }
2078// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2079void AutomationProvider::GetInitialLoadTimes(
2080 Browser*,
2081 DictionaryValue*,
2082 IPC::Message* reply_message) {
2083 scoped_ptr<DictionaryValue> return_value(
2084 initial_load_observer_->GetTimingInformation());
2085
2086 std::string json_return;
2087 base::JSONWriter::Write(return_value.get(), false, &json_return);
2088 AutomationMsg_SendJSONRequest::WriteReplyParams(
2089 reply_message, json_return, true);
2090 Send(reply_message);
2091}
2092
[email protected]f7d48012010-05-06 08:17:052093// Sample json input: { "command": "GetPluginsInfo" }
2094// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582095void AutomationProvider::GetPluginsInfo(Browser* browser,
2096 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052097 IPC::Message* reply_message) {
2098 std::string json_return;
2099 bool reply_return = true;
2100
2101 std::vector<WebPluginInfo> plugins;
2102 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2103 ListValue* items = new ListValue;
2104 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2105 it != plugins.end();
2106 ++it) {
2107 DictionaryValue* item = new DictionaryValue;
2108 item->SetString(L"name", it->name);
2109 item->SetString(L"path", it->path.value());
2110 item->SetString(L"version", it->version);
2111 item->SetString(L"desc", it->desc);
2112 item->SetBoolean(L"enabled", it->enabled);
2113 // Add info about mime types.
2114 ListValue* mime_types = new ListValue();
2115 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2116 it->mime_types.begin();
2117 type_it != it->mime_types.end();
2118 ++type_it) {
2119 DictionaryValue* mime_type = new DictionaryValue();
2120 mime_type->SetString(L"mimeType", type_it->mime_type);
2121 mime_type->SetString(L"description", type_it->description);
2122
2123 ListValue* file_extensions = new ListValue();
2124 for (std::vector<std::string>::const_iterator ext_it =
2125 type_it->file_extensions.begin();
2126 ext_it != type_it->file_extensions.end();
2127 ++ext_it) {
2128 file_extensions->Append(new StringValue(*ext_it));
2129 }
2130 mime_type->Set(L"fileExtensions", file_extensions);
2131
2132 mime_types->Append(mime_type);
2133 }
2134 item->Set(L"mimeTypes", mime_types);
2135 items->Append(item);
2136 }
2137 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2138 return_value->Set(L"plugins", items); // return_value owns items.
2139
2140 base::JSONWriter::Write(return_value.get(), false, &json_return);
2141 AutomationMsg_SendJSONRequest::WriteReplyParams(
2142 reply_message, json_return, reply_return);
2143 Send(reply_message);
2144}
2145
2146// Sample json input:
2147// { "command": "EnablePlugin",
2148// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582149void AutomationProvider::EnablePlugin(Browser* browser,
2150 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052151 IPC::Message* reply_message) {
2152 std::string json_return = "{}";
2153 bool reply_return = true;
2154 FilePath::StringType path;
2155 if (!args->GetString(L"path", &path)) {
2156 json_return = "{\"error\": \"path not specified.\"}";
2157 reply_return = false;
2158 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
2159 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2160 " for path %s.\"}", path.c_str());
2161 reply_return = false;
2162 }
2163
2164 AutomationMsg_SendJSONRequest::WriteReplyParams(
2165 reply_message, json_return, reply_return);
2166 Send(reply_message);
2167}
2168
2169// Sample json input:
2170// { "command": "DisablePlugin",
2171// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582172void AutomationProvider::DisablePlugin(Browser* browser,
2173 DictionaryValue* args,
2174 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052175 std::string json_return = "{}";
2176 bool reply_return = true;
2177 FilePath::StringType path;
2178 if (!args->GetString(L"path", &path)) {
2179 json_return = "{\"error\": \"path not specified.\"}";
2180 reply_return = false;
2181 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
2182 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2183 " for path %s.\"}", path.c_str());
2184 reply_return = false;
2185 }
2186
2187 AutomationMsg_SendJSONRequest::WriteReplyParams(
2188 reply_message, json_return, reply_return);
2189 Send(reply_message);
2190}
2191
[email protected]53329582010-05-14 21:10:582192void AutomationProvider::SendJSONRequest(int handle,
2193 std::string json_request,
2194 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:042195 Browser* browser = NULL;
2196 std::string error_string;
2197 scoped_ptr<Value> values;
2198
2199 // Basic error checking.
2200 if (browser_tracker_->ContainsHandle(handle)) {
2201 browser = browser_tracker_->GetResource(handle);
2202 }
2203 if (!browser) {
2204 error_string = "no browser object";
2205 } else {
2206 base::JSONReader reader;
2207 std::string error;
[email protected]ba399672010-04-06 15:42:392208 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
[email protected]59a611242010-04-02 02:24:042209 if (!error.empty()) {
2210 error_string = error;
2211 }
2212 }
2213
2214 // Make sure input is a dict with a string command.
2215 std::string command;
2216 DictionaryValue* dict_value = NULL;
2217 if (error_string.empty()) {
2218 if (values->GetType() != Value::TYPE_DICTIONARY) {
2219 error_string = "not a dict or no command key in dict";
2220 } else {
2221 // Ownership remains with "values" variable.
2222 dict_value = static_cast<DictionaryValue*>(values.get());
2223 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
2224 error_string = "no command key in dict or not a string command";
2225 }
2226 }
2227 }
2228
[email protected]24e2b102010-04-29 17:56:472229 // Map json commands to their handlers.
2230 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:052231 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
2232 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
2233 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
2234
[email protected]a9ff2c02010-05-13 17:33:052235 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
2236
[email protected]24e2b102010-04-29 17:56:472237 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:292238 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:052239
[email protected]53329582010-05-14 21:10:582240 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
2241 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
2242 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
2243 handler_map["OmniboxMovePopupSelection"] =
2244 &AutomationProvider::OmniboxMovePopupSelection;
2245
[email protected]24e2b102010-04-29 17:56:472246 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
2247 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:052248
[email protected]ef413ca2010-05-25 21:09:142249 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
2250
[email protected]f7d48012010-05-06 08:17:052251 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:472252 handler_map["WaitForAllDownloadsToComplete"] =
2253 &AutomationProvider::WaitForDownloadsToComplete;
2254
[email protected]a3cd5022010-06-16 18:25:292255 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
2256
[email protected]59a611242010-04-02 02:24:042257 if (error_string.empty()) {
[email protected]24e2b102010-04-29 17:56:472258 if (handler_map.find(std::string(command)) != handler_map.end()) {
[email protected]53329582010-05-14 21:10:582259 (this->*handler_map[command])(browser, dict_value, reply_message);
[email protected]59a611242010-04-02 02:24:042260 return;
2261 } else {
[email protected]24e2b102010-04-29 17:56:472262 error_string = "Unknown command. Options: ";
2263 for (std::map<std::string, JsonHandler>::const_iterator it =
2264 handler_map.begin(); it != handler_map.end(); ++it) {
2265 error_string += it->first + ", ";
2266 }
[email protected]59a611242010-04-02 02:24:042267 }
2268 }
2269
2270 // If we hit an error, return info.
[email protected]24e2b102010-04-29 17:56:472271 // Return a dict of {"error", "descriptive_string_for_error"}.
[email protected]59a611242010-04-02 02:24:042272 // Else return an empty dict.
2273 std::string json_string;
2274 bool success = true;
2275 if (!error_string.empty()) {
2276 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
2277 dict->SetString(L"error", error_string);
2278 base::JSONWriter::Write(dict.get(), false, &json_string);
2279 success = false;
2280 } else {
2281 json_string = "{}";
2282 }
2283 AutomationMsg_SendJSONRequest::WriteReplyParams(
2284 reply_message, json_string, success);
2285 Send(reply_message);
2286}
2287
initial.commit09911bf2008-07-26 23:55:292288void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562289 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342290 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2291 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562292 DCHECK(reply_message_ == NULL);
2293 reply_message_ = reply_message;
2294
[email protected]d9f9b792009-06-24 13:17:122295 DevToolsManager::GetInstance()->InspectElement(
2296 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292297 } else {
[email protected]71f65dd2009-02-11 19:14:562298 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2299 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292300 }
2301}
2302
2303void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092304 if (reply_message_) {
2305 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2306 num_resources);
2307 Send(reply_message_);
2308 reply_message_ = NULL;
2309 }
initial.commit09911bf2008-07-26 23:55:292310}
2311
[email protected]a7eee32f2009-05-22 18:08:172312class SetProxyConfigTask : public Task {
2313 public:
[email protected]be180c802009-10-23 06:33:312314 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
2315 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:252316 : request_context_getter_(request_context_getter),
2317 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:172318 virtual void Run() {
2319 // First, deserialize the JSON string. If this fails, log and bail.
2320 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:392321 std::string error_msg;
2322 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:172323 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2324 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:392325 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:172326 return;
2327 }
2328
2329 scoped_ptr<DictionaryValue> dict(
2330 static_cast<DictionaryValue*>(root.release()));
2331 // Now put together a proxy configuration from the deserialized string.
2332 net::ProxyConfig pc;
2333 PopulateProxyConfig(*dict.get(), &pc);
2334
[email protected]be180c802009-10-23 06:33:312335 net::ProxyService* proxy_service =
2336 request_context_getter_->GetURLRequestContext()->proxy_service();
2337 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:172338 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2339 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:312340 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:172341 }
2342
2343 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2344 DCHECK(pc);
2345 bool no_proxy = false;
2346 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2347 // Make no changes to the ProxyConfig.
2348 return;
2349 }
2350 bool auto_config;
2351 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:482352 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:172353 }
2354 std::string pac_url;
2355 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:482356 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:172357 }
2358 std::string proxy_bypass_list;
2359 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:482360 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:172361 }
2362 std::string proxy_server;
2363 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:482364 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:172365 }
2366 }
2367
2368 private:
[email protected]be180c802009-10-23 06:33:312369 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:172370 std::string proxy_config_;
2371};
2372
2373
2374void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:312375 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
2376 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:172377 FilePath user_data_dir;
2378 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2379 ProfileManager* profile_manager = g_browser_process->profile_manager();
2380 DCHECK(profile_manager);
2381 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2382 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:312383 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:172384 }
[email protected]be180c802009-10-23 06:33:312385 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:172386
[email protected]fae20792009-10-28 20:31:582387 ChromeThread::PostTask(
2388 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:312389 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:172390}
2391
[email protected]4f3dc372009-02-24 00:10:292392void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:332393 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:292394 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292395 if (tab_tracker_->ContainsHandle(handle)) {
2396 NavigationController* tab = tab_tracker_->GetResource(handle);
2397 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2398 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:332399 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:292400 }
initial.commit09911bf2008-07-26 23:55:292401}
2402
[email protected]6a5670d22009-10-27 16:21:342403void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:142404 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:532405 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
2406 reply_message);
2407}
2408
2409void AutomationProvider::OpenNewBrowserWindowOfType(
2410 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:142411 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292412 // We may have no current browser windows open so don't rely on
2413 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:532414 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
2415 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:052416 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152417 if (show)
[email protected]15952e462008-11-14 00:29:052418 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292419}
2420
[email protected]71f65dd2009-02-11 19:14:562421void AutomationProvider::GetWindowForBrowser(int browser_handle,
2422 bool* success,
2423 int* handle) {
2424 *success = false;
2425 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292426
2427 if (browser_tracker_->ContainsHandle(browser_handle)) {
2428 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202429 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292430 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202431 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562432 *success = true;
initial.commit09911bf2008-07-26 23:55:292433 }
initial.commit09911bf2008-07-26 23:55:292434}
2435
2436void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562437 int browser_handle,
2438 bool* success,
2439 int* autocomplete_edit_handle) {
2440 *success = false;
2441 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292442
2443 if (browser_tracker_->ContainsHandle(browser_handle)) {
2444 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062445 LocationBar* loc_bar = browser->window()->GetLocationBar();
2446 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292447 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562448 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2449 *success = true;
initial.commit09911bf2008-07-26 23:55:292450 }
initial.commit09911bf2008-07-26 23:55:292451}
initial.commit09911bf2008-07-26 23:55:292452
[email protected]71f65dd2009-02-11 19:14:562453void AutomationProvider::ShowInterstitialPage(int tab_handle,
2454 const std::string& html_text,
2455 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292456 if (tab_tracker_->ContainsHandle(tab_handle)) {
2457 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112458 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402459
[email protected]7dad3d5f2010-03-04 00:27:012460 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:402461 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342462 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402463 GURL("about:interstitial"),
2464 html_text);
2465 interstitial->Show();
2466 return;
initial.commit09911bf2008-07-26 23:55:292467 }
[email protected]71f65dd2009-02-11 19:14:562468
[email protected]457f5cf2009-08-18 16:37:522469 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
2470 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562471 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292472}
2473
[email protected]71f65dd2009-02-11 19:14:562474void AutomationProvider::HideInterstitialPage(int tab_handle,
2475 bool* success) {
2476 *success = false;
[email protected]57c6a652009-05-04 07:58:342477 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2478 if (tab_contents && tab_contents->interstitial_page()) {
2479 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562480 *success = true;
initial.commit09911bf2008-07-26 23:55:292481 }
initial.commit09911bf2008-07-26 23:55:292482}
2483
[email protected]71f65dd2009-02-11 19:14:562484void AutomationProvider::CloseTab(int tab_handle,
2485 bool wait_until_closed,
2486 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292487 if (tab_tracker_->ContainsHandle(tab_handle)) {
2488 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2489 int index;
2490 Browser* browser = Browser::GetBrowserForController(controller, &index);
2491 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142492 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112493 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452494 return;
initial.commit09911bf2008-07-26 23:55:292495 }
[email protected]de246f52009-02-25 18:25:452496
2497 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:552498 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292499}
2500
[email protected]71f65dd2009-02-11 19:14:562501void AutomationProvider::CloseBrowser(int browser_handle,
2502 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292503 if (browser_tracker_->ContainsHandle(browser_handle)) {
2504 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562505 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562506 reply_message);
[email protected]f3e99e32008-07-30 04:48:392507 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292508 } else {
2509 NOTREACHED();
2510 }
2511}
2512
[email protected]71f65dd2009-02-11 19:14:562513void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2514 if (browser_tracker_->ContainsHandle(browser_handle)) {
2515 Browser* browser = browser_tracker_->GetResource(browser_handle);
2516 browser->window()->Close();
2517 } else {
2518 NOTREACHED();
2519 }
2520}
2521
[email protected]71f65dd2009-02-11 19:14:562522void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2523 IPC::Message* reply_message) {
2524 if (tab_tracker_->ContainsHandle(tab_handle)) {
2525 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2526 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:142527 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:562528 }
2529}
2530
[email protected]71f65dd2009-02-11 19:14:562531void AutomationProvider::GetSecurityState(int handle, bool* success,
2532 SecurityStyle* security_style,
2533 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:482534 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:292535 if (tab_tracker_->ContainsHandle(handle)) {
2536 NavigationController* tab = tab_tracker_->GetResource(handle);
2537 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562538 *success = true;
2539 *security_style = entry->ssl().security_style();
2540 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:482541 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292542 } else {
[email protected]71f65dd2009-02-11 19:14:562543 *success = false;
2544 *security_style = SECURITY_STYLE_UNKNOWN;
2545 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:482546 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292547 }
2548}
2549
[email protected]71f65dd2009-02-11 19:14:562550void AutomationProvider::GetPageType(int handle, bool* success,
2551 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:292552 if (tab_tracker_->ContainsHandle(handle)) {
2553 NavigationController* tab = tab_tracker_->GetResource(handle);
2554 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562555 *page_type = entry->page_type();
2556 *success = true;
initial.commit09911bf2008-07-26 23:55:292557 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:342558 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:562559 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:342560 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:562561 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292562 } else {
[email protected]71f65dd2009-02-11 19:14:562563 *success = false;
2564 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292565 }
2566}
2567
[email protected]84abba62009-10-07 17:01:442568void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
2569 int* duration_ms) {
2570 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
2571 event_name);
2572}
2573
[email protected]71f65dd2009-02-11 19:14:562574void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
2575 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292576 if (tab_tracker_->ContainsHandle(handle)) {
2577 NavigationController* tab = tab_tracker_->GetResource(handle);
2578 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072579 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:402580 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:472581 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:342582 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:292583 if (ssl_blocking_page) {
2584 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:012585 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:562586 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:292587 return;
2588 }
2589 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:522590 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2591 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:562592 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292593 return;
2594 }
2595 }
2596 }
2597 // We failed.
[email protected]457f5cf2009-08-18 16:37:522598 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2599 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562600 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292601}
2602
[email protected]71f65dd2009-02-11 19:14:562603void AutomationProvider::BringBrowserToFront(int browser_handle,
2604 bool* success) {
initial.commit09911bf2008-07-26 23:55:292605 if (browser_tracker_->ContainsHandle(browser_handle)) {
2606 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:062607 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:562608 *success = true;
initial.commit09911bf2008-07-26 23:55:292609 } else {
[email protected]71f65dd2009-02-11 19:14:562610 *success = false;
initial.commit09911bf2008-07-26 23:55:292611 }
2612}
2613
[email protected]71f65dd2009-02-11 19:14:562614void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
2615 int message_num,
2616 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:292617 if (browser_tracker_->ContainsHandle(browser_handle)) {
2618 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562619 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:142620 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:292621 } else {
[email protected]71f65dd2009-02-11 19:14:562622 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:292623 }
2624}
2625
[email protected]71f65dd2009-02-11 19:14:562626void AutomationProvider::PrintNow(int tab_handle,
2627 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:572628 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:342629 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2630 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:292631 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572632 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:142633 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:342634 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:572635 return;
initial.commit09911bf2008-07-26 23:55:292636 }
[email protected]71f65dd2009-02-11 19:14:562637 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
2638 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292639}
[email protected]d301c952009-07-13 15:02:412640
[email protected]71f65dd2009-02-11 19:14:562641void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:112642 const FilePath& file_name,
2643 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:562644 int type,
2645 bool* success) {
initial.commit09911bf2008-07-26 23:55:292646 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562647 *success = false;
initial.commit09911bf2008-07-26 23:55:292648 return;
2649 }
2650
2651 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2652 Browser* browser = FindAndActivateTab(nav);
2653 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142654 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562655 *success = false;
initial.commit09911bf2008-07-26 23:55:292656 return;
2657 }
2658
initial.commit09911bf2008-07-26 23:55:292659 SavePackage::SavePackageType save_type =
2660 static_cast<SavePackage::SavePackageType>(type);
2661 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2662 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:342663 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292664
[email protected]71f65dd2009-02-11 19:14:562665 *success = true;
initial.commit09911bf2008-07-26 23:55:292666}
2667
[email protected]71f65dd2009-02-11 19:14:562668void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2669 bool* success,
2670 std::wstring* text) {
2671 *success = false;
initial.commit09911bf2008-07-26 23:55:292672 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562673 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522674 GetText();
[email protected]71f65dd2009-02-11 19:14:562675 *success = true;
initial.commit09911bf2008-07-26 23:55:292676 }
initial.commit09911bf2008-07-26 23:55:292677}
2678
[email protected]71f65dd2009-02-11 19:14:562679void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2680 const std::wstring& text,
2681 bool* success) {
2682 *success = false;
initial.commit09911bf2008-07-26 23:55:292683 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522684 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2685 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562686 *success = true;
initial.commit09911bf2008-07-26 23:55:292687 }
initial.commit09911bf2008-07-26 23:55:292688}
2689
2690void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562691 int autocomplete_edit_handle,
2692 bool* success,
2693 std::vector<AutocompleteMatchData>* matches) {
2694 *success = false;
initial.commit09911bf2008-07-26 23:55:292695 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272696 const AutocompleteResult& result = autocomplete_edit_tracker_->
2697 GetResource(autocomplete_edit_handle)->model()->result();
2698 for (AutocompleteResult::const_iterator i = result.begin();
2699 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562700 matches->push_back(AutocompleteMatchData(*i));
2701 *success = true;
initial.commit09911bf2008-07-26 23:55:292702 }
initial.commit09911bf2008-07-26 23:55:292703}
2704
2705void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562706 int autocomplete_edit_handle,
2707 bool* success,
2708 bool* query_in_progress) {
2709 *success = false;
2710 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292711 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562712 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522713 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562714 *success = true;
initial.commit09911bf2008-07-26 23:55:292715 }
initial.commit09911bf2008-07-26 23:55:292716}
2717
[email protected]63514af2010-03-30 17:17:232718#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:192719
[email protected]5ae5bed2009-08-21 18:52:442720#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:392721
[email protected]57c6a652009-05-04 07:58:342722TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572723 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572724 if (tab_tracker_->ContainsHandle(handle)) {
2725 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342726 if (tab)
2727 *tab = nav_controller;
2728 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572729 }
[email protected]57c6a652009-05-04 07:58:342730 return NULL;
[email protected]20e93d12008-08-28 16:31:572731}
2732
initial.commit09911bf2008-07-26 23:55:292733TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2734 : AutomationProvider(profile) {
2735 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142736 registrar_.Add(this, NotificationType::SESSION_END,
2737 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292738}
2739
2740TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292741 BrowserList::RemoveObserver(this);
2742}
2743
2744void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:142745 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:292746 AutomationProvider::OnChannelError();
2747}
2748
2749void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2750 // For backwards compatibility with the testing automation interface, we
2751 // want the automation provider (and hence the process) to go away when the
2752 // last browser goes away.
2753 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292754 // If you change this, update Observer for NotificationType::SESSION_END
2755 // below.
[email protected]295039bd2008-08-15 04:32:572756 MessageLoop::current()->PostTask(FROM_HERE,
2757 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292758 }
2759}
2760
2761void TestingAutomationProvider::Observe(NotificationType type,
2762 const NotificationSource& source,
2763 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562764 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292765 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2766 // before the task runs resulting in this object not being deleted. This
2767 // Release balance out the Release scheduled by OnBrowserRemoving.
2768 Release();
2769}
[email protected]295039bd2008-08-15 04:32:572770
2771void TestingAutomationProvider::OnRemoveProvider() {
2772 AutomationProviderList::GetInstance()->RemoveProvider(this);
2773}
[email protected]8a3422c92008-09-24 17:42:422774
[email protected]816633a2009-11-11 21:48:182775void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:562776 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422777 if (tab_tracker_->ContainsHandle(handle)) {
2778 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342779 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112780 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422781 }
[email protected]8a3422c92008-09-24 17:42:422782}
2783
[email protected]816633a2009-11-11 21:48:182784void AutomationProvider::ClickInfoBarAccept(int handle,
2785 int info_bar_index,
2786 bool wait_for_navigation,
2787 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422788 bool success = false;
2789 if (tab_tracker_->ContainsHandle(handle)) {
2790 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2791 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112792 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422793 if (info_bar_index >= 0 && info_bar_index < count) {
2794 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:012795 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:422796 }
[email protected]eb9ba192008-12-02 02:41:342797 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112798 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342799 info_bar_index);
2800 if (delegate->AsConfirmInfoBarDelegate())
2801 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422802 success = true;
2803 }
2804 }
[email protected]4f3dc372009-02-24 00:10:292805 }
[email protected]58f622a62009-10-04 01:17:552806
2807 // This "!wait_for_navigation || !success condition" logic looks suspicious.
2808 // It will send a failure message when success is true but
2809 // |wait_for_navigation| is false.
2810 // TODO(phajdan.jr): investgate whether the reply param (currently
2811 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:422812 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:182813 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:522814 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:422815}
2816
[email protected]71f65dd2009-02-11 19:14:562817void AutomationProvider::GetLastNavigationTime(int handle,
2818 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422819 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562820 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422821}
2822
[email protected]71f65dd2009-02-11 19:14:562823void AutomationProvider::WaitForNavigation(int handle,
2824 int64 last_navigation_time,
2825 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:252826 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:422827 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:252828
[email protected]8a3422c92008-09-24 17:42:422829 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562830 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:522831 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
2832 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:552833 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:292834 return;
[email protected]8a3422c92008-09-24 17:42:422835 }
2836
[email protected]7dad3d5f2010-03-04 00:27:012837 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:422838}
2839
[email protected]71f65dd2009-02-11 19:14:562840void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162841 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562842 int value,
2843 bool* success) {
2844 *success = false;
[email protected]8a3422c92008-09-24 17:42:422845 if (browser_tracker_->ContainsHandle(handle)) {
2846 Browser* browser = browser_tracker_->GetResource(handle);
2847 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562848 *success = true;
[email protected]8a3422c92008-09-24 17:42:422849 }
[email protected]8a3422c92008-09-24 17:42:422850}
[email protected]97fa6ce32008-12-19 01:48:162851
[email protected]71f65dd2009-02-11 19:14:562852void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162853 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562854 const std::wstring& value,
2855 bool* success) {
2856 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162857 if (browser_tracker_->ContainsHandle(handle)) {
2858 Browser* browser = browser_tracker_->GetResource(handle);
2859 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562860 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162861 }
[email protected]97fa6ce32008-12-19 01:48:162862}
2863
[email protected]71f65dd2009-02-11 19:14:562864void AutomationProvider::GetBooleanPreference(int handle,
2865 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:362866 bool* success,
2867 bool* value) {
[email protected]71f65dd2009-02-11 19:14:562868 *success = false;
2869 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162870 if (browser_tracker_->ContainsHandle(handle)) {
2871 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562872 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2873 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162874 }
[email protected]97fa6ce32008-12-19 01:48:162875}
2876
[email protected]71f65dd2009-02-11 19:14:562877void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162878 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562879 bool value,
2880 bool* success) {
2881 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162882 if (browser_tracker_->ContainsHandle(handle)) {
2883 Browser* browser = browser_tracker_->GetResource(handle);
2884 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562885 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162886 }
[email protected]97fa6ce32008-12-19 01:48:162887}
2888
2889// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562890void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:402891 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162892 if (tab_tracker_->ContainsHandle(tab_handle)) {
2893 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2894 Browser* browser = FindAndActivateTab(nav);
2895 DCHECK(browser);
2896
[email protected]57c6a652009-05-04 07:58:342897 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2898 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162899 }
[email protected]97fa6ce32008-12-19 01:48:162900}
2901
[email protected]b8f48d12009-11-09 20:14:362902// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562903void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402904 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562905 bool* success) {
2906 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162907 if (tab_tracker_->ContainsHandle(tab_handle)) {
2908 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:192909 if (!nav)
2910 return;
[email protected]97fa6ce32008-12-19 01:48:162911 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:162912
[email protected]2f2afba2010-04-01 01:53:192913 // If the browser has UI, simulate what a user would do.
2914 // Activate the tab and then click the encoding menu.
2915 if (browser &&
2916 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162917 int selected_encoding_id =
2918 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2919 if (selected_encoding_id) {
2920 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562921 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162922 }
[email protected]2f2afba2010-04-01 01:53:192923 } else {
2924 // There is no UI, Chrome probably runs as Chrome-Frame mode.
2925 // Try to get TabContents and call its override_encoding method.
2926 TabContents* contents = nav->tab_contents();
2927 if (!contents)
2928 return;
2929 const std::string selected_encoding =
2930 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
2931 if (selected_encoding.empty())
2932 return;
2933 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:162934 }
2935 }
[email protected]97fa6ce32008-12-19 01:48:162936}
[email protected]5bcdb312009-01-07 21:43:202937
[email protected]4d434a1a2009-02-11 21:06:572938void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202939 SavePackage::SetShouldPromptUser(should_prompt);
2940}
[email protected]87eab222009-03-13 00:47:452941
[email protected]66ba4932009-06-04 19:22:132942void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2943 *count = -1; // -1 is the error code
2944 if (tab_tracker_->ContainsHandle(handle)) {
2945 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2946 TabContents* tab_contents = nav_controller->tab_contents();
2947 if (tab_contents) {
2948 BlockedPopupContainer* container =
2949 tab_contents->blocked_popup_container();
2950 if (container) {
2951 *count = static_cast<int>(container->GetBlockedPopupCount());
2952 } else {
2953 // If we don't have a container, we don't have any blocked popups to
2954 // contain!
2955 *count = 0;
2956 }
2957 }
2958 }
2959}
[email protected]f7a68432009-07-29 23:18:192960
2961void AutomationProvider::SelectAll(int tab_handle) {
2962 RenderViewHost* view = GetViewForTab(tab_handle);
2963 if (!view) {
2964 NOTREACHED();
2965 return;
2966 }
2967
2968 view->SelectAll();
2969}
2970
2971void AutomationProvider::Cut(int tab_handle) {
2972 RenderViewHost* view = GetViewForTab(tab_handle);
2973 if (!view) {
2974 NOTREACHED();
2975 return;
2976 }
2977
2978 view->Cut();
2979}
2980
2981void AutomationProvider::Copy(int tab_handle) {
2982 RenderViewHost* view = GetViewForTab(tab_handle);
2983 if (!view) {
2984 NOTREACHED();
2985 return;
2986 }
2987
2988 view->Copy();
2989}
2990
2991void AutomationProvider::Paste(int tab_handle) {
2992 RenderViewHost* view = GetViewForTab(tab_handle);
2993 if (!view) {
2994 NOTREACHED();
2995 return;
2996 }
2997
2998 view->Paste();
2999}
3000
3001void AutomationProvider::ReloadAsync(int tab_handle) {
3002 if (tab_tracker_->ContainsHandle(tab_handle)) {
3003 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3004 if (!tab) {
3005 NOTREACHED();
3006 return;
3007 }
3008
[email protected]106a0812010-03-18 00:15:123009 const bool check_for_repost = true;
3010 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193011 }
3012}
3013
3014void AutomationProvider::StopAsync(int tab_handle) {
3015 RenderViewHost* view = GetViewForTab(tab_handle);
3016 if (!view) {
[email protected]8b2b3312009-09-14 18:38:363017 // We tolerate StopAsync being called even before a view has been created.
3018 // So just log a warning instead of a NOTREACHED().
3019 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:193020 return;
3021 }
3022
3023 view->Stop();
3024}
3025
[email protected]1bb5f892009-10-06 01:44:573026void AutomationProvider::OnSetPageFontSize(int tab_handle,
3027 int font_size) {
3028 AutomationPageFontSize automation_font_size =
3029 static_cast<AutomationPageFontSize>(font_size);
3030
3031 if (automation_font_size < SMALLEST_FONT ||
3032 automation_font_size > LARGEST_FONT) {
3033 DLOG(ERROR) << "Invalid font size specified : "
3034 << font_size;
3035 return;
3036 }
3037
3038 if (tab_tracker_->ContainsHandle(tab_handle)) {
3039 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3040 DCHECK(tab != NULL);
3041 if (tab && tab->tab_contents()) {
3042 DCHECK(tab->tab_contents()->profile() != NULL);
3043 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
3044 prefs::kWebKitDefaultFontSize, font_size);
3045 }
3046 }
3047}
3048
[email protected]bc73b4e52010-03-26 04:16:203049void AutomationProvider::RemoveBrowsingData(int remove_mask) {
3050 BrowsingDataRemover* remover;
3051 remover = new BrowsingDataRemover(profile(),
3052 BrowsingDataRemover::EVERYTHING, // All time periods.
3053 base::Time());
3054 remover->Remove(remove_mask);
3055 // BrowsingDataRemover deletes itself.
3056}
[email protected]1bb5f892009-10-06 01:44:573057
[email protected]2949e90d2009-08-21 15:32:523058void AutomationProvider::WaitForBrowserWindowCountToBecome(
3059 int target_count, IPC::Message* reply_message) {
3060 if (static_cast<int>(BrowserList::size()) == target_count) {
3061 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
3062 reply_message, true);
3063 Send(reply_message);
3064 return;
3065 }
3066
3067 // Set up an observer (it will delete itself).
3068 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
3069}
3070
3071void AutomationProvider::WaitForAppModalDialogToBeShown(
3072 IPC::Message* reply_message) {
3073 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
3074 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
3075 reply_message, true);
3076 Send(reply_message);
3077 return;
3078 }
3079
3080 // Set up an observer (it will delete itself).
3081 new AppModalDialogShownObserver(this, reply_message);
3082}
3083
[email protected]1126a1d32009-08-26 15:39:263084void AutomationProvider::GoBackBlockUntilNavigationsComplete(
3085 int handle, int number_of_navigations, IPC::Message* reply_message) {
3086 if (tab_tracker_->ContainsHandle(handle)) {
3087 NavigationController* tab = tab_tracker_->GetResource(handle);
3088 Browser* browser = FindAndActivateTab(tab);
3089 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:013090 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3091 false);
[email protected]1126a1d32009-08-26 15:39:263092 browser->GoBack(CURRENT_TAB);
3093 return;
3094 }
3095 }
3096
3097 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
3098 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3099 Send(reply_message);
3100}
3101
3102void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
3103 int handle, int number_of_navigations, IPC::Message* reply_message) {
3104 if (tab_tracker_->ContainsHandle(handle)) {
3105 NavigationController* tab = tab_tracker_->GetResource(handle);
3106 Browser* browser = FindAndActivateTab(tab);
3107 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:013108 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3109 false);
[email protected]1126a1d32009-08-26 15:39:263110 browser->GoForward(CURRENT_TAB);
3111 return;
3112 }
3113 }
3114
3115 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
3116 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3117 Send(reply_message);
3118}
3119
[email protected]f7a68432009-07-29 23:18:193120RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
3121 if (tab_tracker_->ContainsHandle(tab_handle)) {
3122 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3123 if (!tab) {
3124 NOTREACHED();
3125 return NULL;
3126 }
3127
3128 TabContents* tab_contents = tab->tab_contents();
3129 if (!tab_contents) {
3130 NOTREACHED();
3131 return NULL;
3132 }
3133
3134 RenderViewHost* view_host = tab_contents->render_view_host();
3135 return view_host;
3136 }
3137
3138 return NULL;
3139}
[email protected]675595f2009-08-26 22:32:043140
3141void AutomationProvider::GetBrowserForWindow(int window_handle,
3142 bool* success,
3143 int* browser_handle) {
3144 *success = false;
3145 *browser_handle = 0;
3146
3147 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
3148 if (!window)
3149 return;
3150
3151 BrowserList::const_iterator iter = BrowserList::begin();
3152 for (;iter != BrowserList::end(); ++iter) {
3153 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
3154 if (window == this_window) {
3155 // Add() returns the existing handle for the resource if any.
3156 *browser_handle = browser_tracker_->Add(*iter);
3157 *success = true;
3158 return;
3159 }
3160 }
3161}
[email protected]d11c8e92009-10-20 23:26:403162
3163void AutomationProvider::InstallExtension(const FilePath& crx_path,
3164 IPC::Message* reply_message) {
3165 ExtensionsService* service = profile_->GetExtensionsService();
3166 if (service) {
3167 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193168 new ExtensionInstallNotificationObserver(this,
3169 AutomationMsg_InstallExtension::ID,
3170 reply_message);
[email protected]d11c8e92009-10-20 23:26:403171
3172 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:163173 scoped_refptr<CrxInstaller> installer(
3174 new CrxInstaller(install_dir,
3175 service,
3176 NULL)); // silent install, no UI
3177 installer->set_allow_privilege_increase(true);
3178 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:403179 } else {
3180 AutomationMsg_InstallExtension::WriteReplyParams(
3181 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3182 Send(reply_message);
3183 }
3184}
3185
3186void AutomationProvider::LoadExpandedExtension(
3187 const FilePath& extension_dir,
3188 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:383189 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:403190 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193191 new ExtensionInstallNotificationObserver(
3192 this,
3193 AutomationMsg_LoadExpandedExtension::ID,
3194 reply_message);
[email protected]d11c8e92009-10-20 23:26:403195
3196 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:403197 } else {
3198 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
3199 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3200 Send(reply_message);
3201 }
3202}
[email protected]673fd2c02010-02-04 23:10:003203
[email protected]a1e62d12010-03-16 02:18:433204void AutomationProvider::GetEnabledExtensions(
3205 std::vector<FilePath>* result) {
3206 ExtensionsService* service = profile_->GetExtensionsService();
3207 DCHECK(service);
3208 if (service->extensions_enabled()) {
3209 const ExtensionList* extensions = service->extensions();
3210 DCHECK(extensions);
3211 for (size_t i = 0; i < extensions->size(); ++i) {
3212 Extension* extension = (*extensions)[i];
3213 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:123214 if (extension->location() == Extension::INTERNAL ||
3215 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:073216 result->push_back(extension->path());
3217 }
[email protected]a1e62d12010-03-16 02:18:433218 }
3219 }
3220}
3221
[email protected]790788ac2010-04-06 17:52:193222void AutomationProvider::WaitForExtensionTestResult(
3223 IPC::Message* reply_message) {
3224 DCHECK(reply_message_ == NULL);
3225 reply_message_ = reply_message;
3226 // Call MaybeSendResult, because the result might have come in before
3227 // we were waiting on it.
3228 extension_test_result_observer_->MaybeSendResult();
3229}
3230
3231void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:093232 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:193233 ExtensionsService* service = profile_->GetExtensionsService();
3234 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3235 if (service && manager) {
3236 // The observer will delete itself when done.
3237 new ExtensionReadyNotificationObserver(
3238 manager,
3239 this,
3240 AutomationMsg_InstallExtensionAndGetHandle::ID,
3241 reply_message);
3242
[email protected]d7e5525d2010-04-20 14:37:093243 ExtensionInstallUI* client =
3244 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:193245 scoped_refptr<CrxInstaller> installer(
3246 new CrxInstaller(service->install_directory(),
3247 service,
[email protected]d7e5525d2010-04-20 14:37:093248 client));
[email protected]790788ac2010-04-06 17:52:193249 installer->set_allow_privilege_increase(true);
3250 installer->InstallCrx(crx_path);
3251 } else {
3252 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
3253 reply_message, 0);
3254 Send(reply_message);
3255 }
3256}
3257
3258void AutomationProvider::UninstallExtension(int extension_handle,
3259 bool* success) {
3260 *success = false;
3261 Extension* extension = GetExtension(extension_handle);
3262 ExtensionsService* service = profile_->GetExtensionsService();
3263 if (extension && service) {
3264 ExtensionUnloadNotificationObserver observer;
3265 service->UninstallExtension(extension->id(), false);
3266 // The extension unload notification should have been sent synchronously
3267 // with the uninstall. Just to be safe, check that it was received.
3268 *success = observer.did_receive_unload_notification();
3269 }
3270}
3271
3272void AutomationProvider::EnableExtension(int extension_handle,
3273 IPC::Message* reply_message) {
3274 Extension* extension = GetDisabledExtension(extension_handle);
3275 ExtensionsService* service = profile_->GetExtensionsService();
3276 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3277 // Only enable if this extension is disabled.
3278 if (extension && service && manager) {
3279 // The observer will delete itself when done.
3280 new ExtensionReadyNotificationObserver(
3281 manager,
3282 this,
3283 AutomationMsg_EnableExtension::ID,
3284 reply_message);
3285 service->EnableExtension(extension->id());
3286 } else {
3287 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
3288 Send(reply_message);
3289 }
3290}
3291
3292void AutomationProvider::DisableExtension(int extension_handle,
3293 bool* success) {
3294 *success = false;
3295 Extension* extension = GetEnabledExtension(extension_handle);
3296 ExtensionsService* service = profile_->GetExtensionsService();
3297 if (extension && service) {
3298 ExtensionUnloadNotificationObserver observer;
3299 service->DisableExtension(extension->id());
3300 // The extension unload notification should have been sent synchronously
3301 // with the disable. Just to be safe, check that it was received.
3302 *success = observer.did_receive_unload_notification();
3303 }
3304}
3305
3306void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
3307 int extension_handle, int browser_handle,
3308 IPC::Message* reply_message) {
3309 bool success = false;
3310 Extension* extension = GetEnabledExtension(extension_handle);
3311 ExtensionsService* service = profile_->GetExtensionsService();
3312 ExtensionMessageService* message_service =
3313 profile_->GetExtensionMessageService();
3314 Browser* browser = browser_tracker_->GetResource(browser_handle);
3315 if (extension && service && message_service && browser) {
3316 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
3317 if (extension->page_action()) {
3318 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
3319 browser->profile(), extension->id(), "action", tab_id, "", 1);
3320 success = true;
3321 } else if (extension->browser_action()) {
3322 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
3323 browser->profile(), extension->id(), browser);
3324 success = true;
3325 }
3326 }
3327 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3328 reply_message, success);
3329 Send(reply_message);
3330}
3331
3332void AutomationProvider::MoveExtensionBrowserAction(
3333 int extension_handle, int index, bool* success) {
3334 *success = false;
3335 Extension* extension = GetEnabledExtension(extension_handle);
3336 ExtensionsService* service = profile_->GetExtensionsService();
3337 if (extension && service) {
3338 ExtensionToolbarModel* toolbar = service->toolbar_model();
3339 if (toolbar) {
3340 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3341 toolbar->MoveBrowserAction(extension, index);
3342 *success = true;
3343 } else {
3344 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3345 }
3346 }
3347 }
3348}
3349
3350void AutomationProvider::GetExtensionProperty(
3351 int extension_handle,
3352 AutomationMsg_ExtensionProperty type,
3353 bool* success,
3354 std::string* value) {
3355 *success = false;
3356 Extension* extension = GetExtension(extension_handle);
3357 ExtensionsService* service = profile_->GetExtensionsService();
3358 if (extension && service) {
3359 ExtensionToolbarModel* toolbar = service->toolbar_model();
3360 int found_index = -1;
3361 int index = 0;
3362 switch (type) {
3363 case AUTOMATION_MSG_EXTENSION_ID:
3364 *value = extension->id();
3365 *success = true;
3366 break;
3367 case AUTOMATION_MSG_EXTENSION_NAME:
3368 *value = extension->name();
3369 *success = true;
3370 break;
3371 case AUTOMATION_MSG_EXTENSION_VERSION:
3372 *value = extension->VersionString();
3373 *success = true;
3374 break;
3375 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3376 if (toolbar) {
3377 for (ExtensionList::const_iterator iter = toolbar->begin();
3378 iter != toolbar->end(); iter++) {
3379 // Skip this extension if we are in incognito mode
3380 // and it is not incognito-enabled.
3381 if (profile_->IsOffTheRecord() &&
3382 !service->IsIncognitoEnabled(*iter))
3383 continue;
3384 if (*iter == extension) {
3385 found_index = index;
3386 break;
3387 }
3388 index++;
3389 }
3390 *value = IntToString(found_index);
3391 *success = true;
3392 }
3393 break;
3394 default:
3395 LOG(WARNING) << "Trying to get undefined extension property";
3396 break;
3397 }
3398 }
3399}
3400
[email protected]673fd2c02010-02-04 23:10:003401void AutomationProvider::SaveAsAsync(int tab_handle) {
3402 NavigationController* tab = NULL;
3403 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3404 if (tab_contents)
3405 tab_contents->OnSavePage();
3406}
[email protected]7dad3d5f2010-03-04 00:27:013407
3408void AutomationProvider::SetContentSetting(
3409 int handle,
3410 const std::string& host,
3411 ContentSettingsType content_type,
3412 ContentSetting setting,
3413 bool* success) {
3414 *success = false;
3415 if (browser_tracker_->ContainsHandle(handle)) {
3416 Browser* browser = browser_tracker_->GetResource(handle);
3417 HostContentSettingsMap* map =
3418 browser->profile()->GetHostContentSettingsMap();
3419 if (host.empty()) {
3420 map->SetDefaultContentSetting(content_type, setting);
3421 } else {
[email protected]0314ae02010-04-08 09:18:293422 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3423 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013424 }
3425 *success = true;
3426 }
3427}
[email protected]cc824372010-03-31 15:33:013428
3429#if !defined(TOOLKIT_VIEWS)
3430void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3431 NOTIMPLEMENTED();
3432};
3433
3434void AutomationProvider::WaitForFocusedViewIDToChange(
3435 int handle, int previous_view_id, IPC::Message* reply_message) {
3436 NOTIMPLEMENTED();
3437}
3438
3439void AutomationProvider::StartTrackingPopupMenus(
3440 int browser_handle, bool* success) {
3441 NOTIMPLEMENTED();
3442}
3443
3444void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3445 NOTIMPLEMENTED();
3446}
3447#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:093448
3449void AutomationProvider::ResetToDefaultTheme() {
3450 profile_->ClearTheme();
3451}