blob: a2b7d8d66a82577a6c39dc448ba8efe4e5814ff2 [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]a1e62d12010-03-16 02:18:43487 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
488 GetEnabledExtensions)
[email protected]fedaa7d2010-01-26 20:34:57489 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
490 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00491 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]2b19e2fe2010-02-16 02:24:18492#if defined(OS_WIN)
493 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
494#endif
[email protected]7dad3d5f2010-03-04 00:27:01495 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]566a0f762010-03-10 04:14:57496#if defined(OS_CHROMEOS)
497 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
498 LoginWithUserAndPass)
499#endif
initial.commit09911bf2008-07-26 23:55:29500 IPC_END_MESSAGE_MAP()
501}
502
[email protected]71f65dd2009-02-11 19:14:56503void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
504 *status = -1;
initial.commit09911bf2008-07-26 23:55:29505 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
506 Browser* browser = browser_tracker_->GetResource(handle);
507 if (at_index >= 0 && at_index < browser->tab_count()) {
508 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56509 *status = 0;
initial.commit09911bf2008-07-26 23:55:29510 }
511 }
initial.commit09911bf2008-07-26 23:55:29512}
513
[email protected]71f65dd2009-02-11 19:14:56514void AutomationProvider::AppendTab(int handle, const GURL& url,
515 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29516 int append_tab_response = -1; // -1 is the error code
517 NotificationObserver* observer = NULL;
518
519 if (browser_tracker_->ContainsHandle(handle)) {
520 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14521 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58522 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
523 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39524 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29525 if (tab_contents) {
526 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57527 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29528 }
529 }
530
531 if (append_tab_response < 0) {
532 // The append tab failed. Remove the TabStripObserver
533 if (observer) {
534 RemoveTabStripObserver(observer);
535 delete observer;
536 }
537
[email protected]71f65dd2009-02-11 19:14:56538 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
539 append_tab_response);
540 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29541 }
542}
543
[email protected]71f65dd2009-02-11 19:14:56544void AutomationProvider::NavigateToURL(int handle, const GURL& url,
545 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58546 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
547}
548
549void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
550 int handle, const GURL& url, int number_of_navigations,
551 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29552 if (tab_tracker_->ContainsHandle(handle)) {
553 NavigationController* tab = tab_tracker_->GetResource(handle);
554
555 // Simulate what a user would do. Activate the tab and then navigate.
556 // We could allow navigating in a background tab in future.
557 Browser* browser = FindAndActivateTab(tab);
558
559 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01560 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
561 false);
[email protected]71f65dd2009-02-11 19:14:56562
initial.commit09911bf2008-07-26 23:55:29563 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50564 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29565 return;
566 }
567 }
[email protected]71f65dd2009-02-11 19:14:56568
569 AutomationMsg_NavigateToURL::WriteReplyParams(
570 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
571 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29572}
[email protected]2949e90d2009-08-21 15:32:52573
[email protected]71f65dd2009-02-11 19:14:56574void AutomationProvider::NavigationAsync(int handle, const GURL& url,
575 bool* status) {
576 *status = false;
initial.commit09911bf2008-07-26 23:55:29577
578 if (tab_tracker_->ContainsHandle(handle)) {
579 NavigationController* tab = tab_tracker_->GetResource(handle);
580
581 // Simulate what a user would do. Activate the tab and then navigate.
582 // We could allow navigating in a background tab in future.
583 Browser* browser = FindAndActivateTab(tab);
584
585 if (browser) {
586 // Don't add any listener unless a callback mechanism is desired.
587 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50588 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56589 *status = true;
initial.commit09911bf2008-07-26 23:55:29590 }
591 }
initial.commit09911bf2008-07-26 23:55:29592}
593
[email protected]71f65dd2009-02-11 19:14:56594void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29595 if (tab_tracker_->ContainsHandle(handle)) {
596 NavigationController* tab = tab_tracker_->GetResource(handle);
597 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14598 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01599 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29600 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29601 return;
602 }
603 }
[email protected]71f65dd2009-02-11 19:14:56604
605 AutomationMsg_GoBack::WriteReplyParams(
606 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
607 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29608}
609
[email protected]71f65dd2009-02-11 19:14:56610void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29611 if (tab_tracker_->ContainsHandle(handle)) {
612 NavigationController* tab = tab_tracker_->GetResource(handle);
613 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14614 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01615 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29616 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29617 return;
618 }
619 }
[email protected]71f65dd2009-02-11 19:14:56620
621 AutomationMsg_GoForward::WriteReplyParams(
622 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
623 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29624}
625
[email protected]71f65dd2009-02-11 19:14:56626void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29627 if (tab_tracker_->ContainsHandle(handle)) {
628 NavigationController* tab = tab_tracker_->GetResource(handle);
629 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14630 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01631 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29632 browser->Reload();
633 return;
634 }
635 }
[email protected]71f65dd2009-02-11 19:14:56636
637 AutomationMsg_Reload::WriteReplyParams(
638 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
639 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29640}
641
[email protected]71f65dd2009-02-11 19:14:56642void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29643 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56644 const std::wstring& password,
645 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29646 if (tab_tracker_->ContainsHandle(tab_handle)) {
647 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
648 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
649
650 if (iter != login_handler_map_.end()) {
651 // If auth is needed again after this, assume login has failed. This is
652 // not strictly correct, because a navigation can require both proxy and
653 // server auth, but it should be OK for now.
654 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01655 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29656 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52657 return;
initial.commit09911bf2008-07-26 23:55:29658 }
659 }
[email protected]de246f52009-02-25 18:25:45660
[email protected]457f5cf2009-08-18 16:37:52661 AutomationMsg_SetAuth::WriteReplyParams(
662 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
663 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29664}
665
[email protected]71f65dd2009-02-11 19:14:56666void AutomationProvider::CancelAuth(int tab_handle,
667 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29668 if (tab_tracker_->ContainsHandle(tab_handle)) {
669 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
670 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
671
672 if (iter != login_handler_map_.end()) {
673 // If auth is needed again after this, something is screwy.
674 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01675 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29676 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52677 return;
initial.commit09911bf2008-07-26 23:55:29678 }
679 }
[email protected]de246f52009-02-25 18:25:45680
[email protected]457f5cf2009-08-18 16:37:52681 AutomationMsg_CancelAuth::WriteReplyParams(
682 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
683 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29684}
685
[email protected]71f65dd2009-02-11 19:14:56686void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
687 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29688
689 if (tab_tracker_->ContainsHandle(tab_handle)) {
690 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
691 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
692
693 if (iter != login_handler_map_.end()) {
694 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56695 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29696 }
697 }
initial.commit09911bf2008-07-26 23:55:29698}
699
[email protected]71f65dd2009-02-11 19:14:56700void AutomationProvider::GetRedirectsFrom(int tab_handle,
701 const GURL& source_url,
702 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29703 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
704 if (tab_tracker_->ContainsHandle(tab_handle)) {
705 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
706 HistoryService* history_service =
707 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
708
709 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
710 "has no history service";
711 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56712 DCHECK(reply_message_ == NULL);
713 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29714 // Schedule a history query for redirects. The response will be sent
715 // asynchronously from the callback the history system uses to notify us
716 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29717 redirect_query_ = history_service->QueryRedirectsFrom(
718 source_url, &consumer_,
719 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
720 return; // Response will be sent when query completes.
721 }
722 }
723
724 // Send failure response.
[email protected]deb57402009-02-06 01:35:30725 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56726 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
727 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29728}
729
[email protected]71f65dd2009-02-11 19:14:56730void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
731 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29732 if (browser_tracker_->ContainsHandle(handle)) {
733 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56734 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29735 }
initial.commit09911bf2008-07-26 23:55:29736}
737
[email protected]202e7a72009-06-15 03:48:36738void AutomationProvider::GetBrowserLocale(string16* locale) {
739 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06740 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36741}
742
[email protected]71f65dd2009-02-11 19:14:56743void AutomationProvider::GetBrowserWindowCount(int* window_count) {
744 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29745}
746
[email protected]24497032009-05-01 17:00:29747void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
748 *window_count = static_cast<int>(
749 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
750}
751
[email protected]71f65dd2009-02-11 19:14:56752void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
753 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07754 AppModalDialog* dialog_delegate =
755 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50756 *showing_dialog = (dialog_delegate != NULL);
757 if (*showing_dialog)
758 *dialog_button = dialog_delegate->GetDialogButtons();
759 else
[email protected]478ff2ed2009-04-21 23:49:18760 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20761}
762
[email protected]71f65dd2009-02-11 19:14:56763void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
764 *success = false;
[email protected]fad84eab2008-12-05 00:37:20765
[email protected]1f460072009-05-28 17:02:07766 AppModalDialog* dialog_delegate =
767 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50768 if (dialog_delegate &&
769 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18770 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
771 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28772 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56773 *success = true;
[email protected]fad84eab2008-12-05 00:37:20774 }
[email protected]478ff2ed2009-04-21 23:49:18775 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
776 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56777 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28778 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56779 *success = true;
[email protected]fad84eab2008-12-05 00:37:20780 }
781 }
[email protected]c274acc2008-11-11 20:13:44782}
783
[email protected]fedaa7d2010-01-26 20:34:57784void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
785 if (browser_tracker_->ContainsHandle(handle)) {
786 Browser* browser = browser_tracker_->GetResource(handle);
787 browser->profile()->ShutdownSessionService();
788 *result = true;
789 } else {
790 *result = false;
791 }
792}
793
[email protected]71f65dd2009-02-11 19:14:56794void AutomationProvider::GetBrowserWindow(int index, int* handle) {
795 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29796 if (index >= 0) {
797 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29798 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29799 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56800 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29801 }
802 }
initial.commit09911bf2008-07-26 23:55:29803}
804
[email protected]24497032009-05-01 17:00:29805void AutomationProvider::FindNormalBrowserWindow(int* handle) {
806 *handle = 0;
807 Browser* browser = BrowserList::FindBrowserWithType(profile_,
808 Browser::TYPE_NORMAL);
809 if (browser)
810 *handle = browser_tracker_->Add(browser);
811}
812
[email protected]71f65dd2009-02-11 19:14:56813void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
814 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29815 Browser* browser = BrowserList::GetLastActive();
816 if (browser)
[email protected]71f65dd2009-02-11 19:14:56817 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29818}
819
[email protected]b2aa3ed72010-02-01 18:37:14820#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28821// TODO(estade): use this implementation for all platforms?
822void AutomationProvider::GetActiveWindow(int* handle) {
823 gfx::NativeWindow window =
824 BrowserList::GetLastActive()->window()->GetNativeHandle();
825 *handle = window_tracker_->Add(window);
826}
827#endif
828
[email protected]4f6381ee2009-04-16 02:46:33829void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
830 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56831 *success = false;
[email protected]4ae62752008-08-04 23:28:47832 if (browser_tracker_->ContainsHandle(handle)) {
833 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14834 if (browser->command_updater()->SupportsCommand(command) &&
835 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47836 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56837 *success = true;
[email protected]4ae62752008-08-04 23:28:47838 }
839 }
[email protected]4ae62752008-08-04 23:28:47840}
841
[email protected]4f6381ee2009-04-16 02:46:33842void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56843 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53844 // List of commands which just finish synchronously and don't require
845 // setting up an observer.
846 static const int kSynchronousCommands[] = {
847 IDC_HOME,
848 IDC_SELECT_NEXT_TAB,
849 IDC_SELECT_PREVIOUS_TAB,
850 IDC_SHOW_DOWNLOADS,
851 IDC_SHOW_HISTORY,
852 };
[email protected]56e71b7c2009-03-27 03:05:56853 if (browser_tracker_->ContainsHandle(handle)) {
854 Browser* browser = browser_tracker_->GetResource(handle);
855 if (browser->command_updater()->SupportsCommand(command) &&
856 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53857 // First check if we can handle the command without using an observer.
858 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
859 if (command == kSynchronousCommands[i]) {
860 browser->ExecuteCommand(command);
861 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
862 true);
863 Send(reply_message);
864 return;
865 }
866 }
867
868 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42869 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
870 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27871 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42872 return;
873 }
[email protected]56e71b7c2009-03-27 03:05:56874 }
875 }
[email protected]49a14a82009-03-31 04:16:44876 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56877 Send(reply_message);
878}
879
[email protected]fc2e0872009-08-21 22:14:41880// This task just adds another task to the event queue. This is useful if
881// you want to ensure that any tasks added to the event queue after this one
882// have already been processed by the time |task| is run.
883class InvokeTaskLaterTask : public Task {
884 public:
885 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
886 virtual ~InvokeTaskLaterTask() {}
887
888 virtual void Run() {
889 MessageLoop::current()->PostTask(FROM_HERE, task_);
890 }
891
892 private:
893 Task* task_;
894
895 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
896};
897
[email protected]b2aa3ed72010-02-01 18:37:14898#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29899void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
900 int handle,
[email protected]d1a5941e2009-08-13 23:34:24901 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29902 int flags) {
[email protected]b410bc32009-08-14 01:11:14903 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51904 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41905 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29906 }
907}
908
[email protected]60507b12009-11-02 23:51:35909void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
910 int handle,
911 const gfx::Point& location) {
912 if (window_tracker_->ContainsHandle(handle))
913 ui_controls::SendMouseMove(location.x(), location.y());
914}
915
initial.commit09911bf2008-07-26 23:55:29916void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
917 int handle,
[email protected]bc1407f2009-09-29 00:33:35918 int key,
initial.commit09911bf2008-07-26 23:55:29919 int flags) {
[email protected]b410bc32009-08-14 01:11:14920 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29921 return;
922
[email protected]b410bc32009-08-14 01:11:14923 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29924 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35925 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38926 ((flags & views::Event::EF_CONTROL_DOWN) ==
927 views::Event::EF_CONTROL_DOWN),
928 ((flags & views::Event::EF_SHIFT_DOWN) ==
929 views::Event::EF_SHIFT_DOWN),
930 ((flags & views::Event::EF_ALT_DOWN) ==
931 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29932}
[email protected]b2aa3ed72010-02-01 18:37:14933#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29934
[email protected]71f65dd2009-02-11 19:14:56935void AutomationProvider::IsWindowActive(int handle, bool* success,
936 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29937 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17938 *is_active =
939 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56940 *success = true;
initial.commit09911bf2008-07-26 23:55:29941 } else {
[email protected]71f65dd2009-02-11 19:14:56942 *success = false;
943 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29944 }
945}
946
[email protected]71f65dd2009-02-11 19:14:56947void AutomationProvider::GetTabCount(int handle, int* tab_count) {
948 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29949
950 if (browser_tracker_->ContainsHandle(handle)) {
951 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56952 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29953 }
initial.commit09911bf2008-07-26 23:55:29954}
955
[email protected]982921f12009-10-27 21:43:53956void AutomationProvider::GetType(int handle, int* type_as_int) {
957 *type_as_int = -1; // -1 is the error code
958
959 if (browser_tracker_->ContainsHandle(handle)) {
960 Browser* browser = browser_tracker_->GetResource(handle);
961 *type_as_int = static_cast<int>(browser->type());
962 }
963}
964
[email protected]71f65dd2009-02-11 19:14:56965void AutomationProvider::GetTab(int win_handle, int tab_index,
966 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56967 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29968 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
969 Browser* browser = browser_tracker_->GetResource(win_handle);
970 if (tab_index < browser->tab_count()) {
971 TabContents* tab_contents =
972 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57973 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29974 }
975 }
initial.commit09911bf2008-07-26 23:55:29976}
977
[email protected]71f65dd2009-02-11 19:14:56978void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
979 std::wstring* title) {
980 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29981 if (tab_tracker_->ContainsHandle(handle)) {
982 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36983 NavigationEntry* entry = tab->GetActiveEntry();
984 if (entry != NULL) {
985 *title = UTF16ToWideHack(entry->title());
986 } else {
987 *title = std::wstring();
988 }
[email protected]71f65dd2009-02-11 19:14:56989 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:29990 }
initial.commit09911bf2008-07-26 23:55:29991}
992
[email protected]77bc6732009-04-20 22:01:03993void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
994 *tabstrip_index = -1; // -1 is the error code
995
996 if (tab_tracker_->ContainsHandle(handle)) {
997 NavigationController* tab = tab_tracker_->GetResource(handle);
998 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:12999 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031000 }
1001}
1002
initial.commit09911bf2008-07-26 23:55:291003void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1004 if (window_tracker_->ContainsHandle(handle)) {
1005 window_tracker_->Remove(window_tracker_->GetResource(handle));
1006 }
1007}
1008
1009void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161010 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571011 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291012}
1013
1014// TODO(brettw) change this to accept GURLs when history supports it
1015void AutomationProvider::OnRedirectQueryComplete(
1016 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371017 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291018 bool success,
[email protected]379c2b12009-07-01 21:50:331019 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291020 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561021 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291022
[email protected]deb57402009-02-06 01:35:301023 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101024 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291025 if (success) {
initial.commit09911bf2008-07-26 23:55:291026 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301027 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291028 }
1029
[email protected]4f3dc372009-02-24 00:10:291030 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301031
[email protected]71f65dd2009-02-11 19:14:561032 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:171033 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561034 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291035}
1036
1037bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571038 DCHECK(channel_.get());
1039 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291040}
1041
1042Browser* AutomationProvider::FindAndActivateTab(
1043 NavigationController* controller) {
1044 int tab_index;
1045 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1046 if (browser)
1047 browser->SelectTabContentsAt(tab_index, true);
1048
1049 return browser;
1050}
1051
[email protected]71f65dd2009-02-11 19:14:561052void AutomationProvider::GetCookies(const GURL& url, int handle,
1053 int* value_size,
1054 std::string* value) {
1055 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291056 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1057 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311058
1059 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001060 scoped_refptr<URLRequestContextGetter> request_context =
1061 tab->tab_contents()->request_context();
1062 if (!request_context.get())
1063 request_context = tab->profile()->GetRequestContext();
1064
1065 net::CookieStore* cookie_store = request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311066
1067 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561068 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291069 }
initial.commit09911bf2008-07-26 23:55:291070}
1071
[email protected]71f65dd2009-02-11 19:14:561072void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291073 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561074 int handle,
1075 int* response_value) {
1076 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291077
1078 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1079 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311080
[email protected]dfa46e5f2009-11-17 18:48:431081 scoped_refptr<URLRequestContextGetter> request_context =
1082 tab->tab_contents()->request_context();
1083 if (!request_context.get())
1084 request_context = tab->profile()->GetRequestContext();
1085
[email protected]be180c802009-10-23 06:33:311086 // Since we are running on the UI thread don't call GetURLRequestContext().
1087 scoped_refptr<net::CookieStore> cookie_store =
[email protected]dfa46e5f2009-11-17 18:48:431088 request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311089
1090 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561091 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291092 }
initial.commit09911bf2008-07-26 23:55:291093}
1094
[email protected]71f65dd2009-02-11 19:14:561095void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1096 *success = false;
initial.commit09911bf2008-07-26 23:55:291097 if (tab_tracker_->ContainsHandle(handle)) {
1098 NavigationController* tab = tab_tracker_->GetResource(handle);
1099 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541100 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561101 *success = true;
initial.commit09911bf2008-07-26 23:55:291102 }
initial.commit09911bf2008-07-26 23:55:291103}
1104
[email protected]71f65dd2009-02-11 19:14:561105void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1106 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291107
1108 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561109 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341110 TabContents* tab_contents =
1111 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161112 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1113 if (rph)
1114 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291115 }
initial.commit09911bf2008-07-26 23:55:291116}
1117
1118void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331119 NOTREACHED() << "This function has been deprecated. "
1120 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291121}
1122
[email protected]71f65dd2009-02-11 19:14:561123void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291124 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561125 const std::wstring& script,
1126 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291127 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341128 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1129 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571130 // Set the routing id of this message with the controller.
1131 // This routing id needs to be remembered for the reverse
1132 // communication while sending back the response of
1133 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331134 std::wstring set_automation_id;
1135 SStringPrintf(&set_automation_id,
1136 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561137 reply_message->routing_id());
1138
1139 DCHECK(reply_message_ == NULL);
1140 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291141
[email protected]57c6a652009-05-04 07:58:341142 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331143 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341144 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061145 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571146 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291147 }
1148
1149 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561150 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1151 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291152 }
1153}
1154
[email protected]71f65dd2009-02-11 19:14:561155void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1156 *visible = false;
[email protected]20e93d12008-08-28 16:31:571157
[email protected]59560e0b2009-06-04 03:30:221158 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251159#if defined(OS_CHROMEOS)
1160 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1161 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1162 for (BrowserList::const_iterator it = BrowserList::begin();
1163 it != BrowserList::end(); ++it) {
1164 if ((*it)->type() == Browser::TYPE_POPUP) {
1165 const GURL& url =
1166 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1167
1168 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1169 url.host() == chrome::kChromeUIFileBrowseHost) {
1170 *visible = true;
1171 break;
1172 }
1173 }
1174 }
1175#else
[email protected]59560e0b2009-06-04 03:30:221176 Browser* browser = browser_tracker_->GetResource(handle);
1177 if (browser) {
1178 *visible = browser->window()->IsDownloadShelfVisible();
1179 }
[email protected]f5bf8ccf2010-02-05 18:19:251180#endif
[email protected]59560e0b2009-06-04 03:30:221181 }
initial.commit09911bf2008-07-26 23:55:291182}
1183
[email protected]59560e0b2009-06-04 03:30:221184void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1185 if (browser_tracker_->ContainsHandle(handle)) {
1186 Browser* browser = browser_tracker_->GetResource(handle);
1187 if (browser) {
1188 if (visible)
1189 browser->window()->GetDownloadShelf()->Show();
1190 else
1191 browser->window()->GetDownloadShelf()->Close();
1192 }
1193 }
1194}
1195
[email protected]34930432009-11-09 00:12:091196void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1197 *visible = false;
1198
1199 if (browser_tracker_->ContainsHandle(handle)) {
1200 Browser* browser = browser_tracker_->GetResource(handle);
1201 if (browser)
1202 *visible = browser->window()->IsFullscreen();
1203 }
1204}
1205
1206void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1207 bool* visible) {
1208 *visible = false;
1209
1210 if (browser_tracker_->ContainsHandle(handle)) {
1211 Browser* browser = browser_tracker_->GetResource(handle);
1212 if (browser)
1213 *visible = browser->window()->IsFullscreenBubbleVisible();
1214 }
1215}
[email protected]59560e0b2009-06-04 03:30:221216
[email protected]71f65dd2009-02-11 19:14:561217void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1218 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291219 if (tab_tracker_->ContainsHandle(handle)) {
1220 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111221 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291222 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561223 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291224 }
1225 }
initial.commit09911bf2008-07-26 23:55:291226}
1227
initial.commit09911bf2008-07-26 23:55:291228void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561229 int handle, const std::wstring& find_request,
1230 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311231 NOTREACHED() << "This function has been deprecated."
1232 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561233 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311234 return;
1235}
1236
[email protected]4f999132009-03-31 18:08:401237void AutomationProvider::HandleFindRequest(
1238 int handle,
1239 const AutomationMsg_Find_Params& params,
1240 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291241 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561242 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1243 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291244 return;
1245 }
1246
1247 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111248 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291249
1250 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141251 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291252
[email protected]57c6a652009-05-04 07:58:341253 tab_contents->set_current_find_request_id(
1254 FindInPageNotificationObserver::kFindInPageRequestId);
1255 tab_contents->render_view_host()->StartFinding(
1256 FindInPageNotificationObserver::kFindInPageRequestId,
1257 params.search_string, params.forward, params.match_case,
1258 params.find_next);
initial.commit09911bf2008-07-26 23:55:291259}
1260
[email protected]5f8af2a2008-08-06 22:49:451261void AutomationProvider::HandleOpenFindInPageRequest(
1262 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291263 if (browser_tracker_->ContainsHandle(handle)) {
1264 Browser* browser = browser_tracker_->GetResource(handle);
1265 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451266 }
1267}
1268
[email protected]71f65dd2009-02-11 19:14:561269void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561270 *visible = false;
[email protected]855c0142009-09-28 22:35:241271 Browser* browser = browser_tracker_->GetResource(handle);
1272 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581273 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171274 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241275 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291276 }
[email protected]20e93d12008-08-28 16:31:571277}
1278
[email protected]71f65dd2009-02-11 19:14:561279void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1280 int* y) {
[email protected]9e0534b2008-10-21 15:03:011281 gfx::Point position(0, 0);
1282 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291283 if (browser_tracker_->ContainsHandle(handle)) {
1284 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581285 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171286 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581287 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291288 }
[email protected]20e93d12008-08-28 16:31:571289
[email protected]71f65dd2009-02-11 19:14:561290 *x = position.x();
1291 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571292}
1293
[email protected]c3240722010-03-05 21:52:581294void AutomationProvider::GetBookmarkBarVisibility(int handle,
1295 bool* visible,
1296 bool* animating) {
1297 *visible = false;
1298 *animating = false;
1299
1300 if (browser_tracker_->ContainsHandle(handle)) {
1301 Browser* browser = browser_tracker_->GetResource(handle);
1302 if (browser) {
1303 *visible = browser->window()->IsBookmarkBarVisible();
1304 *animating = browser->window()->IsBookmarkBarAnimating();
1305 }
1306 }
1307}
1308
[email protected]6d8ffc9f2010-03-12 18:27:531309void AutomationProvider::GetBookmarksAsJSON(int handle,
1310 std::string* bookmarks_as_json,
1311 bool *success) {
1312 *success = false;
1313 if (browser_tracker_->ContainsHandle(handle)) {
1314 Browser* browser = browser_tracker_->GetResource(handle);
1315 if (browser) {
1316 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1317 return;
1318 }
1319 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1320 browser->profile(),
1321 browser->profile()->GetBookmarkModel());
1322 *success = storage->SerializeData(bookmarks_as_json);
1323 }
1324 }
1325}
1326
1327void AutomationProvider::WaitForBookmarkModelToLoad(
1328 int handle,
1329 IPC::Message* reply_message) {
1330 if (browser_tracker_->ContainsHandle(handle)) {
1331 Browser* browser = browser_tracker_->GetResource(handle);
1332 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1333 if (model->IsLoaded()) {
1334 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1335 reply_message, true);
1336 Send(reply_message);
1337 } else {
1338 // The observer will delete itself when done.
1339 new AutomationProviderBookmarkModelObserver(this, reply_message,
1340 model);
1341 }
1342 }
1343}
1344
1345void AutomationProvider::AddBookmarkGroup(int handle,
1346 int64 parent_id, int index,
1347 std::wstring title,
1348 bool* success) {
1349 if (browser_tracker_->ContainsHandle(handle)) {
1350 Browser* browser = browser_tracker_->GetResource(handle);
1351 if (browser) {
1352 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1353 if (!model->IsLoaded()) {
1354 *success = false;
1355 return;
1356 }
1357 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1358 DCHECK(parent);
1359 if (parent) {
1360 const BookmarkNode* child = model->AddGroup(parent, index,
1361 WideToUTF16(title));
1362 DCHECK(child);
1363 if (child)
1364 *success = true;
1365 }
1366 }
1367 }
1368 *success = false;
1369}
1370
1371void AutomationProvider::AddBookmarkURL(int handle,
1372 int64 parent_id, int index,
1373 std::wstring title, const GURL& url,
1374 bool* success) {
1375 if (browser_tracker_->ContainsHandle(handle)) {
1376 Browser* browser = browser_tracker_->GetResource(handle);
1377 if (browser) {
1378 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1379 if (!model->IsLoaded()) {
1380 *success = false;
1381 return;
1382 }
1383 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1384 DCHECK(parent);
1385 if (parent) {
1386 const BookmarkNode* child = model->AddURL(parent, index,
1387 WideToUTF16(title), url);
1388 DCHECK(child);
1389 if (child)
1390 *success = true;
1391 }
1392 }
1393 }
1394 *success = false;
1395}
1396
1397void AutomationProvider::ReparentBookmark(int handle,
1398 int64 id, int64 new_parent_id,
1399 int index,
1400 bool* success) {
1401 if (browser_tracker_->ContainsHandle(handle)) {
1402 Browser* browser = browser_tracker_->GetResource(handle);
1403 if (browser) {
1404 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1405 if (!model->IsLoaded()) {
1406 *success = false;
1407 return;
1408 }
1409 const BookmarkNode* node = model->GetNodeByID(id);
1410 DCHECK(node);
1411 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1412 DCHECK(new_parent);
1413 if (node && new_parent) {
1414 model->Move(node, new_parent, index);
1415 *success = true;
1416 }
1417 }
1418 }
1419 *success = false;
1420}
1421
1422void AutomationProvider::SetBookmarkTitle(int handle,
1423 int64 id, std::wstring title,
1424 bool* success) {
1425 if (browser_tracker_->ContainsHandle(handle)) {
1426 Browser* browser = browser_tracker_->GetResource(handle);
1427 if (browser) {
1428 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1429 if (!model->IsLoaded()) {
1430 *success = false;
1431 return;
1432 }
1433 const BookmarkNode* node = model->GetNodeByID(id);
1434 DCHECK(node);
1435 if (node) {
1436 model->SetTitle(node, WideToUTF16(title));
1437 *success = true;
1438 }
1439 }
1440 }
1441 *success = false;
1442}
1443
1444void AutomationProvider::SetBookmarkURL(int handle,
1445 int64 id, const GURL& url,
1446 bool* success) {
1447 if (browser_tracker_->ContainsHandle(handle)) {
1448 Browser* browser = browser_tracker_->GetResource(handle);
1449 if (browser) {
1450 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1451 if (!model->IsLoaded()) {
1452 *success = false;
1453 return;
1454 }
1455 const BookmarkNode* node = model->GetNodeByID(id);
1456 DCHECK(node);
1457 if (node) {
1458 model->SetURL(node, url);
1459 *success = true;
1460 }
1461 }
1462 }
1463 *success = false;
1464}
1465
1466void AutomationProvider::RemoveBookmark(int handle,
1467 int64 id,
1468 bool* success) {
1469 if (browser_tracker_->ContainsHandle(handle)) {
1470 Browser* browser = browser_tracker_->GetResource(handle);
1471 if (browser) {
1472 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1473 if (!model->IsLoaded()) {
1474 *success = false;
1475 return;
1476 }
1477 const BookmarkNode* node = model->GetNodeByID(id);
1478 DCHECK(node);
1479 if (node) {
1480 const BookmarkNode* parent = node->GetParent();
1481 DCHECK(parent);
1482 model->Remove(parent, parent->IndexOfChild(node));
1483 *success = true;
1484 }
1485 }
1486 }
1487 *success = false;
1488}
1489
initial.commit09911bf2008-07-26 23:55:291490void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561491 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341492 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1493 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561494 DCHECK(reply_message_ == NULL);
1495 reply_message_ = reply_message;
1496
[email protected]d9f9b792009-06-24 13:17:121497 DevToolsManager::GetInstance()->InspectElement(
1498 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291499 } else {
[email protected]71f65dd2009-02-11 19:14:561500 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1501 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291502 }
1503}
1504
1505void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091506 if (reply_message_) {
1507 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1508 num_resources);
1509 Send(reply_message_);
1510 reply_message_ = NULL;
1511 }
initial.commit09911bf2008-07-26 23:55:291512}
1513
[email protected]a7eee32f2009-05-22 18:08:171514class SetProxyConfigTask : public Task {
1515 public:
[email protected]be180c802009-10-23 06:33:311516 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
1517 const std::string& new_proxy_config)
1518 : request_context_getter_(request_context_getter), proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:171519 virtual void Run() {
1520 // First, deserialize the JSON string. If this fails, log and bail.
1521 JSONStringValueSerializer deserializer(proxy_config_);
1522 std::string error_message;
1523 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1524 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1525 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1526 << error_message;
1527 return;
1528 }
1529
1530 scoped_ptr<DictionaryValue> dict(
1531 static_cast<DictionaryValue*>(root.release()));
1532 // Now put together a proxy configuration from the deserialized string.
1533 net::ProxyConfig pc;
1534 PopulateProxyConfig(*dict.get(), &pc);
1535
[email protected]be180c802009-10-23 06:33:311536 net::ProxyService* proxy_service =
1537 request_context_getter_->GetURLRequestContext()->proxy_service();
1538 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:171539 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1540 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:311541 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:171542 }
1543
1544 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1545 DCHECK(pc);
1546 bool no_proxy = false;
1547 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1548 // Make no changes to the ProxyConfig.
1549 return;
1550 }
1551 bool auto_config;
1552 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:481553 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:171554 }
1555 std::string pac_url;
1556 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:481557 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:171558 }
1559 std::string proxy_bypass_list;
1560 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:481561 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:171562 }
1563 std::string proxy_server;
1564 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:481565 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:171566 }
1567 }
1568
1569 private:
[email protected]be180c802009-10-23 06:33:311570 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:171571 std::string proxy_config_;
1572};
1573
1574
1575void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:311576 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
1577 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:171578 FilePath user_data_dir;
1579 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1580 ProfileManager* profile_manager = g_browser_process->profile_manager();
1581 DCHECK(profile_manager);
1582 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1583 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:311584 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171585 }
[email protected]be180c802009-10-23 06:33:311586 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:171587
[email protected]fae20792009-10-28 20:31:581588 ChromeThread::PostTask(
1589 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:311590 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171591}
1592
[email protected]4f3dc372009-02-24 00:10:291593void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331594 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291595 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291596 if (tab_tracker_->ContainsHandle(handle)) {
1597 NavigationController* tab = tab_tracker_->GetResource(handle);
1598 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1599 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331600 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291601 }
initial.commit09911bf2008-07-26 23:55:291602}
1603
[email protected]6a5670d22009-10-27 16:21:341604void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:141605 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:531606 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
1607 reply_message);
1608}
1609
1610void AutomationProvider::OpenNewBrowserWindowOfType(
1611 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:141612 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291613 // We may have no current browser windows open so don't rely on
1614 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:531615 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
1616 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:051617 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151618 if (show)
[email protected]15952e462008-11-14 00:29:051619 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291620}
1621
[email protected]71f65dd2009-02-11 19:14:561622void AutomationProvider::GetWindowForBrowser(int browser_handle,
1623 bool* success,
1624 int* handle) {
1625 *success = false;
1626 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291627
1628 if (browser_tracker_->ContainsHandle(browser_handle)) {
1629 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201630 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291631 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201632 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561633 *success = true;
initial.commit09911bf2008-07-26 23:55:291634 }
initial.commit09911bf2008-07-26 23:55:291635}
1636
[email protected]5ae5bed2009-08-21 18:52:441637#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291638void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561639 int browser_handle,
1640 bool* success,
1641 int* autocomplete_edit_handle) {
1642 *success = false;
1643 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291644
1645 if (browser_tracker_->ContainsHandle(browser_handle)) {
1646 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061647 LocationBar* loc_bar = browser->window()->GetLocationBar();
1648 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291649 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561650 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1651 *success = true;
initial.commit09911bf2008-07-26 23:55:291652 }
initial.commit09911bf2008-07-26 23:55:291653}
[email protected]5ae5bed2009-08-21 18:52:441654#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291655
[email protected]71f65dd2009-02-11 19:14:561656void AutomationProvider::ShowInterstitialPage(int tab_handle,
1657 const std::string& html_text,
1658 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291659 if (tab_tracker_->ContainsHandle(tab_handle)) {
1660 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111661 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401662
[email protected]7dad3d5f2010-03-04 00:27:011663 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:401664 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341665 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401666 GURL("about:interstitial"),
1667 html_text);
1668 interstitial->Show();
1669 return;
initial.commit09911bf2008-07-26 23:55:291670 }
[email protected]71f65dd2009-02-11 19:14:561671
[email protected]457f5cf2009-08-18 16:37:521672 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1673 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561674 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291675}
1676
[email protected]71f65dd2009-02-11 19:14:561677void AutomationProvider::HideInterstitialPage(int tab_handle,
1678 bool* success) {
1679 *success = false;
[email protected]57c6a652009-05-04 07:58:341680 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1681 if (tab_contents && tab_contents->interstitial_page()) {
1682 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561683 *success = true;
initial.commit09911bf2008-07-26 23:55:291684 }
initial.commit09911bf2008-07-26 23:55:291685}
1686
[email protected]71f65dd2009-02-11 19:14:561687void AutomationProvider::CloseTab(int tab_handle,
1688 bool wait_until_closed,
1689 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291690 if (tab_tracker_->ContainsHandle(tab_handle)) {
1691 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1692 int index;
1693 Browser* browser = Browser::GetBrowserForController(controller, &index);
1694 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141695 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111696 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451697 return;
initial.commit09911bf2008-07-26 23:55:291698 }
[email protected]de246f52009-02-25 18:25:451699
1700 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:551701 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291702}
1703
[email protected]71f65dd2009-02-11 19:14:561704void AutomationProvider::CloseBrowser(int browser_handle,
1705 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291706 if (browser_tracker_->ContainsHandle(browser_handle)) {
1707 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561708 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561709 reply_message);
[email protected]f3e99e32008-07-30 04:48:391710 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291711 } else {
1712 NOTREACHED();
1713 }
1714}
1715
[email protected]71f65dd2009-02-11 19:14:561716void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1717 if (browser_tracker_->ContainsHandle(browser_handle)) {
1718 Browser* browser = browser_tracker_->GetResource(browser_handle);
1719 browser->window()->Close();
1720 } else {
1721 NOTREACHED();
1722 }
1723}
1724
[email protected]71f65dd2009-02-11 19:14:561725void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:571726 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:561727 AutomationMsg_NavigationResponseValues* status) {
1728 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291729
1730 if (tab_tracker_->ContainsHandle(handle)) {
1731 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:571732 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561733 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291734 }
initial.commit09911bf2008-07-26 23:55:291735}
1736
[email protected]4150ef02009-08-19 23:14:261737void AutomationProvider::NavigateExternalTabAtIndex(
1738 int handle, int navigation_index,
1739 AutomationMsg_NavigationResponseValues* status) {
1740 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1741
1742 if (tab_tracker_->ContainsHandle(handle)) {
1743 NavigationController* tab = tab_tracker_->GetResource(handle);
1744 tab->GoToIndex(navigation_index);
1745 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1746 }
1747}
1748
[email protected]71f65dd2009-02-11 19:14:561749void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1750 IPC::Message* reply_message) {
1751 if (tab_tracker_->ContainsHandle(tab_handle)) {
1752 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1753 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141754 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561755 }
1756}
1757
[email protected]71f65dd2009-02-11 19:14:561758void AutomationProvider::GetSecurityState(int handle, bool* success,
1759 SecurityStyle* security_style,
1760 int* ssl_cert_status,
1761 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291762 if (tab_tracker_->ContainsHandle(handle)) {
1763 NavigationController* tab = tab_tracker_->GetResource(handle);
1764 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561765 *success = true;
1766 *security_style = entry->ssl().security_style();
1767 *ssl_cert_status = entry->ssl().cert_status();
1768 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291769 } else {
[email protected]71f65dd2009-02-11 19:14:561770 *success = false;
1771 *security_style = SECURITY_STYLE_UNKNOWN;
1772 *ssl_cert_status = 0;
1773 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291774 }
1775}
1776
[email protected]71f65dd2009-02-11 19:14:561777void AutomationProvider::GetPageType(int handle, bool* success,
1778 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291779 if (tab_tracker_->ContainsHandle(handle)) {
1780 NavigationController* tab = tab_tracker_->GetResource(handle);
1781 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561782 *page_type = entry->page_type();
1783 *success = true;
initial.commit09911bf2008-07-26 23:55:291784 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341785 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561786 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341787 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561788 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291789 } else {
[email protected]71f65dd2009-02-11 19:14:561790 *success = false;
1791 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291792 }
1793}
1794
[email protected]84abba62009-10-07 17:01:441795void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
1796 int* duration_ms) {
1797 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
1798 event_name);
1799}
1800
[email protected]71f65dd2009-02-11 19:14:561801void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1802 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291803 if (tab_tracker_->ContainsHandle(handle)) {
1804 NavigationController* tab = tab_tracker_->GetResource(handle);
1805 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071806 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401807 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471808 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341809 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291810 if (ssl_blocking_page) {
1811 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:011812 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:561813 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291814 return;
1815 }
1816 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521817 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1818 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561819 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291820 return;
1821 }
1822 }
1823 }
1824 // We failed.
[email protected]457f5cf2009-08-18 16:37:521825 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1826 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561827 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291828}
1829
[email protected]71f65dd2009-02-11 19:14:561830void AutomationProvider::BringBrowserToFront(int browser_handle,
1831 bool* success) {
initial.commit09911bf2008-07-26 23:55:291832 if (browser_tracker_->ContainsHandle(browser_handle)) {
1833 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061834 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561835 *success = true;
initial.commit09911bf2008-07-26 23:55:291836 } else {
[email protected]71f65dd2009-02-11 19:14:561837 *success = false;
initial.commit09911bf2008-07-26 23:55:291838 }
1839}
1840
[email protected]71f65dd2009-02-11 19:14:561841void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1842 int message_num,
1843 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291844 if (browser_tracker_->ContainsHandle(browser_handle)) {
1845 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561846 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141847 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291848 } else {
[email protected]71f65dd2009-02-11 19:14:561849 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291850 }
1851}
1852
[email protected]71f65dd2009-02-11 19:14:561853void AutomationProvider::PrintNow(int tab_handle,
1854 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441855#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571856 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341857 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1858 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291859 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571860 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141861 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341862 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571863 return;
initial.commit09911bf2008-07-26 23:55:291864 }
[email protected]71f65dd2009-02-11 19:14:561865 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1866 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441867#else
1868 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
1869 NOTIMPLEMENTED();
1870#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291871}
[email protected]d301c952009-07-13 15:02:411872
[email protected]71f65dd2009-02-11 19:14:561873void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:111874 const FilePath& file_name,
1875 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:561876 int type,
1877 bool* success) {
initial.commit09911bf2008-07-26 23:55:291878 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:561879 *success = false;
initial.commit09911bf2008-07-26 23:55:291880 return;
1881 }
1882
1883 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
1884 Browser* browser = FindAndActivateTab(nav);
1885 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:141886 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:561887 *success = false;
initial.commit09911bf2008-07-26 23:55:291888 return;
1889 }
1890
initial.commit09911bf2008-07-26 23:55:291891 SavePackage::SavePackageType save_type =
1892 static_cast<SavePackage::SavePackageType>(type);
1893 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
1894 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:341895 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:291896
[email protected]71f65dd2009-02-11 19:14:561897 *success = true;
initial.commit09911bf2008-07-26 23:55:291898}
1899
[email protected]5ae5bed2009-08-21 18:52:441900#if !defined(OS_MACOSX)
[email protected]3753f522009-04-14 23:15:471901// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:561902void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
1903 bool* success,
1904 std::wstring* text) {
1905 *success = false;
initial.commit09911bf2008-07-26 23:55:291906 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561907 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:521908 GetText();
[email protected]71f65dd2009-02-11 19:14:561909 *success = true;
initial.commit09911bf2008-07-26 23:55:291910 }
initial.commit09911bf2008-07-26 23:55:291911}
1912
[email protected]71f65dd2009-02-11 19:14:561913void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
1914 const std::wstring& text,
1915 bool* success) {
1916 *success = false;
initial.commit09911bf2008-07-26 23:55:291917 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:521918 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
1919 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:561920 *success = true;
initial.commit09911bf2008-07-26 23:55:291921 }
initial.commit09911bf2008-07-26 23:55:291922}
1923
1924void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:561925 int autocomplete_edit_handle,
1926 bool* success,
1927 std::vector<AutocompleteMatchData>* matches) {
1928 *success = false;
initial.commit09911bf2008-07-26 23:55:291929 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:271930 const AutocompleteResult& result = autocomplete_edit_tracker_->
1931 GetResource(autocomplete_edit_handle)->model()->result();
1932 for (AutocompleteResult::const_iterator i = result.begin();
1933 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:561934 matches->push_back(AutocompleteMatchData(*i));
1935 *success = true;
initial.commit09911bf2008-07-26 23:55:291936 }
initial.commit09911bf2008-07-26 23:55:291937}
1938
1939void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:561940 int autocomplete_edit_handle,
1941 bool* success,
1942 bool* query_in_progress) {
1943 *success = false;
1944 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:291945 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:561946 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:521947 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:561948 *success = true;
initial.commit09911bf2008-07-26 23:55:291949 }
initial.commit09911bf2008-07-26 23:55:291950}
1951
[email protected]28790922009-03-09 19:48:371952void AutomationProvider::OnMessageFromExternalHost(int handle,
1953 const std::string& message,
1954 const std::string& origin,
1955 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:191956 RenderViewHost* view_host = GetViewForTab(handle);
1957 if (!view_host) {
1958 return;
[email protected]fa83e762008-08-15 21:41:391959 }
[email protected]f7a68432009-07-29 23:18:191960
1961 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
1962 view_host, message, origin, target)) {
1963 // Message was diverted.
1964 return;
1965 }
1966
1967 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
1968 origin, target, this, view_host, handle)) {
1969 // Message was diverted.
1970 return;
1971 }
1972
1973 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
1974 // Message was diverted.
1975 return;
1976 }
1977
1978 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:391979}
[email protected]a9024892009-06-16 23:13:551980
1981bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
1982 const std::string& message, const std::string& origin,
1983 const std::string& target) {
1984 if (target !=
1985 extension_automation_constants::kAutomationBrowserEventRequestTarget)
1986 return false;
1987
1988 if (origin != extension_automation_constants::kAutomationOrigin) {
1989 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
1990 return false;
1991 }
1992
1993 // The message is a JSON-encoded array with two elements, both strings. The
1994 // first is the name of the event to dispatch. The second is a JSON-encoding
1995 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:201996 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:551997 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
1998 LOG(WARNING) << "Invalid browser event specified through automation";
1999 return false;
2000 }
2001
2002 const ListValue* args = static_cast<const ListValue*>(message_value.get());
2003
2004 std::string event_name;
2005 if (!args->GetString(0, &event_name)) {
2006 LOG(WARNING) << "No browser event name specified through automation";
2007 return false;
2008 }
2009
2010 std::string json_args;
2011 if (!args->GetString(1, &json_args)) {
2012 LOG(WARNING) << "No browser event args specified through automation";
2013 return false;
2014 }
2015
[email protected]7120f132009-07-20 21:05:372016 if (profile()->GetExtensionMessageService()) {
[email protected]db7331a2010-02-25 22:10:502017 profile()->GetExtensionMessageService()->DispatchEventToRenderers(
2018 event_name, json_args, profile()->IsOffTheRecord());
[email protected]7120f132009-07-20 21:05:372019 }
[email protected]a9024892009-06-16 23:13:552020
2021 return true;
2022}
[email protected]5ae5bed2009-08-21 18:52:442023#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:392024
[email protected]57c6a652009-05-04 07:58:342025TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572026 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572027 if (tab_tracker_->ContainsHandle(handle)) {
2028 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342029 if (tab)
2030 *tab = nav_controller;
2031 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572032 }
[email protected]57c6a652009-05-04 07:58:342033 return NULL;
[email protected]20e93d12008-08-28 16:31:572034}
2035
initial.commit09911bf2008-07-26 23:55:292036TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2037 : AutomationProvider(profile) {
2038 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142039 registrar_.Add(this, NotificationType::SESSION_END,
2040 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292041}
2042
2043TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292044 BrowserList::RemoveObserver(this);
2045}
2046
2047void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:142048 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:292049 AutomationProvider::OnChannelError();
2050}
2051
2052void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2053 // For backwards compatibility with the testing automation interface, we
2054 // want the automation provider (and hence the process) to go away when the
2055 // last browser goes away.
2056 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292057 // If you change this, update Observer for NotificationType::SESSION_END
2058 // below.
[email protected]295039bd2008-08-15 04:32:572059 MessageLoop::current()->PostTask(FROM_HERE,
2060 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292061 }
2062}
2063
2064void TestingAutomationProvider::Observe(NotificationType type,
2065 const NotificationSource& source,
2066 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562067 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292068 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2069 // before the task runs resulting in this object not being deleted. This
2070 // Release balance out the Release scheduled by OnBrowserRemoving.
2071 Release();
2072}
[email protected]295039bd2008-08-15 04:32:572073
2074void TestingAutomationProvider::OnRemoveProvider() {
2075 AutomationProviderList::GetInstance()->RemoveProvider(this);
2076}
[email protected]8a3422c92008-09-24 17:42:422077
[email protected]816633a2009-11-11 21:48:182078void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:562079 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422080 if (tab_tracker_->ContainsHandle(handle)) {
2081 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342082 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112083 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422084 }
[email protected]8a3422c92008-09-24 17:42:422085}
2086
[email protected]816633a2009-11-11 21:48:182087void AutomationProvider::ClickInfoBarAccept(int handle,
2088 int info_bar_index,
2089 bool wait_for_navigation,
2090 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422091 bool success = false;
2092 if (tab_tracker_->ContainsHandle(handle)) {
2093 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2094 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112095 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422096 if (info_bar_index >= 0 && info_bar_index < count) {
2097 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:012098 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:422099 }
[email protected]eb9ba192008-12-02 02:41:342100 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112101 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342102 info_bar_index);
2103 if (delegate->AsConfirmInfoBarDelegate())
2104 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422105 success = true;
2106 }
2107 }
[email protected]4f3dc372009-02-24 00:10:292108 }
[email protected]58f622a62009-10-04 01:17:552109
2110 // This "!wait_for_navigation || !success condition" logic looks suspicious.
2111 // It will send a failure message when success is true but
2112 // |wait_for_navigation| is false.
2113 // TODO(phajdan.jr): investgate whether the reply param (currently
2114 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:422115 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:182116 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:522117 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:422118}
2119
[email protected]71f65dd2009-02-11 19:14:562120void AutomationProvider::GetLastNavigationTime(int handle,
2121 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422122 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562123 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422124}
2125
[email protected]71f65dd2009-02-11 19:14:562126void AutomationProvider::WaitForNavigation(int handle,
2127 int64 last_navigation_time,
2128 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:252129 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:422130 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:252131
[email protected]8a3422c92008-09-24 17:42:422132 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562133 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:522134 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
2135 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:552136 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:292137 return;
[email protected]8a3422c92008-09-24 17:42:422138 }
2139
[email protected]7dad3d5f2010-03-04 00:27:012140 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:422141}
2142
[email protected]71f65dd2009-02-11 19:14:562143void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162144 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562145 int value,
2146 bool* success) {
2147 *success = false;
[email protected]8a3422c92008-09-24 17:42:422148 if (browser_tracker_->ContainsHandle(handle)) {
2149 Browser* browser = browser_tracker_->GetResource(handle);
2150 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562151 *success = true;
[email protected]8a3422c92008-09-24 17:42:422152 }
[email protected]8a3422c92008-09-24 17:42:422153}
[email protected]97fa6ce32008-12-19 01:48:162154
[email protected]71f65dd2009-02-11 19:14:562155void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162156 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562157 const std::wstring& value,
2158 bool* success) {
2159 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162160 if (browser_tracker_->ContainsHandle(handle)) {
2161 Browser* browser = browser_tracker_->GetResource(handle);
2162 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562163 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162164 }
[email protected]97fa6ce32008-12-19 01:48:162165}
2166
[email protected]71f65dd2009-02-11 19:14:562167void AutomationProvider::GetBooleanPreference(int handle,
2168 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:362169 bool* success,
2170 bool* value) {
[email protected]71f65dd2009-02-11 19:14:562171 *success = false;
2172 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162173 if (browser_tracker_->ContainsHandle(handle)) {
2174 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562175 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2176 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162177 }
[email protected]97fa6ce32008-12-19 01:48:162178}
2179
[email protected]71f65dd2009-02-11 19:14:562180void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162181 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562182 bool value,
2183 bool* success) {
2184 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162185 if (browser_tracker_->ContainsHandle(handle)) {
2186 Browser* browser = browser_tracker_->GetResource(handle);
2187 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562188 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162189 }
[email protected]97fa6ce32008-12-19 01:48:162190}
2191
2192// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562193void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:402194 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162195 if (tab_tracker_->ContainsHandle(tab_handle)) {
2196 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2197 Browser* browser = FindAndActivateTab(nav);
2198 DCHECK(browser);
2199
[email protected]57c6a652009-05-04 07:58:342200 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2201 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162202 }
[email protected]97fa6ce32008-12-19 01:48:162203}
2204
[email protected]b8f48d12009-11-09 20:14:362205// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562206void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402207 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562208 bool* success) {
2209 *success = false;
[email protected]de246f52009-02-25 18:25:452210#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:162211 if (tab_tracker_->ContainsHandle(tab_handle)) {
2212 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2213 Browser* browser = FindAndActivateTab(nav);
2214 DCHECK(browser);
2215
[email protected]1fc025202009-01-20 23:03:142216 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162217 int selected_encoding_id =
2218 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2219 if (selected_encoding_id) {
2220 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562221 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162222 }
2223 }
2224 }
[email protected]de246f52009-02-25 18:25:452225#else
2226 // TODO(port): Enable when encoding-related parts of Browser are ported.
2227 NOTIMPLEMENTED();
2228#endif
[email protected]97fa6ce32008-12-19 01:48:162229}
[email protected]5bcdb312009-01-07 21:43:202230
[email protected]4d434a1a2009-02-11 21:06:572231void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202232 SavePackage::SetShouldPromptUser(should_prompt);
2233}
[email protected]87eab222009-03-13 00:47:452234
[email protected]66ba4932009-06-04 19:22:132235void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2236 *count = -1; // -1 is the error code
2237 if (tab_tracker_->ContainsHandle(handle)) {
2238 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2239 TabContents* tab_contents = nav_controller->tab_contents();
2240 if (tab_contents) {
2241 BlockedPopupContainer* container =
2242 tab_contents->blocked_popup_container();
2243 if (container) {
2244 *count = static_cast<int>(container->GetBlockedPopupCount());
2245 } else {
2246 // If we don't have a container, we don't have any blocked popups to
2247 // contain!
2248 *count = 0;
2249 }
2250 }
2251 }
2252}
[email protected]f7a68432009-07-29 23:18:192253
2254void AutomationProvider::SelectAll(int tab_handle) {
2255 RenderViewHost* view = GetViewForTab(tab_handle);
2256 if (!view) {
2257 NOTREACHED();
2258 return;
2259 }
2260
2261 view->SelectAll();
2262}
2263
2264void AutomationProvider::Cut(int tab_handle) {
2265 RenderViewHost* view = GetViewForTab(tab_handle);
2266 if (!view) {
2267 NOTREACHED();
2268 return;
2269 }
2270
2271 view->Cut();
2272}
2273
2274void AutomationProvider::Copy(int tab_handle) {
2275 RenderViewHost* view = GetViewForTab(tab_handle);
2276 if (!view) {
2277 NOTREACHED();
2278 return;
2279 }
2280
2281 view->Copy();
2282}
2283
2284void AutomationProvider::Paste(int tab_handle) {
2285 RenderViewHost* view = GetViewForTab(tab_handle);
2286 if (!view) {
2287 NOTREACHED();
2288 return;
2289 }
2290
2291 view->Paste();
2292}
2293
2294void AutomationProvider::ReloadAsync(int tab_handle) {
2295 if (tab_tracker_->ContainsHandle(tab_handle)) {
2296 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2297 if (!tab) {
2298 NOTREACHED();
2299 return;
2300 }
2301
[email protected]106a0812010-03-18 00:15:122302 const bool check_for_repost = true;
2303 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:192304 }
2305}
2306
2307void AutomationProvider::StopAsync(int tab_handle) {
2308 RenderViewHost* view = GetViewForTab(tab_handle);
2309 if (!view) {
[email protected]8b2b3312009-09-14 18:38:362310 // We tolerate StopAsync being called even before a view has been created.
2311 // So just log a warning instead of a NOTREACHED().
2312 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:192313 return;
2314 }
2315
2316 view->Stop();
2317}
2318
[email protected]1bb5f892009-10-06 01:44:572319void AutomationProvider::OnSetPageFontSize(int tab_handle,
2320 int font_size) {
2321 AutomationPageFontSize automation_font_size =
2322 static_cast<AutomationPageFontSize>(font_size);
2323
2324 if (automation_font_size < SMALLEST_FONT ||
2325 automation_font_size > LARGEST_FONT) {
2326 DLOG(ERROR) << "Invalid font size specified : "
2327 << font_size;
2328 return;
2329 }
2330
2331 if (tab_tracker_->ContainsHandle(tab_handle)) {
2332 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2333 DCHECK(tab != NULL);
2334 if (tab && tab->tab_contents()) {
2335 DCHECK(tab->tab_contents()->profile() != NULL);
2336 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2337 prefs::kWebKitDefaultFontSize, font_size);
2338 }
2339 }
2340}
2341
2342
[email protected]2949e90d2009-08-21 15:32:522343void AutomationProvider::WaitForBrowserWindowCountToBecome(
2344 int target_count, IPC::Message* reply_message) {
2345 if (static_cast<int>(BrowserList::size()) == target_count) {
2346 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2347 reply_message, true);
2348 Send(reply_message);
2349 return;
2350 }
2351
2352 // Set up an observer (it will delete itself).
2353 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2354}
2355
2356void AutomationProvider::WaitForAppModalDialogToBeShown(
2357 IPC::Message* reply_message) {
2358 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2359 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2360 reply_message, true);
2361 Send(reply_message);
2362 return;
2363 }
2364
2365 // Set up an observer (it will delete itself).
2366 new AppModalDialogShownObserver(this, reply_message);
2367}
2368
[email protected]1126a1d32009-08-26 15:39:262369void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2370 int handle, int number_of_navigations, IPC::Message* reply_message) {
2371 if (tab_tracker_->ContainsHandle(handle)) {
2372 NavigationController* tab = tab_tracker_->GetResource(handle);
2373 Browser* browser = FindAndActivateTab(tab);
2374 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:012375 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2376 false);
[email protected]1126a1d32009-08-26 15:39:262377 browser->GoBack(CURRENT_TAB);
2378 return;
2379 }
2380 }
2381
2382 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2383 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2384 Send(reply_message);
2385}
2386
2387void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2388 int handle, int number_of_navigations, IPC::Message* reply_message) {
2389 if (tab_tracker_->ContainsHandle(handle)) {
2390 NavigationController* tab = tab_tracker_->GetResource(handle);
2391 Browser* browser = FindAndActivateTab(tab);
2392 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:012393 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2394 false);
[email protected]1126a1d32009-08-26 15:39:262395 browser->GoForward(CURRENT_TAB);
2396 return;
2397 }
2398 }
2399
2400 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2401 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2402 Send(reply_message);
2403}
2404
[email protected]f7a68432009-07-29 23:18:192405RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2406 if (tab_tracker_->ContainsHandle(tab_handle)) {
2407 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2408 if (!tab) {
2409 NOTREACHED();
2410 return NULL;
2411 }
2412
2413 TabContents* tab_contents = tab->tab_contents();
2414 if (!tab_contents) {
2415 NOTREACHED();
2416 return NULL;
2417 }
2418
2419 RenderViewHost* view_host = tab_contents->render_view_host();
2420 return view_host;
2421 }
2422
2423 return NULL;
2424}
[email protected]675595f2009-08-26 22:32:042425
2426void AutomationProvider::GetBrowserForWindow(int window_handle,
2427 bool* success,
2428 int* browser_handle) {
2429 *success = false;
2430 *browser_handle = 0;
2431
2432 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2433 if (!window)
2434 return;
2435
2436 BrowserList::const_iterator iter = BrowserList::begin();
2437 for (;iter != BrowserList::end(); ++iter) {
2438 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2439 if (window == this_window) {
2440 // Add() returns the existing handle for the resource if any.
2441 *browser_handle = browser_tracker_->Add(*iter);
2442 *success = true;
2443 return;
2444 }
2445 }
2446}
[email protected]d11c8e92009-10-20 23:26:402447
2448void AutomationProvider::InstallExtension(const FilePath& crx_path,
2449 IPC::Message* reply_message) {
2450 ExtensionsService* service = profile_->GetExtensionsService();
2451 if (service) {
2452 // The observer will delete itself when done.
2453 new ExtensionNotificationObserver(this,
2454 AutomationMsg_InstallExtension::ID,
2455 reply_message);
2456
2457 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:162458 scoped_refptr<CrxInstaller> installer(
2459 new CrxInstaller(install_dir,
2460 service,
2461 NULL)); // silent install, no UI
2462 installer->set_allow_privilege_increase(true);
2463 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:402464 } else {
2465 AutomationMsg_InstallExtension::WriteReplyParams(
2466 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2467 Send(reply_message);
2468 }
2469}
2470
2471void AutomationProvider::LoadExpandedExtension(
2472 const FilePath& extension_dir,
2473 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:382474 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:402475 // The observer will delete itself when done.
2476 new ExtensionNotificationObserver(this,
2477 AutomationMsg_LoadExpandedExtension::ID,
2478 reply_message);
2479
2480 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:402481 } else {
2482 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
2483 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2484 Send(reply_message);
2485 }
2486}
[email protected]673fd2c02010-02-04 23:10:002487
[email protected]a1e62d12010-03-16 02:18:432488void AutomationProvider::GetEnabledExtensions(
2489 std::vector<FilePath>* result) {
2490 ExtensionsService* service = profile_->GetExtensionsService();
2491 DCHECK(service);
2492 if (service->extensions_enabled()) {
2493 const ExtensionList* extensions = service->extensions();
2494 DCHECK(extensions);
2495 for (size_t i = 0; i < extensions->size(); ++i) {
2496 Extension* extension = (*extensions)[i];
2497 DCHECK(extension);
[email protected]237f281672010-03-20 12:37:072498 if (extension->location() == Extension::INTERNAL) {
2499 result->push_back(extension->path());
2500 }
[email protected]a1e62d12010-03-16 02:18:432501 }
2502 }
2503}
2504
[email protected]673fd2c02010-02-04 23:10:002505void AutomationProvider::SaveAsAsync(int tab_handle) {
2506 NavigationController* tab = NULL;
2507 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2508 if (tab_contents)
2509 tab_contents->OnSavePage();
2510}
[email protected]7dad3d5f2010-03-04 00:27:012511
2512void AutomationProvider::SetContentSetting(
2513 int handle,
2514 const std::string& host,
2515 ContentSettingsType content_type,
2516 ContentSetting setting,
2517 bool* success) {
2518 *success = false;
2519 if (browser_tracker_->ContainsHandle(handle)) {
2520 Browser* browser = browser_tracker_->GetResource(handle);
2521 HostContentSettingsMap* map =
2522 browser->profile()->GetHostContentSettingsMap();
2523 if (host.empty()) {
2524 map->SetDefaultContentSetting(content_type, setting);
2525 } else {
2526 map->SetContentSetting(host, content_type, setting);
2527 }
2528 *success = true;
2529 }
2530}