blob: 82bd20b711622104876d2bd5935cbdd2760356e8 [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]93d49d72009-10-23 20:00:2012#include "base/json/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]201b2732009-11-13 18:57:4616#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5017#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2718#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3819#include "base/thread.h"
[email protected]a7eee32f2009-05-22 18:08:1720#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2921#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2822#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0923#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3324#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0826#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5527#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5028#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1329#include "chrome/browser/blocked_popup_container.h"
[email protected]5c238752009-06-13 10:29:0730#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3931#include "chrome/browser/browser_window.h"
[email protected]fae20792009-10-28 20:31:5832#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2933#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1234#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5935#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2236#include "chrome/browser/download/download_shelf.h"
[email protected]d11c8e92009-10-20 23:26:4037#include "chrome/browser/extensions/crx_installer.h"
38#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5539#include "chrome/browser/extensions/extension_message_service.h"
[email protected]4801ecc2009-04-05 04:52:5840#include "chrome/browser/find_bar.h"
41#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2942#include "chrome/browser/find_notification_details.h"
[email protected]0ac83682010-01-22 17:46:2743#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0644#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0145#include "chrome/browser/login_prompt.h"
[email protected]be180c802009-10-23 06:33:3146#include "chrome/browser/net/url_request_context_getter.h"
[email protected]f732c1e2009-07-30 15:48:5347#include "chrome/browser/net/url_request_mock_util.h"
[email protected]a7eee32f2009-05-22 18:08:1748#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0749#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2550#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0351#include "chrome/browser/ssl/ssl_manager.h"
52#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3453#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4554#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1755#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2956#include "chrome/common/chrome_paths.h"
[email protected]a7eee32f2009-05-22 18:08:1757#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1458#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4759#include "chrome/common/platform_util.h"
[email protected]1bb5f892009-10-06 01:44:5760#include "chrome/common/pref_names.h"
[email protected]8a3422c92008-09-24 17:42:4261#include "chrome/common/pref_service.h"
[email protected]f5bf8ccf2010-02-05 18:19:2562#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5663#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5764#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1765#include "net/proxy/proxy_service.h"
66#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2167#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2868#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2969
[email protected]de246f52009-02-25 18:25:4570#if defined(OS_WIN)
71// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4572#include "chrome/browser/character_encoding.h"
73#include "chrome/browser/download/save_package.h"
74#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4575#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4576#endif // defined(OS_WIN)
77
[email protected]5ae5bed2009-08-21 18:52:4478#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2879// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2880#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4481#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0682
[email protected]e1acf6f2008-10-27 20:43:3383using base::Time;
84
[email protected]cbab76d2008-10-13 22:42:4785class AutomationInterstitialPage : public InterstitialPage {
86 public:
[email protected]57c6a652009-05-04 07:58:3487 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4788 const GURL& url,
89 const std::string& contents)
90 : InterstitialPage(tab, true, url),
91 contents_(contents) {
92 }
93
94 virtual std::string GetHTMLContents() { return contents_; }
95
96 private:
97 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:2998
[email protected]cbab76d2008-10-13 22:42:4799 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
100};
101
[email protected]c2cb8542009-08-20 21:16:51102#if !defined(OS_MACOSX)
103class ClickTask : public Task {
104 public:
[email protected]fc2e0872009-08-21 22:14:41105 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51106 virtual ~ClickTask() {}
107
108 virtual void Run() {
109 ui_controls::MouseButton button = ui_controls::LEFT;
110 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
111 views::Event::EF_LEFT_BUTTON_DOWN) {
112 button = ui_controls::LEFT;
113 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
114 views::Event::EF_RIGHT_BUTTON_DOWN) {
115 button = ui_controls::RIGHT;
116 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
117 views::Event::EF_MIDDLE_BUTTON_DOWN) {
118 button = ui_controls::MIDDLE;
119 } else {
120 NOTREACHED();
121 }
122
[email protected]fc2e0872009-08-21 22:14:41123 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51124 }
125
126 private:
[email protected]c2cb8542009-08-20 21:16:51127 int flags_;
128
129 DISALLOW_COPY_AND_ASSIGN(ClickTask);
130};
131#endif
132
initial.commit09911bf2008-07-26 23:55:29133AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57134 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56135 profile_(profile),
136 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29137 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29138 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20139 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29140 autocomplete_edit_tracker_.reset(
141 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29142 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
143 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44144 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]528211a2010-01-14 15:25:13145 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29146}
147
148AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50149 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
150 port_containers_.end());
151 port_containers_.clear();
152
[email protected]0da050b92008-08-19 19:29:47153 // Make sure that any outstanding NotificationObservers also get destroyed.
154 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31155 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47156 while ((observer = it.GetNext()) != NULL)
157 delete observer;
[email protected]528211a2010-01-14 15:25:13158
159 if (channel_.get()) {
160 channel_->Close();
161 }
162 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29163}
164
[email protected]9a3a293b2009-06-04 22:28:16165void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06166 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57167 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06168 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
169 automation_resource_message_filter_,
170 g_browser_process->io_thread()->message_loop(),
171 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05172 scoped_ptr<FileVersionInfo> file_version_info(
173 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40174 std::string version_string;
175 if (file_version_info != NULL) {
176 version_string = WideToASCII(file_version_info->file_version());
177 }
[email protected]c6cb1992009-04-13 16:45:29178
179 // Send a hello message with our current automation protocol version.
180 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29181}
182
183void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
184 if (expected_tabs == 0) {
185 Send(new AutomationMsg_InitialLoadsComplete(0));
186 } else {
187 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
188 }
189}
190
191NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58192 NavigationController* tab, IPC::Message* reply_message,
193 int number_of_navigations) {
initial.commit09911bf2008-07-26 23:55:29194 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58195 new NavigationNotificationObserver(tab, this, reply_message,
196 number_of_navigations);
initial.commit09911bf2008-07-26 23:55:29197
[email protected]71f65dd2009-02-11 19:14:56198 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29199 return observer;
200}
201
202void AutomationProvider::RemoveNavigationStatusListener(
203 NotificationObserver* obs) {
204 notification_observer_list_.RemoveObserver(obs);
205}
206
207NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14208 Browser* parent,
209 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56210 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14211 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29212 notification_observer_list_.AddObserver(observer);
213
214 return observer;
215}
216
217void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
218 notification_observer_list_.RemoveObserver(obs);
219}
220
221void AutomationProvider::AddLoginHandler(NavigationController* tab,
222 LoginHandler* handler) {
223 login_handler_map_[tab] = handler;
224}
225
226void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
227 DCHECK(login_handler_map_[tab]);
228 login_handler_map_.erase(tab);
229}
230
[email protected]f44265b2009-05-19 18:52:50231void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
232 int port_id = port->port_id();
233 DCHECK_NE(-1, port_id);
234 DCHECK(port_containers_.find(port_id) == port_containers_.end());
235
236 port_containers_[port_id] = port;
237}
238
239void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
240 int port_id = port->port_id();
241 DCHECK_NE(-1, port_id);
242
243 PortContainerMap::iterator it = port_containers_.find(port_id);
244 DCHECK(it != port_containers_.end());
245
246 if (it != port_containers_.end()) {
247 delete it->second;
248 port_containers_.erase(it);
249 }
250}
251
252ExtensionPortContainer* AutomationProvider::GetPortContainer(
253 int port_id) const {
254 PortContainerMap::const_iterator it = port_containers_.find(port_id);
255 if (it == port_containers_.end())
256 return NULL;
257
258 return it->second;
259}
260
initial.commit09911bf2008-07-26 23:55:29261int AutomationProvider::GetIndexForNavigationController(
262 const NavigationController* controller, const Browser* parent) const {
263 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12264 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29265}
266
267void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
268 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14269 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56270 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
271 CloseBrowserAsync)
272 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
273 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
274 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
275 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
276 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
277 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14278 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58279 IPC_MESSAGE_HANDLER_DELAY_REPLY(
280 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
281 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56282 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
283 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
284 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
285 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
286 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14287 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56288 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
289 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
290 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14291 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29292 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
293 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56294 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36295 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56296 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29297 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56298 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29299 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
300 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56301 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14302 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24303 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44304 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33305 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44306 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33307 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24308 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14309 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24310 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15311 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14312 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44313#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24314 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35315 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14316 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]71f65dd2009-02-11 19:14:56317 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
318 WindowSimulateDrag)
[email protected]b2aa3ed72010-02-01 18:37:14319#endif
[email protected]71f65dd2009-02-11 19:14:56320 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53321 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56322 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37323#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56324 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45325#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56326 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
327 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03328 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56329 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14330 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09331 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
332 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
333 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29334 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14335 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56336 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
337 ExecuteJavascript)
338 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29339 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14340 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
341 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56342 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
343 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14344 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17345 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14346 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14347 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53348 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
349 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14350 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56351 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14352 GetAutocompleteEditForBrowser)
353 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45354#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29355 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17356#endif
[email protected]71f65dd2009-02-11 19:14:56357 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29358 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26359 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
360 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56361 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14362 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56363 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14364 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17365#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29366 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
367 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17368#endif
[email protected]71f65dd2009-02-11 19:14:56369 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
370 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14371 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17372#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04373 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39374 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
375 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17376#endif
[email protected]1c58a5c2009-05-21 18:47:14377 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
378 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44379 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
380 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56381 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
382 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29383 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
384 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
385 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56386 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41387 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56388 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
389 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29390 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56391 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29392 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56393 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29394 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56395 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29396 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56397 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45398 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45399 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
400 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14401 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56402 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57403 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56404 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57405 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56406 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
407 GetBookmarkBarVisibility)
[email protected]816633a2009-11-11 21:48:18408 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
409 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
410 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56411 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42412 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56413 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
414 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14415 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56416 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44417 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56418 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20419 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14420 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56421 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16422 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56423 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16424 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56425 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16426 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14427 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20428 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
429 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14430 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]4cfc1d922009-11-08 14:02:58431#if defined(OS_WIN)
432 // Depends on ExternalTabContainer, so Windows-only
[email protected]b83e4602009-05-15 22:58:33433 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
434 SetEnableExtensionAutomation)
[email protected]4cfc1d922009-11-08 14:02:58435#endif
[email protected]59560e0b2009-06-04 03:30:22436 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13437 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19438 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
439 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
440 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
441 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
442 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
443 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52444 IPC_MESSAGE_HANDLER_DELAY_REPLY(
445 AutomationMsg_WaitForBrowserWindowCountToBecome,
446 WaitForBrowserWindowCountToBecome)
447 IPC_MESSAGE_HANDLER_DELAY_REPLY(
448 AutomationMsg_WaitForAppModalDialogToBeShown,
449 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26450 IPC_MESSAGE_HANDLER_DELAY_REPLY(
451 AutomationMsg_GoBackBlockUntilNavigationsComplete,
452 GoBackBlockUntilNavigationsComplete)
453 IPC_MESSAGE_HANDLER_DELAY_REPLY(
454 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
455 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14456#if defined(OS_WIN)
457 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
458#endif
[email protected]1bb5f892009-10-06 01:44:57459 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40460 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
461 InstallExtension)
462 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
463 LoadExpandedExtension)
[email protected]fedaa7d2010-01-26 20:34:57464 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
465 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00466 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
initial.commit09911bf2008-07-26 23:55:29467 IPC_END_MESSAGE_MAP()
468}
469
[email protected]71f65dd2009-02-11 19:14:56470void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
471 *status = -1;
initial.commit09911bf2008-07-26 23:55:29472 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
473 Browser* browser = browser_tracker_->GetResource(handle);
474 if (at_index >= 0 && at_index < browser->tab_count()) {
475 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56476 *status = 0;
initial.commit09911bf2008-07-26 23:55:29477 }
478 }
initial.commit09911bf2008-07-26 23:55:29479}
480
[email protected]71f65dd2009-02-11 19:14:56481void AutomationProvider::AppendTab(int handle, const GURL& url,
482 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29483 int append_tab_response = -1; // -1 is the error code
484 NotificationObserver* observer = NULL;
485
486 if (browser_tracker_->ContainsHandle(handle)) {
487 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14488 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58489 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
490 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39491 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29492 if (tab_contents) {
493 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57494 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29495 }
496 }
497
498 if (append_tab_response < 0) {
499 // The append tab failed. Remove the TabStripObserver
500 if (observer) {
501 RemoveTabStripObserver(observer);
502 delete observer;
503 }
504
[email protected]71f65dd2009-02-11 19:14:56505 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
506 append_tab_response);
507 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29508 }
509}
510
[email protected]71f65dd2009-02-11 19:14:56511void AutomationProvider::NavigateToURL(int handle, const GURL& url,
512 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58513 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
514}
515
516void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
517 int handle, const GURL& url, int number_of_navigations,
518 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29519 if (tab_tracker_->ContainsHandle(handle)) {
520 NavigationController* tab = tab_tracker_->GetResource(handle);
521
522 // Simulate what a user would do. Activate the tab and then navigate.
523 // We could allow navigating in a background tab in future.
524 Browser* browser = FindAndActivateTab(tab);
525
526 if (browser) {
[email protected]2e028a082009-08-19 20:32:58527 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
[email protected]71f65dd2009-02-11 19:14:56528
initial.commit09911bf2008-07-26 23:55:29529 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50530 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29531 return;
532 }
533 }
[email protected]71f65dd2009-02-11 19:14:56534
535 AutomationMsg_NavigateToURL::WriteReplyParams(
536 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
537 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29538}
[email protected]2949e90d2009-08-21 15:32:52539
[email protected]71f65dd2009-02-11 19:14:56540void AutomationProvider::NavigationAsync(int handle, const GURL& url,
541 bool* status) {
542 *status = false;
initial.commit09911bf2008-07-26 23:55:29543
544 if (tab_tracker_->ContainsHandle(handle)) {
545 NavigationController* tab = tab_tracker_->GetResource(handle);
546
547 // Simulate what a user would do. Activate the tab and then navigate.
548 // We could allow navigating in a background tab in future.
549 Browser* browser = FindAndActivateTab(tab);
550
551 if (browser) {
552 // Don't add any listener unless a callback mechanism is desired.
553 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50554 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56555 *status = true;
initial.commit09911bf2008-07-26 23:55:29556 }
557 }
initial.commit09911bf2008-07-26 23:55:29558}
559
[email protected]71f65dd2009-02-11 19:14:56560void AutomationProvider::GoBack(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_BACK)) {
[email protected]2e028a082009-08-19 20:32:58565 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29566 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29567 return;
568 }
569 }
[email protected]71f65dd2009-02-11 19:14:56570
571 AutomationMsg_GoBack::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::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29577 if (tab_tracker_->ContainsHandle(handle)) {
578 NavigationController* tab = tab_tracker_->GetResource(handle);
579 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14580 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]2e028a082009-08-19 20:32:58581 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29582 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29583 return;
584 }
585 }
[email protected]71f65dd2009-02-11 19:14:56586
587 AutomationMsg_GoForward::WriteReplyParams(
588 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
589 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29590}
591
[email protected]71f65dd2009-02-11 19:14:56592void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29593 if (tab_tracker_->ContainsHandle(handle)) {
594 NavigationController* tab = tab_tracker_->GetResource(handle);
595 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14596 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]2e028a082009-08-19 20:32:58597 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29598 browser->Reload();
599 return;
600 }
601 }
[email protected]71f65dd2009-02-11 19:14:56602
603 AutomationMsg_Reload::WriteReplyParams(
604 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
605 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29606}
607
[email protected]71f65dd2009-02-11 19:14:56608void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29609 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56610 const std::wstring& password,
611 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29612 if (tab_tracker_->ContainsHandle(tab_handle)) {
613 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
614 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
615
616 if (iter != login_handler_map_.end()) {
617 // If auth is needed again after this, assume login has failed. This is
618 // not strictly correct, because a navigation can require both proxy and
619 // server auth, but it should be OK for now.
620 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58621 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29622 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52623 return;
initial.commit09911bf2008-07-26 23:55:29624 }
625 }
[email protected]de246f52009-02-25 18:25:45626
[email protected]457f5cf2009-08-18 16:37:52627 AutomationMsg_SetAuth::WriteReplyParams(
628 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
629 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29630}
631
[email protected]71f65dd2009-02-11 19:14:56632void AutomationProvider::CancelAuth(int tab_handle,
633 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29634 if (tab_tracker_->ContainsHandle(tab_handle)) {
635 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
636 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
637
638 if (iter != login_handler_map_.end()) {
639 // If auth is needed again after this, something is screwy.
640 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58641 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29642 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52643 return;
initial.commit09911bf2008-07-26 23:55:29644 }
645 }
[email protected]de246f52009-02-25 18:25:45646
[email protected]457f5cf2009-08-18 16:37:52647 AutomationMsg_CancelAuth::WriteReplyParams(
648 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
649 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29650}
651
[email protected]71f65dd2009-02-11 19:14:56652void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
653 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29654
655 if (tab_tracker_->ContainsHandle(tab_handle)) {
656 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
657 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
658
659 if (iter != login_handler_map_.end()) {
660 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56661 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29662 }
663 }
initial.commit09911bf2008-07-26 23:55:29664}
665
[email protected]71f65dd2009-02-11 19:14:56666void AutomationProvider::GetRedirectsFrom(int tab_handle,
667 const GURL& source_url,
668 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29669 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
670 if (tab_tracker_->ContainsHandle(tab_handle)) {
671 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
672 HistoryService* history_service =
673 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
674
675 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
676 "has no history service";
677 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56678 DCHECK(reply_message_ == NULL);
679 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29680 // Schedule a history query for redirects. The response will be sent
681 // asynchronously from the callback the history system uses to notify us
682 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29683 redirect_query_ = history_service->QueryRedirectsFrom(
684 source_url, &consumer_,
685 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
686 return; // Response will be sent when query completes.
687 }
688 }
689
690 // Send failure response.
[email protected]deb57402009-02-06 01:35:30691 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56692 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
693 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29694}
695
[email protected]71f65dd2009-02-11 19:14:56696void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
697 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29698 if (browser_tracker_->ContainsHandle(handle)) {
699 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56700 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29701 }
initial.commit09911bf2008-07-26 23:55:29702}
703
[email protected]202e7a72009-06-15 03:48:36704void AutomationProvider::GetBrowserLocale(string16* locale) {
705 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06706 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36707}
708
[email protected]71f65dd2009-02-11 19:14:56709void AutomationProvider::GetBrowserWindowCount(int* window_count) {
710 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29711}
712
[email protected]24497032009-05-01 17:00:29713void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
714 *window_count = static_cast<int>(
715 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
716}
717
[email protected]71f65dd2009-02-11 19:14:56718void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
719 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07720 AppModalDialog* dialog_delegate =
721 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50722 *showing_dialog = (dialog_delegate != NULL);
723 if (*showing_dialog)
724 *dialog_button = dialog_delegate->GetDialogButtons();
725 else
[email protected]478ff2ed2009-04-21 23:49:18726 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20727}
728
[email protected]71f65dd2009-02-11 19:14:56729void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
730 *success = false;
[email protected]fad84eab2008-12-05 00:37:20731
[email protected]1f460072009-05-28 17:02:07732 AppModalDialog* dialog_delegate =
733 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50734 if (dialog_delegate &&
735 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18736 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
737 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28738 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56739 *success = true;
[email protected]fad84eab2008-12-05 00:37:20740 }
[email protected]478ff2ed2009-04-21 23:49:18741 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
742 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56743 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28744 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56745 *success = true;
[email protected]fad84eab2008-12-05 00:37:20746 }
747 }
[email protected]c274acc2008-11-11 20:13:44748}
749
[email protected]fedaa7d2010-01-26 20:34:57750void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
751 if (browser_tracker_->ContainsHandle(handle)) {
752 Browser* browser = browser_tracker_->GetResource(handle);
753 browser->profile()->ShutdownSessionService();
754 *result = true;
755 } else {
756 *result = false;
757 }
758}
759
[email protected]71f65dd2009-02-11 19:14:56760void AutomationProvider::GetBrowserWindow(int index, int* handle) {
761 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29762 if (index >= 0) {
763 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29764 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29765 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56766 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29767 }
768 }
initial.commit09911bf2008-07-26 23:55:29769}
770
[email protected]24497032009-05-01 17:00:29771void AutomationProvider::FindNormalBrowserWindow(int* handle) {
772 *handle = 0;
773 Browser* browser = BrowserList::FindBrowserWithType(profile_,
774 Browser::TYPE_NORMAL);
775 if (browser)
776 *handle = browser_tracker_->Add(browser);
777}
778
[email protected]71f65dd2009-02-11 19:14:56779void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
780 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29781 Browser* browser = BrowserList::GetLastActive();
782 if (browser)
[email protected]71f65dd2009-02-11 19:14:56783 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29784}
785
[email protected]b2aa3ed72010-02-01 18:37:14786#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28787// TODO(estade): use this implementation for all platforms?
788void AutomationProvider::GetActiveWindow(int* handle) {
789 gfx::NativeWindow window =
790 BrowserList::GetLastActive()->window()->GetNativeHandle();
791 *handle = window_tracker_->Add(window);
792}
793#endif
794
[email protected]4f6381ee2009-04-16 02:46:33795void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
796 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56797 *success = false;
[email protected]4ae62752008-08-04 23:28:47798 if (browser_tracker_->ContainsHandle(handle)) {
799 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14800 if (browser->command_updater()->SupportsCommand(command) &&
801 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47802 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56803 *success = true;
[email protected]4ae62752008-08-04 23:28:47804 }
805 }
[email protected]4ae62752008-08-04 23:28:47806}
807
[email protected]4f6381ee2009-04-16 02:46:33808void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56809 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53810 // List of commands which just finish synchronously and don't require
811 // setting up an observer.
812 static const int kSynchronousCommands[] = {
813 IDC_HOME,
814 IDC_SELECT_NEXT_TAB,
815 IDC_SELECT_PREVIOUS_TAB,
816 IDC_SHOW_DOWNLOADS,
817 IDC_SHOW_HISTORY,
818 };
[email protected]56e71b7c2009-03-27 03:05:56819 if (browser_tracker_->ContainsHandle(handle)) {
820 Browser* browser = browser_tracker_->GetResource(handle);
821 if (browser->command_updater()->SupportsCommand(command) &&
822 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53823 // First check if we can handle the command without using an observer.
824 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
825 if (command == kSynchronousCommands[i]) {
826 browser->ExecuteCommand(command);
827 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
828 true);
829 Send(reply_message);
830 return;
831 }
832 }
833
834 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42835 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
836 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27837 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42838 return;
839 }
[email protected]56e71b7c2009-03-27 03:05:56840 }
841 }
[email protected]49a14a82009-03-31 04:16:44842 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56843 Send(reply_message);
844}
845
[email protected]fc2e0872009-08-21 22:14:41846// This task just adds another task to the event queue. This is useful if
847// you want to ensure that any tasks added to the event queue after this one
848// have already been processed by the time |task| is run.
849class InvokeTaskLaterTask : public Task {
850 public:
851 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
852 virtual ~InvokeTaskLaterTask() {}
853
854 virtual void Run() {
855 MessageLoop::current()->PostTask(FROM_HERE, task_);
856 }
857
858 private:
859 Task* task_;
860
861 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
862};
863
[email protected]b2aa3ed72010-02-01 18:37:14864#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29865void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
866 int handle,
[email protected]d1a5941e2009-08-13 23:34:24867 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29868 int flags) {
[email protected]b410bc32009-08-14 01:11:14869 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51870 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41871 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29872 }
873}
874
[email protected]60507b12009-11-02 23:51:35875void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
876 int handle,
877 const gfx::Point& location) {
878 if (window_tracker_->ContainsHandle(handle))
879 ui_controls::SendMouseMove(location.x(), location.y());
880}
881
initial.commit09911bf2008-07-26 23:55:29882void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
883 int handle,
[email protected]bc1407f2009-09-29 00:33:35884 int key,
initial.commit09911bf2008-07-26 23:55:29885 int flags) {
[email protected]b410bc32009-08-14 01:11:14886 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29887 return;
888
[email protected]b410bc32009-08-14 01:11:14889 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29890 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35891 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38892 ((flags & views::Event::EF_CONTROL_DOWN) ==
893 views::Event::EF_CONTROL_DOWN),
894 ((flags & views::Event::EF_SHIFT_DOWN) ==
895 views::Event::EF_SHIFT_DOWN),
896 ((flags & views::Event::EF_ALT_DOWN) ==
897 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29898}
[email protected]b2aa3ed72010-02-01 18:37:14899#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29900
[email protected]71f65dd2009-02-11 19:14:56901void AutomationProvider::IsWindowActive(int handle, bool* success,
902 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29903 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17904 *is_active =
905 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56906 *success = true;
initial.commit09911bf2008-07-26 23:55:29907 } else {
[email protected]71f65dd2009-02-11 19:14:56908 *success = false;
909 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29910 }
911}
912
[email protected]71f65dd2009-02-11 19:14:56913void AutomationProvider::GetTabCount(int handle, int* tab_count) {
914 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29915
916 if (browser_tracker_->ContainsHandle(handle)) {
917 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56918 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29919 }
initial.commit09911bf2008-07-26 23:55:29920}
921
[email protected]982921f12009-10-27 21:43:53922void AutomationProvider::GetType(int handle, int* type_as_int) {
923 *type_as_int = -1; // -1 is the error code
924
925 if (browser_tracker_->ContainsHandle(handle)) {
926 Browser* browser = browser_tracker_->GetResource(handle);
927 *type_as_int = static_cast<int>(browser->type());
928 }
929}
930
[email protected]71f65dd2009-02-11 19:14:56931void AutomationProvider::GetTab(int win_handle, int tab_index,
932 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56933 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29934 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
935 Browser* browser = browser_tracker_->GetResource(win_handle);
936 if (tab_index < browser->tab_count()) {
937 TabContents* tab_contents =
938 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57939 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29940 }
941 }
initial.commit09911bf2008-07-26 23:55:29942}
943
[email protected]71f65dd2009-02-11 19:14:56944void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
945 std::wstring* title) {
946 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29947 if (tab_tracker_->ContainsHandle(handle)) {
948 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36949 NavigationEntry* entry = tab->GetActiveEntry();
950 if (entry != NULL) {
951 *title = UTF16ToWideHack(entry->title());
952 } else {
953 *title = std::wstring();
954 }
[email protected]71f65dd2009-02-11 19:14:56955 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29956 }
initial.commit09911bf2008-07-26 23:55:29957}
958
[email protected]77bc6732009-04-20 22:01:03959void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
960 *tabstrip_index = -1; // -1 is the error code
961
962 if (tab_tracker_->ContainsHandle(handle)) {
963 NavigationController* tab = tab_tracker_->GetResource(handle);
964 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12965 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:03966 }
967}
968
initial.commit09911bf2008-07-26 23:55:29969void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
970 if (window_tracker_->ContainsHandle(handle)) {
971 window_tracker_->Remove(window_tracker_->GetResource(handle));
972 }
973}
974
975void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:16976 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:57977 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:29978}
979
980// TODO(brettw) change this to accept GURLs when history supports it
981void AutomationProvider::OnRedirectQueryComplete(
982 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:37983 GURL from_url,
initial.commit09911bf2008-07-26 23:55:29984 bool success,
[email protected]379c2b12009-07-01 21:50:33985 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:29986 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:56987 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:29988
[email protected]deb57402009-02-06 01:35:30989 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:29990 if (success) {
[email protected]71f65dd2009-02-11 19:14:56991 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:29992 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:30993 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:29994 } else {
[email protected]71f65dd2009-02-11 19:14:56995 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:29996 }
997
[email protected]4f3dc372009-02-24 00:10:29998 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:30999
[email protected]71f65dd2009-02-11 19:14:561000 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:171001 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561002 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291003}
1004
1005bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571006 DCHECK(channel_.get());
1007 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291008}
1009
1010Browser* AutomationProvider::FindAndActivateTab(
1011 NavigationController* controller) {
1012 int tab_index;
1013 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1014 if (browser)
1015 browser->SelectTabContentsAt(tab_index, true);
1016
1017 return browser;
1018}
1019
[email protected]71f65dd2009-02-11 19:14:561020void AutomationProvider::GetCookies(const GURL& url, int handle,
1021 int* value_size,
1022 std::string* value) {
1023 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291024 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1025 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311026
1027 // Since we are running on the UI thread don't call GetURLRequestContext().
1028 net::CookieStore* cookie_store =
1029 tab->profile()->GetRequestContext()->GetCookieStore();
1030
1031 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561032 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291033 }
initial.commit09911bf2008-07-26 23:55:291034}
1035
[email protected]71f65dd2009-02-11 19:14:561036void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291037 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561038 int handle,
1039 int* response_value) {
1040 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291041
1042 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1043 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311044
[email protected]dfa46e5f2009-11-17 18:48:431045 scoped_refptr<URLRequestContextGetter> request_context =
1046 tab->tab_contents()->request_context();
1047 if (!request_context.get())
1048 request_context = tab->profile()->GetRequestContext();
1049
[email protected]be180c802009-10-23 06:33:311050 // Since we are running on the UI thread don't call GetURLRequestContext().
1051 scoped_refptr<net::CookieStore> cookie_store =
[email protected]dfa46e5f2009-11-17 18:48:431052 request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311053
1054 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561055 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291056 }
initial.commit09911bf2008-07-26 23:55:291057}
1058
[email protected]71f65dd2009-02-11 19:14:561059void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1060 *success = false;
initial.commit09911bf2008-07-26 23:55:291061 if (tab_tracker_->ContainsHandle(handle)) {
1062 NavigationController* tab = tab_tracker_->GetResource(handle);
1063 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541064 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561065 *success = true;
initial.commit09911bf2008-07-26 23:55:291066 }
initial.commit09911bf2008-07-26 23:55:291067}
1068
[email protected]71f65dd2009-02-11 19:14:561069void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1070 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291071
1072 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561073 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341074 TabContents* tab_contents =
1075 tab_tracker_->GetResource(handle)->tab_contents();
1076 if (tab_contents->process())
[email protected]201b2732009-11-13 18:57:461077 *process_id = base::GetProcId(tab_contents->process()->GetHandle());
initial.commit09911bf2008-07-26 23:55:291078 }
initial.commit09911bf2008-07-26 23:55:291079}
1080
1081void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331082 NOTREACHED() << "This function has been deprecated. "
1083 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291084}
1085
[email protected]71f65dd2009-02-11 19:14:561086void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291087 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561088 const std::wstring& script,
1089 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291090 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341091 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1092 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571093 // Set the routing id of this message with the controller.
1094 // This routing id needs to be remembered for the reverse
1095 // communication while sending back the response of
1096 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331097 std::wstring set_automation_id;
1098 SStringPrintf(&set_automation_id,
1099 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561100 reply_message->routing_id());
1101
1102 DCHECK(reply_message_ == NULL);
1103 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291104
[email protected]57c6a652009-05-04 07:58:341105 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331106 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341107 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061108 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571109 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291110 }
1111
1112 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561113 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1114 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291115 }
1116}
1117
[email protected]71f65dd2009-02-11 19:14:561118void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1119 *visible = false;
[email protected]20e93d12008-08-28 16:31:571120
[email protected]59560e0b2009-06-04 03:30:221121 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251122#if defined(OS_CHROMEOS)
1123 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1124 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1125 for (BrowserList::const_iterator it = BrowserList::begin();
1126 it != BrowserList::end(); ++it) {
1127 if ((*it)->type() == Browser::TYPE_POPUP) {
1128 const GURL& url =
1129 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1130
1131 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1132 url.host() == chrome::kChromeUIFileBrowseHost) {
1133 *visible = true;
1134 break;
1135 }
1136 }
1137 }
1138#else
[email protected]59560e0b2009-06-04 03:30:221139 Browser* browser = browser_tracker_->GetResource(handle);
1140 if (browser) {
1141 *visible = browser->window()->IsDownloadShelfVisible();
1142 }
[email protected]f5bf8ccf2010-02-05 18:19:251143#endif
[email protected]59560e0b2009-06-04 03:30:221144 }
initial.commit09911bf2008-07-26 23:55:291145}
1146
[email protected]59560e0b2009-06-04 03:30:221147void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1148 if (browser_tracker_->ContainsHandle(handle)) {
1149 Browser* browser = browser_tracker_->GetResource(handle);
1150 if (browser) {
1151 if (visible)
1152 browser->window()->GetDownloadShelf()->Show();
1153 else
1154 browser->window()->GetDownloadShelf()->Close();
1155 }
1156 }
1157}
1158
[email protected]34930432009-11-09 00:12:091159void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1160 *visible = false;
1161
1162 if (browser_tracker_->ContainsHandle(handle)) {
1163 Browser* browser = browser_tracker_->GetResource(handle);
1164 if (browser)
1165 *visible = browser->window()->IsFullscreen();
1166 }
1167}
1168
1169void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1170 bool* visible) {
1171 *visible = false;
1172
1173 if (browser_tracker_->ContainsHandle(handle)) {
1174 Browser* browser = browser_tracker_->GetResource(handle);
1175 if (browser)
1176 *visible = browser->window()->IsFullscreenBubbleVisible();
1177 }
1178}
[email protected]59560e0b2009-06-04 03:30:221179
[email protected]71f65dd2009-02-11 19:14:561180void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1181 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291182 if (tab_tracker_->ContainsHandle(handle)) {
1183 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111184 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291185 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561186 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291187 }
1188 }
initial.commit09911bf2008-07-26 23:55:291189}
1190
initial.commit09911bf2008-07-26 23:55:291191void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561192 int handle, const std::wstring& find_request,
1193 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311194 NOTREACHED() << "This function has been deprecated."
1195 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561196 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311197 return;
1198}
1199
[email protected]4f999132009-03-31 18:08:401200void AutomationProvider::HandleFindRequest(
1201 int handle,
1202 const AutomationMsg_Find_Params& params,
1203 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291204 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561205 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1206 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291207 return;
1208 }
1209
1210 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111211 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291212
1213 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141214 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291215
[email protected]57c6a652009-05-04 07:58:341216 tab_contents->set_current_find_request_id(
1217 FindInPageNotificationObserver::kFindInPageRequestId);
1218 tab_contents->render_view_host()->StartFinding(
1219 FindInPageNotificationObserver::kFindInPageRequestId,
1220 params.search_string, params.forward, params.match_case,
1221 params.find_next);
initial.commit09911bf2008-07-26 23:55:291222}
1223
[email protected]5f8af2a2008-08-06 22:49:451224void AutomationProvider::HandleOpenFindInPageRequest(
1225 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291226 if (browser_tracker_->ContainsHandle(handle)) {
1227 Browser* browser = browser_tracker_->GetResource(handle);
1228 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451229 }
1230}
1231
[email protected]71f65dd2009-02-11 19:14:561232void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561233 *visible = false;
[email protected]855c0142009-09-28 22:35:241234 Browser* browser = browser_tracker_->GetResource(handle);
1235 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581236 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171237 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241238 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291239 }
[email protected]20e93d12008-08-28 16:31:571240}
1241
[email protected]71f65dd2009-02-11 19:14:561242void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1243 int* y) {
[email protected]9e0534b2008-10-21 15:03:011244 gfx::Point position(0, 0);
1245 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291246 if (browser_tracker_->ContainsHandle(handle)) {
1247 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581248 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171249 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581250 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291251 }
[email protected]20e93d12008-08-28 16:31:571252
[email protected]71f65dd2009-02-11 19:14:561253 *x = position.x();
1254 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571255}
1256
initial.commit09911bf2008-07-26 23:55:291257void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561258 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341259 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1260 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561261 DCHECK(reply_message_ == NULL);
1262 reply_message_ = reply_message;
1263
[email protected]d9f9b792009-06-24 13:17:121264 DevToolsManager::GetInstance()->InspectElement(
1265 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291266 } else {
[email protected]71f65dd2009-02-11 19:14:561267 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1268 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291269 }
1270}
1271
1272void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091273 if (reply_message_) {
1274 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1275 num_resources);
1276 Send(reply_message_);
1277 reply_message_ = NULL;
1278 }
initial.commit09911bf2008-07-26 23:55:291279}
1280
[email protected]a7eee32f2009-05-22 18:08:171281class SetProxyConfigTask : public Task {
1282 public:
[email protected]be180c802009-10-23 06:33:311283 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
1284 const std::string& new_proxy_config)
1285 : request_context_getter_(request_context_getter), proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:171286 virtual void Run() {
1287 // First, deserialize the JSON string. If this fails, log and bail.
1288 JSONStringValueSerializer deserializer(proxy_config_);
1289 std::string error_message;
1290 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1291 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1292 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1293 << error_message;
1294 return;
1295 }
1296
1297 scoped_ptr<DictionaryValue> dict(
1298 static_cast<DictionaryValue*>(root.release()));
1299 // Now put together a proxy configuration from the deserialized string.
1300 net::ProxyConfig pc;
1301 PopulateProxyConfig(*dict.get(), &pc);
1302
[email protected]be180c802009-10-23 06:33:311303 net::ProxyService* proxy_service =
1304 request_context_getter_->GetURLRequestContext()->proxy_service();
1305 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:171306 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1307 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:311308 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:171309 }
1310
1311 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1312 DCHECK(pc);
1313 bool no_proxy = false;
1314 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1315 // Make no changes to the ProxyConfig.
1316 return;
1317 }
1318 bool auto_config;
1319 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
1320 pc->auto_detect = true;
1321 }
1322 std::string pac_url;
1323 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
1324 pc->pac_url = GURL(pac_url);
1325 }
1326 std::string proxy_bypass_list;
1327 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
1328 pc->ParseNoProxyList(proxy_bypass_list);
1329 }
1330 std::string proxy_server;
1331 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
1332 pc->proxy_rules.ParseFromString(proxy_server);
1333 }
1334 }
1335
1336 private:
[email protected]be180c802009-10-23 06:33:311337 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:171338 std::string proxy_config_;
1339};
1340
1341
1342void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:311343 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
1344 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:171345 FilePath user_data_dir;
1346 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1347 ProfileManager* profile_manager = g_browser_process->profile_manager();
1348 DCHECK(profile_manager);
1349 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1350 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:311351 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171352 }
[email protected]be180c802009-10-23 06:33:311353 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:171354
[email protected]fae20792009-10-28 20:31:581355 ChromeThread::PostTask(
1356 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:311357 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171358}
1359
[email protected]4f3dc372009-02-24 00:10:291360void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331361 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291362 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291363 if (tab_tracker_->ContainsHandle(handle)) {
1364 NavigationController* tab = tab_tracker_->GetResource(handle);
1365 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1366 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331367 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291368 }
initial.commit09911bf2008-07-26 23:55:291369}
1370
[email protected]6a5670d22009-10-27 16:21:341371void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:141372 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:531373 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
1374 reply_message);
1375}
1376
1377void AutomationProvider::OpenNewBrowserWindowOfType(
1378 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:141379 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291380 // We may have no current browser windows open so don't rely on
1381 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:531382 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
1383 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:051384 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151385 if (show)
[email protected]15952e462008-11-14 00:29:051386 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291387}
1388
[email protected]71f65dd2009-02-11 19:14:561389void AutomationProvider::GetWindowForBrowser(int browser_handle,
1390 bool* success,
1391 int* handle) {
1392 *success = false;
1393 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291394
1395 if (browser_tracker_->ContainsHandle(browser_handle)) {
1396 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201397 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291398 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201399 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561400 *success = true;
initial.commit09911bf2008-07-26 23:55:291401 }
initial.commit09911bf2008-07-26 23:55:291402}
1403
[email protected]5ae5bed2009-08-21 18:52:441404#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291405void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561406 int browser_handle,
1407 bool* success,
1408 int* autocomplete_edit_handle) {
1409 *success = false;
1410 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291411
1412 if (browser_tracker_->ContainsHandle(browser_handle)) {
1413 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061414 LocationBar* loc_bar = browser->window()->GetLocationBar();
1415 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291416 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561417 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1418 *success = true;
initial.commit09911bf2008-07-26 23:55:291419 }
initial.commit09911bf2008-07-26 23:55:291420}
[email protected]5ae5bed2009-08-21 18:52:441421#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291422
[email protected]71f65dd2009-02-11 19:14:561423void AutomationProvider::ShowInterstitialPage(int tab_handle,
1424 const std::string& html_text,
1425 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291426 if (tab_tracker_->ContainsHandle(tab_handle)) {
1427 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111428 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401429
[email protected]2e028a082009-08-19 20:32:581430 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]965524b2009-04-04 21:32:401431 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341432 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401433 GURL("about:interstitial"),
1434 html_text);
1435 interstitial->Show();
1436 return;
initial.commit09911bf2008-07-26 23:55:291437 }
[email protected]71f65dd2009-02-11 19:14:561438
[email protected]457f5cf2009-08-18 16:37:521439 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1440 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561441 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291442}
1443
[email protected]71f65dd2009-02-11 19:14:561444void AutomationProvider::HideInterstitialPage(int tab_handle,
1445 bool* success) {
1446 *success = false;
[email protected]57c6a652009-05-04 07:58:341447 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1448 if (tab_contents && tab_contents->interstitial_page()) {
1449 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561450 *success = true;
initial.commit09911bf2008-07-26 23:55:291451 }
initial.commit09911bf2008-07-26 23:55:291452}
1453
[email protected]71f65dd2009-02-11 19:14:561454void AutomationProvider::CloseTab(int tab_handle,
1455 bool wait_until_closed,
1456 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291457 if (tab_tracker_->ContainsHandle(tab_handle)) {
1458 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1459 int index;
1460 Browser* browser = Browser::GetBrowserForController(controller, &index);
1461 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141462 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111463 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451464 return;
initial.commit09911bf2008-07-26 23:55:291465 }
[email protected]de246f52009-02-25 18:25:451466
1467 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:551468 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291469}
1470
[email protected]71f65dd2009-02-11 19:14:561471void AutomationProvider::CloseBrowser(int browser_handle,
1472 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291473 if (browser_tracker_->ContainsHandle(browser_handle)) {
1474 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561475 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561476 reply_message);
[email protected]f3e99e32008-07-30 04:48:391477 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291478 } else {
1479 NOTREACHED();
1480 }
1481}
1482
[email protected]71f65dd2009-02-11 19:14:561483void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1484 if (browser_tracker_->ContainsHandle(browser_handle)) {
1485 Browser* browser = browser_tracker_->GetResource(browser_handle);
1486 browser->window()->Close();
1487 } else {
1488 NOTREACHED();
1489 }
1490}
1491
[email protected]71f65dd2009-02-11 19:14:561492void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:571493 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:561494 AutomationMsg_NavigationResponseValues* status) {
1495 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291496
1497 if (tab_tracker_->ContainsHandle(handle)) {
1498 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:571499 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561500 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291501 }
initial.commit09911bf2008-07-26 23:55:291502}
1503
[email protected]4150ef02009-08-19 23:14:261504void AutomationProvider::NavigateExternalTabAtIndex(
1505 int handle, int navigation_index,
1506 AutomationMsg_NavigationResponseValues* status) {
1507 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1508
1509 if (tab_tracker_->ContainsHandle(handle)) {
1510 NavigationController* tab = tab_tracker_->GetResource(handle);
1511 tab->GoToIndex(navigation_index);
1512 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1513 }
1514}
1515
[email protected]71f65dd2009-02-11 19:14:561516void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1517 IPC::Message* reply_message) {
1518 if (tab_tracker_->ContainsHandle(tab_handle)) {
1519 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1520 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141521 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561522 }
1523}
1524
[email protected]71f65dd2009-02-11 19:14:561525void AutomationProvider::GetSecurityState(int handle, bool* success,
1526 SecurityStyle* security_style,
1527 int* ssl_cert_status,
1528 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291529 if (tab_tracker_->ContainsHandle(handle)) {
1530 NavigationController* tab = tab_tracker_->GetResource(handle);
1531 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561532 *success = true;
1533 *security_style = entry->ssl().security_style();
1534 *ssl_cert_status = entry->ssl().cert_status();
1535 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291536 } else {
[email protected]71f65dd2009-02-11 19:14:561537 *success = false;
1538 *security_style = SECURITY_STYLE_UNKNOWN;
1539 *ssl_cert_status = 0;
1540 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291541 }
1542}
1543
[email protected]71f65dd2009-02-11 19:14:561544void AutomationProvider::GetPageType(int handle, bool* success,
1545 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291546 if (tab_tracker_->ContainsHandle(handle)) {
1547 NavigationController* tab = tab_tracker_->GetResource(handle);
1548 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561549 *page_type = entry->page_type();
1550 *success = true;
initial.commit09911bf2008-07-26 23:55:291551 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341552 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561553 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341554 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561555 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291556 } else {
[email protected]71f65dd2009-02-11 19:14:561557 *success = false;
1558 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291559 }
1560}
1561
[email protected]84abba62009-10-07 17:01:441562void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
1563 int* duration_ms) {
1564 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
1565 event_name);
1566}
1567
[email protected]71f65dd2009-02-11 19:14:561568void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1569 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291570 if (tab_tracker_->ContainsHandle(handle)) {
1571 NavigationController* tab = tab_tracker_->GetResource(handle);
1572 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071573 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401574 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471575 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341576 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291577 if (ssl_blocking_page) {
1578 if (proceed) {
[email protected]2e028a082009-08-19 20:32:581579 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]71f65dd2009-02-11 19:14:561580 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291581 return;
1582 }
1583 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521584 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1585 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561586 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291587 return;
1588 }
1589 }
1590 }
1591 // We failed.
[email protected]457f5cf2009-08-18 16:37:521592 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1593 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561594 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291595}
1596
[email protected]71f65dd2009-02-11 19:14:561597void AutomationProvider::BringBrowserToFront(int browser_handle,
1598 bool* success) {
initial.commit09911bf2008-07-26 23:55:291599 if (browser_tracker_->ContainsHandle(browser_handle)) {
1600 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061601 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561602 *success = true;
initial.commit09911bf2008-07-26 23:55:291603 } else {
[email protected]71f65dd2009-02-11 19:14:561604 *success = false;
initial.commit09911bf2008-07-26 23:55:291605 }
1606}
1607
[email protected]71f65dd2009-02-11 19:14:561608void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1609 int message_num,
1610 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291611 if (browser_tracker_->ContainsHandle(browser_handle)) {
1612 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561613 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141614 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291615 } else {
[email protected]71f65dd2009-02-11 19:14:561616 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291617 }
1618}
1619
[email protected]71f65dd2009-02-11 19:14:561620void AutomationProvider::PrintNow(int tab_handle,
1621 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441622#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571623 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341624 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1625 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291626 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571627 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141628 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341629 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571630 return;
initial.commit09911bf2008-07-26 23:55:291631 }
[email protected]71f65dd2009-02-11 19:14:561632 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1633 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441634#else
1635 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1636 NOTIMPLEMENTED();
1637#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291638}
[email protected]d301c952009-07-13 15:02:411639
[email protected]71f65dd2009-02-11 19:14:561640void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111641 const FilePath& file_name,
1642 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561643 int type,
1644 bool* success) {
initial.commit09911bf2008-07-26 23:55:291645 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561646 *success = false;
initial.commit09911bf2008-07-26 23:55:291647 return;
1648 }
1649
1650 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1651 Browser* browser = FindAndActivateTab(nav);
1652 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141653 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561654 *success = false;
initial.commit09911bf2008-07-26 23:55:291655 return;
1656 }
1657
initial.commit09911bf2008-07-26 23:55:291658 SavePackage::SavePackageType save_type =
1659 static_cast<SavePackage::SavePackageType>(type);
1660 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1661 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341662 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291663
[email protected]71f65dd2009-02-11 19:14:561664 *success = true;
initial.commit09911bf2008-07-26 23:55:291665}
1666
[email protected]5ae5bed2009-08-21 18:52:441667#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471668// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561669void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1670 bool* success,
1671 std::wstring* text) {
1672 *success = false;
initial.commit09911bf2008-07-26 23:55:291673 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561674 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521675 GetText();
[email protected]71f65dd2009-02-11 19:14:561676 *success = true;
initial.commit09911bf2008-07-26 23:55:291677 }
initial.commit09911bf2008-07-26 23:55:291678}
1679
[email protected]71f65dd2009-02-11 19:14:561680void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1681 const std::wstring& text,
1682 bool* success) {
1683 *success = false;
initial.commit09911bf2008-07-26 23:55:291684 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521685 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1686 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561687 *success = true;
initial.commit09911bf2008-07-26 23:55:291688 }
initial.commit09911bf2008-07-26 23:55:291689}
1690
1691void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561692 int autocomplete_edit_handle,
1693 bool* success,
1694 std::vector<AutocompleteMatchData>* matches) {
1695 *success = false;
initial.commit09911bf2008-07-26 23:55:291696 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271697 const AutocompleteResult& result = autocomplete_edit_tracker_->
1698 GetResource(autocomplete_edit_handle)->model()->result();
1699 for (AutocompleteResult::const_iterator i = result.begin();
1700 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561701 matches->push_back(AutocompleteMatchData(*i));
1702 *success = true;
initial.commit09911bf2008-07-26 23:55:291703 }
initial.commit09911bf2008-07-26 23:55:291704}
1705
1706void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561707 int autocomplete_edit_handle,
1708 bool* success,
1709 bool* query_in_progress) {
1710 *success = false;
1711 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291712 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561713 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521714 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561715 *success = true;
initial.commit09911bf2008-07-26 23:55:291716 }
initial.commit09911bf2008-07-26 23:55:291717}
1718
[email protected]28790922009-03-09 19:48:371719void AutomationProvider::OnMessageFromExternalHost(int handle,
1720 const std::string& message,
1721 const std::string& origin,
1722 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191723 RenderViewHost* view_host = GetViewForTab(handle);
1724 if (!view_host) {
1725 return;
[email protected]fa83e762008-08-15 21:41:391726 }
[email protected]f7a68432009-07-29 23:18:191727
1728 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1729 view_host, message, origin, target)) {
1730 // Message was diverted.
1731 return;
1732 }
1733
1734 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1735 origin, target, this, view_host, handle)) {
1736 // Message was diverted.
1737 return;
1738 }
1739
1740 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1741 // Message was diverted.
1742 return;
1743 }
1744
1745 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391746}
[email protected]a9024892009-06-16 23:13:551747
1748bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1749 const std::string& message, const std::string& origin,
1750 const std::string& target) {
1751 if (target !=
1752 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1753 return false;
1754
1755 if (origin != extension_automation_constants::kAutomationOrigin) {
1756 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1757 return false;
1758 }
1759
1760 // The message is a JSON-encoded array with two elements, both strings. The
1761 // first is the name of the event to dispatch. The second is a JSON-encoding
1762 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:201763 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:551764 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1765 LOG(WARNING) << "Invalid browser event specified through automation";
1766 return false;
1767 }
1768
1769 const ListValue* args = static_cast<const ListValue*>(message_value.get());
1770
1771 std::string event_name;
1772 if (!args->GetString(0, &event_name)) {
1773 LOG(WARNING) << "No browser event name specified through automation";
1774 return false;
1775 }
1776
1777 std::string json_args;
1778 if (!args->GetString(1, &json_args)) {
1779 LOG(WARNING) << "No browser event args specified through automation";
1780 return false;
1781 }
1782
[email protected]7120f132009-07-20 21:05:371783 if (profile()->GetExtensionMessageService()) {
1784 profile()->GetExtensionMessageService()->
1785 DispatchEventToRenderers(event_name.c_str(), json_args);
1786 }
[email protected]a9024892009-06-16 23:13:551787
1788 return true;
1789}
[email protected]5ae5bed2009-08-21 18:52:441790#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:391791
[email protected]57c6a652009-05-04 07:58:341792TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:571793 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:571794 if (tab_tracker_->ContainsHandle(handle)) {
1795 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:341796 if (tab)
1797 *tab = nav_controller;
1798 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:571799 }
[email protected]57c6a652009-05-04 07:58:341800 return NULL;
[email protected]20e93d12008-08-28 16:31:571801}
1802
initial.commit09911bf2008-07-26 23:55:291803TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
1804 : AutomationProvider(profile) {
1805 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:141806 registrar_.Add(this, NotificationType::SESSION_END,
1807 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:291808}
1809
1810TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:291811 BrowserList::RemoveObserver(this);
1812}
1813
1814void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:141815 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:291816 AutomationProvider::OnChannelError();
1817}
1818
1819void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
1820 // For backwards compatibility with the testing automation interface, we
1821 // want the automation provider (and hence the process) to go away when the
1822 // last browser goes away.
1823 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:291824 // If you change this, update Observer for NotificationType::SESSION_END
1825 // below.
[email protected]295039bd2008-08-15 04:32:571826 MessageLoop::current()->PostTask(FROM_HERE,
1827 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:291828 }
1829}
1830
1831void TestingAutomationProvider::Observe(NotificationType type,
1832 const NotificationSource& source,
1833 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561834 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:291835 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
1836 // before the task runs resulting in this object not being deleted. This
1837 // Release balance out the Release scheduled by OnBrowserRemoving.
1838 Release();
1839}
[email protected]295039bd2008-08-15 04:32:571840
1841void TestingAutomationProvider::OnRemoveProvider() {
1842 AutomationProviderList::GetInstance()->RemoveProvider(this);
1843}
[email protected]8a3422c92008-09-24 17:42:421844
[email protected]816633a2009-11-11 21:48:181845void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:561846 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:421847 if (tab_tracker_->ContainsHandle(handle)) {
1848 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:341849 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:111850 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421851 }
[email protected]8a3422c92008-09-24 17:42:421852}
1853
[email protected]816633a2009-11-11 21:48:181854void AutomationProvider::ClickInfoBarAccept(int handle,
1855 int info_bar_index,
1856 bool wait_for_navigation,
1857 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421858 bool success = false;
1859 if (tab_tracker_->ContainsHandle(handle)) {
1860 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1861 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:111862 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421863 if (info_bar_index >= 0 && info_bar_index < count) {
1864 if (wait_for_navigation) {
[email protected]2e028a082009-08-19 20:32:581865 AddNavigationStatusListener(nav_controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421866 }
[email protected]eb9ba192008-12-02 02:41:341867 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:111868 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:341869 info_bar_index);
1870 if (delegate->AsConfirmInfoBarDelegate())
1871 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:421872 success = true;
1873 }
1874 }
[email protected]4f3dc372009-02-24 00:10:291875 }
[email protected]58f622a62009-10-04 01:17:551876
1877 // This "!wait_for_navigation || !success condition" logic looks suspicious.
1878 // It will send a failure message when success is true but
1879 // |wait_for_navigation| is false.
1880 // TODO(phajdan.jr): investgate whether the reply param (currently
1881 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:421882 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:181883 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:521884 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:421885}
1886
[email protected]71f65dd2009-02-11 19:14:561887void AutomationProvider::GetLastNavigationTime(int handle,
1888 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:421889 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:561890 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:421891}
1892
[email protected]71f65dd2009-02-11 19:14:561893void AutomationProvider::WaitForNavigation(int handle,
1894 int64 last_navigation_time,
1895 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:251896 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:421897 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:251898
[email protected]8a3422c92008-09-24 17:42:421899 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:561900 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:521901 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
1902 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:551903 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:291904 return;
[email protected]8a3422c92008-09-24 17:42:421905 }
1906
[email protected]2e028a082009-08-19 20:32:581907 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421908}
1909
[email protected]71f65dd2009-02-11 19:14:561910void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161911 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561912 int value,
1913 bool* success) {
1914 *success = false;
[email protected]8a3422c92008-09-24 17:42:421915 if (browser_tracker_->ContainsHandle(handle)) {
1916 Browser* browser = browser_tracker_->GetResource(handle);
1917 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561918 *success = true;
[email protected]8a3422c92008-09-24 17:42:421919 }
[email protected]8a3422c92008-09-24 17:42:421920}
[email protected]97fa6ce32008-12-19 01:48:161921
[email protected]71f65dd2009-02-11 19:14:561922void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161923 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561924 const std::wstring& value,
1925 bool* success) {
1926 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161927 if (browser_tracker_->ContainsHandle(handle)) {
1928 Browser* browser = browser_tracker_->GetResource(handle);
1929 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561930 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161931 }
[email protected]97fa6ce32008-12-19 01:48:161932}
1933
[email protected]71f65dd2009-02-11 19:14:561934void AutomationProvider::GetBooleanPreference(int handle,
1935 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:361936 bool* success,
1937 bool* value) {
[email protected]71f65dd2009-02-11 19:14:561938 *success = false;
1939 *value = false;
[email protected]97fa6ce32008-12-19 01:48:161940 if (browser_tracker_->ContainsHandle(handle)) {
1941 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561942 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
1943 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161944 }
[email protected]97fa6ce32008-12-19 01:48:161945}
1946
[email protected]71f65dd2009-02-11 19:14:561947void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161948 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561949 bool value,
1950 bool* success) {
1951 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161952 if (browser_tracker_->ContainsHandle(handle)) {
1953 Browser* browser = browser_tracker_->GetResource(handle);
1954 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561955 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161956 }
[email protected]97fa6ce32008-12-19 01:48:161957}
1958
1959// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561960void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:401961 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:161962 if (tab_tracker_->ContainsHandle(tab_handle)) {
1963 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1964 Browser* browser = FindAndActivateTab(nav);
1965 DCHECK(browser);
1966
[email protected]57c6a652009-05-04 07:58:341967 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
1968 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:161969 }
[email protected]97fa6ce32008-12-19 01:48:161970}
1971
[email protected]b8f48d12009-11-09 20:14:361972// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561973void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:401974 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:561975 bool* success) {
1976 *success = false;
[email protected]de246f52009-02-25 18:25:451977#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:161978 if (tab_tracker_->ContainsHandle(tab_handle)) {
1979 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1980 Browser* browser = FindAndActivateTab(nav);
1981 DCHECK(browser);
1982
[email protected]1fc025202009-01-20 23:03:141983 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:161984 int selected_encoding_id =
1985 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
1986 if (selected_encoding_id) {
1987 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:561988 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161989 }
1990 }
1991 }
[email protected]de246f52009-02-25 18:25:451992#else
1993 // TODO(port): Enable when encoding-related parts of Browser are ported.
1994 NOTIMPLEMENTED();
1995#endif
[email protected]97fa6ce32008-12-19 01:48:161996}
[email protected]5bcdb312009-01-07 21:43:201997
[email protected]4d434a1a2009-02-11 21:06:571998void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:201999 SavePackage::SetShouldPromptUser(should_prompt);
2000}
[email protected]87eab222009-03-13 00:47:452001
[email protected]3753f522009-04-14 23:15:472002void AutomationProvider::GetWindowTitle(int handle, string16* text) {
2003 gfx::NativeWindow window = window_tracker_->GetResource(handle);
2004 text->assign(platform_util::GetWindowTitle(window));
2005}
[email protected]66ba4932009-06-04 19:22:132006
2007void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2008 *count = -1; // -1 is the error code
2009 if (tab_tracker_->ContainsHandle(handle)) {
2010 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2011 TabContents* tab_contents = nav_controller->tab_contents();
2012 if (tab_contents) {
2013 BlockedPopupContainer* container =
2014 tab_contents->blocked_popup_container();
2015 if (container) {
2016 *count = static_cast<int>(container->GetBlockedPopupCount());
2017 } else {
2018 // If we don't have a container, we don't have any blocked popups to
2019 // contain!
2020 *count = 0;
2021 }
2022 }
2023 }
2024}
[email protected]f7a68432009-07-29 23:18:192025
2026void AutomationProvider::SelectAll(int tab_handle) {
2027 RenderViewHost* view = GetViewForTab(tab_handle);
2028 if (!view) {
2029 NOTREACHED();
2030 return;
2031 }
2032
2033 view->SelectAll();
2034}
2035
2036void AutomationProvider::Cut(int tab_handle) {
2037 RenderViewHost* view = GetViewForTab(tab_handle);
2038 if (!view) {
2039 NOTREACHED();
2040 return;
2041 }
2042
2043 view->Cut();
2044}
2045
2046void AutomationProvider::Copy(int tab_handle) {
2047 RenderViewHost* view = GetViewForTab(tab_handle);
2048 if (!view) {
2049 NOTREACHED();
2050 return;
2051 }
2052
2053 view->Copy();
2054}
2055
2056void AutomationProvider::Paste(int tab_handle) {
2057 RenderViewHost* view = GetViewForTab(tab_handle);
2058 if (!view) {
2059 NOTREACHED();
2060 return;
2061 }
2062
2063 view->Paste();
2064}
2065
2066void AutomationProvider::ReloadAsync(int tab_handle) {
2067 if (tab_tracker_->ContainsHandle(tab_handle)) {
2068 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2069 if (!tab) {
2070 NOTREACHED();
2071 return;
2072 }
2073
2074 tab->Reload(false);
2075 }
2076}
2077
2078void AutomationProvider::StopAsync(int tab_handle) {
2079 RenderViewHost* view = GetViewForTab(tab_handle);
2080 if (!view) {
[email protected]8b2b3312009-09-14 18:38:362081 // We tolerate StopAsync being called even before a view has been created.
2082 // So just log a warning instead of a NOTREACHED().
2083 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:192084 return;
2085 }
2086
2087 view->Stop();
2088}
2089
[email protected]1bb5f892009-10-06 01:44:572090void AutomationProvider::OnSetPageFontSize(int tab_handle,
2091 int font_size) {
2092 AutomationPageFontSize automation_font_size =
2093 static_cast<AutomationPageFontSize>(font_size);
2094
2095 if (automation_font_size < SMALLEST_FONT ||
2096 automation_font_size > LARGEST_FONT) {
2097 DLOG(ERROR) << "Invalid font size specified : "
2098 << font_size;
2099 return;
2100 }
2101
2102 if (tab_tracker_->ContainsHandle(tab_handle)) {
2103 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2104 DCHECK(tab != NULL);
2105 if (tab && tab->tab_contents()) {
2106 DCHECK(tab->tab_contents()->profile() != NULL);
2107 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2108 prefs::kWebKitDefaultFontSize, font_size);
2109 }
2110 }
2111}
2112
2113
[email protected]2949e90d2009-08-21 15:32:522114void AutomationProvider::WaitForBrowserWindowCountToBecome(
2115 int target_count, IPC::Message* reply_message) {
2116 if (static_cast<int>(BrowserList::size()) == target_count) {
2117 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2118 reply_message, true);
2119 Send(reply_message);
2120 return;
2121 }
2122
2123 // Set up an observer (it will delete itself).
2124 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2125}
2126
2127void AutomationProvider::WaitForAppModalDialogToBeShown(
2128 IPC::Message* reply_message) {
2129 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2130 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2131 reply_message, true);
2132 Send(reply_message);
2133 return;
2134 }
2135
2136 // Set up an observer (it will delete itself).
2137 new AppModalDialogShownObserver(this, reply_message);
2138}
2139
[email protected]1126a1d32009-08-26 15:39:262140void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2141 int handle, int number_of_navigations, IPC::Message* reply_message) {
2142 if (tab_tracker_->ContainsHandle(handle)) {
2143 NavigationController* tab = tab_tracker_->GetResource(handle);
2144 Browser* browser = FindAndActivateTab(tab);
2145 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
2146 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2147 browser->GoBack(CURRENT_TAB);
2148 return;
2149 }
2150 }
2151
2152 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2153 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2154 Send(reply_message);
2155}
2156
2157void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2158 int handle, int number_of_navigations, IPC::Message* reply_message) {
2159 if (tab_tracker_->ContainsHandle(handle)) {
2160 NavigationController* tab = tab_tracker_->GetResource(handle);
2161 Browser* browser = FindAndActivateTab(tab);
2162 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
2163 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2164 browser->GoForward(CURRENT_TAB);
2165 return;
2166 }
2167 }
2168
2169 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2170 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2171 Send(reply_message);
2172}
2173
[email protected]f7a68432009-07-29 23:18:192174RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2175 if (tab_tracker_->ContainsHandle(tab_handle)) {
2176 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2177 if (!tab) {
2178 NOTREACHED();
2179 return NULL;
2180 }
2181
2182 TabContents* tab_contents = tab->tab_contents();
2183 if (!tab_contents) {
2184 NOTREACHED();
2185 return NULL;
2186 }
2187
2188 RenderViewHost* view_host = tab_contents->render_view_host();
2189 return view_host;
2190 }
2191
2192 return NULL;
2193}
[email protected]675595f2009-08-26 22:32:042194
2195void AutomationProvider::GetBrowserForWindow(int window_handle,
2196 bool* success,
2197 int* browser_handle) {
2198 *success = false;
2199 *browser_handle = 0;
2200
2201 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2202 if (!window)
2203 return;
2204
2205 BrowserList::const_iterator iter = BrowserList::begin();
2206 for (;iter != BrowserList::end(); ++iter) {
2207 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2208 if (window == this_window) {
2209 // Add() returns the existing handle for the resource if any.
2210 *browser_handle = browser_tracker_->Add(*iter);
2211 *success = true;
2212 return;
2213 }
2214 }
2215}
[email protected]d11c8e92009-10-20 23:26:402216
2217void AutomationProvider::InstallExtension(const FilePath& crx_path,
2218 IPC::Message* reply_message) {
2219 ExtensionsService* service = profile_->GetExtensionsService();
2220 if (service) {
2221 // The observer will delete itself when done.
2222 new ExtensionNotificationObserver(this,
2223 AutomationMsg_InstallExtension::ID,
2224 reply_message);
2225
2226 const FilePath& install_dir = service->install_directory();
[email protected]d11c8e92009-10-20 23:26:402227 CrxInstaller::Start(crx_path,
2228 install_dir,
2229 Extension::INTERNAL,
2230 "", // no expected id
2231 false, // please do not delete crx file
2232 true, // privilege increase allowed
[email protected]d11c8e92009-10-20 23:26:402233 service,
2234 NULL); // silent isntall, no UI
2235 } else {
2236 AutomationMsg_InstallExtension::WriteReplyParams(
2237 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2238 Send(reply_message);
2239 }
2240}
2241
2242void AutomationProvider::LoadExpandedExtension(
2243 const FilePath& extension_dir,
2244 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:382245 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:402246 // The observer will delete itself when done.
2247 new ExtensionNotificationObserver(this,
2248 AutomationMsg_LoadExpandedExtension::ID,
2249 reply_message);
2250
2251 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:402252 } else {
2253 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
2254 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2255 Send(reply_message);
2256 }
2257}
[email protected]673fd2c02010-02-04 23:10:002258
2259void AutomationProvider::SaveAsAsync(int tab_handle) {
2260 NavigationController* tab = NULL;
2261 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2262 if (tab_contents)
2263 tab_contents->OnSavePage();
2264}
2265