blob: cdb7b6d83bd49f121f8229c632199e7c770efa35 [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]8dd404bb2009-09-22 19:57:24285 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44286 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33287 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44288 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33289 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24290 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14291 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24292 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[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]8dd404bb2009-09-22 19:57:24299#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]71f65dd2009-02-11 19:14:56300 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
301 WindowSimulateDrag)
[email protected]8dd404bb2009-09-22 19:57:24302#endif // defined(OS_WIN) || defined(OS_LINUX)
[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]5a9708432009-09-23 22:15:04350 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)
[email protected]632fbb12009-09-06 15:27:14428#if defined(OS_WIN)
429 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
430#endif
initial.commit09911bf2008-07-26 23:55:29431 IPC_END_MESSAGE_MAP()
432}
433
[email protected]71f65dd2009-02-11 19:14:56434void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
435 *status = -1;
initial.commit09911bf2008-07-26 23:55:29436 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
437 Browser* browser = browser_tracker_->GetResource(handle);
438 if (at_index >= 0 && at_index < browser->tab_count()) {
439 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56440 *status = 0;
initial.commit09911bf2008-07-26 23:55:29441 }
442 }
initial.commit09911bf2008-07-26 23:55:29443}
444
[email protected]71f65dd2009-02-11 19:14:56445void AutomationProvider::AppendTab(int handle, const GURL& url,
446 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29447 int append_tab_response = -1; // -1 is the error code
448 NotificationObserver* observer = NULL;
449
450 if (browser_tracker_->ContainsHandle(handle)) {
451 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14452 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58453 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
454 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39455 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29456 if (tab_contents) {
457 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57458 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29459 }
460 }
461
462 if (append_tab_response < 0) {
463 // The append tab failed. Remove the TabStripObserver
464 if (observer) {
465 RemoveTabStripObserver(observer);
466 delete observer;
467 }
468
[email protected]71f65dd2009-02-11 19:14:56469 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
470 append_tab_response);
471 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29472 }
473}
474
[email protected]71f65dd2009-02-11 19:14:56475void AutomationProvider::NavigateToURL(int handle, const GURL& url,
476 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58477 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
478}
479
480void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
481 int handle, const GURL& url, int number_of_navigations,
482 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29483 if (tab_tracker_->ContainsHandle(handle)) {
484 NavigationController* tab = tab_tracker_->GetResource(handle);
485
486 // Simulate what a user would do. Activate the tab and then navigate.
487 // We could allow navigating in a background tab in future.
488 Browser* browser = FindAndActivateTab(tab);
489
490 if (browser) {
[email protected]2e028a082009-08-19 20:32:58491 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
[email protected]71f65dd2009-02-11 19:14:56492
initial.commit09911bf2008-07-26 23:55:29493 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50494 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29495 return;
496 }
497 }
[email protected]71f65dd2009-02-11 19:14:56498
499 AutomationMsg_NavigateToURL::WriteReplyParams(
500 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
501 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29502}
[email protected]2949e90d2009-08-21 15:32:52503
[email protected]71f65dd2009-02-11 19:14:56504void AutomationProvider::NavigationAsync(int handle, const GURL& url,
505 bool* status) {
506 *status = false;
initial.commit09911bf2008-07-26 23:55:29507
508 if (tab_tracker_->ContainsHandle(handle)) {
509 NavigationController* tab = tab_tracker_->GetResource(handle);
510
511 // Simulate what a user would do. Activate the tab and then navigate.
512 // We could allow navigating in a background tab in future.
513 Browser* browser = FindAndActivateTab(tab);
514
515 if (browser) {
516 // Don't add any listener unless a callback mechanism is desired.
517 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50518 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56519 *status = true;
initial.commit09911bf2008-07-26 23:55:29520 }
521 }
initial.commit09911bf2008-07-26 23:55:29522}
523
[email protected]71f65dd2009-02-11 19:14:56524void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29525 if (tab_tracker_->ContainsHandle(handle)) {
526 NavigationController* tab = tab_tracker_->GetResource(handle);
527 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14528 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]2e028a082009-08-19 20:32:58529 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29530 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29531 return;
532 }
533 }
[email protected]71f65dd2009-02-11 19:14:56534
535 AutomationMsg_GoBack::WriteReplyParams(
536 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
537 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29538}
539
[email protected]71f65dd2009-02-11 19:14:56540void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29541 if (tab_tracker_->ContainsHandle(handle)) {
542 NavigationController* tab = tab_tracker_->GetResource(handle);
543 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14544 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]2e028a082009-08-19 20:32:58545 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]485fba42009-03-24 23:27:29546 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29547 return;
548 }
549 }
[email protected]71f65dd2009-02-11 19:14:56550
551 AutomationMsg_GoForward::WriteReplyParams(
552 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
553 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29554}
555
[email protected]71f65dd2009-02-11 19:14:56556void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29557 if (tab_tracker_->ContainsHandle(handle)) {
558 NavigationController* tab = tab_tracker_->GetResource(handle);
559 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14560 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]2e028a082009-08-19 20:32:58561 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29562 browser->Reload();
563 return;
564 }
565 }
[email protected]71f65dd2009-02-11 19:14:56566
567 AutomationMsg_Reload::WriteReplyParams(
568 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
569 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29570}
571
[email protected]71f65dd2009-02-11 19:14:56572void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29573 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56574 const std::wstring& password,
575 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29576 if (tab_tracker_->ContainsHandle(tab_handle)) {
577 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
578 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
579
580 if (iter != login_handler_map_.end()) {
581 // If auth is needed again after this, assume login has failed. This is
582 // not strictly correct, because a navigation can require both proxy and
583 // server auth, but it should be OK for now.
584 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58585 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29586 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52587 return;
initial.commit09911bf2008-07-26 23:55:29588 }
589 }
[email protected]de246f52009-02-25 18:25:45590
[email protected]457f5cf2009-08-18 16:37:52591 AutomationMsg_SetAuth::WriteReplyParams(
592 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
593 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29594}
595
[email protected]71f65dd2009-02-11 19:14:56596void AutomationProvider::CancelAuth(int tab_handle,
597 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29598 if (tab_tracker_->ContainsHandle(tab_handle)) {
599 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
600 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
601
602 if (iter != login_handler_map_.end()) {
603 // If auth is needed again after this, something is screwy.
604 LoginHandler* handler = iter->second;
[email protected]2e028a082009-08-19 20:32:58605 AddNavigationStatusListener(tab, reply_message, 1);
initial.commit09911bf2008-07-26 23:55:29606 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52607 return;
initial.commit09911bf2008-07-26 23:55:29608 }
609 }
[email protected]de246f52009-02-25 18:25:45610
[email protected]457f5cf2009-08-18 16:37:52611 AutomationMsg_CancelAuth::WriteReplyParams(
612 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
613 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29614}
615
[email protected]71f65dd2009-02-11 19:14:56616void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
617 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29618
619 if (tab_tracker_->ContainsHandle(tab_handle)) {
620 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
621 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
622
623 if (iter != login_handler_map_.end()) {
624 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56625 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29626 }
627 }
initial.commit09911bf2008-07-26 23:55:29628}
629
[email protected]71f65dd2009-02-11 19:14:56630void AutomationProvider::GetRedirectsFrom(int tab_handle,
631 const GURL& source_url,
632 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29633 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
634 if (tab_tracker_->ContainsHandle(tab_handle)) {
635 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
636 HistoryService* history_service =
637 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
638
639 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
640 "has no history service";
641 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56642 DCHECK(reply_message_ == NULL);
643 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29644 // Schedule a history query for redirects. The response will be sent
645 // asynchronously from the callback the history system uses to notify us
646 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29647 redirect_query_ = history_service->QueryRedirectsFrom(
648 source_url, &consumer_,
649 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
650 return; // Response will be sent when query completes.
651 }
652 }
653
654 // Send failure response.
[email protected]deb57402009-02-06 01:35:30655 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56656 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
657 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29658}
659
[email protected]71f65dd2009-02-11 19:14:56660void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
661 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29662 if (browser_tracker_->ContainsHandle(handle)) {
663 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56664 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29665 }
initial.commit09911bf2008-07-26 23:55:29666}
667
[email protected]202e7a72009-06-15 03:48:36668void AutomationProvider::GetBrowserLocale(string16* locale) {
669 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06670 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36671}
672
[email protected]71f65dd2009-02-11 19:14:56673void AutomationProvider::GetBrowserWindowCount(int* window_count) {
674 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29675}
676
[email protected]24497032009-05-01 17:00:29677void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
678 *window_count = static_cast<int>(
679 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
680}
681
[email protected]71f65dd2009-02-11 19:14:56682void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
683 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07684 AppModalDialog* dialog_delegate =
685 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50686 *showing_dialog = (dialog_delegate != NULL);
687 if (*showing_dialog)
688 *dialog_button = dialog_delegate->GetDialogButtons();
689 else
[email protected]478ff2ed2009-04-21 23:49:18690 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20691}
692
[email protected]71f65dd2009-02-11 19:14:56693void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
694 *success = false;
[email protected]fad84eab2008-12-05 00:37:20695
[email protected]1f460072009-05-28 17:02:07696 AppModalDialog* dialog_delegate =
697 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50698 if (dialog_delegate &&
699 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18700 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
701 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28702 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56703 *success = true;
[email protected]fad84eab2008-12-05 00:37:20704 }
[email protected]478ff2ed2009-04-21 23:49:18705 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
706 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56707 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28708 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56709 *success = true;
[email protected]fad84eab2008-12-05 00:37:20710 }
711 }
[email protected]c274acc2008-11-11 20:13:44712}
713
[email protected]71f65dd2009-02-11 19:14:56714void AutomationProvider::GetBrowserWindow(int index, int* handle) {
715 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29716 if (index >= 0) {
717 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29718 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29719 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56720 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29721 }
722 }
initial.commit09911bf2008-07-26 23:55:29723}
724
[email protected]24497032009-05-01 17:00:29725void AutomationProvider::FindNormalBrowserWindow(int* handle) {
726 *handle = 0;
727 Browser* browser = BrowserList::FindBrowserWithType(profile_,
728 Browser::TYPE_NORMAL);
729 if (browser)
730 *handle = browser_tracker_->Add(browser);
731}
732
[email protected]71f65dd2009-02-11 19:14:56733void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
734 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29735 Browser* browser = BrowserList::GetLastActive();
736 if (browser)
[email protected]71f65dd2009-02-11 19:14:56737 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29738}
739
[email protected]fb05865f2009-09-21 15:05:46740#if defined(OS_LINUX) || defined(OS_MACOSX)
[email protected]9a08bcf2009-08-12 19:56:28741// TODO(estade): use this implementation for all platforms?
742void AutomationProvider::GetActiveWindow(int* handle) {
743 gfx::NativeWindow window =
744 BrowserList::GetLastActive()->window()->GetNativeHandle();
745 *handle = window_tracker_->Add(window);
746}
747#endif
748
[email protected]4f6381ee2009-04-16 02:46:33749void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
750 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56751 *success = false;
[email protected]4ae62752008-08-04 23:28:47752 if (browser_tracker_->ContainsHandle(handle)) {
753 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14754 if (browser->command_updater()->SupportsCommand(command) &&
755 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47756 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56757 *success = true;
[email protected]4ae62752008-08-04 23:28:47758 }
759 }
[email protected]4ae62752008-08-04 23:28:47760}
761
[email protected]4f6381ee2009-04-16 02:46:33762void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56763 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53764 // List of commands which just finish synchronously and don't require
765 // setting up an observer.
766 static const int kSynchronousCommands[] = {
767 IDC_HOME,
768 IDC_SELECT_NEXT_TAB,
769 IDC_SELECT_PREVIOUS_TAB,
770 IDC_SHOW_DOWNLOADS,
771 IDC_SHOW_HISTORY,
772 };
[email protected]56e71b7c2009-03-27 03:05:56773 if (browser_tracker_->ContainsHandle(handle)) {
774 Browser* browser = browser_tracker_->GetResource(handle);
775 if (browser->command_updater()->SupportsCommand(command) &&
776 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53777 // First check if we can handle the command without using an observer.
778 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
779 if (command == kSynchronousCommands[i]) {
780 browser->ExecuteCommand(command);
781 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
782 true);
783 Send(reply_message);
784 return;
785 }
786 }
787
788 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42789 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
790 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27791 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42792 return;
793 }
[email protected]56e71b7c2009-03-27 03:05:56794 }
795 }
[email protected]49a14a82009-03-31 04:16:44796 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56797 Send(reply_message);
798}
799
[email protected]fc2e0872009-08-21 22:14:41800// This task just adds another task to the event queue. This is useful if
801// you want to ensure that any tasks added to the event queue after this one
802// have already been processed by the time |task| is run.
803class InvokeTaskLaterTask : public Task {
804 public:
805 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
806 virtual ~InvokeTaskLaterTask() {}
807
808 virtual void Run() {
809 MessageLoop::current()->PostTask(FROM_HERE, task_);
810 }
811
812 private:
813 Task* task_;
814
815 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
816};
817
[email protected]fc2e0872009-08-21 22:14:41818#if defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29819void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
820 int handle,
[email protected]d1a5941e2009-08-13 23:34:24821 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29822 int flags) {
[email protected]b410bc32009-08-14 01:11:14823 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51824 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41825 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29826 }
827}
828
initial.commit09911bf2008-07-26 23:55:29829void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
830 int handle,
[email protected]a4d271e2009-09-28 21:32:30831 wchar_t key,
initial.commit09911bf2008-07-26 23:55:29832 int flags) {
[email protected]b410bc32009-08-14 01:11:14833 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29834 return;
835
[email protected]b410bc32009-08-14 01:11:14836 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29837 // The key event is sent to whatever window is active.
[email protected]a4d271e2009-09-28 21:32:30838 ui_controls::SendKeyPress(window, key,
[email protected]c2dacc92008-10-16 23:51:38839 ((flags & views::Event::EF_CONTROL_DOWN) ==
840 views::Event::EF_CONTROL_DOWN),
841 ((flags & views::Event::EF_SHIFT_DOWN) ==
842 views::Event::EF_SHIFT_DOWN),
843 ((flags & views::Event::EF_ALT_DOWN) ==
844 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29845}
[email protected]fc2e0872009-08-21 22:14:41846#endif // defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:29847
[email protected]71f65dd2009-02-11 19:14:56848void AutomationProvider::IsWindowActive(int handle, bool* success,
849 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29850 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17851 *is_active =
852 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56853 *success = true;
initial.commit09911bf2008-07-26 23:55:29854 } else {
[email protected]71f65dd2009-02-11 19:14:56855 *success = false;
856 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29857 }
858}
859
[email protected]71f65dd2009-02-11 19:14:56860void AutomationProvider::GetTabCount(int handle, int* tab_count) {
861 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29862
863 if (browser_tracker_->ContainsHandle(handle)) {
864 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56865 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29866 }
initial.commit09911bf2008-07-26 23:55:29867}
868
[email protected]71f65dd2009-02-11 19:14:56869void AutomationProvider::GetTab(int win_handle, int tab_index,
870 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56871 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29872 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
873 Browser* browser = browser_tracker_->GetResource(win_handle);
874 if (tab_index < browser->tab_count()) {
875 TabContents* tab_contents =
876 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57877 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29878 }
879 }
initial.commit09911bf2008-07-26 23:55:29880}
881
[email protected]71f65dd2009-02-11 19:14:56882void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
883 std::wstring* title) {
884 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29885 if (tab_tracker_->ContainsHandle(handle)) {
886 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36887 NavigationEntry* entry = tab->GetActiveEntry();
888 if (entry != NULL) {
889 *title = UTF16ToWideHack(entry->title());
890 } else {
891 *title = std::wstring();
892 }
[email protected]71f65dd2009-02-11 19:14:56893 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29894 }
initial.commit09911bf2008-07-26 23:55:29895}
896
[email protected]77bc6732009-04-20 22:01:03897void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
898 *tabstrip_index = -1; // -1 is the error code
899
900 if (tab_tracker_->ContainsHandle(handle)) {
901 NavigationController* tab = tab_tracker_->GetResource(handle);
902 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12903 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:03904 }
905}
906
initial.commit09911bf2008-07-26 23:55:29907void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
908 if (window_tracker_->ContainsHandle(handle)) {
909 window_tracker_->Remove(window_tracker_->GetResource(handle));
910 }
911}
912
913void AutomationProvider::OnChannelError() {
914 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:57915 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:29916}
917
918// TODO(brettw) change this to accept GURLs when history supports it
919void AutomationProvider::OnRedirectQueryComplete(
920 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:37921 GURL from_url,
initial.commit09911bf2008-07-26 23:55:29922 bool success,
[email protected]379c2b12009-07-01 21:50:33923 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:29924 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:56925 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:29926
[email protected]deb57402009-02-06 01:35:30927 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:29928 if (success) {
[email protected]71f65dd2009-02-11 19:14:56929 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:29930 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:30931 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:29932 } else {
[email protected]71f65dd2009-02-11 19:14:56933 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:29934 }
935
[email protected]4f3dc372009-02-24 00:10:29936 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:30937
[email protected]71f65dd2009-02-11 19:14:56938 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:17939 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:56940 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29941}
942
943bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:57944 DCHECK(channel_.get());
945 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:29946}
947
948Browser* AutomationProvider::FindAndActivateTab(
949 NavigationController* controller) {
950 int tab_index;
951 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
952 if (browser)
953 browser->SelectTabContentsAt(tab_index, true);
954
955 return browser;
956}
957
[email protected]71f65dd2009-02-11 19:14:56958void AutomationProvider::GetCookies(const GURL& url, int handle,
959 int* value_size,
960 std::string* value) {
961 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:29962 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
963 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56964 *value =
initial.commit09911bf2008-07-26 23:55:29965 tab->profile()->GetRequestContext()->cookie_store()->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:56966 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:29967 }
initial.commit09911bf2008-07-26 23:55:29968}
969
[email protected]71f65dd2009-02-11 19:14:56970void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:29971 const std::string value,
[email protected]71f65dd2009-02-11 19:14:56972 int handle,
973 int* response_value) {
974 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:29975
976 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
977 NavigationController* tab = tab_tracker_->GetResource(handle);
978 URLRequestContext* context = tab->profile()->GetRequestContext();
979 if (context->cookie_store()->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:56980 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:29981 }
initial.commit09911bf2008-07-26 23:55:29982}
983
[email protected]71f65dd2009-02-11 19:14:56984void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
985 *success = false;
initial.commit09911bf2008-07-26 23:55:29986 if (tab_tracker_->ContainsHandle(handle)) {
987 NavigationController* tab = tab_tracker_->GetResource(handle);
988 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:54989 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:56990 *success = true;
initial.commit09911bf2008-07-26 23:55:29991 }
initial.commit09911bf2008-07-26 23:55:29992}
993
[email protected]71f65dd2009-02-11 19:14:56994void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
995 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:29996
997 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:56998 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:34999 TabContents* tab_contents =
1000 tab_tracker_->GetResource(handle)->tab_contents();
1001 if (tab_contents->process())
1002 *process_id = tab_contents->process()->process().pid();
initial.commit09911bf2008-07-26 23:55:291003 }
initial.commit09911bf2008-07-26 23:55:291004}
1005
1006void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331007 NOTREACHED() << "This function has been deprecated. "
1008 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291009}
1010
[email protected]71f65dd2009-02-11 19:14:561011void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291012 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561013 const std::wstring& script,
1014 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291015 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341016 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1017 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571018 // Set the routing id of this message with the controller.
1019 // This routing id needs to be remembered for the reverse
1020 // communication while sending back the response of
1021 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331022 std::wstring set_automation_id;
1023 SStringPrintf(&set_automation_id,
1024 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561025 reply_message->routing_id());
1026
1027 DCHECK(reply_message_ == NULL);
1028 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291029
[email protected]57c6a652009-05-04 07:58:341030 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331031 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341032 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061033 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571034 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291035 }
1036
1037 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561038 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1039 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291040 }
1041}
1042
[email protected]71f65dd2009-02-11 19:14:561043void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1044 *visible = false;
[email protected]20e93d12008-08-28 16:31:571045
[email protected]59560e0b2009-06-04 03:30:221046 if (browser_tracker_->ContainsHandle(handle)) {
1047 Browser* browser = browser_tracker_->GetResource(handle);
1048 if (browser) {
1049 *visible = browser->window()->IsDownloadShelfVisible();
1050 }
1051 }
initial.commit09911bf2008-07-26 23:55:291052}
1053
[email protected]59560e0b2009-06-04 03:30:221054void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1055 if (browser_tracker_->ContainsHandle(handle)) {
1056 Browser* browser = browser_tracker_->GetResource(handle);
1057 if (browser) {
1058 if (visible)
1059 browser->window()->GetDownloadShelf()->Show();
1060 else
1061 browser->window()->GetDownloadShelf()->Close();
1062 }
1063 }
1064}
1065
1066
[email protected]71f65dd2009-02-11 19:14:561067void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1068 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291069 if (tab_tracker_->ContainsHandle(handle)) {
1070 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111071 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291072 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561073 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291074 }
1075 }
initial.commit09911bf2008-07-26 23:55:291076}
1077
initial.commit09911bf2008-07-26 23:55:291078void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561079 int handle, const std::wstring& find_request,
1080 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311081 NOTREACHED() << "This function has been deprecated."
1082 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561083 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311084 return;
1085}
1086
[email protected]4f999132009-03-31 18:08:401087void AutomationProvider::HandleFindRequest(
1088 int handle,
1089 const AutomationMsg_Find_Params& params,
1090 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291091 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561092 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1093 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291094 return;
1095 }
1096
1097 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111098 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291099
1100 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141101 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291102
[email protected]57c6a652009-05-04 07:58:341103 tab_contents->set_current_find_request_id(
1104 FindInPageNotificationObserver::kFindInPageRequestId);
1105 tab_contents->render_view_host()->StartFinding(
1106 FindInPageNotificationObserver::kFindInPageRequestId,
1107 params.search_string, params.forward, params.match_case,
1108 params.find_next);
initial.commit09911bf2008-07-26 23:55:291109}
1110
[email protected]5f8af2a2008-08-06 22:49:451111void AutomationProvider::HandleOpenFindInPageRequest(
1112 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291113 if (browser_tracker_->ContainsHandle(handle)) {
1114 Browser* browser = browser_tracker_->GetResource(handle);
1115 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451116 }
1117}
1118
[email protected]71f65dd2009-02-11 19:14:561119void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561120 *visible = false;
[email protected]855c0142009-09-28 22:35:241121 Browser* browser = browser_tracker_->GetResource(handle);
1122 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581123 FindBarTesting* find_bar =
1124 browser->find_bar()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241125 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291126 }
[email protected]20e93d12008-08-28 16:31:571127}
1128
[email protected]71f65dd2009-02-11 19:14:561129void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1130 int* y) {
[email protected]9e0534b2008-10-21 15:03:011131 gfx::Point position(0, 0);
1132 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291133 if (browser_tracker_->ContainsHandle(handle)) {
1134 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581135 FindBarTesting* find_bar =
1136 browser->find_bar()->find_bar()->GetFindBarTesting();
1137 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291138 }
[email protected]20e93d12008-08-28 16:31:571139
[email protected]71f65dd2009-02-11 19:14:561140 *x = position.x();
1141 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571142}
1143
initial.commit09911bf2008-07-26 23:55:291144void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561145 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341146 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1147 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561148 DCHECK(reply_message_ == NULL);
1149 reply_message_ = reply_message;
1150
[email protected]d9f9b792009-06-24 13:17:121151 DevToolsManager::GetInstance()->InspectElement(
1152 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291153 } else {
[email protected]71f65dd2009-02-11 19:14:561154 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1155 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291156 }
1157}
1158
1159void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091160 if (reply_message_) {
1161 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1162 num_resources);
1163 Send(reply_message_);
1164 reply_message_ = NULL;
1165 }
initial.commit09911bf2008-07-26 23:55:291166}
1167
[email protected]a7eee32f2009-05-22 18:08:171168class SetProxyConfigTask : public Task {
1169 public:
1170 explicit SetProxyConfigTask(net::ProxyService* proxy_service,
1171 const std::string& new_proxy_config)
1172 : proxy_service_(proxy_service), proxy_config_(new_proxy_config) {}
1173 virtual void Run() {
1174 // First, deserialize the JSON string. If this fails, log and bail.
1175 JSONStringValueSerializer deserializer(proxy_config_);
1176 std::string error_message;
1177 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1178 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1179 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1180 << error_message;
1181 return;
1182 }
1183
1184 scoped_ptr<DictionaryValue> dict(
1185 static_cast<DictionaryValue*>(root.release()));
1186 // Now put together a proxy configuration from the deserialized string.
1187 net::ProxyConfig pc;
1188 PopulateProxyConfig(*dict.get(), &pc);
1189
1190 DCHECK(proxy_service_);
1191 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1192 new net::ProxyConfigServiceFixed(pc));
1193 proxy_service_->ResetConfigService(proxy_config_service.release());
1194 }
1195
1196 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1197 DCHECK(pc);
1198 bool no_proxy = false;
1199 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1200 // Make no changes to the ProxyConfig.
1201 return;
1202 }
1203 bool auto_config;
1204 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
1205 pc->auto_detect = true;
1206 }
1207 std::string pac_url;
1208 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
1209 pc->pac_url = GURL(pac_url);
1210 }
1211 std::string proxy_bypass_list;
1212 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
1213 pc->ParseNoProxyList(proxy_bypass_list);
1214 }
1215 std::string proxy_server;
1216 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
1217 pc->proxy_rules.ParseFromString(proxy_server);
1218 }
1219 }
1220
1221 private:
1222 net::ProxyService* proxy_service_;
1223 std::string proxy_config_;
1224};
1225
1226
1227void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
1228 URLRequestContext* context = Profile::GetDefaultRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171229 if (!context) {
1230 FilePath user_data_dir;
1231 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1232 ProfileManager* profile_manager = g_browser_process->profile_manager();
1233 DCHECK(profile_manager);
1234 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1235 DCHECK(profile);
1236 context = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171237 }
1238 DCHECK(context);
1239 // Every URLRequestContext should have a proxy service.
1240 net::ProxyService* proxy_service = context->proxy_service();
1241 DCHECK(proxy_service);
1242
[email protected]81ae0a92009-08-06 02:16:161243 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
1244 new SetProxyConfigTask(proxy_service, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171245}
1246
[email protected]4f3dc372009-02-24 00:10:291247void AutomationProvider::GetDownloadDirectory(
1248 int handle, std::wstring* download_directory) {
initial.commit09911bf2008-07-26 23:55:291249 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291250 if (tab_tracker_->ContainsHandle(handle)) {
1251 NavigationController* tab = tab_tracker_->GetResource(handle);
1252 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1253 DCHECK(dlm);
[email protected]71f65dd2009-02-11 19:14:561254 *download_directory = dlm->download_path().ToWStringHack();
initial.commit09911bf2008-07-26 23:55:291255 }
initial.commit09911bf2008-07-26 23:55:291256}
1257
[email protected]14c0a032009-04-13 18:15:141258void AutomationProvider::OpenNewBrowserWindow(bool show,
1259 IPC::Message* reply_message) {
1260 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291261 // We may have no current browser windows open so don't rely on
1262 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]15952e462008-11-14 00:29:051263 Browser* browser = Browser::Create(profile_);
1264 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151265 if (show)
[email protected]15952e462008-11-14 00:29:051266 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291267}
1268
[email protected]71f65dd2009-02-11 19:14:561269void AutomationProvider::GetWindowForBrowser(int browser_handle,
1270 bool* success,
1271 int* handle) {
1272 *success = false;
1273 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291274
1275 if (browser_tracker_->ContainsHandle(browser_handle)) {
1276 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201277 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291278 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201279 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561280 *success = true;
initial.commit09911bf2008-07-26 23:55:291281 }
initial.commit09911bf2008-07-26 23:55:291282}
1283
[email protected]5ae5bed2009-08-21 18:52:441284#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291285void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561286 int browser_handle,
1287 bool* success,
1288 int* autocomplete_edit_handle) {
1289 *success = false;
1290 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291291
1292 if (browser_tracker_->ContainsHandle(browser_handle)) {
1293 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061294 LocationBar* loc_bar = browser->window()->GetLocationBar();
1295 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291296 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561297 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1298 *success = true;
initial.commit09911bf2008-07-26 23:55:291299 }
initial.commit09911bf2008-07-26 23:55:291300}
[email protected]5ae5bed2009-08-21 18:52:441301#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291302
[email protected]71f65dd2009-02-11 19:14:561303void AutomationProvider::ShowInterstitialPage(int tab_handle,
1304 const std::string& html_text,
1305 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291306 if (tab_tracker_->ContainsHandle(tab_handle)) {
1307 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111308 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401309
[email protected]2e028a082009-08-19 20:32:581310 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]965524b2009-04-04 21:32:401311 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341312 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401313 GURL("about:interstitial"),
1314 html_text);
1315 interstitial->Show();
1316 return;
initial.commit09911bf2008-07-26 23:55:291317 }
[email protected]71f65dd2009-02-11 19:14:561318
[email protected]457f5cf2009-08-18 16:37:521319 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1320 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561321 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291322}
1323
[email protected]71f65dd2009-02-11 19:14:561324void AutomationProvider::HideInterstitialPage(int tab_handle,
1325 bool* success) {
1326 *success = false;
[email protected]57c6a652009-05-04 07:58:341327 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1328 if (tab_contents && tab_contents->interstitial_page()) {
1329 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561330 *success = true;
initial.commit09911bf2008-07-26 23:55:291331 }
initial.commit09911bf2008-07-26 23:55:291332}
1333
[email protected]71f65dd2009-02-11 19:14:561334void AutomationProvider::CloseTab(int tab_handle,
1335 bool wait_until_closed,
1336 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291337 if (tab_tracker_->ContainsHandle(tab_handle)) {
1338 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1339 int index;
1340 Browser* browser = Browser::GetBrowserForController(controller, &index);
1341 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141342 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111343 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451344 return;
initial.commit09911bf2008-07-26 23:55:291345 }
[email protected]de246f52009-02-25 18:25:451346
1347 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
initial.commit09911bf2008-07-26 23:55:291348}
1349
[email protected]71f65dd2009-02-11 19:14:561350void AutomationProvider::CloseBrowser(int browser_handle,
1351 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291352 if (browser_tracker_->ContainsHandle(browser_handle)) {
1353 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561354 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561355 reply_message);
[email protected]f3e99e32008-07-30 04:48:391356 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291357 } else {
1358 NOTREACHED();
1359 }
1360}
1361
[email protected]71f65dd2009-02-11 19:14:561362void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1363 if (browser_tracker_->ContainsHandle(browser_handle)) {
1364 Browser* browser = browser_tracker_->GetResource(browser_handle);
1365 browser->window()->Close();
1366 } else {
1367 NOTREACHED();
1368 }
1369}
1370
[email protected]71f65dd2009-02-11 19:14:561371void AutomationProvider::NavigateInExternalTab(
1372 int handle, const GURL& url,
1373 AutomationMsg_NavigationResponseValues* status) {
1374 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291375
1376 if (tab_tracker_->ContainsHandle(handle)) {
1377 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c0588052008-10-27 23:01:501378 tab->LoadURL(url, GURL(), PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561379 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291380 }
initial.commit09911bf2008-07-26 23:55:291381}
1382
[email protected]4150ef02009-08-19 23:14:261383void AutomationProvider::NavigateExternalTabAtIndex(
1384 int handle, int navigation_index,
1385 AutomationMsg_NavigationResponseValues* status) {
1386 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1387
1388 if (tab_tracker_->ContainsHandle(handle)) {
1389 NavigationController* tab = tab_tracker_->GetResource(handle);
1390 tab->GoToIndex(navigation_index);
1391 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1392 }
1393}
1394
[email protected]71f65dd2009-02-11 19:14:561395void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1396 IPC::Message* reply_message) {
1397 if (tab_tracker_->ContainsHandle(tab_handle)) {
1398 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1399 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141400 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561401 }
1402}
1403
[email protected]71f65dd2009-02-11 19:14:561404void AutomationProvider::GetSecurityState(int handle, bool* success,
1405 SecurityStyle* security_style,
1406 int* ssl_cert_status,
1407 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291408 if (tab_tracker_->ContainsHandle(handle)) {
1409 NavigationController* tab = tab_tracker_->GetResource(handle);
1410 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561411 *success = true;
1412 *security_style = entry->ssl().security_style();
1413 *ssl_cert_status = entry->ssl().cert_status();
1414 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291415 } else {
[email protected]71f65dd2009-02-11 19:14:561416 *success = false;
1417 *security_style = SECURITY_STYLE_UNKNOWN;
1418 *ssl_cert_status = 0;
1419 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291420 }
1421}
1422
[email protected]71f65dd2009-02-11 19:14:561423void AutomationProvider::GetPageType(int handle, bool* success,
1424 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291425 if (tab_tracker_->ContainsHandle(handle)) {
1426 NavigationController* tab = tab_tracker_->GetResource(handle);
1427 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561428 *page_type = entry->page_type();
1429 *success = true;
initial.commit09911bf2008-07-26 23:55:291430 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341431 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561432 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341433 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561434 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291435 } else {
[email protected]71f65dd2009-02-11 19:14:561436 *success = false;
1437 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291438 }
1439}
1440
[email protected]71f65dd2009-02-11 19:14:561441void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1442 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291443 if (tab_tracker_->ContainsHandle(handle)) {
1444 NavigationController* tab = tab_tracker_->GetResource(handle);
1445 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071446 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401447 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471448 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341449 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291450 if (ssl_blocking_page) {
1451 if (proceed) {
[email protected]2e028a082009-08-19 20:32:581452 AddNavigationStatusListener(tab, reply_message, 1);
[email protected]71f65dd2009-02-11 19:14:561453 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291454 return;
1455 }
1456 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521457 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1458 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561459 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291460 return;
1461 }
1462 }
1463 }
1464 // We failed.
[email protected]457f5cf2009-08-18 16:37:521465 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1466 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561467 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291468}
initial.commit09911bf2008-07-26 23:55:291469
[email protected]71f65dd2009-02-11 19:14:561470void AutomationProvider::BringBrowserToFront(int browser_handle,
1471 bool* success) {
initial.commit09911bf2008-07-26 23:55:291472 if (browser_tracker_->ContainsHandle(browser_handle)) {
1473 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061474 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561475 *success = true;
initial.commit09911bf2008-07-26 23:55:291476 } else {
[email protected]71f65dd2009-02-11 19:14:561477 *success = false;
initial.commit09911bf2008-07-26 23:55:291478 }
1479}
1480
[email protected]71f65dd2009-02-11 19:14:561481void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1482 int message_num,
1483 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291484 if (browser_tracker_->ContainsHandle(browser_handle)) {
1485 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561486 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141487 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291488 } else {
[email protected]71f65dd2009-02-11 19:14:561489 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291490 }
1491}
1492
[email protected]71f65dd2009-02-11 19:14:561493void AutomationProvider::PrintNow(int tab_handle,
1494 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441495#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571496 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341497 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1498 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291499 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571500 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141501 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341502 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571503 return;
initial.commit09911bf2008-07-26 23:55:291504 }
[email protected]71f65dd2009-02-11 19:14:561505 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1506 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441507#else
1508 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1509 NOTIMPLEMENTED();
1510#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291511}
[email protected]d301c952009-07-13 15:02:411512
[email protected]71f65dd2009-02-11 19:14:561513void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111514 const FilePath& file_name,
1515 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561516 int type,
1517 bool* success) {
initial.commit09911bf2008-07-26 23:55:291518 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561519 *success = false;
initial.commit09911bf2008-07-26 23:55:291520 return;
1521 }
1522
1523 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1524 Browser* browser = FindAndActivateTab(nav);
1525 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141526 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561527 *success = false;
initial.commit09911bf2008-07-26 23:55:291528 return;
1529 }
1530
initial.commit09911bf2008-07-26 23:55:291531 SavePackage::SavePackageType save_type =
1532 static_cast<SavePackage::SavePackageType>(type);
1533 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1534 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341535 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291536
[email protected]71f65dd2009-02-11 19:14:561537 *success = true;
initial.commit09911bf2008-07-26 23:55:291538}
1539
[email protected]5ae5bed2009-08-21 18:52:441540#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471541// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561542void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1543 bool* success,
1544 std::wstring* text) {
1545 *success = false;
initial.commit09911bf2008-07-26 23:55:291546 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561547 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521548 GetText();
[email protected]71f65dd2009-02-11 19:14:561549 *success = true;
initial.commit09911bf2008-07-26 23:55:291550 }
initial.commit09911bf2008-07-26 23:55:291551}
1552
[email protected]71f65dd2009-02-11 19:14:561553void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1554 const std::wstring& text,
1555 bool* success) {
1556 *success = false;
initial.commit09911bf2008-07-26 23:55:291557 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521558 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1559 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561560 *success = true;
initial.commit09911bf2008-07-26 23:55:291561 }
initial.commit09911bf2008-07-26 23:55:291562}
1563
1564void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561565 int autocomplete_edit_handle,
1566 bool* success,
1567 std::vector<AutocompleteMatchData>* matches) {
1568 *success = false;
initial.commit09911bf2008-07-26 23:55:291569 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271570 const AutocompleteResult& result = autocomplete_edit_tracker_->
1571 GetResource(autocomplete_edit_handle)->model()->result();
1572 for (AutocompleteResult::const_iterator i = result.begin();
1573 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561574 matches->push_back(AutocompleteMatchData(*i));
1575 *success = true;
initial.commit09911bf2008-07-26 23:55:291576 }
initial.commit09911bf2008-07-26 23:55:291577}
1578
1579void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561580 int autocomplete_edit_handle,
1581 bool* success,
1582 bool* query_in_progress) {
1583 *success = false;
1584 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291585 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561586 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521587 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561588 *success = true;
initial.commit09911bf2008-07-26 23:55:291589 }
initial.commit09911bf2008-07-26 23:55:291590}
1591
[email protected]28790922009-03-09 19:48:371592void AutomationProvider::OnMessageFromExternalHost(int handle,
1593 const std::string& message,
1594 const std::string& origin,
1595 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191596 RenderViewHost* view_host = GetViewForTab(handle);
1597 if (!view_host) {
1598 return;
[email protected]fa83e762008-08-15 21:41:391599 }
[email protected]f7a68432009-07-29 23:18:191600
1601 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1602 view_host, message, origin, target)) {
1603 // Message was diverted.
1604 return;
1605 }
1606
1607 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1608 origin, target, this, view_host, handle)) {
1609 // Message was diverted.
1610 return;
1611 }
1612
1613 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1614 // Message was diverted.
1615 return;
1616 }
1617
1618 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391619}
[email protected]a9024892009-06-16 23:13:551620
1621bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1622 const std::string& message, const std::string& origin,
1623 const std::string& target) {
1624 if (target !=
1625 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1626 return false;
1627
1628 if (origin != extension_automation_constants::kAutomationOrigin) {
1629 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1630 return false;
1631 }
1632
1633 // The message is a JSON-encoded array with two elements, both strings. The
1634 // first is the name of the event to dispatch. The second is a JSON-encoding
1635 // of the arguments specific to that event.
1636 scoped_ptr<Value> message_value(JSONReader::Read(message, false));
1637 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1638 LOG(WARNING) << "Invalid browser event specified through automation";
1639 return false;
1640 }
1641
1642 const ListValue* args = static_cast<const ListValue*>(message_value.get());
1643
1644 std::string event_name;
1645 if (!args->GetString(0, &event_name)) {
1646 LOG(WARNING) << "No browser event name specified through automation";
1647 return false;
1648 }
1649
1650 std::string json_args;
1651 if (!args->GetString(1, &json_args)) {
1652 LOG(WARNING) << "No browser event args specified through automation";
1653 return false;
1654 }
1655
[email protected]7120f132009-07-20 21:05:371656 if (profile()->GetExtensionMessageService()) {
1657 profile()->GetExtensionMessageService()->
1658 DispatchEventToRenderers(event_name.c_str(), json_args);
1659 }
[email protected]a9024892009-06-16 23:13:551660
1661 return true;
1662}
[email protected]5ae5bed2009-08-21 18:52:441663#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:391664
[email protected]57c6a652009-05-04 07:58:341665TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:571666 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:571667 if (tab_tracker_->ContainsHandle(handle)) {
1668 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:341669 if (tab)
1670 *tab = nav_controller;
1671 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:571672 }
[email protected]57c6a652009-05-04 07:58:341673 return NULL;
[email protected]20e93d12008-08-28 16:31:571674}
1675
initial.commit09911bf2008-07-26 23:55:291676TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
1677 : AutomationProvider(profile) {
1678 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:141679 registrar_.Add(this, NotificationType::SESSION_END,
1680 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:291681}
1682
1683TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:291684 BrowserList::RemoveObserver(this);
1685}
1686
1687void TestingAutomationProvider::OnChannelError() {
1688 BrowserList::CloseAllBrowsers(true);
1689 AutomationProvider::OnChannelError();
1690}
1691
1692void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
1693 // For backwards compatibility with the testing automation interface, we
1694 // want the automation provider (and hence the process) to go away when the
1695 // last browser goes away.
1696 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:291697 // If you change this, update Observer for NotificationType::SESSION_END
1698 // below.
[email protected]295039bd2008-08-15 04:32:571699 MessageLoop::current()->PostTask(FROM_HERE,
1700 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:291701 }
1702}
1703
1704void TestingAutomationProvider::Observe(NotificationType type,
1705 const NotificationSource& source,
1706 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:561707 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:291708 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
1709 // before the task runs resulting in this object not being deleted. This
1710 // Release balance out the Release scheduled by OnBrowserRemoving.
1711 Release();
1712}
[email protected]295039bd2008-08-15 04:32:571713
1714void TestingAutomationProvider::OnRemoveProvider() {
1715 AutomationProviderList::GetInstance()->RemoveProvider(this);
1716}
[email protected]8a3422c92008-09-24 17:42:421717
[email protected]71f65dd2009-02-11 19:14:561718void AutomationProvider::GetSSLInfoBarCount(int handle, int* count) {
1719 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:421720 if (tab_tracker_->ContainsHandle(handle)) {
1721 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:341722 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:111723 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421724 }
[email protected]8a3422c92008-09-24 17:42:421725}
1726
[email protected]71f65dd2009-02-11 19:14:561727void AutomationProvider::ClickSSLInfoBarLink(int handle,
[email protected]8a3422c92008-09-24 17:42:421728 int info_bar_index,
[email protected]71f65dd2009-02-11 19:14:561729 bool wait_for_navigation,
1730 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:421731 bool success = false;
1732 if (tab_tracker_->ContainsHandle(handle)) {
1733 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1734 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:111735 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:421736 if (info_bar_index >= 0 && info_bar_index < count) {
1737 if (wait_for_navigation) {
[email protected]2e028a082009-08-19 20:32:581738 AddNavigationStatusListener(nav_controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421739 }
[email protected]eb9ba192008-12-02 02:41:341740 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:111741 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:341742 info_bar_index);
1743 if (delegate->AsConfirmInfoBarDelegate())
1744 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:421745 success = true;
1746 }
1747 }
[email protected]4f3dc372009-02-24 00:10:291748 }
[email protected]8a3422c92008-09-24 17:42:421749 if (!wait_for_navigation || !success)
[email protected]457f5cf2009-08-18 16:37:521750 AutomationMsg_ClickSSLInfoBarLink::WriteReplyParams(
1751 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:421752}
1753
[email protected]71f65dd2009-02-11 19:14:561754void AutomationProvider::GetLastNavigationTime(int handle,
1755 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:421756 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:561757 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:421758}
1759
[email protected]71f65dd2009-02-11 19:14:561760void AutomationProvider::WaitForNavigation(int handle,
1761 int64 last_navigation_time,
1762 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:251763 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:421764 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:251765
[email protected]8a3422c92008-09-24 17:42:421766 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:561767 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:521768 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
1769 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]4f3dc372009-02-24 00:10:291770 return;
[email protected]8a3422c92008-09-24 17:42:421771 }
1772
[email protected]2e028a082009-08-19 20:32:581773 AddNavigationStatusListener(controller, reply_message, 1);
[email protected]8a3422c92008-09-24 17:42:421774}
1775
[email protected]71f65dd2009-02-11 19:14:561776void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161777 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561778 int value,
1779 bool* success) {
1780 *success = false;
[email protected]8a3422c92008-09-24 17:42:421781 if (browser_tracker_->ContainsHandle(handle)) {
1782 Browser* browser = browser_tracker_->GetResource(handle);
1783 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561784 *success = true;
[email protected]8a3422c92008-09-24 17:42:421785 }
[email protected]8a3422c92008-09-24 17:42:421786}
[email protected]97fa6ce32008-12-19 01:48:161787
[email protected]71f65dd2009-02-11 19:14:561788void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161789 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561790 const std::wstring& value,
1791 bool* success) {
1792 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161793 if (browser_tracker_->ContainsHandle(handle)) {
1794 Browser* browser = browser_tracker_->GetResource(handle);
1795 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561796 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161797 }
[email protected]97fa6ce32008-12-19 01:48:161798}
1799
[email protected]71f65dd2009-02-11 19:14:561800void AutomationProvider::GetBooleanPreference(int handle,
1801 const std::wstring& name,
1802 bool* success,
1803 bool* value) {
1804 *success = false;
1805 *value = false;
[email protected]97fa6ce32008-12-19 01:48:161806 if (browser_tracker_->ContainsHandle(handle)) {
1807 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561808 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
1809 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161810 }
[email protected]97fa6ce32008-12-19 01:48:161811}
1812
[email protected]71f65dd2009-02-11 19:14:561813void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:161814 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:561815 bool value,
1816 bool* success) {
1817 *success = false;
[email protected]97fa6ce32008-12-19 01:48:161818 if (browser_tracker_->ContainsHandle(handle)) {
1819 Browser* browser = browser_tracker_->GetResource(handle);
1820 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:561821 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161822 }
[email protected]97fa6ce32008-12-19 01:48:161823}
1824
1825// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561826void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:401827 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:161828 if (tab_tracker_->ContainsHandle(tab_handle)) {
1829 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1830 Browser* browser = FindAndActivateTab(nav);
1831 DCHECK(browser);
1832
[email protected]57c6a652009-05-04 07:58:341833 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
1834 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:161835 }
[email protected]97fa6ce32008-12-19 01:48:161836}
1837
1838// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:561839void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:401840 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:561841 bool* success) {
1842 *success = false;
[email protected]de246f52009-02-25 18:25:451843#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:161844 if (tab_tracker_->ContainsHandle(tab_handle)) {
1845 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1846 Browser* browser = FindAndActivateTab(nav);
1847 DCHECK(browser);
1848
[email protected]1fc025202009-01-20 23:03:141849 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:161850 int selected_encoding_id =
1851 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
1852 if (selected_encoding_id) {
1853 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:561854 *success = true;
[email protected]97fa6ce32008-12-19 01:48:161855 }
1856 }
1857 }
[email protected]de246f52009-02-25 18:25:451858#else
1859 // TODO(port): Enable when encoding-related parts of Browser are ported.
1860 NOTIMPLEMENTED();
1861#endif
[email protected]97fa6ce32008-12-19 01:48:161862}
[email protected]5bcdb312009-01-07 21:43:201863
[email protected]4d434a1a2009-02-11 21:06:571864void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:201865 SavePackage::SetShouldPromptUser(should_prompt);
1866}
[email protected]87eab222009-03-13 00:47:451867
[email protected]b83e4602009-05-15 22:58:331868void AutomationProvider::SetEnableExtensionAutomation(bool automation_enabled) {
1869 AutomationExtensionFunction::SetEnabled(automation_enabled);
1870}
1871
[email protected]3753f522009-04-14 23:15:471872void AutomationProvider::GetWindowTitle(int handle, string16* text) {
1873 gfx::NativeWindow window = window_tracker_->GetResource(handle);
1874 text->assign(platform_util::GetWindowTitle(window));
1875}
[email protected]66ba4932009-06-04 19:22:131876
1877void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
1878 *count = -1; // -1 is the error code
1879 if (tab_tracker_->ContainsHandle(handle)) {
1880 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1881 TabContents* tab_contents = nav_controller->tab_contents();
1882 if (tab_contents) {
1883 BlockedPopupContainer* container =
1884 tab_contents->blocked_popup_container();
1885 if (container) {
1886 *count = static_cast<int>(container->GetBlockedPopupCount());
1887 } else {
1888 // If we don't have a container, we don't have any blocked popups to
1889 // contain!
1890 *count = 0;
1891 }
1892 }
1893 }
1894}
[email protected]f7a68432009-07-29 23:18:191895
1896void AutomationProvider::SelectAll(int tab_handle) {
1897 RenderViewHost* view = GetViewForTab(tab_handle);
1898 if (!view) {
1899 NOTREACHED();
1900 return;
1901 }
1902
1903 view->SelectAll();
1904}
1905
1906void AutomationProvider::Cut(int tab_handle) {
1907 RenderViewHost* view = GetViewForTab(tab_handle);
1908 if (!view) {
1909 NOTREACHED();
1910 return;
1911 }
1912
1913 view->Cut();
1914}
1915
1916void AutomationProvider::Copy(int tab_handle) {
1917 RenderViewHost* view = GetViewForTab(tab_handle);
1918 if (!view) {
1919 NOTREACHED();
1920 return;
1921 }
1922
1923 view->Copy();
1924}
1925
1926void AutomationProvider::Paste(int tab_handle) {
1927 RenderViewHost* view = GetViewForTab(tab_handle);
1928 if (!view) {
1929 NOTREACHED();
1930 return;
1931 }
1932
1933 view->Paste();
1934}
1935
1936void AutomationProvider::ReloadAsync(int tab_handle) {
1937 if (tab_tracker_->ContainsHandle(tab_handle)) {
1938 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1939 if (!tab) {
1940 NOTREACHED();
1941 return;
1942 }
1943
1944 tab->Reload(false);
1945 }
1946}
1947
1948void AutomationProvider::StopAsync(int tab_handle) {
1949 RenderViewHost* view = GetViewForTab(tab_handle);
1950 if (!view) {
[email protected]8b2b3312009-09-14 18:38:361951 // We tolerate StopAsync being called even before a view has been created.
1952 // So just log a warning instead of a NOTREACHED().
1953 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:191954 return;
1955 }
1956
1957 view->Stop();
1958}
1959
[email protected]2949e90d2009-08-21 15:32:521960void AutomationProvider::WaitForBrowserWindowCountToBecome(
1961 int target_count, IPC::Message* reply_message) {
1962 if (static_cast<int>(BrowserList::size()) == target_count) {
1963 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
1964 reply_message, true);
1965 Send(reply_message);
1966 return;
1967 }
1968
1969 // Set up an observer (it will delete itself).
1970 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
1971}
1972
1973void AutomationProvider::WaitForAppModalDialogToBeShown(
1974 IPC::Message* reply_message) {
1975 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
1976 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
1977 reply_message, true);
1978 Send(reply_message);
1979 return;
1980 }
1981
1982 // Set up an observer (it will delete itself).
1983 new AppModalDialogShownObserver(this, reply_message);
1984}
1985
[email protected]1126a1d32009-08-26 15:39:261986void AutomationProvider::GoBackBlockUntilNavigationsComplete(
1987 int handle, int number_of_navigations, IPC::Message* reply_message) {
1988 if (tab_tracker_->ContainsHandle(handle)) {
1989 NavigationController* tab = tab_tracker_->GetResource(handle);
1990 Browser* browser = FindAndActivateTab(tab);
1991 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
1992 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
1993 browser->GoBack(CURRENT_TAB);
1994 return;
1995 }
1996 }
1997
1998 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
1999 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2000 Send(reply_message);
2001}
2002
2003void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2004 int handle, int number_of_navigations, IPC::Message* reply_message) {
2005 if (tab_tracker_->ContainsHandle(handle)) {
2006 NavigationController* tab = tab_tracker_->GetResource(handle);
2007 Browser* browser = FindAndActivateTab(tab);
2008 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
2009 AddNavigationStatusListener(tab, reply_message, number_of_navigations);
2010 browser->GoForward(CURRENT_TAB);
2011 return;
2012 }
2013 }
2014
2015 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2016 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2017 Send(reply_message);
2018}
2019
[email protected]f7a68432009-07-29 23:18:192020RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2021 if (tab_tracker_->ContainsHandle(tab_handle)) {
2022 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2023 if (!tab) {
2024 NOTREACHED();
2025 return NULL;
2026 }
2027
2028 TabContents* tab_contents = tab->tab_contents();
2029 if (!tab_contents) {
2030 NOTREACHED();
2031 return NULL;
2032 }
2033
2034 RenderViewHost* view_host = tab_contents->render_view_host();
2035 return view_host;
2036 }
2037
2038 return NULL;
2039}
[email protected]675595f2009-08-26 22:32:042040
2041void AutomationProvider::GetBrowserForWindow(int window_handle,
2042 bool* success,
2043 int* browser_handle) {
2044 *success = false;
2045 *browser_handle = 0;
2046
2047 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2048 if (!window)
2049 return;
2050
2051 BrowserList::const_iterator iter = BrowserList::begin();
2052 for (;iter != BrowserList::end(); ++iter) {
2053 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2054 if (window == this_window) {
2055 // Add() returns the existing handle for the resource if any.
2056 *browser_handle = browser_tracker_->Add(*iter);
2057 *success = true;
2058 return;
2059 }
2060 }
2061}