blob: 85a8e0bb24093cf92042f2ce78e07c658200fa82 [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]2041cf342010-02-19 03:15:5911#include "base/callback.h"
[email protected]c6cb1992009-04-13 16:45:2912#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2013#include "base/json/json_reader.h"
[email protected]bc1407f2009-09-29 00:33:3514#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3815#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2916#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4617#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5018#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2719#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3820#include "base/thread.h"
[email protected]a7eee32f2009-05-22 18:08:1721#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2922#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2823#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0924#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3325#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2926#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0827#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5528#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5029#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1330#include "chrome/browser/blocked_popup_container.h"
[email protected]5c238752009-06-13 10:29:0731#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3932#include "chrome/browser/browser_window.h"
[email protected]fae20792009-10-28 20:31:5833#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2934#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1235#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5936#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2237#include "chrome/browser/download/download_shelf.h"
[email protected]d11c8e92009-10-20 23:26:4038#include "chrome/browser/extensions/crx_installer.h"
39#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5540#include "chrome/browser/extensions/extension_message_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1641#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5842#include "chrome/browser/find_bar.h"
43#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2944#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0145#include "chrome/browser/host_content_settings_map.h"
[email protected]0ac83682010-01-22 17:46:2746#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0647#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0148#include "chrome/browser/login_prompt.h"
[email protected]be180c802009-10-23 06:33:3149#include "chrome/browser/net/url_request_context_getter.h"
[email protected]f732c1e2009-07-30 15:48:5350#include "chrome/browser/net/url_request_mock_util.h"
[email protected]052313b2010-02-19 09:43:0851#include "chrome/browser/pref_service.h"
[email protected]a7eee32f2009-05-22 18:08:1752#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0753#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2554#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0355#include "chrome/browser/ssl/ssl_manager.h"
56#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3457#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4558#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1759#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2960#include "chrome/common/chrome_paths.h"
[email protected]a7eee32f2009-05-22 18:08:1761#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1462#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4763#include "chrome/common/platform_util.h"
[email protected]1bb5f892009-10-06 01:44:5764#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2565#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5666#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5767#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1768#include "net/proxy/proxy_service.h"
69#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2170#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2871#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2972
[email protected]de246f52009-02-25 18:25:4573#if defined(OS_WIN)
74// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4575#include "chrome/browser/character_encoding.h"
76#include "chrome/browser/download/save_package.h"
77#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4578#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4579#endif // defined(OS_WIN)
80
[email protected]5ae5bed2009-08-21 18:52:4481#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2882// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2883#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4484#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0685
[email protected]e1acf6f2008-10-27 20:43:3386using base::Time;
87
[email protected]cbab76d2008-10-13 22:42:4788class AutomationInterstitialPage : public InterstitialPage {
89 public:
[email protected]57c6a652009-05-04 07:58:3490 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4791 const GURL& url,
92 const std::string& contents)
93 : InterstitialPage(tab, true, url),
94 contents_(contents) {
95 }
96
97 virtual std::string GetHTMLContents() { return contents_; }
98
99 private:
100 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29101
[email protected]cbab76d2008-10-13 22:42:47102 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
103};
104
[email protected]c2cb8542009-08-20 21:16:51105#if !defined(OS_MACOSX)
106class ClickTask : public Task {
107 public:
[email protected]fc2e0872009-08-21 22:14:41108 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51109 virtual ~ClickTask() {}
110
111 virtual void Run() {
112 ui_controls::MouseButton button = ui_controls::LEFT;
113 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
114 views::Event::EF_LEFT_BUTTON_DOWN) {
115 button = ui_controls::LEFT;
116 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
117 views::Event::EF_RIGHT_BUTTON_DOWN) {
118 button = ui_controls::RIGHT;
119 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
120 views::Event::EF_MIDDLE_BUTTON_DOWN) {
121 button = ui_controls::MIDDLE;
122 } else {
123 NOTREACHED();
124 }
125
[email protected]fc2e0872009-08-21 22:14:41126 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51127 }
128
129 private:
[email protected]c2cb8542009-08-20 21:16:51130 int flags_;
131
132 DISALLOW_COPY_AND_ASSIGN(ClickTask);
133};
134#endif
135
initial.commit09911bf2008-07-26 23:55:29136AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57137 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56138 profile_(profile),
139 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29140 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29141 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20142 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29143 autocomplete_edit_tracker_.reset(
144 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29145 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
146 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44147 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]528211a2010-01-14 15:25:13148 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29149}
150
151AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50152 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
153 port_containers_.end());
154 port_containers_.clear();
155
[email protected]0da050b92008-08-19 19:29:47156 // Make sure that any outstanding NotificationObservers also get destroyed.
157 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31158 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47159 while ((observer = it.GetNext()) != NULL)
160 delete observer;
[email protected]528211a2010-01-14 15:25:13161
162 if (channel_.get()) {
163 channel_->Close();
164 }
165 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29166}
167
[email protected]9a3a293b2009-06-04 22:28:16168void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06169 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57170 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06171 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
172 automation_resource_message_filter_,
173 g_browser_process->io_thread()->message_loop(),
174 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05175 scoped_ptr<FileVersionInfo> file_version_info(
176 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40177 std::string version_string;
178 if (file_version_info != NULL) {
179 version_string = WideToASCII(file_version_info->file_version());
180 }
[email protected]c6cb1992009-04-13 16:45:29181
182 // Send a hello message with our current automation protocol version.
183 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29184}
185
186void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
187 if (expected_tabs == 0) {
188 Send(new AutomationMsg_InitialLoadsComplete(0));
189 } else {
190 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
191 }
192}
193
194NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58195 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01196 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29197 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58198 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01199 number_of_navigations,
200 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29201
[email protected]71f65dd2009-02-11 19:14:56202 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29203 return observer;
204}
205
206void AutomationProvider::RemoveNavigationStatusListener(
207 NotificationObserver* obs) {
208 notification_observer_list_.RemoveObserver(obs);
209}
210
211NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14212 Browser* parent,
213 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56214 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14215 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29216 notification_observer_list_.AddObserver(observer);
217
218 return observer;
219}
220
221void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
222 notification_observer_list_.RemoveObserver(obs);
223}
224
225void AutomationProvider::AddLoginHandler(NavigationController* tab,
226 LoginHandler* handler) {
227 login_handler_map_[tab] = handler;
228}
229
230void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
231 DCHECK(login_handler_map_[tab]);
232 login_handler_map_.erase(tab);
233}
234
[email protected]f44265b2009-05-19 18:52:50235void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
236 int port_id = port->port_id();
237 DCHECK_NE(-1, port_id);
238 DCHECK(port_containers_.find(port_id) == port_containers_.end());
239
240 port_containers_[port_id] = port;
241}
242
243void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
244 int port_id = port->port_id();
245 DCHECK_NE(-1, port_id);
246
247 PortContainerMap::iterator it = port_containers_.find(port_id);
248 DCHECK(it != port_containers_.end());
249
250 if (it != port_containers_.end()) {
251 delete it->second;
252 port_containers_.erase(it);
253 }
254}
255
256ExtensionPortContainer* AutomationProvider::GetPortContainer(
257 int port_id) const {
258 PortContainerMap::const_iterator it = port_containers_.find(port_id);
259 if (it == port_containers_.end())
260 return NULL;
261
262 return it->second;
263}
264
initial.commit09911bf2008-07-26 23:55:29265int AutomationProvider::GetIndexForNavigationController(
266 const NavigationController* controller, const Browser* parent) const {
267 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12268 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29269}
270
271void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
272 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14273 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56274 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
275 CloseBrowserAsync)
276 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
277 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
278 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
279 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
280 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
281 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14282 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58283 IPC_MESSAGE_HANDLER_DELAY_REPLY(
284 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
285 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56286 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
287 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
288 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
289 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
290 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14291 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56292 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
293 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
294 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14295 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29296 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
297 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56298 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36299 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56300 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29301 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56302 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29303 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
304 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56305 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14306 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24307 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44308 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33309 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44310 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33311 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24312 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14313 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24314 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15315 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14316 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44317#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24318 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35319 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14320 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]71f65dd2009-02-11 19:14:56321 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
322 WindowSimulateDrag)
[email protected]b2aa3ed72010-02-01 18:37:14323#endif
[email protected]71f65dd2009-02-11 19:14:56324 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53325 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56326 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37327#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56328 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45329#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56330 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
331 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03332 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56333 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14334 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09335 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
336 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
337 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29338 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14339 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56340 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
341 ExecuteJavascript)
342 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29343 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14344 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
345 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56346 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
347 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14348 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17349 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14350 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14351 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53352 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
353 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14354 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56355 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14356 GetAutocompleteEditForBrowser)
357 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45358#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29359 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17360#endif
[email protected]71f65dd2009-02-11 19:14:56361 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29362 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26363 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
364 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56365 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14366 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56367 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14368 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17369#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29370 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
371 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17372#endif
[email protected]71f65dd2009-02-11 19:14:56373 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
374 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14375 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17376#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04377 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39378 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
379 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17380#endif
[email protected]1c58a5c2009-05-21 18:47:14381 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
382 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44383 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
384 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56385 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
386 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29387 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
388 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
389 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56390 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41391 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56392 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
393 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29394 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56395 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29396 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56397 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29398 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56399 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29400 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56401 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45402 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45403 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
404 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14405 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56406 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57407 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56408 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57409 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56410 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
411 GetBookmarkBarVisibility)
[email protected]816633a2009-11-11 21:48:18412 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
413 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
414 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56415 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42416 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56417 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
418 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14419 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56420 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44421 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56422 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20423 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14424 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56425 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16426 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56427 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16428 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56429 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16430 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14431 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20432 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
433 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14434 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]4cfc1d922009-11-08 14:02:58435#if defined(OS_WIN)
436 // Depends on ExternalTabContainer, so Windows-only
[email protected]b83e4602009-05-15 22:58:33437 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
438 SetEnableExtensionAutomation)
[email protected]4cfc1d922009-11-08 14:02:58439#endif
[email protected]59560e0b2009-06-04 03:30:22440 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13441 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19442 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
443 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
444 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
445 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
446 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
447 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52448 IPC_MESSAGE_HANDLER_DELAY_REPLY(
449 AutomationMsg_WaitForBrowserWindowCountToBecome,
450 WaitForBrowserWindowCountToBecome)
451 IPC_MESSAGE_HANDLER_DELAY_REPLY(
452 AutomationMsg_WaitForAppModalDialogToBeShown,
453 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26454 IPC_MESSAGE_HANDLER_DELAY_REPLY(
455 AutomationMsg_GoBackBlockUntilNavigationsComplete,
456 GoBackBlockUntilNavigationsComplete)
457 IPC_MESSAGE_HANDLER_DELAY_REPLY(
458 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
459 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14460#if defined(OS_WIN)
461 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
462#endif
[email protected]1bb5f892009-10-06 01:44:57463 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40464 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
465 InstallExtension)
466 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
467 LoadExpandedExtension)
[email protected]fedaa7d2010-01-26 20:34:57468 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
469 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00470 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]2b19e2fe2010-02-16 02:24:18471#if defined(OS_WIN)
472 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
473#endif
[email protected]7dad3d5f2010-03-04 00:27:01474 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
initial.commit09911bf2008-07-26 23:55:29475 IPC_END_MESSAGE_MAP()
476}
477
[email protected]71f65dd2009-02-11 19:14:56478void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
479 *status = -1;
initial.commit09911bf2008-07-26 23:55:29480 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
481 Browser* browser = browser_tracker_->GetResource(handle);
482 if (at_index >= 0 && at_index < browser->tab_count()) {
483 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56484 *status = 0;
initial.commit09911bf2008-07-26 23:55:29485 }
486 }
initial.commit09911bf2008-07-26 23:55:29487}
488
[email protected]71f65dd2009-02-11 19:14:56489void AutomationProvider::AppendTab(int handle, const GURL& url,
490 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29491 int append_tab_response = -1; // -1 is the error code
492 NotificationObserver* observer = NULL;
493
494 if (browser_tracker_->ContainsHandle(handle)) {
495 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14496 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58497 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
498 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39499 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29500 if (tab_contents) {
501 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57502 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29503 }
504 }
505
506 if (append_tab_response < 0) {
507 // The append tab failed. Remove the TabStripObserver
508 if (observer) {
509 RemoveTabStripObserver(observer);
510 delete observer;
511 }
512
[email protected]71f65dd2009-02-11 19:14:56513 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
514 append_tab_response);
515 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29516 }
517}
518
[email protected]71f65dd2009-02-11 19:14:56519void AutomationProvider::NavigateToURL(int handle, const GURL& url,
520 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58521 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
522}
523
524void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
525 int handle, const GURL& url, int number_of_navigations,
526 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29527 if (tab_tracker_->ContainsHandle(handle)) {
528 NavigationController* tab = tab_tracker_->GetResource(handle);
529
530 // Simulate what a user would do. Activate the tab and then navigate.
531 // We could allow navigating in a background tab in future.
532 Browser* browser = FindAndActivateTab(tab);
533
534 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01535 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
536 false);
[email protected]71f65dd2009-02-11 19:14:56537
initial.commit09911bf2008-07-26 23:55:29538 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50539 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29540 return;
541 }
542 }
[email protected]71f65dd2009-02-11 19:14:56543
544 AutomationMsg_NavigateToURL::WriteReplyParams(
545 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
546 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29547}
[email protected]2949e90d2009-08-21 15:32:52548
[email protected]71f65dd2009-02-11 19:14:56549void AutomationProvider::NavigationAsync(int handle, const GURL& url,
550 bool* status) {
551 *status = false;
initial.commit09911bf2008-07-26 23:55:29552
553 if (tab_tracker_->ContainsHandle(handle)) {
554 NavigationController* tab = tab_tracker_->GetResource(handle);
555
556 // Simulate what a user would do. Activate the tab and then navigate.
557 // We could allow navigating in a background tab in future.
558 Browser* browser = FindAndActivateTab(tab);
559
560 if (browser) {
561 // Don't add any listener unless a callback mechanism is desired.
562 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50563 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56564 *status = true;
initial.commit09911bf2008-07-26 23:55:29565 }
566 }
initial.commit09911bf2008-07-26 23:55:29567}
568
[email protected]71f65dd2009-02-11 19:14:56569void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29570 if (tab_tracker_->ContainsHandle(handle)) {
571 NavigationController* tab = tab_tracker_->GetResource(handle);
572 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14573 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01574 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29575 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29576 return;
577 }
578 }
[email protected]71f65dd2009-02-11 19:14:56579
580 AutomationMsg_GoBack::WriteReplyParams(
581 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
582 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29583}
584
[email protected]71f65dd2009-02-11 19:14:56585void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29586 if (tab_tracker_->ContainsHandle(handle)) {
587 NavigationController* tab = tab_tracker_->GetResource(handle);
588 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14589 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01590 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29591 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29592 return;
593 }
594 }
[email protected]71f65dd2009-02-11 19:14:56595
596 AutomationMsg_GoForward::WriteReplyParams(
597 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
598 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29599}
600
[email protected]71f65dd2009-02-11 19:14:56601void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29602 if (tab_tracker_->ContainsHandle(handle)) {
603 NavigationController* tab = tab_tracker_->GetResource(handle);
604 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14605 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01606 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29607 browser->Reload();
608 return;
609 }
610 }
[email protected]71f65dd2009-02-11 19:14:56611
612 AutomationMsg_Reload::WriteReplyParams(
613 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
614 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29615}
616
[email protected]71f65dd2009-02-11 19:14:56617void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29618 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56619 const std::wstring& password,
620 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29621 if (tab_tracker_->ContainsHandle(tab_handle)) {
622 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
623 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
624
625 if (iter != login_handler_map_.end()) {
626 // If auth is needed again after this, assume login has failed. This is
627 // not strictly correct, because a navigation can require both proxy and
628 // server auth, but it should be OK for now.
629 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01630 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29631 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52632 return;
initial.commit09911bf2008-07-26 23:55:29633 }
634 }
[email protected]de246f52009-02-25 18:25:45635
[email protected]457f5cf2009-08-18 16:37:52636 AutomationMsg_SetAuth::WriteReplyParams(
637 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
638 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29639}
640
[email protected]71f65dd2009-02-11 19:14:56641void AutomationProvider::CancelAuth(int tab_handle,
642 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29643 if (tab_tracker_->ContainsHandle(tab_handle)) {
644 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
645 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
646
647 if (iter != login_handler_map_.end()) {
648 // If auth is needed again after this, something is screwy.
649 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01650 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29651 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52652 return;
initial.commit09911bf2008-07-26 23:55:29653 }
654 }
[email protected]de246f52009-02-25 18:25:45655
[email protected]457f5cf2009-08-18 16:37:52656 AutomationMsg_CancelAuth::WriteReplyParams(
657 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
658 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29659}
660
[email protected]71f65dd2009-02-11 19:14:56661void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
662 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29663
664 if (tab_tracker_->ContainsHandle(tab_handle)) {
665 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
666 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
667
668 if (iter != login_handler_map_.end()) {
669 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56670 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29671 }
672 }
initial.commit09911bf2008-07-26 23:55:29673}
674
[email protected]71f65dd2009-02-11 19:14:56675void AutomationProvider::GetRedirectsFrom(int tab_handle,
676 const GURL& source_url,
677 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29678 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
679 if (tab_tracker_->ContainsHandle(tab_handle)) {
680 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
681 HistoryService* history_service =
682 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
683
684 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
685 "has no history service";
686 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56687 DCHECK(reply_message_ == NULL);
688 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29689 // Schedule a history query for redirects. The response will be sent
690 // asynchronously from the callback the history system uses to notify us
691 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29692 redirect_query_ = history_service->QueryRedirectsFrom(
693 source_url, &consumer_,
694 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
695 return; // Response will be sent when query completes.
696 }
697 }
698
699 // Send failure response.
[email protected]deb57402009-02-06 01:35:30700 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56701 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
702 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29703}
704
[email protected]71f65dd2009-02-11 19:14:56705void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
706 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29707 if (browser_tracker_->ContainsHandle(handle)) {
708 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56709 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29710 }
initial.commit09911bf2008-07-26 23:55:29711}
712
[email protected]202e7a72009-06-15 03:48:36713void AutomationProvider::GetBrowserLocale(string16* locale) {
714 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06715 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36716}
717
[email protected]71f65dd2009-02-11 19:14:56718void AutomationProvider::GetBrowserWindowCount(int* window_count) {
719 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29720}
721
[email protected]24497032009-05-01 17:00:29722void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
723 *window_count = static_cast<int>(
724 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
725}
726
[email protected]71f65dd2009-02-11 19:14:56727void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
728 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07729 AppModalDialog* dialog_delegate =
730 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50731 *showing_dialog = (dialog_delegate != NULL);
732 if (*showing_dialog)
733 *dialog_button = dialog_delegate->GetDialogButtons();
734 else
[email protected]478ff2ed2009-04-21 23:49:18735 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20736}
737
[email protected]71f65dd2009-02-11 19:14:56738void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
739 *success = false;
[email protected]fad84eab2008-12-05 00:37:20740
[email protected]1f460072009-05-28 17:02:07741 AppModalDialog* dialog_delegate =
742 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50743 if (dialog_delegate &&
744 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18745 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
746 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28747 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56748 *success = true;
[email protected]fad84eab2008-12-05 00:37:20749 }
[email protected]478ff2ed2009-04-21 23:49:18750 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
751 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56752 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28753 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56754 *success = true;
[email protected]fad84eab2008-12-05 00:37:20755 }
756 }
[email protected]c274acc2008-11-11 20:13:44757}
758
[email protected]fedaa7d2010-01-26 20:34:57759void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
760 if (browser_tracker_->ContainsHandle(handle)) {
761 Browser* browser = browser_tracker_->GetResource(handle);
762 browser->profile()->ShutdownSessionService();
763 *result = true;
764 } else {
765 *result = false;
766 }
767}
768
[email protected]71f65dd2009-02-11 19:14:56769void AutomationProvider::GetBrowserWindow(int index, int* handle) {
770 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29771 if (index >= 0) {
772 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29773 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29774 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56775 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29776 }
777 }
initial.commit09911bf2008-07-26 23:55:29778}
779
[email protected]24497032009-05-01 17:00:29780void AutomationProvider::FindNormalBrowserWindow(int* handle) {
781 *handle = 0;
782 Browser* browser = BrowserList::FindBrowserWithType(profile_,
783 Browser::TYPE_NORMAL);
784 if (browser)
785 *handle = browser_tracker_->Add(browser);
786}
787
[email protected]71f65dd2009-02-11 19:14:56788void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
789 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29790 Browser* browser = BrowserList::GetLastActive();
791 if (browser)
[email protected]71f65dd2009-02-11 19:14:56792 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29793}
794
[email protected]b2aa3ed72010-02-01 18:37:14795#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28796// TODO(estade): use this implementation for all platforms?
797void AutomationProvider::GetActiveWindow(int* handle) {
798 gfx::NativeWindow window =
799 BrowserList::GetLastActive()->window()->GetNativeHandle();
800 *handle = window_tracker_->Add(window);
801}
802#endif
803
[email protected]4f6381ee2009-04-16 02:46:33804void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
805 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56806 *success = false;
[email protected]4ae62752008-08-04 23:28:47807 if (browser_tracker_->ContainsHandle(handle)) {
808 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14809 if (browser->command_updater()->SupportsCommand(command) &&
810 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47811 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56812 *success = true;
[email protected]4ae62752008-08-04 23:28:47813 }
814 }
[email protected]4ae62752008-08-04 23:28:47815}
816
[email protected]4f6381ee2009-04-16 02:46:33817void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56818 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53819 // List of commands which just finish synchronously and don't require
820 // setting up an observer.
821 static const int kSynchronousCommands[] = {
822 IDC_HOME,
823 IDC_SELECT_NEXT_TAB,
824 IDC_SELECT_PREVIOUS_TAB,
825 IDC_SHOW_DOWNLOADS,
826 IDC_SHOW_HISTORY,
827 };
[email protected]56e71b7c2009-03-27 03:05:56828 if (browser_tracker_->ContainsHandle(handle)) {
829 Browser* browser = browser_tracker_->GetResource(handle);
830 if (browser->command_updater()->SupportsCommand(command) &&
831 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53832 // First check if we can handle the command without using an observer.
833 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
834 if (command == kSynchronousCommands[i]) {
835 browser->ExecuteCommand(command);
836 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
837 true);
838 Send(reply_message);
839 return;
840 }
841 }
842
843 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42844 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
845 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27846 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42847 return;
848 }
[email protected]56e71b7c2009-03-27 03:05:56849 }
850 }
[email protected]49a14a82009-03-31 04:16:44851 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56852 Send(reply_message);
853}
854
[email protected]fc2e0872009-08-21 22:14:41855// This task just adds another task to the event queue. This is useful if
856// you want to ensure that any tasks added to the event queue after this one
857// have already been processed by the time |task| is run.
858class InvokeTaskLaterTask : public Task {
859 public:
860 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
861 virtual ~InvokeTaskLaterTask() {}
862
863 virtual void Run() {
864 MessageLoop::current()->PostTask(FROM_HERE, task_);
865 }
866
867 private:
868 Task* task_;
869
870 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
871};
872
[email protected]b2aa3ed72010-02-01 18:37:14873#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29874void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
875 int handle,
[email protected]d1a5941e2009-08-13 23:34:24876 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29877 int flags) {
[email protected]b410bc32009-08-14 01:11:14878 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51879 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41880 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29881 }
882}
883
[email protected]60507b12009-11-02 23:51:35884void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
885 int handle,
886 const gfx::Point& location) {
887 if (window_tracker_->ContainsHandle(handle))
888 ui_controls::SendMouseMove(location.x(), location.y());
889}
890
initial.commit09911bf2008-07-26 23:55:29891void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
892 int handle,
[email protected]bc1407f2009-09-29 00:33:35893 int key,
initial.commit09911bf2008-07-26 23:55:29894 int flags) {
[email protected]b410bc32009-08-14 01:11:14895 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29896 return;
897
[email protected]b410bc32009-08-14 01:11:14898 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29899 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35900 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38901 ((flags & views::Event::EF_CONTROL_DOWN) ==
902 views::Event::EF_CONTROL_DOWN),
903 ((flags & views::Event::EF_SHIFT_DOWN) ==
904 views::Event::EF_SHIFT_DOWN),
905 ((flags & views::Event::EF_ALT_DOWN) ==
906 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29907}
[email protected]b2aa3ed72010-02-01 18:37:14908#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29909
[email protected]71f65dd2009-02-11 19:14:56910void AutomationProvider::IsWindowActive(int handle, bool* success,
911 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29912 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17913 *is_active =
914 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56915 *success = true;
initial.commit09911bf2008-07-26 23:55:29916 } else {
[email protected]71f65dd2009-02-11 19:14:56917 *success = false;
918 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29919 }
920}
921
[email protected]71f65dd2009-02-11 19:14:56922void AutomationProvider::GetTabCount(int handle, int* tab_count) {
923 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29924
925 if (browser_tracker_->ContainsHandle(handle)) {
926 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56927 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29928 }
initial.commit09911bf2008-07-26 23:55:29929}
930
[email protected]982921f12009-10-27 21:43:53931void AutomationProvider::GetType(int handle, int* type_as_int) {
932 *type_as_int = -1; // -1 is the error code
933
934 if (browser_tracker_->ContainsHandle(handle)) {
935 Browser* browser = browser_tracker_->GetResource(handle);
936 *type_as_int = static_cast<int>(browser->type());
937 }
938}
939
[email protected]71f65dd2009-02-11 19:14:56940void AutomationProvider::GetTab(int win_handle, int tab_index,
941 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56942 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29943 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
944 Browser* browser = browser_tracker_->GetResource(win_handle);
945 if (tab_index < browser->tab_count()) {
946 TabContents* tab_contents =
947 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57948 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29949 }
950 }
initial.commit09911bf2008-07-26 23:55:29951}
952
[email protected]71f65dd2009-02-11 19:14:56953void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
954 std::wstring* title) {
955 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29956 if (tab_tracker_->ContainsHandle(handle)) {
957 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36958 NavigationEntry* entry = tab->GetActiveEntry();
959 if (entry != NULL) {
960 *title = UTF16ToWideHack(entry->title());
961 } else {
962 *title = std::wstring();
963 }
[email protected]71f65dd2009-02-11 19:14:56964 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29965 }
initial.commit09911bf2008-07-26 23:55:29966}
967
[email protected]77bc6732009-04-20 22:01:03968void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
969 *tabstrip_index = -1; // -1 is the error code
970
971 if (tab_tracker_->ContainsHandle(handle)) {
972 NavigationController* tab = tab_tracker_->GetResource(handle);
973 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12974 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:03975 }
976}
977
initial.commit09911bf2008-07-26 23:55:29978void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
979 if (window_tracker_->ContainsHandle(handle)) {
980 window_tracker_->Remove(window_tracker_->GetResource(handle));
981 }
982}
983
984void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:16985 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:57986 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:29987}
988
989// TODO(brettw) change this to accept GURLs when history supports it
990void AutomationProvider::OnRedirectQueryComplete(
991 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:37992 GURL from_url,
initial.commit09911bf2008-07-26 23:55:29993 bool success,
[email protected]379c2b12009-07-01 21:50:33994 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:29995 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:56996 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:29997
[email protected]deb57402009-02-06 01:35:30998 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:29999 if (success) {
[email protected]71f65dd2009-02-11 19:14:561000 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:291001 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301002 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291003 } else {
[email protected]71f65dd2009-02-11 19:14:561004 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:291005 }
1006
[email protected]4f3dc372009-02-24 00:10:291007 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301008
[email protected]71f65dd2009-02-11 19:14:561009 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:171010 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561011 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291012}
1013
1014bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571015 DCHECK(channel_.get());
1016 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291017}
1018
1019Browser* AutomationProvider::FindAndActivateTab(
1020 NavigationController* controller) {
1021 int tab_index;
1022 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1023 if (browser)
1024 browser->SelectTabContentsAt(tab_index, true);
1025
1026 return browser;
1027}
1028
[email protected]71f65dd2009-02-11 19:14:561029void AutomationProvider::GetCookies(const GURL& url, int handle,
1030 int* value_size,
1031 std::string* value) {
1032 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291033 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1034 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311035
1036 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001037 scoped_refptr<URLRequestContextGetter> request_context =
1038 tab->tab_contents()->request_context();
1039 if (!request_context.get())
1040 request_context = tab->profile()->GetRequestContext();
1041
1042 net::CookieStore* cookie_store = request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311043
1044 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561045 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291046 }
initial.commit09911bf2008-07-26 23:55:291047}
1048
[email protected]71f65dd2009-02-11 19:14:561049void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291050 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561051 int handle,
1052 int* response_value) {
1053 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291054
1055 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1056 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311057
[email protected]dfa46e5f2009-11-17 18:48:431058 scoped_refptr<URLRequestContextGetter> request_context =
1059 tab->tab_contents()->request_context();
1060 if (!request_context.get())
1061 request_context = tab->profile()->GetRequestContext();
1062
[email protected]be180c802009-10-23 06:33:311063 // Since we are running on the UI thread don't call GetURLRequestContext().
1064 scoped_refptr<net::CookieStore> cookie_store =
[email protected]dfa46e5f2009-11-17 18:48:431065 request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311066
1067 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561068 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291069 }
initial.commit09911bf2008-07-26 23:55:291070}
1071
[email protected]71f65dd2009-02-11 19:14:561072void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1073 *success = false;
initial.commit09911bf2008-07-26 23:55:291074 if (tab_tracker_->ContainsHandle(handle)) {
1075 NavigationController* tab = tab_tracker_->GetResource(handle);
1076 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541077 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561078 *success = true;
initial.commit09911bf2008-07-26 23:55:291079 }
initial.commit09911bf2008-07-26 23:55:291080}
1081
[email protected]71f65dd2009-02-11 19:14:561082void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1083 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291084
1085 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561086 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341087 TabContents* tab_contents =
1088 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161089 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1090 if (rph)
1091 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291092 }
initial.commit09911bf2008-07-26 23:55:291093}
1094
1095void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331096 NOTREACHED() << "This function has been deprecated. "
1097 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291098}
1099
[email protected]71f65dd2009-02-11 19:14:561100void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291101 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561102 const std::wstring& script,
1103 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291104 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341105 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1106 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571107 // Set the routing id of this message with the controller.
1108 // This routing id needs to be remembered for the reverse
1109 // communication while sending back the response of
1110 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331111 std::wstring set_automation_id;
1112 SStringPrintf(&set_automation_id,
1113 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561114 reply_message->routing_id());
1115
1116 DCHECK(reply_message_ == NULL);
1117 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291118
[email protected]57c6a652009-05-04 07:58:341119 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331120 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341121 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061122 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571123 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291124 }
1125
1126 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561127 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1128 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291129 }
1130}
1131
[email protected]71f65dd2009-02-11 19:14:561132void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1133 *visible = false;
[email protected]20e93d12008-08-28 16:31:571134
[email protected]59560e0b2009-06-04 03:30:221135 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251136#if defined(OS_CHROMEOS)
1137 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1138 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1139 for (BrowserList::const_iterator it = BrowserList::begin();
1140 it != BrowserList::end(); ++it) {
1141 if ((*it)->type() == Browser::TYPE_POPUP) {
1142 const GURL& url =
1143 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1144
1145 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1146 url.host() == chrome::kChromeUIFileBrowseHost) {
1147 *visible = true;
1148 break;
1149 }
1150 }
1151 }
1152#else
[email protected]59560e0b2009-06-04 03:30:221153 Browser* browser = browser_tracker_->GetResource(handle);
1154 if (browser) {
1155 *visible = browser->window()->IsDownloadShelfVisible();
1156 }
[email protected]f5bf8ccf2010-02-05 18:19:251157#endif
[email protected]59560e0b2009-06-04 03:30:221158 }
initial.commit09911bf2008-07-26 23:55:291159}
1160
[email protected]59560e0b2009-06-04 03:30:221161void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1162 if (browser_tracker_->ContainsHandle(handle)) {
1163 Browser* browser = browser_tracker_->GetResource(handle);
1164 if (browser) {
1165 if (visible)
1166 browser->window()->GetDownloadShelf()->Show();
1167 else
1168 browser->window()->GetDownloadShelf()->Close();
1169 }
1170 }
1171}
1172
[email protected]34930432009-11-09 00:12:091173void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1174 *visible = false;
1175
1176 if (browser_tracker_->ContainsHandle(handle)) {
1177 Browser* browser = browser_tracker_->GetResource(handle);
1178 if (browser)
1179 *visible = browser->window()->IsFullscreen();
1180 }
1181}
1182
1183void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1184 bool* visible) {
1185 *visible = false;
1186
1187 if (browser_tracker_->ContainsHandle(handle)) {
1188 Browser* browser = browser_tracker_->GetResource(handle);
1189 if (browser)
1190 *visible = browser->window()->IsFullscreenBubbleVisible();
1191 }
1192}
[email protected]59560e0b2009-06-04 03:30:221193
[email protected]71f65dd2009-02-11 19:14:561194void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1195 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291196 if (tab_tracker_->ContainsHandle(handle)) {
1197 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111198 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291199 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561200 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291201 }
1202 }
initial.commit09911bf2008-07-26 23:55:291203}
1204
initial.commit09911bf2008-07-26 23:55:291205void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561206 int handle, const std::wstring& find_request,
1207 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311208 NOTREACHED() << "This function has been deprecated."
1209 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561210 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311211 return;
1212}
1213
[email protected]4f999132009-03-31 18:08:401214void AutomationProvider::HandleFindRequest(
1215 int handle,
1216 const AutomationMsg_Find_Params& params,
1217 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291218 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561219 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1220 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291221 return;
1222 }
1223
1224 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111225 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291226
1227 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141228 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291229
[email protected]57c6a652009-05-04 07:58:341230 tab_contents->set_current_find_request_id(
1231 FindInPageNotificationObserver::kFindInPageRequestId);
1232 tab_contents->render_view_host()->StartFinding(
1233 FindInPageNotificationObserver::kFindInPageRequestId,
1234 params.search_string, params.forward, params.match_case,
1235 params.find_next);
initial.commit09911bf2008-07-26 23:55:291236}
1237
[email protected]5f8af2a2008-08-06 22:49:451238void AutomationProvider::HandleOpenFindInPageRequest(
1239 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291240 if (browser_tracker_->ContainsHandle(handle)) {
1241 Browser* browser = browser_tracker_->GetResource(handle);
1242 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451243 }
1244}
1245
[email protected]71f65dd2009-02-11 19:14:561246void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561247 *visible = false;
[email protected]855c0142009-09-28 22:35:241248 Browser* browser = browser_tracker_->GetResource(handle);
1249 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581250 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171251 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241252 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291253 }
[email protected]20e93d12008-08-28 16:31:571254}
1255
[email protected]71f65dd2009-02-11 19:14:561256void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1257 int* y) {
[email protected]9e0534b2008-10-21 15:03:011258 gfx::Point position(0, 0);
1259 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291260 if (browser_tracker_->ContainsHandle(handle)) {
1261 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581262 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171263 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581264 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291265 }
[email protected]20e93d12008-08-28 16:31:571266
[email protected]71f65dd2009-02-11 19:14:561267 *x = position.x();
1268 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571269}
1270
initial.commit09911bf2008-07-26 23:55:291271void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561272 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341273 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1274 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561275 DCHECK(reply_message_ == NULL);
1276 reply_message_ = reply_message;
1277
[email protected]d9f9b792009-06-24 13:17:121278 DevToolsManager::GetInstance()->InspectElement(
1279 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291280 } else {
[email protected]71f65dd2009-02-11 19:14:561281 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1282 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291283 }
1284}
1285
1286void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091287 if (reply_message_) {
1288 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1289 num_resources);
1290 Send(reply_message_);
1291 reply_message_ = NULL;
1292 }
initial.commit09911bf2008-07-26 23:55:291293}
1294
[email protected]a7eee32f2009-05-22 18:08:171295class SetProxyConfigTask : public Task {
1296 public:
[email protected]be180c802009-10-23 06:33:311297 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
1298 const std::string& new_proxy_config)
1299 : request_context_getter_(request_context_getter), proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:171300 virtual void Run() {
1301 // First, deserialize the JSON string. If this fails, log and bail.
1302 JSONStringValueSerializer deserializer(proxy_config_);
1303 std::string error_message;
1304 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1305 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1306 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1307 << error_message;
1308 return;
1309 }
1310
1311 scoped_ptr<DictionaryValue> dict(
1312 static_cast<DictionaryValue*>(root.release()));
1313 // Now put together a proxy configuration from the deserialized string.
1314 net::ProxyConfig pc;
1315 PopulateProxyConfig(*dict.get(), &pc);
1316
[email protected]be180c802009-10-23 06:33:311317 net::ProxyService* proxy_service =
1318 request_context_getter_->GetURLRequestContext()->proxy_service();
1319 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:171320 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1321 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:311322 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:171323 }
1324
1325 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1326 DCHECK(pc);
1327 bool no_proxy = false;
1328 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1329 // Make no changes to the ProxyConfig.
1330 return;
1331 }
1332 bool auto_config;
1333 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:481334 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:171335 }
1336 std::string pac_url;
1337 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:481338 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:171339 }
1340 std::string proxy_bypass_list;
1341 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:481342 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:171343 }
1344 std::string proxy_server;
1345 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:481346 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:171347 }
1348 }
1349
1350 private:
[email protected]be180c802009-10-23 06:33:311351 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:171352 std::string proxy_config_;
1353};
1354
1355
1356void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:311357 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
1358 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:171359 FilePath user_data_dir;
1360 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1361 ProfileManager* profile_manager = g_browser_process->profile_manager();
1362 DCHECK(profile_manager);
1363 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1364 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:311365 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171366 }
[email protected]be180c802009-10-23 06:33:311367 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:171368
[email protected]fae20792009-10-28 20:31:581369 ChromeThread::PostTask(
1370 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:311371 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171372}
1373
[email protected]4f3dc372009-02-24 00:10:291374void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331375 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291376 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291377 if (tab_tracker_->ContainsHandle(handle)) {
1378 NavigationController* tab = tab_tracker_->GetResource(handle);
1379 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1380 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331381 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291382 }
initial.commit09911bf2008-07-26 23:55:291383}
1384
[email protected]6a5670d22009-10-27 16:21:341385void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:141386 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:531387 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
1388 reply_message);
1389}
1390
1391void AutomationProvider::OpenNewBrowserWindowOfType(
1392 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:141393 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291394 // We may have no current browser windows open so don't rely on
1395 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:531396 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
1397 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:051398 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151399 if (show)
[email protected]15952e462008-11-14 00:29:051400 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291401}
1402
[email protected]71f65dd2009-02-11 19:14:561403void AutomationProvider::GetWindowForBrowser(int browser_handle,
1404 bool* success,
1405 int* handle) {
1406 *success = false;
1407 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291408
1409 if (browser_tracker_->ContainsHandle(browser_handle)) {
1410 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201411 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291412 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201413 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561414 *success = true;
initial.commit09911bf2008-07-26 23:55:291415 }
initial.commit09911bf2008-07-26 23:55:291416}
1417
[email protected]5ae5bed2009-08-21 18:52:441418#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291419void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561420 int browser_handle,
1421 bool* success,
1422 int* autocomplete_edit_handle) {
1423 *success = false;
1424 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291425
1426 if (browser_tracker_->ContainsHandle(browser_handle)) {
1427 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061428 LocationBar* loc_bar = browser->window()->GetLocationBar();
1429 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291430 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561431 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1432 *success = true;
initial.commit09911bf2008-07-26 23:55:291433 }
initial.commit09911bf2008-07-26 23:55:291434}
[email protected]5ae5bed2009-08-21 18:52:441435#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291436
[email protected]71f65dd2009-02-11 19:14:561437void AutomationProvider::ShowInterstitialPage(int tab_handle,
1438 const std::string& html_text,
1439 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291440 if (tab_tracker_->ContainsHandle(tab_handle)) {
1441 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111442 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401443
[email protected]7dad3d5f2010-03-04 00:27:011444 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:401445 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341446 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401447 GURL("about:interstitial"),
1448 html_text);
1449 interstitial->Show();
1450 return;
initial.commit09911bf2008-07-26 23:55:291451 }
[email protected]71f65dd2009-02-11 19:14:561452
[email protected]457f5cf2009-08-18 16:37:521453 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1454 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561455 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291456}
1457
[email protected]71f65dd2009-02-11 19:14:561458void AutomationProvider::HideInterstitialPage(int tab_handle,
1459 bool* success) {
1460 *success = false;
[email protected]57c6a652009-05-04 07:58:341461 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1462 if (tab_contents && tab_contents->interstitial_page()) {
1463 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561464 *success = true;
initial.commit09911bf2008-07-26 23:55:291465 }
initial.commit09911bf2008-07-26 23:55:291466}
1467
[email protected]71f65dd2009-02-11 19:14:561468void AutomationProvider::CloseTab(int tab_handle,
1469 bool wait_until_closed,
1470 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291471 if (tab_tracker_->ContainsHandle(tab_handle)) {
1472 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1473 int index;
1474 Browser* browser = Browser::GetBrowserForController(controller, &index);
1475 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141476 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111477 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451478 return;
initial.commit09911bf2008-07-26 23:55:291479 }
[email protected]de246f52009-02-25 18:25:451480
1481 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:551482 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291483}
1484
[email protected]71f65dd2009-02-11 19:14:561485void AutomationProvider::CloseBrowser(int browser_handle,
1486 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291487 if (browser_tracker_->ContainsHandle(browser_handle)) {
1488 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561489 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561490 reply_message);
[email protected]f3e99e32008-07-30 04:48:391491 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291492 } else {
1493 NOTREACHED();
1494 }
1495}
1496
[email protected]71f65dd2009-02-11 19:14:561497void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1498 if (browser_tracker_->ContainsHandle(browser_handle)) {
1499 Browser* browser = browser_tracker_->GetResource(browser_handle);
1500 browser->window()->Close();
1501 } else {
1502 NOTREACHED();
1503 }
1504}
1505
[email protected]71f65dd2009-02-11 19:14:561506void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:571507 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:561508 AutomationMsg_NavigationResponseValues* status) {
1509 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291510
1511 if (tab_tracker_->ContainsHandle(handle)) {
1512 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:571513 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561514 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291515 }
initial.commit09911bf2008-07-26 23:55:291516}
1517
[email protected]4150ef02009-08-19 23:14:261518void AutomationProvider::NavigateExternalTabAtIndex(
1519 int handle, int navigation_index,
1520 AutomationMsg_NavigationResponseValues* status) {
1521 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1522
1523 if (tab_tracker_->ContainsHandle(handle)) {
1524 NavigationController* tab = tab_tracker_->GetResource(handle);
1525 tab->GoToIndex(navigation_index);
1526 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1527 }
1528}
1529
[email protected]71f65dd2009-02-11 19:14:561530void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1531 IPC::Message* reply_message) {
1532 if (tab_tracker_->ContainsHandle(tab_handle)) {
1533 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1534 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141535 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561536 }
1537}
1538
[email protected]71f65dd2009-02-11 19:14:561539void AutomationProvider::GetSecurityState(int handle, bool* success,
1540 SecurityStyle* security_style,
1541 int* ssl_cert_status,
1542 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291543 if (tab_tracker_->ContainsHandle(handle)) {
1544 NavigationController* tab = tab_tracker_->GetResource(handle);
1545 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561546 *success = true;
1547 *security_style = entry->ssl().security_style();
1548 *ssl_cert_status = entry->ssl().cert_status();
1549 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291550 } else {
[email protected]71f65dd2009-02-11 19:14:561551 *success = false;
1552 *security_style = SECURITY_STYLE_UNKNOWN;
1553 *ssl_cert_status = 0;
1554 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291555 }
1556}
1557
[email protected]71f65dd2009-02-11 19:14:561558void AutomationProvider::GetPageType(int handle, bool* success,
1559 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291560 if (tab_tracker_->ContainsHandle(handle)) {
1561 NavigationController* tab = tab_tracker_->GetResource(handle);
1562 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561563 *page_type = entry->page_type();
1564 *success = true;
initial.commit09911bf2008-07-26 23:55:291565 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341566 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561567 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341568 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561569 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291570 } else {
[email protected]71f65dd2009-02-11 19:14:561571 *success = false;
1572 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291573 }
1574}
1575
[email protected]84abba62009-10-07 17:01:441576void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
1577 int* duration_ms) {
1578 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
1579 event_name);
1580}
1581
[email protected]71f65dd2009-02-11 19:14:561582void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1583 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291584 if (tab_tracker_->ContainsHandle(handle)) {
1585 NavigationController* tab = tab_tracker_->GetResource(handle);
1586 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071587 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401588 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471589 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341590 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291591 if (ssl_blocking_page) {
1592 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:011593 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:561594 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291595 return;
1596 }
1597 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521598 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1599 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561600 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291601 return;
1602 }
1603 }
1604 }
1605 // We failed.
[email protected]457f5cf2009-08-18 16:37:521606 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1607 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561608 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291609}
1610
[email protected]71f65dd2009-02-11 19:14:561611void AutomationProvider::BringBrowserToFront(int browser_handle,
1612 bool* success) {
initial.commit09911bf2008-07-26 23:55:291613 if (browser_tracker_->ContainsHandle(browser_handle)) {
1614 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061615 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561616 *success = true;
initial.commit09911bf2008-07-26 23:55:291617 } else {
[email protected]71f65dd2009-02-11 19:14:561618 *success = false;
initial.commit09911bf2008-07-26 23:55:291619 }
1620}
1621
[email protected]71f65dd2009-02-11 19:14:561622void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1623 int message_num,
1624 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291625 if (browser_tracker_->ContainsHandle(browser_handle)) {
1626 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561627 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141628 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291629 } else {
[email protected]71f65dd2009-02-11 19:14:561630 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291631 }
1632}
1633
[email protected]71f65dd2009-02-11 19:14:561634void AutomationProvider::PrintNow(int tab_handle,
1635 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441636#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571637 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341638 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1639 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291640 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571641 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141642 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341643 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571644 return;
initial.commit09911bf2008-07-26 23:55:291645 }
[email protected]71f65dd2009-02-11 19:14:561646 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1647 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441648#else
1649 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1650 NOTIMPLEMENTED();
1651#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291652}
[email protected]d301c952009-07-13 15:02:411653
[email protected]71f65dd2009-02-11 19:14:561654void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111655 const FilePath& file_name,
1656 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561657 int type,
1658 bool* success) {
initial.commit09911bf2008-07-26 23:55:291659 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561660 *success = false;
initial.commit09911bf2008-07-26 23:55:291661 return;
1662 }
1663
1664 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1665 Browser* browser = FindAndActivateTab(nav);
1666 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141667 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561668 *success = false;
initial.commit09911bf2008-07-26 23:55:291669 return;
1670 }
1671
initial.commit09911bf2008-07-26 23:55:291672 SavePackage::SavePackageType save_type =
1673 static_cast<SavePackage::SavePackageType>(type);
1674 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1675 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341676 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291677
[email protected]71f65dd2009-02-11 19:14:561678 *success = true;
initial.commit09911bf2008-07-26 23:55:291679}
1680
[email protected]5ae5bed2009-08-21 18:52:441681#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471682// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561683void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1684 bool* success,
1685 std::wstring* text) {
1686 *success = false;
initial.commit09911bf2008-07-26 23:55:291687 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561688 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521689 GetText();
[email protected]71f65dd2009-02-11 19:14:561690 *success = true;
initial.commit09911bf2008-07-26 23:55:291691 }
initial.commit09911bf2008-07-26 23:55:291692}
1693
[email protected]71f65dd2009-02-11 19:14:561694void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1695 const std::wstring& text,
1696 bool* success) {
1697 *success = false;
initial.commit09911bf2008-07-26 23:55:291698 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521699 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1700 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561701 *success = true;
initial.commit09911bf2008-07-26 23:55:291702 }
initial.commit09911bf2008-07-26 23:55:291703}
1704
1705void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561706 int autocomplete_edit_handle,
1707 bool* success,
1708 std::vector<AutocompleteMatchData>* matches) {
1709 *success = false;
initial.commit09911bf2008-07-26 23:55:291710 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271711 const AutocompleteResult& result = autocomplete_edit_tracker_->
1712 GetResource(autocomplete_edit_handle)->model()->result();
1713 for (AutocompleteResult::const_iterator i = result.begin();
1714 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561715 matches->push_back(AutocompleteMatchData(*i));
1716 *success = true;
initial.commit09911bf2008-07-26 23:55:291717 }
initial.commit09911bf2008-07-26 23:55:291718}
1719
1720void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561721 int autocomplete_edit_handle,
1722 bool* success,
1723 bool* query_in_progress) {
1724 *success = false;
1725 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291726 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561727 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521728 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561729 *success = true;
initial.commit09911bf2008-07-26 23:55:291730 }
initial.commit09911bf2008-07-26 23:55:291731}
1732
[email protected]28790922009-03-09 19:48:371733void AutomationProvider::OnMessageFromExternalHost(int handle,
1734 const std::string& message,
1735 const std::string& origin,
1736 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191737 RenderViewHost* view_host = GetViewForTab(handle);
1738 if (!view_host) {
1739 return;
[email protected]fa83e762008-08-15 21:41:391740 }
[email protected]f7a68432009-07-29 23:18:191741
1742 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1743 view_host, message, origin, target)) {
1744 // Message was diverted.
1745 return;
1746 }
1747
1748 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1749 origin, target, this, view_host, handle)) {
1750 // Message was diverted.
1751 return;
1752 }
1753
1754 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1755 // Message was diverted.
1756 return;
1757 }
1758
1759 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391760}
[email protected]a9024892009-06-16 23:13:551761
1762bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1763 const std::string& message, const std::string& origin,
1764 const std::string& target) {
1765 if (target !=
1766 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1767 return false;
1768
1769 if (origin != extension_automation_constants::kAutomationOrigin) {
1770 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1771 return false;
1772 }
1773
1774 // The message is a JSON-encoded array with two elements, both strings. The
1775 // first is the name of the event to dispatch. The second is a JSON-encoding
1776 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:201777 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:551778 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1779 LOG(WARNING) << "Invalid browser event specified through automation";
1780 return false;
1781 }
1782
1783 const ListValue* args = static_cast<const ListValue*>(message_value.get());
1784
1785 std::string event_name;
1786 if (!args->GetString(0, &event_name)) {
1787 LOG(WARNING) << "No browser event name specified through automation";
1788 return false;
1789 }
1790
1791 std::string json_args;
1792 if (!args->GetString(1, &json_args)) {
1793 LOG(WARNING) << "No browser event args specified through automation";
1794 return false;
1795 }
1796
[email protected]7120f132009-07-20 21:05:371797 if (profile()->GetExtensionMessageService()) {
[email protected]db7331a2010-02-25 22:10:501798 profile()->GetExtensionMessageService()->DispatchEventToRenderers(
1799 event_name, json_args, profile()->IsOffTheRecord());
[email protected]7120f132009-07-20 21:05:371800 }
[email protected]a9024892009-06-16 23:13:551801
1802 return true;
1803}
[email protected]5ae5bed2009-08-21 18:52:441804#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:391805
[email protected]57c6a652009-05-04 07:58:341806TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:571807 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:571808 if (tab_tracker_->ContainsHandle(handle)) {
1809 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:341810 if (tab)
1811 *tab = nav_controller;
1812 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:571813 }
[email protected]57c6a652009-05-04 07:58:341814 return NULL;
[email protected]20e93d12008-08-28 16:31:571815}
1816
initial.commit09911bf2008-07-26 23:55:291817TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
1818 : AutomationProvider(profile) {
1819 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:141820 registrar_.Add(this, NotificationType::SESSION_END,
1821 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:291822}
1823
1824TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:291825 BrowserList::RemoveObserver(this);
1826}
1827
1828void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:141829 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:291830 AutomationProvider::OnChannelError();
1831}
1832
1833void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
1834 // For backwards compatibility with the testing automation interface, we
1835 // want the automation provider (and hence the process) to go away when the
1836 // last browser goes away.
1837 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:291838 // If you change this, update Observer for NotificationType::SESSION_END
1839 // below.
[email protected]295039bd2008-08-15 04:32:571840 MessageLoop::current()->PostTask(FROM_HERE,
1841 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:291842 }
1843}
1844
1845void TestingAutomationProvider::Observe(NotificationType type,
1846 const NotificationSource& source,
1847 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561848 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:291849 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
1850 // before the task runs resulting in this object not being deleted. This
1851 // Release balance out the Release scheduled by OnBrowserRemoving.
1852 Release();
1853}
[email protected]295039bd2008-08-15 04:32:571854
1855void TestingAutomationProvider::OnRemoveProvider() {
1856 AutomationProviderList::GetInstance()->RemoveProvider(this);
1857}
[email protected]8a3422c92008-09-24 17:42:421858
[email protected]816633a2009-11-11 21:48:181859void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:561860 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:421861 if (tab_tracker_->ContainsHandle(handle)) {
1862 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:341863 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:111864 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421865 }
[email protected]8a3422c92008-09-24 17:42:421866}
1867
[email protected]816633a2009-11-11 21:48:181868void AutomationProvider::ClickInfoBarAccept(int handle,
1869 int info_bar_index,
1870 bool wait_for_navigation,
1871 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421872 bool success = false;
1873 if (tab_tracker_->ContainsHandle(handle)) {
1874 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1875 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:111876 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421877 if (info_bar_index >= 0 && info_bar_index < count) {
1878 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:011879 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:421880 }
[email protected]eb9ba192008-12-02 02:41:341881 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:111882 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:341883 info_bar_index);
1884 if (delegate->AsConfirmInfoBarDelegate())
1885 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:421886 success = true;
1887 }
1888 }
[email protected]4f3dc372009-02-24 00:10:291889 }
[email protected]58f622a62009-10-04 01:17:551890
1891 // This "!wait_for_navigation || !success condition" logic looks suspicious.
1892 // It will send a failure message when success is true but
1893 // |wait_for_navigation| is false.
1894 // TODO(phajdan.jr): investgate whether the reply param (currently
1895 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:421896 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:181897 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:521898 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:421899}
1900
[email protected]71f65dd2009-02-11 19:14:561901void AutomationProvider::GetLastNavigationTime(int handle,
1902 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:421903 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:561904 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:421905}
1906
[email protected]71f65dd2009-02-11 19:14:561907void AutomationProvider::WaitForNavigation(int handle,
1908 int64 last_navigation_time,
1909 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:251910 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:421911 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:251912
[email protected]8a3422c92008-09-24 17:42:421913 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:561914 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:521915 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
1916 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:551917 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:291918 return;
[email protected]8a3422c92008-09-24 17:42:421919 }
1920
[email protected]7dad3d5f2010-03-04 00:27:011921 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:421922}
1923
[email protected]71f65dd2009-02-11 19:14:561924void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161925 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561926 int value,
1927 bool* success) {
1928 *success = false;
[email protected]8a3422c92008-09-24 17:42:421929 if (browser_tracker_->ContainsHandle(handle)) {
1930 Browser* browser = browser_tracker_->GetResource(handle);
1931 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561932 *success = true;
[email protected]8a3422c92008-09-24 17:42:421933 }
[email protected]8a3422c92008-09-24 17:42:421934}
[email protected]97fa6ce32008-12-19 01:48:161935
[email protected]71f65dd2009-02-11 19:14:561936void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161937 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561938 const std::wstring& value,
1939 bool* success) {
1940 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161941 if (browser_tracker_->ContainsHandle(handle)) {
1942 Browser* browser = browser_tracker_->GetResource(handle);
1943 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561944 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161945 }
[email protected]97fa6ce32008-12-19 01:48:161946}
1947
[email protected]71f65dd2009-02-11 19:14:561948void AutomationProvider::GetBooleanPreference(int handle,
1949 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:361950 bool* success,
1951 bool* value) {
[email protected]71f65dd2009-02-11 19:14:561952 *success = false;
1953 *value = false;
[email protected]97fa6ce32008-12-19 01:48:161954 if (browser_tracker_->ContainsHandle(handle)) {
1955 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561956 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
1957 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161958 }
[email protected]97fa6ce32008-12-19 01:48:161959}
1960
[email protected]71f65dd2009-02-11 19:14:561961void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161962 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561963 bool value,
1964 bool* success) {
1965 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161966 if (browser_tracker_->ContainsHandle(handle)) {
1967 Browser* browser = browser_tracker_->GetResource(handle);
1968 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561969 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161970 }
[email protected]97fa6ce32008-12-19 01:48:161971}
1972
1973// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561974void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:401975 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:161976 if (tab_tracker_->ContainsHandle(tab_handle)) {
1977 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1978 Browser* browser = FindAndActivateTab(nav);
1979 DCHECK(browser);
1980
[email protected]57c6a652009-05-04 07:58:341981 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
1982 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:161983 }
[email protected]97fa6ce32008-12-19 01:48:161984}
1985
[email protected]b8f48d12009-11-09 20:14:361986// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561987void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:401988 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:561989 bool* success) {
1990 *success = false;
[email protected]de246f52009-02-25 18:25:451991#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:161992 if (tab_tracker_->ContainsHandle(tab_handle)) {
1993 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1994 Browser* browser = FindAndActivateTab(nav);
1995 DCHECK(browser);
1996
[email protected]1fc025202009-01-20 23:03:141997 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:161998 int selected_encoding_id =
1999 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2000 if (selected_encoding_id) {
2001 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562002 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162003 }
2004 }
2005 }
[email protected]de246f52009-02-25 18:25:452006#else
2007 // TODO(port): Enable when encoding-related parts of Browser are ported.
2008 NOTIMPLEMENTED();
2009#endif
[email protected]97fa6ce32008-12-19 01:48:162010}
[email protected]5bcdb312009-01-07 21:43:202011
[email protected]4d434a1a2009-02-11 21:06:572012void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202013 SavePackage::SetShouldPromptUser(should_prompt);
2014}
[email protected]87eab222009-03-13 00:47:452015
[email protected]66ba4932009-06-04 19:22:132016void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2017 *count = -1; // -1 is the error code
2018 if (tab_tracker_->ContainsHandle(handle)) {
2019 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2020 TabContents* tab_contents = nav_controller->tab_contents();
2021 if (tab_contents) {
2022 BlockedPopupContainer* container =
2023 tab_contents->blocked_popup_container();
2024 if (container) {
2025 *count = static_cast<int>(container->GetBlockedPopupCount());
2026 } else {
2027 // If we don't have a container, we don't have any blocked popups to
2028 // contain!
2029 *count = 0;
2030 }
2031 }
2032 }
2033}
[email protected]f7a68432009-07-29 23:18:192034
2035void AutomationProvider::SelectAll(int tab_handle) {
2036 RenderViewHost* view = GetViewForTab(tab_handle);
2037 if (!view) {
2038 NOTREACHED();
2039 return;
2040 }
2041
2042 view->SelectAll();
2043}
2044
2045void AutomationProvider::Cut(int tab_handle) {
2046 RenderViewHost* view = GetViewForTab(tab_handle);
2047 if (!view) {
2048 NOTREACHED();
2049 return;
2050 }
2051
2052 view->Cut();
2053}
2054
2055void AutomationProvider::Copy(int tab_handle) {
2056 RenderViewHost* view = GetViewForTab(tab_handle);
2057 if (!view) {
2058 NOTREACHED();
2059 return;
2060 }
2061
2062 view->Copy();
2063}
2064
2065void AutomationProvider::Paste(int tab_handle) {
2066 RenderViewHost* view = GetViewForTab(tab_handle);
2067 if (!view) {
2068 NOTREACHED();
2069 return;
2070 }
2071
2072 view->Paste();
2073}
2074
2075void AutomationProvider::ReloadAsync(int tab_handle) {
2076 if (tab_tracker_->ContainsHandle(tab_handle)) {
2077 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2078 if (!tab) {
2079 NOTREACHED();
2080 return;
2081 }
2082
2083 tab->Reload(false);
2084 }
2085}
2086
2087void AutomationProvider::StopAsync(int tab_handle) {
2088 RenderViewHost* view = GetViewForTab(tab_handle);
2089 if (!view) {
[email protected]8b2b3312009-09-14 18:38:362090 // We tolerate StopAsync being called even before a view has been created.
2091 // So just log a warning instead of a NOTREACHED().
2092 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:192093 return;
2094 }
2095
2096 view->Stop();
2097}
2098
[email protected]1bb5f892009-10-06 01:44:572099void AutomationProvider::OnSetPageFontSize(int tab_handle,
2100 int font_size) {
2101 AutomationPageFontSize automation_font_size =
2102 static_cast<AutomationPageFontSize>(font_size);
2103
2104 if (automation_font_size < SMALLEST_FONT ||
2105 automation_font_size > LARGEST_FONT) {
2106 DLOG(ERROR) << "Invalid font size specified : "
2107 << font_size;
2108 return;
2109 }
2110
2111 if (tab_tracker_->ContainsHandle(tab_handle)) {
2112 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2113 DCHECK(tab != NULL);
2114 if (tab && tab->tab_contents()) {
2115 DCHECK(tab->tab_contents()->profile() != NULL);
2116 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2117 prefs::kWebKitDefaultFontSize, font_size);
2118 }
2119 }
2120}
2121
2122
[email protected]2949e90d2009-08-21 15:32:522123void AutomationProvider::WaitForBrowserWindowCountToBecome(
2124 int target_count, IPC::Message* reply_message) {
2125 if (static_cast<int>(BrowserList::size()) == target_count) {
2126 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2127 reply_message, true);
2128 Send(reply_message);
2129 return;
2130 }
2131
2132 // Set up an observer (it will delete itself).
2133 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2134}
2135
2136void AutomationProvider::WaitForAppModalDialogToBeShown(
2137 IPC::Message* reply_message) {
2138 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2139 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2140 reply_message, true);
2141 Send(reply_message);
2142 return;
2143 }
2144
2145 // Set up an observer (it will delete itself).
2146 new AppModalDialogShownObserver(this, reply_message);
2147}
2148
[email protected]1126a1d32009-08-26 15:39:262149void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2150 int handle, int number_of_navigations, IPC::Message* reply_message) {
2151 if (tab_tracker_->ContainsHandle(handle)) {
2152 NavigationController* tab = tab_tracker_->GetResource(handle);
2153 Browser* browser = FindAndActivateTab(tab);
2154 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:012155 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2156 false);
[email protected]1126a1d32009-08-26 15:39:262157 browser->GoBack(CURRENT_TAB);
2158 return;
2159 }
2160 }
2161
2162 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2163 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2164 Send(reply_message);
2165}
2166
2167void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2168 int handle, int number_of_navigations, IPC::Message* reply_message) {
2169 if (tab_tracker_->ContainsHandle(handle)) {
2170 NavigationController* tab = tab_tracker_->GetResource(handle);
2171 Browser* browser = FindAndActivateTab(tab);
2172 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:012173 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2174 false);
[email protected]1126a1d32009-08-26 15:39:262175 browser->GoForward(CURRENT_TAB);
2176 return;
2177 }
2178 }
2179
2180 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2181 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2182 Send(reply_message);
2183}
2184
[email protected]f7a68432009-07-29 23:18:192185RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2186 if (tab_tracker_->ContainsHandle(tab_handle)) {
2187 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2188 if (!tab) {
2189 NOTREACHED();
2190 return NULL;
2191 }
2192
2193 TabContents* tab_contents = tab->tab_contents();
2194 if (!tab_contents) {
2195 NOTREACHED();
2196 return NULL;
2197 }
2198
2199 RenderViewHost* view_host = tab_contents->render_view_host();
2200 return view_host;
2201 }
2202
2203 return NULL;
2204}
[email protected]675595f2009-08-26 22:32:042205
2206void AutomationProvider::GetBrowserForWindow(int window_handle,
2207 bool* success,
2208 int* browser_handle) {
2209 *success = false;
2210 *browser_handle = 0;
2211
2212 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2213 if (!window)
2214 return;
2215
2216 BrowserList::const_iterator iter = BrowserList::begin();
2217 for (;iter != BrowserList::end(); ++iter) {
2218 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2219 if (window == this_window) {
2220 // Add() returns the existing handle for the resource if any.
2221 *browser_handle = browser_tracker_->Add(*iter);
2222 *success = true;
2223 return;
2224 }
2225 }
2226}
[email protected]d11c8e92009-10-20 23:26:402227
2228void AutomationProvider::InstallExtension(const FilePath& crx_path,
2229 IPC::Message* reply_message) {
2230 ExtensionsService* service = profile_->GetExtensionsService();
2231 if (service) {
2232 // The observer will delete itself when done.
2233 new ExtensionNotificationObserver(this,
2234 AutomationMsg_InstallExtension::ID,
2235 reply_message);
2236
2237 const FilePath& install_dir = service->install_directory();
[email protected]d11c8e92009-10-20 23:26:402238 CrxInstaller::Start(crx_path,
2239 install_dir,
2240 Extension::INTERNAL,
2241 "", // no expected id
2242 false, // please do not delete crx file
2243 true, // privilege increase allowed
[email protected]d11c8e92009-10-20 23:26:402244 service,
2245 NULL); // silent isntall, no UI
2246 } else {
2247 AutomationMsg_InstallExtension::WriteReplyParams(
2248 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2249 Send(reply_message);
2250 }
2251}
2252
2253void AutomationProvider::LoadExpandedExtension(
2254 const FilePath& extension_dir,
2255 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:382256 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:402257 // The observer will delete itself when done.
2258 new ExtensionNotificationObserver(this,
2259 AutomationMsg_LoadExpandedExtension::ID,
2260 reply_message);
2261
2262 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:402263 } else {
2264 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
2265 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2266 Send(reply_message);
2267 }
2268}
[email protected]673fd2c02010-02-04 23:10:002269
2270void AutomationProvider::SaveAsAsync(int tab_handle) {
2271 NavigationController* tab = NULL;
2272 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2273 if (tab_contents)
2274 tab_contents->OnSavePage();
2275}
[email protected]7dad3d5f2010-03-04 00:27:012276
2277void AutomationProvider::SetContentSetting(
2278 int handle,
2279 const std::string& host,
2280 ContentSettingsType content_type,
2281 ContentSetting setting,
2282 bool* success) {
2283 *success = false;
2284 if (browser_tracker_->ContainsHandle(handle)) {
2285 Browser* browser = browser_tracker_->GetResource(handle);
2286 HostContentSettingsMap* map =
2287 browser->profile()->GetHostContentSettingsMap();
2288 if (host.empty()) {
2289 map->SetDefaultContentSetting(content_type, setting);
2290 } else {
2291 map->SetContentSetting(host, content_type, setting);
2292 }
2293 *success = true;
2294 }
2295}