blob: 89a8ec9d7e7e8211788e8ac7fcc2eaca8804d52d [file] [log] [blame]
[email protected]5ae5bed2009-08-21 18:52:441// Copyright (c) 2009 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]c6cb1992009-04-13 16:45:2911#include "base/file_version_info.h"
[email protected]a9024892009-06-16 23:13:5512#include "base/json_reader.h"
[email protected]bc1407f2009-09-29 00:33:3513#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3814#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2915#include "base/path_service.h"
[email protected]f44265b2009-05-19 18:52:5016#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2717#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3818#include "base/thread.h"
[email protected]a7eee32f2009-05-22 18:08:1719#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2920#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2821#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0922#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3323#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0825#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5526#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5027#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1328#include "chrome/browser/blocked_popup_container.h"
[email protected]5c238752009-06-13 10:29:0729#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3930#include "chrome/browser/browser_window.h"
initial.commit09911bf2008-07-26 23:55:2931#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1232#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5933#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2234#include "chrome/browser/download/download_shelf.h"
[email protected]a9024892009-06-16 23:13:5535#include "chrome/browser/extensions/extension_message_service.h"
[email protected]4801ecc2009-04-05 04:52:5836#include "chrome/browser/find_bar.h"
37#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2938#include "chrome/browser/find_notification_details.h"
[email protected]13869dd2009-05-05 00:40:0639#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0140#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5341#include "chrome/browser/net/url_request_mock_util.h"
[email protected]a7eee32f2009-05-22 18:08:1742#include "chrome/browser/profile_manager.h"
[email protected]6524b5f92009-01-22 17:48:2543#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0344#include "chrome/browser/ssl/ssl_manager.h"
45#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3446#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4547#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1748#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2949#include "chrome/common/chrome_paths.h"
[email protected]a7eee32f2009-05-22 18:08:1750#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1451#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4752#include "chrome/common/platform_util.h"
[email protected]1bb5f892009-10-06 01:44:5753#include "chrome/common/pref_names.h"
[email protected]8a3422c92008-09-24 17:42:4254#include "chrome/common/pref_service.h"
[email protected]71f65dd2009-02-11 19:14:5655#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5756#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1757#include "net/proxy/proxy_service.h"
58#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2159#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2860#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2961
[email protected]de246f52009-02-25 18:25:4562#if defined(OS_WIN)
63// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4564#include "chrome/browser/character_encoding.h"
65#include "chrome/browser/download/save_package.h"
66#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4567#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4568#endif // defined(OS_WIN)
69
[email protected]5ae5bed2009-08-21 18:52:4470#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2871// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2872#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4473#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0674
[email protected]e1acf6f2008-10-27 20:43:3375using base::Time;
76
[email protected]cbab76d2008-10-13 22:42:4777class AutomationInterstitialPage : public InterstitialPage {
78 public:
[email protected]57c6a652009-05-04 07:58:3479 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4780 const GURL& url,
81 const std::string& contents)
82 : InterstitialPage(tab, true, url),
83 contents_(contents) {
84 }
85
86 virtual std::string GetHTMLContents() { return contents_; }
87
88 private:
89 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:2990
[email protected]cbab76d2008-10-13 22:42:4791 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
92};
93
[email protected]c2cb8542009-08-20 21:16:5194#if !defined(OS_MACOSX)
95class ClickTask : public Task {
96 public:
[email protected]fc2e0872009-08-21 22:14:4197 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:5198 virtual ~ClickTask() {}
99
100 virtual void Run() {
101 ui_controls::MouseButton button = ui_controls::LEFT;
102 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
103 views::Event::EF_LEFT_BUTTON_DOWN) {
104 button = ui_controls::LEFT;
105 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
106 views::Event::EF_RIGHT_BUTTON_DOWN) {
107 button = ui_controls::RIGHT;
108 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
109 views::Event::EF_MIDDLE_BUTTON_DOWN) {
110 button = ui_controls::MIDDLE;
111 } else {
112 NOTREACHED();
113 }
114
[email protected]fc2e0872009-08-21 22:14:41115 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51116 }
117
118 private:
[email protected]c2cb8542009-08-20 21:16:51119 int flags_;
120
121 DISALLOW_COPY_AND_ASSIGN(ClickTask);
122};
123#endif
124
initial.commit09911bf2008-07-26 23:55:29125AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57126 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56127 profile_(profile),
128 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29129 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29130 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20131 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29132 autocomplete_edit_tracker_.reset(
133 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29134 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
135 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44136 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
initial.commit09911bf2008-07-26 23:55:29137}
138
139AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50140 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
141 port_containers_.end());
142 port_containers_.clear();
143
[email protected]0da050b92008-08-19 19:29:47144 // Make sure that any outstanding NotificationObservers also get destroyed.
145 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31146 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47147 while ((observer = it.GetNext()) != NULL)
148 delete observer;
initial.commit09911bf2008-07-26 23:55:29149}
150
[email protected]9a3a293b2009-06-04 22:28:16151void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06152 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57153 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06154 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
155 automation_resource_message_filter_,
156 g_browser_process->io_thread()->message_loop(),
157 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05158 scoped_ptr<FileVersionInfo> file_version_info(
159 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40160 std::string version_string;
161 if (file_version_info != NULL) {
162 version_string = WideToASCII(file_version_info->file_version());
163 }
[email protected]c6cb1992009-04-13 16:45:29164
165 // Send a hello message with our current automation protocol version.
166 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29167}
168
169void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
170 if (expected_tabs == 0) {
171 Send(new AutomationMsg_InitialLoadsComplete(0));
172 } else {
173 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
174 }
175}
176
initial.commit09911bf2008-07-26 23:55:29177NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58178 NavigationController* tab, IPC::Message* reply_message,
179 int number_of_navigations) {
initial.commit09911bf2008-07-26 23:55:29180 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58181 new NavigationNotificationObserver(tab, this, reply_message,
182 number_of_navigations);
initial.commit09911bf2008-07-26 23:55:29183
[email protected]71f65dd2009-02-11 19:14:56184 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29185 return observer;
186}
187
188void AutomationProvider::RemoveNavigationStatusListener(
189 NotificationObserver* obs) {
190 notification_observer_list_.RemoveObserver(obs);
191}
192
193NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14194 Browser* parent,
195 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56196 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14197 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29198 notification_observer_list_.AddObserver(observer);
199
200 return observer;
201}
202
203void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
204 notification_observer_list_.RemoveObserver(obs);
205}
206
207void AutomationProvider::AddLoginHandler(NavigationController* tab,
208 LoginHandler* handler) {
209 login_handler_map_[tab] = handler;
210}
211
212void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
213 DCHECK(login_handler_map_[tab]);
214 login_handler_map_.erase(tab);
215}
216
[email protected]f44265b2009-05-19 18:52:50217void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
218 int port_id = port->port_id();
219 DCHECK_NE(-1, port_id);
220 DCHECK(port_containers_.find(port_id) == port_containers_.end());
221
222 port_containers_[port_id] = port;
223}
224
225void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
226 int port_id = port->port_id();
227 DCHECK_NE(-1, port_id);
228
229 PortContainerMap::iterator it = port_containers_.find(port_id);
230 DCHECK(it != port_containers_.end());
231
232 if (it != port_containers_.end()) {
233 delete it->second;
234 port_containers_.erase(it);
235 }
236}
237
238ExtensionPortContainer* AutomationProvider::GetPortContainer(
239 int port_id) const {
240 PortContainerMap::const_iterator it = port_containers_.find(port_id);
241 if (it == port_containers_.end())
242 return NULL;
243
244 return it->second;
245}
246
initial.commit09911bf2008-07-26 23:55:29247int AutomationProvider::GetIndexForNavigationController(
248 const NavigationController* controller, const Browser* parent) const {
249 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12250 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29251}
252
253void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
254 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14255 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56256 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
257 CloseBrowserAsync)
258 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
259 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
260 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
261 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
262 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
263 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14264 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58265 IPC_MESSAGE_HANDLER_DELAY_REPLY(
266 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
267 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56268 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
269 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
270 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
271 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
272 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14273 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56274 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
275 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
276 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14277 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29278 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
279 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56280 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36281 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56282 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29283 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56284 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29285 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
286 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56287 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14288 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24289 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44290 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33291 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44292 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33293 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24294 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14295 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24296 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15297 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14298 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44299#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24300 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]1c58a5c2009-05-21 18:47:14301 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]5ae5bed2009-08-21 18:52:44302#endif // !defined(OS_MACOSX)
[email protected]8dd404bb2009-09-22 19:57:24303#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]71f65dd2009-02-11 19:14:56304 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
305 WindowSimulateDrag)
[email protected]8dd404bb2009-09-22 19:57:24306#endif // defined(OS_WIN) || defined(OS_LINUX)
[email protected]71f65dd2009-02-11 19:14:56307 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
308 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37309#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56310 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45311#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56312 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
313 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03314 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56315 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14316 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
initial.commit09911bf2008-07-26 23:55:29317 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14318 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56319 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
320 ExecuteJavascript)
321 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29322 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14323 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
324 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56325 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
326 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14327 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17328 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14329 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14330 OpenNewBrowserWindow)
331 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56332 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14333 GetAutocompleteEditForBrowser)
334 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45335#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29336 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17337#endif
[email protected]71f65dd2009-02-11 19:14:56338 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29339 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26340 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
341 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56342 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14343 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56344 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14345 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17346#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29347 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
348 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17349#endif
[email protected]71f65dd2009-02-11 19:14:56350 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
351 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14352 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17353#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04354 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39355 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
356 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17357#endif
[email protected]1c58a5c2009-05-21 18:47:14358 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
359 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44360 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
361 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56362 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
363 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29364 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
365 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
366 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56367 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41368 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56369 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
370 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29371 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56372 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29373 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56374 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29375 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56376 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29377 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56378 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45379 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45380 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
381 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14382 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56383 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57384 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56385 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57386 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56387 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
388 GetBookmarkBarVisibility)
[email protected]1c58a5c2009-05-21 18:47:14389 IPC_MESSAGE_HANDLER(AutomationMsg_GetSSLInfoBarCount, GetSSLInfoBarCount)
[email protected]71f65dd2009-02-11 19:14:56390 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickSSLInfoBarLink,
391 ClickSSLInfoBarLink)
392 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42393 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56394 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
395 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14396 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56397 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44398 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56399 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20400 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14401 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56402 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16403 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56404 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16405 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56406 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16407 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14408 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20409 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
410 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14411 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]b83e4602009-05-15 22:58:33412 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
413 SetEnableExtensionAutomation)
[email protected]59560e0b2009-06-04 03:30:22414 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13415 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19416 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
417 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
418 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
419 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
420 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
421 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52422 IPC_MESSAGE_HANDLER_DELAY_REPLY(
423 AutomationMsg_WaitForBrowserWindowCountToBecome,
424 WaitForBrowserWindowCountToBecome)
425 IPC_MESSAGE_HANDLER_DELAY_REPLY(
426 AutomationMsg_WaitForAppModalDialogToBeShown,
427 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26428 IPC_MESSAGE_HANDLER_DELAY_REPLY(
429 AutomationMsg_GoBackBlockUntilNavigationsComplete,
430 GoBackBlockUntilNavigationsComplete)
431 IPC_MESSAGE_HANDLER_DELAY_REPLY(
432 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
433 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14434#if defined(OS_WIN)
435 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
436#endif
[email protected]1bb5f892009-10-06 01:44:57437 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
initial.commit09911bf2008-07-26 23:55:29438 IPC_END_MESSAGE_MAP()
439}
440
[email protected]71f65dd2009-02-11 19:14:56441void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
442 *status = -1;
initial.commit09911bf2008-07-26 23:55:29443 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
444 Browser* browser = browser_tracker_->GetResource(handle);
445 if (at_index >= 0 && at_index < browser->tab_count()) {
446 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56447 *status = 0;
initial.commit09911bf2008-07-26 23:55:29448 }
449 }
initial.commit09911bf2008-07-26 23:55:29450}
451
[email protected]71f65dd2009-02-11 19:14:56452void AutomationProvider::AppendTab(int handle, const GURL& url,
453 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29454 int append_tab_response = -1; // -1 is the error code
455 NotificationObserver* observer = NULL;
456
457 if (browser_tracker_->ContainsHandle(handle)) {
458 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14459 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58460 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
461 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39462 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29463 if (tab_contents) {
464 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57465 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29466 }
467 }
468
469 if (append_tab_response < 0) {
470 // The append tab failed. Remove the TabStripObserver
471 if (observer) {
472 RemoveTabStripObserver(observer);
473 delete observer;
474 }
475
[email protected]71f65dd2009-02-11 19:14:56476 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
477 append_tab_response);
478 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29479 }
480}
481
[email protected]71f65dd2009-02-11 19:14:56482void AutomationProvider::NavigateToURL(int handle, const GURL& url,
483 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58484 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
485}
486
487void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
488 int handle, const GURL& url, int number_of_navigations,
489 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29490 if (tab_tracker_->ContainsHandle(handle)) {
491 NavigationController* tab = tab_tracker_->GetResource(handle);
492
493 // Simulate what a user would do. Activate the tab and then navigate.
494 // We could allow navigating in a background tab in future.
495 Browser* browser = FindAndActivateTab(tab);
496
497 if (browser) {
[email protected]2e028a082009-08-19 20:32:58498 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
[email protected]71f65dd2009-02-11 19:14:56499
initial.commit09911bf2008-07-26 23:55:29500 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50501 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29502 return;
503 }
504 }
[email protected]71f65dd2009-02-11 19:14:56505
506 AutomationMsg_NavigateToURL::WriteReplyParams(
507 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
508 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29509}
[email protected]2949e90d2009-08-21 15:32:52510
[email protected]71f65dd2009-02-11 19:14:56511void AutomationProvider::NavigationAsync(int handle, const GURL& url,
512 bool* status) {
513 *status = false;
initial.commit09911bf2008-07-26 23:55:29514
515 if (tab_tracker_->ContainsHandle(handle)) {
516 NavigationController* tab = tab_tracker_->GetResource(handle);
517
518 // Simulate what a user would do. Activate the tab and then navigate.
519 // We could allow navigating in a background tab in future.
520 Browser* browser = FindAndActivateTab(tab);
521
522 if (browser) {
523 // Don't add any listener unless a callback mechanism is desired.
524 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50525 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56526 *status = true;
initial.commit09911bf2008-07-26 23:55:29527 }
528 }
initial.commit09911bf2008-07-26 23:55:29529}
530
[email protected]71f65dd2009-02-11 19:14:56531void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29532 if (tab_tracker_->ContainsHandle(handle)) {
533 NavigationController* tab = tab_tracker_->GetResource(handle);
534 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14535 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]2e028a082009-08-19 20:32:58536 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29537 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29538 return;
539 }
540 }
[email protected]71f65dd2009-02-11 19:14:56541
542 AutomationMsg_GoBack::WriteReplyParams(
543 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
544 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29545}
546
[email protected]71f65dd2009-02-11 19:14:56547void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29548 if (tab_tracker_->ContainsHandle(handle)) {
549 NavigationController* tab = tab_tracker_->GetResource(handle);
550 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14551 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]2e028a082009-08-19 20:32:58552 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29553 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29554 return;
555 }
556 }
[email protected]71f65dd2009-02-11 19:14:56557
558 AutomationMsg_GoForward::WriteReplyParams(
559 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
560 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29561}
562
[email protected]71f65dd2009-02-11 19:14:56563void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29564 if (tab_tracker_->ContainsHandle(handle)) {
565 NavigationController* tab = tab_tracker_->GetResource(handle);
566 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14567 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]2e028a082009-08-19 20:32:58568 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29569 browser->Reload();
570 return;
571 }
572 }
[email protected]71f65dd2009-02-11 19:14:56573
574 AutomationMsg_Reload::WriteReplyParams(
575 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
576 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29577}
578
[email protected]71f65dd2009-02-11 19:14:56579void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29580 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56581 const std::wstring& password,
582 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29583 if (tab_tracker_->ContainsHandle(tab_handle)) {
584 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
585 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
586
587 if (iter != login_handler_map_.end()) {
588 // If auth is needed again after this, assume login has failed. This is
589 // not strictly correct, because a navigation can require both proxy and
590 // server auth, but it should be OK for now.
591 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58592 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29593 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52594 return;
initial.commit09911bf2008-07-26 23:55:29595 }
596 }
[email protected]de246f52009-02-25 18:25:45597
[email protected]457f5cf2009-08-18 16:37:52598 AutomationMsg_SetAuth::WriteReplyParams(
599 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
600 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29601}
602
[email protected]71f65dd2009-02-11 19:14:56603void AutomationProvider::CancelAuth(int tab_handle,
604 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29605 if (tab_tracker_->ContainsHandle(tab_handle)) {
606 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
607 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
608
609 if (iter != login_handler_map_.end()) {
610 // If auth is needed again after this, something is screwy.
611 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58612 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29613 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52614 return;
initial.commit09911bf2008-07-26 23:55:29615 }
616 }
[email protected]de246f52009-02-25 18:25:45617
[email protected]457f5cf2009-08-18 16:37:52618 AutomationMsg_CancelAuth::WriteReplyParams(
619 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
620 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29621}
622
[email protected]71f65dd2009-02-11 19:14:56623void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
624 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29625
626 if (tab_tracker_->ContainsHandle(tab_handle)) {
627 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
628 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
629
630 if (iter != login_handler_map_.end()) {
631 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56632 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29633 }
634 }
initial.commit09911bf2008-07-26 23:55:29635}
636
[email protected]71f65dd2009-02-11 19:14:56637void AutomationProvider::GetRedirectsFrom(int tab_handle,
638 const GURL& source_url,
639 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29640 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
641 if (tab_tracker_->ContainsHandle(tab_handle)) {
642 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
643 HistoryService* history_service =
644 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
645
646 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
647 "has no history service";
648 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56649 DCHECK(reply_message_ == NULL);
650 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29651 // Schedule a history query for redirects. The response will be sent
652 // asynchronously from the callback the history system uses to notify us
653 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29654 redirect_query_ = history_service->QueryRedirectsFrom(
655 source_url, &consumer_,
656 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
657 return; // Response will be sent when query completes.
658 }
659 }
660
661 // Send failure response.
[email protected]deb57402009-02-06 01:35:30662 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56663 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
664 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29665}
666
[email protected]71f65dd2009-02-11 19:14:56667void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
668 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29669 if (browser_tracker_->ContainsHandle(handle)) {
670 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56671 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29672 }
initial.commit09911bf2008-07-26 23:55:29673}
674
[email protected]202e7a72009-06-15 03:48:36675void AutomationProvider::GetBrowserLocale(string16* locale) {
676 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06677 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36678}
679
[email protected]71f65dd2009-02-11 19:14:56680void AutomationProvider::GetBrowserWindowCount(int* window_count) {
681 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29682}
683
[email protected]24497032009-05-01 17:00:29684void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
685 *window_count = static_cast<int>(
686 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
687}
688
[email protected]71f65dd2009-02-11 19:14:56689void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
690 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07691 AppModalDialog* dialog_delegate =
692 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50693 *showing_dialog = (dialog_delegate != NULL);
694 if (*showing_dialog)
695 *dialog_button = dialog_delegate->GetDialogButtons();
696 else
[email protected]478ff2ed2009-04-21 23:49:18697 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20698}
699
[email protected]71f65dd2009-02-11 19:14:56700void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
701 *success = false;
[email protected]fad84eab2008-12-05 00:37:20702
[email protected]1f460072009-05-28 17:02:07703 AppModalDialog* dialog_delegate =
704 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50705 if (dialog_delegate &&
706 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18707 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
708 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28709 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56710 *success = true;
[email protected]fad84eab2008-12-05 00:37:20711 }
[email protected]478ff2ed2009-04-21 23:49:18712 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
713 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56714 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28715 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56716 *success = true;
[email protected]fad84eab2008-12-05 00:37:20717 }
718 }
[email protected]c274acc2008-11-11 20:13:44719}
720
[email protected]71f65dd2009-02-11 19:14:56721void AutomationProvider::GetBrowserWindow(int index, int* handle) {
722 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29723 if (index >= 0) {
724 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29725 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29726 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56727 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29728 }
729 }
initial.commit09911bf2008-07-26 23:55:29730}
731
[email protected]24497032009-05-01 17:00:29732void AutomationProvider::FindNormalBrowserWindow(int* handle) {
733 *handle = 0;
734 Browser* browser = BrowserList::FindBrowserWithType(profile_,
735 Browser::TYPE_NORMAL);
736 if (browser)
737 *handle = browser_tracker_->Add(browser);
738}
739
[email protected]71f65dd2009-02-11 19:14:56740void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
741 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29742 Browser* browser = BrowserList::GetLastActive();
743 if (browser)
[email protected]71f65dd2009-02-11 19:14:56744 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29745}
746
[email protected]fb05865f2009-09-21 15:05:46747#if defined(OS_LINUX) || defined(OS_MACOSX)
[email protected]9a08bcf2009-08-12 19:56:28748// TODO(estade): use this implementation for all platforms?
749void AutomationProvider::GetActiveWindow(int* handle) {
750 gfx::NativeWindow window =
751 BrowserList::GetLastActive()->window()->GetNativeHandle();
752 *handle = window_tracker_->Add(window);
753}
754#endif
755
[email protected]4f6381ee2009-04-16 02:46:33756void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
757 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56758 *success = false;
[email protected]4ae62752008-08-04 23:28:47759 if (browser_tracker_->ContainsHandle(handle)) {
760 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14761 if (browser->command_updater()->SupportsCommand(command) &&
762 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47763 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56764 *success = true;
[email protected]4ae62752008-08-04 23:28:47765 }
766 }
[email protected]4ae62752008-08-04 23:28:47767}
768
[email protected]4f6381ee2009-04-16 02:46:33769void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56770 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53771 // List of commands which just finish synchronously and don't require
772 // setting up an observer.
773 static const int kSynchronousCommands[] = {
774 IDC_HOME,
775 IDC_SELECT_NEXT_TAB,
776 IDC_SELECT_PREVIOUS_TAB,
777 IDC_SHOW_DOWNLOADS,
778 IDC_SHOW_HISTORY,
779 };
[email protected]56e71b7c2009-03-27 03:05:56780 if (browser_tracker_->ContainsHandle(handle)) {
781 Browser* browser = browser_tracker_->GetResource(handle);
782 if (browser->command_updater()->SupportsCommand(command) &&
783 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53784 // First check if we can handle the command without using an observer.
785 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
786 if (command == kSynchronousCommands[i]) {
787 browser->ExecuteCommand(command);
788 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
789 true);
790 Send(reply_message);
791 return;
792 }
793 }
794
795 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42796 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
797 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27798 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42799 return;
800 }
[email protected]56e71b7c2009-03-27 03:05:56801 }
802 }
[email protected]49a14a82009-03-31 04:16:44803 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56804 Send(reply_message);
805}
806
[email protected]fc2e0872009-08-21 22:14:41807// This task just adds another task to the event queue. This is useful if
808// you want to ensure that any tasks added to the event queue after this one
809// have already been processed by the time |task| is run.
810class InvokeTaskLaterTask : public Task {
811 public:
812 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
813 virtual ~InvokeTaskLaterTask() {}
814
815 virtual void Run() {
816 MessageLoop::current()->PostTask(FROM_HERE, task_);
817 }
818
819 private:
820 Task* task_;
821
822 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
823};
824
[email protected]fc2e0872009-08-21 22:14:41825#if defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29826void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
827 int handle,
[email protected]d1a5941e2009-08-13 23:34:24828 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29829 int flags) {
[email protected]b410bc32009-08-14 01:11:14830 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51831 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41832 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29833 }
834}
835
initial.commit09911bf2008-07-26 23:55:29836void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
837 int handle,
[email protected]bc1407f2009-09-29 00:33:35838 int key,
initial.commit09911bf2008-07-26 23:55:29839 int flags) {
[email protected]b410bc32009-08-14 01:11:14840 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29841 return;
842
[email protected]b410bc32009-08-14 01:11:14843 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29844 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35845 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38846 ((flags & views::Event::EF_CONTROL_DOWN) ==
847 views::Event::EF_CONTROL_DOWN),
848 ((flags & views::Event::EF_SHIFT_DOWN) ==
849 views::Event::EF_SHIFT_DOWN),
850 ((flags & views::Event::EF_ALT_DOWN) ==
851 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29852}
[email protected]fc2e0872009-08-21 22:14:41853#endif // defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29854
[email protected]71f65dd2009-02-11 19:14:56855void AutomationProvider::IsWindowActive(int handle, bool* success,
856 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29857 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17858 *is_active =
859 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56860 *success = true;
initial.commit09911bf2008-07-26 23:55:29861 } else {
[email protected]71f65dd2009-02-11 19:14:56862 *success = false;
863 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29864 }
865}
866
[email protected]71f65dd2009-02-11 19:14:56867void AutomationProvider::GetTabCount(int handle, int* tab_count) {
868 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29869
870 if (browser_tracker_->ContainsHandle(handle)) {
871 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56872 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29873 }
initial.commit09911bf2008-07-26 23:55:29874}
875
[email protected]71f65dd2009-02-11 19:14:56876void AutomationProvider::GetTab(int win_handle, int tab_index,
877 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56878 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29879 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
880 Browser* browser = browser_tracker_->GetResource(win_handle);
881 if (tab_index < browser->tab_count()) {
882 TabContents* tab_contents =
883 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57884 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29885 }
886 }
initial.commit09911bf2008-07-26 23:55:29887}
888
[email protected]71f65dd2009-02-11 19:14:56889void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
890 std::wstring* title) {
891 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29892 if (tab_tracker_->ContainsHandle(handle)) {
893 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36894 NavigationEntry* entry = tab->GetActiveEntry();
895 if (entry != NULL) {
896 *title = UTF16ToWideHack(entry->title());
897 } else {
898 *title = std::wstring();
899 }
[email protected]71f65dd2009-02-11 19:14:56900 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29901 }
initial.commit09911bf2008-07-26 23:55:29902}
903
[email protected]77bc6732009-04-20 22:01:03904void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
905 *tabstrip_index = -1; // -1 is the error code
906
907 if (tab_tracker_->ContainsHandle(handle)) {
908 NavigationController* tab = tab_tracker_->GetResource(handle);
909 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12910 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:03911 }
912}
913
initial.commit09911bf2008-07-26 23:55:29914void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
915 if (window_tracker_->ContainsHandle(handle)) {
916 window_tracker_->Remove(window_tracker_->GetResource(handle));
917 }
918}
919
920void AutomationProvider::OnChannelError() {
921 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:57922 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:29923}
924
925// TODO(brettw) change this to accept GURLs when history supports it
926void AutomationProvider::OnRedirectQueryComplete(
927 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:37928 GURL from_url,
initial.commit09911bf2008-07-26 23:55:29929 bool success,
[email protected]379c2b12009-07-01 21:50:33930 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:29931 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:56932 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:29933
[email protected]deb57402009-02-06 01:35:30934 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:29935 if (success) {
[email protected]71f65dd2009-02-11 19:14:56936 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:29937 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:30938 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:29939 } else {
[email protected]71f65dd2009-02-11 19:14:56940 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:29941 }
942
[email protected]4f3dc372009-02-24 00:10:29943 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:30944
[email protected]71f65dd2009-02-11 19:14:56945 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:17946 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:56947 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29948}
949
950bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:57951 DCHECK(channel_.get());
952 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:29953}
954
955Browser* AutomationProvider::FindAndActivateTab(
956 NavigationController* controller) {
957 int tab_index;
958 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
959 if (browser)
960 browser->SelectTabContentsAt(tab_index, true);
961
962 return browser;
963}
964
[email protected]71f65dd2009-02-11 19:14:56965void AutomationProvider::GetCookies(const GURL& url, int handle,
966 int* value_size,
967 std::string* value) {
968 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:29969 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
970 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56971 *value =
initial.commit09911bf2008-07-26 23:55:29972 tab->profile()->GetRequestContext()->cookie_store()->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:56973 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:29974 }
initial.commit09911bf2008-07-26 23:55:29975}
976
[email protected]71f65dd2009-02-11 19:14:56977void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:29978 const std::string value,
[email protected]71f65dd2009-02-11 19:14:56979 int handle,
980 int* response_value) {
981 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:29982
983 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
984 NavigationController* tab = tab_tracker_->GetResource(handle);
985 URLRequestContext* context = tab->profile()->GetRequestContext();
986 if (context->cookie_store()->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:56987 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:29988 }
initial.commit09911bf2008-07-26 23:55:29989}
990
[email protected]71f65dd2009-02-11 19:14:56991void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
992 *success = false;
initial.commit09911bf2008-07-26 23:55:29993 if (tab_tracker_->ContainsHandle(handle)) {
994 NavigationController* tab = tab_tracker_->GetResource(handle);
995 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:54996 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:56997 *success = true;
initial.commit09911bf2008-07-26 23:55:29998 }
initial.commit09911bf2008-07-26 23:55:29999}
1000
[email protected]71f65dd2009-02-11 19:14:561001void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1002 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291003
1004 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561005 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341006 TabContents* tab_contents =
1007 tab_tracker_->GetResource(handle)->tab_contents();
1008 if (tab_contents->process())
1009 *process_id = tab_contents->process()->process().pid();
initial.commit09911bf2008-07-26 23:55:291010 }
initial.commit09911bf2008-07-26 23:55:291011}
1012
1013void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331014 NOTREACHED() << "This function has been deprecated. "
1015 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291016}
1017
[email protected]71f65dd2009-02-11 19:14:561018void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291019 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561020 const std::wstring& script,
1021 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291022 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341023 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1024 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571025 // Set the routing id of this message with the controller.
1026 // This routing id needs to be remembered for the reverse
1027 // communication while sending back the response of
1028 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331029 std::wstring set_automation_id;
1030 SStringPrintf(&set_automation_id,
1031 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561032 reply_message->routing_id());
1033
1034 DCHECK(reply_message_ == NULL);
1035 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291036
[email protected]57c6a652009-05-04 07:58:341037 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331038 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341039 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061040 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571041 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291042 }
1043
1044 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561045 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1046 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291047 }
1048}
1049
[email protected]71f65dd2009-02-11 19:14:561050void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1051 *visible = false;
[email protected]20e93d12008-08-28 16:31:571052
[email protected]59560e0b2009-06-04 03:30:221053 if (browser_tracker_->ContainsHandle(handle)) {
1054 Browser* browser = browser_tracker_->GetResource(handle);
1055 if (browser) {
1056 *visible = browser->window()->IsDownloadShelfVisible();
1057 }
1058 }
initial.commit09911bf2008-07-26 23:55:291059}
1060
[email protected]59560e0b2009-06-04 03:30:221061void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1062 if (browser_tracker_->ContainsHandle(handle)) {
1063 Browser* browser = browser_tracker_->GetResource(handle);
1064 if (browser) {
1065 if (visible)
1066 browser->window()->GetDownloadShelf()->Show();
1067 else
1068 browser->window()->GetDownloadShelf()->Close();
1069 }
1070 }
1071}
1072
1073
[email protected]71f65dd2009-02-11 19:14:561074void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1075 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291076 if (tab_tracker_->ContainsHandle(handle)) {
1077 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111078 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291079 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561080 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291081 }
1082 }
initial.commit09911bf2008-07-26 23:55:291083}
1084
initial.commit09911bf2008-07-26 23:55:291085void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561086 int handle, const std::wstring& find_request,
1087 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311088 NOTREACHED() << "This function has been deprecated."
1089 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561090 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311091 return;
1092}
1093
[email protected]4f999132009-03-31 18:08:401094void AutomationProvider::HandleFindRequest(
1095 int handle,
1096 const AutomationMsg_Find_Params& params,
1097 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291098 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561099 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1100 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291101 return;
1102 }
1103
1104 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111105 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291106
1107 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141108 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291109
[email protected]57c6a652009-05-04 07:58:341110 tab_contents->set_current_find_request_id(
1111 FindInPageNotificationObserver::kFindInPageRequestId);
1112 tab_contents->render_view_host()->StartFinding(
1113 FindInPageNotificationObserver::kFindInPageRequestId,
1114 params.search_string, params.forward, params.match_case,
1115 params.find_next);
initial.commit09911bf2008-07-26 23:55:291116}
1117
[email protected]5f8af2a2008-08-06 22:49:451118void AutomationProvider::HandleOpenFindInPageRequest(
1119 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291120 if (browser_tracker_->ContainsHandle(handle)) {
1121 Browser* browser = browser_tracker_->GetResource(handle);
1122 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451123 }
1124}
1125
[email protected]71f65dd2009-02-11 19:14:561126void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561127 *visible = false;
[email protected]855c0142009-09-28 22:35:241128 Browser* browser = browser_tracker_->GetResource(handle);
1129 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581130 FindBarTesting* find_bar =
1131 browser->find_bar()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241132 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291133 }
[email protected]20e93d12008-08-28 16:31:571134}
1135
[email protected]71f65dd2009-02-11 19:14:561136void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1137 int* y) {
[email protected]9e0534b2008-10-21 15:03:011138 gfx::Point position(0, 0);
1139 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291140 if (browser_tracker_->ContainsHandle(handle)) {
1141 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581142 FindBarTesting* find_bar =
1143 browser->find_bar()->find_bar()->GetFindBarTesting();
1144 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291145 }
[email protected]20e93d12008-08-28 16:31:571146
[email protected]71f65dd2009-02-11 19:14:561147 *x = position.x();
1148 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571149}
1150
initial.commit09911bf2008-07-26 23:55:291151void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561152 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341153 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1154 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561155 DCHECK(reply_message_ == NULL);
1156 reply_message_ = reply_message;
1157
[email protected]d9f9b792009-06-24 13:17:121158 DevToolsManager::GetInstance()->InspectElement(
1159 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291160 } else {
[email protected]71f65dd2009-02-11 19:14:561161 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1162 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291163 }
1164}
1165
1166void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091167 if (reply_message_) {
1168 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1169 num_resources);
1170 Send(reply_message_);
1171 reply_message_ = NULL;
1172 }
initial.commit09911bf2008-07-26 23:55:291173}
1174
[email protected]a7eee32f2009-05-22 18:08:171175class SetProxyConfigTask : public Task {
1176 public:
1177 explicit SetProxyConfigTask(net::ProxyService* proxy_service,
1178 const std::string& new_proxy_config)
1179 : proxy_service_(proxy_service), proxy_config_(new_proxy_config) {}
1180 virtual void Run() {
1181 // First, deserialize the JSON string. If this fails, log and bail.
1182 JSONStringValueSerializer deserializer(proxy_config_);
1183 std::string error_message;
1184 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1185 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1186 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1187 << error_message;
1188 return;
1189 }
1190
1191 scoped_ptr<DictionaryValue> dict(
1192 static_cast<DictionaryValue*>(root.release()));
1193 // Now put together a proxy configuration from the deserialized string.
1194 net::ProxyConfig pc;
1195 PopulateProxyConfig(*dict.get(), &pc);
1196
1197 DCHECK(proxy_service_);
1198 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1199 new net::ProxyConfigServiceFixed(pc));
1200 proxy_service_->ResetConfigService(proxy_config_service.release());
1201 }
1202
1203 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1204 DCHECK(pc);
1205 bool no_proxy = false;
1206 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1207 // Make no changes to the ProxyConfig.
1208 return;
1209 }
1210 bool auto_config;
1211 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
1212 pc->auto_detect = true;
1213 }
1214 std::string pac_url;
1215 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
1216 pc->pac_url = GURL(pac_url);
1217 }
1218 std::string proxy_bypass_list;
1219 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
1220 pc->ParseNoProxyList(proxy_bypass_list);
1221 }
1222 std::string proxy_server;
1223 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
1224 pc->proxy_rules.ParseFromString(proxy_server);
1225 }
1226 }
1227
1228 private:
1229 net::ProxyService* proxy_service_;
1230 std::string proxy_config_;
1231};
1232
1233
1234void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
1235 URLRequestContext* context = Profile::GetDefaultRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171236 if (!context) {
1237 FilePath user_data_dir;
1238 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1239 ProfileManager* profile_manager = g_browser_process->profile_manager();
1240 DCHECK(profile_manager);
1241 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1242 DCHECK(profile);
1243 context = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171244 }
1245 DCHECK(context);
1246 // Every URLRequestContext should have a proxy service.
1247 net::ProxyService* proxy_service = context->proxy_service();
1248 DCHECK(proxy_service);
1249
[email protected]81ae0a92009-08-06 02:16:161250 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
1251 new SetProxyConfigTask(proxy_service, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171252}
1253
[email protected]4f3dc372009-02-24 00:10:291254void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331255 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291256 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291257 if (tab_tracker_->ContainsHandle(handle)) {
1258 NavigationController* tab = tab_tracker_->GetResource(handle);
1259 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1260 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331261 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291262 }
initial.commit09911bf2008-07-26 23:55:291263}
1264
[email protected]14c0a032009-04-13 18:15:141265void AutomationProvider::OpenNewBrowserWindow(bool show,
1266 IPC::Message* reply_message) {
1267 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291268 // We may have no current browser windows open so don't rely on
1269 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]15952e462008-11-14 00:29:051270 Browser* browser = Browser::Create(profile_);
1271 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151272 if (show)
[email protected]15952e462008-11-14 00:29:051273 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291274}
1275
[email protected]71f65dd2009-02-11 19:14:561276void AutomationProvider::GetWindowForBrowser(int browser_handle,
1277 bool* success,
1278 int* handle) {
1279 *success = false;
1280 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291281
1282 if (browser_tracker_->ContainsHandle(browser_handle)) {
1283 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201284 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291285 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201286 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561287 *success = true;
initial.commit09911bf2008-07-26 23:55:291288 }
initial.commit09911bf2008-07-26 23:55:291289}
1290
[email protected]5ae5bed2009-08-21 18:52:441291#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291292void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561293 int browser_handle,
1294 bool* success,
1295 int* autocomplete_edit_handle) {
1296 *success = false;
1297 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291298
1299 if (browser_tracker_->ContainsHandle(browser_handle)) {
1300 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061301 LocationBar* loc_bar = browser->window()->GetLocationBar();
1302 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291303 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561304 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1305 *success = true;
initial.commit09911bf2008-07-26 23:55:291306 }
initial.commit09911bf2008-07-26 23:55:291307}
[email protected]5ae5bed2009-08-21 18:52:441308#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291309
[email protected]71f65dd2009-02-11 19:14:561310void AutomationProvider::ShowInterstitialPage(int tab_handle,
1311 const std::string& html_text,
1312 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291313 if (tab_tracker_->ContainsHandle(tab_handle)) {
1314 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111315 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401316
[email protected]2e028a082009-08-19 20:32:581317 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]965524b2009-04-04 21:32:401318 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341319 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401320 GURL("about:interstitial"),
1321 html_text);
1322 interstitial->Show();
1323 return;
initial.commit09911bf2008-07-26 23:55:291324 }
[email protected]71f65dd2009-02-11 19:14:561325
[email protected]457f5cf2009-08-18 16:37:521326 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1327 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561328 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291329}
1330
[email protected]71f65dd2009-02-11 19:14:561331void AutomationProvider::HideInterstitialPage(int tab_handle,
1332 bool* success) {
1333 *success = false;
[email protected]57c6a652009-05-04 07:58:341334 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1335 if (tab_contents && tab_contents->interstitial_page()) {
1336 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561337 *success = true;
initial.commit09911bf2008-07-26 23:55:291338 }
initial.commit09911bf2008-07-26 23:55:291339}
1340
[email protected]71f65dd2009-02-11 19:14:561341void AutomationProvider::CloseTab(int tab_handle,
1342 bool wait_until_closed,
1343 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291344 if (tab_tracker_->ContainsHandle(tab_handle)) {
1345 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1346 int index;
1347 Browser* browser = Browser::GetBrowserForController(controller, &index);
1348 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141349 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111350 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451351 return;
initial.commit09911bf2008-07-26 23:55:291352 }
[email protected]de246f52009-02-25 18:25:451353
1354 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:551355 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291356}
1357
[email protected]71f65dd2009-02-11 19:14:561358void AutomationProvider::CloseBrowser(int browser_handle,
1359 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291360 if (browser_tracker_->ContainsHandle(browser_handle)) {
1361 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561362 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561363 reply_message);
[email protected]f3e99e32008-07-30 04:48:391364 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291365 } else {
1366 NOTREACHED();
1367 }
1368}
1369
[email protected]71f65dd2009-02-11 19:14:561370void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1371 if (browser_tracker_->ContainsHandle(browser_handle)) {
1372 Browser* browser = browser_tracker_->GetResource(browser_handle);
1373 browser->window()->Close();
1374 } else {
1375 NOTREACHED();
1376 }
1377}
1378
[email protected]71f65dd2009-02-11 19:14:561379void AutomationProvider::NavigateInExternalTab(
1380 int handle, const GURL& url,
1381 AutomationMsg_NavigationResponseValues* status) {
1382 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291383
1384 if (tab_tracker_->ContainsHandle(handle)) {
1385 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c0588052008-10-27 23:01:501386 tab->LoadURL(url, GURL(), PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561387 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291388 }
initial.commit09911bf2008-07-26 23:55:291389}
1390
[email protected]4150ef02009-08-19 23:14:261391void AutomationProvider::NavigateExternalTabAtIndex(
1392 int handle, int navigation_index,
1393 AutomationMsg_NavigationResponseValues* status) {
1394 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1395
1396 if (tab_tracker_->ContainsHandle(handle)) {
1397 NavigationController* tab = tab_tracker_->GetResource(handle);
1398 tab->GoToIndex(navigation_index);
1399 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1400 }
1401}
1402
[email protected]71f65dd2009-02-11 19:14:561403void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1404 IPC::Message* reply_message) {
1405 if (tab_tracker_->ContainsHandle(tab_handle)) {
1406 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1407 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141408 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561409 }
1410}
1411
[email protected]71f65dd2009-02-11 19:14:561412void AutomationProvider::GetSecurityState(int handle, bool* success,
1413 SecurityStyle* security_style,
1414 int* ssl_cert_status,
1415 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291416 if (tab_tracker_->ContainsHandle(handle)) {
1417 NavigationController* tab = tab_tracker_->GetResource(handle);
1418 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561419 *success = true;
1420 *security_style = entry->ssl().security_style();
1421 *ssl_cert_status = entry->ssl().cert_status();
1422 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291423 } else {
[email protected]71f65dd2009-02-11 19:14:561424 *success = false;
1425 *security_style = SECURITY_STYLE_UNKNOWN;
1426 *ssl_cert_status = 0;
1427 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291428 }
1429}
1430
[email protected]71f65dd2009-02-11 19:14:561431void AutomationProvider::GetPageType(int handle, bool* success,
1432 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291433 if (tab_tracker_->ContainsHandle(handle)) {
1434 NavigationController* tab = tab_tracker_->GetResource(handle);
1435 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561436 *page_type = entry->page_type();
1437 *success = true;
initial.commit09911bf2008-07-26 23:55:291438 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341439 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561440 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341441 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561442 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291443 } else {
[email protected]71f65dd2009-02-11 19:14:561444 *success = false;
1445 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291446 }
1447}
1448
[email protected]84abba62009-10-07 17:01:441449void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
1450 int* duration_ms) {
1451 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
1452 event_name);
1453}
1454
[email protected]71f65dd2009-02-11 19:14:561455void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1456 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291457 if (tab_tracker_->ContainsHandle(handle)) {
1458 NavigationController* tab = tab_tracker_->GetResource(handle);
1459 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071460 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401461 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471462 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341463 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291464 if (ssl_blocking_page) {
1465 if (proceed) {
[email protected]2e028a082009-08-19 20:32:581466 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]71f65dd2009-02-11 19:14:561467 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291468 return;
1469 }
1470 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521471 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1472 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561473 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291474 return;
1475 }
1476 }
1477 }
1478 // We failed.
[email protected]457f5cf2009-08-18 16:37:521479 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1480 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561481 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291482}
initial.commit09911bf2008-07-26 23:55:291483
[email protected]71f65dd2009-02-11 19:14:561484void AutomationProvider::BringBrowserToFront(int browser_handle,
1485 bool* success) {
initial.commit09911bf2008-07-26 23:55:291486 if (browser_tracker_->ContainsHandle(browser_handle)) {
1487 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061488 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561489 *success = true;
initial.commit09911bf2008-07-26 23:55:291490 } else {
[email protected]71f65dd2009-02-11 19:14:561491 *success = false;
initial.commit09911bf2008-07-26 23:55:291492 }
1493}
1494
[email protected]71f65dd2009-02-11 19:14:561495void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1496 int message_num,
1497 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291498 if (browser_tracker_->ContainsHandle(browser_handle)) {
1499 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561500 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141501 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291502 } else {
[email protected]71f65dd2009-02-11 19:14:561503 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291504 }
1505}
1506
[email protected]71f65dd2009-02-11 19:14:561507void AutomationProvider::PrintNow(int tab_handle,
1508 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441509#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571510 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341511 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1512 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291513 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571514 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141515 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341516 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571517 return;
initial.commit09911bf2008-07-26 23:55:291518 }
[email protected]71f65dd2009-02-11 19:14:561519 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1520 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441521#else
1522 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1523 NOTIMPLEMENTED();
1524#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291525}
[email protected]d301c952009-07-13 15:02:411526
[email protected]71f65dd2009-02-11 19:14:561527void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111528 const FilePath& file_name,
1529 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561530 int type,
1531 bool* success) {
initial.commit09911bf2008-07-26 23:55:291532 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561533 *success = false;
initial.commit09911bf2008-07-26 23:55:291534 return;
1535 }
1536
1537 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1538 Browser* browser = FindAndActivateTab(nav);
1539 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141540 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561541 *success = false;
initial.commit09911bf2008-07-26 23:55:291542 return;
1543 }
1544
initial.commit09911bf2008-07-26 23:55:291545 SavePackage::SavePackageType save_type =
1546 static_cast<SavePackage::SavePackageType>(type);
1547 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1548 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341549 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291550
[email protected]71f65dd2009-02-11 19:14:561551 *success = true;
initial.commit09911bf2008-07-26 23:55:291552}
1553
[email protected]5ae5bed2009-08-21 18:52:441554#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471555// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561556void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1557 bool* success,
1558 std::wstring* text) {
1559 *success = false;
initial.commit09911bf2008-07-26 23:55:291560 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561561 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521562 GetText();
[email protected]71f65dd2009-02-11 19:14:561563 *success = true;
initial.commit09911bf2008-07-26 23:55:291564 }
initial.commit09911bf2008-07-26 23:55:291565}
1566
[email protected]71f65dd2009-02-11 19:14:561567void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1568 const std::wstring& text,
1569 bool* success) {
1570 *success = false;
initial.commit09911bf2008-07-26 23:55:291571 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521572 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1573 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561574 *success = true;
initial.commit09911bf2008-07-26 23:55:291575 }
initial.commit09911bf2008-07-26 23:55:291576}
1577
1578void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561579 int autocomplete_edit_handle,
1580 bool* success,
1581 std::vector<AutocompleteMatchData>* matches) {
1582 *success = false;
initial.commit09911bf2008-07-26 23:55:291583 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271584 const AutocompleteResult& result = autocomplete_edit_tracker_->
1585 GetResource(autocomplete_edit_handle)->model()->result();
1586 for (AutocompleteResult::const_iterator i = result.begin();
1587 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561588 matches->push_back(AutocompleteMatchData(*i));
1589 *success = true;
initial.commit09911bf2008-07-26 23:55:291590 }
initial.commit09911bf2008-07-26 23:55:291591}
1592
1593void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561594 int autocomplete_edit_handle,
1595 bool* success,
1596 bool* query_in_progress) {
1597 *success = false;
1598 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291599 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561600 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521601 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561602 *success = true;
initial.commit09911bf2008-07-26 23:55:291603 }
initial.commit09911bf2008-07-26 23:55:291604}
1605
[email protected]28790922009-03-09 19:48:371606void AutomationProvider::OnMessageFromExternalHost(int handle,
1607 const std::string& message,
1608 const std::string& origin,
1609 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191610 RenderViewHost* view_host = GetViewForTab(handle);
1611 if (!view_host) {
1612 return;
[email protected]fa83e762008-08-15 21:41:391613 }
[email protected]f7a68432009-07-29 23:18:191614
1615 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1616 view_host, message, origin, target)) {
1617 // Message was diverted.
1618 return;
1619 }
1620
1621 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1622 origin, target, this, view_host, handle)) {
1623 // Message was diverted.
1624 return;
1625 }
1626
1627 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1628 // Message was diverted.
1629 return;
1630 }
1631
1632 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391633}
[email protected]a9024892009-06-16 23:13:551634
1635bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1636 const std::string& message, const std::string& origin,
1637 const std::string& target) {
1638 if (target !=
1639 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1640 return false;
1641
1642 if (origin != extension_automation_constants::kAutomationOrigin) {
1643 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1644 return false;
1645 }
1646
1647 // The message is a JSON-encoded array with two elements, both strings. The
1648 // first is the name of the event to dispatch. The second is a JSON-encoding
1649 // of the arguments specific to that event.
1650 scoped_ptr<Value> message_value(JSONReader::Read(message, false));
1651 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1652 LOG(WARNING) << "Invalid browser event specified through automation";
1653 return false;
1654 }
1655
1656 const ListValue* args = static_cast<const ListValue*>(message_value.get());
1657
1658 std::string event_name;
1659 if (!args->GetString(0, &event_name)) {
1660 LOG(WARNING) << "No browser event name specified through automation";
1661 return false;
1662 }
1663
1664 std::string json_args;
1665 if (!args->GetString(1, &json_args)) {
1666 LOG(WARNING) << "No browser event args specified through automation";
1667 return false;
1668 }
1669
[email protected]7120f132009-07-20 21:05:371670 if (profile()->GetExtensionMessageService()) {
1671 profile()->GetExtensionMessageService()->
1672 DispatchEventToRenderers(event_name.c_str(), json_args);
1673 }
[email protected]a9024892009-06-16 23:13:551674
1675 return true;
1676}
[email protected]5ae5bed2009-08-21 18:52:441677#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:391678
[email protected]57c6a652009-05-04 07:58:341679TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:571680 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:571681 if (tab_tracker_->ContainsHandle(handle)) {
1682 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:341683 if (tab)
1684 *tab = nav_controller;
1685 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:571686 }
[email protected]57c6a652009-05-04 07:58:341687 return NULL;
[email protected]20e93d12008-08-28 16:31:571688}
1689
initial.commit09911bf2008-07-26 23:55:291690TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
1691 : AutomationProvider(profile) {
1692 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:141693 registrar_.Add(this, NotificationType::SESSION_END,
1694 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:291695}
1696
1697TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:291698 BrowserList::RemoveObserver(this);
1699}
1700
1701void TestingAutomationProvider::OnChannelError() {
1702 BrowserList::CloseAllBrowsers(true);
1703 AutomationProvider::OnChannelError();
1704}
1705
1706void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
1707 // For backwards compatibility with the testing automation interface, we
1708 // want the automation provider (and hence the process) to go away when the
1709 // last browser goes away.
1710 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:291711 // If you change this, update Observer for NotificationType::SESSION_END
1712 // below.
[email protected]295039bd2008-08-15 04:32:571713 MessageLoop::current()->PostTask(FROM_HERE,
1714 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:291715 }
1716}
1717
1718void TestingAutomationProvider::Observe(NotificationType type,
1719 const NotificationSource& source,
1720 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561721 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:291722 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
1723 // before the task runs resulting in this object not being deleted. This
1724 // Release balance out the Release scheduled by OnBrowserRemoving.
1725 Release();
1726}
[email protected]295039bd2008-08-15 04:32:571727
1728void TestingAutomationProvider::OnRemoveProvider() {
1729 AutomationProviderList::GetInstance()->RemoveProvider(this);
1730}
[email protected]8a3422c92008-09-24 17:42:421731
[email protected]71f65dd2009-02-11 19:14:561732void AutomationProvider::GetSSLInfoBarCount(int handle, int* count) {
1733 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:421734 if (tab_tracker_->ContainsHandle(handle)) {
1735 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:341736 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:111737 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421738 }
[email protected]8a3422c92008-09-24 17:42:421739}
1740
[email protected]71f65dd2009-02-11 19:14:561741void AutomationProvider::ClickSSLInfoBarLink(int handle,
[email protected]8a3422c92008-09-24 17:42:421742 int info_bar_index,
[email protected]71f65dd2009-02-11 19:14:561743 bool wait_for_navigation,
1744 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421745 bool success = false;
1746 if (tab_tracker_->ContainsHandle(handle)) {
1747 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1748 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:111749 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421750 if (info_bar_index >= 0 && info_bar_index < count) {
1751 if (wait_for_navigation) {
[email protected]2e028a082009-08-19 20:32:581752 AddNavigationStatusListener(nav_controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421753 }
[email protected]eb9ba192008-12-02 02:41:341754 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:111755 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:341756 info_bar_index);
1757 if (delegate->AsConfirmInfoBarDelegate())
1758 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:421759 success = true;
1760 }
1761 }
[email protected]4f3dc372009-02-24 00:10:291762 }
[email protected]58f622a62009-10-04 01:17:551763
1764 // This "!wait_for_navigation || !success condition" logic looks suspicious.
1765 // It will send a failure message when success is true but
1766 // |wait_for_navigation| is false.
1767 // TODO(phajdan.jr): investgate whether the reply param (currently
1768 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:421769 if (!wait_for_navigation || !success)
[email protected]457f5cf2009-08-18 16:37:521770 AutomationMsg_ClickSSLInfoBarLink::WriteReplyParams(
1771 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:421772}
1773
[email protected]71f65dd2009-02-11 19:14:561774void AutomationProvider::GetLastNavigationTime(int handle,
1775 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:421776 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:561777 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:421778}
1779
[email protected]71f65dd2009-02-11 19:14:561780void AutomationProvider::WaitForNavigation(int handle,
1781 int64 last_navigation_time,
1782 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:251783 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:421784 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:251785
[email protected]8a3422c92008-09-24 17:42:421786 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:561787 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:521788 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
1789 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:551790 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:291791 return;
[email protected]8a3422c92008-09-24 17:42:421792 }
1793
[email protected]2e028a082009-08-19 20:32:581794 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421795}
1796
[email protected]71f65dd2009-02-11 19:14:561797void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161798 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561799 int value,
1800 bool* success) {
1801 *success = false;
[email protected]8a3422c92008-09-24 17:42:421802 if (browser_tracker_->ContainsHandle(handle)) {
1803 Browser* browser = browser_tracker_->GetResource(handle);
1804 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561805 *success = true;
[email protected]8a3422c92008-09-24 17:42:421806 }
[email protected]8a3422c92008-09-24 17:42:421807}
[email protected]97fa6ce32008-12-19 01:48:161808
[email protected]71f65dd2009-02-11 19:14:561809void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161810 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561811 const std::wstring& value,
1812 bool* success) {
1813 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161814 if (browser_tracker_->ContainsHandle(handle)) {
1815 Browser* browser = browser_tracker_->GetResource(handle);
1816 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561817 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161818 }
[email protected]97fa6ce32008-12-19 01:48:161819}
1820
[email protected]71f65dd2009-02-11 19:14:561821void AutomationProvider::GetBooleanPreference(int handle,
1822 const std::wstring& name,
1823 bool* success,
1824 bool* value) {
1825 *success = false;
1826 *value = false;
[email protected]97fa6ce32008-12-19 01:48:161827 if (browser_tracker_->ContainsHandle(handle)) {
1828 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561829 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
1830 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161831 }
[email protected]97fa6ce32008-12-19 01:48:161832}
1833
[email protected]71f65dd2009-02-11 19:14:561834void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161835 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561836 bool value,
1837 bool* success) {
1838 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161839 if (browser_tracker_->ContainsHandle(handle)) {
1840 Browser* browser = browser_tracker_->GetResource(handle);
1841 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561842 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161843 }
[email protected]97fa6ce32008-12-19 01:48:161844}
1845
1846// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561847void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:401848 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:161849 if (tab_tracker_->ContainsHandle(tab_handle)) {
1850 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1851 Browser* browser = FindAndActivateTab(nav);
1852 DCHECK(browser);
1853
[email protected]57c6a652009-05-04 07:58:341854 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
1855 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:161856 }
[email protected]97fa6ce32008-12-19 01:48:161857}
1858
1859// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561860void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:401861 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:561862 bool* success) {
1863 *success = false;
[email protected]de246f52009-02-25 18:25:451864#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:161865 if (tab_tracker_->ContainsHandle(tab_handle)) {
1866 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1867 Browser* browser = FindAndActivateTab(nav);
1868 DCHECK(browser);
1869
[email protected]1fc025202009-01-20 23:03:141870 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:161871 int selected_encoding_id =
1872 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
1873 if (selected_encoding_id) {
1874 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:561875 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161876 }
1877 }
1878 }
[email protected]de246f52009-02-25 18:25:451879#else
1880 // TODO(port): Enable when encoding-related parts of Browser are ported.
1881 NOTIMPLEMENTED();
1882#endif
[email protected]97fa6ce32008-12-19 01:48:161883}
[email protected]5bcdb312009-01-07 21:43:201884
[email protected]4d434a1a2009-02-11 21:06:571885void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:201886 SavePackage::SetShouldPromptUser(should_prompt);
1887}
[email protected]87eab222009-03-13 00:47:451888
[email protected]b83e4602009-05-15 22:58:331889void AutomationProvider::SetEnableExtensionAutomation(bool automation_enabled) {
1890 AutomationExtensionFunction::SetEnabled(automation_enabled);
1891}
1892
[email protected]3753f522009-04-14 23:15:471893void AutomationProvider::GetWindowTitle(int handle, string16* text) {
1894 gfx::NativeWindow window = window_tracker_->GetResource(handle);
1895 text->assign(platform_util::GetWindowTitle(window));
1896}
[email protected]66ba4932009-06-04 19:22:131897
1898void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
1899 *count = -1; // -1 is the error code
1900 if (tab_tracker_->ContainsHandle(handle)) {
1901 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1902 TabContents* tab_contents = nav_controller->tab_contents();
1903 if (tab_contents) {
1904 BlockedPopupContainer* container =
1905 tab_contents->blocked_popup_container();
1906 if (container) {
1907 *count = static_cast<int>(container->GetBlockedPopupCount());
1908 } else {
1909 // If we don't have a container, we don't have any blocked popups to
1910 // contain!
1911 *count = 0;
1912 }
1913 }
1914 }
1915}
[email protected]f7a68432009-07-29 23:18:191916
1917void AutomationProvider::SelectAll(int tab_handle) {
1918 RenderViewHost* view = GetViewForTab(tab_handle);
1919 if (!view) {
1920 NOTREACHED();
1921 return;
1922 }
1923
1924 view->SelectAll();
1925}
1926
1927void AutomationProvider::Cut(int tab_handle) {
1928 RenderViewHost* view = GetViewForTab(tab_handle);
1929 if (!view) {
1930 NOTREACHED();
1931 return;
1932 }
1933
1934 view->Cut();
1935}
1936
1937void AutomationProvider::Copy(int tab_handle) {
1938 RenderViewHost* view = GetViewForTab(tab_handle);
1939 if (!view) {
1940 NOTREACHED();
1941 return;
1942 }
1943
1944 view->Copy();
1945}
1946
1947void AutomationProvider::Paste(int tab_handle) {
1948 RenderViewHost* view = GetViewForTab(tab_handle);
1949 if (!view) {
1950 NOTREACHED();
1951 return;
1952 }
1953
1954 view->Paste();
1955}
1956
1957void AutomationProvider::ReloadAsync(int tab_handle) {
1958 if (tab_tracker_->ContainsHandle(tab_handle)) {
1959 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1960 if (!tab) {
1961 NOTREACHED();
1962 return;
1963 }
1964
1965 tab->Reload(false);
1966 }
1967}
1968
1969void AutomationProvider::StopAsync(int tab_handle) {
1970 RenderViewHost* view = GetViewForTab(tab_handle);
1971 if (!view) {
[email protected]8b2b3312009-09-14 18:38:361972 // We tolerate StopAsync being called even before a view has been created.
1973 // So just log a warning instead of a NOTREACHED().
1974 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:191975 return;
1976 }
1977
1978 view->Stop();
1979}
1980
[email protected]1bb5f892009-10-06 01:44:571981void AutomationProvider::OnSetPageFontSize(int tab_handle,
1982 int font_size) {
1983 AutomationPageFontSize automation_font_size =
1984 static_cast<AutomationPageFontSize>(font_size);
1985
1986 if (automation_font_size < SMALLEST_FONT ||
1987 automation_font_size > LARGEST_FONT) {
1988 DLOG(ERROR) << "Invalid font size specified : "
1989 << font_size;
1990 return;
1991 }
1992
1993 if (tab_tracker_->ContainsHandle(tab_handle)) {
1994 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1995 DCHECK(tab != NULL);
1996 if (tab && tab->tab_contents()) {
1997 DCHECK(tab->tab_contents()->profile() != NULL);
1998 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
1999 prefs::kWebKitDefaultFontSize, font_size);
2000 }
2001 }
2002}
2003
2004
[email protected]2949e90d2009-08-21 15:32:522005void AutomationProvider::WaitForBrowserWindowCountToBecome(
2006 int target_count, IPC::Message* reply_message) {
2007 if (static_cast<int>(BrowserList::size()) == target_count) {
2008 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2009 reply_message, true);
2010 Send(reply_message);
2011 return;
2012 }
2013
2014 // Set up an observer (it will delete itself).
2015 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2016}
2017
2018void AutomationProvider::WaitForAppModalDialogToBeShown(
2019 IPC::Message* reply_message) {
2020 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2021 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2022 reply_message, true);
2023 Send(reply_message);
2024 return;
2025 }
2026
2027 // Set up an observer (it will delete itself).
2028 new AppModalDialogShownObserver(this, reply_message);
2029}
2030
[email protected]1126a1d32009-08-26 15:39:262031void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2032 int handle, int number_of_navigations, IPC::Message* reply_message) {
2033 if (tab_tracker_->ContainsHandle(handle)) {
2034 NavigationController* tab = tab_tracker_->GetResource(handle);
2035 Browser* browser = FindAndActivateTab(tab);
2036 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
2037 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2038 browser->GoBack(CURRENT_TAB);
2039 return;
2040 }
2041 }
2042
2043 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2044 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2045 Send(reply_message);
2046}
2047
2048void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2049 int handle, int number_of_navigations, IPC::Message* reply_message) {
2050 if (tab_tracker_->ContainsHandle(handle)) {
2051 NavigationController* tab = tab_tracker_->GetResource(handle);
2052 Browser* browser = FindAndActivateTab(tab);
2053 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
2054 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2055 browser->GoForward(CURRENT_TAB);
2056 return;
2057 }
2058 }
2059
2060 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2061 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2062 Send(reply_message);
2063}
2064
[email protected]f7a68432009-07-29 23:18:192065RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2066 if (tab_tracker_->ContainsHandle(tab_handle)) {
2067 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2068 if (!tab) {
2069 NOTREACHED();
2070 return NULL;
2071 }
2072
2073 TabContents* tab_contents = tab->tab_contents();
2074 if (!tab_contents) {
2075 NOTREACHED();
2076 return NULL;
2077 }
2078
2079 RenderViewHost* view_host = tab_contents->render_view_host();
2080 return view_host;
2081 }
2082
2083 return NULL;
2084}
[email protected]675595f2009-08-26 22:32:042085
2086void AutomationProvider::GetBrowserForWindow(int window_handle,
2087 bool* success,
2088 int* browser_handle) {
2089 *success = false;
2090 *browser_handle = 0;
2091
2092 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2093 if (!window)
2094 return;
2095
2096 BrowserList::const_iterator iter = BrowserList::begin();
2097 for (;iter != BrowserList::end(); ++iter) {
2098 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2099 if (window == this_window) {
2100 // Add() returns the existing handle for the resource if any.
2101 *browser_handle = browser_tracker_->Add(*iter);
2102 *success = true;
2103 return;
2104 }
2105 }
2106}