blob: 38b732ee724c46d6f642282e0c96fd744e663a5e [file] [log] [blame]
[email protected]5ae5bed2009-08-21 18:52:441// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/automation/automation_provider.h"
6
[email protected]5ae5bed2009-08-21 18:52:447#include <set>
8
[email protected]202e7a72009-06-15 03:48:369#include "app/l10n_util.h"
[email protected]37126212009-05-06 02:23:3110#include "app/message_box_flags.h"
[email protected]c6cb1992009-04-13 16:45:2911#include "base/file_version_info.h"
[email protected]a9024892009-06-16 23:13:5512#include "base/json_reader.h"
[email protected]bc1407f2009-09-29 00:33:3513#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3814#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2915#include "base/path_service.h"
[email protected]f44265b2009-05-19 18:52:5016#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2717#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3818#include "base/thread.h"
[email protected]a7eee32f2009-05-22 18:08:1719#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2920#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2821#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0922#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3323#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0825#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5526#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5027#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1328#include "chrome/browser/blocked_popup_container.h"
[email protected]5c238752009-06-13 10:29:0729#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3930#include "chrome/browser/browser_window.h"
initial.commit09911bf2008-07-26 23:55:2931#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1232#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5933#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2234#include "chrome/browser/download/download_shelf.h"
[email protected]a9024892009-06-16 23:13:5535#include "chrome/browser/extensions/extension_message_service.h"
[email protected]4801ecc2009-04-05 04:52:5836#include "chrome/browser/find_bar.h"
37#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2938#include "chrome/browser/find_notification_details.h"
[email protected]13869dd2009-05-05 00:40:0639#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0140#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5341#include "chrome/browser/net/url_request_mock_util.h"
[email protected]a7eee32f2009-05-22 18:08:1742#include "chrome/browser/profile_manager.h"
[email protected]6524b5f92009-01-22 17:48:2543#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0344#include "chrome/browser/ssl/ssl_manager.h"
45#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3446#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4547#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1748#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2949#include "chrome/common/chrome_paths.h"
[email protected]a7eee32f2009-05-22 18:08:1750#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1451#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4752#include "chrome/common/platform_util.h"
[email protected]1bb5f892009-10-06 01:44:5753#include "chrome/common/pref_names.h"
[email protected]8a3422c92008-09-24 17:42:4254#include "chrome/common/pref_service.h"
[email protected]71f65dd2009-02-11 19:14:5655#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5756#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1757#include "net/proxy/proxy_service.h"
58#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2159#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2860#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2961
[email protected]de246f52009-02-25 18:25:4562#if defined(OS_WIN)
63// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4564#include "chrome/browser/character_encoding.h"
65#include "chrome/browser/download/save_package.h"
66#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4567#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4568#endif // defined(OS_WIN)
69
[email protected]5ae5bed2009-08-21 18:52:4470#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2871// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2872#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4473#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0674
[email protected]e1acf6f2008-10-27 20:43:3375using base::Time;
76
[email protected]cbab76d2008-10-13 22:42:4777class AutomationInterstitialPage : public InterstitialPage {
78 public:
[email protected]57c6a652009-05-04 07:58:3479 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4780 const GURL& url,
81 const std::string& contents)
82 : InterstitialPage(tab, true, url),
83 contents_(contents) {
84 }
85
86 virtual std::string GetHTMLContents() { return contents_; }
87
88 private:
89 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:2990
[email protected]cbab76d2008-10-13 22:42:4791 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
92};
93
[email protected]c2cb8542009-08-20 21:16:5194#if !defined(OS_MACOSX)
95class ClickTask : public Task {
96 public:
[email protected]fc2e0872009-08-21 22:14:4197 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:5198 virtual ~ClickTask() {}
99
100 virtual void Run() {
101 ui_controls::MouseButton button = ui_controls::LEFT;
102 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
103 views::Event::EF_LEFT_BUTTON_DOWN) {
104 button = ui_controls::LEFT;
105 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
106 views::Event::EF_RIGHT_BUTTON_DOWN) {
107 button = ui_controls::RIGHT;
108 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
109 views::Event::EF_MIDDLE_BUTTON_DOWN) {
110 button = ui_controls::MIDDLE;
111 } else {
112 NOTREACHED();
113 }
114
[email protected]fc2e0872009-08-21 22:14:41115 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51116 }
117
118 private:
[email protected]c2cb8542009-08-20 21:16:51119 int flags_;
120
121 DISALLOW_COPY_AND_ASSIGN(ClickTask);
122};
123#endif
124
initial.commit09911bf2008-07-26 23:55:29125AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57126 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56127 profile_(profile),
128 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29129 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29130 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20131 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29132 autocomplete_edit_tracker_.reset(
133 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29134 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
135 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
initial.commit09911bf2008-07-26 23:55:29136}
137
138AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50139 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
140 port_containers_.end());
141 port_containers_.clear();
142
[email protected]0da050b92008-08-19 19:29:47143 // Make sure that any outstanding NotificationObservers also get destroyed.
144 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31145 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47146 while ((observer = it.GetNext()) != NULL)
147 delete observer;
initial.commit09911bf2008-07-26 23:55:29148}
149
[email protected]9a3a293b2009-06-04 22:28:16150void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06151 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57152 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06153 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
154 automation_resource_message_filter_,
155 g_browser_process->io_thread()->message_loop(),
156 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05157 scoped_ptr<FileVersionInfo> file_version_info(
158 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40159 std::string version_string;
160 if (file_version_info != NULL) {
161 version_string = WideToASCII(file_version_info->file_version());
162 }
[email protected]c6cb1992009-04-13 16:45:29163
164 // Send a hello message with our current automation protocol version.
165 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29166}
167
168void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
169 if (expected_tabs == 0) {
170 Send(new AutomationMsg_InitialLoadsComplete(0));
171 } else {
172 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
173 }
174}
175
initial.commit09911bf2008-07-26 23:55:29176NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58177 NavigationController* tab, IPC::Message* reply_message,
178 int number_of_navigations) {
initial.commit09911bf2008-07-26 23:55:29179 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58180 new NavigationNotificationObserver(tab, this, reply_message,
181 number_of_navigations);
initial.commit09911bf2008-07-26 23:55:29182
[email protected]71f65dd2009-02-11 19:14:56183 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29184 return observer;
185}
186
187void AutomationProvider::RemoveNavigationStatusListener(
188 NotificationObserver* obs) {
189 notification_observer_list_.RemoveObserver(obs);
190}
191
192NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14193 Browser* parent,
194 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56195 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14196 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29197 notification_observer_list_.AddObserver(observer);
198
199 return observer;
200}
201
202void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
203 notification_observer_list_.RemoveObserver(obs);
204}
205
206void AutomationProvider::AddLoginHandler(NavigationController* tab,
207 LoginHandler* handler) {
208 login_handler_map_[tab] = handler;
209}
210
211void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
212 DCHECK(login_handler_map_[tab]);
213 login_handler_map_.erase(tab);
214}
215
[email protected]f44265b2009-05-19 18:52:50216void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
217 int port_id = port->port_id();
218 DCHECK_NE(-1, port_id);
219 DCHECK(port_containers_.find(port_id) == port_containers_.end());
220
221 port_containers_[port_id] = port;
222}
223
224void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
225 int port_id = port->port_id();
226 DCHECK_NE(-1, port_id);
227
228 PortContainerMap::iterator it = port_containers_.find(port_id);
229 DCHECK(it != port_containers_.end());
230
231 if (it != port_containers_.end()) {
232 delete it->second;
233 port_containers_.erase(it);
234 }
235}
236
237ExtensionPortContainer* AutomationProvider::GetPortContainer(
238 int port_id) const {
239 PortContainerMap::const_iterator it = port_containers_.find(port_id);
240 if (it == port_containers_.end())
241 return NULL;
242
243 return it->second;
244}
245
initial.commit09911bf2008-07-26 23:55:29246int AutomationProvider::GetIndexForNavigationController(
247 const NavigationController* controller, const Browser* parent) const {
248 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12249 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29250}
251
252void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
253 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14254 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56255 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
256 CloseBrowserAsync)
257 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
258 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
259 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
260 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
261 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
262 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14263 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58264 IPC_MESSAGE_HANDLER_DELAY_REPLY(
265 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
266 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56267 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
268 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
269 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
270 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
271 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14272 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56273 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
274 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
275 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14276 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29277 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
278 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56279 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36280 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56281 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29282 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56283 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29284 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
285 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56286 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14287 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24288 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44289 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33290 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44291 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33292 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24293 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14294 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24295 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15296 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14297 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44298#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24299 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]1c58a5c2009-05-21 18:47:14300 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]5ae5bed2009-08-21 18:52:44301#endif // !defined(OS_MACOSX)
[email protected]8dd404bb2009-09-22 19:57:24302#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]71f65dd2009-02-11 19:14:56303 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
304 WindowSimulateDrag)
[email protected]8dd404bb2009-09-22 19:57:24305#endif // defined(OS_WIN) || defined(OS_LINUX)
[email protected]71f65dd2009-02-11 19:14:56306 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
307 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37308#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56309 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45310#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56311 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
312 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03313 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56314 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14315 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
initial.commit09911bf2008-07-26 23:55:29316 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14317 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56318 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
319 ExecuteJavascript)
320 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29321 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14322 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
323 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56324 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
325 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14326 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17327 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14328 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14329 OpenNewBrowserWindow)
330 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56331 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14332 GetAutocompleteEditForBrowser)
333 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45334#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29335 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17336#endif
[email protected]71f65dd2009-02-11 19:14:56337 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29338 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26339 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
340 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56341 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14342 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56343 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14344 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17345#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29346 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
347 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17348#endif
[email protected]71f65dd2009-02-11 19:14:56349 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
350 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14351 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17352#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04353 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39354 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
355 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17356#endif
[email protected]1c58a5c2009-05-21 18:47:14357 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
358 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]71f65dd2009-02-11 19:14:56359 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
360 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29361 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
362 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
363 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56364 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41365 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56366 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
367 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29368 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56369 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29370 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56371 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29372 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56373 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29374 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56375 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45376 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45377 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
378 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14379 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56380 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57381 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56382 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57383 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56384 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
385 GetBookmarkBarVisibility)
[email protected]1c58a5c2009-05-21 18:47:14386 IPC_MESSAGE_HANDLER(AutomationMsg_GetSSLInfoBarCount, GetSSLInfoBarCount)
[email protected]71f65dd2009-02-11 19:14:56387 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickSSLInfoBarLink,
388 ClickSSLInfoBarLink)
389 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42390 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56391 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
392 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14393 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56394 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44395 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56396 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20397 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14398 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56399 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16400 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56401 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16402 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56403 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16404 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14405 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20406 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
407 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14408 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]b83e4602009-05-15 22:58:33409 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
410 SetEnableExtensionAutomation)
[email protected]59560e0b2009-06-04 03:30:22411 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13412 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19413 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
414 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
415 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
416 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
417 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
418 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52419 IPC_MESSAGE_HANDLER_DELAY_REPLY(
420 AutomationMsg_WaitForBrowserWindowCountToBecome,
421 WaitForBrowserWindowCountToBecome)
422 IPC_MESSAGE_HANDLER_DELAY_REPLY(
423 AutomationMsg_WaitForAppModalDialogToBeShown,
424 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26425 IPC_MESSAGE_HANDLER_DELAY_REPLY(
426 AutomationMsg_GoBackBlockUntilNavigationsComplete,
427 GoBackBlockUntilNavigationsComplete)
428 IPC_MESSAGE_HANDLER_DELAY_REPLY(
429 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
430 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14431#if defined(OS_WIN)
432 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
433#endif
[email protected]1bb5f892009-10-06 01:44:57434 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
initial.commit09911bf2008-07-26 23:55:29435 IPC_END_MESSAGE_MAP()
436}
437
[email protected]71f65dd2009-02-11 19:14:56438void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
439 *status = -1;
initial.commit09911bf2008-07-26 23:55:29440 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
441 Browser* browser = browser_tracker_->GetResource(handle);
442 if (at_index >= 0 && at_index < browser->tab_count()) {
443 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56444 *status = 0;
initial.commit09911bf2008-07-26 23:55:29445 }
446 }
initial.commit09911bf2008-07-26 23:55:29447}
448
[email protected]71f65dd2009-02-11 19:14:56449void AutomationProvider::AppendTab(int handle, const GURL& url,
450 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29451 int append_tab_response = -1; // -1 is the error code
452 NotificationObserver* observer = NULL;
453
454 if (browser_tracker_->ContainsHandle(handle)) {
455 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14456 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58457 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
458 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39459 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29460 if (tab_contents) {
461 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57462 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29463 }
464 }
465
466 if (append_tab_response < 0) {
467 // The append tab failed. Remove the TabStripObserver
468 if (observer) {
469 RemoveTabStripObserver(observer);
470 delete observer;
471 }
472
[email protected]71f65dd2009-02-11 19:14:56473 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
474 append_tab_response);
475 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29476 }
477}
478
[email protected]71f65dd2009-02-11 19:14:56479void AutomationProvider::NavigateToURL(int handle, const GURL& url,
480 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58481 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
482}
483
484void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
485 int handle, const GURL& url, int number_of_navigations,
486 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29487 if (tab_tracker_->ContainsHandle(handle)) {
488 NavigationController* tab = tab_tracker_->GetResource(handle);
489
490 // Simulate what a user would do. Activate the tab and then navigate.
491 // We could allow navigating in a background tab in future.
492 Browser* browser = FindAndActivateTab(tab);
493
494 if (browser) {
[email protected]2e028a082009-08-19 20:32:58495 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
[email protected]71f65dd2009-02-11 19:14:56496
initial.commit09911bf2008-07-26 23:55:29497 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50498 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29499 return;
500 }
501 }
[email protected]71f65dd2009-02-11 19:14:56502
503 AutomationMsg_NavigateToURL::WriteReplyParams(
504 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
505 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29506}
[email protected]2949e90d2009-08-21 15:32:52507
[email protected]71f65dd2009-02-11 19:14:56508void AutomationProvider::NavigationAsync(int handle, const GURL& url,
509 bool* status) {
510 *status = false;
initial.commit09911bf2008-07-26 23:55:29511
512 if (tab_tracker_->ContainsHandle(handle)) {
513 NavigationController* tab = tab_tracker_->GetResource(handle);
514
515 // Simulate what a user would do. Activate the tab and then navigate.
516 // We could allow navigating in a background tab in future.
517 Browser* browser = FindAndActivateTab(tab);
518
519 if (browser) {
520 // Don't add any listener unless a callback mechanism is desired.
521 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50522 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56523 *status = true;
initial.commit09911bf2008-07-26 23:55:29524 }
525 }
initial.commit09911bf2008-07-26 23:55:29526}
527
[email protected]71f65dd2009-02-11 19:14:56528void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29529 if (tab_tracker_->ContainsHandle(handle)) {
530 NavigationController* tab = tab_tracker_->GetResource(handle);
531 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14532 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]2e028a082009-08-19 20:32:58533 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29534 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29535 return;
536 }
537 }
[email protected]71f65dd2009-02-11 19:14:56538
539 AutomationMsg_GoBack::WriteReplyParams(
540 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
541 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29542}
543
[email protected]71f65dd2009-02-11 19:14:56544void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29545 if (tab_tracker_->ContainsHandle(handle)) {
546 NavigationController* tab = tab_tracker_->GetResource(handle);
547 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14548 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]2e028a082009-08-19 20:32:58549 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29550 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29551 return;
552 }
553 }
[email protected]71f65dd2009-02-11 19:14:56554
555 AutomationMsg_GoForward::WriteReplyParams(
556 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
557 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29558}
559
[email protected]71f65dd2009-02-11 19:14:56560void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29561 if (tab_tracker_->ContainsHandle(handle)) {
562 NavigationController* tab = tab_tracker_->GetResource(handle);
563 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14564 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]2e028a082009-08-19 20:32:58565 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29566 browser->Reload();
567 return;
568 }
569 }
[email protected]71f65dd2009-02-11 19:14:56570
571 AutomationMsg_Reload::WriteReplyParams(
572 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
573 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29574}
575
[email protected]71f65dd2009-02-11 19:14:56576void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29577 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56578 const std::wstring& password,
579 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29580 if (tab_tracker_->ContainsHandle(tab_handle)) {
581 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
582 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
583
584 if (iter != login_handler_map_.end()) {
585 // If auth is needed again after this, assume login has failed. This is
586 // not strictly correct, because a navigation can require both proxy and
587 // server auth, but it should be OK for now.
588 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58589 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29590 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52591 return;
initial.commit09911bf2008-07-26 23:55:29592 }
593 }
[email protected]de246f52009-02-25 18:25:45594
[email protected]457f5cf2009-08-18 16:37:52595 AutomationMsg_SetAuth::WriteReplyParams(
596 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
597 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29598}
599
[email protected]71f65dd2009-02-11 19:14:56600void AutomationProvider::CancelAuth(int tab_handle,
601 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29602 if (tab_tracker_->ContainsHandle(tab_handle)) {
603 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
604 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
605
606 if (iter != login_handler_map_.end()) {
607 // If auth is needed again after this, something is screwy.
608 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58609 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29610 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52611 return;
initial.commit09911bf2008-07-26 23:55:29612 }
613 }
[email protected]de246f52009-02-25 18:25:45614
[email protected]457f5cf2009-08-18 16:37:52615 AutomationMsg_CancelAuth::WriteReplyParams(
616 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
617 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29618}
619
[email protected]71f65dd2009-02-11 19:14:56620void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
621 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29622
623 if (tab_tracker_->ContainsHandle(tab_handle)) {
624 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
625 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
626
627 if (iter != login_handler_map_.end()) {
628 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56629 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29630 }
631 }
initial.commit09911bf2008-07-26 23:55:29632}
633
[email protected]71f65dd2009-02-11 19:14:56634void AutomationProvider::GetRedirectsFrom(int tab_handle,
635 const GURL& source_url,
636 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29637 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
638 if (tab_tracker_->ContainsHandle(tab_handle)) {
639 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
640 HistoryService* history_service =
641 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
642
643 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
644 "has no history service";
645 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56646 DCHECK(reply_message_ == NULL);
647 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29648 // Schedule a history query for redirects. The response will be sent
649 // asynchronously from the callback the history system uses to notify us
650 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29651 redirect_query_ = history_service->QueryRedirectsFrom(
652 source_url, &consumer_,
653 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
654 return; // Response will be sent when query completes.
655 }
656 }
657
658 // Send failure response.
[email protected]deb57402009-02-06 01:35:30659 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56660 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
661 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29662}
663
[email protected]71f65dd2009-02-11 19:14:56664void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
665 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29666 if (browser_tracker_->ContainsHandle(handle)) {
667 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56668 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29669 }
initial.commit09911bf2008-07-26 23:55:29670}
671
[email protected]202e7a72009-06-15 03:48:36672void AutomationProvider::GetBrowserLocale(string16* locale) {
673 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06674 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36675}
676
[email protected]71f65dd2009-02-11 19:14:56677void AutomationProvider::GetBrowserWindowCount(int* window_count) {
678 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29679}
680
[email protected]24497032009-05-01 17:00:29681void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
682 *window_count = static_cast<int>(
683 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
684}
685
[email protected]71f65dd2009-02-11 19:14:56686void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
687 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07688 AppModalDialog* dialog_delegate =
689 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50690 *showing_dialog = (dialog_delegate != NULL);
691 if (*showing_dialog)
692 *dialog_button = dialog_delegate->GetDialogButtons();
693 else
[email protected]478ff2ed2009-04-21 23:49:18694 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20695}
696
[email protected]71f65dd2009-02-11 19:14:56697void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
698 *success = false;
[email protected]fad84eab2008-12-05 00:37:20699
[email protected]1f460072009-05-28 17:02:07700 AppModalDialog* dialog_delegate =
701 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50702 if (dialog_delegate &&
703 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18704 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
705 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28706 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56707 *success = true;
[email protected]fad84eab2008-12-05 00:37:20708 }
[email protected]478ff2ed2009-04-21 23:49:18709 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
710 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56711 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28712 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56713 *success = true;
[email protected]fad84eab2008-12-05 00:37:20714 }
715 }
[email protected]c274acc2008-11-11 20:13:44716}
717
[email protected]71f65dd2009-02-11 19:14:56718void AutomationProvider::GetBrowserWindow(int index, int* handle) {
719 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29720 if (index >= 0) {
721 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29722 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29723 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56724 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29725 }
726 }
initial.commit09911bf2008-07-26 23:55:29727}
728
[email protected]24497032009-05-01 17:00:29729void AutomationProvider::FindNormalBrowserWindow(int* handle) {
730 *handle = 0;
731 Browser* browser = BrowserList::FindBrowserWithType(profile_,
732 Browser::TYPE_NORMAL);
733 if (browser)
734 *handle = browser_tracker_->Add(browser);
735}
736
[email protected]71f65dd2009-02-11 19:14:56737void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
738 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29739 Browser* browser = BrowserList::GetLastActive();
740 if (browser)
[email protected]71f65dd2009-02-11 19:14:56741 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29742}
743
[email protected]fb05865f2009-09-21 15:05:46744#if defined(OS_LINUX) || defined(OS_MACOSX)
[email protected]9a08bcf2009-08-12 19:56:28745// TODO(estade): use this implementation for all platforms?
746void AutomationProvider::GetActiveWindow(int* handle) {
747 gfx::NativeWindow window =
748 BrowserList::GetLastActive()->window()->GetNativeHandle();
749 *handle = window_tracker_->Add(window);
750}
751#endif
752
[email protected]4f6381ee2009-04-16 02:46:33753void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
754 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56755 *success = false;
[email protected]4ae62752008-08-04 23:28:47756 if (browser_tracker_->ContainsHandle(handle)) {
757 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14758 if (browser->command_updater()->SupportsCommand(command) &&
759 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47760 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56761 *success = true;
[email protected]4ae62752008-08-04 23:28:47762 }
763 }
[email protected]4ae62752008-08-04 23:28:47764}
765
[email protected]4f6381ee2009-04-16 02:46:33766void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56767 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53768 // List of commands which just finish synchronously and don't require
769 // setting up an observer.
770 static const int kSynchronousCommands[] = {
771 IDC_HOME,
772 IDC_SELECT_NEXT_TAB,
773 IDC_SELECT_PREVIOUS_TAB,
774 IDC_SHOW_DOWNLOADS,
775 IDC_SHOW_HISTORY,
776 };
[email protected]56e71b7c2009-03-27 03:05:56777 if (browser_tracker_->ContainsHandle(handle)) {
778 Browser* browser = browser_tracker_->GetResource(handle);
779 if (browser->command_updater()->SupportsCommand(command) &&
780 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53781 // First check if we can handle the command without using an observer.
782 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
783 if (command == kSynchronousCommands[i]) {
784 browser->ExecuteCommand(command);
785 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
786 true);
787 Send(reply_message);
788 return;
789 }
790 }
791
792 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42793 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
794 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27795 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42796 return;
797 }
[email protected]56e71b7c2009-03-27 03:05:56798 }
799 }
[email protected]49a14a82009-03-31 04:16:44800 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56801 Send(reply_message);
802}
803
[email protected]fc2e0872009-08-21 22:14:41804// This task just adds another task to the event queue. This is useful if
805// you want to ensure that any tasks added to the event queue after this one
806// have already been processed by the time |task| is run.
807class InvokeTaskLaterTask : public Task {
808 public:
809 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
810 virtual ~InvokeTaskLaterTask() {}
811
812 virtual void Run() {
813 MessageLoop::current()->PostTask(FROM_HERE, task_);
814 }
815
816 private:
817 Task* task_;
818
819 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
820};
821
[email protected]fc2e0872009-08-21 22:14:41822#if defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29823void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
824 int handle,
[email protected]d1a5941e2009-08-13 23:34:24825 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29826 int flags) {
[email protected]b410bc32009-08-14 01:11:14827 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51828 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41829 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29830 }
831}
832
initial.commit09911bf2008-07-26 23:55:29833void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
834 int handle,
[email protected]bc1407f2009-09-29 00:33:35835 int key,
initial.commit09911bf2008-07-26 23:55:29836 int flags) {
[email protected]b410bc32009-08-14 01:11:14837 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29838 return;
839
[email protected]b410bc32009-08-14 01:11:14840 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29841 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35842 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38843 ((flags & views::Event::EF_CONTROL_DOWN) ==
844 views::Event::EF_CONTROL_DOWN),
845 ((flags & views::Event::EF_SHIFT_DOWN) ==
846 views::Event::EF_SHIFT_DOWN),
847 ((flags & views::Event::EF_ALT_DOWN) ==
848 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29849}
[email protected]fc2e0872009-08-21 22:14:41850#endif // defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29851
[email protected]71f65dd2009-02-11 19:14:56852void AutomationProvider::IsWindowActive(int handle, bool* success,
853 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29854 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17855 *is_active =
856 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56857 *success = true;
initial.commit09911bf2008-07-26 23:55:29858 } else {
[email protected]71f65dd2009-02-11 19:14:56859 *success = false;
860 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29861 }
862}
863
[email protected]71f65dd2009-02-11 19:14:56864void AutomationProvider::GetTabCount(int handle, int* tab_count) {
865 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29866
867 if (browser_tracker_->ContainsHandle(handle)) {
868 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56869 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29870 }
initial.commit09911bf2008-07-26 23:55:29871}
872
[email protected]71f65dd2009-02-11 19:14:56873void AutomationProvider::GetTab(int win_handle, int tab_index,
874 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56875 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29876 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
877 Browser* browser = browser_tracker_->GetResource(win_handle);
878 if (tab_index < browser->tab_count()) {
879 TabContents* tab_contents =
880 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57881 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29882 }
883 }
initial.commit09911bf2008-07-26 23:55:29884}
885
[email protected]71f65dd2009-02-11 19:14:56886void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
887 std::wstring* title) {
888 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29889 if (tab_tracker_->ContainsHandle(handle)) {
890 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36891 NavigationEntry* entry = tab->GetActiveEntry();
892 if (entry != NULL) {
893 *title = UTF16ToWideHack(entry->title());
894 } else {
895 *title = std::wstring();
896 }
[email protected]71f65dd2009-02-11 19:14:56897 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29898 }
initial.commit09911bf2008-07-26 23:55:29899}
900
[email protected]77bc6732009-04-20 22:01:03901void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
902 *tabstrip_index = -1; // -1 is the error code
903
904 if (tab_tracker_->ContainsHandle(handle)) {
905 NavigationController* tab = tab_tracker_->GetResource(handle);
906 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12907 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:03908 }
909}
910
initial.commit09911bf2008-07-26 23:55:29911void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
912 if (window_tracker_->ContainsHandle(handle)) {
913 window_tracker_->Remove(window_tracker_->GetResource(handle));
914 }
915}
916
917void AutomationProvider::OnChannelError() {
918 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:57919 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:29920}
921
922// TODO(brettw) change this to accept GURLs when history supports it
923void AutomationProvider::OnRedirectQueryComplete(
924 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:37925 GURL from_url,
initial.commit09911bf2008-07-26 23:55:29926 bool success,
[email protected]379c2b12009-07-01 21:50:33927 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:29928 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:56929 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:29930
[email protected]deb57402009-02-06 01:35:30931 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:29932 if (success) {
[email protected]71f65dd2009-02-11 19:14:56933 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:29934 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:30935 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:29936 } else {
[email protected]71f65dd2009-02-11 19:14:56937 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:29938 }
939
[email protected]4f3dc372009-02-24 00:10:29940 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:30941
[email protected]71f65dd2009-02-11 19:14:56942 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:17943 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:56944 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29945}
946
947bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:57948 DCHECK(channel_.get());
949 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:29950}
951
952Browser* AutomationProvider::FindAndActivateTab(
953 NavigationController* controller) {
954 int tab_index;
955 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
956 if (browser)
957 browser->SelectTabContentsAt(tab_index, true);
958
959 return browser;
960}
961
[email protected]71f65dd2009-02-11 19:14:56962void AutomationProvider::GetCookies(const GURL& url, int handle,
963 int* value_size,
964 std::string* value) {
965 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:29966 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
967 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56968 *value =
initial.commit09911bf2008-07-26 23:55:29969 tab->profile()->GetRequestContext()->cookie_store()->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:56970 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:29971 }
initial.commit09911bf2008-07-26 23:55:29972}
973
[email protected]71f65dd2009-02-11 19:14:56974void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:29975 const std::string value,
[email protected]71f65dd2009-02-11 19:14:56976 int handle,
977 int* response_value) {
978 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:29979
980 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
981 NavigationController* tab = tab_tracker_->GetResource(handle);
982 URLRequestContext* context = tab->profile()->GetRequestContext();
983 if (context->cookie_store()->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:56984 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:29985 }
initial.commit09911bf2008-07-26 23:55:29986}
987
[email protected]71f65dd2009-02-11 19:14:56988void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
989 *success = false;
initial.commit09911bf2008-07-26 23:55:29990 if (tab_tracker_->ContainsHandle(handle)) {
991 NavigationController* tab = tab_tracker_->GetResource(handle);
992 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:54993 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:56994 *success = true;
initial.commit09911bf2008-07-26 23:55:29995 }
initial.commit09911bf2008-07-26 23:55:29996}
997
[email protected]71f65dd2009-02-11 19:14:56998void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
999 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291000
1001 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561002 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341003 TabContents* tab_contents =
1004 tab_tracker_->GetResource(handle)->tab_contents();
1005 if (tab_contents->process())
1006 *process_id = tab_contents->process()->process().pid();
initial.commit09911bf2008-07-26 23:55:291007 }
initial.commit09911bf2008-07-26 23:55:291008}
1009
1010void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331011 NOTREACHED() << "This function has been deprecated. "
1012 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291013}
1014
[email protected]71f65dd2009-02-11 19:14:561015void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291016 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561017 const std::wstring& script,
1018 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291019 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341020 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1021 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571022 // Set the routing id of this message with the controller.
1023 // This routing id needs to be remembered for the reverse
1024 // communication while sending back the response of
1025 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331026 std::wstring set_automation_id;
1027 SStringPrintf(&set_automation_id,
1028 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561029 reply_message->routing_id());
1030
1031 DCHECK(reply_message_ == NULL);
1032 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291033
[email protected]57c6a652009-05-04 07:58:341034 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331035 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341036 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061037 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571038 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291039 }
1040
1041 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561042 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1043 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291044 }
1045}
1046
[email protected]71f65dd2009-02-11 19:14:561047void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1048 *visible = false;
[email protected]20e93d12008-08-28 16:31:571049
[email protected]59560e0b2009-06-04 03:30:221050 if (browser_tracker_->ContainsHandle(handle)) {
1051 Browser* browser = browser_tracker_->GetResource(handle);
1052 if (browser) {
1053 *visible = browser->window()->IsDownloadShelfVisible();
1054 }
1055 }
initial.commit09911bf2008-07-26 23:55:291056}
1057
[email protected]59560e0b2009-06-04 03:30:221058void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1059 if (browser_tracker_->ContainsHandle(handle)) {
1060 Browser* browser = browser_tracker_->GetResource(handle);
1061 if (browser) {
1062 if (visible)
1063 browser->window()->GetDownloadShelf()->Show();
1064 else
1065 browser->window()->GetDownloadShelf()->Close();
1066 }
1067 }
1068}
1069
1070
[email protected]71f65dd2009-02-11 19:14:561071void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1072 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291073 if (tab_tracker_->ContainsHandle(handle)) {
1074 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111075 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291076 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561077 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291078 }
1079 }
initial.commit09911bf2008-07-26 23:55:291080}
1081
initial.commit09911bf2008-07-26 23:55:291082void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561083 int handle, const std::wstring& find_request,
1084 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311085 NOTREACHED() << "This function has been deprecated."
1086 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561087 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311088 return;
1089}
1090
[email protected]4f999132009-03-31 18:08:401091void AutomationProvider::HandleFindRequest(
1092 int handle,
1093 const AutomationMsg_Find_Params& params,
1094 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291095 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561096 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1097 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291098 return;
1099 }
1100
1101 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111102 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291103
1104 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141105 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291106
[email protected]57c6a652009-05-04 07:58:341107 tab_contents->set_current_find_request_id(
1108 FindInPageNotificationObserver::kFindInPageRequestId);
1109 tab_contents->render_view_host()->StartFinding(
1110 FindInPageNotificationObserver::kFindInPageRequestId,
1111 params.search_string, params.forward, params.match_case,
1112 params.find_next);
initial.commit09911bf2008-07-26 23:55:291113}
1114
[email protected]5f8af2a2008-08-06 22:49:451115void AutomationProvider::HandleOpenFindInPageRequest(
1116 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291117 if (browser_tracker_->ContainsHandle(handle)) {
1118 Browser* browser = browser_tracker_->GetResource(handle);
1119 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451120 }
1121}
1122
[email protected]71f65dd2009-02-11 19:14:561123void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561124 *visible = false;
[email protected]855c0142009-09-28 22:35:241125 Browser* browser = browser_tracker_->GetResource(handle);
1126 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581127 FindBarTesting* find_bar =
1128 browser->find_bar()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241129 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291130 }
[email protected]20e93d12008-08-28 16:31:571131}
1132
[email protected]71f65dd2009-02-11 19:14:561133void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1134 int* y) {
[email protected]9e0534b2008-10-21 15:03:011135 gfx::Point position(0, 0);
1136 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291137 if (browser_tracker_->ContainsHandle(handle)) {
1138 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581139 FindBarTesting* find_bar =
1140 browser->find_bar()->find_bar()->GetFindBarTesting();
1141 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291142 }
[email protected]20e93d12008-08-28 16:31:571143
[email protected]71f65dd2009-02-11 19:14:561144 *x = position.x();
1145 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571146}
1147
initial.commit09911bf2008-07-26 23:55:291148void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561149 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341150 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1151 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561152 DCHECK(reply_message_ == NULL);
1153 reply_message_ = reply_message;
1154
[email protected]d9f9b792009-06-24 13:17:121155 DevToolsManager::GetInstance()->InspectElement(
1156 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291157 } else {
[email protected]71f65dd2009-02-11 19:14:561158 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1159 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291160 }
1161}
1162
1163void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091164 if (reply_message_) {
1165 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1166 num_resources);
1167 Send(reply_message_);
1168 reply_message_ = NULL;
1169 }
initial.commit09911bf2008-07-26 23:55:291170}
1171
[email protected]a7eee32f2009-05-22 18:08:171172class SetProxyConfigTask : public Task {
1173 public:
1174 explicit SetProxyConfigTask(net::ProxyService* proxy_service,
1175 const std::string& new_proxy_config)
1176 : proxy_service_(proxy_service), proxy_config_(new_proxy_config) {}
1177 virtual void Run() {
1178 // First, deserialize the JSON string. If this fails, log and bail.
1179 JSONStringValueSerializer deserializer(proxy_config_);
1180 std::string error_message;
1181 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1182 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1183 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1184 << error_message;
1185 return;
1186 }
1187
1188 scoped_ptr<DictionaryValue> dict(
1189 static_cast<DictionaryValue*>(root.release()));
1190 // Now put together a proxy configuration from the deserialized string.
1191 net::ProxyConfig pc;
1192 PopulateProxyConfig(*dict.get(), &pc);
1193
1194 DCHECK(proxy_service_);
1195 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1196 new net::ProxyConfigServiceFixed(pc));
1197 proxy_service_->ResetConfigService(proxy_config_service.release());
1198 }
1199
1200 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1201 DCHECK(pc);
1202 bool no_proxy = false;
1203 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1204 // Make no changes to the ProxyConfig.
1205 return;
1206 }
1207 bool auto_config;
1208 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
1209 pc->auto_detect = true;
1210 }
1211 std::string pac_url;
1212 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
1213 pc->pac_url = GURL(pac_url);
1214 }
1215 std::string proxy_bypass_list;
1216 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
1217 pc->ParseNoProxyList(proxy_bypass_list);
1218 }
1219 std::string proxy_server;
1220 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
1221 pc->proxy_rules.ParseFromString(proxy_server);
1222 }
1223 }
1224
1225 private:
1226 net::ProxyService* proxy_service_;
1227 std::string proxy_config_;
1228};
1229
1230
1231void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
1232 URLRequestContext* context = Profile::GetDefaultRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171233 if (!context) {
1234 FilePath user_data_dir;
1235 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1236 ProfileManager* profile_manager = g_browser_process->profile_manager();
1237 DCHECK(profile_manager);
1238 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1239 DCHECK(profile);
1240 context = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171241 }
1242 DCHECK(context);
1243 // Every URLRequestContext should have a proxy service.
1244 net::ProxyService* proxy_service = context->proxy_service();
1245 DCHECK(proxy_service);
1246
[email protected]81ae0a92009-08-06 02:16:161247 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
1248 new SetProxyConfigTask(proxy_service, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171249}
1250
[email protected]4f3dc372009-02-24 00:10:291251void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331252 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291253 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291254 if (tab_tracker_->ContainsHandle(handle)) {
1255 NavigationController* tab = tab_tracker_->GetResource(handle);
1256 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1257 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331258 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291259 }
initial.commit09911bf2008-07-26 23:55:291260}
1261
[email protected]14c0a032009-04-13 18:15:141262void AutomationProvider::OpenNewBrowserWindow(bool show,
1263 IPC::Message* reply_message) {
1264 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291265 // We may have no current browser windows open so don't rely on
1266 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]15952e462008-11-14 00:29:051267 Browser* browser = Browser::Create(profile_);
1268 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151269 if (show)
[email protected]15952e462008-11-14 00:29:051270 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291271}
1272
[email protected]71f65dd2009-02-11 19:14:561273void AutomationProvider::GetWindowForBrowser(int browser_handle,
1274 bool* success,
1275 int* handle) {
1276 *success = false;
1277 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291278
1279 if (browser_tracker_->ContainsHandle(browser_handle)) {
1280 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201281 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291282 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201283 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561284 *success = true;
initial.commit09911bf2008-07-26 23:55:291285 }
initial.commit09911bf2008-07-26 23:55:291286}
1287
[email protected]5ae5bed2009-08-21 18:52:441288#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291289void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561290 int browser_handle,
1291 bool* success,
1292 int* autocomplete_edit_handle) {
1293 *success = false;
1294 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291295
1296 if (browser_tracker_->ContainsHandle(browser_handle)) {
1297 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061298 LocationBar* loc_bar = browser->window()->GetLocationBar();
1299 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291300 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561301 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1302 *success = true;
initial.commit09911bf2008-07-26 23:55:291303 }
initial.commit09911bf2008-07-26 23:55:291304}
[email protected]5ae5bed2009-08-21 18:52:441305#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291306
[email protected]71f65dd2009-02-11 19:14:561307void AutomationProvider::ShowInterstitialPage(int tab_handle,
1308 const std::string& html_text,
1309 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291310 if (tab_tracker_->ContainsHandle(tab_handle)) {
1311 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111312 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401313
[email protected]2e028a082009-08-19 20:32:581314 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]965524b2009-04-04 21:32:401315 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341316 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401317 GURL("about:interstitial"),
1318 html_text);
1319 interstitial->Show();
1320 return;
initial.commit09911bf2008-07-26 23:55:291321 }
[email protected]71f65dd2009-02-11 19:14:561322
[email protected]457f5cf2009-08-18 16:37:521323 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1324 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561325 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291326}
1327
[email protected]71f65dd2009-02-11 19:14:561328void AutomationProvider::HideInterstitialPage(int tab_handle,
1329 bool* success) {
1330 *success = false;
[email protected]57c6a652009-05-04 07:58:341331 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1332 if (tab_contents && tab_contents->interstitial_page()) {
1333 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561334 *success = true;
initial.commit09911bf2008-07-26 23:55:291335 }
initial.commit09911bf2008-07-26 23:55:291336}
1337
[email protected]71f65dd2009-02-11 19:14:561338void AutomationProvider::CloseTab(int tab_handle,
1339 bool wait_until_closed,
1340 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291341 if (tab_tracker_->ContainsHandle(tab_handle)) {
1342 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1343 int index;
1344 Browser* browser = Browser::GetBrowserForController(controller, &index);
1345 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141346 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111347 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451348 return;
initial.commit09911bf2008-07-26 23:55:291349 }
[email protected]de246f52009-02-25 18:25:451350
1351 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:551352 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291353}
1354
[email protected]71f65dd2009-02-11 19:14:561355void AutomationProvider::CloseBrowser(int browser_handle,
1356 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291357 if (browser_tracker_->ContainsHandle(browser_handle)) {
1358 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561359 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561360 reply_message);
[email protected]f3e99e32008-07-30 04:48:391361 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291362 } else {
1363 NOTREACHED();
1364 }
1365}
1366
[email protected]71f65dd2009-02-11 19:14:561367void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1368 if (browser_tracker_->ContainsHandle(browser_handle)) {
1369 Browser* browser = browser_tracker_->GetResource(browser_handle);
1370 browser->window()->Close();
1371 } else {
1372 NOTREACHED();
1373 }
1374}
1375
[email protected]71f65dd2009-02-11 19:14:561376void AutomationProvider::NavigateInExternalTab(
1377 int handle, const GURL& url,
1378 AutomationMsg_NavigationResponseValues* status) {
1379 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291380
1381 if (tab_tracker_->ContainsHandle(handle)) {
1382 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c0588052008-10-27 23:01:501383 tab->LoadURL(url, GURL(), PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561384 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291385 }
initial.commit09911bf2008-07-26 23:55:291386}
1387
[email protected]4150ef02009-08-19 23:14:261388void AutomationProvider::NavigateExternalTabAtIndex(
1389 int handle, int navigation_index,
1390 AutomationMsg_NavigationResponseValues* status) {
1391 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1392
1393 if (tab_tracker_->ContainsHandle(handle)) {
1394 NavigationController* tab = tab_tracker_->GetResource(handle);
1395 tab->GoToIndex(navigation_index);
1396 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1397 }
1398}
1399
[email protected]71f65dd2009-02-11 19:14:561400void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1401 IPC::Message* reply_message) {
1402 if (tab_tracker_->ContainsHandle(tab_handle)) {
1403 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1404 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141405 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561406 }
1407}
1408
[email protected]71f65dd2009-02-11 19:14:561409void AutomationProvider::GetSecurityState(int handle, bool* success,
1410 SecurityStyle* security_style,
1411 int* ssl_cert_status,
1412 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291413 if (tab_tracker_->ContainsHandle(handle)) {
1414 NavigationController* tab = tab_tracker_->GetResource(handle);
1415 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561416 *success = true;
1417 *security_style = entry->ssl().security_style();
1418 *ssl_cert_status = entry->ssl().cert_status();
1419 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291420 } else {
[email protected]71f65dd2009-02-11 19:14:561421 *success = false;
1422 *security_style = SECURITY_STYLE_UNKNOWN;
1423 *ssl_cert_status = 0;
1424 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291425 }
1426}
1427
[email protected]71f65dd2009-02-11 19:14:561428void AutomationProvider::GetPageType(int handle, bool* success,
1429 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291430 if (tab_tracker_->ContainsHandle(handle)) {
1431 NavigationController* tab = tab_tracker_->GetResource(handle);
1432 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561433 *page_type = entry->page_type();
1434 *success = true;
initial.commit09911bf2008-07-26 23:55:291435 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341436 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561437 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341438 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561439 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291440 } else {
[email protected]71f65dd2009-02-11 19:14:561441 *success = false;
1442 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291443 }
1444}
1445
[email protected]71f65dd2009-02-11 19:14:561446void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1447 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291448 if (tab_tracker_->ContainsHandle(handle)) {
1449 NavigationController* tab = tab_tracker_->GetResource(handle);
1450 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071451 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401452 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471453 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341454 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291455 if (ssl_blocking_page) {
1456 if (proceed) {
[email protected]2e028a082009-08-19 20:32:581457 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]71f65dd2009-02-11 19:14:561458 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291459 return;
1460 }
1461 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521462 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1463 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561464 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291465 return;
1466 }
1467 }
1468 }
1469 // We failed.
[email protected]457f5cf2009-08-18 16:37:521470 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1471 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561472 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291473}
initial.commit09911bf2008-07-26 23:55:291474
[email protected]71f65dd2009-02-11 19:14:561475void AutomationProvider::BringBrowserToFront(int browser_handle,
1476 bool* success) {
initial.commit09911bf2008-07-26 23:55:291477 if (browser_tracker_->ContainsHandle(browser_handle)) {
1478 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061479 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561480 *success = true;
initial.commit09911bf2008-07-26 23:55:291481 } else {
[email protected]71f65dd2009-02-11 19:14:561482 *success = false;
initial.commit09911bf2008-07-26 23:55:291483 }
1484}
1485
[email protected]71f65dd2009-02-11 19:14:561486void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1487 int message_num,
1488 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291489 if (browser_tracker_->ContainsHandle(browser_handle)) {
1490 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561491 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141492 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291493 } else {
[email protected]71f65dd2009-02-11 19:14:561494 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291495 }
1496}
1497
[email protected]71f65dd2009-02-11 19:14:561498void AutomationProvider::PrintNow(int tab_handle,
1499 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441500#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571501 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341502 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1503 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291504 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571505 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141506 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341507 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571508 return;
initial.commit09911bf2008-07-26 23:55:291509 }
[email protected]71f65dd2009-02-11 19:14:561510 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1511 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441512#else
1513 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1514 NOTIMPLEMENTED();
1515#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291516}
[email protected]d301c952009-07-13 15:02:411517
[email protected]71f65dd2009-02-11 19:14:561518void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111519 const FilePath& file_name,
1520 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561521 int type,
1522 bool* success) {
initial.commit09911bf2008-07-26 23:55:291523 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561524 *success = false;
initial.commit09911bf2008-07-26 23:55:291525 return;
1526 }
1527
1528 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1529 Browser* browser = FindAndActivateTab(nav);
1530 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141531 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561532 *success = false;
initial.commit09911bf2008-07-26 23:55:291533 return;
1534 }
1535
initial.commit09911bf2008-07-26 23:55:291536 SavePackage::SavePackageType save_type =
1537 static_cast<SavePackage::SavePackageType>(type);
1538 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1539 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341540 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291541
[email protected]71f65dd2009-02-11 19:14:561542 *success = true;
initial.commit09911bf2008-07-26 23:55:291543}
1544
[email protected]5ae5bed2009-08-21 18:52:441545#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471546// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561547void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1548 bool* success,
1549 std::wstring* text) {
1550 *success = false;
initial.commit09911bf2008-07-26 23:55:291551 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561552 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521553 GetText();
[email protected]71f65dd2009-02-11 19:14:561554 *success = true;
initial.commit09911bf2008-07-26 23:55:291555 }
initial.commit09911bf2008-07-26 23:55:291556}
1557
[email protected]71f65dd2009-02-11 19:14:561558void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1559 const std::wstring& text,
1560 bool* success) {
1561 *success = false;
initial.commit09911bf2008-07-26 23:55:291562 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521563 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1564 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561565 *success = true;
initial.commit09911bf2008-07-26 23:55:291566 }
initial.commit09911bf2008-07-26 23:55:291567}
1568
1569void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561570 int autocomplete_edit_handle,
1571 bool* success,
1572 std::vector<AutocompleteMatchData>* matches) {
1573 *success = false;
initial.commit09911bf2008-07-26 23:55:291574 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271575 const AutocompleteResult& result = autocomplete_edit_tracker_->
1576 GetResource(autocomplete_edit_handle)->model()->result();
1577 for (AutocompleteResult::const_iterator i = result.begin();
1578 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561579 matches->push_back(AutocompleteMatchData(*i));
1580 *success = true;
initial.commit09911bf2008-07-26 23:55:291581 }
initial.commit09911bf2008-07-26 23:55:291582}
1583
1584void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561585 int autocomplete_edit_handle,
1586 bool* success,
1587 bool* query_in_progress) {
1588 *success = false;
1589 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291590 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561591 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521592 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561593 *success = true;
initial.commit09911bf2008-07-26 23:55:291594 }
initial.commit09911bf2008-07-26 23:55:291595}
1596
[email protected]28790922009-03-09 19:48:371597void AutomationProvider::OnMessageFromExternalHost(int handle,
1598 const std::string& message,
1599 const std::string& origin,
1600 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191601 RenderViewHost* view_host = GetViewForTab(handle);
1602 if (!view_host) {
1603 return;
[email protected]fa83e762008-08-15 21:41:391604 }
[email protected]f7a68432009-07-29 23:18:191605
1606 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1607 view_host, message, origin, target)) {
1608 // Message was diverted.
1609 return;
1610 }
1611
1612 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1613 origin, target, this, view_host, handle)) {
1614 // Message was diverted.
1615 return;
1616 }
1617
1618 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1619 // Message was diverted.
1620 return;
1621 }
1622
1623 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391624}
[email protected]a9024892009-06-16 23:13:551625
1626bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1627 const std::string& message, const std::string& origin,
1628 const std::string& target) {
1629 if (target !=
1630 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1631 return false;
1632
1633 if (origin != extension_automation_constants::kAutomationOrigin) {
1634 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1635 return false;
1636 }
1637
1638 // The message is a JSON-encoded array with two elements, both strings. The
1639 // first is the name of the event to dispatch. The second is a JSON-encoding
1640 // of the arguments specific to that event.
1641 scoped_ptr<Value> message_value(JSONReader::Read(message, false));
1642 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1643 LOG(WARNING) << "Invalid browser event specified through automation";
1644 return false;
1645 }
1646
1647 const ListValue* args = static_cast<const ListValue*>(message_value.get());
1648
1649 std::string event_name;
1650 if (!args->GetString(0, &event_name)) {
1651 LOG(WARNING) << "No browser event name specified through automation";
1652 return false;
1653 }
1654
1655 std::string json_args;
1656 if (!args->GetString(1, &json_args)) {
1657 LOG(WARNING) << "No browser event args specified through automation";
1658 return false;
1659 }
1660
[email protected]7120f132009-07-20 21:05:371661 if (profile()->GetExtensionMessageService()) {
1662 profile()->GetExtensionMessageService()->
1663 DispatchEventToRenderers(event_name.c_str(), json_args);
1664 }
[email protected]a9024892009-06-16 23:13:551665
1666 return true;
1667}
[email protected]5ae5bed2009-08-21 18:52:441668#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:391669
[email protected]57c6a652009-05-04 07:58:341670TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:571671 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:571672 if (tab_tracker_->ContainsHandle(handle)) {
1673 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:341674 if (tab)
1675 *tab = nav_controller;
1676 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:571677 }
[email protected]57c6a652009-05-04 07:58:341678 return NULL;
[email protected]20e93d12008-08-28 16:31:571679}
1680
initial.commit09911bf2008-07-26 23:55:291681TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
1682 : AutomationProvider(profile) {
1683 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:141684 registrar_.Add(this, NotificationType::SESSION_END,
1685 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:291686}
1687
1688TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:291689 BrowserList::RemoveObserver(this);
1690}
1691
1692void TestingAutomationProvider::OnChannelError() {
1693 BrowserList::CloseAllBrowsers(true);
1694 AutomationProvider::OnChannelError();
1695}
1696
1697void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
1698 // For backwards compatibility with the testing automation interface, we
1699 // want the automation provider (and hence the process) to go away when the
1700 // last browser goes away.
1701 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:291702 // If you change this, update Observer for NotificationType::SESSION_END
1703 // below.
[email protected]295039bd2008-08-15 04:32:571704 MessageLoop::current()->PostTask(FROM_HERE,
1705 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:291706 }
1707}
1708
1709void TestingAutomationProvider::Observe(NotificationType type,
1710 const NotificationSource& source,
1711 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561712 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:291713 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
1714 // before the task runs resulting in this object not being deleted. This
1715 // Release balance out the Release scheduled by OnBrowserRemoving.
1716 Release();
1717}
[email protected]295039bd2008-08-15 04:32:571718
1719void TestingAutomationProvider::OnRemoveProvider() {
1720 AutomationProviderList::GetInstance()->RemoveProvider(this);
1721}
[email protected]8a3422c92008-09-24 17:42:421722
[email protected]71f65dd2009-02-11 19:14:561723void AutomationProvider::GetSSLInfoBarCount(int handle, int* count) {
1724 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:421725 if (tab_tracker_->ContainsHandle(handle)) {
1726 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:341727 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:111728 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421729 }
[email protected]8a3422c92008-09-24 17:42:421730}
1731
[email protected]71f65dd2009-02-11 19:14:561732void AutomationProvider::ClickSSLInfoBarLink(int handle,
[email protected]8a3422c92008-09-24 17:42:421733 int info_bar_index,
[email protected]71f65dd2009-02-11 19:14:561734 bool wait_for_navigation,
1735 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421736 bool success = false;
1737 if (tab_tracker_->ContainsHandle(handle)) {
1738 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1739 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:111740 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421741 if (info_bar_index >= 0 && info_bar_index < count) {
1742 if (wait_for_navigation) {
[email protected]2e028a082009-08-19 20:32:581743 AddNavigationStatusListener(nav_controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421744 }
[email protected]eb9ba192008-12-02 02:41:341745 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:111746 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:341747 info_bar_index);
1748 if (delegate->AsConfirmInfoBarDelegate())
1749 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:421750 success = true;
1751 }
1752 }
[email protected]4f3dc372009-02-24 00:10:291753 }
[email protected]58f622a62009-10-04 01:17:551754
1755 // This "!wait_for_navigation || !success condition" logic looks suspicious.
1756 // It will send a failure message when success is true but
1757 // |wait_for_navigation| is false.
1758 // TODO(phajdan.jr): investgate whether the reply param (currently
1759 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:421760 if (!wait_for_navigation || !success)
[email protected]457f5cf2009-08-18 16:37:521761 AutomationMsg_ClickSSLInfoBarLink::WriteReplyParams(
1762 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:421763}
1764
[email protected]71f65dd2009-02-11 19:14:561765void AutomationProvider::GetLastNavigationTime(int handle,
1766 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:421767 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:561768 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:421769}
1770
[email protected]71f65dd2009-02-11 19:14:561771void AutomationProvider::WaitForNavigation(int handle,
1772 int64 last_navigation_time,
1773 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:251774 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:421775 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:251776
[email protected]8a3422c92008-09-24 17:42:421777 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:561778 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:521779 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
1780 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:551781 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:291782 return;
[email protected]8a3422c92008-09-24 17:42:421783 }
1784
[email protected]2e028a082009-08-19 20:32:581785 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421786}
1787
[email protected]71f65dd2009-02-11 19:14:561788void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161789 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561790 int value,
1791 bool* success) {
1792 *success = false;
[email protected]8a3422c92008-09-24 17:42:421793 if (browser_tracker_->ContainsHandle(handle)) {
1794 Browser* browser = browser_tracker_->GetResource(handle);
1795 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561796 *success = true;
[email protected]8a3422c92008-09-24 17:42:421797 }
[email protected]8a3422c92008-09-24 17:42:421798}
[email protected]97fa6ce32008-12-19 01:48:161799
[email protected]71f65dd2009-02-11 19:14:561800void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161801 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561802 const std::wstring& value,
1803 bool* success) {
1804 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161805 if (browser_tracker_->ContainsHandle(handle)) {
1806 Browser* browser = browser_tracker_->GetResource(handle);
1807 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561808 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161809 }
[email protected]97fa6ce32008-12-19 01:48:161810}
1811
[email protected]71f65dd2009-02-11 19:14:561812void AutomationProvider::GetBooleanPreference(int handle,
1813 const std::wstring& name,
1814 bool* success,
1815 bool* value) {
1816 *success = false;
1817 *value = false;
[email protected]97fa6ce32008-12-19 01:48:161818 if (browser_tracker_->ContainsHandle(handle)) {
1819 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561820 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
1821 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161822 }
[email protected]97fa6ce32008-12-19 01:48:161823}
1824
[email protected]71f65dd2009-02-11 19:14:561825void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161826 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561827 bool value,
1828 bool* success) {
1829 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161830 if (browser_tracker_->ContainsHandle(handle)) {
1831 Browser* browser = browser_tracker_->GetResource(handle);
1832 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561833 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161834 }
[email protected]97fa6ce32008-12-19 01:48:161835}
1836
1837// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561838void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:401839 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:161840 if (tab_tracker_->ContainsHandle(tab_handle)) {
1841 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1842 Browser* browser = FindAndActivateTab(nav);
1843 DCHECK(browser);
1844
[email protected]57c6a652009-05-04 07:58:341845 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
1846 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:161847 }
[email protected]97fa6ce32008-12-19 01:48:161848}
1849
1850// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561851void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:401852 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:561853 bool* success) {
1854 *success = false;
[email protected]de246f52009-02-25 18:25:451855#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:161856 if (tab_tracker_->ContainsHandle(tab_handle)) {
1857 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1858 Browser* browser = FindAndActivateTab(nav);
1859 DCHECK(browser);
1860
[email protected]1fc025202009-01-20 23:03:141861 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:161862 int selected_encoding_id =
1863 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
1864 if (selected_encoding_id) {
1865 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:561866 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161867 }
1868 }
1869 }
[email protected]de246f52009-02-25 18:25:451870#else
1871 // TODO(port): Enable when encoding-related parts of Browser are ported.
1872 NOTIMPLEMENTED();
1873#endif
[email protected]97fa6ce32008-12-19 01:48:161874}
[email protected]5bcdb312009-01-07 21:43:201875
[email protected]4d434a1a2009-02-11 21:06:571876void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:201877 SavePackage::SetShouldPromptUser(should_prompt);
1878}
[email protected]87eab222009-03-13 00:47:451879
[email protected]b83e4602009-05-15 22:58:331880void AutomationProvider::SetEnableExtensionAutomation(bool automation_enabled) {
1881 AutomationExtensionFunction::SetEnabled(automation_enabled);
1882}
1883
[email protected]3753f522009-04-14 23:15:471884void AutomationProvider::GetWindowTitle(int handle, string16* text) {
1885 gfx::NativeWindow window = window_tracker_->GetResource(handle);
1886 text->assign(platform_util::GetWindowTitle(window));
1887}
[email protected]66ba4932009-06-04 19:22:131888
1889void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
1890 *count = -1; // -1 is the error code
1891 if (tab_tracker_->ContainsHandle(handle)) {
1892 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1893 TabContents* tab_contents = nav_controller->tab_contents();
1894 if (tab_contents) {
1895 BlockedPopupContainer* container =
1896 tab_contents->blocked_popup_container();
1897 if (container) {
1898 *count = static_cast<int>(container->GetBlockedPopupCount());
1899 } else {
1900 // If we don't have a container, we don't have any blocked popups to
1901 // contain!
1902 *count = 0;
1903 }
1904 }
1905 }
1906}
[email protected]f7a68432009-07-29 23:18:191907
1908void AutomationProvider::SelectAll(int tab_handle) {
1909 RenderViewHost* view = GetViewForTab(tab_handle);
1910 if (!view) {
1911 NOTREACHED();
1912 return;
1913 }
1914
1915 view->SelectAll();
1916}
1917
1918void AutomationProvider::Cut(int tab_handle) {
1919 RenderViewHost* view = GetViewForTab(tab_handle);
1920 if (!view) {
1921 NOTREACHED();
1922 return;
1923 }
1924
1925 view->Cut();
1926}
1927
1928void AutomationProvider::Copy(int tab_handle) {
1929 RenderViewHost* view = GetViewForTab(tab_handle);
1930 if (!view) {
1931 NOTREACHED();
1932 return;
1933 }
1934
1935 view->Copy();
1936}
1937
1938void AutomationProvider::Paste(int tab_handle) {
1939 RenderViewHost* view = GetViewForTab(tab_handle);
1940 if (!view) {
1941 NOTREACHED();
1942 return;
1943 }
1944
1945 view->Paste();
1946}
1947
1948void AutomationProvider::ReloadAsync(int tab_handle) {
1949 if (tab_tracker_->ContainsHandle(tab_handle)) {
1950 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1951 if (!tab) {
1952 NOTREACHED();
1953 return;
1954 }
1955
1956 tab->Reload(false);
1957 }
1958}
1959
1960void AutomationProvider::StopAsync(int tab_handle) {
1961 RenderViewHost* view = GetViewForTab(tab_handle);
1962 if (!view) {
[email protected]8b2b3312009-09-14 18:38:361963 // We tolerate StopAsync being called even before a view has been created.
1964 // So just log a warning instead of a NOTREACHED().
1965 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:191966 return;
1967 }
1968
1969 view->Stop();
1970}
1971
[email protected]1bb5f892009-10-06 01:44:571972void AutomationProvider::OnSetPageFontSize(int tab_handle,
1973 int font_size) {
1974 AutomationPageFontSize automation_font_size =
1975 static_cast<AutomationPageFontSize>(font_size);
1976
1977 if (automation_font_size < SMALLEST_FONT ||
1978 automation_font_size > LARGEST_FONT) {
1979 DLOG(ERROR) << "Invalid font size specified : "
1980 << font_size;
1981 return;
1982 }
1983
1984 if (tab_tracker_->ContainsHandle(tab_handle)) {
1985 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1986 DCHECK(tab != NULL);
1987 if (tab && tab->tab_contents()) {
1988 DCHECK(tab->tab_contents()->profile() != NULL);
1989 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
1990 prefs::kWebKitDefaultFontSize, font_size);
1991 }
1992 }
1993}
1994
1995
[email protected]2949e90d2009-08-21 15:32:521996void AutomationProvider::WaitForBrowserWindowCountToBecome(
1997 int target_count, IPC::Message* reply_message) {
1998 if (static_cast<int>(BrowserList::size()) == target_count) {
1999 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2000 reply_message, true);
2001 Send(reply_message);
2002 return;
2003 }
2004
2005 // Set up an observer (it will delete itself).
2006 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2007}
2008
2009void AutomationProvider::WaitForAppModalDialogToBeShown(
2010 IPC::Message* reply_message) {
2011 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2012 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2013 reply_message, true);
2014 Send(reply_message);
2015 return;
2016 }
2017
2018 // Set up an observer (it will delete itself).
2019 new AppModalDialogShownObserver(this, reply_message);
2020}
2021
[email protected]1126a1d32009-08-26 15:39:262022void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2023 int handle, int number_of_navigations, IPC::Message* reply_message) {
2024 if (tab_tracker_->ContainsHandle(handle)) {
2025 NavigationController* tab = tab_tracker_->GetResource(handle);
2026 Browser* browser = FindAndActivateTab(tab);
2027 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
2028 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2029 browser->GoBack(CURRENT_TAB);
2030 return;
2031 }
2032 }
2033
2034 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2035 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2036 Send(reply_message);
2037}
2038
2039void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2040 int handle, int number_of_navigations, IPC::Message* reply_message) {
2041 if (tab_tracker_->ContainsHandle(handle)) {
2042 NavigationController* tab = tab_tracker_->GetResource(handle);
2043 Browser* browser = FindAndActivateTab(tab);
2044 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
2045 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2046 browser->GoForward(CURRENT_TAB);
2047 return;
2048 }
2049 }
2050
2051 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2052 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2053 Send(reply_message);
2054}
2055
[email protected]f7a68432009-07-29 23:18:192056RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2057 if (tab_tracker_->ContainsHandle(tab_handle)) {
2058 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2059 if (!tab) {
2060 NOTREACHED();
2061 return NULL;
2062 }
2063
2064 TabContents* tab_contents = tab->tab_contents();
2065 if (!tab_contents) {
2066 NOTREACHED();
2067 return NULL;
2068 }
2069
2070 RenderViewHost* view_host = tab_contents->render_view_host();
2071 return view_host;
2072 }
2073
2074 return NULL;
2075}
[email protected]675595f2009-08-26 22:32:042076
2077void AutomationProvider::GetBrowserForWindow(int window_handle,
2078 bool* success,
2079 int* browser_handle) {
2080 *success = false;
2081 *browser_handle = 0;
2082
2083 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2084 if (!window)
2085 return;
2086
2087 BrowserList::const_iterator iter = BrowserList::begin();
2088 for (;iter != BrowserList::end(); ++iter) {
2089 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2090 if (window == this_window) {
2091 // Add() returns the existing handle for the resource if any.
2092 *browser_handle = browser_tracker_->Add(*iter);
2093 *success = true;
2094 return;
2095 }
2096 }
2097}