blob: 998b252e2935274e011797b58b9f8dffaf167ec7 [file] [log] [blame]
[email protected]5ae5bed2009-08-21 18:52:441// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/automation/automation_provider.h"
6
[email protected]5ae5bed2009-08-21 18:52:447#include <set>
8
[email protected]202e7a72009-06-15 03:48:369#include "app/l10n_util.h"
[email protected]37126212009-05-06 02:23:3110#include "app/message_box_flags.h"
[email protected]2041cf342010-02-19 03:15:5911#include "base/callback.h"
[email protected]c6cb1992009-04-13 16:45:2912#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2013#include "base/json/json_reader.h"
[email protected]bc1407f2009-09-29 00:33:3514#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3815#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2916#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4617#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5018#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2719#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3820#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5321#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1722#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2923#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2824#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0925#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3326#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2927#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0828#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5529#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5030#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1331#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5332#include "chrome/browser/bookmarks/bookmark_model.h"
33#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]5c238752009-06-13 10:29:0734#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3935#include "chrome/browser/browser_window.h"
[email protected]fae20792009-10-28 20:31:5836#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2937#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1238#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5939#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2240#include "chrome/browser/download/download_shelf.h"
[email protected]d11c8e92009-10-20 23:26:4041#include "chrome/browser/extensions/crx_installer.h"
42#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5543#include "chrome/browser/extensions/extension_message_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1644#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5845#include "chrome/browser/find_bar.h"
46#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2947#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0148#include "chrome/browser/host_content_settings_map.h"
[email protected]0ac83682010-01-22 17:46:2749#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0650#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0151#include "chrome/browser/login_prompt.h"
[email protected]be180c802009-10-23 06:33:3152#include "chrome/browser/net/url_request_context_getter.h"
[email protected]f732c1e2009-07-30 15:48:5353#include "chrome/browser/net/url_request_mock_util.h"
[email protected]052313b2010-02-19 09:43:0854#include "chrome/browser/pref_service.h"
[email protected]a7eee32f2009-05-22 18:08:1755#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0756#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2557#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0358#include "chrome/browser/ssl/ssl_manager.h"
59#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3460#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4561#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1762#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2963#include "chrome/common/chrome_paths.h"
[email protected]a7eee32f2009-05-22 18:08:1764#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1465#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4766#include "chrome/common/platform_util.h"
[email protected]1bb5f892009-10-06 01:44:5767#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2568#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5669#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5770#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1771#include "net/proxy/proxy_service.h"
72#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2173#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2874#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2975
[email protected]de246f52009-02-25 18:25:4576#if defined(OS_WIN)
77// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4578#include "chrome/browser/character_encoding.h"
79#include "chrome/browser/download/save_package.h"
80#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4581#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4582#endif // defined(OS_WIN)
83
[email protected]5ae5bed2009-08-21 18:52:4484#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2885// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2886#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4487#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0688
[email protected]e1acf6f2008-10-27 20:43:3389using base::Time;
90
[email protected]cbab76d2008-10-13 22:42:4791class AutomationInterstitialPage : public InterstitialPage {
92 public:
[email protected]57c6a652009-05-04 07:58:3493 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4794 const GURL& url,
95 const std::string& contents)
96 : InterstitialPage(tab, true, url),
97 contents_(contents) {
98 }
99
100 virtual std::string GetHTMLContents() { return contents_; }
101
102 private:
103 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29104
[email protected]cbab76d2008-10-13 22:42:47105 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
106};
107
[email protected]c2cb8542009-08-20 21:16:51108#if !defined(OS_MACOSX)
109class ClickTask : public Task {
110 public:
[email protected]fc2e0872009-08-21 22:14:41111 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51112 virtual ~ClickTask() {}
113
114 virtual void Run() {
115 ui_controls::MouseButton button = ui_controls::LEFT;
116 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
117 views::Event::EF_LEFT_BUTTON_DOWN) {
118 button = ui_controls::LEFT;
119 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
120 views::Event::EF_RIGHT_BUTTON_DOWN) {
121 button = ui_controls::RIGHT;
122 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
123 views::Event::EF_MIDDLE_BUTTON_DOWN) {
124 button = ui_controls::MIDDLE;
125 } else {
126 NOTREACHED();
127 }
128
[email protected]fc2e0872009-08-21 22:14:41129 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51130 }
131
132 private:
[email protected]c2cb8542009-08-20 21:16:51133 int flags_;
134
135 DISALLOW_COPY_AND_ASSIGN(ClickTask);
136};
137#endif
138
initial.commit09911bf2008-07-26 23:55:29139AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57140 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56141 profile_(profile),
142 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29143 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29144 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20145 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29146 autocomplete_edit_tracker_.reset(
147 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29148 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
149 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44150 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]528211a2010-01-14 15:25:13151 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29152}
153
154AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50155 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
156 port_containers_.end());
157 port_containers_.clear();
158
[email protected]0da050b92008-08-19 19:29:47159 // Make sure that any outstanding NotificationObservers also get destroyed.
160 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31161 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47162 while ((observer = it.GetNext()) != NULL)
163 delete observer;
[email protected]528211a2010-01-14 15:25:13164
165 if (channel_.get()) {
166 channel_->Close();
167 }
168 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29169}
170
[email protected]9a3a293b2009-06-04 22:28:16171void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06172 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57173 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06174 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
175 automation_resource_message_filter_,
176 g_browser_process->io_thread()->message_loop(),
177 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05178 scoped_ptr<FileVersionInfo> file_version_info(
179 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40180 std::string version_string;
181 if (file_version_info != NULL) {
182 version_string = WideToASCII(file_version_info->file_version());
183 }
[email protected]c6cb1992009-04-13 16:45:29184
185 // Send a hello message with our current automation protocol version.
186 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29187}
188
189void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
190 if (expected_tabs == 0) {
191 Send(new AutomationMsg_InitialLoadsComplete(0));
192 } else {
193 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
194 }
195}
196
197NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58198 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01199 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29200 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58201 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01202 number_of_navigations,
203 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29204
[email protected]71f65dd2009-02-11 19:14:56205 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29206 return observer;
207}
208
209void AutomationProvider::RemoveNavigationStatusListener(
210 NotificationObserver* obs) {
211 notification_observer_list_.RemoveObserver(obs);
212}
213
214NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14215 Browser* parent,
216 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56217 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14218 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29219 notification_observer_list_.AddObserver(observer);
220
221 return observer;
222}
223
224void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
225 notification_observer_list_.RemoveObserver(obs);
226}
227
228void AutomationProvider::AddLoginHandler(NavigationController* tab,
229 LoginHandler* handler) {
230 login_handler_map_[tab] = handler;
231}
232
233void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
234 DCHECK(login_handler_map_[tab]);
235 login_handler_map_.erase(tab);
236}
237
[email protected]f44265b2009-05-19 18:52:50238void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
239 int port_id = port->port_id();
240 DCHECK_NE(-1, port_id);
241 DCHECK(port_containers_.find(port_id) == port_containers_.end());
242
243 port_containers_[port_id] = port;
244}
245
246void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
247 int port_id = port->port_id();
248 DCHECK_NE(-1, port_id);
249
250 PortContainerMap::iterator it = port_containers_.find(port_id);
251 DCHECK(it != port_containers_.end());
252
253 if (it != port_containers_.end()) {
254 delete it->second;
255 port_containers_.erase(it);
256 }
257}
258
259ExtensionPortContainer* AutomationProvider::GetPortContainer(
260 int port_id) const {
261 PortContainerMap::const_iterator it = port_containers_.find(port_id);
262 if (it == port_containers_.end())
263 return NULL;
264
265 return it->second;
266}
267
initial.commit09911bf2008-07-26 23:55:29268int AutomationProvider::GetIndexForNavigationController(
269 const NavigationController* controller, const Browser* parent) const {
270 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12271 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29272}
273
274void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
275 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14276 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56277 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
278 CloseBrowserAsync)
279 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
280 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
281 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
282 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
283 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
284 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14285 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58286 IPC_MESSAGE_HANDLER_DELAY_REPLY(
287 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
288 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56289 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
290 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
291 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
292 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
293 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14294 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56295 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
296 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
297 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14298 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29299 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
300 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56301 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36302 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56303 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29304 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56305 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29306 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
307 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56308 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14309 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24310 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44311 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33312 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44313 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33314 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24315 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14316 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24317 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15318 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14319 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44320#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24321 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35322 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14323 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]71f65dd2009-02-11 19:14:56324 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
325 WindowSimulateDrag)
[email protected]b2aa3ed72010-02-01 18:37:14326#endif
[email protected]71f65dd2009-02-11 19:14:56327 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53328 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56329 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37330#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56331 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45332#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56333 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
334 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03335 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56336 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14337 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09338 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
339 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
340 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29341 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14342 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56343 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
344 ExecuteJavascript)
345 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29346 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14347 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
348 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56349 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
350 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14351 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17352 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14353 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14354 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53355 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
356 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14357 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56358 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14359 GetAutocompleteEditForBrowser)
360 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45361#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29362 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17363#endif
[email protected]71f65dd2009-02-11 19:14:56364 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29365 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26366 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
367 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56368 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14369 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56370 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14371 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17372#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29373 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
374 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17375#endif
[email protected]71f65dd2009-02-11 19:14:56376 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
377 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14378 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17379#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04380 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39381 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
382 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17383#endif
[email protected]1c58a5c2009-05-21 18:47:14384 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
385 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44386 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
387 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56388 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
389 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29390 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
391 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
392 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56393 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41394 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56395 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
396 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29397 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56398 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29399 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56400 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29401 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56402 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29403 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56404 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45405 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45406 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
407 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14408 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56409 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57410 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56411 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57412 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56413 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
414 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53415 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
416 GetBookmarksAsJSON)
417 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
418 WaitForBookmarkModelToLoad)
419 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
420 AddBookmarkGroup)
421 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
422 AddBookmarkURL)
423 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
424 ReparentBookmark)
425 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
426 SetBookmarkTitle)
427 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
428 SetBookmarkURL)
429 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
430 RemoveBookmark)
[email protected]816633a2009-11-11 21:48:18431 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
432 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
433 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56434 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42435 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56436 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
437 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14438 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56439 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44440 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56441 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20442 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14443 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56444 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16445 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56446 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16447 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56448 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16449 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14450 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20451 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
452 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14453 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]4cfc1d922009-11-08 14:02:58454#if defined(OS_WIN)
455 // Depends on ExternalTabContainer, so Windows-only
[email protected]b83e4602009-05-15 22:58:33456 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
457 SetEnableExtensionAutomation)
[email protected]4cfc1d922009-11-08 14:02:58458#endif
[email protected]59560e0b2009-06-04 03:30:22459 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13460 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19461 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
462 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
463 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
464 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
465 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
466 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52467 IPC_MESSAGE_HANDLER_DELAY_REPLY(
468 AutomationMsg_WaitForBrowserWindowCountToBecome,
469 WaitForBrowserWindowCountToBecome)
470 IPC_MESSAGE_HANDLER_DELAY_REPLY(
471 AutomationMsg_WaitForAppModalDialogToBeShown,
472 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26473 IPC_MESSAGE_HANDLER_DELAY_REPLY(
474 AutomationMsg_GoBackBlockUntilNavigationsComplete,
475 GoBackBlockUntilNavigationsComplete)
476 IPC_MESSAGE_HANDLER_DELAY_REPLY(
477 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
478 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14479#if defined(OS_WIN)
480 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
481#endif
[email protected]1bb5f892009-10-06 01:44:57482 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40483 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
484 InstallExtension)
485 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
486 LoadExpandedExtension)
[email protected]fedaa7d2010-01-26 20:34:57487 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
488 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00489 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]2b19e2fe2010-02-16 02:24:18490#if defined(OS_WIN)
491 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
492#endif
[email protected]7dad3d5f2010-03-04 00:27:01493 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]566a0f762010-03-10 04:14:57494#if defined(OS_CHROMEOS)
495 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
496 LoginWithUserAndPass)
497#endif
initial.commit09911bf2008-07-26 23:55:29498 IPC_END_MESSAGE_MAP()
499}
500
[email protected]71f65dd2009-02-11 19:14:56501void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
502 *status = -1;
initial.commit09911bf2008-07-26 23:55:29503 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
504 Browser* browser = browser_tracker_->GetResource(handle);
505 if (at_index >= 0 && at_index < browser->tab_count()) {
506 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56507 *status = 0;
initial.commit09911bf2008-07-26 23:55:29508 }
509 }
initial.commit09911bf2008-07-26 23:55:29510}
511
[email protected]71f65dd2009-02-11 19:14:56512void AutomationProvider::AppendTab(int handle, const GURL& url,
513 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29514 int append_tab_response = -1; // -1 is the error code
515 NotificationObserver* observer = NULL;
516
517 if (browser_tracker_->ContainsHandle(handle)) {
518 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14519 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58520 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
521 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39522 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29523 if (tab_contents) {
524 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57525 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29526 }
527 }
528
529 if (append_tab_response < 0) {
530 // The append tab failed. Remove the TabStripObserver
531 if (observer) {
532 RemoveTabStripObserver(observer);
533 delete observer;
534 }
535
[email protected]71f65dd2009-02-11 19:14:56536 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
537 append_tab_response);
538 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29539 }
540}
541
[email protected]71f65dd2009-02-11 19:14:56542void AutomationProvider::NavigateToURL(int handle, const GURL& url,
543 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58544 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
545}
546
547void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
548 int handle, const GURL& url, int number_of_navigations,
549 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29550 if (tab_tracker_->ContainsHandle(handle)) {
551 NavigationController* tab = tab_tracker_->GetResource(handle);
552
553 // Simulate what a user would do. Activate the tab and then navigate.
554 // We could allow navigating in a background tab in future.
555 Browser* browser = FindAndActivateTab(tab);
556
557 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01558 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
559 false);
[email protected]71f65dd2009-02-11 19:14:56560
initial.commit09911bf2008-07-26 23:55:29561 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50562 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29563 return;
564 }
565 }
[email protected]71f65dd2009-02-11 19:14:56566
567 AutomationMsg_NavigateToURL::WriteReplyParams(
568 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
569 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29570}
[email protected]2949e90d2009-08-21 15:32:52571
[email protected]71f65dd2009-02-11 19:14:56572void AutomationProvider::NavigationAsync(int handle, const GURL& url,
573 bool* status) {
574 *status = false;
initial.commit09911bf2008-07-26 23:55:29575
576 if (tab_tracker_->ContainsHandle(handle)) {
577 NavigationController* tab = tab_tracker_->GetResource(handle);
578
579 // Simulate what a user would do. Activate the tab and then navigate.
580 // We could allow navigating in a background tab in future.
581 Browser* browser = FindAndActivateTab(tab);
582
583 if (browser) {
584 // Don't add any listener unless a callback mechanism is desired.
585 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50586 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56587 *status = true;
initial.commit09911bf2008-07-26 23:55:29588 }
589 }
initial.commit09911bf2008-07-26 23:55:29590}
591
[email protected]71f65dd2009-02-11 19:14:56592void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29593 if (tab_tracker_->ContainsHandle(handle)) {
594 NavigationController* tab = tab_tracker_->GetResource(handle);
595 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14596 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01597 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29598 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29599 return;
600 }
601 }
[email protected]71f65dd2009-02-11 19:14:56602
603 AutomationMsg_GoBack::WriteReplyParams(
604 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
605 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29606}
607
[email protected]71f65dd2009-02-11 19:14:56608void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29609 if (tab_tracker_->ContainsHandle(handle)) {
610 NavigationController* tab = tab_tracker_->GetResource(handle);
611 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14612 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01613 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29614 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29615 return;
616 }
617 }
[email protected]71f65dd2009-02-11 19:14:56618
619 AutomationMsg_GoForward::WriteReplyParams(
620 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
621 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29622}
623
[email protected]71f65dd2009-02-11 19:14:56624void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29625 if (tab_tracker_->ContainsHandle(handle)) {
626 NavigationController* tab = tab_tracker_->GetResource(handle);
627 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14628 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01629 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29630 browser->Reload();
631 return;
632 }
633 }
[email protected]71f65dd2009-02-11 19:14:56634
635 AutomationMsg_Reload::WriteReplyParams(
636 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
637 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29638}
639
[email protected]71f65dd2009-02-11 19:14:56640void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29641 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56642 const std::wstring& password,
643 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29644 if (tab_tracker_->ContainsHandle(tab_handle)) {
645 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
646 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
647
648 if (iter != login_handler_map_.end()) {
649 // If auth is needed again after this, assume login has failed. This is
650 // not strictly correct, because a navigation can require both proxy and
651 // server auth, but it should be OK for now.
652 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01653 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29654 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52655 return;
initial.commit09911bf2008-07-26 23:55:29656 }
657 }
[email protected]de246f52009-02-25 18:25:45658
[email protected]457f5cf2009-08-18 16:37:52659 AutomationMsg_SetAuth::WriteReplyParams(
660 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
661 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29662}
663
[email protected]71f65dd2009-02-11 19:14:56664void AutomationProvider::CancelAuth(int tab_handle,
665 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29666 if (tab_tracker_->ContainsHandle(tab_handle)) {
667 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
668 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
669
670 if (iter != login_handler_map_.end()) {
671 // If auth is needed again after this, something is screwy.
672 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01673 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29674 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52675 return;
initial.commit09911bf2008-07-26 23:55:29676 }
677 }
[email protected]de246f52009-02-25 18:25:45678
[email protected]457f5cf2009-08-18 16:37:52679 AutomationMsg_CancelAuth::WriteReplyParams(
680 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
681 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29682}
683
[email protected]71f65dd2009-02-11 19:14:56684void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
685 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29686
687 if (tab_tracker_->ContainsHandle(tab_handle)) {
688 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
689 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
690
691 if (iter != login_handler_map_.end()) {
692 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56693 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29694 }
695 }
initial.commit09911bf2008-07-26 23:55:29696}
697
[email protected]71f65dd2009-02-11 19:14:56698void AutomationProvider::GetRedirectsFrom(int tab_handle,
699 const GURL& source_url,
700 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29701 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
702 if (tab_tracker_->ContainsHandle(tab_handle)) {
703 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
704 HistoryService* history_service =
705 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
706
707 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
708 "has no history service";
709 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56710 DCHECK(reply_message_ == NULL);
711 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29712 // Schedule a history query for redirects. The response will be sent
713 // asynchronously from the callback the history system uses to notify us
714 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29715 redirect_query_ = history_service->QueryRedirectsFrom(
716 source_url, &consumer_,
717 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
718 return; // Response will be sent when query completes.
719 }
720 }
721
722 // Send failure response.
[email protected]deb57402009-02-06 01:35:30723 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56724 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
725 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29726}
727
[email protected]71f65dd2009-02-11 19:14:56728void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
729 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29730 if (browser_tracker_->ContainsHandle(handle)) {
731 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56732 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29733 }
initial.commit09911bf2008-07-26 23:55:29734}
735
[email protected]202e7a72009-06-15 03:48:36736void AutomationProvider::GetBrowserLocale(string16* locale) {
737 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06738 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36739}
740
[email protected]71f65dd2009-02-11 19:14:56741void AutomationProvider::GetBrowserWindowCount(int* window_count) {
742 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29743}
744
[email protected]24497032009-05-01 17:00:29745void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
746 *window_count = static_cast<int>(
747 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
748}
749
[email protected]71f65dd2009-02-11 19:14:56750void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
751 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07752 AppModalDialog* dialog_delegate =
753 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50754 *showing_dialog = (dialog_delegate != NULL);
755 if (*showing_dialog)
756 *dialog_button = dialog_delegate->GetDialogButtons();
757 else
[email protected]478ff2ed2009-04-21 23:49:18758 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20759}
760
[email protected]71f65dd2009-02-11 19:14:56761void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
762 *success = false;
[email protected]fad84eab2008-12-05 00:37:20763
[email protected]1f460072009-05-28 17:02:07764 AppModalDialog* dialog_delegate =
765 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50766 if (dialog_delegate &&
767 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18768 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
769 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28770 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56771 *success = true;
[email protected]fad84eab2008-12-05 00:37:20772 }
[email protected]478ff2ed2009-04-21 23:49:18773 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
774 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56775 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28776 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56777 *success = true;
[email protected]fad84eab2008-12-05 00:37:20778 }
779 }
[email protected]c274acc2008-11-11 20:13:44780}
781
[email protected]fedaa7d2010-01-26 20:34:57782void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
783 if (browser_tracker_->ContainsHandle(handle)) {
784 Browser* browser = browser_tracker_->GetResource(handle);
785 browser->profile()->ShutdownSessionService();
786 *result = true;
787 } else {
788 *result = false;
789 }
790}
791
[email protected]71f65dd2009-02-11 19:14:56792void AutomationProvider::GetBrowserWindow(int index, int* handle) {
793 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29794 if (index >= 0) {
795 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29796 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29797 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56798 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29799 }
800 }
initial.commit09911bf2008-07-26 23:55:29801}
802
[email protected]24497032009-05-01 17:00:29803void AutomationProvider::FindNormalBrowserWindow(int* handle) {
804 *handle = 0;
805 Browser* browser = BrowserList::FindBrowserWithType(profile_,
806 Browser::TYPE_NORMAL);
807 if (browser)
808 *handle = browser_tracker_->Add(browser);
809}
810
[email protected]71f65dd2009-02-11 19:14:56811void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
812 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29813 Browser* browser = BrowserList::GetLastActive();
814 if (browser)
[email protected]71f65dd2009-02-11 19:14:56815 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29816}
817
[email protected]b2aa3ed72010-02-01 18:37:14818#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28819// TODO(estade): use this implementation for all platforms?
820void AutomationProvider::GetActiveWindow(int* handle) {
821 gfx::NativeWindow window =
822 BrowserList::GetLastActive()->window()->GetNativeHandle();
823 *handle = window_tracker_->Add(window);
824}
825#endif
826
[email protected]4f6381ee2009-04-16 02:46:33827void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
828 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56829 *success = false;
[email protected]4ae62752008-08-04 23:28:47830 if (browser_tracker_->ContainsHandle(handle)) {
831 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14832 if (browser->command_updater()->SupportsCommand(command) &&
833 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47834 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56835 *success = true;
[email protected]4ae62752008-08-04 23:28:47836 }
837 }
[email protected]4ae62752008-08-04 23:28:47838}
839
[email protected]4f6381ee2009-04-16 02:46:33840void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56841 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53842 // List of commands which just finish synchronously and don't require
843 // setting up an observer.
844 static const int kSynchronousCommands[] = {
845 IDC_HOME,
846 IDC_SELECT_NEXT_TAB,
847 IDC_SELECT_PREVIOUS_TAB,
848 IDC_SHOW_DOWNLOADS,
849 IDC_SHOW_HISTORY,
850 };
[email protected]56e71b7c2009-03-27 03:05:56851 if (browser_tracker_->ContainsHandle(handle)) {
852 Browser* browser = browser_tracker_->GetResource(handle);
853 if (browser->command_updater()->SupportsCommand(command) &&
854 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53855 // First check if we can handle the command without using an observer.
856 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
857 if (command == kSynchronousCommands[i]) {
858 browser->ExecuteCommand(command);
859 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
860 true);
861 Send(reply_message);
862 return;
863 }
864 }
865
866 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42867 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
868 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27869 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42870 return;
871 }
[email protected]56e71b7c2009-03-27 03:05:56872 }
873 }
[email protected]49a14a82009-03-31 04:16:44874 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56875 Send(reply_message);
876}
877
[email protected]fc2e0872009-08-21 22:14:41878// This task just adds another task to the event queue. This is useful if
879// you want to ensure that any tasks added to the event queue after this one
880// have already been processed by the time |task| is run.
881class InvokeTaskLaterTask : public Task {
882 public:
883 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
884 virtual ~InvokeTaskLaterTask() {}
885
886 virtual void Run() {
887 MessageLoop::current()->PostTask(FROM_HERE, task_);
888 }
889
890 private:
891 Task* task_;
892
893 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
894};
895
[email protected]b2aa3ed72010-02-01 18:37:14896#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29897void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
898 int handle,
[email protected]d1a5941e2009-08-13 23:34:24899 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29900 int flags) {
[email protected]b410bc32009-08-14 01:11:14901 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51902 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41903 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29904 }
905}
906
[email protected]60507b12009-11-02 23:51:35907void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
908 int handle,
909 const gfx::Point& location) {
910 if (window_tracker_->ContainsHandle(handle))
911 ui_controls::SendMouseMove(location.x(), location.y());
912}
913
initial.commit09911bf2008-07-26 23:55:29914void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
915 int handle,
[email protected]bc1407f2009-09-29 00:33:35916 int key,
initial.commit09911bf2008-07-26 23:55:29917 int flags) {
[email protected]b410bc32009-08-14 01:11:14918 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29919 return;
920
[email protected]b410bc32009-08-14 01:11:14921 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29922 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35923 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38924 ((flags & views::Event::EF_CONTROL_DOWN) ==
925 views::Event::EF_CONTROL_DOWN),
926 ((flags & views::Event::EF_SHIFT_DOWN) ==
927 views::Event::EF_SHIFT_DOWN),
928 ((flags & views::Event::EF_ALT_DOWN) ==
929 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29930}
[email protected]b2aa3ed72010-02-01 18:37:14931#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29932
[email protected]71f65dd2009-02-11 19:14:56933void AutomationProvider::IsWindowActive(int handle, bool* success,
934 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29935 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17936 *is_active =
937 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56938 *success = true;
initial.commit09911bf2008-07-26 23:55:29939 } else {
[email protected]71f65dd2009-02-11 19:14:56940 *success = false;
941 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29942 }
943}
944
[email protected]71f65dd2009-02-11 19:14:56945void AutomationProvider::GetTabCount(int handle, int* tab_count) {
946 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29947
948 if (browser_tracker_->ContainsHandle(handle)) {
949 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56950 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29951 }
initial.commit09911bf2008-07-26 23:55:29952}
953
[email protected]982921f12009-10-27 21:43:53954void AutomationProvider::GetType(int handle, int* type_as_int) {
955 *type_as_int = -1; // -1 is the error code
956
957 if (browser_tracker_->ContainsHandle(handle)) {
958 Browser* browser = browser_tracker_->GetResource(handle);
959 *type_as_int = static_cast<int>(browser->type());
960 }
961}
962
[email protected]71f65dd2009-02-11 19:14:56963void AutomationProvider::GetTab(int win_handle, int tab_index,
964 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56965 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29966 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
967 Browser* browser = browser_tracker_->GetResource(win_handle);
968 if (tab_index < browser->tab_count()) {
969 TabContents* tab_contents =
970 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57971 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29972 }
973 }
initial.commit09911bf2008-07-26 23:55:29974}
975
[email protected]71f65dd2009-02-11 19:14:56976void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
977 std::wstring* title) {
978 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29979 if (tab_tracker_->ContainsHandle(handle)) {
980 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36981 NavigationEntry* entry = tab->GetActiveEntry();
982 if (entry != NULL) {
983 *title = UTF16ToWideHack(entry->title());
984 } else {
985 *title = std::wstring();
986 }
[email protected]71f65dd2009-02-11 19:14:56987 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29988 }
initial.commit09911bf2008-07-26 23:55:29989}
990
[email protected]77bc6732009-04-20 22:01:03991void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
992 *tabstrip_index = -1; // -1 is the error code
993
994 if (tab_tracker_->ContainsHandle(handle)) {
995 NavigationController* tab = tab_tracker_->GetResource(handle);
996 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12997 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:03998 }
999}
1000
initial.commit09911bf2008-07-26 23:55:291001void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1002 if (window_tracker_->ContainsHandle(handle)) {
1003 window_tracker_->Remove(window_tracker_->GetResource(handle));
1004 }
1005}
1006
1007void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161008 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571009 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291010}
1011
1012// TODO(brettw) change this to accept GURLs when history supports it
1013void AutomationProvider::OnRedirectQueryComplete(
1014 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371015 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291016 bool success,
[email protected]379c2b12009-07-01 21:50:331017 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291018 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561019 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291020
[email protected]deb57402009-02-06 01:35:301021 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101022 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291023 if (success) {
initial.commit09911bf2008-07-26 23:55:291024 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301025 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291026 }
1027
[email protected]4f3dc372009-02-24 00:10:291028 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301029
[email protected]71f65dd2009-02-11 19:14:561030 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:171031 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561032 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291033}
1034
1035bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571036 DCHECK(channel_.get());
1037 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291038}
1039
1040Browser* AutomationProvider::FindAndActivateTab(
1041 NavigationController* controller) {
1042 int tab_index;
1043 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1044 if (browser)
1045 browser->SelectTabContentsAt(tab_index, true);
1046
1047 return browser;
1048}
1049
[email protected]71f65dd2009-02-11 19:14:561050void AutomationProvider::GetCookies(const GURL& url, int handle,
1051 int* value_size,
1052 std::string* value) {
1053 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291054 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1055 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311056
1057 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001058 scoped_refptr<URLRequestContextGetter> request_context =
1059 tab->tab_contents()->request_context();
1060 if (!request_context.get())
1061 request_context = tab->profile()->GetRequestContext();
1062
1063 net::CookieStore* cookie_store = request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311064
1065 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561066 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291067 }
initial.commit09911bf2008-07-26 23:55:291068}
1069
[email protected]71f65dd2009-02-11 19:14:561070void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291071 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561072 int handle,
1073 int* response_value) {
1074 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291075
1076 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1077 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311078
[email protected]dfa46e5f2009-11-17 18:48:431079 scoped_refptr<URLRequestContextGetter> request_context =
1080 tab->tab_contents()->request_context();
1081 if (!request_context.get())
1082 request_context = tab->profile()->GetRequestContext();
1083
[email protected]be180c802009-10-23 06:33:311084 // Since we are running on the UI thread don't call GetURLRequestContext().
1085 scoped_refptr<net::CookieStore> cookie_store =
[email protected]dfa46e5f2009-11-17 18:48:431086 request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311087
1088 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561089 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291090 }
initial.commit09911bf2008-07-26 23:55:291091}
1092
[email protected]71f65dd2009-02-11 19:14:561093void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1094 *success = false;
initial.commit09911bf2008-07-26 23:55:291095 if (tab_tracker_->ContainsHandle(handle)) {
1096 NavigationController* tab = tab_tracker_->GetResource(handle);
1097 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541098 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561099 *success = true;
initial.commit09911bf2008-07-26 23:55:291100 }
initial.commit09911bf2008-07-26 23:55:291101}
1102
[email protected]71f65dd2009-02-11 19:14:561103void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1104 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291105
1106 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561107 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341108 TabContents* tab_contents =
1109 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161110 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1111 if (rph)
1112 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291113 }
initial.commit09911bf2008-07-26 23:55:291114}
1115
1116void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331117 NOTREACHED() << "This function has been deprecated. "
1118 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291119}
1120
[email protected]71f65dd2009-02-11 19:14:561121void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291122 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561123 const std::wstring& script,
1124 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291125 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341126 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1127 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571128 // Set the routing id of this message with the controller.
1129 // This routing id needs to be remembered for the reverse
1130 // communication while sending back the response of
1131 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331132 std::wstring set_automation_id;
1133 SStringPrintf(&set_automation_id,
1134 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561135 reply_message->routing_id());
1136
1137 DCHECK(reply_message_ == NULL);
1138 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291139
[email protected]57c6a652009-05-04 07:58:341140 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331141 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341142 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061143 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571144 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291145 }
1146
1147 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561148 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1149 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291150 }
1151}
1152
[email protected]71f65dd2009-02-11 19:14:561153void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1154 *visible = false;
[email protected]20e93d12008-08-28 16:31:571155
[email protected]59560e0b2009-06-04 03:30:221156 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251157#if defined(OS_CHROMEOS)
1158 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1159 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1160 for (BrowserList::const_iterator it = BrowserList::begin();
1161 it != BrowserList::end(); ++it) {
1162 if ((*it)->type() == Browser::TYPE_POPUP) {
1163 const GURL& url =
1164 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1165
1166 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1167 url.host() == chrome::kChromeUIFileBrowseHost) {
1168 *visible = true;
1169 break;
1170 }
1171 }
1172 }
1173#else
[email protected]59560e0b2009-06-04 03:30:221174 Browser* browser = browser_tracker_->GetResource(handle);
1175 if (browser) {
1176 *visible = browser->window()->IsDownloadShelfVisible();
1177 }
[email protected]f5bf8ccf2010-02-05 18:19:251178#endif
[email protected]59560e0b2009-06-04 03:30:221179 }
initial.commit09911bf2008-07-26 23:55:291180}
1181
[email protected]59560e0b2009-06-04 03:30:221182void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1183 if (browser_tracker_->ContainsHandle(handle)) {
1184 Browser* browser = browser_tracker_->GetResource(handle);
1185 if (browser) {
1186 if (visible)
1187 browser->window()->GetDownloadShelf()->Show();
1188 else
1189 browser->window()->GetDownloadShelf()->Close();
1190 }
1191 }
1192}
1193
[email protected]34930432009-11-09 00:12:091194void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1195 *visible = false;
1196
1197 if (browser_tracker_->ContainsHandle(handle)) {
1198 Browser* browser = browser_tracker_->GetResource(handle);
1199 if (browser)
1200 *visible = browser->window()->IsFullscreen();
1201 }
1202}
1203
1204void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1205 bool* visible) {
1206 *visible = false;
1207
1208 if (browser_tracker_->ContainsHandle(handle)) {
1209 Browser* browser = browser_tracker_->GetResource(handle);
1210 if (browser)
1211 *visible = browser->window()->IsFullscreenBubbleVisible();
1212 }
1213}
[email protected]59560e0b2009-06-04 03:30:221214
[email protected]71f65dd2009-02-11 19:14:561215void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1216 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291217 if (tab_tracker_->ContainsHandle(handle)) {
1218 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111219 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291220 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561221 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291222 }
1223 }
initial.commit09911bf2008-07-26 23:55:291224}
1225
initial.commit09911bf2008-07-26 23:55:291226void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561227 int handle, const std::wstring& find_request,
1228 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311229 NOTREACHED() << "This function has been deprecated."
1230 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561231 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311232 return;
1233}
1234
[email protected]4f999132009-03-31 18:08:401235void AutomationProvider::HandleFindRequest(
1236 int handle,
1237 const AutomationMsg_Find_Params& params,
1238 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291239 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561240 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1241 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291242 return;
1243 }
1244
1245 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111246 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291247
1248 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141249 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291250
[email protected]57c6a652009-05-04 07:58:341251 tab_contents->set_current_find_request_id(
1252 FindInPageNotificationObserver::kFindInPageRequestId);
1253 tab_contents->render_view_host()->StartFinding(
1254 FindInPageNotificationObserver::kFindInPageRequestId,
1255 params.search_string, params.forward, params.match_case,
1256 params.find_next);
initial.commit09911bf2008-07-26 23:55:291257}
1258
[email protected]5f8af2a2008-08-06 22:49:451259void AutomationProvider::HandleOpenFindInPageRequest(
1260 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291261 if (browser_tracker_->ContainsHandle(handle)) {
1262 Browser* browser = browser_tracker_->GetResource(handle);
1263 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451264 }
1265}
1266
[email protected]71f65dd2009-02-11 19:14:561267void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561268 *visible = false;
[email protected]855c0142009-09-28 22:35:241269 Browser* browser = browser_tracker_->GetResource(handle);
1270 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581271 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171272 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241273 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291274 }
[email protected]20e93d12008-08-28 16:31:571275}
1276
[email protected]71f65dd2009-02-11 19:14:561277void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1278 int* y) {
[email protected]9e0534b2008-10-21 15:03:011279 gfx::Point position(0, 0);
1280 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291281 if (browser_tracker_->ContainsHandle(handle)) {
1282 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581283 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171284 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581285 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291286 }
[email protected]20e93d12008-08-28 16:31:571287
[email protected]71f65dd2009-02-11 19:14:561288 *x = position.x();
1289 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571290}
1291
[email protected]c3240722010-03-05 21:52:581292void AutomationProvider::GetBookmarkBarVisibility(int handle,
1293 bool* visible,
1294 bool* animating) {
1295 *visible = false;
1296 *animating = false;
1297
1298 if (browser_tracker_->ContainsHandle(handle)) {
1299 Browser* browser = browser_tracker_->GetResource(handle);
1300 if (browser) {
1301 *visible = browser->window()->IsBookmarkBarVisible();
1302 *animating = browser->window()->IsBookmarkBarAnimating();
1303 }
1304 }
1305}
1306
[email protected]6d8ffc9f2010-03-12 18:27:531307void AutomationProvider::GetBookmarksAsJSON(int handle,
1308 std::string* bookmarks_as_json,
1309 bool *success) {
1310 *success = false;
1311 if (browser_tracker_->ContainsHandle(handle)) {
1312 Browser* browser = browser_tracker_->GetResource(handle);
1313 if (browser) {
1314 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1315 return;
1316 }
1317 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1318 browser->profile(),
1319 browser->profile()->GetBookmarkModel());
1320 *success = storage->SerializeData(bookmarks_as_json);
1321 }
1322 }
1323}
1324
1325void AutomationProvider::WaitForBookmarkModelToLoad(
1326 int handle,
1327 IPC::Message* reply_message) {
1328 if (browser_tracker_->ContainsHandle(handle)) {
1329 Browser* browser = browser_tracker_->GetResource(handle);
1330 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1331 if (model->IsLoaded()) {
1332 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1333 reply_message, true);
1334 Send(reply_message);
1335 } else {
1336 // The observer will delete itself when done.
1337 new AutomationProviderBookmarkModelObserver(this, reply_message,
1338 model);
1339 }
1340 }
1341}
1342
1343void AutomationProvider::AddBookmarkGroup(int handle,
1344 int64 parent_id, int index,
1345 std::wstring title,
1346 bool* success) {
1347 if (browser_tracker_->ContainsHandle(handle)) {
1348 Browser* browser = browser_tracker_->GetResource(handle);
1349 if (browser) {
1350 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1351 if (!model->IsLoaded()) {
1352 *success = false;
1353 return;
1354 }
1355 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1356 DCHECK(parent);
1357 if (parent) {
1358 const BookmarkNode* child = model->AddGroup(parent, index,
1359 WideToUTF16(title));
1360 DCHECK(child);
1361 if (child)
1362 *success = true;
1363 }
1364 }
1365 }
1366 *success = false;
1367}
1368
1369void AutomationProvider::AddBookmarkURL(int handle,
1370 int64 parent_id, int index,
1371 std::wstring title, const GURL& url,
1372 bool* success) {
1373 if (browser_tracker_->ContainsHandle(handle)) {
1374 Browser* browser = browser_tracker_->GetResource(handle);
1375 if (browser) {
1376 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1377 if (!model->IsLoaded()) {
1378 *success = false;
1379 return;
1380 }
1381 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1382 DCHECK(parent);
1383 if (parent) {
1384 const BookmarkNode* child = model->AddURL(parent, index,
1385 WideToUTF16(title), url);
1386 DCHECK(child);
1387 if (child)
1388 *success = true;
1389 }
1390 }
1391 }
1392 *success = false;
1393}
1394
1395void AutomationProvider::ReparentBookmark(int handle,
1396 int64 id, int64 new_parent_id,
1397 int index,
1398 bool* success) {
1399 if (browser_tracker_->ContainsHandle(handle)) {
1400 Browser* browser = browser_tracker_->GetResource(handle);
1401 if (browser) {
1402 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1403 if (!model->IsLoaded()) {
1404 *success = false;
1405 return;
1406 }
1407 const BookmarkNode* node = model->GetNodeByID(id);
1408 DCHECK(node);
1409 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1410 DCHECK(new_parent);
1411 if (node && new_parent) {
1412 model->Move(node, new_parent, index);
1413 *success = true;
1414 }
1415 }
1416 }
1417 *success = false;
1418}
1419
1420void AutomationProvider::SetBookmarkTitle(int handle,
1421 int64 id, std::wstring title,
1422 bool* success) {
1423 if (browser_tracker_->ContainsHandle(handle)) {
1424 Browser* browser = browser_tracker_->GetResource(handle);
1425 if (browser) {
1426 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1427 if (!model->IsLoaded()) {
1428 *success = false;
1429 return;
1430 }
1431 const BookmarkNode* node = model->GetNodeByID(id);
1432 DCHECK(node);
1433 if (node) {
1434 model->SetTitle(node, WideToUTF16(title));
1435 *success = true;
1436 }
1437 }
1438 }
1439 *success = false;
1440}
1441
1442void AutomationProvider::SetBookmarkURL(int handle,
1443 int64 id, const GURL& url,
1444 bool* success) {
1445 if (browser_tracker_->ContainsHandle(handle)) {
1446 Browser* browser = browser_tracker_->GetResource(handle);
1447 if (browser) {
1448 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1449 if (!model->IsLoaded()) {
1450 *success = false;
1451 return;
1452 }
1453 const BookmarkNode* node = model->GetNodeByID(id);
1454 DCHECK(node);
1455 if (node) {
1456 model->SetURL(node, url);
1457 *success = true;
1458 }
1459 }
1460 }
1461 *success = false;
1462}
1463
1464void AutomationProvider::RemoveBookmark(int handle,
1465 int64 id,
1466 bool* success) {
1467 if (browser_tracker_->ContainsHandle(handle)) {
1468 Browser* browser = browser_tracker_->GetResource(handle);
1469 if (browser) {
1470 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1471 if (!model->IsLoaded()) {
1472 *success = false;
1473 return;
1474 }
1475 const BookmarkNode* node = model->GetNodeByID(id);
1476 DCHECK(node);
1477 if (node) {
1478 const BookmarkNode* parent = node->GetParent();
1479 DCHECK(parent);
1480 model->Remove(parent, parent->IndexOfChild(node));
1481 *success = true;
1482 }
1483 }
1484 }
1485 *success = false;
1486}
1487
initial.commit09911bf2008-07-26 23:55:291488void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561489 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341490 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1491 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561492 DCHECK(reply_message_ == NULL);
1493 reply_message_ = reply_message;
1494
[email protected]d9f9b792009-06-24 13:17:121495 DevToolsManager::GetInstance()->InspectElement(
1496 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291497 } else {
[email protected]71f65dd2009-02-11 19:14:561498 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1499 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291500 }
1501}
1502
1503void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091504 if (reply_message_) {
1505 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1506 num_resources);
1507 Send(reply_message_);
1508 reply_message_ = NULL;
1509 }
initial.commit09911bf2008-07-26 23:55:291510}
1511
[email protected]a7eee32f2009-05-22 18:08:171512class SetProxyConfigTask : public Task {
1513 public:
[email protected]be180c802009-10-23 06:33:311514 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
1515 const std::string& new_proxy_config)
1516 : request_context_getter_(request_context_getter), proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:171517 virtual void Run() {
1518 // First, deserialize the JSON string. If this fails, log and bail.
1519 JSONStringValueSerializer deserializer(proxy_config_);
1520 std::string error_message;
1521 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1522 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1523 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1524 << error_message;
1525 return;
1526 }
1527
1528 scoped_ptr<DictionaryValue> dict(
1529 static_cast<DictionaryValue*>(root.release()));
1530 // Now put together a proxy configuration from the deserialized string.
1531 net::ProxyConfig pc;
1532 PopulateProxyConfig(*dict.get(), &pc);
1533
[email protected]be180c802009-10-23 06:33:311534 net::ProxyService* proxy_service =
1535 request_context_getter_->GetURLRequestContext()->proxy_service();
1536 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:171537 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1538 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:311539 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:171540 }
1541
1542 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1543 DCHECK(pc);
1544 bool no_proxy = false;
1545 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1546 // Make no changes to the ProxyConfig.
1547 return;
1548 }
1549 bool auto_config;
1550 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:481551 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:171552 }
1553 std::string pac_url;
1554 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:481555 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:171556 }
1557 std::string proxy_bypass_list;
1558 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:481559 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:171560 }
1561 std::string proxy_server;
1562 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:481563 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:171564 }
1565 }
1566
1567 private:
[email protected]be180c802009-10-23 06:33:311568 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:171569 std::string proxy_config_;
1570};
1571
1572
1573void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:311574 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
1575 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:171576 FilePath user_data_dir;
1577 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1578 ProfileManager* profile_manager = g_browser_process->profile_manager();
1579 DCHECK(profile_manager);
1580 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1581 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:311582 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171583 }
[email protected]be180c802009-10-23 06:33:311584 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:171585
[email protected]fae20792009-10-28 20:31:581586 ChromeThread::PostTask(
1587 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:311588 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171589}
1590
[email protected]4f3dc372009-02-24 00:10:291591void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331592 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291593 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291594 if (tab_tracker_->ContainsHandle(handle)) {
1595 NavigationController* tab = tab_tracker_->GetResource(handle);
1596 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1597 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331598 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291599 }
initial.commit09911bf2008-07-26 23:55:291600}
1601
[email protected]6a5670d22009-10-27 16:21:341602void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:141603 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:531604 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
1605 reply_message);
1606}
1607
1608void AutomationProvider::OpenNewBrowserWindowOfType(
1609 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:141610 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291611 // We may have no current browser windows open so don't rely on
1612 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:531613 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
1614 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:051615 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151616 if (show)
[email protected]15952e462008-11-14 00:29:051617 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291618}
1619
[email protected]71f65dd2009-02-11 19:14:561620void AutomationProvider::GetWindowForBrowser(int browser_handle,
1621 bool* success,
1622 int* handle) {
1623 *success = false;
1624 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291625
1626 if (browser_tracker_->ContainsHandle(browser_handle)) {
1627 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201628 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291629 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201630 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561631 *success = true;
initial.commit09911bf2008-07-26 23:55:291632 }
initial.commit09911bf2008-07-26 23:55:291633}
1634
[email protected]5ae5bed2009-08-21 18:52:441635#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291636void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561637 int browser_handle,
1638 bool* success,
1639 int* autocomplete_edit_handle) {
1640 *success = false;
1641 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291642
1643 if (browser_tracker_->ContainsHandle(browser_handle)) {
1644 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061645 LocationBar* loc_bar = browser->window()->GetLocationBar();
1646 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291647 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561648 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1649 *success = true;
initial.commit09911bf2008-07-26 23:55:291650 }
initial.commit09911bf2008-07-26 23:55:291651}
[email protected]5ae5bed2009-08-21 18:52:441652#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291653
[email protected]71f65dd2009-02-11 19:14:561654void AutomationProvider::ShowInterstitialPage(int tab_handle,
1655 const std::string& html_text,
1656 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291657 if (tab_tracker_->ContainsHandle(tab_handle)) {
1658 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111659 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401660
[email protected]7dad3d5f2010-03-04 00:27:011661 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:401662 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341663 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401664 GURL("about:interstitial"),
1665 html_text);
1666 interstitial->Show();
1667 return;
initial.commit09911bf2008-07-26 23:55:291668 }
[email protected]71f65dd2009-02-11 19:14:561669
[email protected]457f5cf2009-08-18 16:37:521670 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1671 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561672 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291673}
1674
[email protected]71f65dd2009-02-11 19:14:561675void AutomationProvider::HideInterstitialPage(int tab_handle,
1676 bool* success) {
1677 *success = false;
[email protected]57c6a652009-05-04 07:58:341678 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1679 if (tab_contents && tab_contents->interstitial_page()) {
1680 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561681 *success = true;
initial.commit09911bf2008-07-26 23:55:291682 }
initial.commit09911bf2008-07-26 23:55:291683}
1684
[email protected]71f65dd2009-02-11 19:14:561685void AutomationProvider::CloseTab(int tab_handle,
1686 bool wait_until_closed,
1687 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291688 if (tab_tracker_->ContainsHandle(tab_handle)) {
1689 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1690 int index;
1691 Browser* browser = Browser::GetBrowserForController(controller, &index);
1692 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141693 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111694 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451695 return;
initial.commit09911bf2008-07-26 23:55:291696 }
[email protected]de246f52009-02-25 18:25:451697
1698 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:551699 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291700}
1701
[email protected]71f65dd2009-02-11 19:14:561702void AutomationProvider::CloseBrowser(int browser_handle,
1703 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291704 if (browser_tracker_->ContainsHandle(browser_handle)) {
1705 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561706 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561707 reply_message);
[email protected]f3e99e32008-07-30 04:48:391708 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291709 } else {
1710 NOTREACHED();
1711 }
1712}
1713
[email protected]71f65dd2009-02-11 19:14:561714void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1715 if (browser_tracker_->ContainsHandle(browser_handle)) {
1716 Browser* browser = browser_tracker_->GetResource(browser_handle);
1717 browser->window()->Close();
1718 } else {
1719 NOTREACHED();
1720 }
1721}
1722
[email protected]71f65dd2009-02-11 19:14:561723void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:571724 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:561725 AutomationMsg_NavigationResponseValues* status) {
1726 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291727
1728 if (tab_tracker_->ContainsHandle(handle)) {
1729 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:571730 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561731 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291732 }
initial.commit09911bf2008-07-26 23:55:291733}
1734
[email protected]4150ef02009-08-19 23:14:261735void AutomationProvider::NavigateExternalTabAtIndex(
1736 int handle, int navigation_index,
1737 AutomationMsg_NavigationResponseValues* status) {
1738 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1739
1740 if (tab_tracker_->ContainsHandle(handle)) {
1741 NavigationController* tab = tab_tracker_->GetResource(handle);
1742 tab->GoToIndex(navigation_index);
1743 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1744 }
1745}
1746
[email protected]71f65dd2009-02-11 19:14:561747void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1748 IPC::Message* reply_message) {
1749 if (tab_tracker_->ContainsHandle(tab_handle)) {
1750 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1751 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141752 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561753 }
1754}
1755
[email protected]71f65dd2009-02-11 19:14:561756void AutomationProvider::GetSecurityState(int handle, bool* success,
1757 SecurityStyle* security_style,
1758 int* ssl_cert_status,
1759 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291760 if (tab_tracker_->ContainsHandle(handle)) {
1761 NavigationController* tab = tab_tracker_->GetResource(handle);
1762 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561763 *success = true;
1764 *security_style = entry->ssl().security_style();
1765 *ssl_cert_status = entry->ssl().cert_status();
1766 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291767 } else {
[email protected]71f65dd2009-02-11 19:14:561768 *success = false;
1769 *security_style = SECURITY_STYLE_UNKNOWN;
1770 *ssl_cert_status = 0;
1771 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291772 }
1773}
1774
[email protected]71f65dd2009-02-11 19:14:561775void AutomationProvider::GetPageType(int handle, bool* success,
1776 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291777 if (tab_tracker_->ContainsHandle(handle)) {
1778 NavigationController* tab = tab_tracker_->GetResource(handle);
1779 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561780 *page_type = entry->page_type();
1781 *success = true;
initial.commit09911bf2008-07-26 23:55:291782 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341783 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561784 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341785 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561786 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291787 } else {
[email protected]71f65dd2009-02-11 19:14:561788 *success = false;
1789 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291790 }
1791}
1792
[email protected]84abba62009-10-07 17:01:441793void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
1794 int* duration_ms) {
1795 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
1796 event_name);
1797}
1798
[email protected]71f65dd2009-02-11 19:14:561799void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1800 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291801 if (tab_tracker_->ContainsHandle(handle)) {
1802 NavigationController* tab = tab_tracker_->GetResource(handle);
1803 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071804 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401805 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471806 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341807 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291808 if (ssl_blocking_page) {
1809 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:011810 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:561811 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291812 return;
1813 }
1814 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521815 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1816 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561817 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291818 return;
1819 }
1820 }
1821 }
1822 // We failed.
[email protected]457f5cf2009-08-18 16:37:521823 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1824 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561825 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291826}
1827
[email protected]71f65dd2009-02-11 19:14:561828void AutomationProvider::BringBrowserToFront(int browser_handle,
1829 bool* success) {
initial.commit09911bf2008-07-26 23:55:291830 if (browser_tracker_->ContainsHandle(browser_handle)) {
1831 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061832 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561833 *success = true;
initial.commit09911bf2008-07-26 23:55:291834 } else {
[email protected]71f65dd2009-02-11 19:14:561835 *success = false;
initial.commit09911bf2008-07-26 23:55:291836 }
1837}
1838
[email protected]71f65dd2009-02-11 19:14:561839void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1840 int message_num,
1841 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291842 if (browser_tracker_->ContainsHandle(browser_handle)) {
1843 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561844 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141845 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291846 } else {
[email protected]71f65dd2009-02-11 19:14:561847 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291848 }
1849}
1850
[email protected]71f65dd2009-02-11 19:14:561851void AutomationProvider::PrintNow(int tab_handle,
1852 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441853#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571854 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341855 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1856 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291857 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571858 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141859 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341860 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571861 return;
initial.commit09911bf2008-07-26 23:55:291862 }
[email protected]71f65dd2009-02-11 19:14:561863 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1864 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441865#else
1866 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1867 NOTIMPLEMENTED();
1868#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291869}
[email protected]d301c952009-07-13 15:02:411870
[email protected]71f65dd2009-02-11 19:14:561871void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111872 const FilePath& file_name,
1873 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561874 int type,
1875 bool* success) {
initial.commit09911bf2008-07-26 23:55:291876 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561877 *success = false;
initial.commit09911bf2008-07-26 23:55:291878 return;
1879 }
1880
1881 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1882 Browser* browser = FindAndActivateTab(nav);
1883 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141884 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561885 *success = false;
initial.commit09911bf2008-07-26 23:55:291886 return;
1887 }
1888
initial.commit09911bf2008-07-26 23:55:291889 SavePackage::SavePackageType save_type =
1890 static_cast<SavePackage::SavePackageType>(type);
1891 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1892 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341893 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291894
[email protected]71f65dd2009-02-11 19:14:561895 *success = true;
initial.commit09911bf2008-07-26 23:55:291896}
1897
[email protected]5ae5bed2009-08-21 18:52:441898#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471899// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561900void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1901 bool* success,
1902 std::wstring* text) {
1903 *success = false;
initial.commit09911bf2008-07-26 23:55:291904 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561905 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521906 GetText();
[email protected]71f65dd2009-02-11 19:14:561907 *success = true;
initial.commit09911bf2008-07-26 23:55:291908 }
initial.commit09911bf2008-07-26 23:55:291909}
1910
[email protected]71f65dd2009-02-11 19:14:561911void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1912 const std::wstring& text,
1913 bool* success) {
1914 *success = false;
initial.commit09911bf2008-07-26 23:55:291915 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521916 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1917 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561918 *success = true;
initial.commit09911bf2008-07-26 23:55:291919 }
initial.commit09911bf2008-07-26 23:55:291920}
1921
1922void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561923 int autocomplete_edit_handle,
1924 bool* success,
1925 std::vector<AutocompleteMatchData>* matches) {
1926 *success = false;
initial.commit09911bf2008-07-26 23:55:291927 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271928 const AutocompleteResult& result = autocomplete_edit_tracker_->
1929 GetResource(autocomplete_edit_handle)->model()->result();
1930 for (AutocompleteResult::const_iterator i = result.begin();
1931 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561932 matches->push_back(AutocompleteMatchData(*i));
1933 *success = true;
initial.commit09911bf2008-07-26 23:55:291934 }
initial.commit09911bf2008-07-26 23:55:291935}
1936
1937void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561938 int autocomplete_edit_handle,
1939 bool* success,
1940 bool* query_in_progress) {
1941 *success = false;
1942 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291943 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561944 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521945 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561946 *success = true;
initial.commit09911bf2008-07-26 23:55:291947 }
initial.commit09911bf2008-07-26 23:55:291948}
1949
[email protected]28790922009-03-09 19:48:371950void AutomationProvider::OnMessageFromExternalHost(int handle,
1951 const std::string& message,
1952 const std::string& origin,
1953 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191954 RenderViewHost* view_host = GetViewForTab(handle);
1955 if (!view_host) {
1956 return;
[email protected]fa83e762008-08-15 21:41:391957 }
[email protected]f7a68432009-07-29 23:18:191958
1959 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1960 view_host, message, origin, target)) {
1961 // Message was diverted.
1962 return;
1963 }
1964
1965 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1966 origin, target, this, view_host, handle)) {
1967 // Message was diverted.
1968 return;
1969 }
1970
1971 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1972 // Message was diverted.
1973 return;
1974 }
1975
1976 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391977}
[email protected]a9024892009-06-16 23:13:551978
1979bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1980 const std::string& message, const std::string& origin,
1981 const std::string& target) {
1982 if (target !=
1983 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1984 return false;
1985
1986 if (origin != extension_automation_constants::kAutomationOrigin) {
1987 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1988 return false;
1989 }
1990
1991 // The message is a JSON-encoded array with two elements, both strings. The
1992 // first is the name of the event to dispatch. The second is a JSON-encoding
1993 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:201994 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:551995 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1996 LOG(WARNING) << "Invalid browser event specified through automation";
1997 return false;
1998 }
1999
2000 const ListValue* args = static_cast<const ListValue*>(message_value.get());
2001
2002 std::string event_name;
2003 if (!args->GetString(0, &event_name)) {
2004 LOG(WARNING) << "No browser event name specified through automation";
2005 return false;
2006 }
2007
2008 std::string json_args;
2009 if (!args->GetString(1, &json_args)) {
2010 LOG(WARNING) << "No browser event args specified through automation";
2011 return false;
2012 }
2013
[email protected]7120f132009-07-20 21:05:372014 if (profile()->GetExtensionMessageService()) {
[email protected]db7331a2010-02-25 22:10:502015 profile()->GetExtensionMessageService()->DispatchEventToRenderers(
2016 event_name, json_args, profile()->IsOffTheRecord());
[email protected]7120f132009-07-20 21:05:372017 }
[email protected]a9024892009-06-16 23:13:552018
2019 return true;
2020}
[email protected]5ae5bed2009-08-21 18:52:442021#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:392022
[email protected]57c6a652009-05-04 07:58:342023TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572024 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572025 if (tab_tracker_->ContainsHandle(handle)) {
2026 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342027 if (tab)
2028 *tab = nav_controller;
2029 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572030 }
[email protected]57c6a652009-05-04 07:58:342031 return NULL;
[email protected]20e93d12008-08-28 16:31:572032}
2033
initial.commit09911bf2008-07-26 23:55:292034TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2035 : AutomationProvider(profile) {
2036 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142037 registrar_.Add(this, NotificationType::SESSION_END,
2038 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292039}
2040
2041TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292042 BrowserList::RemoveObserver(this);
2043}
2044
2045void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:142046 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:292047 AutomationProvider::OnChannelError();
2048}
2049
2050void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2051 // For backwards compatibility with the testing automation interface, we
2052 // want the automation provider (and hence the process) to go away when the
2053 // last browser goes away.
2054 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292055 // If you change this, update Observer for NotificationType::SESSION_END
2056 // below.
[email protected]295039bd2008-08-15 04:32:572057 MessageLoop::current()->PostTask(FROM_HERE,
2058 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292059 }
2060}
2061
2062void TestingAutomationProvider::Observe(NotificationType type,
2063 const NotificationSource& source,
2064 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562065 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292066 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2067 // before the task runs resulting in this object not being deleted. This
2068 // Release balance out the Release scheduled by OnBrowserRemoving.
2069 Release();
2070}
[email protected]295039bd2008-08-15 04:32:572071
2072void TestingAutomationProvider::OnRemoveProvider() {
2073 AutomationProviderList::GetInstance()->RemoveProvider(this);
2074}
[email protected]8a3422c92008-09-24 17:42:422075
[email protected]816633a2009-11-11 21:48:182076void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:562077 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422078 if (tab_tracker_->ContainsHandle(handle)) {
2079 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342080 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112081 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422082 }
[email protected]8a3422c92008-09-24 17:42:422083}
2084
[email protected]816633a2009-11-11 21:48:182085void AutomationProvider::ClickInfoBarAccept(int handle,
2086 int info_bar_index,
2087 bool wait_for_navigation,
2088 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422089 bool success = false;
2090 if (tab_tracker_->ContainsHandle(handle)) {
2091 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2092 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112093 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422094 if (info_bar_index >= 0 && info_bar_index < count) {
2095 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:012096 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:422097 }
[email protected]eb9ba192008-12-02 02:41:342098 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112099 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342100 info_bar_index);
2101 if (delegate->AsConfirmInfoBarDelegate())
2102 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422103 success = true;
2104 }
2105 }
[email protected]4f3dc372009-02-24 00:10:292106 }
[email protected]58f622a62009-10-04 01:17:552107
2108 // This "!wait_for_navigation || !success condition" logic looks suspicious.
2109 // It will send a failure message when success is true but
2110 // |wait_for_navigation| is false.
2111 // TODO(phajdan.jr): investgate whether the reply param (currently
2112 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:422113 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:182114 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:522115 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:422116}
2117
[email protected]71f65dd2009-02-11 19:14:562118void AutomationProvider::GetLastNavigationTime(int handle,
2119 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422120 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562121 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422122}
2123
[email protected]71f65dd2009-02-11 19:14:562124void AutomationProvider::WaitForNavigation(int handle,
2125 int64 last_navigation_time,
2126 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:252127 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:422128 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:252129
[email protected]8a3422c92008-09-24 17:42:422130 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562131 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:522132 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
2133 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:552134 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:292135 return;
[email protected]8a3422c92008-09-24 17:42:422136 }
2137
[email protected]7dad3d5f2010-03-04 00:27:012138 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:422139}
2140
[email protected]71f65dd2009-02-11 19:14:562141void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162142 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562143 int value,
2144 bool* success) {
2145 *success = false;
[email protected]8a3422c92008-09-24 17:42:422146 if (browser_tracker_->ContainsHandle(handle)) {
2147 Browser* browser = browser_tracker_->GetResource(handle);
2148 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562149 *success = true;
[email protected]8a3422c92008-09-24 17:42:422150 }
[email protected]8a3422c92008-09-24 17:42:422151}
[email protected]97fa6ce32008-12-19 01:48:162152
[email protected]71f65dd2009-02-11 19:14:562153void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162154 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562155 const std::wstring& value,
2156 bool* success) {
2157 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162158 if (browser_tracker_->ContainsHandle(handle)) {
2159 Browser* browser = browser_tracker_->GetResource(handle);
2160 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562161 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162162 }
[email protected]97fa6ce32008-12-19 01:48:162163}
2164
[email protected]71f65dd2009-02-11 19:14:562165void AutomationProvider::GetBooleanPreference(int handle,
2166 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:362167 bool* success,
2168 bool* value) {
[email protected]71f65dd2009-02-11 19:14:562169 *success = false;
2170 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162171 if (browser_tracker_->ContainsHandle(handle)) {
2172 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562173 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2174 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162175 }
[email protected]97fa6ce32008-12-19 01:48:162176}
2177
[email protected]71f65dd2009-02-11 19:14:562178void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162179 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562180 bool value,
2181 bool* success) {
2182 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162183 if (browser_tracker_->ContainsHandle(handle)) {
2184 Browser* browser = browser_tracker_->GetResource(handle);
2185 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562186 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162187 }
[email protected]97fa6ce32008-12-19 01:48:162188}
2189
2190// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562191void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:402192 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162193 if (tab_tracker_->ContainsHandle(tab_handle)) {
2194 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2195 Browser* browser = FindAndActivateTab(nav);
2196 DCHECK(browser);
2197
[email protected]57c6a652009-05-04 07:58:342198 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2199 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162200 }
[email protected]97fa6ce32008-12-19 01:48:162201}
2202
[email protected]b8f48d12009-11-09 20:14:362203// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562204void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402205 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562206 bool* success) {
2207 *success = false;
[email protected]de246f52009-02-25 18:25:452208#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:162209 if (tab_tracker_->ContainsHandle(tab_handle)) {
2210 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2211 Browser* browser = FindAndActivateTab(nav);
2212 DCHECK(browser);
2213
[email protected]1fc025202009-01-20 23:03:142214 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162215 int selected_encoding_id =
2216 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2217 if (selected_encoding_id) {
2218 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562219 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162220 }
2221 }
2222 }
[email protected]de246f52009-02-25 18:25:452223#else
2224 // TODO(port): Enable when encoding-related parts of Browser are ported.
2225 NOTIMPLEMENTED();
2226#endif
[email protected]97fa6ce32008-12-19 01:48:162227}
[email protected]5bcdb312009-01-07 21:43:202228
[email protected]4d434a1a2009-02-11 21:06:572229void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202230 SavePackage::SetShouldPromptUser(should_prompt);
2231}
[email protected]87eab222009-03-13 00:47:452232
[email protected]66ba4932009-06-04 19:22:132233void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2234 *count = -1; // -1 is the error code
2235 if (tab_tracker_->ContainsHandle(handle)) {
2236 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2237 TabContents* tab_contents = nav_controller->tab_contents();
2238 if (tab_contents) {
2239 BlockedPopupContainer* container =
2240 tab_contents->blocked_popup_container();
2241 if (container) {
2242 *count = static_cast<int>(container->GetBlockedPopupCount());
2243 } else {
2244 // If we don't have a container, we don't have any blocked popups to
2245 // contain!
2246 *count = 0;
2247 }
2248 }
2249 }
2250}
[email protected]f7a68432009-07-29 23:18:192251
2252void AutomationProvider::SelectAll(int tab_handle) {
2253 RenderViewHost* view = GetViewForTab(tab_handle);
2254 if (!view) {
2255 NOTREACHED();
2256 return;
2257 }
2258
2259 view->SelectAll();
2260}
2261
2262void AutomationProvider::Cut(int tab_handle) {
2263 RenderViewHost* view = GetViewForTab(tab_handle);
2264 if (!view) {
2265 NOTREACHED();
2266 return;
2267 }
2268
2269 view->Cut();
2270}
2271
2272void AutomationProvider::Copy(int tab_handle) {
2273 RenderViewHost* view = GetViewForTab(tab_handle);
2274 if (!view) {
2275 NOTREACHED();
2276 return;
2277 }
2278
2279 view->Copy();
2280}
2281
2282void AutomationProvider::Paste(int tab_handle) {
2283 RenderViewHost* view = GetViewForTab(tab_handle);
2284 if (!view) {
2285 NOTREACHED();
2286 return;
2287 }
2288
2289 view->Paste();
2290}
2291
2292void AutomationProvider::ReloadAsync(int tab_handle) {
2293 if (tab_tracker_->ContainsHandle(tab_handle)) {
2294 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2295 if (!tab) {
2296 NOTREACHED();
2297 return;
2298 }
2299
2300 tab->Reload(false);
2301 }
2302}
2303
2304void AutomationProvider::StopAsync(int tab_handle) {
2305 RenderViewHost* view = GetViewForTab(tab_handle);
2306 if (!view) {
[email protected]8b2b3312009-09-14 18:38:362307 // We tolerate StopAsync being called even before a view has been created.
2308 // So just log a warning instead of a NOTREACHED().
2309 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:192310 return;
2311 }
2312
2313 view->Stop();
2314}
2315
[email protected]1bb5f892009-10-06 01:44:572316void AutomationProvider::OnSetPageFontSize(int tab_handle,
2317 int font_size) {
2318 AutomationPageFontSize automation_font_size =
2319 static_cast<AutomationPageFontSize>(font_size);
2320
2321 if (automation_font_size < SMALLEST_FONT ||
2322 automation_font_size > LARGEST_FONT) {
2323 DLOG(ERROR) << "Invalid font size specified : "
2324 << font_size;
2325 return;
2326 }
2327
2328 if (tab_tracker_->ContainsHandle(tab_handle)) {
2329 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2330 DCHECK(tab != NULL);
2331 if (tab && tab->tab_contents()) {
2332 DCHECK(tab->tab_contents()->profile() != NULL);
2333 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2334 prefs::kWebKitDefaultFontSize, font_size);
2335 }
2336 }
2337}
2338
2339
[email protected]2949e90d2009-08-21 15:32:522340void AutomationProvider::WaitForBrowserWindowCountToBecome(
2341 int target_count, IPC::Message* reply_message) {
2342 if (static_cast<int>(BrowserList::size()) == target_count) {
2343 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2344 reply_message, true);
2345 Send(reply_message);
2346 return;
2347 }
2348
2349 // Set up an observer (it will delete itself).
2350 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2351}
2352
2353void AutomationProvider::WaitForAppModalDialogToBeShown(
2354 IPC::Message* reply_message) {
2355 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2356 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2357 reply_message, true);
2358 Send(reply_message);
2359 return;
2360 }
2361
2362 // Set up an observer (it will delete itself).
2363 new AppModalDialogShownObserver(this, reply_message);
2364}
2365
[email protected]1126a1d32009-08-26 15:39:262366void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2367 int handle, int number_of_navigations, IPC::Message* reply_message) {
2368 if (tab_tracker_->ContainsHandle(handle)) {
2369 NavigationController* tab = tab_tracker_->GetResource(handle);
2370 Browser* browser = FindAndActivateTab(tab);
2371 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:012372 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2373 false);
[email protected]1126a1d32009-08-26 15:39:262374 browser->GoBack(CURRENT_TAB);
2375 return;
2376 }
2377 }
2378
2379 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2380 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2381 Send(reply_message);
2382}
2383
2384void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2385 int handle, int number_of_navigations, IPC::Message* reply_message) {
2386 if (tab_tracker_->ContainsHandle(handle)) {
2387 NavigationController* tab = tab_tracker_->GetResource(handle);
2388 Browser* browser = FindAndActivateTab(tab);
2389 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:012390 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2391 false);
[email protected]1126a1d32009-08-26 15:39:262392 browser->GoForward(CURRENT_TAB);
2393 return;
2394 }
2395 }
2396
2397 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2398 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2399 Send(reply_message);
2400}
2401
[email protected]f7a68432009-07-29 23:18:192402RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2403 if (tab_tracker_->ContainsHandle(tab_handle)) {
2404 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2405 if (!tab) {
2406 NOTREACHED();
2407 return NULL;
2408 }
2409
2410 TabContents* tab_contents = tab->tab_contents();
2411 if (!tab_contents) {
2412 NOTREACHED();
2413 return NULL;
2414 }
2415
2416 RenderViewHost* view_host = tab_contents->render_view_host();
2417 return view_host;
2418 }
2419
2420 return NULL;
2421}
[email protected]675595f2009-08-26 22:32:042422
2423void AutomationProvider::GetBrowserForWindow(int window_handle,
2424 bool* success,
2425 int* browser_handle) {
2426 *success = false;
2427 *browser_handle = 0;
2428
2429 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2430 if (!window)
2431 return;
2432
2433 BrowserList::const_iterator iter = BrowserList::begin();
2434 for (;iter != BrowserList::end(); ++iter) {
2435 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2436 if (window == this_window) {
2437 // Add() returns the existing handle for the resource if any.
2438 *browser_handle = browser_tracker_->Add(*iter);
2439 *success = true;
2440 return;
2441 }
2442 }
2443}
[email protected]d11c8e92009-10-20 23:26:402444
2445void AutomationProvider::InstallExtension(const FilePath& crx_path,
2446 IPC::Message* reply_message) {
2447 ExtensionsService* service = profile_->GetExtensionsService();
2448 if (service) {
2449 // The observer will delete itself when done.
2450 new ExtensionNotificationObserver(this,
2451 AutomationMsg_InstallExtension::ID,
2452 reply_message);
2453
2454 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:162455 scoped_refptr<CrxInstaller> installer(
2456 new CrxInstaller(install_dir,
2457 service,
2458 NULL)); // silent install, no UI
2459 installer->set_allow_privilege_increase(true);
2460 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:402461 } else {
2462 AutomationMsg_InstallExtension::WriteReplyParams(
2463 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2464 Send(reply_message);
2465 }
2466}
2467
2468void AutomationProvider::LoadExpandedExtension(
2469 const FilePath& extension_dir,
2470 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:382471 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:402472 // The observer will delete itself when done.
2473 new ExtensionNotificationObserver(this,
2474 AutomationMsg_LoadExpandedExtension::ID,
2475 reply_message);
2476
2477 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:402478 } else {
2479 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
2480 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2481 Send(reply_message);
2482 }
2483}
[email protected]673fd2c02010-02-04 23:10:002484
2485void AutomationProvider::SaveAsAsync(int tab_handle) {
2486 NavigationController* tab = NULL;
2487 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2488 if (tab_contents)
2489 tab_contents->OnSavePage();
2490}
[email protected]7dad3d5f2010-03-04 00:27:012491
2492void AutomationProvider::SetContentSetting(
2493 int handle,
2494 const std::string& host,
2495 ContentSettingsType content_type,
2496 ContentSetting setting,
2497 bool* success) {
2498 *success = false;
2499 if (browser_tracker_->ContainsHandle(handle)) {
2500 Browser* browser = browser_tracker_->GetResource(handle);
2501 HostContentSettingsMap* map =
2502 browser->profile()->GetHostContentSettingsMap();
2503 if (host.empty()) {
2504 map->SetDefaultContentSetting(content_type, setting);
2505 } else {
2506 map->SetContentSetting(host, content_type, setting);
2507 }
2508 *success = true;
2509 }
2510}