blob: e8cab6199718d5938e3d7e28f5637854d848f242 [file] [log] [blame]
[email protected]3b5f7022010-03-25 20:37:401// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/automation/automation_provider.h"
6
[email protected]5ae5bed2009-08-21 18:52:447#include <set>
8
[email protected]202e7a72009-06-15 03:48:369#include "app/l10n_util.h"
[email protected]37126212009-05-06 02:23:3110#include "app/message_box_flags.h"
[email protected]2041cf342010-02-19 03:15:5911#include "base/callback.h"
[email protected]c6cb1992009-04-13 16:45:2912#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2013#include "base/json/json_reader.h"
[email protected]59a611242010-04-02 02:24:0414#include "base/json/json_writer.h"
[email protected]bc1407f2009-09-29 00:33:3515#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3816#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4618#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5019#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2720#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3821#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5322#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1723#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2924#include "chrome/app/chrome_dll_resource.h"
[email protected]bcff05a2010-04-14 01:46:4325#include "chrome/app/chrome_version_info.h"
[email protected]0bfa713f2009-04-07 20:18:2826#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0927#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3328#include "chrome/browser/automation/automation_extension_function.h"
[email protected]790788ac2010-04-06 17:52:1929#include "chrome/browser/automation/automation_extension_tracker.h"
initial.commit09911bf2008-07-26 23:55:2930#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0831#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5532#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5033#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1334#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5335#include "chrome/browser/bookmarks/bookmark_model.h"
36#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]5c238752009-06-13 10:29:0737#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3938#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2039#include "chrome/browser/browsing_data_remover.h"
[email protected]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]d11c8e92009-10-20 23:26:4045#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1946#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]d11c8e92009-10-20 23:26:4047#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5548#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1949#include "chrome/browser/extensions/extension_tabs_module.h"
50#include "chrome/browser/extensions/extension_toolbar_model.h"
51#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1652#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5853#include "chrome/browser/find_bar.h"
54#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2955#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0156#include "chrome/browser/host_content_settings_map.h"
[email protected]0ac83682010-01-22 17:46:2757#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0658#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0159#include "chrome/browser/login_prompt.h"
[email protected]be180c802009-10-23 06:33:3160#include "chrome/browser/net/url_request_context_getter.h"
[email protected]f732c1e2009-07-30 15:48:5361#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2462#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0863#include "chrome/browser/pref_service.h"
[email protected]a7eee32f2009-05-22 18:08:1764#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0765#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2566#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0367#include "chrome/browser/ssl/ssl_manager.h"
68#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3469#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4570#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1771#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2972#include "chrome/common/chrome_paths.h"
[email protected]790788ac2010-04-06 17:52:1973#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1774#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1475#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5776#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2577#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5678#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5779#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1780#include "net/proxy/proxy_service.h"
81#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2182#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:3083#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:2884#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2985
[email protected]de246f52009-02-25 18:25:4586#if defined(OS_WIN)
87// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4588#include "chrome/browser/character_encoding.h"
89#include "chrome/browser/download/save_package.h"
90#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4591#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4592#endif // defined(OS_WIN)
93
[email protected]e1acf6f2008-10-27 20:43:3394using base::Time;
95
[email protected]cbab76d2008-10-13 22:42:4796class AutomationInterstitialPage : public InterstitialPage {
97 public:
[email protected]57c6a652009-05-04 07:58:3498 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4799 const GURL& url,
100 const std::string& contents)
101 : InterstitialPage(tab, true, url),
102 contents_(contents) {
103 }
104
105 virtual std::string GetHTMLContents() { return contents_; }
106
107 private:
108 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29109
[email protected]cbab76d2008-10-13 22:42:47110 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
111};
112
[email protected]c2cb8542009-08-20 21:16:51113class ClickTask : public Task {
114 public:
[email protected]fc2e0872009-08-21 22:14:41115 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51116 virtual ~ClickTask() {}
117
118 virtual void Run() {
119 ui_controls::MouseButton button = ui_controls::LEFT;
120 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
121 views::Event::EF_LEFT_BUTTON_DOWN) {
122 button = ui_controls::LEFT;
123 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
124 views::Event::EF_RIGHT_BUTTON_DOWN) {
125 button = ui_controls::RIGHT;
126 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
127 views::Event::EF_MIDDLE_BUTTON_DOWN) {
128 button = ui_controls::MIDDLE;
129 } else {
130 NOTREACHED();
131 }
132
[email protected]fc2e0872009-08-21 22:14:41133 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51134 }
135
136 private:
[email protected]c2cb8542009-08-20 21:16:51137 int flags_;
138
139 DISALLOW_COPY_AND_ASSIGN(ClickTask);
140};
[email protected]c2cb8542009-08-20 21:16:51141
initial.commit09911bf2008-07-26 23:55:29142AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57143 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56144 profile_(profile),
[email protected]cc824372010-03-31 15:33:01145 reply_message_(NULL),
146 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29147 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19148 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29149 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20150 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29151 autocomplete_edit_tracker_.reset(
152 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29153 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
154 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44155 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19156 extension_test_result_observer_.reset(
157 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13158 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29159}
160
161AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50162 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
163 port_containers_.end());
164 port_containers_.clear();
165
[email protected]0da050b92008-08-19 19:29:47166 // Make sure that any outstanding NotificationObservers also get destroyed.
167 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31168 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47169 while ((observer = it.GetNext()) != NULL)
170 delete observer;
[email protected]528211a2010-01-14 15:25:13171
172 if (channel_.get()) {
173 channel_->Close();
174 }
175 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29176}
177
[email protected]9a3a293b2009-06-04 22:28:16178void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06179 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57180 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06181 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
182 automation_resource_message_filter_,
183 g_browser_process->io_thread()->message_loop(),
184 true, g_browser_process->shutdown_event()));
[email protected]bcff05a2010-04-14 01:46:43185 scoped_ptr<FileVersionInfo> version_info(
186 chrome_app::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40187 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43188 if (version_info != NULL) {
189 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40190 }
[email protected]c6cb1992009-04-13 16:45:29191
192 // Send a hello message with our current automation protocol version.
193 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29194}
195
196void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
197 if (expected_tabs == 0) {
198 Send(new AutomationMsg_InitialLoadsComplete(0));
199 } else {
200 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
201 }
202}
203
204NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58205 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01206 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29207 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58208 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01209 number_of_navigations,
210 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29211
[email protected]71f65dd2009-02-11 19:14:56212 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29213 return observer;
214}
215
216void AutomationProvider::RemoveNavigationStatusListener(
217 NotificationObserver* obs) {
218 notification_observer_list_.RemoveObserver(obs);
219}
220
221NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14222 Browser* parent,
223 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56224 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14225 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29226 notification_observer_list_.AddObserver(observer);
227
228 return observer;
229}
230
231void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
232 notification_observer_list_.RemoveObserver(obs);
233}
234
235void AutomationProvider::AddLoginHandler(NavigationController* tab,
236 LoginHandler* handler) {
237 login_handler_map_[tab] = handler;
238}
239
240void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
241 DCHECK(login_handler_map_[tab]);
242 login_handler_map_.erase(tab);
243}
244
[email protected]f44265b2009-05-19 18:52:50245void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
246 int port_id = port->port_id();
247 DCHECK_NE(-1, port_id);
248 DCHECK(port_containers_.find(port_id) == port_containers_.end());
249
250 port_containers_[port_id] = port;
251}
252
253void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
254 int port_id = port->port_id();
255 DCHECK_NE(-1, port_id);
256
257 PortContainerMap::iterator it = port_containers_.find(port_id);
258 DCHECK(it != port_containers_.end());
259
260 if (it != port_containers_.end()) {
261 delete it->second;
262 port_containers_.erase(it);
263 }
264}
265
266ExtensionPortContainer* AutomationProvider::GetPortContainer(
267 int port_id) const {
268 PortContainerMap::const_iterator it = port_containers_.find(port_id);
269 if (it == port_containers_.end())
270 return NULL;
271
272 return it->second;
273}
274
initial.commit09911bf2008-07-26 23:55:29275int AutomationProvider::GetIndexForNavigationController(
276 const NavigationController* controller, const Browser* parent) const {
277 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12278 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29279}
280
[email protected]790788ac2010-04-06 17:52:19281int AutomationProvider::AddExtension(Extension* extension) {
282 DCHECK(extension);
283 return extension_tracker_->Add(extension);
284}
285
286Extension* AutomationProvider::GetExtension(int extension_handle) {
287 return extension_tracker_->GetResource(extension_handle);
288}
289
290Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
291 Extension* extension = extension_tracker_->GetResource(extension_handle);
292 ExtensionsService* service = profile_->GetExtensionsService();
293 if (extension && service &&
294 service->GetExtensionById(extension->id(), false))
295 return extension;
296 return NULL;
297}
298
299Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
300 Extension* extension = extension_tracker_->GetResource(extension_handle);
301 ExtensionsService* service = profile_->GetExtensionsService();
302 if (extension && service &&
303 service->GetExtensionById(extension->id(), true) &&
304 !service->GetExtensionById(extension->id(), false))
305 return extension;
306 return NULL;
307}
308
initial.commit09911bf2008-07-26 23:55:29309void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
310 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14311 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56312 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
313 CloseBrowserAsync)
314 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
315 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
316 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
317 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
318 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
319 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22320 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]1c58a5c2009-05-21 18:47:14321 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58322 IPC_MESSAGE_HANDLER_DELAY_REPLY(
323 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
324 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56325 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11326 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
327 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56328 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
329 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
330 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
331 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14332 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56333 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
334 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
335 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14336 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29337 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
338 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56339 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36340 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56341 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29342 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56343 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29344 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
345 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56346 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14347 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24348 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44349 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33350 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44351 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33352 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24353 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14354 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24355 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15356 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14357 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24358 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35359 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14360 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30361#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56362 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
363 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30364#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56365 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53366 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56367 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37368#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56369 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45370#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56371 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
372 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03373 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56374 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14375 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09376 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
377 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
378 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29379 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14380 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56381 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
382 ExecuteJavascript)
383 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29384 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14385 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
386 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56387 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
388 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14389 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17390 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14391 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14392 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53393 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
394 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14395 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56396 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14397 GetAutocompleteEditForBrowser)
398 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45399#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29400 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17401#endif
[email protected]71f65dd2009-02-11 19:14:56402 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29403 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26404 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
405 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56406 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14407 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56408 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14409 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17410#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29411 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
412 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17413#endif
[email protected]71f65dd2009-02-11 19:14:56414 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
415 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14416 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17417#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04418 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39419 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
420 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17421#endif
[email protected]1c58a5c2009-05-21 18:47:14422 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
423 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44424 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
425 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56426 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
427 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29428 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
429 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
430 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56431 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41432 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56433 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
434 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29435 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56436 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29437 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56438 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29439 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56440 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29441 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56442 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45443 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45444 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
445 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14446 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56447 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57448 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56449 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57450 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56451 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
452 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53453 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
454 GetBookmarksAsJSON)
455 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
456 WaitForBookmarkModelToLoad)
457 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
458 AddBookmarkGroup)
459 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
460 AddBookmarkURL)
461 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
462 ReparentBookmark)
463 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
464 SetBookmarkTitle)
465 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
466 SetBookmarkURL)
467 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
468 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04469 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
470 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18471 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
472 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
473 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56474 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42475 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56476 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
477 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14478 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56479 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44480 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56481 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20482 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14483 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56484 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16485 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56486 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16487 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56488 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16489 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14490 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20491 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
492 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14493 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]4cfc1d922009-11-08 14:02:58494#if defined(OS_WIN)
495 // Depends on ExternalTabContainer, so Windows-only
[email protected]b83e4602009-05-15 22:58:33496 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
497 SetEnableExtensionAutomation)
[email protected]4cfc1d922009-11-08 14:02:58498#endif
[email protected]59560e0b2009-06-04 03:30:22499 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13500 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19501 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
502 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
503 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
504 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
505 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
506 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52507 IPC_MESSAGE_HANDLER_DELAY_REPLY(
508 AutomationMsg_WaitForBrowserWindowCountToBecome,
509 WaitForBrowserWindowCountToBecome)
510 IPC_MESSAGE_HANDLER_DELAY_REPLY(
511 AutomationMsg_WaitForAppModalDialogToBeShown,
512 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26513 IPC_MESSAGE_HANDLER_DELAY_REPLY(
514 AutomationMsg_GoBackBlockUntilNavigationsComplete,
515 GoBackBlockUntilNavigationsComplete)
516 IPC_MESSAGE_HANDLER_DELAY_REPLY(
517 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
518 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14519#if defined(OS_WIN)
520 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
521#endif
[email protected]1bb5f892009-10-06 01:44:57522 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40523 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
524 InstallExtension)
525 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
526 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43527 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
528 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19529 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
530 WaitForExtensionTestResult)
531 IPC_MESSAGE_HANDLER_DELAY_REPLY(
532 AutomationMsg_InstallExtensionAndGetHandle,
533 InstallExtensionAndGetHandle)
534 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
535 UninstallExtension)
536 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
537 EnableExtension)
538 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
539 DisableExtension)
540 IPC_MESSAGE_HANDLER_DELAY_REPLY(
541 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
542 ExecuteExtensionActionInActiveTabAsync)
543 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
544 MoveExtensionBrowserAction)
545 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
546 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57547 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
548 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00549 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]2b19e2fe2010-02-16 02:24:18550#if defined(OS_WIN)
551 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
552#endif
[email protected]7dad3d5f2010-03-04 00:27:01553 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]566a0f762010-03-10 04:14:57554#if defined(OS_CHROMEOS)
555 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
556 LoginWithUserAndPass)
557#endif
[email protected]bc73b4e52010-03-26 04:16:20558 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]cc824372010-03-31 15:33:01559#if defined(TOOLKIT_VIEWS)
560 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
561 WaitForFocusedViewIDToChange)
562 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
563 StartTrackingPopupMenus)
564 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
565 WaitForPopupMenuToOpen)
566#endif
[email protected]d7e5525d2010-04-20 14:37:09567 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
initial.commit09911bf2008-07-26 23:55:29568 IPC_END_MESSAGE_MAP()
569}
570
[email protected]71f65dd2009-02-11 19:14:56571void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
572 *status = -1;
initial.commit09911bf2008-07-26 23:55:29573 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
574 Browser* browser = browser_tracker_->GetResource(handle);
575 if (at_index >= 0 && at_index < browser->tab_count()) {
576 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56577 *status = 0;
initial.commit09911bf2008-07-26 23:55:29578 }
579 }
initial.commit09911bf2008-07-26 23:55:29580}
581
[email protected]71f65dd2009-02-11 19:14:56582void AutomationProvider::AppendTab(int handle, const GURL& url,
583 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29584 int append_tab_response = -1; // -1 is the error code
585 NotificationObserver* observer = NULL;
586
587 if (browser_tracker_->ContainsHandle(handle)) {
588 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14589 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38590 TabContents* tab_contents = browser->AddTabWithURL(
591 url, GURL(), PageTransition::TYPED, -1, Browser::ADD_SELECTED, NULL,
592 std::string());
initial.commit09911bf2008-07-26 23:55:29593 if (tab_contents) {
594 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57595 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29596 }
597 }
598
599 if (append_tab_response < 0) {
600 // The append tab failed. Remove the TabStripObserver
601 if (observer) {
602 RemoveTabStripObserver(observer);
603 delete observer;
604 }
605
[email protected]71f65dd2009-02-11 19:14:56606 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
607 append_tab_response);
608 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29609 }
610}
611
[email protected]71f65dd2009-02-11 19:14:56612void AutomationProvider::NavigateToURL(int handle, const GURL& url,
613 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58614 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
615}
616
617void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
618 int handle, const GURL& url, int number_of_navigations,
619 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29620 if (tab_tracker_->ContainsHandle(handle)) {
621 NavigationController* tab = tab_tracker_->GetResource(handle);
622
623 // Simulate what a user would do. Activate the tab and then navigate.
624 // We could allow navigating in a background tab in future.
625 Browser* browser = FindAndActivateTab(tab);
626
627 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01628 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
629 false);
[email protected]71f65dd2009-02-11 19:14:56630
initial.commit09911bf2008-07-26 23:55:29631 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50632 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29633 return;
634 }
635 }
[email protected]71f65dd2009-02-11 19:14:56636
637 AutomationMsg_NavigateToURL::WriteReplyParams(
638 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
639 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29640}
[email protected]2949e90d2009-08-21 15:32:52641
[email protected]c70f9b82010-04-21 07:31:11642void AutomationProvider::NavigationAsync(int handle,
643 const GURL& url,
644 bool* status) {
645 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
646}
647
648void AutomationProvider::NavigationAsyncWithDisposition(
649 int handle,
650 const GURL& url,
651 WindowOpenDisposition disposition,
652 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56653 *status = false;
initial.commit09911bf2008-07-26 23:55:29654
655 if (tab_tracker_->ContainsHandle(handle)) {
656 NavigationController* tab = tab_tracker_->GetResource(handle);
657
658 // Simulate what a user would do. Activate the tab and then navigate.
659 // We could allow navigating in a background tab in future.
660 Browser* browser = FindAndActivateTab(tab);
661
662 if (browser) {
663 // Don't add any listener unless a callback mechanism is desired.
664 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11665 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56666 *status = true;
initial.commit09911bf2008-07-26 23:55:29667 }
668 }
initial.commit09911bf2008-07-26 23:55:29669}
670
[email protected]71f65dd2009-02-11 19:14:56671void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29672 if (tab_tracker_->ContainsHandle(handle)) {
673 NavigationController* tab = tab_tracker_->GetResource(handle);
674 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14675 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01676 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29677 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29678 return;
679 }
680 }
[email protected]71f65dd2009-02-11 19:14:56681
682 AutomationMsg_GoBack::WriteReplyParams(
683 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
684 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29685}
686
[email protected]71f65dd2009-02-11 19:14:56687void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29688 if (tab_tracker_->ContainsHandle(handle)) {
689 NavigationController* tab = tab_tracker_->GetResource(handle);
690 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14691 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01692 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29693 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29694 return;
695 }
696 }
[email protected]71f65dd2009-02-11 19:14:56697
698 AutomationMsg_GoForward::WriteReplyParams(
699 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
700 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29701}
702
[email protected]71f65dd2009-02-11 19:14:56703void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29704 if (tab_tracker_->ContainsHandle(handle)) {
705 NavigationController* tab = tab_tracker_->GetResource(handle);
706 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14707 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01708 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29709 browser->Reload();
710 return;
711 }
712 }
[email protected]71f65dd2009-02-11 19:14:56713
714 AutomationMsg_Reload::WriteReplyParams(
715 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
716 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29717}
718
[email protected]71f65dd2009-02-11 19:14:56719void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29720 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56721 const std::wstring& password,
722 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29723 if (tab_tracker_->ContainsHandle(tab_handle)) {
724 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
725 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
726
727 if (iter != login_handler_map_.end()) {
728 // If auth is needed again after this, assume login has failed. This is
729 // not strictly correct, because a navigation can require both proxy and
730 // server auth, but it should be OK for now.
731 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01732 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29733 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52734 return;
initial.commit09911bf2008-07-26 23:55:29735 }
736 }
[email protected]de246f52009-02-25 18:25:45737
[email protected]457f5cf2009-08-18 16:37:52738 AutomationMsg_SetAuth::WriteReplyParams(
739 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
740 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29741}
742
[email protected]71f65dd2009-02-11 19:14:56743void AutomationProvider::CancelAuth(int tab_handle,
744 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29745 if (tab_tracker_->ContainsHandle(tab_handle)) {
746 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
747 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
748
749 if (iter != login_handler_map_.end()) {
750 // If auth is needed again after this, something is screwy.
751 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01752 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29753 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52754 return;
initial.commit09911bf2008-07-26 23:55:29755 }
756 }
[email protected]de246f52009-02-25 18:25:45757
[email protected]457f5cf2009-08-18 16:37:52758 AutomationMsg_CancelAuth::WriteReplyParams(
759 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
760 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29761}
762
[email protected]71f65dd2009-02-11 19:14:56763void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
764 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29765
766 if (tab_tracker_->ContainsHandle(tab_handle)) {
767 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
768 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
769
770 if (iter != login_handler_map_.end()) {
771 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56772 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29773 }
774 }
initial.commit09911bf2008-07-26 23:55:29775}
776
[email protected]71f65dd2009-02-11 19:14:56777void AutomationProvider::GetRedirectsFrom(int tab_handle,
778 const GURL& source_url,
779 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29780 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
781 if (tab_tracker_->ContainsHandle(tab_handle)) {
782 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
783 HistoryService* history_service =
784 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
785
786 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
787 "has no history service";
788 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56789 DCHECK(reply_message_ == NULL);
790 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29791 // Schedule a history query for redirects. The response will be sent
792 // asynchronously from the callback the history system uses to notify us
793 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29794 redirect_query_ = history_service->QueryRedirectsFrom(
795 source_url, &consumer_,
796 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
797 return; // Response will be sent when query completes.
798 }
799 }
800
801 // Send failure response.
[email protected]deb57402009-02-06 01:35:30802 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56803 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
804 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29805}
806
[email protected]71f65dd2009-02-11 19:14:56807void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
808 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29809 if (browser_tracker_->ContainsHandle(handle)) {
810 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56811 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29812 }
initial.commit09911bf2008-07-26 23:55:29813}
814
[email protected]202e7a72009-06-15 03:48:36815void AutomationProvider::GetBrowserLocale(string16* locale) {
816 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06817 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36818}
819
[email protected]71f65dd2009-02-11 19:14:56820void AutomationProvider::GetBrowserWindowCount(int* window_count) {
821 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29822}
823
[email protected]24497032009-05-01 17:00:29824void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
825 *window_count = static_cast<int>(
826 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
827}
828
[email protected]71f65dd2009-02-11 19:14:56829void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
830 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07831 AppModalDialog* dialog_delegate =
832 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50833 *showing_dialog = (dialog_delegate != NULL);
834 if (*showing_dialog)
835 *dialog_button = dialog_delegate->GetDialogButtons();
836 else
[email protected]478ff2ed2009-04-21 23:49:18837 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20838}
839
[email protected]71f65dd2009-02-11 19:14:56840void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
841 *success = false;
[email protected]fad84eab2008-12-05 00:37:20842
[email protected]1f460072009-05-28 17:02:07843 AppModalDialog* dialog_delegate =
844 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50845 if (dialog_delegate &&
846 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18847 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
848 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28849 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56850 *success = true;
[email protected]fad84eab2008-12-05 00:37:20851 }
[email protected]478ff2ed2009-04-21 23:49:18852 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
853 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56854 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28855 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56856 *success = true;
[email protected]fad84eab2008-12-05 00:37:20857 }
858 }
[email protected]c274acc2008-11-11 20:13:44859}
860
[email protected]fedaa7d2010-01-26 20:34:57861void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
862 if (browser_tracker_->ContainsHandle(handle)) {
863 Browser* browser = browser_tracker_->GetResource(handle);
864 browser->profile()->ShutdownSessionService();
865 *result = true;
866 } else {
867 *result = false;
868 }
869}
870
[email protected]71f65dd2009-02-11 19:14:56871void AutomationProvider::GetBrowserWindow(int index, int* handle) {
872 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29873 if (index >= 0) {
874 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29875 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29876 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56877 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29878 }
879 }
initial.commit09911bf2008-07-26 23:55:29880}
881
[email protected]24497032009-05-01 17:00:29882void AutomationProvider::FindNormalBrowserWindow(int* handle) {
883 *handle = 0;
884 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31885 Browser::TYPE_NORMAL,
886 false);
[email protected]24497032009-05-01 17:00:29887 if (browser)
888 *handle = browser_tracker_->Add(browser);
889}
890
[email protected]71f65dd2009-02-11 19:14:56891void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
892 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29893 Browser* browser = BrowserList::GetLastActive();
894 if (browser)
[email protected]71f65dd2009-02-11 19:14:56895 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29896}
897
[email protected]b2aa3ed72010-02-01 18:37:14898#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28899// TODO(estade): use this implementation for all platforms?
900void AutomationProvider::GetActiveWindow(int* handle) {
901 gfx::NativeWindow window =
902 BrowserList::GetLastActive()->window()->GetNativeHandle();
903 *handle = window_tracker_->Add(window);
904}
905#endif
906
[email protected]4f6381ee2009-04-16 02:46:33907void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
908 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56909 *success = false;
[email protected]4ae62752008-08-04 23:28:47910 if (browser_tracker_->ContainsHandle(handle)) {
911 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14912 if (browser->command_updater()->SupportsCommand(command) &&
913 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47914 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56915 *success = true;
[email protected]4ae62752008-08-04 23:28:47916 }
917 }
[email protected]4ae62752008-08-04 23:28:47918}
919
[email protected]4f6381ee2009-04-16 02:46:33920void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56921 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53922 // List of commands which just finish synchronously and don't require
923 // setting up an observer.
924 static const int kSynchronousCommands[] = {
925 IDC_HOME,
926 IDC_SELECT_NEXT_TAB,
927 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25928 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53929 };
[email protected]56e71b7c2009-03-27 03:05:56930 if (browser_tracker_->ContainsHandle(handle)) {
931 Browser* browser = browser_tracker_->GetResource(handle);
932 if (browser->command_updater()->SupportsCommand(command) &&
933 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53934 // First check if we can handle the command without using an observer.
935 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
936 if (command == kSynchronousCommands[i]) {
937 browser->ExecuteCommand(command);
938 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
939 true);
940 Send(reply_message);
941 return;
942 }
943 }
944
945 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42946 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
947 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27948 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42949 return;
950 }
[email protected]56e71b7c2009-03-27 03:05:56951 }
952 }
[email protected]49a14a82009-03-31 04:16:44953 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56954 Send(reply_message);
955}
956
[email protected]fc2e0872009-08-21 22:14:41957// This task just adds another task to the event queue. This is useful if
958// you want to ensure that any tasks added to the event queue after this one
959// have already been processed by the time |task| is run.
960class InvokeTaskLaterTask : public Task {
961 public:
962 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
963 virtual ~InvokeTaskLaterTask() {}
964
965 virtual void Run() {
966 MessageLoop::current()->PostTask(FROM_HERE, task_);
967 }
968
969 private:
970 Task* task_;
971
972 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
973};
974
initial.commit09911bf2008-07-26 23:55:29975void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
976 int handle,
[email protected]d1a5941e2009-08-13 23:34:24977 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29978 int flags) {
[email protected]b410bc32009-08-14 01:11:14979 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51980 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41981 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29982 }
983}
984
[email protected]60507b12009-11-02 23:51:35985void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
986 int handle,
987 const gfx::Point& location) {
988 if (window_tracker_->ContainsHandle(handle))
989 ui_controls::SendMouseMove(location.x(), location.y());
990}
991
initial.commit09911bf2008-07-26 23:55:29992void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
993 int handle,
[email protected]bc1407f2009-09-29 00:33:35994 int key,
initial.commit09911bf2008-07-26 23:55:29995 int flags) {
[email protected]b410bc32009-08-14 01:11:14996 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29997 return;
998
[email protected]b410bc32009-08-14 01:11:14999 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291000 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:351001 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:381002 ((flags & views::Event::EF_CONTROL_DOWN) ==
1003 views::Event::EF_CONTROL_DOWN),
1004 ((flags & views::Event::EF_SHIFT_DOWN) ==
1005 views::Event::EF_SHIFT_DOWN),
1006 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301007 views::Event::EF_ALT_DOWN),
1008 ((flags & views::Event::EF_COMMAND_DOWN) ==
1009 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291010}
initial.commit09911bf2008-07-26 23:55:291011
[email protected]71f65dd2009-02-11 19:14:561012void AutomationProvider::IsWindowActive(int handle, bool* success,
1013 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291014 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171015 *is_active =
1016 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561017 *success = true;
initial.commit09911bf2008-07-26 23:55:291018 } else {
[email protected]71f65dd2009-02-11 19:14:561019 *success = false;
1020 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291021 }
1022}
1023
[email protected]71f65dd2009-02-11 19:14:561024void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1025 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291026
1027 if (browser_tracker_->ContainsHandle(handle)) {
1028 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561029 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291030 }
initial.commit09911bf2008-07-26 23:55:291031}
1032
[email protected]982921f12009-10-27 21:43:531033void AutomationProvider::GetType(int handle, int* type_as_int) {
1034 *type_as_int = -1; // -1 is the error code
1035
1036 if (browser_tracker_->ContainsHandle(handle)) {
1037 Browser* browser = browser_tracker_->GetResource(handle);
1038 *type_as_int = static_cast<int>(browser->type());
1039 }
1040}
1041
[email protected]71f65dd2009-02-11 19:14:561042void AutomationProvider::GetTab(int win_handle, int tab_index,
1043 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561044 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291045 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1046 Browser* browser = browser_tracker_->GetResource(win_handle);
1047 if (tab_index < browser->tab_count()) {
1048 TabContents* tab_contents =
1049 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571050 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291051 }
1052 }
initial.commit09911bf2008-07-26 23:55:291053}
1054
[email protected]71f65dd2009-02-11 19:14:561055void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1056 std::wstring* title) {
1057 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291058 if (tab_tracker_->ContainsHandle(handle)) {
1059 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361060 NavigationEntry* entry = tab->GetActiveEntry();
1061 if (entry != NULL) {
1062 *title = UTF16ToWideHack(entry->title());
1063 } else {
1064 *title = std::wstring();
1065 }
[email protected]71f65dd2009-02-11 19:14:561066 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291067 }
initial.commit09911bf2008-07-26 23:55:291068}
1069
[email protected]77bc6732009-04-20 22:01:031070void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1071 *tabstrip_index = -1; // -1 is the error code
1072
1073 if (tab_tracker_->ContainsHandle(handle)) {
1074 NavigationController* tab = tab_tracker_->GetResource(handle);
1075 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121076 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031077 }
1078}
1079
initial.commit09911bf2008-07-26 23:55:291080void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1081 if (window_tracker_->ContainsHandle(handle)) {
1082 window_tracker_->Remove(window_tracker_->GetResource(handle));
1083 }
1084}
1085
1086void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161087 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571088 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291089}
1090
1091// TODO(brettw) change this to accept GURLs when history supports it
1092void AutomationProvider::OnRedirectQueryComplete(
1093 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371094 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291095 bool success,
[email protected]379c2b12009-07-01 21:50:331096 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291097 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561098 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291099
[email protected]deb57402009-02-06 01:35:301100 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101101 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291102 if (success) {
initial.commit09911bf2008-07-26 23:55:291103 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301104 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291105 }
1106
[email protected]4f3dc372009-02-24 00:10:291107 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301108
[email protected]71f65dd2009-02-11 19:14:561109 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:171110 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561111 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291112}
1113
1114bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571115 DCHECK(channel_.get());
1116 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291117}
1118
1119Browser* AutomationProvider::FindAndActivateTab(
1120 NavigationController* controller) {
1121 int tab_index;
1122 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1123 if (browser)
1124 browser->SelectTabContentsAt(tab_index, true);
1125
1126 return browser;
1127}
1128
[email protected]71f65dd2009-02-11 19:14:561129void AutomationProvider::GetCookies(const GURL& url, int handle,
1130 int* value_size,
1131 std::string* value) {
1132 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291133 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1134 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311135
1136 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001137 scoped_refptr<URLRequestContextGetter> request_context =
1138 tab->tab_contents()->request_context();
1139 if (!request_context.get())
1140 request_context = tab->profile()->GetRequestContext();
1141
1142 net::CookieStore* cookie_store = request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311143
1144 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561145 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291146 }
initial.commit09911bf2008-07-26 23:55:291147}
1148
[email protected]71f65dd2009-02-11 19:14:561149void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291150 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561151 int handle,
1152 int* response_value) {
1153 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291154
1155 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1156 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311157
[email protected]dfa46e5f2009-11-17 18:48:431158 scoped_refptr<URLRequestContextGetter> request_context =
1159 tab->tab_contents()->request_context();
1160 if (!request_context.get())
1161 request_context = tab->profile()->GetRequestContext();
1162
[email protected]be180c802009-10-23 06:33:311163 // Since we are running on the UI thread don't call GetURLRequestContext().
1164 scoped_refptr<net::CookieStore> cookie_store =
[email protected]dfa46e5f2009-11-17 18:48:431165 request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311166
1167 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561168 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291169 }
initial.commit09911bf2008-07-26 23:55:291170}
1171
[email protected]5fa57942010-04-21 23:07:221172void AutomationProvider::DeleteCookie(const GURL& url,
1173 const std::string& cookie_name,
1174 int handle, bool* success) {
1175 *success = false;
1176 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1177 NavigationController* tab = tab_tracker_->GetResource(handle);
1178 net::CookieStore* cookie_store =
1179 tab->profile()->GetRequestContext()->GetCookieStore();
1180 cookie_store->DeleteCookie(url, cookie_name);
1181 *success = true;
1182 }
1183}
1184
[email protected]71f65dd2009-02-11 19:14:561185void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1186 *success = false;
initial.commit09911bf2008-07-26 23:55:291187 if (tab_tracker_->ContainsHandle(handle)) {
1188 NavigationController* tab = tab_tracker_->GetResource(handle);
1189 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541190 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561191 *success = true;
initial.commit09911bf2008-07-26 23:55:291192 }
initial.commit09911bf2008-07-26 23:55:291193}
1194
[email protected]71f65dd2009-02-11 19:14:561195void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1196 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291197
1198 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561199 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341200 TabContents* tab_contents =
1201 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161202 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1203 if (rph)
1204 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291205 }
initial.commit09911bf2008-07-26 23:55:291206}
1207
1208void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331209 NOTREACHED() << "This function has been deprecated. "
1210 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291211}
1212
[email protected]71f65dd2009-02-11 19:14:561213void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291214 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561215 const std::wstring& script,
1216 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291217 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341218 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1219 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571220 // Set the routing id of this message with the controller.
1221 // This routing id needs to be remembered for the reverse
1222 // communication while sending back the response of
1223 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331224 std::wstring set_automation_id;
1225 SStringPrintf(&set_automation_id,
1226 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561227 reply_message->routing_id());
1228
1229 DCHECK(reply_message_ == NULL);
1230 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291231
[email protected]57c6a652009-05-04 07:58:341232 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331233 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341234 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061235 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571236 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291237 }
1238
1239 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561240 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1241 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291242 }
1243}
1244
[email protected]71f65dd2009-02-11 19:14:561245void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1246 *visible = false;
[email protected]20e93d12008-08-28 16:31:571247
[email protected]59560e0b2009-06-04 03:30:221248 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251249#if defined(OS_CHROMEOS)
1250 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1251 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1252 for (BrowserList::const_iterator it = BrowserList::begin();
1253 it != BrowserList::end(); ++it) {
1254 if ((*it)->type() == Browser::TYPE_POPUP) {
1255 const GURL& url =
1256 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1257
1258 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1259 url.host() == chrome::kChromeUIFileBrowseHost) {
1260 *visible = true;
1261 break;
1262 }
1263 }
1264 }
1265#else
[email protected]59560e0b2009-06-04 03:30:221266 Browser* browser = browser_tracker_->GetResource(handle);
1267 if (browser) {
1268 *visible = browser->window()->IsDownloadShelfVisible();
1269 }
[email protected]f5bf8ccf2010-02-05 18:19:251270#endif
[email protected]59560e0b2009-06-04 03:30:221271 }
initial.commit09911bf2008-07-26 23:55:291272}
1273
[email protected]59560e0b2009-06-04 03:30:221274void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1275 if (browser_tracker_->ContainsHandle(handle)) {
1276 Browser* browser = browser_tracker_->GetResource(handle);
1277 if (browser) {
1278 if (visible)
1279 browser->window()->GetDownloadShelf()->Show();
1280 else
1281 browser->window()->GetDownloadShelf()->Close();
1282 }
1283 }
1284}
1285
[email protected]34930432009-11-09 00:12:091286void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1287 *visible = false;
1288
1289 if (browser_tracker_->ContainsHandle(handle)) {
1290 Browser* browser = browser_tracker_->GetResource(handle);
1291 if (browser)
1292 *visible = browser->window()->IsFullscreen();
1293 }
1294}
1295
1296void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1297 bool* visible) {
1298 *visible = false;
1299
1300 if (browser_tracker_->ContainsHandle(handle)) {
1301 Browser* browser = browser_tracker_->GetResource(handle);
1302 if (browser)
1303 *visible = browser->window()->IsFullscreenBubbleVisible();
1304 }
1305}
[email protected]59560e0b2009-06-04 03:30:221306
[email protected]71f65dd2009-02-11 19:14:561307void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1308 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291309 if (tab_tracker_->ContainsHandle(handle)) {
1310 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111311 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291312 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561313 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291314 }
1315 }
initial.commit09911bf2008-07-26 23:55:291316}
1317
initial.commit09911bf2008-07-26 23:55:291318void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561319 int handle, const std::wstring& find_request,
1320 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311321 NOTREACHED() << "This function has been deprecated."
1322 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561323 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311324 return;
1325}
1326
[email protected]4f999132009-03-31 18:08:401327void AutomationProvider::HandleFindRequest(
1328 int handle,
1329 const AutomationMsg_Find_Params& params,
1330 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291331 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561332 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1333 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291334 return;
1335 }
1336
1337 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111338 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291339
1340 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141341 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291342
[email protected]57c6a652009-05-04 07:58:341343 tab_contents->set_current_find_request_id(
1344 FindInPageNotificationObserver::kFindInPageRequestId);
1345 tab_contents->render_view_host()->StartFinding(
1346 FindInPageNotificationObserver::kFindInPageRequestId,
1347 params.search_string, params.forward, params.match_case,
1348 params.find_next);
initial.commit09911bf2008-07-26 23:55:291349}
1350
[email protected]5f8af2a2008-08-06 22:49:451351void AutomationProvider::HandleOpenFindInPageRequest(
1352 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291353 if (browser_tracker_->ContainsHandle(handle)) {
1354 Browser* browser = browser_tracker_->GetResource(handle);
1355 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451356 }
1357}
1358
[email protected]71f65dd2009-02-11 19:14:561359void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561360 *visible = false;
[email protected]855c0142009-09-28 22:35:241361 Browser* browser = browser_tracker_->GetResource(handle);
1362 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581363 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171364 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241365 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291366 }
[email protected]20e93d12008-08-28 16:31:571367}
1368
[email protected]71f65dd2009-02-11 19:14:561369void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1370 int* y) {
[email protected]9e0534b2008-10-21 15:03:011371 gfx::Point position(0, 0);
1372 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291373 if (browser_tracker_->ContainsHandle(handle)) {
1374 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581375 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171376 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581377 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291378 }
[email protected]20e93d12008-08-28 16:31:571379
[email protected]71f65dd2009-02-11 19:14:561380 *x = position.x();
1381 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571382}
1383
[email protected]4512cb52010-04-05 19:50:251384// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1385// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581386void AutomationProvider::GetBookmarkBarVisibility(int handle,
1387 bool* visible,
1388 bool* animating) {
1389 *visible = false;
1390 *animating = false;
1391
1392 if (browser_tracker_->ContainsHandle(handle)) {
1393 Browser* browser = browser_tracker_->GetResource(handle);
1394 if (browser) {
[email protected]ab6ca392010-04-07 00:44:131395#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1396 // IsBookmarkBarVisible() line looks correct but is not
1397 // consistent across platforms. Specifically, on Mac/Linux, it
1398 // returns false if the bar is hidden in a pref (even if visible
1399 // on the NTP). On ChromeOS, it returned true if on NTP
1400 // independent of the pref. Making the code more consistent
1401 // caused a perf bot regression on Windows (which shares views).
1402 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251403 *visible = browser->profile()->GetPrefs()->GetBoolean(
1404 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401405#else
1406 *visible = browser->window()->IsBookmarkBarVisible();
1407#endif
[email protected]c3240722010-03-05 21:52:581408 *animating = browser->window()->IsBookmarkBarAnimating();
1409 }
1410 }
1411}
1412
[email protected]6d8ffc9f2010-03-12 18:27:531413void AutomationProvider::GetBookmarksAsJSON(int handle,
1414 std::string* bookmarks_as_json,
1415 bool *success) {
1416 *success = false;
1417 if (browser_tracker_->ContainsHandle(handle)) {
1418 Browser* browser = browser_tracker_->GetResource(handle);
1419 if (browser) {
1420 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1421 return;
1422 }
1423 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1424 browser->profile(),
1425 browser->profile()->GetBookmarkModel());
1426 *success = storage->SerializeData(bookmarks_as_json);
1427 }
1428 }
1429}
1430
1431void AutomationProvider::WaitForBookmarkModelToLoad(
1432 int handle,
1433 IPC::Message* reply_message) {
1434 if (browser_tracker_->ContainsHandle(handle)) {
1435 Browser* browser = browser_tracker_->GetResource(handle);
1436 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1437 if (model->IsLoaded()) {
1438 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1439 reply_message, true);
1440 Send(reply_message);
1441 } else {
1442 // The observer will delete itself when done.
1443 new AutomationProviderBookmarkModelObserver(this, reply_message,
1444 model);
1445 }
1446 }
1447}
1448
1449void AutomationProvider::AddBookmarkGroup(int handle,
1450 int64 parent_id, int index,
1451 std::wstring title,
1452 bool* success) {
1453 if (browser_tracker_->ContainsHandle(handle)) {
1454 Browser* browser = browser_tracker_->GetResource(handle);
1455 if (browser) {
1456 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1457 if (!model->IsLoaded()) {
1458 *success = false;
1459 return;
1460 }
1461 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1462 DCHECK(parent);
1463 if (parent) {
1464 const BookmarkNode* child = model->AddGroup(parent, index,
1465 WideToUTF16(title));
1466 DCHECK(child);
1467 if (child)
1468 *success = true;
1469 }
1470 }
1471 }
1472 *success = false;
1473}
1474
1475void AutomationProvider::AddBookmarkURL(int handle,
1476 int64 parent_id, int index,
1477 std::wstring title, const GURL& url,
1478 bool* success) {
1479 if (browser_tracker_->ContainsHandle(handle)) {
1480 Browser* browser = browser_tracker_->GetResource(handle);
1481 if (browser) {
1482 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1483 if (!model->IsLoaded()) {
1484 *success = false;
1485 return;
1486 }
1487 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1488 DCHECK(parent);
1489 if (parent) {
1490 const BookmarkNode* child = model->AddURL(parent, index,
1491 WideToUTF16(title), url);
1492 DCHECK(child);
1493 if (child)
1494 *success = true;
1495 }
1496 }
1497 }
1498 *success = false;
1499}
1500
1501void AutomationProvider::ReparentBookmark(int handle,
1502 int64 id, int64 new_parent_id,
1503 int index,
1504 bool* success) {
1505 if (browser_tracker_->ContainsHandle(handle)) {
1506 Browser* browser = browser_tracker_->GetResource(handle);
1507 if (browser) {
1508 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1509 if (!model->IsLoaded()) {
1510 *success = false;
1511 return;
1512 }
1513 const BookmarkNode* node = model->GetNodeByID(id);
1514 DCHECK(node);
1515 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1516 DCHECK(new_parent);
1517 if (node && new_parent) {
1518 model->Move(node, new_parent, index);
1519 *success = true;
1520 }
1521 }
1522 }
1523 *success = false;
1524}
1525
1526void AutomationProvider::SetBookmarkTitle(int handle,
1527 int64 id, std::wstring title,
1528 bool* success) {
1529 if (browser_tracker_->ContainsHandle(handle)) {
1530 Browser* browser = browser_tracker_->GetResource(handle);
1531 if (browser) {
1532 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1533 if (!model->IsLoaded()) {
1534 *success = false;
1535 return;
1536 }
1537 const BookmarkNode* node = model->GetNodeByID(id);
1538 DCHECK(node);
1539 if (node) {
1540 model->SetTitle(node, WideToUTF16(title));
1541 *success = true;
1542 }
1543 }
1544 }
1545 *success = false;
1546}
1547
1548void AutomationProvider::SetBookmarkURL(int handle,
1549 int64 id, const GURL& url,
1550 bool* success) {
1551 if (browser_tracker_->ContainsHandle(handle)) {
1552 Browser* browser = browser_tracker_->GetResource(handle);
1553 if (browser) {
1554 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1555 if (!model->IsLoaded()) {
1556 *success = false;
1557 return;
1558 }
1559 const BookmarkNode* node = model->GetNodeByID(id);
1560 DCHECK(node);
1561 if (node) {
1562 model->SetURL(node, url);
1563 *success = true;
1564 }
1565 }
1566 }
1567 *success = false;
1568}
1569
1570void AutomationProvider::RemoveBookmark(int handle,
1571 int64 id,
1572 bool* success) {
1573 if (browser_tracker_->ContainsHandle(handle)) {
1574 Browser* browser = browser_tracker_->GetResource(handle);
1575 if (browser) {
1576 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1577 if (!model->IsLoaded()) {
1578 *success = false;
1579 return;
1580 }
1581 const BookmarkNode* node = model->GetNodeByID(id);
1582 DCHECK(node);
1583 if (node) {
1584 const BookmarkNode* parent = node->GetParent();
1585 DCHECK(parent);
1586 model->Remove(parent, parent->IndexOfChild(node));
1587 *success = true;
1588 }
1589 }
1590 }
1591 *success = false;
1592}
1593
[email protected]24e2b102010-04-29 17:56:471594// Sample json input: { "command": "GetHistoryInfo",
1595// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361596// Refer chrome/test/pyautolib/history_info.py for sample json output.
1597void AutomationProvider::GetHistoryInfo(
1598 DictionaryValue* args,
1599 IPC::Message* reply_message) {
1600 consumer_.CancelAllRequests();
1601
1602 std::wstring search_text;
1603 args->GetString(L"search_text", &search_text);
1604
1605 // Fetch history.
1606 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1607 history::QueryOptions options;
1608 // The observer owns itself. It deletes itself after it fetches history.
1609 AutomationProviderHistoryObserver* history_observer =
1610 new AutomationProviderHistoryObserver(this, reply_message);
1611 hs->QueryHistory(
1612 search_text,
1613 options,
1614 &consumer_,
1615 NewCallback(history_observer,
1616 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1617}
1618
[email protected]24e2b102010-04-29 17:56:471619// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361620// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]d4adc292010-04-15 18:06:391621void AutomationProvider::GetDownloadsInfo(
1622 DictionaryValue* args,
1623 IPC::Message* reply_message) {
1624 std::string json_return;
1625 bool reply_return = true;
1626 AutomationProviderDownloadManagerObserver observer;
1627 std::vector<DownloadItem*> downloads;
1628 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1629
1630 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471631 json_return = "{\"error\": \"no download manager\"}";
[email protected]d4adc292010-04-15 18:06:391632 reply_return = false;
1633 } else {
1634 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1635 // since that would be transient; a download might enter and empty out
1636 // the current download queue too soon to be noticed.
1637 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1638 downloads = observer.Downloads();
1639 }
1640
1641 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1642 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1643 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1644 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1645 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1646
1647 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1648 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1649 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1650 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1651 std::string("DANGEROUS_BUT_VALIDATED");
1652
1653 ListValue* list_of_downloads = new ListValue;
1654 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1655 it != downloads.end();
1656 it++) { // Fill info about each download item.
1657 DictionaryValue* dl_item_value = new DictionaryValue;
1658 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1659 dl_item_value->SetString(L"url", (*it)->url().spec());
1660 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1661 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1662 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1663 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1664 dl_item_value->SetBoolean(L"open_when_complete",
1665 (*it)->open_when_complete());
1666 dl_item_value->SetBoolean(L"is_extension_install",
1667 (*it)->is_extension_install());
1668 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1669 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1670 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1671 dl_item_value->SetString(L"safety_state",
1672 safety_state_to_string[(*it)->safety_state()]);
1673 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1674 list_of_downloads->Append(dl_item_value);
1675 }
1676 return_value->Set(L"downloads", list_of_downloads);
1677 base::JSONWriter::Write(return_value.get(), false, &json_return);
1678
1679 AutomationMsg_SendJSONRequest::WriteReplyParams(
1680 reply_message, json_return, reply_return);
1681 Send(reply_message);
1682 // All value objects allocated above are owned by |return_value|
1683 // and get freed by it.
1684}
1685
[email protected]59a611242010-04-02 02:24:041686void AutomationProvider::WaitForDownloadsToComplete(
1687 DictionaryValue* args,
1688 IPC::Message* reply_message) {
1689 std::string json_return;
1690 bool reply_return = true;
1691 AutomationProviderDownloadManagerObserver observer;
1692 std::vector<DownloadItem*> downloads;
1693
1694 // Look for a quick return.
1695 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471696 json_return = "{\"error\": \"no download manager\"}";
[email protected]59a611242010-04-02 02:24:041697 reply_return = false;
1698 } else {
1699 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
1700 FilePath());
1701 downloads = observer.Downloads();
1702 if (downloads.size() == 0) {
1703 json_return = "{}";
1704 }
1705 }
1706 if (!json_return.empty()) {
1707 AutomationMsg_SendJSONRequest::WriteReplyParams(
1708 reply_message, json_return, reply_return);
1709 Send(reply_message);
1710 }
1711
1712 // The observer owns itself. When the last observed item pings, it
1713 // deletes itself.
1714 AutomationProviderDownloadItemObserver* item_observer =
1715 new AutomationProviderDownloadItemObserver(
1716 this, reply_message, downloads.size());
1717 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
1718 i != downloads.end();
1719 i++) {
1720 (*i)->AddObserver(item_observer);
1721 }
1722}
1723
[email protected]24e2b102010-04-29 17:56:471724// Sample json input: { "command": "GetPrefsInfo" }
1725// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
1726void AutomationProvider::GetPrefsInfo(DictionaryValue* args,
1727 IPC::Message* reply_message) {
1728 std::string json_return;
1729 bool reply_return = true;
1730
1731 const PrefService::PreferenceSet& prefs =
1732 profile_->GetPrefs()->preference_set();
1733 DictionaryValue* items = new DictionaryValue;
1734 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
1735 it != prefs.end(); ++it) {
1736 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
1737 }
1738 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1739 return_value->Set(L"prefs", items); // return_value owns items.
1740
1741 base::JSONWriter::Write(return_value.get(), false, &json_return);
1742 AutomationMsg_SendJSONRequest::WriteReplyParams(
1743 reply_message, json_return, reply_return);
1744 Send(reply_message);
1745}
1746
1747// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
1748void AutomationProvider::SetPrefs(DictionaryValue* args,
1749 IPC::Message* reply_message) {
1750 bool reply_return = true;
1751 std::string json_return = "{}";
1752 std::wstring path;
1753 Value* val;
1754 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
1755 PrefService* pref_service = profile_->GetPrefs();
1756 const PrefService::Preference* pref =
1757 pref_service->FindPreference(path.c_str());
1758 if (!pref) { // Not a registered pref.
1759 json_return = "{\"error\": \"pref not registered.\"}";
1760 reply_return = false;
1761 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
1762 json_return = "{\"error\": \"pref is managed. cannot be changed.\"}";
1763 reply_return = false;
1764 } else { // Set the pref.
1765 pref_service->Set(path.c_str(), *val);
1766 }
1767 } else {
1768 json_return = "{\"error\": \"no pref path or value given.\"}";
1769 reply_return = false;
1770 }
1771
1772 AutomationMsg_SendJSONRequest::WriteReplyParams(
1773 reply_message, json_return, reply_return);
1774 Send(reply_message);
1775}
1776
[email protected]59a611242010-04-02 02:24:041777void AutomationProvider::SendJSONRequest(
1778 int handle,
1779 std::string json_request,
1780 IPC::Message* reply_message) {
1781 Browser* browser = NULL;
1782 std::string error_string;
1783 scoped_ptr<Value> values;
1784
1785 // Basic error checking.
1786 if (browser_tracker_->ContainsHandle(handle)) {
1787 browser = browser_tracker_->GetResource(handle);
1788 }
1789 if (!browser) {
1790 error_string = "no browser object";
1791 } else {
1792 base::JSONReader reader;
1793 std::string error;
[email protected]ba399672010-04-06 15:42:391794 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
[email protected]59a611242010-04-02 02:24:041795 if (!error.empty()) {
1796 error_string = error;
1797 }
1798 }
1799
1800 // Make sure input is a dict with a string command.
1801 std::string command;
1802 DictionaryValue* dict_value = NULL;
1803 if (error_string.empty()) {
1804 if (values->GetType() != Value::TYPE_DICTIONARY) {
1805 error_string = "not a dict or no command key in dict";
1806 } else {
1807 // Ownership remains with "values" variable.
1808 dict_value = static_cast<DictionaryValue*>(values.get());
1809 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
1810 error_string = "no command key in dict or not a string command";
1811 }
1812 }
1813 }
1814
[email protected]24e2b102010-04-29 17:56:471815 // Map json commands to their handlers.
1816 std::map<std::string, JsonHandler> handler_map;
1817 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
1818 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
1819 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
1820 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
1821 handler_map["WaitForAllDownloadsToComplete"] =
1822 &AutomationProvider::WaitForDownloadsToComplete;
1823
[email protected]59a611242010-04-02 02:24:041824 if (error_string.empty()) {
[email protected]24e2b102010-04-29 17:56:471825 if (handler_map.find(std::string(command)) != handler_map.end()) {
1826 (this->*handler_map[command])(dict_value, reply_message);
[email protected]59a611242010-04-02 02:24:041827 return;
1828 } else {
[email protected]24e2b102010-04-29 17:56:471829 error_string = "Unknown command. Options: ";
1830 for (std::map<std::string, JsonHandler>::const_iterator it =
1831 handler_map.begin(); it != handler_map.end(); ++it) {
1832 error_string += it->first + ", ";
1833 }
[email protected]59a611242010-04-02 02:24:041834 }
1835 }
1836
1837 // If we hit an error, return info.
[email protected]24e2b102010-04-29 17:56:471838 // Return a dict of {"error", "descriptive_string_for_error"}.
[email protected]59a611242010-04-02 02:24:041839 // Else return an empty dict.
1840 std::string json_string;
1841 bool success = true;
1842 if (!error_string.empty()) {
1843 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
1844 dict->SetString(L"error", error_string);
1845 base::JSONWriter::Write(dict.get(), false, &json_string);
1846 success = false;
1847 } else {
1848 json_string = "{}";
1849 }
1850 AutomationMsg_SendJSONRequest::WriteReplyParams(
1851 reply_message, json_string, success);
1852 Send(reply_message);
1853}
1854
initial.commit09911bf2008-07-26 23:55:291855void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561856 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341857 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1858 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561859 DCHECK(reply_message_ == NULL);
1860 reply_message_ = reply_message;
1861
[email protected]d9f9b792009-06-24 13:17:121862 DevToolsManager::GetInstance()->InspectElement(
1863 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291864 } else {
[email protected]71f65dd2009-02-11 19:14:561865 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1866 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291867 }
1868}
1869
1870void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091871 if (reply_message_) {
1872 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1873 num_resources);
1874 Send(reply_message_);
1875 reply_message_ = NULL;
1876 }
initial.commit09911bf2008-07-26 23:55:291877}
1878
[email protected]a7eee32f2009-05-22 18:08:171879class SetProxyConfigTask : public Task {
1880 public:
[email protected]be180c802009-10-23 06:33:311881 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
1882 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:251883 : request_context_getter_(request_context_getter),
1884 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:171885 virtual void Run() {
1886 // First, deserialize the JSON string. If this fails, log and bail.
1887 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:391888 std::string error_msg;
1889 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:171890 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1891 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:391892 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:171893 return;
1894 }
1895
1896 scoped_ptr<DictionaryValue> dict(
1897 static_cast<DictionaryValue*>(root.release()));
1898 // Now put together a proxy configuration from the deserialized string.
1899 net::ProxyConfig pc;
1900 PopulateProxyConfig(*dict.get(), &pc);
1901
[email protected]be180c802009-10-23 06:33:311902 net::ProxyService* proxy_service =
1903 request_context_getter_->GetURLRequestContext()->proxy_service();
1904 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:171905 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1906 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:311907 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:171908 }
1909
1910 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1911 DCHECK(pc);
1912 bool no_proxy = false;
1913 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1914 // Make no changes to the ProxyConfig.
1915 return;
1916 }
1917 bool auto_config;
1918 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:481919 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:171920 }
1921 std::string pac_url;
1922 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:481923 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:171924 }
1925 std::string proxy_bypass_list;
1926 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:481927 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:171928 }
1929 std::string proxy_server;
1930 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:481931 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:171932 }
1933 }
1934
1935 private:
[email protected]be180c802009-10-23 06:33:311936 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:171937 std::string proxy_config_;
1938};
1939
1940
1941void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:311942 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
1943 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:171944 FilePath user_data_dir;
1945 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1946 ProfileManager* profile_manager = g_browser_process->profile_manager();
1947 DCHECK(profile_manager);
1948 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1949 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:311950 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171951 }
[email protected]be180c802009-10-23 06:33:311952 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:171953
[email protected]fae20792009-10-28 20:31:581954 ChromeThread::PostTask(
1955 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:311956 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171957}
1958
[email protected]4f3dc372009-02-24 00:10:291959void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331960 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291961 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291962 if (tab_tracker_->ContainsHandle(handle)) {
1963 NavigationController* tab = tab_tracker_->GetResource(handle);
1964 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1965 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331966 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291967 }
initial.commit09911bf2008-07-26 23:55:291968}
1969
[email protected]6a5670d22009-10-27 16:21:341970void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:141971 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:531972 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
1973 reply_message);
1974}
1975
1976void AutomationProvider::OpenNewBrowserWindowOfType(
1977 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:141978 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291979 // We may have no current browser windows open so don't rely on
1980 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:531981 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
1982 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:051983 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151984 if (show)
[email protected]15952e462008-11-14 00:29:051985 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291986}
1987
[email protected]71f65dd2009-02-11 19:14:561988void AutomationProvider::GetWindowForBrowser(int browser_handle,
1989 bool* success,
1990 int* handle) {
1991 *success = false;
1992 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291993
1994 if (browser_tracker_->ContainsHandle(browser_handle)) {
1995 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201996 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291997 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201998 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561999 *success = true;
initial.commit09911bf2008-07-26 23:55:292000 }
initial.commit09911bf2008-07-26 23:55:292001}
2002
2003void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562004 int browser_handle,
2005 bool* success,
2006 int* autocomplete_edit_handle) {
2007 *success = false;
2008 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292009
2010 if (browser_tracker_->ContainsHandle(browser_handle)) {
2011 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062012 LocationBar* loc_bar = browser->window()->GetLocationBar();
2013 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292014 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562015 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2016 *success = true;
initial.commit09911bf2008-07-26 23:55:292017 }
initial.commit09911bf2008-07-26 23:55:292018}
initial.commit09911bf2008-07-26 23:55:292019
[email protected]71f65dd2009-02-11 19:14:562020void AutomationProvider::ShowInterstitialPage(int tab_handle,
2021 const std::string& html_text,
2022 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292023 if (tab_tracker_->ContainsHandle(tab_handle)) {
2024 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112025 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402026
[email protected]7dad3d5f2010-03-04 00:27:012027 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:402028 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342029 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402030 GURL("about:interstitial"),
2031 html_text);
2032 interstitial->Show();
2033 return;
initial.commit09911bf2008-07-26 23:55:292034 }
[email protected]71f65dd2009-02-11 19:14:562035
[email protected]457f5cf2009-08-18 16:37:522036 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
2037 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562038 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292039}
2040
[email protected]71f65dd2009-02-11 19:14:562041void AutomationProvider::HideInterstitialPage(int tab_handle,
2042 bool* success) {
2043 *success = false;
[email protected]57c6a652009-05-04 07:58:342044 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2045 if (tab_contents && tab_contents->interstitial_page()) {
2046 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562047 *success = true;
initial.commit09911bf2008-07-26 23:55:292048 }
initial.commit09911bf2008-07-26 23:55:292049}
2050
[email protected]71f65dd2009-02-11 19:14:562051void AutomationProvider::CloseTab(int tab_handle,
2052 bool wait_until_closed,
2053 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292054 if (tab_tracker_->ContainsHandle(tab_handle)) {
2055 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2056 int index;
2057 Browser* browser = Browser::GetBrowserForController(controller, &index);
2058 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142059 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112060 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452061 return;
initial.commit09911bf2008-07-26 23:55:292062 }
[email protected]de246f52009-02-25 18:25:452063
2064 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:552065 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292066}
2067
[email protected]71f65dd2009-02-11 19:14:562068void AutomationProvider::CloseBrowser(int browser_handle,
2069 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292070 if (browser_tracker_->ContainsHandle(browser_handle)) {
2071 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562072 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562073 reply_message);
[email protected]f3e99e32008-07-30 04:48:392074 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292075 } else {
2076 NOTREACHED();
2077 }
2078}
2079
[email protected]71f65dd2009-02-11 19:14:562080void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2081 if (browser_tracker_->ContainsHandle(browser_handle)) {
2082 Browser* browser = browser_tracker_->GetResource(browser_handle);
2083 browser->window()->Close();
2084 } else {
2085 NOTREACHED();
2086 }
2087}
2088
[email protected]71f65dd2009-02-11 19:14:562089void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:572090 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:562091 AutomationMsg_NavigationResponseValues* status) {
2092 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:292093
2094 if (tab_tracker_->ContainsHandle(handle)) {
2095 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:572096 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:562097 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:292098 }
initial.commit09911bf2008-07-26 23:55:292099}
2100
[email protected]4150ef02009-08-19 23:14:262101void AutomationProvider::NavigateExternalTabAtIndex(
2102 int handle, int navigation_index,
2103 AutomationMsg_NavigationResponseValues* status) {
2104 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
2105
2106 if (tab_tracker_->ContainsHandle(handle)) {
2107 NavigationController* tab = tab_tracker_->GetResource(handle);
2108 tab->GoToIndex(navigation_index);
2109 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
2110 }
2111}
2112
[email protected]71f65dd2009-02-11 19:14:562113void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2114 IPC::Message* reply_message) {
2115 if (tab_tracker_->ContainsHandle(tab_handle)) {
2116 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2117 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:142118 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:562119 }
2120}
2121
[email protected]71f65dd2009-02-11 19:14:562122void AutomationProvider::GetSecurityState(int handle, bool* success,
2123 SecurityStyle* security_style,
2124 int* ssl_cert_status,
2125 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:292126 if (tab_tracker_->ContainsHandle(handle)) {
2127 NavigationController* tab = tab_tracker_->GetResource(handle);
2128 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562129 *success = true;
2130 *security_style = entry->ssl().security_style();
2131 *ssl_cert_status = entry->ssl().cert_status();
2132 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292133 } else {
[email protected]71f65dd2009-02-11 19:14:562134 *success = false;
2135 *security_style = SECURITY_STYLE_UNKNOWN;
2136 *ssl_cert_status = 0;
2137 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292138 }
2139}
2140
[email protected]71f65dd2009-02-11 19:14:562141void AutomationProvider::GetPageType(int handle, bool* success,
2142 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:292143 if (tab_tracker_->ContainsHandle(handle)) {
2144 NavigationController* tab = tab_tracker_->GetResource(handle);
2145 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562146 *page_type = entry->page_type();
2147 *success = true;
initial.commit09911bf2008-07-26 23:55:292148 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:342149 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:562150 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:342151 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:562152 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292153 } else {
[email protected]71f65dd2009-02-11 19:14:562154 *success = false;
2155 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292156 }
2157}
2158
[email protected]84abba62009-10-07 17:01:442159void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
2160 int* duration_ms) {
2161 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
2162 event_name);
2163}
2164
[email protected]71f65dd2009-02-11 19:14:562165void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
2166 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292167 if (tab_tracker_->ContainsHandle(handle)) {
2168 NavigationController* tab = tab_tracker_->GetResource(handle);
2169 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072170 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:402171 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:472172 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:342173 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:292174 if (ssl_blocking_page) {
2175 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:012176 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:562177 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:292178 return;
2179 }
2180 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:522181 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2182 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:562183 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292184 return;
2185 }
2186 }
2187 }
2188 // We failed.
[email protected]457f5cf2009-08-18 16:37:522189 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2190 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562191 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292192}
2193
[email protected]71f65dd2009-02-11 19:14:562194void AutomationProvider::BringBrowserToFront(int browser_handle,
2195 bool* success) {
initial.commit09911bf2008-07-26 23:55:292196 if (browser_tracker_->ContainsHandle(browser_handle)) {
2197 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:062198 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:562199 *success = true;
initial.commit09911bf2008-07-26 23:55:292200 } else {
[email protected]71f65dd2009-02-11 19:14:562201 *success = false;
initial.commit09911bf2008-07-26 23:55:292202 }
2203}
2204
[email protected]71f65dd2009-02-11 19:14:562205void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
2206 int message_num,
2207 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:292208 if (browser_tracker_->ContainsHandle(browser_handle)) {
2209 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562210 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:142211 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:292212 } else {
[email protected]71f65dd2009-02-11 19:14:562213 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:292214 }
2215}
2216
[email protected]71f65dd2009-02-11 19:14:562217void AutomationProvider::PrintNow(int tab_handle,
2218 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:442219#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:572220 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:342221 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2222 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:292223 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572224 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:142225 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:342226 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:572227 return;
initial.commit09911bf2008-07-26 23:55:292228 }
[email protected]71f65dd2009-02-11 19:14:562229 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
2230 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:442231#else
2232 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
2233 NOTIMPLEMENTED();
2234#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292235}
[email protected]d301c952009-07-13 15:02:412236
[email protected]71f65dd2009-02-11 19:14:562237void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:112238 const FilePath& file_name,
2239 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:562240 int type,
2241 bool* success) {
initial.commit09911bf2008-07-26 23:55:292242 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562243 *success = false;
initial.commit09911bf2008-07-26 23:55:292244 return;
2245 }
2246
2247 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2248 Browser* browser = FindAndActivateTab(nav);
2249 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142250 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562251 *success = false;
initial.commit09911bf2008-07-26 23:55:292252 return;
2253 }
2254
initial.commit09911bf2008-07-26 23:55:292255 SavePackage::SavePackageType save_type =
2256 static_cast<SavePackage::SavePackageType>(type);
2257 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2258 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:342259 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292260
[email protected]71f65dd2009-02-11 19:14:562261 *success = true;
initial.commit09911bf2008-07-26 23:55:292262}
2263
[email protected]71f65dd2009-02-11 19:14:562264void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2265 bool* success,
2266 std::wstring* text) {
2267 *success = false;
initial.commit09911bf2008-07-26 23:55:292268 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562269 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522270 GetText();
[email protected]71f65dd2009-02-11 19:14:562271 *success = true;
initial.commit09911bf2008-07-26 23:55:292272 }
initial.commit09911bf2008-07-26 23:55:292273}
2274
[email protected]71f65dd2009-02-11 19:14:562275void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2276 const std::wstring& text,
2277 bool* success) {
2278 *success = false;
initial.commit09911bf2008-07-26 23:55:292279 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522280 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2281 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562282 *success = true;
initial.commit09911bf2008-07-26 23:55:292283 }
initial.commit09911bf2008-07-26 23:55:292284}
2285
2286void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562287 int autocomplete_edit_handle,
2288 bool* success,
2289 std::vector<AutocompleteMatchData>* matches) {
2290 *success = false;
initial.commit09911bf2008-07-26 23:55:292291 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272292 const AutocompleteResult& result = autocomplete_edit_tracker_->
2293 GetResource(autocomplete_edit_handle)->model()->result();
2294 for (AutocompleteResult::const_iterator i = result.begin();
2295 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562296 matches->push_back(AutocompleteMatchData(*i));
2297 *success = true;
initial.commit09911bf2008-07-26 23:55:292298 }
initial.commit09911bf2008-07-26 23:55:292299}
2300
2301void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562302 int autocomplete_edit_handle,
2303 bool* success,
2304 bool* query_in_progress) {
2305 *success = false;
2306 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292307 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562308 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522309 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562310 *success = true;
initial.commit09911bf2008-07-26 23:55:292311 }
initial.commit09911bf2008-07-26 23:55:292312}
2313
[email protected]63514af2010-03-30 17:17:232314#if !defined(OS_MACOSX)
[email protected]28790922009-03-09 19:48:372315void AutomationProvider::OnMessageFromExternalHost(int handle,
2316 const std::string& message,
2317 const std::string& origin,
2318 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:192319 RenderViewHost* view_host = GetViewForTab(handle);
2320 if (!view_host) {
2321 return;
[email protected]fa83e762008-08-15 21:41:392322 }
[email protected]f7a68432009-07-29 23:18:192323
2324 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
2325 view_host, message, origin, target)) {
2326 // Message was diverted.
2327 return;
2328 }
2329
2330 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
2331 origin, target, this, view_host, handle)) {
2332 // Message was diverted.
2333 return;
2334 }
2335
2336 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
2337 // Message was diverted.
2338 return;
2339 }
2340
2341 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:392342}
[email protected]a9024892009-06-16 23:13:552343
2344bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
2345 const std::string& message, const std::string& origin,
2346 const std::string& target) {
2347 if (target !=
2348 extension_automation_constants::kAutomationBrowserEventRequestTarget)
2349 return false;
2350
2351 if (origin != extension_automation_constants::kAutomationOrigin) {
2352 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
2353 return false;
2354 }
2355
2356 // The message is a JSON-encoded array with two elements, both strings. The
2357 // first is the name of the event to dispatch. The second is a JSON-encoding
2358 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:202359 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:552360 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
2361 LOG(WARNING) << "Invalid browser event specified through automation";
2362 return false;
2363 }
2364
2365 const ListValue* args = static_cast<const ListValue*>(message_value.get());
2366
2367 std::string event_name;
2368 if (!args->GetString(0, &event_name)) {
2369 LOG(WARNING) << "No browser event name specified through automation";
2370 return false;
2371 }
2372
2373 std::string json_args;
2374 if (!args->GetString(1, &json_args)) {
2375 LOG(WARNING) << "No browser event args specified through automation";
2376 return false;
2377 }
2378
[email protected]7120f132009-07-20 21:05:372379 if (profile()->GetExtensionMessageService()) {
[email protected]db7331a2010-02-25 22:10:502380 profile()->GetExtensionMessageService()->DispatchEventToRenderers(
[email protected]a807bbe2010-04-14 10:51:192381 event_name, json_args, profile()->IsOffTheRecord(), GURL());
[email protected]7120f132009-07-20 21:05:372382 }
[email protected]a9024892009-06-16 23:13:552383
2384 return true;
2385}
[email protected]5ae5bed2009-08-21 18:52:442386#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:392387
[email protected]57c6a652009-05-04 07:58:342388TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572389 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572390 if (tab_tracker_->ContainsHandle(handle)) {
2391 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342392 if (tab)
2393 *tab = nav_controller;
2394 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572395 }
[email protected]57c6a652009-05-04 07:58:342396 return NULL;
[email protected]20e93d12008-08-28 16:31:572397}
2398
initial.commit09911bf2008-07-26 23:55:292399TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2400 : AutomationProvider(profile) {
2401 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142402 registrar_.Add(this, NotificationType::SESSION_END,
2403 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292404}
2405
2406TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292407 BrowserList::RemoveObserver(this);
2408}
2409
2410void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:142411 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:292412 AutomationProvider::OnChannelError();
2413}
2414
2415void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2416 // For backwards compatibility with the testing automation interface, we
2417 // want the automation provider (and hence the process) to go away when the
2418 // last browser goes away.
2419 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292420 // If you change this, update Observer for NotificationType::SESSION_END
2421 // below.
[email protected]295039bd2008-08-15 04:32:572422 MessageLoop::current()->PostTask(FROM_HERE,
2423 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292424 }
2425}
2426
2427void TestingAutomationProvider::Observe(NotificationType type,
2428 const NotificationSource& source,
2429 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562430 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292431 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2432 // before the task runs resulting in this object not being deleted. This
2433 // Release balance out the Release scheduled by OnBrowserRemoving.
2434 Release();
2435}
[email protected]295039bd2008-08-15 04:32:572436
2437void TestingAutomationProvider::OnRemoveProvider() {
2438 AutomationProviderList::GetInstance()->RemoveProvider(this);
2439}
[email protected]8a3422c92008-09-24 17:42:422440
[email protected]816633a2009-11-11 21:48:182441void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:562442 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422443 if (tab_tracker_->ContainsHandle(handle)) {
2444 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342445 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112446 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422447 }
[email protected]8a3422c92008-09-24 17:42:422448}
2449
[email protected]816633a2009-11-11 21:48:182450void AutomationProvider::ClickInfoBarAccept(int handle,
2451 int info_bar_index,
2452 bool wait_for_navigation,
2453 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422454 bool success = false;
2455 if (tab_tracker_->ContainsHandle(handle)) {
2456 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2457 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112458 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422459 if (info_bar_index >= 0 && info_bar_index < count) {
2460 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:012461 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:422462 }
[email protected]eb9ba192008-12-02 02:41:342463 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112464 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342465 info_bar_index);
2466 if (delegate->AsConfirmInfoBarDelegate())
2467 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422468 success = true;
2469 }
2470 }
[email protected]4f3dc372009-02-24 00:10:292471 }
[email protected]58f622a62009-10-04 01:17:552472
2473 // This "!wait_for_navigation || !success condition" logic looks suspicious.
2474 // It will send a failure message when success is true but
2475 // |wait_for_navigation| is false.
2476 // TODO(phajdan.jr): investgate whether the reply param (currently
2477 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:422478 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:182479 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:522480 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:422481}
2482
[email protected]71f65dd2009-02-11 19:14:562483void AutomationProvider::GetLastNavigationTime(int handle,
2484 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422485 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562486 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422487}
2488
[email protected]71f65dd2009-02-11 19:14:562489void AutomationProvider::WaitForNavigation(int handle,
2490 int64 last_navigation_time,
2491 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:252492 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:422493 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:252494
[email protected]8a3422c92008-09-24 17:42:422495 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562496 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:522497 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
2498 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:552499 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:292500 return;
[email protected]8a3422c92008-09-24 17:42:422501 }
2502
[email protected]7dad3d5f2010-03-04 00:27:012503 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:422504}
2505
[email protected]71f65dd2009-02-11 19:14:562506void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162507 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562508 int value,
2509 bool* success) {
2510 *success = false;
[email protected]8a3422c92008-09-24 17:42:422511 if (browser_tracker_->ContainsHandle(handle)) {
2512 Browser* browser = browser_tracker_->GetResource(handle);
2513 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562514 *success = true;
[email protected]8a3422c92008-09-24 17:42:422515 }
[email protected]8a3422c92008-09-24 17:42:422516}
[email protected]97fa6ce32008-12-19 01:48:162517
[email protected]71f65dd2009-02-11 19:14:562518void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162519 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562520 const std::wstring& value,
2521 bool* success) {
2522 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162523 if (browser_tracker_->ContainsHandle(handle)) {
2524 Browser* browser = browser_tracker_->GetResource(handle);
2525 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562526 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162527 }
[email protected]97fa6ce32008-12-19 01:48:162528}
2529
[email protected]71f65dd2009-02-11 19:14:562530void AutomationProvider::GetBooleanPreference(int handle,
2531 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:362532 bool* success,
2533 bool* value) {
[email protected]71f65dd2009-02-11 19:14:562534 *success = false;
2535 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162536 if (browser_tracker_->ContainsHandle(handle)) {
2537 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562538 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2539 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162540 }
[email protected]97fa6ce32008-12-19 01:48:162541}
2542
[email protected]71f65dd2009-02-11 19:14:562543void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162544 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562545 bool value,
2546 bool* success) {
2547 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162548 if (browser_tracker_->ContainsHandle(handle)) {
2549 Browser* browser = browser_tracker_->GetResource(handle);
2550 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562551 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162552 }
[email protected]97fa6ce32008-12-19 01:48:162553}
2554
2555// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562556void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:402557 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162558 if (tab_tracker_->ContainsHandle(tab_handle)) {
2559 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2560 Browser* browser = FindAndActivateTab(nav);
2561 DCHECK(browser);
2562
[email protected]57c6a652009-05-04 07:58:342563 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2564 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162565 }
[email protected]97fa6ce32008-12-19 01:48:162566}
2567
[email protected]b8f48d12009-11-09 20:14:362568// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562569void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402570 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562571 bool* success) {
2572 *success = false;
[email protected]de246f52009-02-25 18:25:452573#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:162574 if (tab_tracker_->ContainsHandle(tab_handle)) {
2575 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:192576 if (!nav)
2577 return;
[email protected]97fa6ce32008-12-19 01:48:162578 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:162579
[email protected]2f2afba2010-04-01 01:53:192580 // If the browser has UI, simulate what a user would do.
2581 // Activate the tab and then click the encoding menu.
2582 if (browser &&
2583 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162584 int selected_encoding_id =
2585 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2586 if (selected_encoding_id) {
2587 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562588 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162589 }
[email protected]2f2afba2010-04-01 01:53:192590 } else {
2591 // There is no UI, Chrome probably runs as Chrome-Frame mode.
2592 // Try to get TabContents and call its override_encoding method.
2593 TabContents* contents = nav->tab_contents();
2594 if (!contents)
2595 return;
2596 const std::string selected_encoding =
2597 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
2598 if (selected_encoding.empty())
2599 return;
2600 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:162601 }
2602 }
[email protected]de246f52009-02-25 18:25:452603#else
2604 // TODO(port): Enable when encoding-related parts of Browser are ported.
2605 NOTIMPLEMENTED();
2606#endif
[email protected]97fa6ce32008-12-19 01:48:162607}
[email protected]5bcdb312009-01-07 21:43:202608
[email protected]4d434a1a2009-02-11 21:06:572609void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202610 SavePackage::SetShouldPromptUser(should_prompt);
2611}
[email protected]87eab222009-03-13 00:47:452612
[email protected]66ba4932009-06-04 19:22:132613void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2614 *count = -1; // -1 is the error code
2615 if (tab_tracker_->ContainsHandle(handle)) {
2616 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2617 TabContents* tab_contents = nav_controller->tab_contents();
2618 if (tab_contents) {
2619 BlockedPopupContainer* container =
2620 tab_contents->blocked_popup_container();
2621 if (container) {
2622 *count = static_cast<int>(container->GetBlockedPopupCount());
2623 } else {
2624 // If we don't have a container, we don't have any blocked popups to
2625 // contain!
2626 *count = 0;
2627 }
2628 }
2629 }
2630}
[email protected]f7a68432009-07-29 23:18:192631
2632void AutomationProvider::SelectAll(int tab_handle) {
2633 RenderViewHost* view = GetViewForTab(tab_handle);
2634 if (!view) {
2635 NOTREACHED();
2636 return;
2637 }
2638
2639 view->SelectAll();
2640}
2641
2642void AutomationProvider::Cut(int tab_handle) {
2643 RenderViewHost* view = GetViewForTab(tab_handle);
2644 if (!view) {
2645 NOTREACHED();
2646 return;
2647 }
2648
2649 view->Cut();
2650}
2651
2652void AutomationProvider::Copy(int tab_handle) {
2653 RenderViewHost* view = GetViewForTab(tab_handle);
2654 if (!view) {
2655 NOTREACHED();
2656 return;
2657 }
2658
2659 view->Copy();
2660}
2661
2662void AutomationProvider::Paste(int tab_handle) {
2663 RenderViewHost* view = GetViewForTab(tab_handle);
2664 if (!view) {
2665 NOTREACHED();
2666 return;
2667 }
2668
2669 view->Paste();
2670}
2671
2672void AutomationProvider::ReloadAsync(int tab_handle) {
2673 if (tab_tracker_->ContainsHandle(tab_handle)) {
2674 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2675 if (!tab) {
2676 NOTREACHED();
2677 return;
2678 }
2679
[email protected]106a0812010-03-18 00:15:122680 const bool check_for_repost = true;
2681 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:192682 }
2683}
2684
2685void AutomationProvider::StopAsync(int tab_handle) {
2686 RenderViewHost* view = GetViewForTab(tab_handle);
2687 if (!view) {
[email protected]8b2b3312009-09-14 18:38:362688 // We tolerate StopAsync being called even before a view has been created.
2689 // So just log a warning instead of a NOTREACHED().
2690 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:192691 return;
2692 }
2693
2694 view->Stop();
2695}
2696
[email protected]1bb5f892009-10-06 01:44:572697void AutomationProvider::OnSetPageFontSize(int tab_handle,
2698 int font_size) {
2699 AutomationPageFontSize automation_font_size =
2700 static_cast<AutomationPageFontSize>(font_size);
2701
2702 if (automation_font_size < SMALLEST_FONT ||
2703 automation_font_size > LARGEST_FONT) {
2704 DLOG(ERROR) << "Invalid font size specified : "
2705 << font_size;
2706 return;
2707 }
2708
2709 if (tab_tracker_->ContainsHandle(tab_handle)) {
2710 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2711 DCHECK(tab != NULL);
2712 if (tab && tab->tab_contents()) {
2713 DCHECK(tab->tab_contents()->profile() != NULL);
2714 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2715 prefs::kWebKitDefaultFontSize, font_size);
2716 }
2717 }
2718}
2719
[email protected]bc73b4e52010-03-26 04:16:202720void AutomationProvider::RemoveBrowsingData(int remove_mask) {
2721 BrowsingDataRemover* remover;
2722 remover = new BrowsingDataRemover(profile(),
2723 BrowsingDataRemover::EVERYTHING, // All time periods.
2724 base::Time());
2725 remover->Remove(remove_mask);
2726 // BrowsingDataRemover deletes itself.
2727}
[email protected]1bb5f892009-10-06 01:44:572728
[email protected]2949e90d2009-08-21 15:32:522729void AutomationProvider::WaitForBrowserWindowCountToBecome(
2730 int target_count, IPC::Message* reply_message) {
2731 if (static_cast<int>(BrowserList::size()) == target_count) {
2732 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2733 reply_message, true);
2734 Send(reply_message);
2735 return;
2736 }
2737
2738 // Set up an observer (it will delete itself).
2739 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2740}
2741
2742void AutomationProvider::WaitForAppModalDialogToBeShown(
2743 IPC::Message* reply_message) {
2744 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2745 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2746 reply_message, true);
2747 Send(reply_message);
2748 return;
2749 }
2750
2751 // Set up an observer (it will delete itself).
2752 new AppModalDialogShownObserver(this, reply_message);
2753}
2754
[email protected]1126a1d32009-08-26 15:39:262755void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2756 int handle, int number_of_navigations, IPC::Message* reply_message) {
2757 if (tab_tracker_->ContainsHandle(handle)) {
2758 NavigationController* tab = tab_tracker_->GetResource(handle);
2759 Browser* browser = FindAndActivateTab(tab);
2760 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:012761 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2762 false);
[email protected]1126a1d32009-08-26 15:39:262763 browser->GoBack(CURRENT_TAB);
2764 return;
2765 }
2766 }
2767
2768 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2769 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2770 Send(reply_message);
2771}
2772
2773void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2774 int handle, int number_of_navigations, IPC::Message* reply_message) {
2775 if (tab_tracker_->ContainsHandle(handle)) {
2776 NavigationController* tab = tab_tracker_->GetResource(handle);
2777 Browser* browser = FindAndActivateTab(tab);
2778 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:012779 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2780 false);
[email protected]1126a1d32009-08-26 15:39:262781 browser->GoForward(CURRENT_TAB);
2782 return;
2783 }
2784 }
2785
2786 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2787 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2788 Send(reply_message);
2789}
2790
[email protected]f7a68432009-07-29 23:18:192791RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2792 if (tab_tracker_->ContainsHandle(tab_handle)) {
2793 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2794 if (!tab) {
2795 NOTREACHED();
2796 return NULL;
2797 }
2798
2799 TabContents* tab_contents = tab->tab_contents();
2800 if (!tab_contents) {
2801 NOTREACHED();
2802 return NULL;
2803 }
2804
2805 RenderViewHost* view_host = tab_contents->render_view_host();
2806 return view_host;
2807 }
2808
2809 return NULL;
2810}
[email protected]675595f2009-08-26 22:32:042811
2812void AutomationProvider::GetBrowserForWindow(int window_handle,
2813 bool* success,
2814 int* browser_handle) {
2815 *success = false;
2816 *browser_handle = 0;
2817
2818 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2819 if (!window)
2820 return;
2821
2822 BrowserList::const_iterator iter = BrowserList::begin();
2823 for (;iter != BrowserList::end(); ++iter) {
2824 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2825 if (window == this_window) {
2826 // Add() returns the existing handle for the resource if any.
2827 *browser_handle = browser_tracker_->Add(*iter);
2828 *success = true;
2829 return;
2830 }
2831 }
2832}
[email protected]d11c8e92009-10-20 23:26:402833
2834void AutomationProvider::InstallExtension(const FilePath& crx_path,
2835 IPC::Message* reply_message) {
2836 ExtensionsService* service = profile_->GetExtensionsService();
2837 if (service) {
2838 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:192839 new ExtensionInstallNotificationObserver(this,
2840 AutomationMsg_InstallExtension::ID,
2841 reply_message);
[email protected]d11c8e92009-10-20 23:26:402842
2843 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:162844 scoped_refptr<CrxInstaller> installer(
2845 new CrxInstaller(install_dir,
2846 service,
2847 NULL)); // silent install, no UI
2848 installer->set_allow_privilege_increase(true);
2849 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:402850 } else {
2851 AutomationMsg_InstallExtension::WriteReplyParams(
2852 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2853 Send(reply_message);
2854 }
2855}
2856
2857void AutomationProvider::LoadExpandedExtension(
2858 const FilePath& extension_dir,
2859 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:382860 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:402861 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:192862 new ExtensionInstallNotificationObserver(
2863 this,
2864 AutomationMsg_LoadExpandedExtension::ID,
2865 reply_message);
[email protected]d11c8e92009-10-20 23:26:402866
2867 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:402868 } else {
2869 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
2870 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2871 Send(reply_message);
2872 }
2873}
[email protected]673fd2c02010-02-04 23:10:002874
[email protected]a1e62d12010-03-16 02:18:432875void AutomationProvider::GetEnabledExtensions(
2876 std::vector<FilePath>* result) {
2877 ExtensionsService* service = profile_->GetExtensionsService();
2878 DCHECK(service);
2879 if (service->extensions_enabled()) {
2880 const ExtensionList* extensions = service->extensions();
2881 DCHECK(extensions);
2882 for (size_t i = 0; i < extensions->size(); ++i) {
2883 Extension* extension = (*extensions)[i];
2884 DCHECK(extension);
[email protected]237f281672010-03-20 12:37:072885 if (extension->location() == Extension::INTERNAL) {
2886 result->push_back(extension->path());
2887 }
[email protected]a1e62d12010-03-16 02:18:432888 }
2889 }
2890}
2891
[email protected]790788ac2010-04-06 17:52:192892void AutomationProvider::WaitForExtensionTestResult(
2893 IPC::Message* reply_message) {
2894 DCHECK(reply_message_ == NULL);
2895 reply_message_ = reply_message;
2896 // Call MaybeSendResult, because the result might have come in before
2897 // we were waiting on it.
2898 extension_test_result_observer_->MaybeSendResult();
2899}
2900
2901void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:092902 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:192903 ExtensionsService* service = profile_->GetExtensionsService();
2904 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
2905 if (service && manager) {
2906 // The observer will delete itself when done.
2907 new ExtensionReadyNotificationObserver(
2908 manager,
2909 this,
2910 AutomationMsg_InstallExtensionAndGetHandle::ID,
2911 reply_message);
2912
[email protected]d7e5525d2010-04-20 14:37:092913 ExtensionInstallUI* client =
2914 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:192915 scoped_refptr<CrxInstaller> installer(
2916 new CrxInstaller(service->install_directory(),
2917 service,
[email protected]d7e5525d2010-04-20 14:37:092918 client));
[email protected]790788ac2010-04-06 17:52:192919 installer->set_allow_privilege_increase(true);
2920 installer->InstallCrx(crx_path);
2921 } else {
2922 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
2923 reply_message, 0);
2924 Send(reply_message);
2925 }
2926}
2927
2928void AutomationProvider::UninstallExtension(int extension_handle,
2929 bool* success) {
2930 *success = false;
2931 Extension* extension = GetExtension(extension_handle);
2932 ExtensionsService* service = profile_->GetExtensionsService();
2933 if (extension && service) {
2934 ExtensionUnloadNotificationObserver observer;
2935 service->UninstallExtension(extension->id(), false);
2936 // The extension unload notification should have been sent synchronously
2937 // with the uninstall. Just to be safe, check that it was received.
2938 *success = observer.did_receive_unload_notification();
2939 }
2940}
2941
2942void AutomationProvider::EnableExtension(int extension_handle,
2943 IPC::Message* reply_message) {
2944 Extension* extension = GetDisabledExtension(extension_handle);
2945 ExtensionsService* service = profile_->GetExtensionsService();
2946 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
2947 // Only enable if this extension is disabled.
2948 if (extension && service && manager) {
2949 // The observer will delete itself when done.
2950 new ExtensionReadyNotificationObserver(
2951 manager,
2952 this,
2953 AutomationMsg_EnableExtension::ID,
2954 reply_message);
2955 service->EnableExtension(extension->id());
2956 } else {
2957 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
2958 Send(reply_message);
2959 }
2960}
2961
2962void AutomationProvider::DisableExtension(int extension_handle,
2963 bool* success) {
2964 *success = false;
2965 Extension* extension = GetEnabledExtension(extension_handle);
2966 ExtensionsService* service = profile_->GetExtensionsService();
2967 if (extension && service) {
2968 ExtensionUnloadNotificationObserver observer;
2969 service->DisableExtension(extension->id());
2970 // The extension unload notification should have been sent synchronously
2971 // with the disable. Just to be safe, check that it was received.
2972 *success = observer.did_receive_unload_notification();
2973 }
2974}
2975
2976void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
2977 int extension_handle, int browser_handle,
2978 IPC::Message* reply_message) {
2979 bool success = false;
2980 Extension* extension = GetEnabledExtension(extension_handle);
2981 ExtensionsService* service = profile_->GetExtensionsService();
2982 ExtensionMessageService* message_service =
2983 profile_->GetExtensionMessageService();
2984 Browser* browser = browser_tracker_->GetResource(browser_handle);
2985 if (extension && service && message_service && browser) {
2986 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
2987 if (extension->page_action()) {
2988 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
2989 browser->profile(), extension->id(), "action", tab_id, "", 1);
2990 success = true;
2991 } else if (extension->browser_action()) {
2992 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
2993 browser->profile(), extension->id(), browser);
2994 success = true;
2995 }
2996 }
2997 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
2998 reply_message, success);
2999 Send(reply_message);
3000}
3001
3002void AutomationProvider::MoveExtensionBrowserAction(
3003 int extension_handle, int index, bool* success) {
3004 *success = false;
3005 Extension* extension = GetEnabledExtension(extension_handle);
3006 ExtensionsService* service = profile_->GetExtensionsService();
3007 if (extension && service) {
3008 ExtensionToolbarModel* toolbar = service->toolbar_model();
3009 if (toolbar) {
3010 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3011 toolbar->MoveBrowserAction(extension, index);
3012 *success = true;
3013 } else {
3014 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3015 }
3016 }
3017 }
3018}
3019
3020void AutomationProvider::GetExtensionProperty(
3021 int extension_handle,
3022 AutomationMsg_ExtensionProperty type,
3023 bool* success,
3024 std::string* value) {
3025 *success = false;
3026 Extension* extension = GetExtension(extension_handle);
3027 ExtensionsService* service = profile_->GetExtensionsService();
3028 if (extension && service) {
3029 ExtensionToolbarModel* toolbar = service->toolbar_model();
3030 int found_index = -1;
3031 int index = 0;
3032 switch (type) {
3033 case AUTOMATION_MSG_EXTENSION_ID:
3034 *value = extension->id();
3035 *success = true;
3036 break;
3037 case AUTOMATION_MSG_EXTENSION_NAME:
3038 *value = extension->name();
3039 *success = true;
3040 break;
3041 case AUTOMATION_MSG_EXTENSION_VERSION:
3042 *value = extension->VersionString();
3043 *success = true;
3044 break;
3045 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3046 if (toolbar) {
3047 for (ExtensionList::const_iterator iter = toolbar->begin();
3048 iter != toolbar->end(); iter++) {
3049 // Skip this extension if we are in incognito mode
3050 // and it is not incognito-enabled.
3051 if (profile_->IsOffTheRecord() &&
3052 !service->IsIncognitoEnabled(*iter))
3053 continue;
3054 if (*iter == extension) {
3055 found_index = index;
3056 break;
3057 }
3058 index++;
3059 }
3060 *value = IntToString(found_index);
3061 *success = true;
3062 }
3063 break;
3064 default:
3065 LOG(WARNING) << "Trying to get undefined extension property";
3066 break;
3067 }
3068 }
3069}
3070
[email protected]673fd2c02010-02-04 23:10:003071void AutomationProvider::SaveAsAsync(int tab_handle) {
3072 NavigationController* tab = NULL;
3073 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3074 if (tab_contents)
3075 tab_contents->OnSavePage();
3076}
[email protected]7dad3d5f2010-03-04 00:27:013077
3078void AutomationProvider::SetContentSetting(
3079 int handle,
3080 const std::string& host,
3081 ContentSettingsType content_type,
3082 ContentSetting setting,
3083 bool* success) {
3084 *success = false;
3085 if (browser_tracker_->ContainsHandle(handle)) {
3086 Browser* browser = browser_tracker_->GetResource(handle);
3087 HostContentSettingsMap* map =
3088 browser->profile()->GetHostContentSettingsMap();
3089 if (host.empty()) {
3090 map->SetDefaultContentSetting(content_type, setting);
3091 } else {
[email protected]0314ae02010-04-08 09:18:293092 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3093 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013094 }
3095 *success = true;
3096 }
3097}
[email protected]cc824372010-03-31 15:33:013098
3099#if !defined(TOOLKIT_VIEWS)
3100void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3101 NOTIMPLEMENTED();
3102};
3103
3104void AutomationProvider::WaitForFocusedViewIDToChange(
3105 int handle, int previous_view_id, IPC::Message* reply_message) {
3106 NOTIMPLEMENTED();
3107}
3108
3109void AutomationProvider::StartTrackingPopupMenus(
3110 int browser_handle, bool* success) {
3111 NOTIMPLEMENTED();
3112}
3113
3114void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3115 NOTIMPLEMENTED();
3116}
3117#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:093118
3119void AutomationProvider::ResetToDefaultTheme() {
3120 profile_->ClearTheme();
3121}