blob: d791afb6cdbeaa285316417dd68a9ce6cbfa89e0 [file] [log] [blame]
[email protected]5ae5bed2009-08-21 18:52:441// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/automation/automation_provider.h"
6
[email protected]5ae5bed2009-08-21 18:52:447#include <set>
8
[email protected]202e7a72009-06-15 03:48:369#include "app/l10n_util.h"
[email protected]37126212009-05-06 02:23:3110#include "app/message_box_flags.h"
[email protected]c6cb1992009-04-13 16:45:2911#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2012#include "base/json/json_reader.h"
[email protected]bc1407f2009-09-29 00:33:3513#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3814#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2915#include "base/path_service.h"
[email protected]f44265b2009-05-19 18:52:5016#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2717#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3818#include "base/thread.h"
[email protected]a7eee32f2009-05-22 18:08:1719#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2920#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2821#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0922#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3323#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0825#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5526#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5027#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1328#include "chrome/browser/blocked_popup_container.h"
[email protected]5c238752009-06-13 10:29:0729#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3930#include "chrome/browser/browser_window.h"
initial.commit09911bf2008-07-26 23:55:2931#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1232#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5933#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2234#include "chrome/browser/download/download_shelf.h"
[email protected]d11c8e92009-10-20 23:26:4035#include "chrome/browser/extensions/crx_installer.h"
36#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5537#include "chrome/browser/extensions/extension_message_service.h"
[email protected]4801ecc2009-04-05 04:52:5838#include "chrome/browser/find_bar.h"
39#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2940#include "chrome/browser/find_notification_details.h"
[email protected]13869dd2009-05-05 00:40:0641#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0142#include "chrome/browser/login_prompt.h"
[email protected]be180c802009-10-23 06:33:3143#include "chrome/browser/net/url_request_context_getter.h"
[email protected]f732c1e2009-07-30 15:48:5344#include "chrome/browser/net/url_request_mock_util.h"
[email protected]a7eee32f2009-05-22 18:08:1745#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0746#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2547#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0348#include "chrome/browser/ssl/ssl_manager.h"
49#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3450#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4551#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1752#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2953#include "chrome/common/chrome_paths.h"
[email protected]a7eee32f2009-05-22 18:08:1754#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1455#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4756#include "chrome/common/platform_util.h"
[email protected]1bb5f892009-10-06 01:44:5757#include "chrome/common/pref_names.h"
[email protected]8a3422c92008-09-24 17:42:4258#include "chrome/common/pref_service.h"
[email protected]71f65dd2009-02-11 19:14:5659#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5760#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1761#include "net/proxy/proxy_service.h"
62#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2163#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2864#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2965
[email protected]de246f52009-02-25 18:25:4566#if defined(OS_WIN)
67// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4568#include "chrome/browser/character_encoding.h"
69#include "chrome/browser/download/save_package.h"
70#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4571#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4572#endif // defined(OS_WIN)
73
[email protected]5ae5bed2009-08-21 18:52:4474#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2875// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2876#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4477#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0678
[email protected]e1acf6f2008-10-27 20:43:3379using base::Time;
80
[email protected]cbab76d2008-10-13 22:42:4781class AutomationInterstitialPage : public InterstitialPage {
82 public:
[email protected]57c6a652009-05-04 07:58:3483 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4784 const GURL& url,
85 const std::string& contents)
86 : InterstitialPage(tab, true, url),
87 contents_(contents) {
88 }
89
90 virtual std::string GetHTMLContents() { return contents_; }
91
92 private:
93 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:2994
[email protected]cbab76d2008-10-13 22:42:4795 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
96};
97
[email protected]c2cb8542009-08-20 21:16:5198#if !defined(OS_MACOSX)
99class ClickTask : public Task {
100 public:
[email protected]fc2e0872009-08-21 22:14:41101 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51102 virtual ~ClickTask() {}
103
104 virtual void Run() {
105 ui_controls::MouseButton button = ui_controls::LEFT;
106 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
107 views::Event::EF_LEFT_BUTTON_DOWN) {
108 button = ui_controls::LEFT;
109 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
110 views::Event::EF_RIGHT_BUTTON_DOWN) {
111 button = ui_controls::RIGHT;
112 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
113 views::Event::EF_MIDDLE_BUTTON_DOWN) {
114 button = ui_controls::MIDDLE;
115 } else {
116 NOTREACHED();
117 }
118
[email protected]fc2e0872009-08-21 22:14:41119 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51120 }
121
122 private:
[email protected]c2cb8542009-08-20 21:16:51123 int flags_;
124
125 DISALLOW_COPY_AND_ASSIGN(ClickTask);
126};
127#endif
128
initial.commit09911bf2008-07-26 23:55:29129AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57130 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56131 profile_(profile),
132 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29133 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29134 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20135 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29136 autocomplete_edit_tracker_.reset(
137 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29138 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
139 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44140 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
initial.commit09911bf2008-07-26 23:55:29141}
142
143AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50144 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
145 port_containers_.end());
146 port_containers_.clear();
147
[email protected]0da050b92008-08-19 19:29:47148 // Make sure that any outstanding NotificationObservers also get destroyed.
149 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31150 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47151 while ((observer = it.GetNext()) != NULL)
152 delete observer;
initial.commit09911bf2008-07-26 23:55:29153}
154
[email protected]9a3a293b2009-06-04 22:28:16155void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06156 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57157 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06158 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
159 automation_resource_message_filter_,
160 g_browser_process->io_thread()->message_loop(),
161 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05162 scoped_ptr<FileVersionInfo> file_version_info(
163 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40164 std::string version_string;
165 if (file_version_info != NULL) {
166 version_string = WideToASCII(file_version_info->file_version());
167 }
[email protected]c6cb1992009-04-13 16:45:29168
169 // Send a hello message with our current automation protocol version.
170 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29171}
172
173void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
174 if (expected_tabs == 0) {
175 Send(new AutomationMsg_InitialLoadsComplete(0));
176 } else {
177 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
178 }
179}
180
initial.commit09911bf2008-07-26 23:55:29181NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58182 NavigationController* tab, IPC::Message* reply_message,
183 int number_of_navigations) {
initial.commit09911bf2008-07-26 23:55:29184 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58185 new NavigationNotificationObserver(tab, this, reply_message,
186 number_of_navigations);
initial.commit09911bf2008-07-26 23:55:29187
[email protected]71f65dd2009-02-11 19:14:56188 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29189 return observer;
190}
191
192void AutomationProvider::RemoveNavigationStatusListener(
193 NotificationObserver* obs) {
194 notification_observer_list_.RemoveObserver(obs);
195}
196
197NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14198 Browser* parent,
199 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56200 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14201 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29202 notification_observer_list_.AddObserver(observer);
203
204 return observer;
205}
206
207void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
208 notification_observer_list_.RemoveObserver(obs);
209}
210
211void AutomationProvider::AddLoginHandler(NavigationController* tab,
212 LoginHandler* handler) {
213 login_handler_map_[tab] = handler;
214}
215
216void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
217 DCHECK(login_handler_map_[tab]);
218 login_handler_map_.erase(tab);
219}
220
[email protected]f44265b2009-05-19 18:52:50221void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
222 int port_id = port->port_id();
223 DCHECK_NE(-1, port_id);
224 DCHECK(port_containers_.find(port_id) == port_containers_.end());
225
226 port_containers_[port_id] = port;
227}
228
229void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
230 int port_id = port->port_id();
231 DCHECK_NE(-1, port_id);
232
233 PortContainerMap::iterator it = port_containers_.find(port_id);
234 DCHECK(it != port_containers_.end());
235
236 if (it != port_containers_.end()) {
237 delete it->second;
238 port_containers_.erase(it);
239 }
240}
241
242ExtensionPortContainer* AutomationProvider::GetPortContainer(
243 int port_id) const {
244 PortContainerMap::const_iterator it = port_containers_.find(port_id);
245 if (it == port_containers_.end())
246 return NULL;
247
248 return it->second;
249}
250
initial.commit09911bf2008-07-26 23:55:29251int AutomationProvider::GetIndexForNavigationController(
252 const NavigationController* controller, const Browser* parent) const {
253 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12254 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29255}
256
257void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
258 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14259 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56260 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
261 CloseBrowserAsync)
262 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
263 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
264 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
265 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
266 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
267 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14268 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58269 IPC_MESSAGE_HANDLER_DELAY_REPLY(
270 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
271 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56272 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
273 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
274 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
275 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
276 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14277 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56278 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
279 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
280 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14281 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29282 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
283 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56284 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36285 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56286 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29287 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56288 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29289 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
290 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56291 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14292 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24293 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44294 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33295 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44296 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33297 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24298 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14299 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24300 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15301 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14302 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44303#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24304 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]1c58a5c2009-05-21 18:47:14305 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]5ae5bed2009-08-21 18:52:44306#endif // !defined(OS_MACOSX)
[email protected]8dd404bb2009-09-22 19:57:24307#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]71f65dd2009-02-11 19:14:56308 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
309 WindowSimulateDrag)
[email protected]8dd404bb2009-09-22 19:57:24310#endif // defined(OS_WIN) || defined(OS_LINUX)
[email protected]71f65dd2009-02-11 19:14:56311 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
312 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37313#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56314 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45315#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56316 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
317 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03318 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56319 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14320 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
initial.commit09911bf2008-07-26 23:55:29321 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14322 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56323 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
324 ExecuteJavascript)
325 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29326 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14327 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
328 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56329 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
330 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14331 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17332 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14333 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14334 OpenNewBrowserWindow)
335 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56336 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14337 GetAutocompleteEditForBrowser)
338 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45339#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29340 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17341#endif
[email protected]71f65dd2009-02-11 19:14:56342 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29343 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26344 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
345 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56346 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14347 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56348 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14349 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17350#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29351 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
352 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17353#endif
[email protected]71f65dd2009-02-11 19:14:56354 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
355 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14356 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17357#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04358 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39359 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
360 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17361#endif
[email protected]1c58a5c2009-05-21 18:47:14362 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
363 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44364 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
365 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56366 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
367 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29368 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
369 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
370 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56371 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41372 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56373 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
374 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29375 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56376 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29377 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56378 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29379 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56380 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29381 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56382 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45383 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45384 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
385 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14386 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56387 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57388 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56389 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57390 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56391 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
392 GetBookmarkBarVisibility)
[email protected]1c58a5c2009-05-21 18:47:14393 IPC_MESSAGE_HANDLER(AutomationMsg_GetSSLInfoBarCount, GetSSLInfoBarCount)
[email protected]71f65dd2009-02-11 19:14:56394 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickSSLInfoBarLink,
395 ClickSSLInfoBarLink)
396 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42397 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56398 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
399 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14400 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56401 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44402 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56403 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20404 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14405 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56406 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16407 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56408 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16409 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56410 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16411 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14412 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20413 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
414 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14415 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]b83e4602009-05-15 22:58:33416 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
417 SetEnableExtensionAutomation)
[email protected]59560e0b2009-06-04 03:30:22418 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13419 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19420 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
421 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
422 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
423 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
424 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
425 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52426 IPC_MESSAGE_HANDLER_DELAY_REPLY(
427 AutomationMsg_WaitForBrowserWindowCountToBecome,
428 WaitForBrowserWindowCountToBecome)
429 IPC_MESSAGE_HANDLER_DELAY_REPLY(
430 AutomationMsg_WaitForAppModalDialogToBeShown,
431 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26432 IPC_MESSAGE_HANDLER_DELAY_REPLY(
433 AutomationMsg_GoBackBlockUntilNavigationsComplete,
434 GoBackBlockUntilNavigationsComplete)
435 IPC_MESSAGE_HANDLER_DELAY_REPLY(
436 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
437 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14438#if defined(OS_WIN)
439 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
440#endif
[email protected]1bb5f892009-10-06 01:44:57441 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40442 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
443 InstallExtension)
444 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
445 LoadExpandedExtension)
initial.commit09911bf2008-07-26 23:55:29446 IPC_END_MESSAGE_MAP()
447}
448
[email protected]71f65dd2009-02-11 19:14:56449void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
450 *status = -1;
initial.commit09911bf2008-07-26 23:55:29451 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
452 Browser* browser = browser_tracker_->GetResource(handle);
453 if (at_index >= 0 && at_index < browser->tab_count()) {
454 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56455 *status = 0;
initial.commit09911bf2008-07-26 23:55:29456 }
457 }
initial.commit09911bf2008-07-26 23:55:29458}
459
[email protected]71f65dd2009-02-11 19:14:56460void AutomationProvider::AppendTab(int handle, const GURL& url,
461 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29462 int append_tab_response = -1; // -1 is the error code
463 NotificationObserver* observer = NULL;
464
465 if (browser_tracker_->ContainsHandle(handle)) {
466 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14467 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58468 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
469 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39470 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29471 if (tab_contents) {
472 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57473 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29474 }
475 }
476
477 if (append_tab_response < 0) {
478 // The append tab failed. Remove the TabStripObserver
479 if (observer) {
480 RemoveTabStripObserver(observer);
481 delete observer;
482 }
483
[email protected]71f65dd2009-02-11 19:14:56484 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
485 append_tab_response);
486 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29487 }
488}
489
[email protected]71f65dd2009-02-11 19:14:56490void AutomationProvider::NavigateToURL(int handle, const GURL& url,
491 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58492 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
493}
494
495void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
496 int handle, const GURL& url, int number_of_navigations,
497 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29498 if (tab_tracker_->ContainsHandle(handle)) {
499 NavigationController* tab = tab_tracker_->GetResource(handle);
500
501 // Simulate what a user would do. Activate the tab and then navigate.
502 // We could allow navigating in a background tab in future.
503 Browser* browser = FindAndActivateTab(tab);
504
505 if (browser) {
[email protected]2e028a082009-08-19 20:32:58506 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
[email protected]71f65dd2009-02-11 19:14:56507
initial.commit09911bf2008-07-26 23:55:29508 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50509 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29510 return;
511 }
512 }
[email protected]71f65dd2009-02-11 19:14:56513
514 AutomationMsg_NavigateToURL::WriteReplyParams(
515 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
516 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29517}
[email protected]2949e90d2009-08-21 15:32:52518
[email protected]71f65dd2009-02-11 19:14:56519void AutomationProvider::NavigationAsync(int handle, const GURL& url,
520 bool* status) {
521 *status = false;
initial.commit09911bf2008-07-26 23:55:29522
523 if (tab_tracker_->ContainsHandle(handle)) {
524 NavigationController* tab = tab_tracker_->GetResource(handle);
525
526 // Simulate what a user would do. Activate the tab and then navigate.
527 // We could allow navigating in a background tab in future.
528 Browser* browser = FindAndActivateTab(tab);
529
530 if (browser) {
531 // Don't add any listener unless a callback mechanism is desired.
532 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50533 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56534 *status = true;
initial.commit09911bf2008-07-26 23:55:29535 }
536 }
initial.commit09911bf2008-07-26 23:55:29537}
538
[email protected]71f65dd2009-02-11 19:14:56539void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29540 if (tab_tracker_->ContainsHandle(handle)) {
541 NavigationController* tab = tab_tracker_->GetResource(handle);
542 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14543 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]2e028a082009-08-19 20:32:58544 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29545 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29546 return;
547 }
548 }
[email protected]71f65dd2009-02-11 19:14:56549
550 AutomationMsg_GoBack::WriteReplyParams(
551 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
552 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29553}
554
[email protected]71f65dd2009-02-11 19:14:56555void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29556 if (tab_tracker_->ContainsHandle(handle)) {
557 NavigationController* tab = tab_tracker_->GetResource(handle);
558 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14559 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]2e028a082009-08-19 20:32:58560 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29561 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29562 return;
563 }
564 }
[email protected]71f65dd2009-02-11 19:14:56565
566 AutomationMsg_GoForward::WriteReplyParams(
567 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
568 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29569}
570
[email protected]71f65dd2009-02-11 19:14:56571void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29572 if (tab_tracker_->ContainsHandle(handle)) {
573 NavigationController* tab = tab_tracker_->GetResource(handle);
574 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14575 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]2e028a082009-08-19 20:32:58576 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29577 browser->Reload();
578 return;
579 }
580 }
[email protected]71f65dd2009-02-11 19:14:56581
582 AutomationMsg_Reload::WriteReplyParams(
583 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
584 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29585}
586
[email protected]71f65dd2009-02-11 19:14:56587void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29588 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56589 const std::wstring& password,
590 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29591 if (tab_tracker_->ContainsHandle(tab_handle)) {
592 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
593 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
594
595 if (iter != login_handler_map_.end()) {
596 // If auth is needed again after this, assume login has failed. This is
597 // not strictly correct, because a navigation can require both proxy and
598 // server auth, but it should be OK for now.
599 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58600 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29601 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52602 return;
initial.commit09911bf2008-07-26 23:55:29603 }
604 }
[email protected]de246f52009-02-25 18:25:45605
[email protected]457f5cf2009-08-18 16:37:52606 AutomationMsg_SetAuth::WriteReplyParams(
607 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
608 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29609}
610
[email protected]71f65dd2009-02-11 19:14:56611void AutomationProvider::CancelAuth(int tab_handle,
612 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29613 if (tab_tracker_->ContainsHandle(tab_handle)) {
614 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
615 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
616
617 if (iter != login_handler_map_.end()) {
618 // If auth is needed again after this, something is screwy.
619 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58620 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29621 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52622 return;
initial.commit09911bf2008-07-26 23:55:29623 }
624 }
[email protected]de246f52009-02-25 18:25:45625
[email protected]457f5cf2009-08-18 16:37:52626 AutomationMsg_CancelAuth::WriteReplyParams(
627 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
628 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29629}
630
[email protected]71f65dd2009-02-11 19:14:56631void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
632 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29633
634 if (tab_tracker_->ContainsHandle(tab_handle)) {
635 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
636 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
637
638 if (iter != login_handler_map_.end()) {
639 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56640 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29641 }
642 }
initial.commit09911bf2008-07-26 23:55:29643}
644
[email protected]71f65dd2009-02-11 19:14:56645void AutomationProvider::GetRedirectsFrom(int tab_handle,
646 const GURL& source_url,
647 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29648 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
649 if (tab_tracker_->ContainsHandle(tab_handle)) {
650 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
651 HistoryService* history_service =
652 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
653
654 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
655 "has no history service";
656 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56657 DCHECK(reply_message_ == NULL);
658 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29659 // Schedule a history query for redirects. The response will be sent
660 // asynchronously from the callback the history system uses to notify us
661 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29662 redirect_query_ = history_service->QueryRedirectsFrom(
663 source_url, &consumer_,
664 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
665 return; // Response will be sent when query completes.
666 }
667 }
668
669 // Send failure response.
[email protected]deb57402009-02-06 01:35:30670 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56671 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
672 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29673}
674
[email protected]71f65dd2009-02-11 19:14:56675void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
676 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29677 if (browser_tracker_->ContainsHandle(handle)) {
678 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56679 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29680 }
initial.commit09911bf2008-07-26 23:55:29681}
682
[email protected]202e7a72009-06-15 03:48:36683void AutomationProvider::GetBrowserLocale(string16* locale) {
684 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06685 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36686}
687
[email protected]71f65dd2009-02-11 19:14:56688void AutomationProvider::GetBrowserWindowCount(int* window_count) {
689 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29690}
691
[email protected]24497032009-05-01 17:00:29692void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
693 *window_count = static_cast<int>(
694 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
695}
696
[email protected]71f65dd2009-02-11 19:14:56697void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
698 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07699 AppModalDialog* dialog_delegate =
700 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50701 *showing_dialog = (dialog_delegate != NULL);
702 if (*showing_dialog)
703 *dialog_button = dialog_delegate->GetDialogButtons();
704 else
[email protected]478ff2ed2009-04-21 23:49:18705 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20706}
707
[email protected]71f65dd2009-02-11 19:14:56708void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
709 *success = false;
[email protected]fad84eab2008-12-05 00:37:20710
[email protected]1f460072009-05-28 17:02:07711 AppModalDialog* dialog_delegate =
712 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50713 if (dialog_delegate &&
714 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18715 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
716 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28717 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56718 *success = true;
[email protected]fad84eab2008-12-05 00:37:20719 }
[email protected]478ff2ed2009-04-21 23:49:18720 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
721 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56722 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28723 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56724 *success = true;
[email protected]fad84eab2008-12-05 00:37:20725 }
726 }
[email protected]c274acc2008-11-11 20:13:44727}
728
[email protected]71f65dd2009-02-11 19:14:56729void AutomationProvider::GetBrowserWindow(int index, int* handle) {
730 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29731 if (index >= 0) {
732 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29733 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29734 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56735 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29736 }
737 }
initial.commit09911bf2008-07-26 23:55:29738}
739
[email protected]24497032009-05-01 17:00:29740void AutomationProvider::FindNormalBrowserWindow(int* handle) {
741 *handle = 0;
742 Browser* browser = BrowserList::FindBrowserWithType(profile_,
743 Browser::TYPE_NORMAL);
744 if (browser)
745 *handle = browser_tracker_->Add(browser);
746}
747
[email protected]71f65dd2009-02-11 19:14:56748void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
749 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29750 Browser* browser = BrowserList::GetLastActive();
751 if (browser)
[email protected]71f65dd2009-02-11 19:14:56752 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29753}
754
[email protected]fb05865f2009-09-21 15:05:46755#if defined(OS_LINUX) || defined(OS_MACOSX)
[email protected]9a08bcf2009-08-12 19:56:28756// TODO(estade): use this implementation for all platforms?
757void AutomationProvider::GetActiveWindow(int* handle) {
758 gfx::NativeWindow window =
759 BrowserList::GetLastActive()->window()->GetNativeHandle();
760 *handle = window_tracker_->Add(window);
761}
762#endif
763
[email protected]4f6381ee2009-04-16 02:46:33764void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
765 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56766 *success = false;
[email protected]4ae62752008-08-04 23:28:47767 if (browser_tracker_->ContainsHandle(handle)) {
768 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14769 if (browser->command_updater()->SupportsCommand(command) &&
770 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47771 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56772 *success = true;
[email protected]4ae62752008-08-04 23:28:47773 }
774 }
[email protected]4ae62752008-08-04 23:28:47775}
776
[email protected]4f6381ee2009-04-16 02:46:33777void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56778 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53779 // List of commands which just finish synchronously and don't require
780 // setting up an observer.
781 static const int kSynchronousCommands[] = {
782 IDC_HOME,
783 IDC_SELECT_NEXT_TAB,
784 IDC_SELECT_PREVIOUS_TAB,
785 IDC_SHOW_DOWNLOADS,
786 IDC_SHOW_HISTORY,
787 };
[email protected]56e71b7c2009-03-27 03:05:56788 if (browser_tracker_->ContainsHandle(handle)) {
789 Browser* browser = browser_tracker_->GetResource(handle);
790 if (browser->command_updater()->SupportsCommand(command) &&
791 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53792 // First check if we can handle the command without using an observer.
793 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
794 if (command == kSynchronousCommands[i]) {
795 browser->ExecuteCommand(command);
796 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
797 true);
798 Send(reply_message);
799 return;
800 }
801 }
802
803 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42804 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
805 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27806 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42807 return;
808 }
[email protected]56e71b7c2009-03-27 03:05:56809 }
810 }
[email protected]49a14a82009-03-31 04:16:44811 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56812 Send(reply_message);
813}
814
[email protected]fc2e0872009-08-21 22:14:41815// This task just adds another task to the event queue. This is useful if
816// you want to ensure that any tasks added to the event queue after this one
817// have already been processed by the time |task| is run.
818class InvokeTaskLaterTask : public Task {
819 public:
820 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
821 virtual ~InvokeTaskLaterTask() {}
822
823 virtual void Run() {
824 MessageLoop::current()->PostTask(FROM_HERE, task_);
825 }
826
827 private:
828 Task* task_;
829
830 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
831};
832
[email protected]fc2e0872009-08-21 22:14:41833#if defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29834void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
835 int handle,
[email protected]d1a5941e2009-08-13 23:34:24836 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29837 int flags) {
[email protected]b410bc32009-08-14 01:11:14838 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51839 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41840 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29841 }
842}
843
initial.commit09911bf2008-07-26 23:55:29844void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
845 int handle,
[email protected]bc1407f2009-09-29 00:33:35846 int key,
initial.commit09911bf2008-07-26 23:55:29847 int flags) {
[email protected]b410bc32009-08-14 01:11:14848 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29849 return;
850
[email protected]b410bc32009-08-14 01:11:14851 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29852 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35853 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38854 ((flags & views::Event::EF_CONTROL_DOWN) ==
855 views::Event::EF_CONTROL_DOWN),
856 ((flags & views::Event::EF_SHIFT_DOWN) ==
857 views::Event::EF_SHIFT_DOWN),
858 ((flags & views::Event::EF_ALT_DOWN) ==
859 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29860}
[email protected]fc2e0872009-08-21 22:14:41861#endif // defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29862
[email protected]71f65dd2009-02-11 19:14:56863void AutomationProvider::IsWindowActive(int handle, bool* success,
864 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29865 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17866 *is_active =
867 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56868 *success = true;
initial.commit09911bf2008-07-26 23:55:29869 } else {
[email protected]71f65dd2009-02-11 19:14:56870 *success = false;
871 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29872 }
873}
874
[email protected]71f65dd2009-02-11 19:14:56875void AutomationProvider::GetTabCount(int handle, int* tab_count) {
876 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29877
878 if (browser_tracker_->ContainsHandle(handle)) {
879 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56880 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29881 }
initial.commit09911bf2008-07-26 23:55:29882}
883
[email protected]71f65dd2009-02-11 19:14:56884void AutomationProvider::GetTab(int win_handle, int tab_index,
885 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56886 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29887 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
888 Browser* browser = browser_tracker_->GetResource(win_handle);
889 if (tab_index < browser->tab_count()) {
890 TabContents* tab_contents =
891 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57892 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29893 }
894 }
initial.commit09911bf2008-07-26 23:55:29895}
896
[email protected]71f65dd2009-02-11 19:14:56897void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
898 std::wstring* title) {
899 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29900 if (tab_tracker_->ContainsHandle(handle)) {
901 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36902 NavigationEntry* entry = tab->GetActiveEntry();
903 if (entry != NULL) {
904 *title = UTF16ToWideHack(entry->title());
905 } else {
906 *title = std::wstring();
907 }
[email protected]71f65dd2009-02-11 19:14:56908 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29909 }
initial.commit09911bf2008-07-26 23:55:29910}
911
[email protected]77bc6732009-04-20 22:01:03912void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
913 *tabstrip_index = -1; // -1 is the error code
914
915 if (tab_tracker_->ContainsHandle(handle)) {
916 NavigationController* tab = tab_tracker_->GetResource(handle);
917 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12918 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:03919 }
920}
921
initial.commit09911bf2008-07-26 23:55:29922void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
923 if (window_tracker_->ContainsHandle(handle)) {
924 window_tracker_->Remove(window_tracker_->GetResource(handle));
925 }
926}
927
928void AutomationProvider::OnChannelError() {
929 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:57930 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:29931}
932
933// TODO(brettw) change this to accept GURLs when history supports it
934void AutomationProvider::OnRedirectQueryComplete(
935 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:37936 GURL from_url,
initial.commit09911bf2008-07-26 23:55:29937 bool success,
[email protected]379c2b12009-07-01 21:50:33938 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:29939 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:56940 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:29941
[email protected]deb57402009-02-06 01:35:30942 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:29943 if (success) {
[email protected]71f65dd2009-02-11 19:14:56944 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:29945 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:30946 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:29947 } else {
[email protected]71f65dd2009-02-11 19:14:56948 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:29949 }
950
[email protected]4f3dc372009-02-24 00:10:29951 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:30952
[email protected]71f65dd2009-02-11 19:14:56953 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:17954 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:56955 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29956}
957
958bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:57959 DCHECK(channel_.get());
960 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:29961}
962
963Browser* AutomationProvider::FindAndActivateTab(
964 NavigationController* controller) {
965 int tab_index;
966 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
967 if (browser)
968 browser->SelectTabContentsAt(tab_index, true);
969
970 return browser;
971}
972
[email protected]71f65dd2009-02-11 19:14:56973void AutomationProvider::GetCookies(const GURL& url, int handle,
974 int* value_size,
975 std::string* value) {
976 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:29977 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
978 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:31979
980 // Since we are running on the UI thread don't call GetURLRequestContext().
981 net::CookieStore* cookie_store =
982 tab->profile()->GetRequestContext()->GetCookieStore();
983
984 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:56985 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:29986 }
initial.commit09911bf2008-07-26 23:55:29987}
988
[email protected]71f65dd2009-02-11 19:14:56989void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:29990 const std::string value,
[email protected]71f65dd2009-02-11 19:14:56991 int handle,
992 int* response_value) {
993 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:29994
995 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
996 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:31997
998 // Since we are running on the UI thread don't call GetURLRequestContext().
999 scoped_refptr<net::CookieStore> cookie_store =
1000 tab->profile()->GetRequestContext()->GetCookieStore();
1001
1002 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561003 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291004 }
initial.commit09911bf2008-07-26 23:55:291005}
1006
[email protected]71f65dd2009-02-11 19:14:561007void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1008 *success = false;
initial.commit09911bf2008-07-26 23:55:291009 if (tab_tracker_->ContainsHandle(handle)) {
1010 NavigationController* tab = tab_tracker_->GetResource(handle);
1011 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541012 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561013 *success = true;
initial.commit09911bf2008-07-26 23:55:291014 }
initial.commit09911bf2008-07-26 23:55:291015}
1016
[email protected]71f65dd2009-02-11 19:14:561017void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1018 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291019
1020 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561021 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341022 TabContents* tab_contents =
1023 tab_tracker_->GetResource(handle)->tab_contents();
1024 if (tab_contents->process())
1025 *process_id = tab_contents->process()->process().pid();
initial.commit09911bf2008-07-26 23:55:291026 }
initial.commit09911bf2008-07-26 23:55:291027}
1028
1029void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331030 NOTREACHED() << "This function has been deprecated. "
1031 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291032}
1033
[email protected]71f65dd2009-02-11 19:14:561034void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291035 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561036 const std::wstring& script,
1037 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291038 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341039 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1040 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571041 // Set the routing id of this message with the controller.
1042 // This routing id needs to be remembered for the reverse
1043 // communication while sending back the response of
1044 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331045 std::wstring set_automation_id;
1046 SStringPrintf(&set_automation_id,
1047 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561048 reply_message->routing_id());
1049
1050 DCHECK(reply_message_ == NULL);
1051 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291052
[email protected]57c6a652009-05-04 07:58:341053 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331054 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341055 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061056 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571057 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291058 }
1059
1060 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561061 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1062 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291063 }
1064}
1065
[email protected]71f65dd2009-02-11 19:14:561066void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1067 *visible = false;
[email protected]20e93d12008-08-28 16:31:571068
[email protected]59560e0b2009-06-04 03:30:221069 if (browser_tracker_->ContainsHandle(handle)) {
1070 Browser* browser = browser_tracker_->GetResource(handle);
1071 if (browser) {
1072 *visible = browser->window()->IsDownloadShelfVisible();
1073 }
1074 }
initial.commit09911bf2008-07-26 23:55:291075}
1076
[email protected]59560e0b2009-06-04 03:30:221077void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1078 if (browser_tracker_->ContainsHandle(handle)) {
1079 Browser* browser = browser_tracker_->GetResource(handle);
1080 if (browser) {
1081 if (visible)
1082 browser->window()->GetDownloadShelf()->Show();
1083 else
1084 browser->window()->GetDownloadShelf()->Close();
1085 }
1086 }
1087}
1088
1089
[email protected]71f65dd2009-02-11 19:14:561090void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1091 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291092 if (tab_tracker_->ContainsHandle(handle)) {
1093 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111094 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291095 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561096 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291097 }
1098 }
initial.commit09911bf2008-07-26 23:55:291099}
1100
initial.commit09911bf2008-07-26 23:55:291101void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561102 int handle, const std::wstring& find_request,
1103 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311104 NOTREACHED() << "This function has been deprecated."
1105 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561106 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311107 return;
1108}
1109
[email protected]4f999132009-03-31 18:08:401110void AutomationProvider::HandleFindRequest(
1111 int handle,
1112 const AutomationMsg_Find_Params& params,
1113 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291114 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561115 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1116 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291117 return;
1118 }
1119
1120 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111121 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291122
1123 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141124 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291125
[email protected]57c6a652009-05-04 07:58:341126 tab_contents->set_current_find_request_id(
1127 FindInPageNotificationObserver::kFindInPageRequestId);
1128 tab_contents->render_view_host()->StartFinding(
1129 FindInPageNotificationObserver::kFindInPageRequestId,
1130 params.search_string, params.forward, params.match_case,
1131 params.find_next);
initial.commit09911bf2008-07-26 23:55:291132}
1133
[email protected]5f8af2a2008-08-06 22:49:451134void AutomationProvider::HandleOpenFindInPageRequest(
1135 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291136 if (browser_tracker_->ContainsHandle(handle)) {
1137 Browser* browser = browser_tracker_->GetResource(handle);
1138 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451139 }
1140}
1141
[email protected]71f65dd2009-02-11 19:14:561142void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561143 *visible = false;
[email protected]855c0142009-09-28 22:35:241144 Browser* browser = browser_tracker_->GetResource(handle);
1145 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581146 FindBarTesting* find_bar =
1147 browser->find_bar()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241148 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291149 }
[email protected]20e93d12008-08-28 16:31:571150}
1151
[email protected]71f65dd2009-02-11 19:14:561152void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1153 int* y) {
[email protected]9e0534b2008-10-21 15:03:011154 gfx::Point position(0, 0);
1155 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291156 if (browser_tracker_->ContainsHandle(handle)) {
1157 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581158 FindBarTesting* find_bar =
1159 browser->find_bar()->find_bar()->GetFindBarTesting();
1160 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291161 }
[email protected]20e93d12008-08-28 16:31:571162
[email protected]71f65dd2009-02-11 19:14:561163 *x = position.x();
1164 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571165}
1166
initial.commit09911bf2008-07-26 23:55:291167void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561168 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341169 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1170 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561171 DCHECK(reply_message_ == NULL);
1172 reply_message_ = reply_message;
1173
[email protected]d9f9b792009-06-24 13:17:121174 DevToolsManager::GetInstance()->InspectElement(
1175 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291176 } else {
[email protected]71f65dd2009-02-11 19:14:561177 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1178 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291179 }
1180}
1181
1182void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091183 if (reply_message_) {
1184 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1185 num_resources);
1186 Send(reply_message_);
1187 reply_message_ = NULL;
1188 }
initial.commit09911bf2008-07-26 23:55:291189}
1190
[email protected]a7eee32f2009-05-22 18:08:171191class SetProxyConfigTask : public Task {
1192 public:
[email protected]be180c802009-10-23 06:33:311193 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
1194 const std::string& new_proxy_config)
1195 : request_context_getter_(request_context_getter), proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:171196 virtual void Run() {
1197 // First, deserialize the JSON string. If this fails, log and bail.
1198 JSONStringValueSerializer deserializer(proxy_config_);
1199 std::string error_message;
1200 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1201 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1202 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1203 << error_message;
1204 return;
1205 }
1206
1207 scoped_ptr<DictionaryValue> dict(
1208 static_cast<DictionaryValue*>(root.release()));
1209 // Now put together a proxy configuration from the deserialized string.
1210 net::ProxyConfig pc;
1211 PopulateProxyConfig(*dict.get(), &pc);
1212
[email protected]be180c802009-10-23 06:33:311213 net::ProxyService* proxy_service =
1214 request_context_getter_->GetURLRequestContext()->proxy_service();
1215 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:171216 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1217 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:311218 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:171219 }
1220
1221 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1222 DCHECK(pc);
1223 bool no_proxy = false;
1224 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1225 // Make no changes to the ProxyConfig.
1226 return;
1227 }
1228 bool auto_config;
1229 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
1230 pc->auto_detect = true;
1231 }
1232 std::string pac_url;
1233 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
1234 pc->pac_url = GURL(pac_url);
1235 }
1236 std::string proxy_bypass_list;
1237 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
1238 pc->ParseNoProxyList(proxy_bypass_list);
1239 }
1240 std::string proxy_server;
1241 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
1242 pc->proxy_rules.ParseFromString(proxy_server);
1243 }
1244 }
1245
1246 private:
[email protected]be180c802009-10-23 06:33:311247 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:171248 std::string proxy_config_;
1249};
1250
1251
1252void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:311253 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
1254 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:171255 FilePath user_data_dir;
1256 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1257 ProfileManager* profile_manager = g_browser_process->profile_manager();
1258 DCHECK(profile_manager);
1259 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1260 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:311261 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171262 }
[email protected]be180c802009-10-23 06:33:311263 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:171264
[email protected]81ae0a92009-08-06 02:16:161265 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
[email protected]be180c802009-10-23 06:33:311266 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171267}
1268
[email protected]4f3dc372009-02-24 00:10:291269void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331270 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291271 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291272 if (tab_tracker_->ContainsHandle(handle)) {
1273 NavigationController* tab = tab_tracker_->GetResource(handle);
1274 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1275 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331276 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291277 }
initial.commit09911bf2008-07-26 23:55:291278}
1279
[email protected]14c0a032009-04-13 18:15:141280void AutomationProvider::OpenNewBrowserWindow(bool show,
1281 IPC::Message* reply_message) {
1282 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291283 // We may have no current browser windows open so don't rely on
1284 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]15952e462008-11-14 00:29:051285 Browser* browser = Browser::Create(profile_);
1286 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151287 if (show)
[email protected]15952e462008-11-14 00:29:051288 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291289}
1290
[email protected]71f65dd2009-02-11 19:14:561291void AutomationProvider::GetWindowForBrowser(int browser_handle,
1292 bool* success,
1293 int* handle) {
1294 *success = false;
1295 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291296
1297 if (browser_tracker_->ContainsHandle(browser_handle)) {
1298 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201299 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291300 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201301 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561302 *success = true;
initial.commit09911bf2008-07-26 23:55:291303 }
initial.commit09911bf2008-07-26 23:55:291304}
1305
[email protected]5ae5bed2009-08-21 18:52:441306#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291307void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561308 int browser_handle,
1309 bool* success,
1310 int* autocomplete_edit_handle) {
1311 *success = false;
1312 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291313
1314 if (browser_tracker_->ContainsHandle(browser_handle)) {
1315 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061316 LocationBar* loc_bar = browser->window()->GetLocationBar();
1317 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291318 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561319 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1320 *success = true;
initial.commit09911bf2008-07-26 23:55:291321 }
initial.commit09911bf2008-07-26 23:55:291322}
[email protected]5ae5bed2009-08-21 18:52:441323#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291324
[email protected]71f65dd2009-02-11 19:14:561325void AutomationProvider::ShowInterstitialPage(int tab_handle,
1326 const std::string& html_text,
1327 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291328 if (tab_tracker_->ContainsHandle(tab_handle)) {
1329 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111330 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401331
[email protected]2e028a082009-08-19 20:32:581332 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]965524b2009-04-04 21:32:401333 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341334 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401335 GURL("about:interstitial"),
1336 html_text);
1337 interstitial->Show();
1338 return;
initial.commit09911bf2008-07-26 23:55:291339 }
[email protected]71f65dd2009-02-11 19:14:561340
[email protected]457f5cf2009-08-18 16:37:521341 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1342 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561343 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291344}
1345
[email protected]71f65dd2009-02-11 19:14:561346void AutomationProvider::HideInterstitialPage(int tab_handle,
1347 bool* success) {
1348 *success = false;
[email protected]57c6a652009-05-04 07:58:341349 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1350 if (tab_contents && tab_contents->interstitial_page()) {
1351 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561352 *success = true;
initial.commit09911bf2008-07-26 23:55:291353 }
initial.commit09911bf2008-07-26 23:55:291354}
1355
[email protected]71f65dd2009-02-11 19:14:561356void AutomationProvider::CloseTab(int tab_handle,
1357 bool wait_until_closed,
1358 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291359 if (tab_tracker_->ContainsHandle(tab_handle)) {
1360 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1361 int index;
1362 Browser* browser = Browser::GetBrowserForController(controller, &index);
1363 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141364 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111365 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451366 return;
initial.commit09911bf2008-07-26 23:55:291367 }
[email protected]de246f52009-02-25 18:25:451368
1369 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:551370 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291371}
1372
[email protected]71f65dd2009-02-11 19:14:561373void AutomationProvider::CloseBrowser(int browser_handle,
1374 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291375 if (browser_tracker_->ContainsHandle(browser_handle)) {
1376 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561377 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561378 reply_message);
[email protected]f3e99e32008-07-30 04:48:391379 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291380 } else {
1381 NOTREACHED();
1382 }
1383}
1384
[email protected]71f65dd2009-02-11 19:14:561385void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1386 if (browser_tracker_->ContainsHandle(browser_handle)) {
1387 Browser* browser = browser_tracker_->GetResource(browser_handle);
1388 browser->window()->Close();
1389 } else {
1390 NOTREACHED();
1391 }
1392}
1393
[email protected]71f65dd2009-02-11 19:14:561394void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:571395 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:561396 AutomationMsg_NavigationResponseValues* status) {
1397 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291398
1399 if (tab_tracker_->ContainsHandle(handle)) {
1400 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:571401 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561402 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291403 }
initial.commit09911bf2008-07-26 23:55:291404}
1405
[email protected]4150ef02009-08-19 23:14:261406void AutomationProvider::NavigateExternalTabAtIndex(
1407 int handle, int navigation_index,
1408 AutomationMsg_NavigationResponseValues* status) {
1409 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1410
1411 if (tab_tracker_->ContainsHandle(handle)) {
1412 NavigationController* tab = tab_tracker_->GetResource(handle);
1413 tab->GoToIndex(navigation_index);
1414 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1415 }
1416}
1417
[email protected]71f65dd2009-02-11 19:14:561418void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1419 IPC::Message* reply_message) {
1420 if (tab_tracker_->ContainsHandle(tab_handle)) {
1421 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1422 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141423 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561424 }
1425}
1426
[email protected]71f65dd2009-02-11 19:14:561427void AutomationProvider::GetSecurityState(int handle, bool* success,
1428 SecurityStyle* security_style,
1429 int* ssl_cert_status,
1430 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291431 if (tab_tracker_->ContainsHandle(handle)) {
1432 NavigationController* tab = tab_tracker_->GetResource(handle);
1433 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561434 *success = true;
1435 *security_style = entry->ssl().security_style();
1436 *ssl_cert_status = entry->ssl().cert_status();
1437 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291438 } else {
[email protected]71f65dd2009-02-11 19:14:561439 *success = false;
1440 *security_style = SECURITY_STYLE_UNKNOWN;
1441 *ssl_cert_status = 0;
1442 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291443 }
1444}
1445
[email protected]71f65dd2009-02-11 19:14:561446void AutomationProvider::GetPageType(int handle, bool* success,
1447 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291448 if (tab_tracker_->ContainsHandle(handle)) {
1449 NavigationController* tab = tab_tracker_->GetResource(handle);
1450 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561451 *page_type = entry->page_type();
1452 *success = true;
initial.commit09911bf2008-07-26 23:55:291453 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341454 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561455 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341456 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561457 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291458 } else {
[email protected]71f65dd2009-02-11 19:14:561459 *success = false;
1460 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291461 }
1462}
1463
[email protected]84abba62009-10-07 17:01:441464void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
1465 int* duration_ms) {
1466 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
1467 event_name);
1468}
1469
[email protected]71f65dd2009-02-11 19:14:561470void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1471 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291472 if (tab_tracker_->ContainsHandle(handle)) {
1473 NavigationController* tab = tab_tracker_->GetResource(handle);
1474 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071475 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401476 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471477 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341478 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291479 if (ssl_blocking_page) {
1480 if (proceed) {
[email protected]2e028a082009-08-19 20:32:581481 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]71f65dd2009-02-11 19:14:561482 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291483 return;
1484 }
1485 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521486 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1487 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561488 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291489 return;
1490 }
1491 }
1492 }
1493 // We failed.
[email protected]457f5cf2009-08-18 16:37:521494 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1495 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561496 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291497}
initial.commit09911bf2008-07-26 23:55:291498
[email protected]71f65dd2009-02-11 19:14:561499void AutomationProvider::BringBrowserToFront(int browser_handle,
1500 bool* success) {
initial.commit09911bf2008-07-26 23:55:291501 if (browser_tracker_->ContainsHandle(browser_handle)) {
1502 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061503 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561504 *success = true;
initial.commit09911bf2008-07-26 23:55:291505 } else {
[email protected]71f65dd2009-02-11 19:14:561506 *success = false;
initial.commit09911bf2008-07-26 23:55:291507 }
1508}
1509
[email protected]71f65dd2009-02-11 19:14:561510void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1511 int message_num,
1512 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291513 if (browser_tracker_->ContainsHandle(browser_handle)) {
1514 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561515 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141516 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291517 } else {
[email protected]71f65dd2009-02-11 19:14:561518 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291519 }
1520}
1521
[email protected]71f65dd2009-02-11 19:14:561522void AutomationProvider::PrintNow(int tab_handle,
1523 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441524#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571525 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341526 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1527 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291528 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571529 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141530 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341531 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571532 return;
initial.commit09911bf2008-07-26 23:55:291533 }
[email protected]71f65dd2009-02-11 19:14:561534 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1535 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441536#else
1537 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1538 NOTIMPLEMENTED();
1539#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291540}
[email protected]d301c952009-07-13 15:02:411541
[email protected]71f65dd2009-02-11 19:14:561542void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111543 const FilePath& file_name,
1544 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561545 int type,
1546 bool* success) {
initial.commit09911bf2008-07-26 23:55:291547 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561548 *success = false;
initial.commit09911bf2008-07-26 23:55:291549 return;
1550 }
1551
1552 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1553 Browser* browser = FindAndActivateTab(nav);
1554 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141555 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561556 *success = false;
initial.commit09911bf2008-07-26 23:55:291557 return;
1558 }
1559
initial.commit09911bf2008-07-26 23:55:291560 SavePackage::SavePackageType save_type =
1561 static_cast<SavePackage::SavePackageType>(type);
1562 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1563 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341564 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291565
[email protected]71f65dd2009-02-11 19:14:561566 *success = true;
initial.commit09911bf2008-07-26 23:55:291567}
1568
[email protected]5ae5bed2009-08-21 18:52:441569#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471570// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561571void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1572 bool* success,
1573 std::wstring* text) {
1574 *success = false;
initial.commit09911bf2008-07-26 23:55:291575 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561576 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521577 GetText();
[email protected]71f65dd2009-02-11 19:14:561578 *success = true;
initial.commit09911bf2008-07-26 23:55:291579 }
initial.commit09911bf2008-07-26 23:55:291580}
1581
[email protected]71f65dd2009-02-11 19:14:561582void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1583 const std::wstring& text,
1584 bool* success) {
1585 *success = false;
initial.commit09911bf2008-07-26 23:55:291586 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521587 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1588 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561589 *success = true;
initial.commit09911bf2008-07-26 23:55:291590 }
initial.commit09911bf2008-07-26 23:55:291591}
1592
1593void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561594 int autocomplete_edit_handle,
1595 bool* success,
1596 std::vector<AutocompleteMatchData>* matches) {
1597 *success = false;
initial.commit09911bf2008-07-26 23:55:291598 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271599 const AutocompleteResult& result = autocomplete_edit_tracker_->
1600 GetResource(autocomplete_edit_handle)->model()->result();
1601 for (AutocompleteResult::const_iterator i = result.begin();
1602 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561603 matches->push_back(AutocompleteMatchData(*i));
1604 *success = true;
initial.commit09911bf2008-07-26 23:55:291605 }
initial.commit09911bf2008-07-26 23:55:291606}
1607
1608void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561609 int autocomplete_edit_handle,
1610 bool* success,
1611 bool* query_in_progress) {
1612 *success = false;
1613 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291614 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561615 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521616 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561617 *success = true;
initial.commit09911bf2008-07-26 23:55:291618 }
initial.commit09911bf2008-07-26 23:55:291619}
1620
[email protected]28790922009-03-09 19:48:371621void AutomationProvider::OnMessageFromExternalHost(int handle,
1622 const std::string& message,
1623 const std::string& origin,
1624 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191625 RenderViewHost* view_host = GetViewForTab(handle);
1626 if (!view_host) {
1627 return;
[email protected]fa83e762008-08-15 21:41:391628 }
[email protected]f7a68432009-07-29 23:18:191629
1630 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1631 view_host, message, origin, target)) {
1632 // Message was diverted.
1633 return;
1634 }
1635
1636 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1637 origin, target, this, view_host, handle)) {
1638 // Message was diverted.
1639 return;
1640 }
1641
1642 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1643 // Message was diverted.
1644 return;
1645 }
1646
1647 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391648}
[email protected]a9024892009-06-16 23:13:551649
1650bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1651 const std::string& message, const std::string& origin,
1652 const std::string& target) {
1653 if (target !=
1654 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1655 return false;
1656
1657 if (origin != extension_automation_constants::kAutomationOrigin) {
1658 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1659 return false;
1660 }
1661
1662 // The message is a JSON-encoded array with two elements, both strings. The
1663 // first is the name of the event to dispatch. The second is a JSON-encoding
1664 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:201665 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:551666 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1667 LOG(WARNING) << "Invalid browser event specified through automation";
1668 return false;
1669 }
1670
1671 const ListValue* args = static_cast<const ListValue*>(message_value.get());
1672
1673 std::string event_name;
1674 if (!args->GetString(0, &event_name)) {
1675 LOG(WARNING) << "No browser event name specified through automation";
1676 return false;
1677 }
1678
1679 std::string json_args;
1680 if (!args->GetString(1, &json_args)) {
1681 LOG(WARNING) << "No browser event args specified through automation";
1682 return false;
1683 }
1684
[email protected]7120f132009-07-20 21:05:371685 if (profile()->GetExtensionMessageService()) {
1686 profile()->GetExtensionMessageService()->
1687 DispatchEventToRenderers(event_name.c_str(), json_args);
1688 }
[email protected]a9024892009-06-16 23:13:551689
1690 return true;
1691}
[email protected]5ae5bed2009-08-21 18:52:441692#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:391693
[email protected]57c6a652009-05-04 07:58:341694TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:571695 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:571696 if (tab_tracker_->ContainsHandle(handle)) {
1697 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:341698 if (tab)
1699 *tab = nav_controller;
1700 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:571701 }
[email protected]57c6a652009-05-04 07:58:341702 return NULL;
[email protected]20e93d12008-08-28 16:31:571703}
1704
initial.commit09911bf2008-07-26 23:55:291705TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
1706 : AutomationProvider(profile) {
1707 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:141708 registrar_.Add(this, NotificationType::SESSION_END,
1709 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:291710}
1711
1712TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:291713 BrowserList::RemoveObserver(this);
1714}
1715
1716void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:141717 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:291718 AutomationProvider::OnChannelError();
1719}
1720
1721void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
1722 // For backwards compatibility with the testing automation interface, we
1723 // want the automation provider (and hence the process) to go away when the
1724 // last browser goes away.
1725 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:291726 // If you change this, update Observer for NotificationType::SESSION_END
1727 // below.
[email protected]295039bd2008-08-15 04:32:571728 MessageLoop::current()->PostTask(FROM_HERE,
1729 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:291730 }
1731}
1732
1733void TestingAutomationProvider::Observe(NotificationType type,
1734 const NotificationSource& source,
1735 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561736 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:291737 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
1738 // before the task runs resulting in this object not being deleted. This
1739 // Release balance out the Release scheduled by OnBrowserRemoving.
1740 Release();
1741}
[email protected]295039bd2008-08-15 04:32:571742
1743void TestingAutomationProvider::OnRemoveProvider() {
1744 AutomationProviderList::GetInstance()->RemoveProvider(this);
1745}
[email protected]8a3422c92008-09-24 17:42:421746
[email protected]71f65dd2009-02-11 19:14:561747void AutomationProvider::GetSSLInfoBarCount(int handle, int* count) {
1748 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:421749 if (tab_tracker_->ContainsHandle(handle)) {
1750 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:341751 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:111752 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421753 }
[email protected]8a3422c92008-09-24 17:42:421754}
1755
[email protected]71f65dd2009-02-11 19:14:561756void AutomationProvider::ClickSSLInfoBarLink(int handle,
[email protected]8a3422c92008-09-24 17:42:421757 int info_bar_index,
[email protected]71f65dd2009-02-11 19:14:561758 bool wait_for_navigation,
1759 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421760 bool success = false;
1761 if (tab_tracker_->ContainsHandle(handle)) {
1762 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1763 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:111764 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421765 if (info_bar_index >= 0 && info_bar_index < count) {
1766 if (wait_for_navigation) {
[email protected]2e028a082009-08-19 20:32:581767 AddNavigationStatusListener(nav_controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421768 }
[email protected]eb9ba192008-12-02 02:41:341769 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:111770 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:341771 info_bar_index);
1772 if (delegate->AsConfirmInfoBarDelegate())
1773 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:421774 success = true;
1775 }
1776 }
[email protected]4f3dc372009-02-24 00:10:291777 }
[email protected]58f622a62009-10-04 01:17:551778
1779 // This "!wait_for_navigation || !success condition" logic looks suspicious.
1780 // It will send a failure message when success is true but
1781 // |wait_for_navigation| is false.
1782 // TODO(phajdan.jr): investgate whether the reply param (currently
1783 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:421784 if (!wait_for_navigation || !success)
[email protected]457f5cf2009-08-18 16:37:521785 AutomationMsg_ClickSSLInfoBarLink::WriteReplyParams(
1786 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:421787}
1788
[email protected]71f65dd2009-02-11 19:14:561789void AutomationProvider::GetLastNavigationTime(int handle,
1790 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:421791 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:561792 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:421793}
1794
[email protected]71f65dd2009-02-11 19:14:561795void AutomationProvider::WaitForNavigation(int handle,
1796 int64 last_navigation_time,
1797 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:251798 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:421799 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:251800
[email protected]8a3422c92008-09-24 17:42:421801 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:561802 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:521803 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
1804 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:551805 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:291806 return;
[email protected]8a3422c92008-09-24 17:42:421807 }
1808
[email protected]2e028a082009-08-19 20:32:581809 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421810}
1811
[email protected]71f65dd2009-02-11 19:14:561812void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161813 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561814 int value,
1815 bool* success) {
1816 *success = false;
[email protected]8a3422c92008-09-24 17:42:421817 if (browser_tracker_->ContainsHandle(handle)) {
1818 Browser* browser = browser_tracker_->GetResource(handle);
1819 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561820 *success = true;
[email protected]8a3422c92008-09-24 17:42:421821 }
[email protected]8a3422c92008-09-24 17:42:421822}
[email protected]97fa6ce32008-12-19 01:48:161823
[email protected]71f65dd2009-02-11 19:14:561824void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161825 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561826 const std::wstring& value,
1827 bool* success) {
1828 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161829 if (browser_tracker_->ContainsHandle(handle)) {
1830 Browser* browser = browser_tracker_->GetResource(handle);
1831 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561832 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161833 }
[email protected]97fa6ce32008-12-19 01:48:161834}
1835
[email protected]71f65dd2009-02-11 19:14:561836void AutomationProvider::GetBooleanPreference(int handle,
1837 const std::wstring& name,
1838 bool* success,
1839 bool* value) {
1840 *success = false;
1841 *value = false;
[email protected]97fa6ce32008-12-19 01:48:161842 if (browser_tracker_->ContainsHandle(handle)) {
1843 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561844 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
1845 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161846 }
[email protected]97fa6ce32008-12-19 01:48:161847}
1848
[email protected]71f65dd2009-02-11 19:14:561849void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161850 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561851 bool value,
1852 bool* success) {
1853 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161854 if (browser_tracker_->ContainsHandle(handle)) {
1855 Browser* browser = browser_tracker_->GetResource(handle);
1856 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561857 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161858 }
[email protected]97fa6ce32008-12-19 01:48:161859}
1860
1861// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561862void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:401863 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:161864 if (tab_tracker_->ContainsHandle(tab_handle)) {
1865 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1866 Browser* browser = FindAndActivateTab(nav);
1867 DCHECK(browser);
1868
[email protected]57c6a652009-05-04 07:58:341869 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
1870 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:161871 }
[email protected]97fa6ce32008-12-19 01:48:161872}
1873
1874// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561875void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:401876 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:561877 bool* success) {
1878 *success = false;
[email protected]de246f52009-02-25 18:25:451879#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:161880 if (tab_tracker_->ContainsHandle(tab_handle)) {
1881 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1882 Browser* browser = FindAndActivateTab(nav);
1883 DCHECK(browser);
1884
[email protected]1fc025202009-01-20 23:03:141885 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:161886 int selected_encoding_id =
1887 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
1888 if (selected_encoding_id) {
1889 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:561890 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161891 }
1892 }
1893 }
[email protected]de246f52009-02-25 18:25:451894#else
1895 // TODO(port): Enable when encoding-related parts of Browser are ported.
1896 NOTIMPLEMENTED();
1897#endif
[email protected]97fa6ce32008-12-19 01:48:161898}
[email protected]5bcdb312009-01-07 21:43:201899
[email protected]4d434a1a2009-02-11 21:06:571900void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:201901 SavePackage::SetShouldPromptUser(should_prompt);
1902}
[email protected]87eab222009-03-13 00:47:451903
[email protected]50f53162009-10-23 19:16:201904void AutomationProvider::SetEnableExtensionAutomation(
1905 const std::vector<std::string>& functions_enabled) {
1906 AutomationExtensionFunction::SetEnabled(functions_enabled);
[email protected]b83e4602009-05-15 22:58:331907}
1908
[email protected]3753f522009-04-14 23:15:471909void AutomationProvider::GetWindowTitle(int handle, string16* text) {
1910 gfx::NativeWindow window = window_tracker_->GetResource(handle);
1911 text->assign(platform_util::GetWindowTitle(window));
1912}
[email protected]66ba4932009-06-04 19:22:131913
1914void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
1915 *count = -1; // -1 is the error code
1916 if (tab_tracker_->ContainsHandle(handle)) {
1917 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1918 TabContents* tab_contents = nav_controller->tab_contents();
1919 if (tab_contents) {
1920 BlockedPopupContainer* container =
1921 tab_contents->blocked_popup_container();
1922 if (container) {
1923 *count = static_cast<int>(container->GetBlockedPopupCount());
1924 } else {
1925 // If we don't have a container, we don't have any blocked popups to
1926 // contain!
1927 *count = 0;
1928 }
1929 }
1930 }
1931}
[email protected]f7a68432009-07-29 23:18:191932
1933void AutomationProvider::SelectAll(int tab_handle) {
1934 RenderViewHost* view = GetViewForTab(tab_handle);
1935 if (!view) {
1936 NOTREACHED();
1937 return;
1938 }
1939
1940 view->SelectAll();
1941}
1942
1943void AutomationProvider::Cut(int tab_handle) {
1944 RenderViewHost* view = GetViewForTab(tab_handle);
1945 if (!view) {
1946 NOTREACHED();
1947 return;
1948 }
1949
1950 view->Cut();
1951}
1952
1953void AutomationProvider::Copy(int tab_handle) {
1954 RenderViewHost* view = GetViewForTab(tab_handle);
1955 if (!view) {
1956 NOTREACHED();
1957 return;
1958 }
1959
1960 view->Copy();
1961}
1962
1963void AutomationProvider::Paste(int tab_handle) {
1964 RenderViewHost* view = GetViewForTab(tab_handle);
1965 if (!view) {
1966 NOTREACHED();
1967 return;
1968 }
1969
1970 view->Paste();
1971}
1972
1973void AutomationProvider::ReloadAsync(int tab_handle) {
1974 if (tab_tracker_->ContainsHandle(tab_handle)) {
1975 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1976 if (!tab) {
1977 NOTREACHED();
1978 return;
1979 }
1980
1981 tab->Reload(false);
1982 }
1983}
1984
1985void AutomationProvider::StopAsync(int tab_handle) {
1986 RenderViewHost* view = GetViewForTab(tab_handle);
1987 if (!view) {
[email protected]8b2b3312009-09-14 18:38:361988 // We tolerate StopAsync being called even before a view has been created.
1989 // So just log a warning instead of a NOTREACHED().
1990 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:191991 return;
1992 }
1993
1994 view->Stop();
1995}
1996
[email protected]1bb5f892009-10-06 01:44:571997void AutomationProvider::OnSetPageFontSize(int tab_handle,
1998 int font_size) {
1999 AutomationPageFontSize automation_font_size =
2000 static_cast<AutomationPageFontSize>(font_size);
2001
2002 if (automation_font_size < SMALLEST_FONT ||
2003 automation_font_size > LARGEST_FONT) {
2004 DLOG(ERROR) << "Invalid font size specified : "
2005 << font_size;
2006 return;
2007 }
2008
2009 if (tab_tracker_->ContainsHandle(tab_handle)) {
2010 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2011 DCHECK(tab != NULL);
2012 if (tab && tab->tab_contents()) {
2013 DCHECK(tab->tab_contents()->profile() != NULL);
2014 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2015 prefs::kWebKitDefaultFontSize, font_size);
2016 }
2017 }
2018}
2019
2020
[email protected]2949e90d2009-08-21 15:32:522021void AutomationProvider::WaitForBrowserWindowCountToBecome(
2022 int target_count, IPC::Message* reply_message) {
2023 if (static_cast<int>(BrowserList::size()) == target_count) {
2024 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2025 reply_message, true);
2026 Send(reply_message);
2027 return;
2028 }
2029
2030 // Set up an observer (it will delete itself).
2031 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2032}
2033
2034void AutomationProvider::WaitForAppModalDialogToBeShown(
2035 IPC::Message* reply_message) {
2036 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2037 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2038 reply_message, true);
2039 Send(reply_message);
2040 return;
2041 }
2042
2043 // Set up an observer (it will delete itself).
2044 new AppModalDialogShownObserver(this, reply_message);
2045}
2046
[email protected]1126a1d32009-08-26 15:39:262047void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2048 int handle, int number_of_navigations, IPC::Message* reply_message) {
2049 if (tab_tracker_->ContainsHandle(handle)) {
2050 NavigationController* tab = tab_tracker_->GetResource(handle);
2051 Browser* browser = FindAndActivateTab(tab);
2052 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
2053 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2054 browser->GoBack(CURRENT_TAB);
2055 return;
2056 }
2057 }
2058
2059 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2060 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2061 Send(reply_message);
2062}
2063
2064void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2065 int handle, int number_of_navigations, IPC::Message* reply_message) {
2066 if (tab_tracker_->ContainsHandle(handle)) {
2067 NavigationController* tab = tab_tracker_->GetResource(handle);
2068 Browser* browser = FindAndActivateTab(tab);
2069 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
2070 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2071 browser->GoForward(CURRENT_TAB);
2072 return;
2073 }
2074 }
2075
2076 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2077 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2078 Send(reply_message);
2079}
2080
[email protected]f7a68432009-07-29 23:18:192081RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2082 if (tab_tracker_->ContainsHandle(tab_handle)) {
2083 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2084 if (!tab) {
2085 NOTREACHED();
2086 return NULL;
2087 }
2088
2089 TabContents* tab_contents = tab->tab_contents();
2090 if (!tab_contents) {
2091 NOTREACHED();
2092 return NULL;
2093 }
2094
2095 RenderViewHost* view_host = tab_contents->render_view_host();
2096 return view_host;
2097 }
2098
2099 return NULL;
2100}
[email protected]675595f2009-08-26 22:32:042101
2102void AutomationProvider::GetBrowserForWindow(int window_handle,
2103 bool* success,
2104 int* browser_handle) {
2105 *success = false;
2106 *browser_handle = 0;
2107
2108 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2109 if (!window)
2110 return;
2111
2112 BrowserList::const_iterator iter = BrowserList::begin();
2113 for (;iter != BrowserList::end(); ++iter) {
2114 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2115 if (window == this_window) {
2116 // Add() returns the existing handle for the resource if any.
2117 *browser_handle = browser_tracker_->Add(*iter);
2118 *success = true;
2119 return;
2120 }
2121 }
2122}
[email protected]d11c8e92009-10-20 23:26:402123
2124void AutomationProvider::InstallExtension(const FilePath& crx_path,
2125 IPC::Message* reply_message) {
2126 ExtensionsService* service = profile_->GetExtensionsService();
2127 if (service) {
2128 // The observer will delete itself when done.
2129 new ExtensionNotificationObserver(this,
2130 AutomationMsg_InstallExtension::ID,
2131 reply_message);
2132
2133 const FilePath& install_dir = service->install_directory();
2134 MessageLoop* io_loop = g_browser_process->file_thread()->message_loop();
2135
2136 CrxInstaller::Start(crx_path,
2137 install_dir,
2138 Extension::INTERNAL,
2139 "", // no expected id
2140 false, // please do not delete crx file
2141 true, // privilege increase allowed
2142 io_loop,
2143 service,
2144 NULL); // silent isntall, no UI
2145 } else {
2146 AutomationMsg_InstallExtension::WriteReplyParams(
2147 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2148 Send(reply_message);
2149 }
2150}
2151
2152void AutomationProvider::LoadExpandedExtension(
2153 const FilePath& extension_dir,
2154 IPC::Message* reply_message) {
2155 if (profile_->GetExtensionsService() && profile_->GetUserScriptMaster()) {
2156 // The observer will delete itself when done.
2157 new ExtensionNotificationObserver(this,
2158 AutomationMsg_LoadExpandedExtension::ID,
2159 reply_message);
2160
2161 profile_->GetExtensionsService()->LoadExtension(extension_dir);
2162 profile_->GetUserScriptMaster()->AddWatchedPath(extension_dir);
2163 } else {
2164 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
2165 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2166 Send(reply_message);
2167 }
2168}