blob: 93aad4d80c6dbbe8824ce2e828a59a86f9600e7a [file] [log] [blame]
[email protected]5ae5bed2009-08-21 18:52:441// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/automation/automation_provider.h"
6
[email protected]5ae5bed2009-08-21 18:52:447#include <set>
8
[email protected]202e7a72009-06-15 03:48:369#include "app/l10n_util.h"
[email protected]37126212009-05-06 02:23:3110#include "app/message_box_flags.h"
[email protected]c6cb1992009-04-13 16:45:2911#include "base/file_version_info.h"
[email protected]a9024892009-06-16 23:13:5512#include "base/json_reader.h"
[email protected]5fac9622009-02-04 21:49:3813#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/path_service.h"
[email protected]f44265b2009-05-19 18:52:5015#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2716#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3817#include "base/thread.h"
[email protected]a7eee32f2009-05-22 18:08:1718#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2919#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2820#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0921#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3322#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0824#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5525#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5026#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1327#include "chrome/browser/blocked_popup_container.h"
[email protected]5c238752009-06-13 10:29:0728#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3929#include "chrome/browser/browser_window.h"
initial.commit09911bf2008-07-26 23:55:2930#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1231#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5932#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2233#include "chrome/browser/download/download_shelf.h"
[email protected]a9024892009-06-16 23:13:5534#include "chrome/browser/extensions/extension_message_service.h"
[email protected]4801ecc2009-04-05 04:52:5835#include "chrome/browser/find_bar.h"
36#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2937#include "chrome/browser/find_notification_details.h"
[email protected]13869dd2009-05-05 00:40:0638#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0139#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5340#include "chrome/browser/net/url_request_mock_util.h"
[email protected]a7eee32f2009-05-22 18:08:1741#include "chrome/browser/profile_manager.h"
[email protected]6524b5f92009-01-22 17:48:2542#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0343#include "chrome/browser/ssl/ssl_manager.h"
44#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3445#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4546#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1747#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2948#include "chrome/common/chrome_paths.h"
[email protected]a7eee32f2009-05-22 18:08:1749#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1450#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4751#include "chrome/common/platform_util.h"
[email protected]8a3422c92008-09-24 17:42:4252#include "chrome/common/pref_service.h"
[email protected]71f65dd2009-02-11 19:14:5653#include "chrome/test/automation/automation_messages.h"
[email protected]a7eee32f2009-05-22 18:08:1754#include "net/proxy/proxy_service.h"
55#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2156#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2857#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2958
[email protected]de246f52009-02-25 18:25:4559#if defined(OS_WIN)
60// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4561#include "chrome/browser/character_encoding.h"
62#include "chrome/browser/download/save_package.h"
63#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4564#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4565#endif // defined(OS_WIN)
66
[email protected]5ae5bed2009-08-21 18:52:4467#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2868// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2869#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4470#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0671
[email protected]e1acf6f2008-10-27 20:43:3372using base::Time;
73
[email protected]cbab76d2008-10-13 22:42:4774class AutomationInterstitialPage : public InterstitialPage {
75 public:
[email protected]57c6a652009-05-04 07:58:3476 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4777 const GURL& url,
78 const std::string& contents)
79 : InterstitialPage(tab, true, url),
80 contents_(contents) {
81 }
82
83 virtual std::string GetHTMLContents() { return contents_; }
84
85 private:
86 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:2987
[email protected]cbab76d2008-10-13 22:42:4788 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
89};
90
[email protected]c2cb8542009-08-20 21:16:5191#if !defined(OS_MACOSX)
92class ClickTask : public Task {
93 public:
[email protected]fc2e0872009-08-21 22:14:4194 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:5195 virtual ~ClickTask() {}
96
97 virtual void Run() {
98 ui_controls::MouseButton button = ui_controls::LEFT;
99 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
100 views::Event::EF_LEFT_BUTTON_DOWN) {
101 button = ui_controls::LEFT;
102 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
103 views::Event::EF_RIGHT_BUTTON_DOWN) {
104 button = ui_controls::RIGHT;
105 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
106 views::Event::EF_MIDDLE_BUTTON_DOWN) {
107 button = ui_controls::MIDDLE;
108 } else {
109 NOTREACHED();
110 }
111
[email protected]fc2e0872009-08-21 22:14:41112 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51113 }
114
115 private:
[email protected]c2cb8542009-08-20 21:16:51116 int flags_;
117
118 DISALLOW_COPY_AND_ASSIGN(ClickTask);
119};
120#endif
121
initial.commit09911bf2008-07-26 23:55:29122AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57123 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56124 profile_(profile),
125 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29126 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29127 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20128 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29129 autocomplete_edit_tracker_.reset(
130 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29131 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
132 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
initial.commit09911bf2008-07-26 23:55:29133}
134
135AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50136 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
137 port_containers_.end());
138 port_containers_.clear();
139
[email protected]0da050b92008-08-19 19:29:47140 // Make sure that any outstanding NotificationObservers also get destroyed.
141 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31142 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47143 while ((observer = it.GetNext()) != NULL)
144 delete observer;
initial.commit09911bf2008-07-26 23:55:29145}
146
[email protected]9a3a293b2009-06-04 22:28:16147void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06148 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57149 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06150 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
151 automation_resource_message_filter_,
152 g_browser_process->io_thread()->message_loop(),
153 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05154 scoped_ptr<FileVersionInfo> file_version_info(
155 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40156 std::string version_string;
157 if (file_version_info != NULL) {
158 version_string = WideToASCII(file_version_info->file_version());
159 }
[email protected]c6cb1992009-04-13 16:45:29160
161 // Send a hello message with our current automation protocol version.
162 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29163}
164
165void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
166 if (expected_tabs == 0) {
167 Send(new AutomationMsg_InitialLoadsComplete(0));
168 } else {
169 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
170 }
171}
172
initial.commit09911bf2008-07-26 23:55:29173NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58174 NavigationController* tab, IPC::Message* reply_message,
175 int number_of_navigations) {
initial.commit09911bf2008-07-26 23:55:29176 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58177 new NavigationNotificationObserver(tab, this, reply_message,
178 number_of_navigations);
initial.commit09911bf2008-07-26 23:55:29179
[email protected]71f65dd2009-02-11 19:14:56180 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29181 return observer;
182}
183
184void AutomationProvider::RemoveNavigationStatusListener(
185 NotificationObserver* obs) {
186 notification_observer_list_.RemoveObserver(obs);
187}
188
189NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14190 Browser* parent,
191 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56192 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14193 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29194 notification_observer_list_.AddObserver(observer);
195
196 return observer;
197}
198
199void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
200 notification_observer_list_.RemoveObserver(obs);
201}
202
203void AutomationProvider::AddLoginHandler(NavigationController* tab,
204 LoginHandler* handler) {
205 login_handler_map_[tab] = handler;
206}
207
208void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
209 DCHECK(login_handler_map_[tab]);
210 login_handler_map_.erase(tab);
211}
212
[email protected]f44265b2009-05-19 18:52:50213void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
214 int port_id = port->port_id();
215 DCHECK_NE(-1, port_id);
216 DCHECK(port_containers_.find(port_id) == port_containers_.end());
217
218 port_containers_[port_id] = port;
219}
220
221void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
222 int port_id = port->port_id();
223 DCHECK_NE(-1, port_id);
224
225 PortContainerMap::iterator it = port_containers_.find(port_id);
226 DCHECK(it != port_containers_.end());
227
228 if (it != port_containers_.end()) {
229 delete it->second;
230 port_containers_.erase(it);
231 }
232}
233
234ExtensionPortContainer* AutomationProvider::GetPortContainer(
235 int port_id) const {
236 PortContainerMap::const_iterator it = port_containers_.find(port_id);
237 if (it == port_containers_.end())
238 return NULL;
239
240 return it->second;
241}
242
initial.commit09911bf2008-07-26 23:55:29243int AutomationProvider::GetIndexForNavigationController(
244 const NavigationController* controller, const Browser* parent) const {
245 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12246 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29247}
248
249void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
250 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14251 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56252 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
253 CloseBrowserAsync)
254 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
255 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
256 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
257 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
258 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
259 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14260 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58261 IPC_MESSAGE_HANDLER_DELAY_REPLY(
262 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
263 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56264 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
265 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
266 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
267 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
268 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14269 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56270 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
271 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
272 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14273 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29274 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
275 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56276 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36277 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56278 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29279 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56280 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29281 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
282 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56283 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14284 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]de246f52009-02-25 18:25:45285#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56286 IPC_MESSAGE_HANDLER(AutomationMsg_WindowHWND, GetWindowHWND)
[email protected]de246f52009-02-25 18:25:45287#endif // defined(OS_WIN)
[email protected]49a14a82009-03-31 04:16:44288 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33289 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44290 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33291 ExecuteBrowserCommand)
[email protected]1c58a5c2009-05-21 18:47:14292 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8f04ff92009-07-08 02:37:15293 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14294 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44295#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24296 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]1c58a5c2009-05-21 18:47:14297 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]5ae5bed2009-08-21 18:52:44298#endif // !defined(OS_MACOSX)
[email protected]9a08bcf2009-08-12 19:56:28299#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56300 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
301 WindowSimulateDrag)
[email protected]d7fa7552009-03-20 21:06:37302#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56303 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
304 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37305#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56306 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45307#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56308 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
309 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03310 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56311 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14312 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
initial.commit09911bf2008-07-26 23:55:29313 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14314 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56315 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
316 ExecuteJavascript)
317 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29318 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14319 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
320 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56321 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
322 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14323 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17324 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14325 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14326 OpenNewBrowserWindow)
327 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56328 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14329 GetAutocompleteEditForBrowser)
330 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45331#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29332 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17333#endif
[email protected]71f65dd2009-02-11 19:14:56334 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29335 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26336 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
337 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56338 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14339 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56340 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14341 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17342#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29343 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
344 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17345#endif
[email protected]71f65dd2009-02-11 19:14:56346 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
347 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14348 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17349#if defined(OS_WIN)
[email protected]87eab222009-03-13 00:47:45350 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39351 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
352 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17353#endif
[email protected]1c58a5c2009-05-21 18:47:14354 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
355 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]71f65dd2009-02-11 19:14:56356 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
357 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29358 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
359 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
360 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56361 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41362 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56363 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
364 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29365 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56366 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29367 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56368 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29369 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56370 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29371 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56372 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45373 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45374 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
375 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14376 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56377 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57378 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56379 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57380 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56381 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
382 GetBookmarkBarVisibility)
[email protected]1c58a5c2009-05-21 18:47:14383 IPC_MESSAGE_HANDLER(AutomationMsg_GetSSLInfoBarCount, GetSSLInfoBarCount)
[email protected]71f65dd2009-02-11 19:14:56384 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickSSLInfoBarLink,
385 ClickSSLInfoBarLink)
386 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42387 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56388 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
389 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14390 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56391 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44392 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56393 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20394 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14395 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56396 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16397 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56398 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16399 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56400 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16401 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14402 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20403 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
404 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14405 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]b83e4602009-05-15 22:58:33406 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
407 SetEnableExtensionAutomation)
[email protected]59560e0b2009-06-04 03:30:22408 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13409 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19410 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
411 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
412 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
413 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
414 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
415 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52416 IPC_MESSAGE_HANDLER_DELAY_REPLY(
417 AutomationMsg_WaitForBrowserWindowCountToBecome,
418 WaitForBrowserWindowCountToBecome)
419 IPC_MESSAGE_HANDLER_DELAY_REPLY(
420 AutomationMsg_WaitForAppModalDialogToBeShown,
421 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26422 IPC_MESSAGE_HANDLER_DELAY_REPLY(
423 AutomationMsg_GoBackBlockUntilNavigationsComplete,
424 GoBackBlockUntilNavigationsComplete)
425 IPC_MESSAGE_HANDLER_DELAY_REPLY(
426 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
427 GoForwardBlockUntilNavigationsComplete)
initial.commit09911bf2008-07-26 23:55:29428 IPC_END_MESSAGE_MAP()
429}
430
[email protected]71f65dd2009-02-11 19:14:56431void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
432 *status = -1;
initial.commit09911bf2008-07-26 23:55:29433 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
434 Browser* browser = browser_tracker_->GetResource(handle);
435 if (at_index >= 0 && at_index < browser->tab_count()) {
436 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56437 *status = 0;
initial.commit09911bf2008-07-26 23:55:29438 }
439 }
initial.commit09911bf2008-07-26 23:55:29440}
441
[email protected]71f65dd2009-02-11 19:14:56442void AutomationProvider::AppendTab(int handle, const GURL& url,
443 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29444 int append_tab_response = -1; // -1 is the error code
445 NotificationObserver* observer = NULL;
446
447 if (browser_tracker_->ContainsHandle(handle)) {
448 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14449 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58450 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
451 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39452 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29453 if (tab_contents) {
454 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57455 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29456 }
457 }
458
459 if (append_tab_response < 0) {
460 // The append tab failed. Remove the TabStripObserver
461 if (observer) {
462 RemoveTabStripObserver(observer);
463 delete observer;
464 }
465
[email protected]71f65dd2009-02-11 19:14:56466 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
467 append_tab_response);
468 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29469 }
470}
471
[email protected]71f65dd2009-02-11 19:14:56472void AutomationProvider::NavigateToURL(int handle, const GURL& url,
473 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58474 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
475}
476
477void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
478 int handle, const GURL& url, int number_of_navigations,
479 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29480 if (tab_tracker_->ContainsHandle(handle)) {
481 NavigationController* tab = tab_tracker_->GetResource(handle);
482
483 // Simulate what a user would do. Activate the tab and then navigate.
484 // We could allow navigating in a background tab in future.
485 Browser* browser = FindAndActivateTab(tab);
486
487 if (browser) {
[email protected]2e028a082009-08-19 20:32:58488 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
[email protected]71f65dd2009-02-11 19:14:56489
initial.commit09911bf2008-07-26 23:55:29490 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50491 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29492 return;
493 }
494 }
[email protected]71f65dd2009-02-11 19:14:56495
496 AutomationMsg_NavigateToURL::WriteReplyParams(
497 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
498 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29499}
[email protected]2949e90d2009-08-21 15:32:52500
[email protected]71f65dd2009-02-11 19:14:56501void AutomationProvider::NavigationAsync(int handle, const GURL& url,
502 bool* status) {
503 *status = false;
initial.commit09911bf2008-07-26 23:55:29504
505 if (tab_tracker_->ContainsHandle(handle)) {
506 NavigationController* tab = tab_tracker_->GetResource(handle);
507
508 // Simulate what a user would do. Activate the tab and then navigate.
509 // We could allow navigating in a background tab in future.
510 Browser* browser = FindAndActivateTab(tab);
511
512 if (browser) {
513 // Don't add any listener unless a callback mechanism is desired.
514 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50515 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56516 *status = true;
initial.commit09911bf2008-07-26 23:55:29517 }
518 }
initial.commit09911bf2008-07-26 23:55:29519}
520
[email protected]71f65dd2009-02-11 19:14:56521void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29522 if (tab_tracker_->ContainsHandle(handle)) {
523 NavigationController* tab = tab_tracker_->GetResource(handle);
524 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14525 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]2e028a082009-08-19 20:32:58526 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29527 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29528 return;
529 }
530 }
[email protected]71f65dd2009-02-11 19:14:56531
532 AutomationMsg_GoBack::WriteReplyParams(
533 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
534 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29535}
536
[email protected]71f65dd2009-02-11 19:14:56537void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29538 if (tab_tracker_->ContainsHandle(handle)) {
539 NavigationController* tab = tab_tracker_->GetResource(handle);
540 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14541 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]2e028a082009-08-19 20:32:58542 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29543 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29544 return;
545 }
546 }
[email protected]71f65dd2009-02-11 19:14:56547
548 AutomationMsg_GoForward::WriteReplyParams(
549 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
550 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29551}
552
[email protected]71f65dd2009-02-11 19:14:56553void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29554 if (tab_tracker_->ContainsHandle(handle)) {
555 NavigationController* tab = tab_tracker_->GetResource(handle);
556 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14557 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]2e028a082009-08-19 20:32:58558 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29559 browser->Reload();
560 return;
561 }
562 }
[email protected]71f65dd2009-02-11 19:14:56563
564 AutomationMsg_Reload::WriteReplyParams(
565 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
566 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29567}
568
[email protected]71f65dd2009-02-11 19:14:56569void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29570 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56571 const std::wstring& password,
572 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29573 if (tab_tracker_->ContainsHandle(tab_handle)) {
574 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
575 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
576
577 if (iter != login_handler_map_.end()) {
578 // If auth is needed again after this, assume login has failed. This is
579 // not strictly correct, because a navigation can require both proxy and
580 // server auth, but it should be OK for now.
581 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58582 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29583 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52584 return;
initial.commit09911bf2008-07-26 23:55:29585 }
586 }
[email protected]de246f52009-02-25 18:25:45587
[email protected]457f5cf2009-08-18 16:37:52588 AutomationMsg_SetAuth::WriteReplyParams(
589 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
590 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29591}
592
[email protected]71f65dd2009-02-11 19:14:56593void AutomationProvider::CancelAuth(int tab_handle,
594 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29595 if (tab_tracker_->ContainsHandle(tab_handle)) {
596 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
597 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
598
599 if (iter != login_handler_map_.end()) {
600 // If auth is needed again after this, something is screwy.
601 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58602 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29603 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52604 return;
initial.commit09911bf2008-07-26 23:55:29605 }
606 }
[email protected]de246f52009-02-25 18:25:45607
[email protected]457f5cf2009-08-18 16:37:52608 AutomationMsg_CancelAuth::WriteReplyParams(
609 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
610 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29611}
612
[email protected]71f65dd2009-02-11 19:14:56613void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
614 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29615
616 if (tab_tracker_->ContainsHandle(tab_handle)) {
617 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
618 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
619
620 if (iter != login_handler_map_.end()) {
621 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56622 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29623 }
624 }
initial.commit09911bf2008-07-26 23:55:29625}
626
[email protected]71f65dd2009-02-11 19:14:56627void AutomationProvider::GetRedirectsFrom(int tab_handle,
628 const GURL& source_url,
629 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29630 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
631 if (tab_tracker_->ContainsHandle(tab_handle)) {
632 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
633 HistoryService* history_service =
634 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
635
636 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
637 "has no history service";
638 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56639 DCHECK(reply_message_ == NULL);
640 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29641 // Schedule a history query for redirects. The response will be sent
642 // asynchronously from the callback the history system uses to notify us
643 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29644 redirect_query_ = history_service->QueryRedirectsFrom(
645 source_url, &consumer_,
646 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
647 return; // Response will be sent when query completes.
648 }
649 }
650
651 // Send failure response.
[email protected]deb57402009-02-06 01:35:30652 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56653 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
654 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29655}
656
[email protected]71f65dd2009-02-11 19:14:56657void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
658 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29659 if (browser_tracker_->ContainsHandle(handle)) {
660 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56661 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29662 }
initial.commit09911bf2008-07-26 23:55:29663}
664
[email protected]202e7a72009-06-15 03:48:36665void AutomationProvider::GetBrowserLocale(string16* locale) {
666 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06667 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36668}
669
[email protected]71f65dd2009-02-11 19:14:56670void AutomationProvider::GetBrowserWindowCount(int* window_count) {
671 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29672}
673
[email protected]24497032009-05-01 17:00:29674void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
675 *window_count = static_cast<int>(
676 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
677}
678
[email protected]71f65dd2009-02-11 19:14:56679void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
680 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07681 AppModalDialog* dialog_delegate =
682 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50683 *showing_dialog = (dialog_delegate != NULL);
684 if (*showing_dialog)
685 *dialog_button = dialog_delegate->GetDialogButtons();
686 else
[email protected]478ff2ed2009-04-21 23:49:18687 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20688}
689
[email protected]71f65dd2009-02-11 19:14:56690void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
691 *success = false;
[email protected]fad84eab2008-12-05 00:37:20692
[email protected]1f460072009-05-28 17:02:07693 AppModalDialog* dialog_delegate =
694 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50695 if (dialog_delegate &&
696 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18697 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
698 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28699 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56700 *success = true;
[email protected]fad84eab2008-12-05 00:37:20701 }
[email protected]478ff2ed2009-04-21 23:49:18702 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
703 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56704 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28705 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56706 *success = true;
[email protected]fad84eab2008-12-05 00:37:20707 }
708 }
[email protected]c274acc2008-11-11 20:13:44709}
710
[email protected]71f65dd2009-02-11 19:14:56711void AutomationProvider::GetBrowserWindow(int index, int* handle) {
712 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29713 if (index >= 0) {
714 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29715 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29716 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56717 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29718 }
719 }
initial.commit09911bf2008-07-26 23:55:29720}
721
[email protected]24497032009-05-01 17:00:29722void AutomationProvider::FindNormalBrowserWindow(int* handle) {
723 *handle = 0;
724 Browser* browser = BrowserList::FindBrowserWithType(profile_,
725 Browser::TYPE_NORMAL);
726 if (browser)
727 *handle = browser_tracker_->Add(browser);
728}
729
[email protected]71f65dd2009-02-11 19:14:56730void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
731 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29732 Browser* browser = BrowserList::GetLastActive();
733 if (browser)
[email protected]71f65dd2009-02-11 19:14:56734 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29735}
736
[email protected]9a08bcf2009-08-12 19:56:28737#if defined(OS_LINUX)
738// TODO(estade): use this implementation for all platforms?
739void AutomationProvider::GetActiveWindow(int* handle) {
740 gfx::NativeWindow window =
741 BrowserList::GetLastActive()->window()->GetNativeHandle();
742 *handle = window_tracker_->Add(window);
743}
744#endif
745
[email protected]4f6381ee2009-04-16 02:46:33746void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
747 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56748 *success = false;
[email protected]4ae62752008-08-04 23:28:47749 if (browser_tracker_->ContainsHandle(handle)) {
750 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14751 if (browser->command_updater()->SupportsCommand(command) &&
752 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47753 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56754 *success = true;
[email protected]4ae62752008-08-04 23:28:47755 }
756 }
[email protected]4ae62752008-08-04 23:28:47757}
758
[email protected]4f6381ee2009-04-16 02:46:33759void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56760 int handle, int command, IPC::Message* reply_message) {
761 if (browser_tracker_->ContainsHandle(handle)) {
762 Browser* browser = browser_tracker_->GetResource(handle);
763 if (browser->command_updater()->SupportsCommand(command) &&
764 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]d79ffea2009-05-07 20:51:42765 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
766 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27767 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42768 return;
769 }
[email protected]56e71b7c2009-03-27 03:05:56770 }
771 }
[email protected]49a14a82009-03-31 04:16:44772 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56773 Send(reply_message);
774}
775
[email protected]fc2e0872009-08-21 22:14:41776// This task just adds another task to the event queue. This is useful if
777// you want to ensure that any tasks added to the event queue after this one
778// have already been processed by the time |task| is run.
779class InvokeTaskLaterTask : public Task {
780 public:
781 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
782 virtual ~InvokeTaskLaterTask() {}
783
784 virtual void Run() {
785 MessageLoop::current()->PostTask(FROM_HERE, task_);
786 }
787
788 private:
789 Task* task_;
790
791 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
792};
793
794#if defined(OS_WIN)
795// TODO(port): Replace POINT and other windowsisms.
796
797// This task sends a WindowDragResponse message with the appropriate
798// routing ID to the automation proxy. This is implemented as a task so that
799// we know that the mouse events (and any tasks that they spawn on the message
800// loop) have been processed by the time this is sent.
801class WindowDragResponseTask : public Task {
802 public:
803 WindowDragResponseTask(AutomationProvider* provider,
804 IPC::Message* reply_message)
805 : provider_(provider), reply_message_(reply_message) {}
806 virtual ~WindowDragResponseTask() {}
807
808 virtual void Run() {
809 DCHECK(reply_message_ != NULL);
810 AutomationMsg_WindowDrag::WriteReplyParams(reply_message_, true);
811 provider_->Send(reply_message_);
812 }
813
814 private:
815 AutomationProvider* provider_;
816 IPC::Message* reply_message_;
817
818 DISALLOW_COPY_AND_ASSIGN(WindowDragResponseTask);
819};
820#endif // defined(OS_WIN)
821
822#if defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29823void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
824 int handle,
[email protected]d1a5941e2009-08-13 23:34:24825 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29826 int flags) {
[email protected]b410bc32009-08-14 01:11:14827 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51828 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41829 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29830 }
831}
832
initial.commit09911bf2008-07-26 23:55:29833void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
834 int handle,
835 wchar_t key,
836 int flags) {
[email protected]b410bc32009-08-14 01:11:14837 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29838 return;
839
[email protected]b410bc32009-08-14 01:11:14840 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29841 // The key event is sent to whatever window is active.
[email protected]d1a5941e2009-08-13 23:34:24842 ui_controls::SendKeyPress(window, key,
[email protected]c2dacc92008-10-16 23:51:38843 ((flags & views::Event::EF_CONTROL_DOWN) ==
844 views::Event::EF_CONTROL_DOWN),
845 ((flags & views::Event::EF_SHIFT_DOWN) ==
846 views::Event::EF_SHIFT_DOWN),
847 ((flags & views::Event::EF_ALT_DOWN) ==
848 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29849}
[email protected]fc2e0872009-08-21 22:14:41850#endif // defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29851
[email protected]71f65dd2009-02-11 19:14:56852void AutomationProvider::IsWindowActive(int handle, bool* success,
853 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29854 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17855 *is_active =
856 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56857 *success = true;
initial.commit09911bf2008-07-26 23:55:29858 } else {
[email protected]71f65dd2009-02-11 19:14:56859 *success = false;
860 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29861 }
862}
863
[email protected]71f65dd2009-02-11 19:14:56864void AutomationProvider::GetTabCount(int handle, int* tab_count) {
865 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29866
867 if (browser_tracker_->ContainsHandle(handle)) {
868 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56869 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29870 }
initial.commit09911bf2008-07-26 23:55:29871}
872
[email protected]71f65dd2009-02-11 19:14:56873void AutomationProvider::GetTab(int win_handle, int tab_index,
874 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56875 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29876 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
877 Browser* browser = browser_tracker_->GetResource(win_handle);
878 if (tab_index < browser->tab_count()) {
879 TabContents* tab_contents =
880 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57881 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29882 }
883 }
initial.commit09911bf2008-07-26 23:55:29884}
885
[email protected]71f65dd2009-02-11 19:14:56886void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
887 std::wstring* title) {
888 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29889 if (tab_tracker_->ContainsHandle(handle)) {
890 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36891 NavigationEntry* entry = tab->GetActiveEntry();
892 if (entry != NULL) {
893 *title = UTF16ToWideHack(entry->title());
894 } else {
895 *title = std::wstring();
896 }
[email protected]71f65dd2009-02-11 19:14:56897 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29898 }
initial.commit09911bf2008-07-26 23:55:29899}
900
[email protected]77bc6732009-04-20 22:01:03901void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
902 *tabstrip_index = -1; // -1 is the error code
903
904 if (tab_tracker_->ContainsHandle(handle)) {
905 NavigationController* tab = tab_tracker_->GetResource(handle);
906 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12907 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:03908 }
909}
910
initial.commit09911bf2008-07-26 23:55:29911void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
912 if (window_tracker_->ContainsHandle(handle)) {
913 window_tracker_->Remove(window_tracker_->GetResource(handle));
914 }
915}
916
917void AutomationProvider::OnChannelError() {
918 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:57919 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:29920}
921
922// TODO(brettw) change this to accept GURLs when history supports it
923void AutomationProvider::OnRedirectQueryComplete(
924 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:37925 GURL from_url,
initial.commit09911bf2008-07-26 23:55:29926 bool success,
[email protected]379c2b12009-07-01 21:50:33927 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:29928 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:56929 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:29930
[email protected]deb57402009-02-06 01:35:30931 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:29932 if (success) {
[email protected]71f65dd2009-02-11 19:14:56933 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:29934 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:30935 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:29936 } else {
[email protected]71f65dd2009-02-11 19:14:56937 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:29938 }
939
[email protected]4f3dc372009-02-24 00:10:29940 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:30941
[email protected]71f65dd2009-02-11 19:14:56942 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:17943 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:56944 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29945}
946
947bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:57948 DCHECK(channel_.get());
949 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:29950}
951
952Browser* AutomationProvider::FindAndActivateTab(
953 NavigationController* controller) {
954 int tab_index;
955 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
956 if (browser)
957 browser->SelectTabContentsAt(tab_index, true);
958
959 return browser;
960}
961
[email protected]71f65dd2009-02-11 19:14:56962void AutomationProvider::GetCookies(const GURL& url, int handle,
963 int* value_size,
964 std::string* value) {
965 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:29966 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
967 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56968 *value =
initial.commit09911bf2008-07-26 23:55:29969 tab->profile()->GetRequestContext()->cookie_store()->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:56970 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:29971 }
initial.commit09911bf2008-07-26 23:55:29972}
973
[email protected]71f65dd2009-02-11 19:14:56974void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:29975 const std::string value,
[email protected]71f65dd2009-02-11 19:14:56976 int handle,
977 int* response_value) {
978 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:29979
980 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
981 NavigationController* tab = tab_tracker_->GetResource(handle);
982 URLRequestContext* context = tab->profile()->GetRequestContext();
983 if (context->cookie_store()->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:56984 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:29985 }
initial.commit09911bf2008-07-26 23:55:29986}
987
[email protected]71f65dd2009-02-11 19:14:56988void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
989 *success = false;
initial.commit09911bf2008-07-26 23:55:29990 if (tab_tracker_->ContainsHandle(handle)) {
991 NavigationController* tab = tab_tracker_->GetResource(handle);
992 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:54993 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:56994 *success = true;
initial.commit09911bf2008-07-26 23:55:29995 }
initial.commit09911bf2008-07-26 23:55:29996}
997
[email protected]71f65dd2009-02-11 19:14:56998void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
999 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291000
1001 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561002 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341003 TabContents* tab_contents =
1004 tab_tracker_->GetResource(handle)->tab_contents();
1005 if (tab_contents->process())
1006 *process_id = tab_contents->process()->process().pid();
initial.commit09911bf2008-07-26 23:55:291007 }
initial.commit09911bf2008-07-26 23:55:291008}
1009
1010void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331011 NOTREACHED() << "This function has been deprecated. "
1012 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291013}
1014
[email protected]71f65dd2009-02-11 19:14:561015void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291016 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561017 const std::wstring& script,
1018 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291019 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341020 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1021 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571022 // Set the routing id of this message with the controller.
1023 // This routing id needs to be remembered for the reverse
1024 // communication while sending back the response of
1025 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331026 std::wstring set_automation_id;
1027 SStringPrintf(&set_automation_id,
1028 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561029 reply_message->routing_id());
1030
1031 DCHECK(reply_message_ == NULL);
1032 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291033
[email protected]57c6a652009-05-04 07:58:341034 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331035 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341036 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061037 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571038 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291039 }
1040
1041 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561042 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1043 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291044 }
1045}
1046
[email protected]71f65dd2009-02-11 19:14:561047void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1048 *visible = false;
[email protected]20e93d12008-08-28 16:31:571049
[email protected]59560e0b2009-06-04 03:30:221050 if (browser_tracker_->ContainsHandle(handle)) {
1051 Browser* browser = browser_tracker_->GetResource(handle);
1052 if (browser) {
1053 *visible = browser->window()->IsDownloadShelfVisible();
1054 }
1055 }
initial.commit09911bf2008-07-26 23:55:291056}
1057
[email protected]59560e0b2009-06-04 03:30:221058void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1059 if (browser_tracker_->ContainsHandle(handle)) {
1060 Browser* browser = browser_tracker_->GetResource(handle);
1061 if (browser) {
1062 if (visible)
1063 browser->window()->GetDownloadShelf()->Show();
1064 else
1065 browser->window()->GetDownloadShelf()->Close();
1066 }
1067 }
1068}
1069
1070
[email protected]71f65dd2009-02-11 19:14:561071void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1072 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291073 if (tab_tracker_->ContainsHandle(handle)) {
1074 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111075 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291076 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561077 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291078 }
1079 }
initial.commit09911bf2008-07-26 23:55:291080}
1081
initial.commit09911bf2008-07-26 23:55:291082void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561083 int handle, const std::wstring& find_request,
1084 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311085 NOTREACHED() << "This function has been deprecated."
1086 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561087 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311088 return;
1089}
1090
[email protected]4f999132009-03-31 18:08:401091void AutomationProvider::HandleFindRequest(
1092 int handle,
1093 const AutomationMsg_Find_Params& params,
1094 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291095 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561096 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1097 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291098 return;
1099 }
1100
1101 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111102 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291103
1104 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141105 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291106
[email protected]57c6a652009-05-04 07:58:341107 tab_contents->set_current_find_request_id(
1108 FindInPageNotificationObserver::kFindInPageRequestId);
1109 tab_contents->render_view_host()->StartFinding(
1110 FindInPageNotificationObserver::kFindInPageRequestId,
1111 params.search_string, params.forward, params.match_case,
1112 params.find_next);
initial.commit09911bf2008-07-26 23:55:291113}
1114
[email protected]5f8af2a2008-08-06 22:49:451115void AutomationProvider::HandleOpenFindInPageRequest(
1116 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291117 if (browser_tracker_->ContainsHandle(handle)) {
1118 Browser* browser = browser_tracker_->GetResource(handle);
1119 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451120 }
1121}
1122
[email protected]71f65dd2009-02-11 19:14:561123void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]9e0534b2008-10-21 15:03:011124 gfx::Point position;
[email protected]71f65dd2009-02-11 19:14:561125 *visible = false;
[email protected]4f3dc372009-02-24 00:10:291126 if (browser_tracker_->ContainsHandle(handle)) {
1127 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581128 FindBarTesting* find_bar =
1129 browser->find_bar()->find_bar()->GetFindBarTesting();
1130 find_bar->GetFindBarWindowInfo(&position, visible);
[email protected]4f3dc372009-02-24 00:10:291131 }
[email protected]20e93d12008-08-28 16:31:571132}
1133
[email protected]71f65dd2009-02-11 19:14:561134void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1135 int* y) {
[email protected]9e0534b2008-10-21 15:03:011136 gfx::Point position(0, 0);
1137 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291138 if (browser_tracker_->ContainsHandle(handle)) {
1139 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581140 FindBarTesting* find_bar =
1141 browser->find_bar()->find_bar()->GetFindBarTesting();
1142 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291143 }
[email protected]20e93d12008-08-28 16:31:571144
[email protected]71f65dd2009-02-11 19:14:561145 *x = position.x();
1146 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571147}
1148
initial.commit09911bf2008-07-26 23:55:291149void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561150 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341151 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1152 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561153 DCHECK(reply_message_ == NULL);
1154 reply_message_ = reply_message;
1155
[email protected]d9f9b792009-06-24 13:17:121156 DevToolsManager::GetInstance()->InspectElement(
1157 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291158 } else {
[email protected]71f65dd2009-02-11 19:14:561159 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1160 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291161 }
1162}
1163
1164void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091165 if (reply_message_) {
1166 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1167 num_resources);
1168 Send(reply_message_);
1169 reply_message_ = NULL;
1170 }
initial.commit09911bf2008-07-26 23:55:291171}
1172
[email protected]a7eee32f2009-05-22 18:08:171173class SetProxyConfigTask : public Task {
1174 public:
1175 explicit SetProxyConfigTask(net::ProxyService* proxy_service,
1176 const std::string& new_proxy_config)
1177 : proxy_service_(proxy_service), proxy_config_(new_proxy_config) {}
1178 virtual void Run() {
1179 // First, deserialize the JSON string. If this fails, log and bail.
1180 JSONStringValueSerializer deserializer(proxy_config_);
1181 std::string error_message;
1182 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1183 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1184 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1185 << error_message;
1186 return;
1187 }
1188
1189 scoped_ptr<DictionaryValue> dict(
1190 static_cast<DictionaryValue*>(root.release()));
1191 // Now put together a proxy configuration from the deserialized string.
1192 net::ProxyConfig pc;
1193 PopulateProxyConfig(*dict.get(), &pc);
1194
1195 DCHECK(proxy_service_);
1196 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1197 new net::ProxyConfigServiceFixed(pc));
1198 proxy_service_->ResetConfigService(proxy_config_service.release());
1199 }
1200
1201 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1202 DCHECK(pc);
1203 bool no_proxy = false;
1204 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1205 // Make no changes to the ProxyConfig.
1206 return;
1207 }
1208 bool auto_config;
1209 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
1210 pc->auto_detect = true;
1211 }
1212 std::string pac_url;
1213 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
1214 pc->pac_url = GURL(pac_url);
1215 }
1216 std::string proxy_bypass_list;
1217 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
1218 pc->ParseNoProxyList(proxy_bypass_list);
1219 }
1220 std::string proxy_server;
1221 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
1222 pc->proxy_rules.ParseFromString(proxy_server);
1223 }
1224 }
1225
1226 private:
1227 net::ProxyService* proxy_service_;
1228 std::string proxy_config_;
1229};
1230
1231
1232void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
1233 URLRequestContext* context = Profile::GetDefaultRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171234 if (!context) {
1235 FilePath user_data_dir;
1236 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1237 ProfileManager* profile_manager = g_browser_process->profile_manager();
1238 DCHECK(profile_manager);
1239 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1240 DCHECK(profile);
1241 context = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171242 }
1243 DCHECK(context);
1244 // Every URLRequestContext should have a proxy service.
1245 net::ProxyService* proxy_service = context->proxy_service();
1246 DCHECK(proxy_service);
1247
[email protected]81ae0a92009-08-06 02:16:161248 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
1249 new SetProxyConfigTask(proxy_service, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171250}
1251
[email protected]4f3dc372009-02-24 00:10:291252void AutomationProvider::GetDownloadDirectory(
1253 int handle, std::wstring* download_directory) {
initial.commit09911bf2008-07-26 23:55:291254 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291255 if (tab_tracker_->ContainsHandle(handle)) {
1256 NavigationController* tab = tab_tracker_->GetResource(handle);
1257 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1258 DCHECK(dlm);
[email protected]71f65dd2009-02-11 19:14:561259 *download_directory = dlm->download_path().ToWStringHack();
initial.commit09911bf2008-07-26 23:55:291260 }
initial.commit09911bf2008-07-26 23:55:291261}
1262
[email protected]14c0a032009-04-13 18:15:141263void AutomationProvider::OpenNewBrowserWindow(bool show,
1264 IPC::Message* reply_message) {
1265 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291266 // We may have no current browser windows open so don't rely on
1267 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]15952e462008-11-14 00:29:051268 Browser* browser = Browser::Create(profile_);
1269 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151270 if (show)
[email protected]15952e462008-11-14 00:29:051271 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291272}
1273
[email protected]71f65dd2009-02-11 19:14:561274void AutomationProvider::GetWindowForBrowser(int browser_handle,
1275 bool* success,
1276 int* handle) {
1277 *success = false;
1278 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291279
1280 if (browser_tracker_->ContainsHandle(browser_handle)) {
1281 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201282 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291283 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201284 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561285 *success = true;
initial.commit09911bf2008-07-26 23:55:291286 }
initial.commit09911bf2008-07-26 23:55:291287}
1288
[email protected]5ae5bed2009-08-21 18:52:441289#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291290void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561291 int browser_handle,
1292 bool* success,
1293 int* autocomplete_edit_handle) {
1294 *success = false;
1295 *autocomplete_edit_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]13869dd2009-05-05 00:40:061299 LocationBar* loc_bar = browser->window()->GetLocationBar();
1300 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291301 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561302 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1303 *success = true;
initial.commit09911bf2008-07-26 23:55:291304 }
initial.commit09911bf2008-07-26 23:55:291305}
[email protected]5ae5bed2009-08-21 18:52:441306#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291307
[email protected]71f65dd2009-02-11 19:14:561308void AutomationProvider::ShowInterstitialPage(int tab_handle,
1309 const std::string& html_text,
1310 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291311 if (tab_tracker_->ContainsHandle(tab_handle)) {
1312 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111313 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401314
[email protected]2e028a082009-08-19 20:32:581315 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]965524b2009-04-04 21:32:401316 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341317 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401318 GURL("about:interstitial"),
1319 html_text);
1320 interstitial->Show();
1321 return;
initial.commit09911bf2008-07-26 23:55:291322 }
[email protected]71f65dd2009-02-11 19:14:561323
[email protected]457f5cf2009-08-18 16:37:521324 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1325 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561326 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291327}
1328
[email protected]71f65dd2009-02-11 19:14:561329void AutomationProvider::HideInterstitialPage(int tab_handle,
1330 bool* success) {
1331 *success = false;
[email protected]57c6a652009-05-04 07:58:341332 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1333 if (tab_contents && tab_contents->interstitial_page()) {
1334 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561335 *success = true;
initial.commit09911bf2008-07-26 23:55:291336 }
initial.commit09911bf2008-07-26 23:55:291337}
1338
[email protected]71f65dd2009-02-11 19:14:561339void AutomationProvider::CloseTab(int tab_handle,
1340 bool wait_until_closed,
1341 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291342 if (tab_tracker_->ContainsHandle(tab_handle)) {
1343 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1344 int index;
1345 Browser* browser = Browser::GetBrowserForController(controller, &index);
1346 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141347 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111348 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451349 return;
initial.commit09911bf2008-07-26 23:55:291350 }
[email protected]de246f52009-02-25 18:25:451351
1352 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
initial.commit09911bf2008-07-26 23:55:291353}
1354
[email protected]71f65dd2009-02-11 19:14:561355void AutomationProvider::CloseBrowser(int browser_handle,
1356 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291357 if (browser_tracker_->ContainsHandle(browser_handle)) {
1358 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561359 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561360 reply_message);
[email protected]f3e99e32008-07-30 04:48:391361 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291362 } else {
1363 NOTREACHED();
1364 }
1365}
1366
[email protected]71f65dd2009-02-11 19:14:561367void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1368 if (browser_tracker_->ContainsHandle(browser_handle)) {
1369 Browser* browser = browser_tracker_->GetResource(browser_handle);
1370 browser->window()->Close();
1371 } else {
1372 NOTREACHED();
1373 }
1374}
1375
[email protected]71f65dd2009-02-11 19:14:561376void AutomationProvider::NavigateInExternalTab(
1377 int handle, const GURL& url,
1378 AutomationMsg_NavigationResponseValues* status) {
1379 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291380
1381 if (tab_tracker_->ContainsHandle(handle)) {
1382 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c0588052008-10-27 23:01:501383 tab->LoadURL(url, GURL(), PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561384 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291385 }
initial.commit09911bf2008-07-26 23:55:291386}
1387
[email protected]4150ef02009-08-19 23:14:261388void AutomationProvider::NavigateExternalTabAtIndex(
1389 int handle, int navigation_index,
1390 AutomationMsg_NavigationResponseValues* status) {
1391 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1392
1393 if (tab_tracker_->ContainsHandle(handle)) {
1394 NavigationController* tab = tab_tracker_->GetResource(handle);
1395 tab->GoToIndex(navigation_index);
1396 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1397 }
1398}
1399
[email protected]71f65dd2009-02-11 19:14:561400void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1401 IPC::Message* reply_message) {
1402 if (tab_tracker_->ContainsHandle(tab_handle)) {
1403 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1404 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141405 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561406 }
1407}
1408
[email protected]71f65dd2009-02-11 19:14:561409void AutomationProvider::GetSecurityState(int handle, bool* success,
1410 SecurityStyle* security_style,
1411 int* ssl_cert_status,
1412 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291413 if (tab_tracker_->ContainsHandle(handle)) {
1414 NavigationController* tab = tab_tracker_->GetResource(handle);
1415 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561416 *success = true;
1417 *security_style = entry->ssl().security_style();
1418 *ssl_cert_status = entry->ssl().cert_status();
1419 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291420 } else {
[email protected]71f65dd2009-02-11 19:14:561421 *success = false;
1422 *security_style = SECURITY_STYLE_UNKNOWN;
1423 *ssl_cert_status = 0;
1424 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291425 }
1426}
1427
[email protected]71f65dd2009-02-11 19:14:561428void AutomationProvider::GetPageType(int handle, bool* success,
1429 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291430 if (tab_tracker_->ContainsHandle(handle)) {
1431 NavigationController* tab = tab_tracker_->GetResource(handle);
1432 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561433 *page_type = entry->page_type();
1434 *success = true;
initial.commit09911bf2008-07-26 23:55:291435 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341436 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561437 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341438 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561439 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291440 } else {
[email protected]71f65dd2009-02-11 19:14:561441 *success = false;
1442 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291443 }
1444}
1445
[email protected]71f65dd2009-02-11 19:14:561446void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1447 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291448 if (tab_tracker_->ContainsHandle(handle)) {
1449 NavigationController* tab = tab_tracker_->GetResource(handle);
1450 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071451 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401452 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471453 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341454 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291455 if (ssl_blocking_page) {
1456 if (proceed) {
[email protected]2e028a082009-08-19 20:32:581457 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]71f65dd2009-02-11 19:14:561458 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291459 return;
1460 }
1461 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521462 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1463 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561464 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291465 return;
1466 }
1467 }
1468 }
1469 // We failed.
[email protected]457f5cf2009-08-18 16:37:521470 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1471 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561472 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291473}
initial.commit09911bf2008-07-26 23:55:291474
[email protected]71f65dd2009-02-11 19:14:561475void AutomationProvider::BringBrowserToFront(int browser_handle,
1476 bool* success) {
initial.commit09911bf2008-07-26 23:55:291477 if (browser_tracker_->ContainsHandle(browser_handle)) {
1478 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061479 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561480 *success = true;
initial.commit09911bf2008-07-26 23:55:291481 } else {
[email protected]71f65dd2009-02-11 19:14:561482 *success = false;
initial.commit09911bf2008-07-26 23:55:291483 }
1484}
1485
[email protected]71f65dd2009-02-11 19:14:561486void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1487 int message_num,
1488 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291489 if (browser_tracker_->ContainsHandle(browser_handle)) {
1490 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561491 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141492 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291493 } else {
[email protected]71f65dd2009-02-11 19:14:561494 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291495 }
1496}
1497
[email protected]71f65dd2009-02-11 19:14:561498void AutomationProvider::PrintNow(int tab_handle,
1499 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441500#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571501 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341502 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1503 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291504 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571505 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141506 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341507 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571508 return;
initial.commit09911bf2008-07-26 23:55:291509 }
[email protected]71f65dd2009-02-11 19:14:561510 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1511 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441512#else
1513 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1514 NOTIMPLEMENTED();
1515#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291516}
[email protected]d301c952009-07-13 15:02:411517
[email protected]71f65dd2009-02-11 19:14:561518void AutomationProvider::SavePage(int tab_handle,
initial.commit09911bf2008-07-26 23:55:291519 const std::wstring& file_name,
1520 const std::wstring& dir_path,
[email protected]71f65dd2009-02-11 19:14:561521 int type,
1522 bool* success) {
initial.commit09911bf2008-07-26 23:55:291523 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561524 *success = false;
initial.commit09911bf2008-07-26 23:55:291525 return;
1526 }
1527
1528 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1529 Browser* browser = FindAndActivateTab(nav);
1530 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141531 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561532 *success = false;
initial.commit09911bf2008-07-26 23:55:291533 return;
1534 }
1535
initial.commit09911bf2008-07-26 23:55:291536 SavePackage::SavePackageType save_type =
1537 static_cast<SavePackage::SavePackageType>(type);
1538 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1539 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341540 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291541
[email protected]71f65dd2009-02-11 19:14:561542 *success = true;
initial.commit09911bf2008-07-26 23:55:291543}
1544
[email protected]5ae5bed2009-08-21 18:52:441545#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471546// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561547void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1548 bool* success,
1549 std::wstring* text) {
1550 *success = false;
initial.commit09911bf2008-07-26 23:55:291551 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561552 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521553 GetText();
[email protected]71f65dd2009-02-11 19:14:561554 *success = true;
initial.commit09911bf2008-07-26 23:55:291555 }
initial.commit09911bf2008-07-26 23:55:291556}
1557
[email protected]71f65dd2009-02-11 19:14:561558void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1559 const std::wstring& text,
1560 bool* success) {
1561 *success = false;
initial.commit09911bf2008-07-26 23:55:291562 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521563 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1564 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561565 *success = true;
initial.commit09911bf2008-07-26 23:55:291566 }
initial.commit09911bf2008-07-26 23:55:291567}
1568
1569void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561570 int autocomplete_edit_handle,
1571 bool* success,
1572 std::vector<AutocompleteMatchData>* matches) {
1573 *success = false;
initial.commit09911bf2008-07-26 23:55:291574 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271575 const AutocompleteResult& result = autocomplete_edit_tracker_->
1576 GetResource(autocomplete_edit_handle)->model()->result();
1577 for (AutocompleteResult::const_iterator i = result.begin();
1578 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561579 matches->push_back(AutocompleteMatchData(*i));
1580 *success = true;
initial.commit09911bf2008-07-26 23:55:291581 }
initial.commit09911bf2008-07-26 23:55:291582}
1583
1584void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561585 int autocomplete_edit_handle,
1586 bool* success,
1587 bool* query_in_progress) {
1588 *success = false;
1589 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291590 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561591 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521592 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561593 *success = true;
initial.commit09911bf2008-07-26 23:55:291594 }
initial.commit09911bf2008-07-26 23:55:291595}
1596
[email protected]28790922009-03-09 19:48:371597void AutomationProvider::OnMessageFromExternalHost(int handle,
1598 const std::string& message,
1599 const std::string& origin,
1600 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191601 RenderViewHost* view_host = GetViewForTab(handle);
1602 if (!view_host) {
1603 return;
[email protected]fa83e762008-08-15 21:41:391604 }
[email protected]f7a68432009-07-29 23:18:191605
1606 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1607 view_host, message, origin, target)) {
1608 // Message was diverted.
1609 return;
1610 }
1611
1612 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1613 origin, target, this, view_host, handle)) {
1614 // Message was diverted.
1615 return;
1616 }
1617
1618 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1619 // Message was diverted.
1620 return;
1621 }
1622
1623 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391624}
[email protected]a9024892009-06-16 23:13:551625
1626bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1627 const std::string& message, const std::string& origin,
1628 const std::string& target) {
1629 if (target !=
1630 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1631 return false;
1632
1633 if (origin != extension_automation_constants::kAutomationOrigin) {
1634 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1635 return false;
1636 }
1637
1638 // The message is a JSON-encoded array with two elements, both strings. The
1639 // first is the name of the event to dispatch. The second is a JSON-encoding
1640 // of the arguments specific to that event.
1641 scoped_ptr<Value> message_value(JSONReader::Read(message, false));
1642 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1643 LOG(WARNING) << "Invalid browser event specified through automation";
1644 return false;
1645 }
1646
1647 const ListValue* args = static_cast<const ListValue*>(message_value.get());
1648
1649 std::string event_name;
1650 if (!args->GetString(0, &event_name)) {
1651 LOG(WARNING) << "No browser event name specified through automation";
1652 return false;
1653 }
1654
1655 std::string json_args;
1656 if (!args->GetString(1, &json_args)) {
1657 LOG(WARNING) << "No browser event args specified through automation";
1658 return false;
1659 }
1660
[email protected]7120f132009-07-20 21:05:371661 if (profile()->GetExtensionMessageService()) {
1662 profile()->GetExtensionMessageService()->
1663 DispatchEventToRenderers(event_name.c_str(), json_args);
1664 }
[email protected]a9024892009-06-16 23:13:551665
1666 return true;
1667}
[email protected]5ae5bed2009-08-21 18:52:441668#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:391669
[email protected]57c6a652009-05-04 07:58:341670TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:571671 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:571672 if (tab_tracker_->ContainsHandle(handle)) {
1673 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:341674 if (tab)
1675 *tab = nav_controller;
1676 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:571677 }
[email protected]57c6a652009-05-04 07:58:341678 return NULL;
[email protected]20e93d12008-08-28 16:31:571679}
1680
initial.commit09911bf2008-07-26 23:55:291681TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
1682 : AutomationProvider(profile) {
1683 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:141684 registrar_.Add(this, NotificationType::SESSION_END,
1685 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:291686}
1687
1688TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:291689 BrowserList::RemoveObserver(this);
1690}
1691
1692void TestingAutomationProvider::OnChannelError() {
1693 BrowserList::CloseAllBrowsers(true);
1694 AutomationProvider::OnChannelError();
1695}
1696
1697void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
1698 // For backwards compatibility with the testing automation interface, we
1699 // want the automation provider (and hence the process) to go away when the
1700 // last browser goes away.
1701 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:291702 // If you change this, update Observer for NotificationType::SESSION_END
1703 // below.
[email protected]295039bd2008-08-15 04:32:571704 MessageLoop::current()->PostTask(FROM_HERE,
1705 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:291706 }
1707}
1708
1709void TestingAutomationProvider::Observe(NotificationType type,
1710 const NotificationSource& source,
1711 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561712 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:291713 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
1714 // before the task runs resulting in this object not being deleted. This
1715 // Release balance out the Release scheduled by OnBrowserRemoving.
1716 Release();
1717}
[email protected]295039bd2008-08-15 04:32:571718
1719void TestingAutomationProvider::OnRemoveProvider() {
1720 AutomationProviderList::GetInstance()->RemoveProvider(this);
1721}
[email protected]8a3422c92008-09-24 17:42:421722
[email protected]71f65dd2009-02-11 19:14:561723void AutomationProvider::GetSSLInfoBarCount(int handle, int* count) {
1724 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:421725 if (tab_tracker_->ContainsHandle(handle)) {
1726 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:341727 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:111728 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421729 }
[email protected]8a3422c92008-09-24 17:42:421730}
1731
[email protected]71f65dd2009-02-11 19:14:561732void AutomationProvider::ClickSSLInfoBarLink(int handle,
[email protected]8a3422c92008-09-24 17:42:421733 int info_bar_index,
[email protected]71f65dd2009-02-11 19:14:561734 bool wait_for_navigation,
1735 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421736 bool success = false;
1737 if (tab_tracker_->ContainsHandle(handle)) {
1738 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1739 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:111740 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421741 if (info_bar_index >= 0 && info_bar_index < count) {
1742 if (wait_for_navigation) {
[email protected]2e028a082009-08-19 20:32:581743 AddNavigationStatusListener(nav_controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421744 }
[email protected]eb9ba192008-12-02 02:41:341745 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:111746 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:341747 info_bar_index);
1748 if (delegate->AsConfirmInfoBarDelegate())
1749 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:421750 success = true;
1751 }
1752 }
[email protected]4f3dc372009-02-24 00:10:291753 }
[email protected]8a3422c92008-09-24 17:42:421754 if (!wait_for_navigation || !success)
[email protected]457f5cf2009-08-18 16:37:521755 AutomationMsg_ClickSSLInfoBarLink::WriteReplyParams(
1756 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:421757}
1758
[email protected]71f65dd2009-02-11 19:14:561759void AutomationProvider::GetLastNavigationTime(int handle,
1760 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:421761 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:561762 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:421763}
1764
[email protected]71f65dd2009-02-11 19:14:561765void AutomationProvider::WaitForNavigation(int handle,
1766 int64 last_navigation_time,
1767 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421768 NavigationController* controller = NULL;
1769 if (tab_tracker_->ContainsHandle(handle))
1770 controller = tab_tracker_->GetResource(handle);
1771
1772 Time time = tab_tracker_->GetLastNavigationTime(handle);
1773 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:561774 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:521775 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
1776 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]4f3dc372009-02-24 00:10:291777 return;
[email protected]8a3422c92008-09-24 17:42:421778 }
1779
[email protected]2e028a082009-08-19 20:32:581780 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421781}
1782
[email protected]71f65dd2009-02-11 19:14:561783void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161784 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561785 int value,
1786 bool* success) {
1787 *success = false;
[email protected]8a3422c92008-09-24 17:42:421788 if (browser_tracker_->ContainsHandle(handle)) {
1789 Browser* browser = browser_tracker_->GetResource(handle);
1790 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561791 *success = true;
[email protected]8a3422c92008-09-24 17:42:421792 }
[email protected]8a3422c92008-09-24 17:42:421793}
[email protected]97fa6ce32008-12-19 01:48:161794
[email protected]71f65dd2009-02-11 19:14:561795void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161796 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561797 const std::wstring& value,
1798 bool* success) {
1799 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161800 if (browser_tracker_->ContainsHandle(handle)) {
1801 Browser* browser = browser_tracker_->GetResource(handle);
1802 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561803 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161804 }
[email protected]97fa6ce32008-12-19 01:48:161805}
1806
[email protected]71f65dd2009-02-11 19:14:561807void AutomationProvider::GetBooleanPreference(int handle,
1808 const std::wstring& name,
1809 bool* success,
1810 bool* value) {
1811 *success = false;
1812 *value = false;
[email protected]97fa6ce32008-12-19 01:48:161813 if (browser_tracker_->ContainsHandle(handle)) {
1814 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561815 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
1816 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161817 }
[email protected]97fa6ce32008-12-19 01:48:161818}
1819
[email protected]71f65dd2009-02-11 19:14:561820void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161821 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561822 bool value,
1823 bool* success) {
1824 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161825 if (browser_tracker_->ContainsHandle(handle)) {
1826 Browser* browser = browser_tracker_->GetResource(handle);
1827 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561828 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161829 }
[email protected]97fa6ce32008-12-19 01:48:161830}
1831
1832// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561833void AutomationProvider::GetPageCurrentEncoding(
1834 int tab_handle, std::wstring* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:161835 if (tab_tracker_->ContainsHandle(tab_handle)) {
1836 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1837 Browser* browser = FindAndActivateTab(nav);
1838 DCHECK(browser);
1839
[email protected]57c6a652009-05-04 07:58:341840 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
1841 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:161842 }
[email protected]97fa6ce32008-12-19 01:48:161843}
1844
1845// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561846void AutomationProvider::OverrideEncoding(int tab_handle,
1847 const std::wstring& encoding_name,
1848 bool* success) {
1849 *success = false;
[email protected]de246f52009-02-25 18:25:451850#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:161851 if (tab_tracker_->ContainsHandle(tab_handle)) {
1852 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1853 Browser* browser = FindAndActivateTab(nav);
1854 DCHECK(browser);
1855
[email protected]1fc025202009-01-20 23:03:141856 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]7f0005a2009-04-15 03:25:111857 TabContents* tab_contents = nav->tab_contents();
[email protected]97fa6ce32008-12-19 01:48:161858 int selected_encoding_id =
1859 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
1860 if (selected_encoding_id) {
1861 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:561862 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161863 }
1864 }
1865 }
[email protected]de246f52009-02-25 18:25:451866#else
1867 // TODO(port): Enable when encoding-related parts of Browser are ported.
1868 NOTIMPLEMENTED();
1869#endif
[email protected]97fa6ce32008-12-19 01:48:161870}
[email protected]5bcdb312009-01-07 21:43:201871
[email protected]4d434a1a2009-02-11 21:06:571872void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:201873 SavePackage::SetShouldPromptUser(should_prompt);
1874}
[email protected]87eab222009-03-13 00:47:451875
[email protected]b83e4602009-05-15 22:58:331876void AutomationProvider::SetEnableExtensionAutomation(bool automation_enabled) {
1877 AutomationExtensionFunction::SetEnabled(automation_enabled);
1878}
1879
[email protected]3753f522009-04-14 23:15:471880void AutomationProvider::GetWindowTitle(int handle, string16* text) {
1881 gfx::NativeWindow window = window_tracker_->GetResource(handle);
1882 text->assign(platform_util::GetWindowTitle(window));
1883}
[email protected]66ba4932009-06-04 19:22:131884
1885void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
1886 *count = -1; // -1 is the error code
1887 if (tab_tracker_->ContainsHandle(handle)) {
1888 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1889 TabContents* tab_contents = nav_controller->tab_contents();
1890 if (tab_contents) {
1891 BlockedPopupContainer* container =
1892 tab_contents->blocked_popup_container();
1893 if (container) {
1894 *count = static_cast<int>(container->GetBlockedPopupCount());
1895 } else {
1896 // If we don't have a container, we don't have any blocked popups to
1897 // contain!
1898 *count = 0;
1899 }
1900 }
1901 }
1902}
[email protected]f7a68432009-07-29 23:18:191903
1904void AutomationProvider::SelectAll(int tab_handle) {
1905 RenderViewHost* view = GetViewForTab(tab_handle);
1906 if (!view) {
1907 NOTREACHED();
1908 return;
1909 }
1910
1911 view->SelectAll();
1912}
1913
1914void AutomationProvider::Cut(int tab_handle) {
1915 RenderViewHost* view = GetViewForTab(tab_handle);
1916 if (!view) {
1917 NOTREACHED();
1918 return;
1919 }
1920
1921 view->Cut();
1922}
1923
1924void AutomationProvider::Copy(int tab_handle) {
1925 RenderViewHost* view = GetViewForTab(tab_handle);
1926 if (!view) {
1927 NOTREACHED();
1928 return;
1929 }
1930
1931 view->Copy();
1932}
1933
1934void AutomationProvider::Paste(int tab_handle) {
1935 RenderViewHost* view = GetViewForTab(tab_handle);
1936 if (!view) {
1937 NOTREACHED();
1938 return;
1939 }
1940
1941 view->Paste();
1942}
1943
1944void AutomationProvider::ReloadAsync(int tab_handle) {
1945 if (tab_tracker_->ContainsHandle(tab_handle)) {
1946 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1947 if (!tab) {
1948 NOTREACHED();
1949 return;
1950 }
1951
1952 tab->Reload(false);
1953 }
1954}
1955
1956void AutomationProvider::StopAsync(int tab_handle) {
1957 RenderViewHost* view = GetViewForTab(tab_handle);
1958 if (!view) {
1959 NOTREACHED();
1960 return;
1961 }
1962
1963 view->Stop();
1964}
1965
[email protected]2949e90d2009-08-21 15:32:521966void AutomationProvider::WaitForBrowserWindowCountToBecome(
1967 int target_count, IPC::Message* reply_message) {
1968 if (static_cast<int>(BrowserList::size()) == target_count) {
1969 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
1970 reply_message, true);
1971 Send(reply_message);
1972 return;
1973 }
1974
1975 // Set up an observer (it will delete itself).
1976 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
1977}
1978
1979void AutomationProvider::WaitForAppModalDialogToBeShown(
1980 IPC::Message* reply_message) {
1981 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
1982 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
1983 reply_message, true);
1984 Send(reply_message);
1985 return;
1986 }
1987
1988 // Set up an observer (it will delete itself).
1989 new AppModalDialogShownObserver(this, reply_message);
1990}
1991
[email protected]1126a1d32009-08-26 15:39:261992void AutomationProvider::GoBackBlockUntilNavigationsComplete(
1993 int handle, int number_of_navigations, IPC::Message* reply_message) {
1994 if (tab_tracker_->ContainsHandle(handle)) {
1995 NavigationController* tab = tab_tracker_->GetResource(handle);
1996 Browser* browser = FindAndActivateTab(tab);
1997 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
1998 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
1999 browser->GoBack(CURRENT_TAB);
2000 return;
2001 }
2002 }
2003
2004 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2005 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2006 Send(reply_message);
2007}
2008
2009void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2010 int handle, int number_of_navigations, IPC::Message* reply_message) {
2011 if (tab_tracker_->ContainsHandle(handle)) {
2012 NavigationController* tab = tab_tracker_->GetResource(handle);
2013 Browser* browser = FindAndActivateTab(tab);
2014 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
2015 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2016 browser->GoForward(CURRENT_TAB);
2017 return;
2018 }
2019 }
2020
2021 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2022 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2023 Send(reply_message);
2024}
2025
[email protected]f7a68432009-07-29 23:18:192026RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2027 if (tab_tracker_->ContainsHandle(tab_handle)) {
2028 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2029 if (!tab) {
2030 NOTREACHED();
2031 return NULL;
2032 }
2033
2034 TabContents* tab_contents = tab->tab_contents();
2035 if (!tab_contents) {
2036 NOTREACHED();
2037 return NULL;
2038 }
2039
2040 RenderViewHost* view_host = tab_contents->render_view_host();
2041 return view_host;
2042 }
2043
2044 return NULL;
2045}
[email protected]675595f2009-08-26 22:32:042046
2047void AutomationProvider::GetBrowserForWindow(int window_handle,
2048 bool* success,
2049 int* browser_handle) {
2050 *success = false;
2051 *browser_handle = 0;
2052
2053 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2054 if (!window)
2055 return;
2056
2057 BrowserList::const_iterator iter = BrowserList::begin();
2058 for (;iter != BrowserList::end(); ++iter) {
2059 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2060 if (window == this_window) {
2061 // Add() returns the existing handle for the resource if any.
2062 *browser_handle = browser_tracker_->Add(*iter);
2063 *success = true;
2064 return;
2065 }
2066 }
2067}