blob: f93d39c61f0e89a4feb016f21559bc1819da68e8 [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]8a3422c92008-09-24 17:42:4253#include "chrome/common/pref_service.h"
[email protected]71f65dd2009-02-11 19:14:5654#include "chrome/test/automation/automation_messages.h"
[email protected]a7eee32f2009-05-22 18:08:1755#include "net/proxy/proxy_service.h"
56#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2157#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2858#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2959
[email protected]de246f52009-02-25 18:25:4560#if defined(OS_WIN)
61// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4562#include "chrome/browser/character_encoding.h"
63#include "chrome/browser/download/save_package.h"
64#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4565#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4566#endif // defined(OS_WIN)
67
[email protected]5ae5bed2009-08-21 18:52:4468#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2869// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2870#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4471#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0672
[email protected]e1acf6f2008-10-27 20:43:3373using base::Time;
74
[email protected]cbab76d2008-10-13 22:42:4775class AutomationInterstitialPage : public InterstitialPage {
76 public:
[email protected]57c6a652009-05-04 07:58:3477 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4778 const GURL& url,
79 const std::string& contents)
80 : InterstitialPage(tab, true, url),
81 contents_(contents) {
82 }
83
84 virtual std::string GetHTMLContents() { return contents_; }
85
86 private:
87 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:2988
[email protected]cbab76d2008-10-13 22:42:4789 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
90};
91
[email protected]c2cb8542009-08-20 21:16:5192#if !defined(OS_MACOSX)
93class ClickTask : public Task {
94 public:
[email protected]fc2e0872009-08-21 22:14:4195 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:5196 virtual ~ClickTask() {}
97
98 virtual void Run() {
99 ui_controls::MouseButton button = ui_controls::LEFT;
100 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
101 views::Event::EF_LEFT_BUTTON_DOWN) {
102 button = ui_controls::LEFT;
103 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
104 views::Event::EF_RIGHT_BUTTON_DOWN) {
105 button = ui_controls::RIGHT;
106 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
107 views::Event::EF_MIDDLE_BUTTON_DOWN) {
108 button = ui_controls::MIDDLE;
109 } else {
110 NOTREACHED();
111 }
112
[email protected]fc2e0872009-08-21 22:14:41113 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51114 }
115
116 private:
[email protected]c2cb8542009-08-20 21:16:51117 int flags_;
118
119 DISALLOW_COPY_AND_ASSIGN(ClickTask);
120};
121#endif
122
initial.commit09911bf2008-07-26 23:55:29123AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57124 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56125 profile_(profile),
126 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29127 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29128 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20129 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29130 autocomplete_edit_tracker_.reset(
131 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29132 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
133 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
initial.commit09911bf2008-07-26 23:55:29134}
135
136AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50137 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
138 port_containers_.end());
139 port_containers_.clear();
140
[email protected]0da050b92008-08-19 19:29:47141 // Make sure that any outstanding NotificationObservers also get destroyed.
142 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31143 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47144 while ((observer = it.GetNext()) != NULL)
145 delete observer;
initial.commit09911bf2008-07-26 23:55:29146}
147
[email protected]9a3a293b2009-06-04 22:28:16148void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06149 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57150 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06151 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
152 automation_resource_message_filter_,
153 g_browser_process->io_thread()->message_loop(),
154 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05155 scoped_ptr<FileVersionInfo> file_version_info(
156 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40157 std::string version_string;
158 if (file_version_info != NULL) {
159 version_string = WideToASCII(file_version_info->file_version());
160 }
[email protected]c6cb1992009-04-13 16:45:29161
162 // Send a hello message with our current automation protocol version.
163 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29164}
165
166void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
167 if (expected_tabs == 0) {
168 Send(new AutomationMsg_InitialLoadsComplete(0));
169 } else {
170 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
171 }
172}
173
initial.commit09911bf2008-07-26 23:55:29174NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58175 NavigationController* tab, IPC::Message* reply_message,
176 int number_of_navigations) {
initial.commit09911bf2008-07-26 23:55:29177 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58178 new NavigationNotificationObserver(tab, this, reply_message,
179 number_of_navigations);
initial.commit09911bf2008-07-26 23:55:29180
[email protected]71f65dd2009-02-11 19:14:56181 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29182 return observer;
183}
184
185void AutomationProvider::RemoveNavigationStatusListener(
186 NotificationObserver* obs) {
187 notification_observer_list_.RemoveObserver(obs);
188}
189
190NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14191 Browser* parent,
192 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56193 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14194 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29195 notification_observer_list_.AddObserver(observer);
196
197 return observer;
198}
199
200void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
201 notification_observer_list_.RemoveObserver(obs);
202}
203
204void AutomationProvider::AddLoginHandler(NavigationController* tab,
205 LoginHandler* handler) {
206 login_handler_map_[tab] = handler;
207}
208
209void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
210 DCHECK(login_handler_map_[tab]);
211 login_handler_map_.erase(tab);
212}
213
[email protected]f44265b2009-05-19 18:52:50214void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
215 int port_id = port->port_id();
216 DCHECK_NE(-1, port_id);
217 DCHECK(port_containers_.find(port_id) == port_containers_.end());
218
219 port_containers_[port_id] = port;
220}
221
222void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
223 int port_id = port->port_id();
224 DCHECK_NE(-1, port_id);
225
226 PortContainerMap::iterator it = port_containers_.find(port_id);
227 DCHECK(it != port_containers_.end());
228
229 if (it != port_containers_.end()) {
230 delete it->second;
231 port_containers_.erase(it);
232 }
233}
234
235ExtensionPortContainer* AutomationProvider::GetPortContainer(
236 int port_id) const {
237 PortContainerMap::const_iterator it = port_containers_.find(port_id);
238 if (it == port_containers_.end())
239 return NULL;
240
241 return it->second;
242}
243
initial.commit09911bf2008-07-26 23:55:29244int AutomationProvider::GetIndexForNavigationController(
245 const NavigationController* controller, const Browser* parent) const {
246 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12247 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29248}
249
250void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
251 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14252 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56253 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
254 CloseBrowserAsync)
255 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
256 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
257 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
258 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
259 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
260 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14261 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58262 IPC_MESSAGE_HANDLER_DELAY_REPLY(
263 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
264 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56265 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
266 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
267 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
268 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
269 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14270 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56271 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
272 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
273 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14274 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29275 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
276 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56277 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36278 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56279 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29280 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56281 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29282 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
283 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56284 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14285 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24286 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44287 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33288 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44289 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33290 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24291 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14292 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24293 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15294 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14295 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44296#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24297 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]1c58a5c2009-05-21 18:47:14298 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]5ae5bed2009-08-21 18:52:44299#endif // !defined(OS_MACOSX)
[email protected]8dd404bb2009-09-22 19:57:24300#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]71f65dd2009-02-11 19:14:56301 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
302 WindowSimulateDrag)
[email protected]8dd404bb2009-09-22 19:57:24303#endif // defined(OS_WIN) || defined(OS_LINUX)
[email protected]71f65dd2009-02-11 19:14:56304 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
305 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37306#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56307 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45308#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56309 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
310 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03311 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56312 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14313 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
initial.commit09911bf2008-07-26 23:55:29314 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14315 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56316 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
317 ExecuteJavascript)
318 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29319 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14320 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
321 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56322 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
323 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14324 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17325 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14326 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14327 OpenNewBrowserWindow)
328 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56329 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14330 GetAutocompleteEditForBrowser)
331 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45332#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29333 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17334#endif
[email protected]71f65dd2009-02-11 19:14:56335 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29336 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26337 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
338 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56339 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14340 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56341 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14342 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17343#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29344 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
345 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17346#endif
[email protected]71f65dd2009-02-11 19:14:56347 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
348 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14349 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17350#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04351 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39352 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
353 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17354#endif
[email protected]1c58a5c2009-05-21 18:47:14355 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
356 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]71f65dd2009-02-11 19:14:56357 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
358 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29359 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
360 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
361 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56362 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41363 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56364 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
365 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29366 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56367 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29368 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56369 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29370 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56371 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29372 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56373 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45374 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45375 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
376 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14377 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56378 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57379 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56380 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57381 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56382 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
383 GetBookmarkBarVisibility)
[email protected]1c58a5c2009-05-21 18:47:14384 IPC_MESSAGE_HANDLER(AutomationMsg_GetSSLInfoBarCount, GetSSLInfoBarCount)
[email protected]71f65dd2009-02-11 19:14:56385 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickSSLInfoBarLink,
386 ClickSSLInfoBarLink)
387 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42388 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56389 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
390 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14391 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56392 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44393 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56394 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20395 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14396 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56397 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16398 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56399 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16400 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56401 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16402 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14403 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20404 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
405 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14406 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]b83e4602009-05-15 22:58:33407 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
408 SetEnableExtensionAutomation)
[email protected]59560e0b2009-06-04 03:30:22409 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13410 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19411 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
412 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
413 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
414 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
415 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
416 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52417 IPC_MESSAGE_HANDLER_DELAY_REPLY(
418 AutomationMsg_WaitForBrowserWindowCountToBecome,
419 WaitForBrowserWindowCountToBecome)
420 IPC_MESSAGE_HANDLER_DELAY_REPLY(
421 AutomationMsg_WaitForAppModalDialogToBeShown,
422 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26423 IPC_MESSAGE_HANDLER_DELAY_REPLY(
424 AutomationMsg_GoBackBlockUntilNavigationsComplete,
425 GoBackBlockUntilNavigationsComplete)
426 IPC_MESSAGE_HANDLER_DELAY_REPLY(
427 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
428 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14429#if defined(OS_WIN)
430 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
431#endif
initial.commit09911bf2008-07-26 23:55:29432 IPC_END_MESSAGE_MAP()
433}
434
[email protected]71f65dd2009-02-11 19:14:56435void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
436 *status = -1;
initial.commit09911bf2008-07-26 23:55:29437 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
438 Browser* browser = browser_tracker_->GetResource(handle);
439 if (at_index >= 0 && at_index < browser->tab_count()) {
440 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56441 *status = 0;
initial.commit09911bf2008-07-26 23:55:29442 }
443 }
initial.commit09911bf2008-07-26 23:55:29444}
445
[email protected]71f65dd2009-02-11 19:14:56446void AutomationProvider::AppendTab(int handle, const GURL& url,
447 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29448 int append_tab_response = -1; // -1 is the error code
449 NotificationObserver* observer = NULL;
450
451 if (browser_tracker_->ContainsHandle(handle)) {
452 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14453 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58454 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
455 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39456 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29457 if (tab_contents) {
458 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57459 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29460 }
461 }
462
463 if (append_tab_response < 0) {
464 // The append tab failed. Remove the TabStripObserver
465 if (observer) {
466 RemoveTabStripObserver(observer);
467 delete observer;
468 }
469
[email protected]71f65dd2009-02-11 19:14:56470 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
471 append_tab_response);
472 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29473 }
474}
475
[email protected]71f65dd2009-02-11 19:14:56476void AutomationProvider::NavigateToURL(int handle, const GURL& url,
477 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58478 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
479}
480
481void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
482 int handle, const GURL& url, int number_of_navigations,
483 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29484 if (tab_tracker_->ContainsHandle(handle)) {
485 NavigationController* tab = tab_tracker_->GetResource(handle);
486
487 // Simulate what a user would do. Activate the tab and then navigate.
488 // We could allow navigating in a background tab in future.
489 Browser* browser = FindAndActivateTab(tab);
490
491 if (browser) {
[email protected]2e028a082009-08-19 20:32:58492 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
[email protected]71f65dd2009-02-11 19:14:56493
initial.commit09911bf2008-07-26 23:55:29494 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50495 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29496 return;
497 }
498 }
[email protected]71f65dd2009-02-11 19:14:56499
500 AutomationMsg_NavigateToURL::WriteReplyParams(
501 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
502 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29503}
[email protected]2949e90d2009-08-21 15:32:52504
[email protected]71f65dd2009-02-11 19:14:56505void AutomationProvider::NavigationAsync(int handle, const GURL& url,
506 bool* status) {
507 *status = false;
initial.commit09911bf2008-07-26 23:55:29508
509 if (tab_tracker_->ContainsHandle(handle)) {
510 NavigationController* tab = tab_tracker_->GetResource(handle);
511
512 // Simulate what a user would do. Activate the tab and then navigate.
513 // We could allow navigating in a background tab in future.
514 Browser* browser = FindAndActivateTab(tab);
515
516 if (browser) {
517 // Don't add any listener unless a callback mechanism is desired.
518 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50519 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56520 *status = true;
initial.commit09911bf2008-07-26 23:55:29521 }
522 }
initial.commit09911bf2008-07-26 23:55:29523}
524
[email protected]71f65dd2009-02-11 19:14:56525void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29526 if (tab_tracker_->ContainsHandle(handle)) {
527 NavigationController* tab = tab_tracker_->GetResource(handle);
528 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14529 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]2e028a082009-08-19 20:32:58530 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29531 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29532 return;
533 }
534 }
[email protected]71f65dd2009-02-11 19:14:56535
536 AutomationMsg_GoBack::WriteReplyParams(
537 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
538 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29539}
540
[email protected]71f65dd2009-02-11 19:14:56541void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29542 if (tab_tracker_->ContainsHandle(handle)) {
543 NavigationController* tab = tab_tracker_->GetResource(handle);
544 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14545 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]2e028a082009-08-19 20:32:58546 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29547 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29548 return;
549 }
550 }
[email protected]71f65dd2009-02-11 19:14:56551
552 AutomationMsg_GoForward::WriteReplyParams(
553 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
554 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29555}
556
[email protected]71f65dd2009-02-11 19:14:56557void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29558 if (tab_tracker_->ContainsHandle(handle)) {
559 NavigationController* tab = tab_tracker_->GetResource(handle);
560 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14561 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]2e028a082009-08-19 20:32:58562 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29563 browser->Reload();
564 return;
565 }
566 }
[email protected]71f65dd2009-02-11 19:14:56567
568 AutomationMsg_Reload::WriteReplyParams(
569 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
570 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29571}
572
[email protected]71f65dd2009-02-11 19:14:56573void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29574 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56575 const std::wstring& password,
576 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29577 if (tab_tracker_->ContainsHandle(tab_handle)) {
578 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
579 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
580
581 if (iter != login_handler_map_.end()) {
582 // If auth is needed again after this, assume login has failed. This is
583 // not strictly correct, because a navigation can require both proxy and
584 // server auth, but it should be OK for now.
585 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58586 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29587 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52588 return;
initial.commit09911bf2008-07-26 23:55:29589 }
590 }
[email protected]de246f52009-02-25 18:25:45591
[email protected]457f5cf2009-08-18 16:37:52592 AutomationMsg_SetAuth::WriteReplyParams(
593 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
594 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29595}
596
[email protected]71f65dd2009-02-11 19:14:56597void AutomationProvider::CancelAuth(int tab_handle,
598 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29599 if (tab_tracker_->ContainsHandle(tab_handle)) {
600 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
601 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
602
603 if (iter != login_handler_map_.end()) {
604 // If auth is needed again after this, something is screwy.
605 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58606 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29607 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52608 return;
initial.commit09911bf2008-07-26 23:55:29609 }
610 }
[email protected]de246f52009-02-25 18:25:45611
[email protected]457f5cf2009-08-18 16:37:52612 AutomationMsg_CancelAuth::WriteReplyParams(
613 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
614 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29615}
616
[email protected]71f65dd2009-02-11 19:14:56617void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
618 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29619
620 if (tab_tracker_->ContainsHandle(tab_handle)) {
621 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
622 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
623
624 if (iter != login_handler_map_.end()) {
625 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56626 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29627 }
628 }
initial.commit09911bf2008-07-26 23:55:29629}
630
[email protected]71f65dd2009-02-11 19:14:56631void AutomationProvider::GetRedirectsFrom(int tab_handle,
632 const GURL& source_url,
633 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29634 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
635 if (tab_tracker_->ContainsHandle(tab_handle)) {
636 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
637 HistoryService* history_service =
638 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
639
640 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
641 "has no history service";
642 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56643 DCHECK(reply_message_ == NULL);
644 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29645 // Schedule a history query for redirects. The response will be sent
646 // asynchronously from the callback the history system uses to notify us
647 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29648 redirect_query_ = history_service->QueryRedirectsFrom(
649 source_url, &consumer_,
650 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
651 return; // Response will be sent when query completes.
652 }
653 }
654
655 // Send failure response.
[email protected]deb57402009-02-06 01:35:30656 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56657 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
658 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29659}
660
[email protected]71f65dd2009-02-11 19:14:56661void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
662 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29663 if (browser_tracker_->ContainsHandle(handle)) {
664 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56665 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29666 }
initial.commit09911bf2008-07-26 23:55:29667}
668
[email protected]202e7a72009-06-15 03:48:36669void AutomationProvider::GetBrowserLocale(string16* locale) {
670 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06671 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36672}
673
[email protected]71f65dd2009-02-11 19:14:56674void AutomationProvider::GetBrowserWindowCount(int* window_count) {
675 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29676}
677
[email protected]24497032009-05-01 17:00:29678void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
679 *window_count = static_cast<int>(
680 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
681}
682
[email protected]71f65dd2009-02-11 19:14:56683void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
684 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07685 AppModalDialog* dialog_delegate =
686 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50687 *showing_dialog = (dialog_delegate != NULL);
688 if (*showing_dialog)
689 *dialog_button = dialog_delegate->GetDialogButtons();
690 else
[email protected]478ff2ed2009-04-21 23:49:18691 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20692}
693
[email protected]71f65dd2009-02-11 19:14:56694void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
695 *success = false;
[email protected]fad84eab2008-12-05 00:37:20696
[email protected]1f460072009-05-28 17:02:07697 AppModalDialog* dialog_delegate =
698 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50699 if (dialog_delegate &&
700 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18701 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
702 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28703 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56704 *success = true;
[email protected]fad84eab2008-12-05 00:37:20705 }
[email protected]478ff2ed2009-04-21 23:49:18706 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
707 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56708 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28709 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56710 *success = true;
[email protected]fad84eab2008-12-05 00:37:20711 }
712 }
[email protected]c274acc2008-11-11 20:13:44713}
714
[email protected]71f65dd2009-02-11 19:14:56715void AutomationProvider::GetBrowserWindow(int index, int* handle) {
716 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29717 if (index >= 0) {
718 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29719 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29720 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56721 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29722 }
723 }
initial.commit09911bf2008-07-26 23:55:29724}
725
[email protected]24497032009-05-01 17:00:29726void AutomationProvider::FindNormalBrowserWindow(int* handle) {
727 *handle = 0;
728 Browser* browser = BrowserList::FindBrowserWithType(profile_,
729 Browser::TYPE_NORMAL);
730 if (browser)
731 *handle = browser_tracker_->Add(browser);
732}
733
[email protected]71f65dd2009-02-11 19:14:56734void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
735 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29736 Browser* browser = BrowserList::GetLastActive();
737 if (browser)
[email protected]71f65dd2009-02-11 19:14:56738 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29739}
740
[email protected]fb05865f2009-09-21 15:05:46741#if defined(OS_LINUX) || defined(OS_MACOSX)
[email protected]9a08bcf2009-08-12 19:56:28742// TODO(estade): use this implementation for all platforms?
743void AutomationProvider::GetActiveWindow(int* handle) {
744 gfx::NativeWindow window =
745 BrowserList::GetLastActive()->window()->GetNativeHandle();
746 *handle = window_tracker_->Add(window);
747}
748#endif
749
[email protected]4f6381ee2009-04-16 02:46:33750void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
751 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56752 *success = false;
[email protected]4ae62752008-08-04 23:28:47753 if (browser_tracker_->ContainsHandle(handle)) {
754 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14755 if (browser->command_updater()->SupportsCommand(command) &&
756 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47757 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56758 *success = true;
[email protected]4ae62752008-08-04 23:28:47759 }
760 }
[email protected]4ae62752008-08-04 23:28:47761}
762
[email protected]4f6381ee2009-04-16 02:46:33763void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56764 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53765 // List of commands which just finish synchronously and don't require
766 // setting up an observer.
767 static const int kSynchronousCommands[] = {
768 IDC_HOME,
769 IDC_SELECT_NEXT_TAB,
770 IDC_SELECT_PREVIOUS_TAB,
771 IDC_SHOW_DOWNLOADS,
772 IDC_SHOW_HISTORY,
773 };
[email protected]56e71b7c2009-03-27 03:05:56774 if (browser_tracker_->ContainsHandle(handle)) {
775 Browser* browser = browser_tracker_->GetResource(handle);
776 if (browser->command_updater()->SupportsCommand(command) &&
777 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53778 // First check if we can handle the command without using an observer.
779 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
780 if (command == kSynchronousCommands[i]) {
781 browser->ExecuteCommand(command);
782 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
783 true);
784 Send(reply_message);
785 return;
786 }
787 }
788
789 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42790 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
791 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27792 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42793 return;
794 }
[email protected]56e71b7c2009-03-27 03:05:56795 }
796 }
[email protected]49a14a82009-03-31 04:16:44797 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56798 Send(reply_message);
799}
800
[email protected]fc2e0872009-08-21 22:14:41801// This task just adds another task to the event queue. This is useful if
802// you want to ensure that any tasks added to the event queue after this one
803// have already been processed by the time |task| is run.
804class InvokeTaskLaterTask : public Task {
805 public:
806 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
807 virtual ~InvokeTaskLaterTask() {}
808
809 virtual void Run() {
810 MessageLoop::current()->PostTask(FROM_HERE, task_);
811 }
812
813 private:
814 Task* task_;
815
816 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
817};
818
[email protected]fc2e0872009-08-21 22:14:41819#if defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29820void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
821 int handle,
[email protected]d1a5941e2009-08-13 23:34:24822 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29823 int flags) {
[email protected]b410bc32009-08-14 01:11:14824 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51825 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41826 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29827 }
828}
829
initial.commit09911bf2008-07-26 23:55:29830void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
831 int handle,
[email protected]bc1407f2009-09-29 00:33:35832 int key,
initial.commit09911bf2008-07-26 23:55:29833 int flags) {
[email protected]b410bc32009-08-14 01:11:14834 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29835 return;
836
[email protected]b410bc32009-08-14 01:11:14837 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29838 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35839 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38840 ((flags & views::Event::EF_CONTROL_DOWN) ==
841 views::Event::EF_CONTROL_DOWN),
842 ((flags & views::Event::EF_SHIFT_DOWN) ==
843 views::Event::EF_SHIFT_DOWN),
844 ((flags & views::Event::EF_ALT_DOWN) ==
845 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29846}
[email protected]fc2e0872009-08-21 22:14:41847#endif // defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29848
[email protected]71f65dd2009-02-11 19:14:56849void AutomationProvider::IsWindowActive(int handle, bool* success,
850 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29851 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17852 *is_active =
853 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56854 *success = true;
initial.commit09911bf2008-07-26 23:55:29855 } else {
[email protected]71f65dd2009-02-11 19:14:56856 *success = false;
857 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29858 }
859}
860
[email protected]71f65dd2009-02-11 19:14:56861void AutomationProvider::GetTabCount(int handle, int* tab_count) {
862 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29863
864 if (browser_tracker_->ContainsHandle(handle)) {
865 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56866 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29867 }
initial.commit09911bf2008-07-26 23:55:29868}
869
[email protected]71f65dd2009-02-11 19:14:56870void AutomationProvider::GetTab(int win_handle, int tab_index,
871 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56872 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29873 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
874 Browser* browser = browser_tracker_->GetResource(win_handle);
875 if (tab_index < browser->tab_count()) {
876 TabContents* tab_contents =
877 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57878 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29879 }
880 }
initial.commit09911bf2008-07-26 23:55:29881}
882
[email protected]71f65dd2009-02-11 19:14:56883void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
884 std::wstring* title) {
885 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29886 if (tab_tracker_->ContainsHandle(handle)) {
887 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36888 NavigationEntry* entry = tab->GetActiveEntry();
889 if (entry != NULL) {
890 *title = UTF16ToWideHack(entry->title());
891 } else {
892 *title = std::wstring();
893 }
[email protected]71f65dd2009-02-11 19:14:56894 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29895 }
initial.commit09911bf2008-07-26 23:55:29896}
897
[email protected]77bc6732009-04-20 22:01:03898void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
899 *tabstrip_index = -1; // -1 is the error code
900
901 if (tab_tracker_->ContainsHandle(handle)) {
902 NavigationController* tab = tab_tracker_->GetResource(handle);
903 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12904 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:03905 }
906}
907
initial.commit09911bf2008-07-26 23:55:29908void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
909 if (window_tracker_->ContainsHandle(handle)) {
910 window_tracker_->Remove(window_tracker_->GetResource(handle));
911 }
912}
913
914void AutomationProvider::OnChannelError() {
915 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:57916 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:29917}
918
919// TODO(brettw) change this to accept GURLs when history supports it
920void AutomationProvider::OnRedirectQueryComplete(
921 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:37922 GURL from_url,
initial.commit09911bf2008-07-26 23:55:29923 bool success,
[email protected]379c2b12009-07-01 21:50:33924 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:29925 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:56926 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:29927
[email protected]deb57402009-02-06 01:35:30928 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:29929 if (success) {
[email protected]71f65dd2009-02-11 19:14:56930 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:29931 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:30932 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:29933 } else {
[email protected]71f65dd2009-02-11 19:14:56934 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:29935 }
936
[email protected]4f3dc372009-02-24 00:10:29937 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:30938
[email protected]71f65dd2009-02-11 19:14:56939 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:17940 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:56941 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29942}
943
944bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:57945 DCHECK(channel_.get());
946 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:29947}
948
949Browser* AutomationProvider::FindAndActivateTab(
950 NavigationController* controller) {
951 int tab_index;
952 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
953 if (browser)
954 browser->SelectTabContentsAt(tab_index, true);
955
956 return browser;
957}
958
[email protected]71f65dd2009-02-11 19:14:56959void AutomationProvider::GetCookies(const GURL& url, int handle,
960 int* value_size,
961 std::string* value) {
962 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:29963 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
964 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56965 *value =
initial.commit09911bf2008-07-26 23:55:29966 tab->profile()->GetRequestContext()->cookie_store()->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:56967 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:29968 }
initial.commit09911bf2008-07-26 23:55:29969}
970
[email protected]71f65dd2009-02-11 19:14:56971void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:29972 const std::string value,
[email protected]71f65dd2009-02-11 19:14:56973 int handle,
974 int* response_value) {
975 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:29976
977 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
978 NavigationController* tab = tab_tracker_->GetResource(handle);
979 URLRequestContext* context = tab->profile()->GetRequestContext();
980 if (context->cookie_store()->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:56981 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:29982 }
initial.commit09911bf2008-07-26 23:55:29983}
984
[email protected]71f65dd2009-02-11 19:14:56985void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
986 *success = false;
initial.commit09911bf2008-07-26 23:55:29987 if (tab_tracker_->ContainsHandle(handle)) {
988 NavigationController* tab = tab_tracker_->GetResource(handle);
989 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:54990 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:56991 *success = true;
initial.commit09911bf2008-07-26 23:55:29992 }
initial.commit09911bf2008-07-26 23:55:29993}
994
[email protected]71f65dd2009-02-11 19:14:56995void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
996 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:29997
998 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:56999 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341000 TabContents* tab_contents =
1001 tab_tracker_->GetResource(handle)->tab_contents();
1002 if (tab_contents->process())
1003 *process_id = tab_contents->process()->process().pid();
initial.commit09911bf2008-07-26 23:55:291004 }
initial.commit09911bf2008-07-26 23:55:291005}
1006
1007void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331008 NOTREACHED() << "This function has been deprecated. "
1009 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291010}
1011
[email protected]71f65dd2009-02-11 19:14:561012void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291013 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561014 const std::wstring& script,
1015 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291016 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341017 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1018 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571019 // Set the routing id of this message with the controller.
1020 // This routing id needs to be remembered for the reverse
1021 // communication while sending back the response of
1022 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331023 std::wstring set_automation_id;
1024 SStringPrintf(&set_automation_id,
1025 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561026 reply_message->routing_id());
1027
1028 DCHECK(reply_message_ == NULL);
1029 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291030
[email protected]57c6a652009-05-04 07:58:341031 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331032 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341033 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061034 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571035 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291036 }
1037
1038 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561039 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1040 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291041 }
1042}
1043
[email protected]71f65dd2009-02-11 19:14:561044void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1045 *visible = false;
[email protected]20e93d12008-08-28 16:31:571046
[email protected]59560e0b2009-06-04 03:30:221047 if (browser_tracker_->ContainsHandle(handle)) {
1048 Browser* browser = browser_tracker_->GetResource(handle);
1049 if (browser) {
1050 *visible = browser->window()->IsDownloadShelfVisible();
1051 }
1052 }
initial.commit09911bf2008-07-26 23:55:291053}
1054
[email protected]59560e0b2009-06-04 03:30:221055void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1056 if (browser_tracker_->ContainsHandle(handle)) {
1057 Browser* browser = browser_tracker_->GetResource(handle);
1058 if (browser) {
1059 if (visible)
1060 browser->window()->GetDownloadShelf()->Show();
1061 else
1062 browser->window()->GetDownloadShelf()->Close();
1063 }
1064 }
1065}
1066
1067
[email protected]71f65dd2009-02-11 19:14:561068void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1069 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291070 if (tab_tracker_->ContainsHandle(handle)) {
1071 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111072 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291073 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561074 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291075 }
1076 }
initial.commit09911bf2008-07-26 23:55:291077}
1078
initial.commit09911bf2008-07-26 23:55:291079void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561080 int handle, const std::wstring& find_request,
1081 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311082 NOTREACHED() << "This function has been deprecated."
1083 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561084 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311085 return;
1086}
1087
[email protected]4f999132009-03-31 18:08:401088void AutomationProvider::HandleFindRequest(
1089 int handle,
1090 const AutomationMsg_Find_Params& params,
1091 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291092 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561093 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1094 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291095 return;
1096 }
1097
1098 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111099 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291100
1101 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141102 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291103
[email protected]57c6a652009-05-04 07:58:341104 tab_contents->set_current_find_request_id(
1105 FindInPageNotificationObserver::kFindInPageRequestId);
1106 tab_contents->render_view_host()->StartFinding(
1107 FindInPageNotificationObserver::kFindInPageRequestId,
1108 params.search_string, params.forward, params.match_case,
1109 params.find_next);
initial.commit09911bf2008-07-26 23:55:291110}
1111
[email protected]5f8af2a2008-08-06 22:49:451112void AutomationProvider::HandleOpenFindInPageRequest(
1113 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291114 if (browser_tracker_->ContainsHandle(handle)) {
1115 Browser* browser = browser_tracker_->GetResource(handle);
1116 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451117 }
1118}
1119
[email protected]71f65dd2009-02-11 19:14:561120void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561121 *visible = false;
[email protected]855c0142009-09-28 22:35:241122 Browser* browser = browser_tracker_->GetResource(handle);
1123 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581124 FindBarTesting* find_bar =
1125 browser->find_bar()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241126 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291127 }
[email protected]20e93d12008-08-28 16:31:571128}
1129
[email protected]71f65dd2009-02-11 19:14:561130void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1131 int* y) {
[email protected]9e0534b2008-10-21 15:03:011132 gfx::Point position(0, 0);
1133 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291134 if (browser_tracker_->ContainsHandle(handle)) {
1135 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581136 FindBarTesting* find_bar =
1137 browser->find_bar()->find_bar()->GetFindBarTesting();
1138 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291139 }
[email protected]20e93d12008-08-28 16:31:571140
[email protected]71f65dd2009-02-11 19:14:561141 *x = position.x();
1142 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571143}
1144
initial.commit09911bf2008-07-26 23:55:291145void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561146 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341147 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1148 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561149 DCHECK(reply_message_ == NULL);
1150 reply_message_ = reply_message;
1151
[email protected]d9f9b792009-06-24 13:17:121152 DevToolsManager::GetInstance()->InspectElement(
1153 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291154 } else {
[email protected]71f65dd2009-02-11 19:14:561155 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1156 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291157 }
1158}
1159
1160void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091161 if (reply_message_) {
1162 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1163 num_resources);
1164 Send(reply_message_);
1165 reply_message_ = NULL;
1166 }
initial.commit09911bf2008-07-26 23:55:291167}
1168
[email protected]a7eee32f2009-05-22 18:08:171169class SetProxyConfigTask : public Task {
1170 public:
1171 explicit SetProxyConfigTask(net::ProxyService* proxy_service,
1172 const std::string& new_proxy_config)
1173 : proxy_service_(proxy_service), proxy_config_(new_proxy_config) {}
1174 virtual void Run() {
1175 // First, deserialize the JSON string. If this fails, log and bail.
1176 JSONStringValueSerializer deserializer(proxy_config_);
1177 std::string error_message;
1178 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1179 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1180 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1181 << error_message;
1182 return;
1183 }
1184
1185 scoped_ptr<DictionaryValue> dict(
1186 static_cast<DictionaryValue*>(root.release()));
1187 // Now put together a proxy configuration from the deserialized string.
1188 net::ProxyConfig pc;
1189 PopulateProxyConfig(*dict.get(), &pc);
1190
1191 DCHECK(proxy_service_);
1192 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1193 new net::ProxyConfigServiceFixed(pc));
1194 proxy_service_->ResetConfigService(proxy_config_service.release());
1195 }
1196
1197 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1198 DCHECK(pc);
1199 bool no_proxy = false;
1200 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1201 // Make no changes to the ProxyConfig.
1202 return;
1203 }
1204 bool auto_config;
1205 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
1206 pc->auto_detect = true;
1207 }
1208 std::string pac_url;
1209 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
1210 pc->pac_url = GURL(pac_url);
1211 }
1212 std::string proxy_bypass_list;
1213 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
1214 pc->ParseNoProxyList(proxy_bypass_list);
1215 }
1216 std::string proxy_server;
1217 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
1218 pc->proxy_rules.ParseFromString(proxy_server);
1219 }
1220 }
1221
1222 private:
1223 net::ProxyService* proxy_service_;
1224 std::string proxy_config_;
1225};
1226
1227
1228void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
1229 URLRequestContext* context = Profile::GetDefaultRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171230 if (!context) {
1231 FilePath user_data_dir;
1232 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1233 ProfileManager* profile_manager = g_browser_process->profile_manager();
1234 DCHECK(profile_manager);
1235 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1236 DCHECK(profile);
1237 context = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171238 }
1239 DCHECK(context);
1240 // Every URLRequestContext should have a proxy service.
1241 net::ProxyService* proxy_service = context->proxy_service();
1242 DCHECK(proxy_service);
1243
[email protected]81ae0a92009-08-06 02:16:161244 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
1245 new SetProxyConfigTask(proxy_service, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171246}
1247
[email protected]4f3dc372009-02-24 00:10:291248void AutomationProvider::GetDownloadDirectory(
1249 int handle, std::wstring* download_directory) {
initial.commit09911bf2008-07-26 23:55:291250 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291251 if (tab_tracker_->ContainsHandle(handle)) {
1252 NavigationController* tab = tab_tracker_->GetResource(handle);
1253 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1254 DCHECK(dlm);
[email protected]71f65dd2009-02-11 19:14:561255 *download_directory = dlm->download_path().ToWStringHack();
initial.commit09911bf2008-07-26 23:55:291256 }
initial.commit09911bf2008-07-26 23:55:291257}
1258
[email protected]14c0a032009-04-13 18:15:141259void AutomationProvider::OpenNewBrowserWindow(bool show,
1260 IPC::Message* reply_message) {
1261 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291262 // We may have no current browser windows open so don't rely on
1263 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]15952e462008-11-14 00:29:051264 Browser* browser = Browser::Create(profile_);
1265 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151266 if (show)
[email protected]15952e462008-11-14 00:29:051267 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291268}
1269
[email protected]71f65dd2009-02-11 19:14:561270void AutomationProvider::GetWindowForBrowser(int browser_handle,
1271 bool* success,
1272 int* handle) {
1273 *success = false;
1274 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291275
1276 if (browser_tracker_->ContainsHandle(browser_handle)) {
1277 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201278 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291279 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201280 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561281 *success = true;
initial.commit09911bf2008-07-26 23:55:291282 }
initial.commit09911bf2008-07-26 23:55:291283}
1284
[email protected]5ae5bed2009-08-21 18:52:441285#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291286void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561287 int browser_handle,
1288 bool* success,
1289 int* autocomplete_edit_handle) {
1290 *success = false;
1291 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291292
1293 if (browser_tracker_->ContainsHandle(browser_handle)) {
1294 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061295 LocationBar* loc_bar = browser->window()->GetLocationBar();
1296 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291297 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561298 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1299 *success = true;
initial.commit09911bf2008-07-26 23:55:291300 }
initial.commit09911bf2008-07-26 23:55:291301}
[email protected]5ae5bed2009-08-21 18:52:441302#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291303
[email protected]71f65dd2009-02-11 19:14:561304void AutomationProvider::ShowInterstitialPage(int tab_handle,
1305 const std::string& html_text,
1306 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291307 if (tab_tracker_->ContainsHandle(tab_handle)) {
1308 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111309 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401310
[email protected]2e028a082009-08-19 20:32:581311 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]965524b2009-04-04 21:32:401312 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341313 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401314 GURL("about:interstitial"),
1315 html_text);
1316 interstitial->Show();
1317 return;
initial.commit09911bf2008-07-26 23:55:291318 }
[email protected]71f65dd2009-02-11 19:14:561319
[email protected]457f5cf2009-08-18 16:37:521320 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1321 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561322 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291323}
1324
[email protected]71f65dd2009-02-11 19:14:561325void AutomationProvider::HideInterstitialPage(int tab_handle,
1326 bool* success) {
1327 *success = false;
[email protected]57c6a652009-05-04 07:58:341328 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1329 if (tab_contents && tab_contents->interstitial_page()) {
1330 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561331 *success = true;
initial.commit09911bf2008-07-26 23:55:291332 }
initial.commit09911bf2008-07-26 23:55:291333}
1334
[email protected]71f65dd2009-02-11 19:14:561335void AutomationProvider::CloseTab(int tab_handle,
1336 bool wait_until_closed,
1337 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291338 if (tab_tracker_->ContainsHandle(tab_handle)) {
1339 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1340 int index;
1341 Browser* browser = Browser::GetBrowserForController(controller, &index);
1342 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141343 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111344 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451345 return;
initial.commit09911bf2008-07-26 23:55:291346 }
[email protected]de246f52009-02-25 18:25:451347
1348 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
initial.commit09911bf2008-07-26 23:55:291349}
1350
[email protected]71f65dd2009-02-11 19:14:561351void AutomationProvider::CloseBrowser(int browser_handle,
1352 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291353 if (browser_tracker_->ContainsHandle(browser_handle)) {
1354 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561355 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561356 reply_message);
[email protected]f3e99e32008-07-30 04:48:391357 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291358 } else {
1359 NOTREACHED();
1360 }
1361}
1362
[email protected]71f65dd2009-02-11 19:14:561363void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1364 if (browser_tracker_->ContainsHandle(browser_handle)) {
1365 Browser* browser = browser_tracker_->GetResource(browser_handle);
1366 browser->window()->Close();
1367 } else {
1368 NOTREACHED();
1369 }
1370}
1371
[email protected]71f65dd2009-02-11 19:14:561372void AutomationProvider::NavigateInExternalTab(
1373 int handle, const GURL& url,
1374 AutomationMsg_NavigationResponseValues* status) {
1375 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291376
1377 if (tab_tracker_->ContainsHandle(handle)) {
1378 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c0588052008-10-27 23:01:501379 tab->LoadURL(url, GURL(), PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561380 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291381 }
initial.commit09911bf2008-07-26 23:55:291382}
1383
[email protected]4150ef02009-08-19 23:14:261384void AutomationProvider::NavigateExternalTabAtIndex(
1385 int handle, int navigation_index,
1386 AutomationMsg_NavigationResponseValues* status) {
1387 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1388
1389 if (tab_tracker_->ContainsHandle(handle)) {
1390 NavigationController* tab = tab_tracker_->GetResource(handle);
1391 tab->GoToIndex(navigation_index);
1392 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1393 }
1394}
1395
[email protected]71f65dd2009-02-11 19:14:561396void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1397 IPC::Message* reply_message) {
1398 if (tab_tracker_->ContainsHandle(tab_handle)) {
1399 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1400 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141401 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561402 }
1403}
1404
[email protected]71f65dd2009-02-11 19:14:561405void AutomationProvider::GetSecurityState(int handle, bool* success,
1406 SecurityStyle* security_style,
1407 int* ssl_cert_status,
1408 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291409 if (tab_tracker_->ContainsHandle(handle)) {
1410 NavigationController* tab = tab_tracker_->GetResource(handle);
1411 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561412 *success = true;
1413 *security_style = entry->ssl().security_style();
1414 *ssl_cert_status = entry->ssl().cert_status();
1415 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291416 } else {
[email protected]71f65dd2009-02-11 19:14:561417 *success = false;
1418 *security_style = SECURITY_STYLE_UNKNOWN;
1419 *ssl_cert_status = 0;
1420 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291421 }
1422}
1423
[email protected]71f65dd2009-02-11 19:14:561424void AutomationProvider::GetPageType(int handle, bool* success,
1425 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291426 if (tab_tracker_->ContainsHandle(handle)) {
1427 NavigationController* tab = tab_tracker_->GetResource(handle);
1428 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561429 *page_type = entry->page_type();
1430 *success = true;
initial.commit09911bf2008-07-26 23:55:291431 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341432 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561433 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341434 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561435 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291436 } else {
[email protected]71f65dd2009-02-11 19:14:561437 *success = false;
1438 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291439 }
1440}
1441
[email protected]71f65dd2009-02-11 19:14:561442void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1443 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291444 if (tab_tracker_->ContainsHandle(handle)) {
1445 NavigationController* tab = tab_tracker_->GetResource(handle);
1446 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071447 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401448 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471449 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341450 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291451 if (ssl_blocking_page) {
1452 if (proceed) {
[email protected]2e028a082009-08-19 20:32:581453 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]71f65dd2009-02-11 19:14:561454 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291455 return;
1456 }
1457 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521458 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1459 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561460 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291461 return;
1462 }
1463 }
1464 }
1465 // We failed.
[email protected]457f5cf2009-08-18 16:37:521466 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1467 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561468 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291469}
initial.commit09911bf2008-07-26 23:55:291470
[email protected]71f65dd2009-02-11 19:14:561471void AutomationProvider::BringBrowserToFront(int browser_handle,
1472 bool* success) {
initial.commit09911bf2008-07-26 23:55:291473 if (browser_tracker_->ContainsHandle(browser_handle)) {
1474 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061475 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561476 *success = true;
initial.commit09911bf2008-07-26 23:55:291477 } else {
[email protected]71f65dd2009-02-11 19:14:561478 *success = false;
initial.commit09911bf2008-07-26 23:55:291479 }
1480}
1481
[email protected]71f65dd2009-02-11 19:14:561482void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1483 int message_num,
1484 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291485 if (browser_tracker_->ContainsHandle(browser_handle)) {
1486 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561487 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141488 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291489 } else {
[email protected]71f65dd2009-02-11 19:14:561490 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291491 }
1492}
1493
[email protected]71f65dd2009-02-11 19:14:561494void AutomationProvider::PrintNow(int tab_handle,
1495 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441496#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571497 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341498 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1499 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291500 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571501 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141502 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341503 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571504 return;
initial.commit09911bf2008-07-26 23:55:291505 }
[email protected]71f65dd2009-02-11 19:14:561506 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1507 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441508#else
1509 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1510 NOTIMPLEMENTED();
1511#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291512}
[email protected]d301c952009-07-13 15:02:411513
[email protected]71f65dd2009-02-11 19:14:561514void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111515 const FilePath& file_name,
1516 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561517 int type,
1518 bool* success) {
initial.commit09911bf2008-07-26 23:55:291519 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561520 *success = false;
initial.commit09911bf2008-07-26 23:55:291521 return;
1522 }
1523
1524 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1525 Browser* browser = FindAndActivateTab(nav);
1526 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141527 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561528 *success = false;
initial.commit09911bf2008-07-26 23:55:291529 return;
1530 }
1531
initial.commit09911bf2008-07-26 23:55:291532 SavePackage::SavePackageType save_type =
1533 static_cast<SavePackage::SavePackageType>(type);
1534 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1535 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341536 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291537
[email protected]71f65dd2009-02-11 19:14:561538 *success = true;
initial.commit09911bf2008-07-26 23:55:291539}
1540
[email protected]5ae5bed2009-08-21 18:52:441541#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471542// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561543void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1544 bool* success,
1545 std::wstring* text) {
1546 *success = false;
initial.commit09911bf2008-07-26 23:55:291547 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561548 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521549 GetText();
[email protected]71f65dd2009-02-11 19:14:561550 *success = true;
initial.commit09911bf2008-07-26 23:55:291551 }
initial.commit09911bf2008-07-26 23:55:291552}
1553
[email protected]71f65dd2009-02-11 19:14:561554void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1555 const std::wstring& text,
1556 bool* success) {
1557 *success = false;
initial.commit09911bf2008-07-26 23:55:291558 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521559 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1560 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561561 *success = true;
initial.commit09911bf2008-07-26 23:55:291562 }
initial.commit09911bf2008-07-26 23:55:291563}
1564
1565void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561566 int autocomplete_edit_handle,
1567 bool* success,
1568 std::vector<AutocompleteMatchData>* matches) {
1569 *success = false;
initial.commit09911bf2008-07-26 23:55:291570 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271571 const AutocompleteResult& result = autocomplete_edit_tracker_->
1572 GetResource(autocomplete_edit_handle)->model()->result();
1573 for (AutocompleteResult::const_iterator i = result.begin();
1574 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561575 matches->push_back(AutocompleteMatchData(*i));
1576 *success = true;
initial.commit09911bf2008-07-26 23:55:291577 }
initial.commit09911bf2008-07-26 23:55:291578}
1579
1580void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561581 int autocomplete_edit_handle,
1582 bool* success,
1583 bool* query_in_progress) {
1584 *success = false;
1585 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291586 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561587 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521588 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561589 *success = true;
initial.commit09911bf2008-07-26 23:55:291590 }
initial.commit09911bf2008-07-26 23:55:291591}
1592
[email protected]28790922009-03-09 19:48:371593void AutomationProvider::OnMessageFromExternalHost(int handle,
1594 const std::string& message,
1595 const std::string& origin,
1596 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191597 RenderViewHost* view_host = GetViewForTab(handle);
1598 if (!view_host) {
1599 return;
[email protected]fa83e762008-08-15 21:41:391600 }
[email protected]f7a68432009-07-29 23:18:191601
1602 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1603 view_host, message, origin, target)) {
1604 // Message was diverted.
1605 return;
1606 }
1607
1608 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1609 origin, target, this, view_host, handle)) {
1610 // Message was diverted.
1611 return;
1612 }
1613
1614 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1615 // Message was diverted.
1616 return;
1617 }
1618
1619 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391620}
[email protected]a9024892009-06-16 23:13:551621
1622bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1623 const std::string& message, const std::string& origin,
1624 const std::string& target) {
1625 if (target !=
1626 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1627 return false;
1628
1629 if (origin != extension_automation_constants::kAutomationOrigin) {
1630 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1631 return false;
1632 }
1633
1634 // The message is a JSON-encoded array with two elements, both strings. The
1635 // first is the name of the event to dispatch. The second is a JSON-encoding
1636 // of the arguments specific to that event.
1637 scoped_ptr<Value> message_value(JSONReader::Read(message, false));
1638 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1639 LOG(WARNING) << "Invalid browser event specified through automation";
1640 return false;
1641 }
1642
1643 const ListValue* args = static_cast<const ListValue*>(message_value.get());
1644
1645 std::string event_name;
1646 if (!args->GetString(0, &event_name)) {
1647 LOG(WARNING) << "No browser event name specified through automation";
1648 return false;
1649 }
1650
1651 std::string json_args;
1652 if (!args->GetString(1, &json_args)) {
1653 LOG(WARNING) << "No browser event args specified through automation";
1654 return false;
1655 }
1656
[email protected]7120f132009-07-20 21:05:371657 if (profile()->GetExtensionMessageService()) {
1658 profile()->GetExtensionMessageService()->
1659 DispatchEventToRenderers(event_name.c_str(), json_args);
1660 }
[email protected]a9024892009-06-16 23:13:551661
1662 return true;
1663}
[email protected]5ae5bed2009-08-21 18:52:441664#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:391665
[email protected]57c6a652009-05-04 07:58:341666TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:571667 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:571668 if (tab_tracker_->ContainsHandle(handle)) {
1669 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:341670 if (tab)
1671 *tab = nav_controller;
1672 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:571673 }
[email protected]57c6a652009-05-04 07:58:341674 return NULL;
[email protected]20e93d12008-08-28 16:31:571675}
1676
initial.commit09911bf2008-07-26 23:55:291677TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
1678 : AutomationProvider(profile) {
1679 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:141680 registrar_.Add(this, NotificationType::SESSION_END,
1681 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:291682}
1683
1684TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:291685 BrowserList::RemoveObserver(this);
1686}
1687
1688void TestingAutomationProvider::OnChannelError() {
1689 BrowserList::CloseAllBrowsers(true);
1690 AutomationProvider::OnChannelError();
1691}
1692
1693void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
1694 // For backwards compatibility with the testing automation interface, we
1695 // want the automation provider (and hence the process) to go away when the
1696 // last browser goes away.
1697 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:291698 // If you change this, update Observer for NotificationType::SESSION_END
1699 // below.
[email protected]295039bd2008-08-15 04:32:571700 MessageLoop::current()->PostTask(FROM_HERE,
1701 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:291702 }
1703}
1704
1705void TestingAutomationProvider::Observe(NotificationType type,
1706 const NotificationSource& source,
1707 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561708 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:291709 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
1710 // before the task runs resulting in this object not being deleted. This
1711 // Release balance out the Release scheduled by OnBrowserRemoving.
1712 Release();
1713}
[email protected]295039bd2008-08-15 04:32:571714
1715void TestingAutomationProvider::OnRemoveProvider() {
1716 AutomationProviderList::GetInstance()->RemoveProvider(this);
1717}
[email protected]8a3422c92008-09-24 17:42:421718
[email protected]71f65dd2009-02-11 19:14:561719void AutomationProvider::GetSSLInfoBarCount(int handle, int* count) {
1720 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:421721 if (tab_tracker_->ContainsHandle(handle)) {
1722 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:341723 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:111724 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421725 }
[email protected]8a3422c92008-09-24 17:42:421726}
1727
[email protected]71f65dd2009-02-11 19:14:561728void AutomationProvider::ClickSSLInfoBarLink(int handle,
[email protected]8a3422c92008-09-24 17:42:421729 int info_bar_index,
[email protected]71f65dd2009-02-11 19:14:561730 bool wait_for_navigation,
1731 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421732 bool success = false;
1733 if (tab_tracker_->ContainsHandle(handle)) {
1734 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1735 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:111736 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421737 if (info_bar_index >= 0 && info_bar_index < count) {
1738 if (wait_for_navigation) {
[email protected]2e028a082009-08-19 20:32:581739 AddNavigationStatusListener(nav_controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421740 }
[email protected]eb9ba192008-12-02 02:41:341741 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:111742 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:341743 info_bar_index);
1744 if (delegate->AsConfirmInfoBarDelegate())
1745 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:421746 success = true;
1747 }
1748 }
[email protected]4f3dc372009-02-24 00:10:291749 }
[email protected]8a3422c92008-09-24 17:42:421750 if (!wait_for_navigation || !success)
[email protected]457f5cf2009-08-18 16:37:521751 AutomationMsg_ClickSSLInfoBarLink::WriteReplyParams(
1752 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:421753}
1754
[email protected]71f65dd2009-02-11 19:14:561755void AutomationProvider::GetLastNavigationTime(int handle,
1756 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:421757 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:561758 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:421759}
1760
[email protected]71f65dd2009-02-11 19:14:561761void AutomationProvider::WaitForNavigation(int handle,
1762 int64 last_navigation_time,
1763 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:251764 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:421765 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:251766
[email protected]8a3422c92008-09-24 17:42:421767 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:561768 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:521769 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
1770 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]4f3dc372009-02-24 00:10:291771 return;
[email protected]8a3422c92008-09-24 17:42:421772 }
1773
[email protected]2e028a082009-08-19 20:32:581774 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421775}
1776
[email protected]71f65dd2009-02-11 19:14:561777void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161778 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561779 int value,
1780 bool* success) {
1781 *success = false;
[email protected]8a3422c92008-09-24 17:42:421782 if (browser_tracker_->ContainsHandle(handle)) {
1783 Browser* browser = browser_tracker_->GetResource(handle);
1784 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561785 *success = true;
[email protected]8a3422c92008-09-24 17:42:421786 }
[email protected]8a3422c92008-09-24 17:42:421787}
[email protected]97fa6ce32008-12-19 01:48:161788
[email protected]71f65dd2009-02-11 19:14:561789void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161790 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561791 const std::wstring& value,
1792 bool* success) {
1793 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161794 if (browser_tracker_->ContainsHandle(handle)) {
1795 Browser* browser = browser_tracker_->GetResource(handle);
1796 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561797 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161798 }
[email protected]97fa6ce32008-12-19 01:48:161799}
1800
[email protected]71f65dd2009-02-11 19:14:561801void AutomationProvider::GetBooleanPreference(int handle,
1802 const std::wstring& name,
1803 bool* success,
1804 bool* value) {
1805 *success = false;
1806 *value = false;
[email protected]97fa6ce32008-12-19 01:48:161807 if (browser_tracker_->ContainsHandle(handle)) {
1808 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561809 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
1810 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161811 }
[email protected]97fa6ce32008-12-19 01:48:161812}
1813
[email protected]71f65dd2009-02-11 19:14:561814void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161815 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561816 bool value,
1817 bool* success) {
1818 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161819 if (browser_tracker_->ContainsHandle(handle)) {
1820 Browser* browser = browser_tracker_->GetResource(handle);
1821 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561822 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161823 }
[email protected]97fa6ce32008-12-19 01:48:161824}
1825
1826// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561827void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:401828 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:161829 if (tab_tracker_->ContainsHandle(tab_handle)) {
1830 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1831 Browser* browser = FindAndActivateTab(nav);
1832 DCHECK(browser);
1833
[email protected]57c6a652009-05-04 07:58:341834 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
1835 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:161836 }
[email protected]97fa6ce32008-12-19 01:48:161837}
1838
1839// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561840void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:401841 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:561842 bool* success) {
1843 *success = false;
[email protected]de246f52009-02-25 18:25:451844#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:161845 if (tab_tracker_->ContainsHandle(tab_handle)) {
1846 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1847 Browser* browser = FindAndActivateTab(nav);
1848 DCHECK(browser);
1849
[email protected]1fc025202009-01-20 23:03:141850 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:161851 int selected_encoding_id =
1852 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
1853 if (selected_encoding_id) {
1854 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:561855 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161856 }
1857 }
1858 }
[email protected]de246f52009-02-25 18:25:451859#else
1860 // TODO(port): Enable when encoding-related parts of Browser are ported.
1861 NOTIMPLEMENTED();
1862#endif
[email protected]97fa6ce32008-12-19 01:48:161863}
[email protected]5bcdb312009-01-07 21:43:201864
[email protected]4d434a1a2009-02-11 21:06:571865void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:201866 SavePackage::SetShouldPromptUser(should_prompt);
1867}
[email protected]87eab222009-03-13 00:47:451868
[email protected]b83e4602009-05-15 22:58:331869void AutomationProvider::SetEnableExtensionAutomation(bool automation_enabled) {
1870 AutomationExtensionFunction::SetEnabled(automation_enabled);
1871}
1872
[email protected]3753f522009-04-14 23:15:471873void AutomationProvider::GetWindowTitle(int handle, string16* text) {
1874 gfx::NativeWindow window = window_tracker_->GetResource(handle);
1875 text->assign(platform_util::GetWindowTitle(window));
1876}
[email protected]66ba4932009-06-04 19:22:131877
1878void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
1879 *count = -1; // -1 is the error code
1880 if (tab_tracker_->ContainsHandle(handle)) {
1881 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1882 TabContents* tab_contents = nav_controller->tab_contents();
1883 if (tab_contents) {
1884 BlockedPopupContainer* container =
1885 tab_contents->blocked_popup_container();
1886 if (container) {
1887 *count = static_cast<int>(container->GetBlockedPopupCount());
1888 } else {
1889 // If we don't have a container, we don't have any blocked popups to
1890 // contain!
1891 *count = 0;
1892 }
1893 }
1894 }
1895}
[email protected]f7a68432009-07-29 23:18:191896
1897void AutomationProvider::SelectAll(int tab_handle) {
1898 RenderViewHost* view = GetViewForTab(tab_handle);
1899 if (!view) {
1900 NOTREACHED();
1901 return;
1902 }
1903
1904 view->SelectAll();
1905}
1906
1907void AutomationProvider::Cut(int tab_handle) {
1908 RenderViewHost* view = GetViewForTab(tab_handle);
1909 if (!view) {
1910 NOTREACHED();
1911 return;
1912 }
1913
1914 view->Cut();
1915}
1916
1917void AutomationProvider::Copy(int tab_handle) {
1918 RenderViewHost* view = GetViewForTab(tab_handle);
1919 if (!view) {
1920 NOTREACHED();
1921 return;
1922 }
1923
1924 view->Copy();
1925}
1926
1927void AutomationProvider::Paste(int tab_handle) {
1928 RenderViewHost* view = GetViewForTab(tab_handle);
1929 if (!view) {
1930 NOTREACHED();
1931 return;
1932 }
1933
1934 view->Paste();
1935}
1936
1937void AutomationProvider::ReloadAsync(int tab_handle) {
1938 if (tab_tracker_->ContainsHandle(tab_handle)) {
1939 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1940 if (!tab) {
1941 NOTREACHED();
1942 return;
1943 }
1944
1945 tab->Reload(false);
1946 }
1947}
1948
1949void AutomationProvider::StopAsync(int tab_handle) {
1950 RenderViewHost* view = GetViewForTab(tab_handle);
1951 if (!view) {
[email protected]8b2b3312009-09-14 18:38:361952 // We tolerate StopAsync being called even before a view has been created.
1953 // So just log a warning instead of a NOTREACHED().
1954 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:191955 return;
1956 }
1957
1958 view->Stop();
1959}
1960
[email protected]2949e90d2009-08-21 15:32:521961void AutomationProvider::WaitForBrowserWindowCountToBecome(
1962 int target_count, IPC::Message* reply_message) {
1963 if (static_cast<int>(BrowserList::size()) == target_count) {
1964 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
1965 reply_message, true);
1966 Send(reply_message);
1967 return;
1968 }
1969
1970 // Set up an observer (it will delete itself).
1971 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
1972}
1973
1974void AutomationProvider::WaitForAppModalDialogToBeShown(
1975 IPC::Message* reply_message) {
1976 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
1977 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
1978 reply_message, true);
1979 Send(reply_message);
1980 return;
1981 }
1982
1983 // Set up an observer (it will delete itself).
1984 new AppModalDialogShownObserver(this, reply_message);
1985}
1986
[email protected]1126a1d32009-08-26 15:39:261987void AutomationProvider::GoBackBlockUntilNavigationsComplete(
1988 int handle, int number_of_navigations, IPC::Message* reply_message) {
1989 if (tab_tracker_->ContainsHandle(handle)) {
1990 NavigationController* tab = tab_tracker_->GetResource(handle);
1991 Browser* browser = FindAndActivateTab(tab);
1992 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
1993 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
1994 browser->GoBack(CURRENT_TAB);
1995 return;
1996 }
1997 }
1998
1999 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2000 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2001 Send(reply_message);
2002}
2003
2004void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2005 int handle, int number_of_navigations, IPC::Message* reply_message) {
2006 if (tab_tracker_->ContainsHandle(handle)) {
2007 NavigationController* tab = tab_tracker_->GetResource(handle);
2008 Browser* browser = FindAndActivateTab(tab);
2009 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
2010 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2011 browser->GoForward(CURRENT_TAB);
2012 return;
2013 }
2014 }
2015
2016 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2017 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2018 Send(reply_message);
2019}
2020
[email protected]f7a68432009-07-29 23:18:192021RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2022 if (tab_tracker_->ContainsHandle(tab_handle)) {
2023 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2024 if (!tab) {
2025 NOTREACHED();
2026 return NULL;
2027 }
2028
2029 TabContents* tab_contents = tab->tab_contents();
2030 if (!tab_contents) {
2031 NOTREACHED();
2032 return NULL;
2033 }
2034
2035 RenderViewHost* view_host = tab_contents->render_view_host();
2036 return view_host;
2037 }
2038
2039 return NULL;
2040}
[email protected]675595f2009-08-26 22:32:042041
2042void AutomationProvider::GetBrowserForWindow(int window_handle,
2043 bool* success,
2044 int* browser_handle) {
2045 *success = false;
2046 *browser_handle = 0;
2047
2048 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2049 if (!window)
2050 return;
2051
2052 BrowserList::const_iterator iter = BrowserList::begin();
2053 for (;iter != BrowserList::end(); ++iter) {
2054 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2055 if (window == this_window) {
2056 // Add() returns the existing handle for the resource if any.
2057 *browser_handle = browser_tracker_->Add(*iter);
2058 *success = true;
2059 return;
2060 }
2061 }
2062}