blob: 6a29cba25ec46e4fb8c7c2372ad8e3ede01fedc3 [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;
[email protected]0bc24482010-03-05 00:33:10999 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291000 if (success) {
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 }
1004
[email protected]4f3dc372009-02-24 00:10:291005 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301006
[email protected]71f65dd2009-02-11 19:14:561007 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:171008 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561009 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291010}
1011
1012bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571013 DCHECK(channel_.get());
1014 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291015}
1016
1017Browser* AutomationProvider::FindAndActivateTab(
1018 NavigationController* controller) {
1019 int tab_index;
1020 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1021 if (browser)
1022 browser->SelectTabContentsAt(tab_index, true);
1023
1024 return browser;
1025}
1026
[email protected]71f65dd2009-02-11 19:14:561027void AutomationProvider::GetCookies(const GURL& url, int handle,
1028 int* value_size,
1029 std::string* value) {
1030 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291031 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1032 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311033
1034 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001035 scoped_refptr<URLRequestContextGetter> request_context =
1036 tab->tab_contents()->request_context();
1037 if (!request_context.get())
1038 request_context = tab->profile()->GetRequestContext();
1039
1040 net::CookieStore* cookie_store = request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311041
1042 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561043 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291044 }
initial.commit09911bf2008-07-26 23:55:291045}
1046
[email protected]71f65dd2009-02-11 19:14:561047void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291048 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561049 int handle,
1050 int* response_value) {
1051 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291052
1053 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1054 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311055
[email protected]dfa46e5f2009-11-17 18:48:431056 scoped_refptr<URLRequestContextGetter> request_context =
1057 tab->tab_contents()->request_context();
1058 if (!request_context.get())
1059 request_context = tab->profile()->GetRequestContext();
1060
[email protected]be180c802009-10-23 06:33:311061 // Since we are running on the UI thread don't call GetURLRequestContext().
1062 scoped_refptr<net::CookieStore> cookie_store =
[email protected]dfa46e5f2009-11-17 18:48:431063 request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311064
1065 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561066 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291067 }
initial.commit09911bf2008-07-26 23:55:291068}
1069
[email protected]71f65dd2009-02-11 19:14:561070void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1071 *success = false;
initial.commit09911bf2008-07-26 23:55:291072 if (tab_tracker_->ContainsHandle(handle)) {
1073 NavigationController* tab = tab_tracker_->GetResource(handle);
1074 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541075 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561076 *success = true;
initial.commit09911bf2008-07-26 23:55:291077 }
initial.commit09911bf2008-07-26 23:55:291078}
1079
[email protected]71f65dd2009-02-11 19:14:561080void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1081 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291082
1083 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561084 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341085 TabContents* tab_contents =
1086 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161087 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1088 if (rph)
1089 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291090 }
initial.commit09911bf2008-07-26 23:55:291091}
1092
1093void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331094 NOTREACHED() << "This function has been deprecated. "
1095 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291096}
1097
[email protected]71f65dd2009-02-11 19:14:561098void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291099 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561100 const std::wstring& script,
1101 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291102 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341103 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1104 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571105 // Set the routing id of this message with the controller.
1106 // This routing id needs to be remembered for the reverse
1107 // communication while sending back the response of
1108 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331109 std::wstring set_automation_id;
1110 SStringPrintf(&set_automation_id,
1111 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561112 reply_message->routing_id());
1113
1114 DCHECK(reply_message_ == NULL);
1115 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291116
[email protected]57c6a652009-05-04 07:58:341117 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331118 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341119 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061120 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571121 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291122 }
1123
1124 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561125 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1126 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291127 }
1128}
1129
[email protected]71f65dd2009-02-11 19:14:561130void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1131 *visible = false;
[email protected]20e93d12008-08-28 16:31:571132
[email protected]59560e0b2009-06-04 03:30:221133 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251134#if defined(OS_CHROMEOS)
1135 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1136 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1137 for (BrowserList::const_iterator it = BrowserList::begin();
1138 it != BrowserList::end(); ++it) {
1139 if ((*it)->type() == Browser::TYPE_POPUP) {
1140 const GURL& url =
1141 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1142
1143 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1144 url.host() == chrome::kChromeUIFileBrowseHost) {
1145 *visible = true;
1146 break;
1147 }
1148 }
1149 }
1150#else
[email protected]59560e0b2009-06-04 03:30:221151 Browser* browser = browser_tracker_->GetResource(handle);
1152 if (browser) {
1153 *visible = browser->window()->IsDownloadShelfVisible();
1154 }
[email protected]f5bf8ccf2010-02-05 18:19:251155#endif
[email protected]59560e0b2009-06-04 03:30:221156 }
initial.commit09911bf2008-07-26 23:55:291157}
1158
[email protected]59560e0b2009-06-04 03:30:221159void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1160 if (browser_tracker_->ContainsHandle(handle)) {
1161 Browser* browser = browser_tracker_->GetResource(handle);
1162 if (browser) {
1163 if (visible)
1164 browser->window()->GetDownloadShelf()->Show();
1165 else
1166 browser->window()->GetDownloadShelf()->Close();
1167 }
1168 }
1169}
1170
[email protected]34930432009-11-09 00:12:091171void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1172 *visible = false;
1173
1174 if (browser_tracker_->ContainsHandle(handle)) {
1175 Browser* browser = browser_tracker_->GetResource(handle);
1176 if (browser)
1177 *visible = browser->window()->IsFullscreen();
1178 }
1179}
1180
1181void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1182 bool* visible) {
1183 *visible = false;
1184
1185 if (browser_tracker_->ContainsHandle(handle)) {
1186 Browser* browser = browser_tracker_->GetResource(handle);
1187 if (browser)
1188 *visible = browser->window()->IsFullscreenBubbleVisible();
1189 }
1190}
[email protected]59560e0b2009-06-04 03:30:221191
[email protected]71f65dd2009-02-11 19:14:561192void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1193 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291194 if (tab_tracker_->ContainsHandle(handle)) {
1195 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111196 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291197 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561198 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291199 }
1200 }
initial.commit09911bf2008-07-26 23:55:291201}
1202
initial.commit09911bf2008-07-26 23:55:291203void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561204 int handle, const std::wstring& find_request,
1205 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311206 NOTREACHED() << "This function has been deprecated."
1207 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561208 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311209 return;
1210}
1211
[email protected]4f999132009-03-31 18:08:401212void AutomationProvider::HandleFindRequest(
1213 int handle,
1214 const AutomationMsg_Find_Params& params,
1215 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291216 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561217 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1218 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291219 return;
1220 }
1221
1222 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111223 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291224
1225 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141226 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291227
[email protected]57c6a652009-05-04 07:58:341228 tab_contents->set_current_find_request_id(
1229 FindInPageNotificationObserver::kFindInPageRequestId);
1230 tab_contents->render_view_host()->StartFinding(
1231 FindInPageNotificationObserver::kFindInPageRequestId,
1232 params.search_string, params.forward, params.match_case,
1233 params.find_next);
initial.commit09911bf2008-07-26 23:55:291234}
1235
[email protected]5f8af2a2008-08-06 22:49:451236void AutomationProvider::HandleOpenFindInPageRequest(
1237 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291238 if (browser_tracker_->ContainsHandle(handle)) {
1239 Browser* browser = browser_tracker_->GetResource(handle);
1240 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451241 }
1242}
1243
[email protected]71f65dd2009-02-11 19:14:561244void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561245 *visible = false;
[email protected]855c0142009-09-28 22:35:241246 Browser* browser = browser_tracker_->GetResource(handle);
1247 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581248 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171249 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241250 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291251 }
[email protected]20e93d12008-08-28 16:31:571252}
1253
[email protected]71f65dd2009-02-11 19:14:561254void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1255 int* y) {
[email protected]9e0534b2008-10-21 15:03:011256 gfx::Point position(0, 0);
1257 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291258 if (browser_tracker_->ContainsHandle(handle)) {
1259 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581260 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171261 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581262 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291263 }
[email protected]20e93d12008-08-28 16:31:571264
[email protected]71f65dd2009-02-11 19:14:561265 *x = position.x();
1266 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571267}
1268
[email protected]c3240722010-03-05 21:52:581269void AutomationProvider::GetBookmarkBarVisibility(int handle,
1270 bool* visible,
1271 bool* animating) {
1272 *visible = false;
1273 *animating = false;
1274
1275 if (browser_tracker_->ContainsHandle(handle)) {
1276 Browser* browser = browser_tracker_->GetResource(handle);
1277 if (browser) {
1278 *visible = browser->window()->IsBookmarkBarVisible();
1279 *animating = browser->window()->IsBookmarkBarAnimating();
1280 }
1281 }
1282}
1283
initial.commit09911bf2008-07-26 23:55:291284void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561285 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341286 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1287 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561288 DCHECK(reply_message_ == NULL);
1289 reply_message_ = reply_message;
1290
[email protected]d9f9b792009-06-24 13:17:121291 DevToolsManager::GetInstance()->InspectElement(
1292 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291293 } else {
[email protected]71f65dd2009-02-11 19:14:561294 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1295 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291296 }
1297}
1298
1299void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091300 if (reply_message_) {
1301 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1302 num_resources);
1303 Send(reply_message_);
1304 reply_message_ = NULL;
1305 }
initial.commit09911bf2008-07-26 23:55:291306}
1307
[email protected]a7eee32f2009-05-22 18:08:171308class SetProxyConfigTask : public Task {
1309 public:
[email protected]be180c802009-10-23 06:33:311310 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
1311 const std::string& new_proxy_config)
1312 : request_context_getter_(request_context_getter), proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:171313 virtual void Run() {
1314 // First, deserialize the JSON string. If this fails, log and bail.
1315 JSONStringValueSerializer deserializer(proxy_config_);
1316 std::string error_message;
1317 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1318 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1319 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1320 << error_message;
1321 return;
1322 }
1323
1324 scoped_ptr<DictionaryValue> dict(
1325 static_cast<DictionaryValue*>(root.release()));
1326 // Now put together a proxy configuration from the deserialized string.
1327 net::ProxyConfig pc;
1328 PopulateProxyConfig(*dict.get(), &pc);
1329
[email protected]be180c802009-10-23 06:33:311330 net::ProxyService* proxy_service =
1331 request_context_getter_->GetURLRequestContext()->proxy_service();
1332 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:171333 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1334 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:311335 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:171336 }
1337
1338 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1339 DCHECK(pc);
1340 bool no_proxy = false;
1341 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1342 // Make no changes to the ProxyConfig.
1343 return;
1344 }
1345 bool auto_config;
1346 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:481347 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:171348 }
1349 std::string pac_url;
1350 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:481351 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:171352 }
1353 std::string proxy_bypass_list;
1354 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:481355 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:171356 }
1357 std::string proxy_server;
1358 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:481359 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:171360 }
1361 }
1362
1363 private:
[email protected]be180c802009-10-23 06:33:311364 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:171365 std::string proxy_config_;
1366};
1367
1368
1369void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:311370 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
1371 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:171372 FilePath user_data_dir;
1373 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1374 ProfileManager* profile_manager = g_browser_process->profile_manager();
1375 DCHECK(profile_manager);
1376 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1377 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:311378 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171379 }
[email protected]be180c802009-10-23 06:33:311380 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:171381
[email protected]fae20792009-10-28 20:31:581382 ChromeThread::PostTask(
1383 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:311384 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171385}
1386
[email protected]4f3dc372009-02-24 00:10:291387void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331388 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291389 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291390 if (tab_tracker_->ContainsHandle(handle)) {
1391 NavigationController* tab = tab_tracker_->GetResource(handle);
1392 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1393 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331394 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291395 }
initial.commit09911bf2008-07-26 23:55:291396}
1397
[email protected]6a5670d22009-10-27 16:21:341398void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:141399 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:531400 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
1401 reply_message);
1402}
1403
1404void AutomationProvider::OpenNewBrowserWindowOfType(
1405 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:141406 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291407 // We may have no current browser windows open so don't rely on
1408 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:531409 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
1410 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:051411 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151412 if (show)
[email protected]15952e462008-11-14 00:29:051413 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291414}
1415
[email protected]71f65dd2009-02-11 19:14:561416void AutomationProvider::GetWindowForBrowser(int browser_handle,
1417 bool* success,
1418 int* handle) {
1419 *success = false;
1420 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291421
1422 if (browser_tracker_->ContainsHandle(browser_handle)) {
1423 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201424 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291425 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201426 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561427 *success = true;
initial.commit09911bf2008-07-26 23:55:291428 }
initial.commit09911bf2008-07-26 23:55:291429}
1430
[email protected]5ae5bed2009-08-21 18:52:441431#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291432void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561433 int browser_handle,
1434 bool* success,
1435 int* autocomplete_edit_handle) {
1436 *success = false;
1437 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291438
1439 if (browser_tracker_->ContainsHandle(browser_handle)) {
1440 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061441 LocationBar* loc_bar = browser->window()->GetLocationBar();
1442 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291443 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561444 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1445 *success = true;
initial.commit09911bf2008-07-26 23:55:291446 }
initial.commit09911bf2008-07-26 23:55:291447}
[email protected]5ae5bed2009-08-21 18:52:441448#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291449
[email protected]71f65dd2009-02-11 19:14:561450void AutomationProvider::ShowInterstitialPage(int tab_handle,
1451 const std::string& html_text,
1452 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291453 if (tab_tracker_->ContainsHandle(tab_handle)) {
1454 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111455 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401456
[email protected]7dad3d5f2010-03-04 00:27:011457 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:401458 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341459 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401460 GURL("about:interstitial"),
1461 html_text);
1462 interstitial->Show();
1463 return;
initial.commit09911bf2008-07-26 23:55:291464 }
[email protected]71f65dd2009-02-11 19:14:561465
[email protected]457f5cf2009-08-18 16:37:521466 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1467 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561468 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291469}
1470
[email protected]71f65dd2009-02-11 19:14:561471void AutomationProvider::HideInterstitialPage(int tab_handle,
1472 bool* success) {
1473 *success = false;
[email protected]57c6a652009-05-04 07:58:341474 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1475 if (tab_contents && tab_contents->interstitial_page()) {
1476 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561477 *success = true;
initial.commit09911bf2008-07-26 23:55:291478 }
initial.commit09911bf2008-07-26 23:55:291479}
1480
[email protected]71f65dd2009-02-11 19:14:561481void AutomationProvider::CloseTab(int tab_handle,
1482 bool wait_until_closed,
1483 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291484 if (tab_tracker_->ContainsHandle(tab_handle)) {
1485 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1486 int index;
1487 Browser* browser = Browser::GetBrowserForController(controller, &index);
1488 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141489 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111490 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451491 return;
initial.commit09911bf2008-07-26 23:55:291492 }
[email protected]de246f52009-02-25 18:25:451493
1494 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:551495 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291496}
1497
[email protected]71f65dd2009-02-11 19:14:561498void AutomationProvider::CloseBrowser(int browser_handle,
1499 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291500 if (browser_tracker_->ContainsHandle(browser_handle)) {
1501 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561502 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561503 reply_message);
[email protected]f3e99e32008-07-30 04:48:391504 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291505 } else {
1506 NOTREACHED();
1507 }
1508}
1509
[email protected]71f65dd2009-02-11 19:14:561510void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1511 if (browser_tracker_->ContainsHandle(browser_handle)) {
1512 Browser* browser = browser_tracker_->GetResource(browser_handle);
1513 browser->window()->Close();
1514 } else {
1515 NOTREACHED();
1516 }
1517}
1518
[email protected]71f65dd2009-02-11 19:14:561519void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:571520 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:561521 AutomationMsg_NavigationResponseValues* status) {
1522 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291523
1524 if (tab_tracker_->ContainsHandle(handle)) {
1525 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:571526 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561527 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291528 }
initial.commit09911bf2008-07-26 23:55:291529}
1530
[email protected]4150ef02009-08-19 23:14:261531void AutomationProvider::NavigateExternalTabAtIndex(
1532 int handle, int navigation_index,
1533 AutomationMsg_NavigationResponseValues* status) {
1534 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1535
1536 if (tab_tracker_->ContainsHandle(handle)) {
1537 NavigationController* tab = tab_tracker_->GetResource(handle);
1538 tab->GoToIndex(navigation_index);
1539 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1540 }
1541}
1542
[email protected]71f65dd2009-02-11 19:14:561543void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1544 IPC::Message* reply_message) {
1545 if (tab_tracker_->ContainsHandle(tab_handle)) {
1546 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1547 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141548 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561549 }
1550}
1551
[email protected]71f65dd2009-02-11 19:14:561552void AutomationProvider::GetSecurityState(int handle, bool* success,
1553 SecurityStyle* security_style,
1554 int* ssl_cert_status,
1555 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291556 if (tab_tracker_->ContainsHandle(handle)) {
1557 NavigationController* tab = tab_tracker_->GetResource(handle);
1558 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561559 *success = true;
1560 *security_style = entry->ssl().security_style();
1561 *ssl_cert_status = entry->ssl().cert_status();
1562 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291563 } else {
[email protected]71f65dd2009-02-11 19:14:561564 *success = false;
1565 *security_style = SECURITY_STYLE_UNKNOWN;
1566 *ssl_cert_status = 0;
1567 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291568 }
1569}
1570
[email protected]71f65dd2009-02-11 19:14:561571void AutomationProvider::GetPageType(int handle, bool* success,
1572 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291573 if (tab_tracker_->ContainsHandle(handle)) {
1574 NavigationController* tab = tab_tracker_->GetResource(handle);
1575 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561576 *page_type = entry->page_type();
1577 *success = true;
initial.commit09911bf2008-07-26 23:55:291578 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341579 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561580 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341581 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561582 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291583 } else {
[email protected]71f65dd2009-02-11 19:14:561584 *success = false;
1585 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291586 }
1587}
1588
[email protected]84abba62009-10-07 17:01:441589void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
1590 int* duration_ms) {
1591 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
1592 event_name);
1593}
1594
[email protected]71f65dd2009-02-11 19:14:561595void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1596 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291597 if (tab_tracker_->ContainsHandle(handle)) {
1598 NavigationController* tab = tab_tracker_->GetResource(handle);
1599 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071600 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401601 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471602 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341603 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291604 if (ssl_blocking_page) {
1605 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:011606 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:561607 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291608 return;
1609 }
1610 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521611 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1612 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561613 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291614 return;
1615 }
1616 }
1617 }
1618 // We failed.
[email protected]457f5cf2009-08-18 16:37:521619 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1620 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561621 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291622}
1623
[email protected]71f65dd2009-02-11 19:14:561624void AutomationProvider::BringBrowserToFront(int browser_handle,
1625 bool* success) {
initial.commit09911bf2008-07-26 23:55:291626 if (browser_tracker_->ContainsHandle(browser_handle)) {
1627 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061628 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561629 *success = true;
initial.commit09911bf2008-07-26 23:55:291630 } else {
[email protected]71f65dd2009-02-11 19:14:561631 *success = false;
initial.commit09911bf2008-07-26 23:55:291632 }
1633}
1634
[email protected]71f65dd2009-02-11 19:14:561635void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1636 int message_num,
1637 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291638 if (browser_tracker_->ContainsHandle(browser_handle)) {
1639 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561640 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141641 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291642 } else {
[email protected]71f65dd2009-02-11 19:14:561643 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291644 }
1645}
1646
[email protected]71f65dd2009-02-11 19:14:561647void AutomationProvider::PrintNow(int tab_handle,
1648 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441649#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571650 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341651 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1652 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291653 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571654 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141655 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341656 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571657 return;
initial.commit09911bf2008-07-26 23:55:291658 }
[email protected]71f65dd2009-02-11 19:14:561659 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1660 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441661#else
1662 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1663 NOTIMPLEMENTED();
1664#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291665}
[email protected]d301c952009-07-13 15:02:411666
[email protected]71f65dd2009-02-11 19:14:561667void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111668 const FilePath& file_name,
1669 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561670 int type,
1671 bool* success) {
initial.commit09911bf2008-07-26 23:55:291672 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561673 *success = false;
initial.commit09911bf2008-07-26 23:55:291674 return;
1675 }
1676
1677 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1678 Browser* browser = FindAndActivateTab(nav);
1679 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141680 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561681 *success = false;
initial.commit09911bf2008-07-26 23:55:291682 return;
1683 }
1684
initial.commit09911bf2008-07-26 23:55:291685 SavePackage::SavePackageType save_type =
1686 static_cast<SavePackage::SavePackageType>(type);
1687 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1688 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341689 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291690
[email protected]71f65dd2009-02-11 19:14:561691 *success = true;
initial.commit09911bf2008-07-26 23:55:291692}
1693
[email protected]5ae5bed2009-08-21 18:52:441694#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471695// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561696void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1697 bool* success,
1698 std::wstring* text) {
1699 *success = false;
initial.commit09911bf2008-07-26 23:55:291700 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561701 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521702 GetText();
[email protected]71f65dd2009-02-11 19:14:561703 *success = true;
initial.commit09911bf2008-07-26 23:55:291704 }
initial.commit09911bf2008-07-26 23:55:291705}
1706
[email protected]71f65dd2009-02-11 19:14:561707void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1708 const std::wstring& text,
1709 bool* success) {
1710 *success = false;
initial.commit09911bf2008-07-26 23:55:291711 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521712 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1713 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561714 *success = true;
initial.commit09911bf2008-07-26 23:55:291715 }
initial.commit09911bf2008-07-26 23:55:291716}
1717
1718void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561719 int autocomplete_edit_handle,
1720 bool* success,
1721 std::vector<AutocompleteMatchData>* matches) {
1722 *success = false;
initial.commit09911bf2008-07-26 23:55:291723 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271724 const AutocompleteResult& result = autocomplete_edit_tracker_->
1725 GetResource(autocomplete_edit_handle)->model()->result();
1726 for (AutocompleteResult::const_iterator i = result.begin();
1727 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561728 matches->push_back(AutocompleteMatchData(*i));
1729 *success = true;
initial.commit09911bf2008-07-26 23:55:291730 }
initial.commit09911bf2008-07-26 23:55:291731}
1732
1733void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561734 int autocomplete_edit_handle,
1735 bool* success,
1736 bool* query_in_progress) {
1737 *success = false;
1738 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291739 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561740 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521741 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561742 *success = true;
initial.commit09911bf2008-07-26 23:55:291743 }
initial.commit09911bf2008-07-26 23:55:291744}
1745
[email protected]28790922009-03-09 19:48:371746void AutomationProvider::OnMessageFromExternalHost(int handle,
1747 const std::string& message,
1748 const std::string& origin,
1749 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191750 RenderViewHost* view_host = GetViewForTab(handle);
1751 if (!view_host) {
1752 return;
[email protected]fa83e762008-08-15 21:41:391753 }
[email protected]f7a68432009-07-29 23:18:191754
1755 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1756 view_host, message, origin, target)) {
1757 // Message was diverted.
1758 return;
1759 }
1760
1761 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1762 origin, target, this, view_host, handle)) {
1763 // Message was diverted.
1764 return;
1765 }
1766
1767 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1768 // Message was diverted.
1769 return;
1770 }
1771
1772 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391773}
[email protected]a9024892009-06-16 23:13:551774
1775bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1776 const std::string& message, const std::string& origin,
1777 const std::string& target) {
1778 if (target !=
1779 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1780 return false;
1781
1782 if (origin != extension_automation_constants::kAutomationOrigin) {
1783 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1784 return false;
1785 }
1786
1787 // The message is a JSON-encoded array with two elements, both strings. The
1788 // first is the name of the event to dispatch. The second is a JSON-encoding
1789 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:201790 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:551791 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1792 LOG(WARNING) << "Invalid browser event specified through automation";
1793 return false;
1794 }
1795
1796 const ListValue* args = static_cast<const ListValue*>(message_value.get());
1797
1798 std::string event_name;
1799 if (!args->GetString(0, &event_name)) {
1800 LOG(WARNING) << "No browser event name specified through automation";
1801 return false;
1802 }
1803
1804 std::string json_args;
1805 if (!args->GetString(1, &json_args)) {
1806 LOG(WARNING) << "No browser event args specified through automation";
1807 return false;
1808 }
1809
[email protected]7120f132009-07-20 21:05:371810 if (profile()->GetExtensionMessageService()) {
[email protected]db7331a2010-02-25 22:10:501811 profile()->GetExtensionMessageService()->DispatchEventToRenderers(
1812 event_name, json_args, profile()->IsOffTheRecord());
[email protected]7120f132009-07-20 21:05:371813 }
[email protected]a9024892009-06-16 23:13:551814
1815 return true;
1816}
[email protected]5ae5bed2009-08-21 18:52:441817#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:391818
[email protected]57c6a652009-05-04 07:58:341819TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:571820 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:571821 if (tab_tracker_->ContainsHandle(handle)) {
1822 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:341823 if (tab)
1824 *tab = nav_controller;
1825 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:571826 }
[email protected]57c6a652009-05-04 07:58:341827 return NULL;
[email protected]20e93d12008-08-28 16:31:571828}
1829
initial.commit09911bf2008-07-26 23:55:291830TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
1831 : AutomationProvider(profile) {
1832 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:141833 registrar_.Add(this, NotificationType::SESSION_END,
1834 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:291835}
1836
1837TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:291838 BrowserList::RemoveObserver(this);
1839}
1840
1841void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:141842 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:291843 AutomationProvider::OnChannelError();
1844}
1845
1846void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
1847 // For backwards compatibility with the testing automation interface, we
1848 // want the automation provider (and hence the process) to go away when the
1849 // last browser goes away.
1850 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:291851 // If you change this, update Observer for NotificationType::SESSION_END
1852 // below.
[email protected]295039bd2008-08-15 04:32:571853 MessageLoop::current()->PostTask(FROM_HERE,
1854 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:291855 }
1856}
1857
1858void TestingAutomationProvider::Observe(NotificationType type,
1859 const NotificationSource& source,
1860 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561861 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:291862 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
1863 // before the task runs resulting in this object not being deleted. This
1864 // Release balance out the Release scheduled by OnBrowserRemoving.
1865 Release();
1866}
[email protected]295039bd2008-08-15 04:32:571867
1868void TestingAutomationProvider::OnRemoveProvider() {
1869 AutomationProviderList::GetInstance()->RemoveProvider(this);
1870}
[email protected]8a3422c92008-09-24 17:42:421871
[email protected]816633a2009-11-11 21:48:181872void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:561873 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:421874 if (tab_tracker_->ContainsHandle(handle)) {
1875 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:341876 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:111877 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421878 }
[email protected]8a3422c92008-09-24 17:42:421879}
1880
[email protected]816633a2009-11-11 21:48:181881void AutomationProvider::ClickInfoBarAccept(int handle,
1882 int info_bar_index,
1883 bool wait_for_navigation,
1884 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421885 bool success = false;
1886 if (tab_tracker_->ContainsHandle(handle)) {
1887 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1888 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:111889 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421890 if (info_bar_index >= 0 && info_bar_index < count) {
1891 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:011892 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:421893 }
[email protected]eb9ba192008-12-02 02:41:341894 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:111895 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:341896 info_bar_index);
1897 if (delegate->AsConfirmInfoBarDelegate())
1898 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:421899 success = true;
1900 }
1901 }
[email protected]4f3dc372009-02-24 00:10:291902 }
[email protected]58f622a62009-10-04 01:17:551903
1904 // This "!wait_for_navigation || !success condition" logic looks suspicious.
1905 // It will send a failure message when success is true but
1906 // |wait_for_navigation| is false.
1907 // TODO(phajdan.jr): investgate whether the reply param (currently
1908 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:421909 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:181910 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:521911 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:421912}
1913
[email protected]71f65dd2009-02-11 19:14:561914void AutomationProvider::GetLastNavigationTime(int handle,
1915 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:421916 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:561917 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:421918}
1919
[email protected]71f65dd2009-02-11 19:14:561920void AutomationProvider::WaitForNavigation(int handle,
1921 int64 last_navigation_time,
1922 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:251923 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:421924 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:251925
[email protected]8a3422c92008-09-24 17:42:421926 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:561927 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:521928 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
1929 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:551930 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:291931 return;
[email protected]8a3422c92008-09-24 17:42:421932 }
1933
[email protected]7dad3d5f2010-03-04 00:27:011934 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:421935}
1936
[email protected]71f65dd2009-02-11 19:14:561937void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161938 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561939 int value,
1940 bool* success) {
1941 *success = false;
[email protected]8a3422c92008-09-24 17:42:421942 if (browser_tracker_->ContainsHandle(handle)) {
1943 Browser* browser = browser_tracker_->GetResource(handle);
1944 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561945 *success = true;
[email protected]8a3422c92008-09-24 17:42:421946 }
[email protected]8a3422c92008-09-24 17:42:421947}
[email protected]97fa6ce32008-12-19 01:48:161948
[email protected]71f65dd2009-02-11 19:14:561949void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161950 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561951 const std::wstring& value,
1952 bool* success) {
1953 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161954 if (browser_tracker_->ContainsHandle(handle)) {
1955 Browser* browser = browser_tracker_->GetResource(handle);
1956 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561957 *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::GetBooleanPreference(int handle,
1962 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:361963 bool* success,
1964 bool* value) {
[email protected]71f65dd2009-02-11 19:14:561965 *success = false;
1966 *value = false;
[email protected]97fa6ce32008-12-19 01:48:161967 if (browser_tracker_->ContainsHandle(handle)) {
1968 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561969 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
1970 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161971 }
[email protected]97fa6ce32008-12-19 01:48:161972}
1973
[email protected]71f65dd2009-02-11 19:14:561974void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161975 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561976 bool value,
1977 bool* success) {
1978 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161979 if (browser_tracker_->ContainsHandle(handle)) {
1980 Browser* browser = browser_tracker_->GetResource(handle);
1981 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561982 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161983 }
[email protected]97fa6ce32008-12-19 01:48:161984}
1985
1986// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561987void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:401988 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:161989 if (tab_tracker_->ContainsHandle(tab_handle)) {
1990 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1991 Browser* browser = FindAndActivateTab(nav);
1992 DCHECK(browser);
1993
[email protected]57c6a652009-05-04 07:58:341994 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
1995 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:161996 }
[email protected]97fa6ce32008-12-19 01:48:161997}
1998
[email protected]b8f48d12009-11-09 20:14:361999// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562000void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402001 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562002 bool* success) {
2003 *success = false;
[email protected]de246f52009-02-25 18:25:452004#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:162005 if (tab_tracker_->ContainsHandle(tab_handle)) {
2006 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2007 Browser* browser = FindAndActivateTab(nav);
2008 DCHECK(browser);
2009
[email protected]1fc025202009-01-20 23:03:142010 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162011 int selected_encoding_id =
2012 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2013 if (selected_encoding_id) {
2014 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562015 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162016 }
2017 }
2018 }
[email protected]de246f52009-02-25 18:25:452019#else
2020 // TODO(port): Enable when encoding-related parts of Browser are ported.
2021 NOTIMPLEMENTED();
2022#endif
[email protected]97fa6ce32008-12-19 01:48:162023}
[email protected]5bcdb312009-01-07 21:43:202024
[email protected]4d434a1a2009-02-11 21:06:572025void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202026 SavePackage::SetShouldPromptUser(should_prompt);
2027}
[email protected]87eab222009-03-13 00:47:452028
[email protected]66ba4932009-06-04 19:22:132029void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2030 *count = -1; // -1 is the error code
2031 if (tab_tracker_->ContainsHandle(handle)) {
2032 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2033 TabContents* tab_contents = nav_controller->tab_contents();
2034 if (tab_contents) {
2035 BlockedPopupContainer* container =
2036 tab_contents->blocked_popup_container();
2037 if (container) {
2038 *count = static_cast<int>(container->GetBlockedPopupCount());
2039 } else {
2040 // If we don't have a container, we don't have any blocked popups to
2041 // contain!
2042 *count = 0;
2043 }
2044 }
2045 }
2046}
[email protected]f7a68432009-07-29 23:18:192047
2048void AutomationProvider::SelectAll(int tab_handle) {
2049 RenderViewHost* view = GetViewForTab(tab_handle);
2050 if (!view) {
2051 NOTREACHED();
2052 return;
2053 }
2054
2055 view->SelectAll();
2056}
2057
2058void AutomationProvider::Cut(int tab_handle) {
2059 RenderViewHost* view = GetViewForTab(tab_handle);
2060 if (!view) {
2061 NOTREACHED();
2062 return;
2063 }
2064
2065 view->Cut();
2066}
2067
2068void AutomationProvider::Copy(int tab_handle) {
2069 RenderViewHost* view = GetViewForTab(tab_handle);
2070 if (!view) {
2071 NOTREACHED();
2072 return;
2073 }
2074
2075 view->Copy();
2076}
2077
2078void AutomationProvider::Paste(int tab_handle) {
2079 RenderViewHost* view = GetViewForTab(tab_handle);
2080 if (!view) {
2081 NOTREACHED();
2082 return;
2083 }
2084
2085 view->Paste();
2086}
2087
2088void AutomationProvider::ReloadAsync(int tab_handle) {
2089 if (tab_tracker_->ContainsHandle(tab_handle)) {
2090 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2091 if (!tab) {
2092 NOTREACHED();
2093 return;
2094 }
2095
2096 tab->Reload(false);
2097 }
2098}
2099
2100void AutomationProvider::StopAsync(int tab_handle) {
2101 RenderViewHost* view = GetViewForTab(tab_handle);
2102 if (!view) {
[email protected]8b2b3312009-09-14 18:38:362103 // We tolerate StopAsync being called even before a view has been created.
2104 // So just log a warning instead of a NOTREACHED().
2105 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:192106 return;
2107 }
2108
2109 view->Stop();
2110}
2111
[email protected]1bb5f892009-10-06 01:44:572112void AutomationProvider::OnSetPageFontSize(int tab_handle,
2113 int font_size) {
2114 AutomationPageFontSize automation_font_size =
2115 static_cast<AutomationPageFontSize>(font_size);
2116
2117 if (automation_font_size < SMALLEST_FONT ||
2118 automation_font_size > LARGEST_FONT) {
2119 DLOG(ERROR) << "Invalid font size specified : "
2120 << font_size;
2121 return;
2122 }
2123
2124 if (tab_tracker_->ContainsHandle(tab_handle)) {
2125 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2126 DCHECK(tab != NULL);
2127 if (tab && tab->tab_contents()) {
2128 DCHECK(tab->tab_contents()->profile() != NULL);
2129 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2130 prefs::kWebKitDefaultFontSize, font_size);
2131 }
2132 }
2133}
2134
2135
[email protected]2949e90d2009-08-21 15:32:522136void AutomationProvider::WaitForBrowserWindowCountToBecome(
2137 int target_count, IPC::Message* reply_message) {
2138 if (static_cast<int>(BrowserList::size()) == target_count) {
2139 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2140 reply_message, true);
2141 Send(reply_message);
2142 return;
2143 }
2144
2145 // Set up an observer (it will delete itself).
2146 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2147}
2148
2149void AutomationProvider::WaitForAppModalDialogToBeShown(
2150 IPC::Message* reply_message) {
2151 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2152 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2153 reply_message, true);
2154 Send(reply_message);
2155 return;
2156 }
2157
2158 // Set up an observer (it will delete itself).
2159 new AppModalDialogShownObserver(this, reply_message);
2160}
2161
[email protected]1126a1d32009-08-26 15:39:262162void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2163 int handle, int number_of_navigations, IPC::Message* reply_message) {
2164 if (tab_tracker_->ContainsHandle(handle)) {
2165 NavigationController* tab = tab_tracker_->GetResource(handle);
2166 Browser* browser = FindAndActivateTab(tab);
2167 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:012168 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2169 false);
[email protected]1126a1d32009-08-26 15:39:262170 browser->GoBack(CURRENT_TAB);
2171 return;
2172 }
2173 }
2174
2175 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2176 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2177 Send(reply_message);
2178}
2179
2180void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2181 int handle, int number_of_navigations, IPC::Message* reply_message) {
2182 if (tab_tracker_->ContainsHandle(handle)) {
2183 NavigationController* tab = tab_tracker_->GetResource(handle);
2184 Browser* browser = FindAndActivateTab(tab);
2185 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:012186 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2187 false);
[email protected]1126a1d32009-08-26 15:39:262188 browser->GoForward(CURRENT_TAB);
2189 return;
2190 }
2191 }
2192
2193 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2194 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2195 Send(reply_message);
2196}
2197
[email protected]f7a68432009-07-29 23:18:192198RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2199 if (tab_tracker_->ContainsHandle(tab_handle)) {
2200 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2201 if (!tab) {
2202 NOTREACHED();
2203 return NULL;
2204 }
2205
2206 TabContents* tab_contents = tab->tab_contents();
2207 if (!tab_contents) {
2208 NOTREACHED();
2209 return NULL;
2210 }
2211
2212 RenderViewHost* view_host = tab_contents->render_view_host();
2213 return view_host;
2214 }
2215
2216 return NULL;
2217}
[email protected]675595f2009-08-26 22:32:042218
2219void AutomationProvider::GetBrowserForWindow(int window_handle,
2220 bool* success,
2221 int* browser_handle) {
2222 *success = false;
2223 *browser_handle = 0;
2224
2225 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2226 if (!window)
2227 return;
2228
2229 BrowserList::const_iterator iter = BrowserList::begin();
2230 for (;iter != BrowserList::end(); ++iter) {
2231 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2232 if (window == this_window) {
2233 // Add() returns the existing handle for the resource if any.
2234 *browser_handle = browser_tracker_->Add(*iter);
2235 *success = true;
2236 return;
2237 }
2238 }
2239}
[email protected]d11c8e92009-10-20 23:26:402240
2241void AutomationProvider::InstallExtension(const FilePath& crx_path,
2242 IPC::Message* reply_message) {
2243 ExtensionsService* service = profile_->GetExtensionsService();
2244 if (service) {
2245 // The observer will delete itself when done.
2246 new ExtensionNotificationObserver(this,
2247 AutomationMsg_InstallExtension::ID,
2248 reply_message);
2249
2250 const FilePath& install_dir = service->install_directory();
[email protected]d11c8e92009-10-20 23:26:402251 CrxInstaller::Start(crx_path,
2252 install_dir,
2253 Extension::INTERNAL,
2254 "", // no expected id
2255 false, // please do not delete crx file
2256 true, // privilege increase allowed
[email protected]d11c8e92009-10-20 23:26:402257 service,
2258 NULL); // silent isntall, no UI
2259 } else {
2260 AutomationMsg_InstallExtension::WriteReplyParams(
2261 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2262 Send(reply_message);
2263 }
2264}
2265
2266void AutomationProvider::LoadExpandedExtension(
2267 const FilePath& extension_dir,
2268 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:382269 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:402270 // The observer will delete itself when done.
2271 new ExtensionNotificationObserver(this,
2272 AutomationMsg_LoadExpandedExtension::ID,
2273 reply_message);
2274
2275 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:402276 } else {
2277 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
2278 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2279 Send(reply_message);
2280 }
2281}
[email protected]673fd2c02010-02-04 23:10:002282
2283void AutomationProvider::SaveAsAsync(int tab_handle) {
2284 NavigationController* tab = NULL;
2285 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2286 if (tab_contents)
2287 tab_contents->OnSavePage();
2288}
[email protected]7dad3d5f2010-03-04 00:27:012289
2290void AutomationProvider::SetContentSetting(
2291 int handle,
2292 const std::string& host,
2293 ContentSettingsType content_type,
2294 ContentSetting setting,
2295 bool* success) {
2296 *success = false;
2297 if (browser_tracker_->ContainsHandle(handle)) {
2298 Browser* browser = browser_tracker_->GetResource(handle);
2299 HostContentSettingsMap* map =
2300 browser->profile()->GetHostContentSettingsMap();
2301 if (host.empty()) {
2302 map->SetDefaultContentSetting(content_type, setting);
2303 } else {
2304 map->SetContentSetting(host, content_type, setting);
2305 }
2306 *success = true;
2307 }
2308}