blob: a878fcaebfb90b2584f7ba1a07ebdbf97998e58e [file] [log] [blame]
[email protected]3b5f7022010-03-25 20:37:401// Copyright (c) 2010 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]59a611242010-04-02 02:24:0414#include "base/json/json_writer.h"
[email protected]bc1407f2009-09-29 00:33:3515#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3816#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4618#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5019#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2720#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3821#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5322#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1723#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2924#include "chrome/app/chrome_dll_resource.h"
[email protected]bcff05a2010-04-14 01:46:4325#include "chrome/app/chrome_version_info.h"
[email protected]0bfa713f2009-04-07 20:18:2826#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0927#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3328#include "chrome/browser/automation/automation_extension_function.h"
[email protected]790788ac2010-04-06 17:52:1929#include "chrome/browser/automation/automation_extension_tracker.h"
initial.commit09911bf2008-07-26 23:55:2930#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0831#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5532#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5033#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1334#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5335#include "chrome/browser/bookmarks/bookmark_model.h"
36#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]5c238752009-06-13 10:29:0737#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3938#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2039#include "chrome/browser/browsing_data_remover.h"
[email protected]fae20792009-10-28 20:31:5840#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2941#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1242#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5943#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2244#include "chrome/browser/download/download_shelf.h"
[email protected]d11c8e92009-10-20 23:26:4045#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1946#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]d11c8e92009-10-20 23:26:4047#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5548#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1949#include "chrome/browser/extensions/extension_tabs_module.h"
50#include "chrome/browser/extensions/extension_toolbar_model.h"
51#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1652#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5853#include "chrome/browser/find_bar.h"
54#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2955#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0156#include "chrome/browser/host_content_settings_map.h"
[email protected]0ac83682010-01-22 17:46:2757#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0658#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0159#include "chrome/browser/login_prompt.h"
[email protected]be180c802009-10-23 06:33:3160#include "chrome/browser/net/url_request_context_getter.h"
[email protected]f732c1e2009-07-30 15:48:5361#include "chrome/browser/net/url_request_mock_util.h"
[email protected]052313b2010-02-19 09:43:0862#include "chrome/browser/pref_service.h"
[email protected]a7eee32f2009-05-22 18:08:1763#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0764#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2565#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0366#include "chrome/browser/ssl/ssl_manager.h"
67#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3468#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4569#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1770#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2971#include "chrome/common/chrome_paths.h"
[email protected]790788ac2010-04-06 17:52:1972#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1773#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1474#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4775#include "chrome/common/platform_util.h"
[email protected]1bb5f892009-10-06 01:44:5776#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2577#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5678#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5779#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1780#include "net/proxy/proxy_service.h"
81#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2182#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2883#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2984
[email protected]de246f52009-02-25 18:25:4585#if defined(OS_WIN)
86// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4587#include "chrome/browser/character_encoding.h"
88#include "chrome/browser/download/save_package.h"
89#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4590#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4591#endif // defined(OS_WIN)
92
[email protected]5ae5bed2009-08-21 18:52:4493#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2894// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2895#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4496#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0697
[email protected]e1acf6f2008-10-27 20:43:3398using base::Time;
99
[email protected]cbab76d2008-10-13 22:42:47100class AutomationInterstitialPage : public InterstitialPage {
101 public:
[email protected]57c6a652009-05-04 07:58:34102 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47103 const GURL& url,
104 const std::string& contents)
105 : InterstitialPage(tab, true, url),
106 contents_(contents) {
107 }
108
109 virtual std::string GetHTMLContents() { return contents_; }
110
111 private:
112 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29113
[email protected]cbab76d2008-10-13 22:42:47114 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
115};
116
[email protected]c2cb8542009-08-20 21:16:51117#if !defined(OS_MACOSX)
118class ClickTask : public Task {
119 public:
[email protected]fc2e0872009-08-21 22:14:41120 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51121 virtual ~ClickTask() {}
122
123 virtual void Run() {
124 ui_controls::MouseButton button = ui_controls::LEFT;
125 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
126 views::Event::EF_LEFT_BUTTON_DOWN) {
127 button = ui_controls::LEFT;
128 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
129 views::Event::EF_RIGHT_BUTTON_DOWN) {
130 button = ui_controls::RIGHT;
131 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
132 views::Event::EF_MIDDLE_BUTTON_DOWN) {
133 button = ui_controls::MIDDLE;
134 } else {
135 NOTREACHED();
136 }
137
[email protected]fc2e0872009-08-21 22:14:41138 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51139 }
140
141 private:
[email protected]c2cb8542009-08-20 21:16:51142 int flags_;
143
144 DISALLOW_COPY_AND_ASSIGN(ClickTask);
145};
146#endif
147
initial.commit09911bf2008-07-26 23:55:29148AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57149 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56150 profile_(profile),
[email protected]cc824372010-03-31 15:33:01151 reply_message_(NULL),
152 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29153 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19154 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29155 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20156 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29157 autocomplete_edit_tracker_.reset(
158 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29159 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
160 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44161 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19162 extension_test_result_observer_.reset(
163 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13164 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29165}
166
167AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50168 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
169 port_containers_.end());
170 port_containers_.clear();
171
[email protected]0da050b92008-08-19 19:29:47172 // Make sure that any outstanding NotificationObservers also get destroyed.
173 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31174 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47175 while ((observer = it.GetNext()) != NULL)
176 delete observer;
[email protected]528211a2010-01-14 15:25:13177
178 if (channel_.get()) {
179 channel_->Close();
180 }
181 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29182}
183
[email protected]9a3a293b2009-06-04 22:28:16184void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06185 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57186 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06187 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
188 automation_resource_message_filter_,
189 g_browser_process->io_thread()->message_loop(),
190 true, g_browser_process->shutdown_event()));
[email protected]bcff05a2010-04-14 01:46:43191 scoped_ptr<FileVersionInfo> version_info(
192 chrome_app::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40193 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43194 if (version_info != NULL) {
195 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40196 }
[email protected]c6cb1992009-04-13 16:45:29197
198 // Send a hello message with our current automation protocol version.
199 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29200}
201
202void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
203 if (expected_tabs == 0) {
204 Send(new AutomationMsg_InitialLoadsComplete(0));
205 } else {
206 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
207 }
208}
209
210NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58211 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01212 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29213 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58214 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01215 number_of_navigations,
216 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29217
[email protected]71f65dd2009-02-11 19:14:56218 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29219 return observer;
220}
221
222void AutomationProvider::RemoveNavigationStatusListener(
223 NotificationObserver* obs) {
224 notification_observer_list_.RemoveObserver(obs);
225}
226
227NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14228 Browser* parent,
229 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56230 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14231 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29232 notification_observer_list_.AddObserver(observer);
233
234 return observer;
235}
236
237void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
238 notification_observer_list_.RemoveObserver(obs);
239}
240
241void AutomationProvider::AddLoginHandler(NavigationController* tab,
242 LoginHandler* handler) {
243 login_handler_map_[tab] = handler;
244}
245
246void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
247 DCHECK(login_handler_map_[tab]);
248 login_handler_map_.erase(tab);
249}
250
[email protected]f44265b2009-05-19 18:52:50251void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
252 int port_id = port->port_id();
253 DCHECK_NE(-1, port_id);
254 DCHECK(port_containers_.find(port_id) == port_containers_.end());
255
256 port_containers_[port_id] = port;
257}
258
259void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
260 int port_id = port->port_id();
261 DCHECK_NE(-1, port_id);
262
263 PortContainerMap::iterator it = port_containers_.find(port_id);
264 DCHECK(it != port_containers_.end());
265
266 if (it != port_containers_.end()) {
267 delete it->second;
268 port_containers_.erase(it);
269 }
270}
271
272ExtensionPortContainer* AutomationProvider::GetPortContainer(
273 int port_id) const {
274 PortContainerMap::const_iterator it = port_containers_.find(port_id);
275 if (it == port_containers_.end())
276 return NULL;
277
278 return it->second;
279}
280
initial.commit09911bf2008-07-26 23:55:29281int AutomationProvider::GetIndexForNavigationController(
282 const NavigationController* controller, const Browser* parent) const {
283 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12284 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29285}
286
[email protected]790788ac2010-04-06 17:52:19287int AutomationProvider::AddExtension(Extension* extension) {
288 DCHECK(extension);
289 return extension_tracker_->Add(extension);
290}
291
292Extension* AutomationProvider::GetExtension(int extension_handle) {
293 return extension_tracker_->GetResource(extension_handle);
294}
295
296Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
297 Extension* extension = extension_tracker_->GetResource(extension_handle);
298 ExtensionsService* service = profile_->GetExtensionsService();
299 if (extension && service &&
300 service->GetExtensionById(extension->id(), false))
301 return extension;
302 return NULL;
303}
304
305Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
306 Extension* extension = extension_tracker_->GetResource(extension_handle);
307 ExtensionsService* service = profile_->GetExtensionsService();
308 if (extension && service &&
309 service->GetExtensionById(extension->id(), true) &&
310 !service->GetExtensionById(extension->id(), false))
311 return extension;
312 return NULL;
313}
314
initial.commit09911bf2008-07-26 23:55:29315void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
316 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14317 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56318 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
319 CloseBrowserAsync)
320 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
321 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
322 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
323 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
324 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
325 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22326 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]1c58a5c2009-05-21 18:47:14327 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58328 IPC_MESSAGE_HANDLER_DELAY_REPLY(
329 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
330 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56331 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11332 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
333 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56334 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
335 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
336 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
337 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14338 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56339 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
340 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
341 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14342 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29343 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
344 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56345 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36346 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56347 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29348 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56349 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29350 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
351 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56352 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14353 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24354 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44355 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33356 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44357 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33358 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24359 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14360 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24361 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15362 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14363 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44364#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24365 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35366 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14367 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]71f65dd2009-02-11 19:14:56368 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
369 WindowSimulateDrag)
[email protected]b2aa3ed72010-02-01 18:37:14370#endif
[email protected]71f65dd2009-02-11 19:14:56371 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53372 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56373 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37374#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56375 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45376#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56377 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
378 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03379 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56380 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14381 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09382 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
383 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
384 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29385 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14386 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56387 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
388 ExecuteJavascript)
389 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29390 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14391 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
392 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56393 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
394 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14395 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17396 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14397 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14398 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53399 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
400 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14401 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56402 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14403 GetAutocompleteEditForBrowser)
404 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45405#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29406 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17407#endif
[email protected]71f65dd2009-02-11 19:14:56408 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29409 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26410 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
411 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56412 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14413 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56414 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14415 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17416#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29417 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
418 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17419#endif
[email protected]71f65dd2009-02-11 19:14:56420 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
421 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14422 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17423#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04424 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39425 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
426 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17427#endif
[email protected]1c58a5c2009-05-21 18:47:14428 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
429 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44430 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
431 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56432 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
433 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29434 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
435 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
436 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56437 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41438 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56439 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
440 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29441 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56442 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29443 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56444 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29445 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56446 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29447 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56448 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45449 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45450 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
451 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14452 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56453 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57454 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56455 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57456 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56457 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
458 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53459 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
460 GetBookmarksAsJSON)
461 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
462 WaitForBookmarkModelToLoad)
463 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
464 AddBookmarkGroup)
465 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
466 AddBookmarkURL)
467 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
468 ReparentBookmark)
469 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
470 SetBookmarkTitle)
471 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
472 SetBookmarkURL)
473 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
474 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04475 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
476 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18477 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
478 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
479 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56480 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42481 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56482 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
483 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14484 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56485 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44486 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56487 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20488 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14489 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56490 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16491 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56492 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16493 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56494 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16495 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14496 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20497 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
498 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14499 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]4cfc1d922009-11-08 14:02:58500#if defined(OS_WIN)
501 // Depends on ExternalTabContainer, so Windows-only
[email protected]b83e4602009-05-15 22:58:33502 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
503 SetEnableExtensionAutomation)
[email protected]4cfc1d922009-11-08 14:02:58504#endif
[email protected]59560e0b2009-06-04 03:30:22505 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13506 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19507 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
508 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
509 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
510 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
511 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
512 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52513 IPC_MESSAGE_HANDLER_DELAY_REPLY(
514 AutomationMsg_WaitForBrowserWindowCountToBecome,
515 WaitForBrowserWindowCountToBecome)
516 IPC_MESSAGE_HANDLER_DELAY_REPLY(
517 AutomationMsg_WaitForAppModalDialogToBeShown,
518 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26519 IPC_MESSAGE_HANDLER_DELAY_REPLY(
520 AutomationMsg_GoBackBlockUntilNavigationsComplete,
521 GoBackBlockUntilNavigationsComplete)
522 IPC_MESSAGE_HANDLER_DELAY_REPLY(
523 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
524 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14525#if defined(OS_WIN)
526 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
527#endif
[email protected]1bb5f892009-10-06 01:44:57528 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40529 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
530 InstallExtension)
531 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
532 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43533 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
534 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19535 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
536 WaitForExtensionTestResult)
537 IPC_MESSAGE_HANDLER_DELAY_REPLY(
538 AutomationMsg_InstallExtensionAndGetHandle,
539 InstallExtensionAndGetHandle)
540 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
541 UninstallExtension)
542 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
543 EnableExtension)
544 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
545 DisableExtension)
546 IPC_MESSAGE_HANDLER_DELAY_REPLY(
547 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
548 ExecuteExtensionActionInActiveTabAsync)
549 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
550 MoveExtensionBrowserAction)
551 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
552 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57553 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
554 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00555 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]2b19e2fe2010-02-16 02:24:18556#if defined(OS_WIN)
557 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
558#endif
[email protected]7dad3d5f2010-03-04 00:27:01559 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]566a0f762010-03-10 04:14:57560#if defined(OS_CHROMEOS)
561 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
562 LoginWithUserAndPass)
563#endif
[email protected]bc73b4e52010-03-26 04:16:20564 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]cc824372010-03-31 15:33:01565#if defined(TOOLKIT_VIEWS)
566 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
567 WaitForFocusedViewIDToChange)
568 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
569 StartTrackingPopupMenus)
570 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
571 WaitForPopupMenuToOpen)
572#endif
[email protected]d7e5525d2010-04-20 14:37:09573 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
initial.commit09911bf2008-07-26 23:55:29574 IPC_END_MESSAGE_MAP()
575}
576
[email protected]71f65dd2009-02-11 19:14:56577void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
578 *status = -1;
initial.commit09911bf2008-07-26 23:55:29579 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
580 Browser* browser = browser_tracker_->GetResource(handle);
581 if (at_index >= 0 && at_index < browser->tab_count()) {
582 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56583 *status = 0;
initial.commit09911bf2008-07-26 23:55:29584 }
585 }
initial.commit09911bf2008-07-26 23:55:29586}
587
[email protected]71f65dd2009-02-11 19:14:56588void AutomationProvider::AppendTab(int handle, const GURL& url,
589 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29590 int append_tab_response = -1; // -1 is the error code
591 NotificationObserver* observer = NULL;
592
593 if (browser_tracker_->ContainsHandle(handle)) {
594 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14595 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38596 TabContents* tab_contents = browser->AddTabWithURL(
597 url, GURL(), PageTransition::TYPED, -1, Browser::ADD_SELECTED, NULL,
598 std::string());
initial.commit09911bf2008-07-26 23:55:29599 if (tab_contents) {
600 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57601 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29602 }
603 }
604
605 if (append_tab_response < 0) {
606 // The append tab failed. Remove the TabStripObserver
607 if (observer) {
608 RemoveTabStripObserver(observer);
609 delete observer;
610 }
611
[email protected]71f65dd2009-02-11 19:14:56612 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
613 append_tab_response);
614 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29615 }
616}
617
[email protected]71f65dd2009-02-11 19:14:56618void AutomationProvider::NavigateToURL(int handle, const GURL& url,
619 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58620 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
621}
622
623void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
624 int handle, const GURL& url, int number_of_navigations,
625 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29626 if (tab_tracker_->ContainsHandle(handle)) {
627 NavigationController* tab = tab_tracker_->GetResource(handle);
628
629 // Simulate what a user would do. Activate the tab and then navigate.
630 // We could allow navigating in a background tab in future.
631 Browser* browser = FindAndActivateTab(tab);
632
633 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01634 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
635 false);
[email protected]71f65dd2009-02-11 19:14:56636
initial.commit09911bf2008-07-26 23:55:29637 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50638 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29639 return;
640 }
641 }
[email protected]71f65dd2009-02-11 19:14:56642
643 AutomationMsg_NavigateToURL::WriteReplyParams(
644 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
645 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29646}
[email protected]2949e90d2009-08-21 15:32:52647
[email protected]c70f9b82010-04-21 07:31:11648void AutomationProvider::NavigationAsync(int handle,
649 const GURL& url,
650 bool* status) {
651 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
652}
653
654void AutomationProvider::NavigationAsyncWithDisposition(
655 int handle,
656 const GURL& url,
657 WindowOpenDisposition disposition,
658 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56659 *status = false;
initial.commit09911bf2008-07-26 23:55:29660
661 if (tab_tracker_->ContainsHandle(handle)) {
662 NavigationController* tab = tab_tracker_->GetResource(handle);
663
664 // Simulate what a user would do. Activate the tab and then navigate.
665 // We could allow navigating in a background tab in future.
666 Browser* browser = FindAndActivateTab(tab);
667
668 if (browser) {
669 // Don't add any listener unless a callback mechanism is desired.
670 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11671 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56672 *status = true;
initial.commit09911bf2008-07-26 23:55:29673 }
674 }
initial.commit09911bf2008-07-26 23:55:29675}
676
[email protected]71f65dd2009-02-11 19:14:56677void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29678 if (tab_tracker_->ContainsHandle(handle)) {
679 NavigationController* tab = tab_tracker_->GetResource(handle);
680 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14681 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01682 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29683 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29684 return;
685 }
686 }
[email protected]71f65dd2009-02-11 19:14:56687
688 AutomationMsg_GoBack::WriteReplyParams(
689 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
690 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29691}
692
[email protected]71f65dd2009-02-11 19:14:56693void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29694 if (tab_tracker_->ContainsHandle(handle)) {
695 NavigationController* tab = tab_tracker_->GetResource(handle);
696 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14697 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01698 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29699 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29700 return;
701 }
702 }
[email protected]71f65dd2009-02-11 19:14:56703
704 AutomationMsg_GoForward::WriteReplyParams(
705 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
706 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29707}
708
[email protected]71f65dd2009-02-11 19:14:56709void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29710 if (tab_tracker_->ContainsHandle(handle)) {
711 NavigationController* tab = tab_tracker_->GetResource(handle);
712 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14713 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01714 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29715 browser->Reload();
716 return;
717 }
718 }
[email protected]71f65dd2009-02-11 19:14:56719
720 AutomationMsg_Reload::WriteReplyParams(
721 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
722 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29723}
724
[email protected]71f65dd2009-02-11 19:14:56725void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29726 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56727 const std::wstring& password,
728 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29729 if (tab_tracker_->ContainsHandle(tab_handle)) {
730 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
731 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
732
733 if (iter != login_handler_map_.end()) {
734 // If auth is needed again after this, assume login has failed. This is
735 // not strictly correct, because a navigation can require both proxy and
736 // server auth, but it should be OK for now.
737 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01738 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29739 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52740 return;
initial.commit09911bf2008-07-26 23:55:29741 }
742 }
[email protected]de246f52009-02-25 18:25:45743
[email protected]457f5cf2009-08-18 16:37:52744 AutomationMsg_SetAuth::WriteReplyParams(
745 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
746 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29747}
748
[email protected]71f65dd2009-02-11 19:14:56749void AutomationProvider::CancelAuth(int tab_handle,
750 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29751 if (tab_tracker_->ContainsHandle(tab_handle)) {
752 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
753 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
754
755 if (iter != login_handler_map_.end()) {
756 // If auth is needed again after this, something is screwy.
757 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01758 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29759 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52760 return;
initial.commit09911bf2008-07-26 23:55:29761 }
762 }
[email protected]de246f52009-02-25 18:25:45763
[email protected]457f5cf2009-08-18 16:37:52764 AutomationMsg_CancelAuth::WriteReplyParams(
765 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
766 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29767}
768
[email protected]71f65dd2009-02-11 19:14:56769void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
770 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29771
772 if (tab_tracker_->ContainsHandle(tab_handle)) {
773 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
774 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
775
776 if (iter != login_handler_map_.end()) {
777 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56778 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29779 }
780 }
initial.commit09911bf2008-07-26 23:55:29781}
782
[email protected]71f65dd2009-02-11 19:14:56783void AutomationProvider::GetRedirectsFrom(int tab_handle,
784 const GURL& source_url,
785 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29786 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
787 if (tab_tracker_->ContainsHandle(tab_handle)) {
788 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
789 HistoryService* history_service =
790 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
791
792 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
793 "has no history service";
794 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56795 DCHECK(reply_message_ == NULL);
796 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29797 // Schedule a history query for redirects. The response will be sent
798 // asynchronously from the callback the history system uses to notify us
799 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29800 redirect_query_ = history_service->QueryRedirectsFrom(
801 source_url, &consumer_,
802 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
803 return; // Response will be sent when query completes.
804 }
805 }
806
807 // Send failure response.
[email protected]deb57402009-02-06 01:35:30808 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56809 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
810 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29811}
812
[email protected]71f65dd2009-02-11 19:14:56813void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
814 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29815 if (browser_tracker_->ContainsHandle(handle)) {
816 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56817 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29818 }
initial.commit09911bf2008-07-26 23:55:29819}
820
[email protected]202e7a72009-06-15 03:48:36821void AutomationProvider::GetBrowserLocale(string16* locale) {
822 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06823 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36824}
825
[email protected]71f65dd2009-02-11 19:14:56826void AutomationProvider::GetBrowserWindowCount(int* window_count) {
827 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29828}
829
[email protected]24497032009-05-01 17:00:29830void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
831 *window_count = static_cast<int>(
832 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
833}
834
[email protected]71f65dd2009-02-11 19:14:56835void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
836 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07837 AppModalDialog* dialog_delegate =
838 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50839 *showing_dialog = (dialog_delegate != NULL);
840 if (*showing_dialog)
841 *dialog_button = dialog_delegate->GetDialogButtons();
842 else
[email protected]478ff2ed2009-04-21 23:49:18843 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20844}
845
[email protected]71f65dd2009-02-11 19:14:56846void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
847 *success = false;
[email protected]fad84eab2008-12-05 00:37:20848
[email protected]1f460072009-05-28 17:02:07849 AppModalDialog* dialog_delegate =
850 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50851 if (dialog_delegate &&
852 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18853 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
854 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28855 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56856 *success = true;
[email protected]fad84eab2008-12-05 00:37:20857 }
[email protected]478ff2ed2009-04-21 23:49:18858 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
859 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56860 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28861 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56862 *success = true;
[email protected]fad84eab2008-12-05 00:37:20863 }
864 }
[email protected]c274acc2008-11-11 20:13:44865}
866
[email protected]fedaa7d2010-01-26 20:34:57867void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
868 if (browser_tracker_->ContainsHandle(handle)) {
869 Browser* browser = browser_tracker_->GetResource(handle);
870 browser->profile()->ShutdownSessionService();
871 *result = true;
872 } else {
873 *result = false;
874 }
875}
876
[email protected]71f65dd2009-02-11 19:14:56877void AutomationProvider::GetBrowserWindow(int index, int* handle) {
878 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29879 if (index >= 0) {
880 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29881 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29882 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56883 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29884 }
885 }
initial.commit09911bf2008-07-26 23:55:29886}
887
[email protected]24497032009-05-01 17:00:29888void AutomationProvider::FindNormalBrowserWindow(int* handle) {
889 *handle = 0;
890 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31891 Browser::TYPE_NORMAL,
892 false);
[email protected]24497032009-05-01 17:00:29893 if (browser)
894 *handle = browser_tracker_->Add(browser);
895}
896
[email protected]71f65dd2009-02-11 19:14:56897void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
898 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29899 Browser* browser = BrowserList::GetLastActive();
900 if (browser)
[email protected]71f65dd2009-02-11 19:14:56901 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29902}
903
[email protected]b2aa3ed72010-02-01 18:37:14904#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28905// TODO(estade): use this implementation for all platforms?
906void AutomationProvider::GetActiveWindow(int* handle) {
907 gfx::NativeWindow window =
908 BrowserList::GetLastActive()->window()->GetNativeHandle();
909 *handle = window_tracker_->Add(window);
910}
911#endif
912
[email protected]4f6381ee2009-04-16 02:46:33913void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
914 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56915 *success = false;
[email protected]4ae62752008-08-04 23:28:47916 if (browser_tracker_->ContainsHandle(handle)) {
917 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14918 if (browser->command_updater()->SupportsCommand(command) &&
919 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47920 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56921 *success = true;
[email protected]4ae62752008-08-04 23:28:47922 }
923 }
[email protected]4ae62752008-08-04 23:28:47924}
925
[email protected]4f6381ee2009-04-16 02:46:33926void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56927 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53928 // List of commands which just finish synchronously and don't require
929 // setting up an observer.
930 static const int kSynchronousCommands[] = {
931 IDC_HOME,
932 IDC_SELECT_NEXT_TAB,
933 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25934 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53935 };
[email protected]56e71b7c2009-03-27 03:05:56936 if (browser_tracker_->ContainsHandle(handle)) {
937 Browser* browser = browser_tracker_->GetResource(handle);
938 if (browser->command_updater()->SupportsCommand(command) &&
939 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53940 // First check if we can handle the command without using an observer.
941 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
942 if (command == kSynchronousCommands[i]) {
943 browser->ExecuteCommand(command);
944 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
945 true);
946 Send(reply_message);
947 return;
948 }
949 }
950
951 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42952 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
953 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27954 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42955 return;
956 }
[email protected]56e71b7c2009-03-27 03:05:56957 }
958 }
[email protected]49a14a82009-03-31 04:16:44959 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56960 Send(reply_message);
961}
962
[email protected]fc2e0872009-08-21 22:14:41963// This task just adds another task to the event queue. This is useful if
964// you want to ensure that any tasks added to the event queue after this one
965// have already been processed by the time |task| is run.
966class InvokeTaskLaterTask : public Task {
967 public:
968 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
969 virtual ~InvokeTaskLaterTask() {}
970
971 virtual void Run() {
972 MessageLoop::current()->PostTask(FROM_HERE, task_);
973 }
974
975 private:
976 Task* task_;
977
978 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
979};
980
[email protected]b2aa3ed72010-02-01 18:37:14981#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29982void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
983 int handle,
[email protected]d1a5941e2009-08-13 23:34:24984 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29985 int flags) {
[email protected]b410bc32009-08-14 01:11:14986 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51987 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41988 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29989 }
990}
991
[email protected]60507b12009-11-02 23:51:35992void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
993 int handle,
994 const gfx::Point& location) {
995 if (window_tracker_->ContainsHandle(handle))
996 ui_controls::SendMouseMove(location.x(), location.y());
997}
998
initial.commit09911bf2008-07-26 23:55:29999void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
1000 int handle,
[email protected]bc1407f2009-09-29 00:33:351001 int key,
initial.commit09911bf2008-07-26 23:55:291002 int flags) {
[email protected]b410bc32009-08-14 01:11:141003 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:291004 return;
1005
[email protected]b410bc32009-08-14 01:11:141006 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291007 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:351008 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:381009 ((flags & views::Event::EF_CONTROL_DOWN) ==
1010 views::Event::EF_CONTROL_DOWN),
1011 ((flags & views::Event::EF_SHIFT_DOWN) ==
1012 views::Event::EF_SHIFT_DOWN),
1013 ((flags & views::Event::EF_ALT_DOWN) ==
1014 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:291015}
[email protected]b2aa3ed72010-02-01 18:37:141016#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:291017
[email protected]71f65dd2009-02-11 19:14:561018void AutomationProvider::IsWindowActive(int handle, bool* success,
1019 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291020 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171021 *is_active =
1022 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561023 *success = true;
initial.commit09911bf2008-07-26 23:55:291024 } else {
[email protected]71f65dd2009-02-11 19:14:561025 *success = false;
1026 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291027 }
1028}
1029
[email protected]71f65dd2009-02-11 19:14:561030void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1031 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291032
1033 if (browser_tracker_->ContainsHandle(handle)) {
1034 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561035 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291036 }
initial.commit09911bf2008-07-26 23:55:291037}
1038
[email protected]982921f12009-10-27 21:43:531039void AutomationProvider::GetType(int handle, int* type_as_int) {
1040 *type_as_int = -1; // -1 is the error code
1041
1042 if (browser_tracker_->ContainsHandle(handle)) {
1043 Browser* browser = browser_tracker_->GetResource(handle);
1044 *type_as_int = static_cast<int>(browser->type());
1045 }
1046}
1047
[email protected]71f65dd2009-02-11 19:14:561048void AutomationProvider::GetTab(int win_handle, int tab_index,
1049 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561050 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291051 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1052 Browser* browser = browser_tracker_->GetResource(win_handle);
1053 if (tab_index < browser->tab_count()) {
1054 TabContents* tab_contents =
1055 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571056 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291057 }
1058 }
initial.commit09911bf2008-07-26 23:55:291059}
1060
[email protected]71f65dd2009-02-11 19:14:561061void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1062 std::wstring* title) {
1063 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291064 if (tab_tracker_->ContainsHandle(handle)) {
1065 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361066 NavigationEntry* entry = tab->GetActiveEntry();
1067 if (entry != NULL) {
1068 *title = UTF16ToWideHack(entry->title());
1069 } else {
1070 *title = std::wstring();
1071 }
[email protected]71f65dd2009-02-11 19:14:561072 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291073 }
initial.commit09911bf2008-07-26 23:55:291074}
1075
[email protected]77bc6732009-04-20 22:01:031076void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1077 *tabstrip_index = -1; // -1 is the error code
1078
1079 if (tab_tracker_->ContainsHandle(handle)) {
1080 NavigationController* tab = tab_tracker_->GetResource(handle);
1081 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121082 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031083 }
1084}
1085
initial.commit09911bf2008-07-26 23:55:291086void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1087 if (window_tracker_->ContainsHandle(handle)) {
1088 window_tracker_->Remove(window_tracker_->GetResource(handle));
1089 }
1090}
1091
1092void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161093 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571094 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291095}
1096
1097// TODO(brettw) change this to accept GURLs when history supports it
1098void AutomationProvider::OnRedirectQueryComplete(
1099 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371100 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291101 bool success,
[email protected]379c2b12009-07-01 21:50:331102 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291103 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561104 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291105
[email protected]deb57402009-02-06 01:35:301106 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101107 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291108 if (success) {
initial.commit09911bf2008-07-26 23:55:291109 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301110 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291111 }
1112
[email protected]4f3dc372009-02-24 00:10:291113 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301114
[email protected]71f65dd2009-02-11 19:14:561115 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:171116 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561117 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291118}
1119
1120bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571121 DCHECK(channel_.get());
1122 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291123}
1124
1125Browser* AutomationProvider::FindAndActivateTab(
1126 NavigationController* controller) {
1127 int tab_index;
1128 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1129 if (browser)
1130 browser->SelectTabContentsAt(tab_index, true);
1131
1132 return browser;
1133}
1134
[email protected]71f65dd2009-02-11 19:14:561135void AutomationProvider::GetCookies(const GURL& url, int handle,
1136 int* value_size,
1137 std::string* value) {
1138 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291139 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1140 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311141
1142 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001143 scoped_refptr<URLRequestContextGetter> request_context =
1144 tab->tab_contents()->request_context();
1145 if (!request_context.get())
1146 request_context = tab->profile()->GetRequestContext();
1147
1148 net::CookieStore* cookie_store = request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311149
1150 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561151 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291152 }
initial.commit09911bf2008-07-26 23:55:291153}
1154
[email protected]71f65dd2009-02-11 19:14:561155void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291156 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561157 int handle,
1158 int* response_value) {
1159 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291160
1161 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1162 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311163
[email protected]dfa46e5f2009-11-17 18:48:431164 scoped_refptr<URLRequestContextGetter> request_context =
1165 tab->tab_contents()->request_context();
1166 if (!request_context.get())
1167 request_context = tab->profile()->GetRequestContext();
1168
[email protected]be180c802009-10-23 06:33:311169 // Since we are running on the UI thread don't call GetURLRequestContext().
1170 scoped_refptr<net::CookieStore> cookie_store =
[email protected]dfa46e5f2009-11-17 18:48:431171 request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311172
1173 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561174 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291175 }
initial.commit09911bf2008-07-26 23:55:291176}
1177
[email protected]5fa57942010-04-21 23:07:221178void AutomationProvider::DeleteCookie(const GURL& url,
1179 const std::string& cookie_name,
1180 int handle, bool* success) {
1181 *success = false;
1182 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1183 NavigationController* tab = tab_tracker_->GetResource(handle);
1184 net::CookieStore* cookie_store =
1185 tab->profile()->GetRequestContext()->GetCookieStore();
1186 cookie_store->DeleteCookie(url, cookie_name);
1187 *success = true;
1188 }
1189}
1190
[email protected]71f65dd2009-02-11 19:14:561191void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1192 *success = false;
initial.commit09911bf2008-07-26 23:55:291193 if (tab_tracker_->ContainsHandle(handle)) {
1194 NavigationController* tab = tab_tracker_->GetResource(handle);
1195 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541196 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561197 *success = true;
initial.commit09911bf2008-07-26 23:55:291198 }
initial.commit09911bf2008-07-26 23:55:291199}
1200
[email protected]71f65dd2009-02-11 19:14:561201void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1202 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291203
1204 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561205 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341206 TabContents* tab_contents =
1207 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161208 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1209 if (rph)
1210 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291211 }
initial.commit09911bf2008-07-26 23:55:291212}
1213
1214void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331215 NOTREACHED() << "This function has been deprecated. "
1216 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291217}
1218
[email protected]71f65dd2009-02-11 19:14:561219void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291220 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561221 const std::wstring& script,
1222 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291223 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341224 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1225 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571226 // Set the routing id of this message with the controller.
1227 // This routing id needs to be remembered for the reverse
1228 // communication while sending back the response of
1229 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331230 std::wstring set_automation_id;
1231 SStringPrintf(&set_automation_id,
1232 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561233 reply_message->routing_id());
1234
1235 DCHECK(reply_message_ == NULL);
1236 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291237
[email protected]57c6a652009-05-04 07:58:341238 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331239 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341240 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061241 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571242 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291243 }
1244
1245 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561246 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1247 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291248 }
1249}
1250
[email protected]71f65dd2009-02-11 19:14:561251void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1252 *visible = false;
[email protected]20e93d12008-08-28 16:31:571253
[email protected]59560e0b2009-06-04 03:30:221254 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251255#if defined(OS_CHROMEOS)
1256 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1257 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1258 for (BrowserList::const_iterator it = BrowserList::begin();
1259 it != BrowserList::end(); ++it) {
1260 if ((*it)->type() == Browser::TYPE_POPUP) {
1261 const GURL& url =
1262 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1263
1264 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1265 url.host() == chrome::kChromeUIFileBrowseHost) {
1266 *visible = true;
1267 break;
1268 }
1269 }
1270 }
1271#else
[email protected]59560e0b2009-06-04 03:30:221272 Browser* browser = browser_tracker_->GetResource(handle);
1273 if (browser) {
1274 *visible = browser->window()->IsDownloadShelfVisible();
1275 }
[email protected]f5bf8ccf2010-02-05 18:19:251276#endif
[email protected]59560e0b2009-06-04 03:30:221277 }
initial.commit09911bf2008-07-26 23:55:291278}
1279
[email protected]59560e0b2009-06-04 03:30:221280void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1281 if (browser_tracker_->ContainsHandle(handle)) {
1282 Browser* browser = browser_tracker_->GetResource(handle);
1283 if (browser) {
1284 if (visible)
1285 browser->window()->GetDownloadShelf()->Show();
1286 else
1287 browser->window()->GetDownloadShelf()->Close();
1288 }
1289 }
1290}
1291
[email protected]34930432009-11-09 00:12:091292void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1293 *visible = false;
1294
1295 if (browser_tracker_->ContainsHandle(handle)) {
1296 Browser* browser = browser_tracker_->GetResource(handle);
1297 if (browser)
1298 *visible = browser->window()->IsFullscreen();
1299 }
1300}
1301
1302void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1303 bool* visible) {
1304 *visible = false;
1305
1306 if (browser_tracker_->ContainsHandle(handle)) {
1307 Browser* browser = browser_tracker_->GetResource(handle);
1308 if (browser)
1309 *visible = browser->window()->IsFullscreenBubbleVisible();
1310 }
1311}
[email protected]59560e0b2009-06-04 03:30:221312
[email protected]71f65dd2009-02-11 19:14:561313void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1314 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291315 if (tab_tracker_->ContainsHandle(handle)) {
1316 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111317 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291318 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561319 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291320 }
1321 }
initial.commit09911bf2008-07-26 23:55:291322}
1323
initial.commit09911bf2008-07-26 23:55:291324void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561325 int handle, const std::wstring& find_request,
1326 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311327 NOTREACHED() << "This function has been deprecated."
1328 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561329 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311330 return;
1331}
1332
[email protected]4f999132009-03-31 18:08:401333void AutomationProvider::HandleFindRequest(
1334 int handle,
1335 const AutomationMsg_Find_Params& params,
1336 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291337 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561338 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1339 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291340 return;
1341 }
1342
1343 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111344 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291345
1346 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141347 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291348
[email protected]57c6a652009-05-04 07:58:341349 tab_contents->set_current_find_request_id(
1350 FindInPageNotificationObserver::kFindInPageRequestId);
1351 tab_contents->render_view_host()->StartFinding(
1352 FindInPageNotificationObserver::kFindInPageRequestId,
1353 params.search_string, params.forward, params.match_case,
1354 params.find_next);
initial.commit09911bf2008-07-26 23:55:291355}
1356
[email protected]5f8af2a2008-08-06 22:49:451357void AutomationProvider::HandleOpenFindInPageRequest(
1358 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291359 if (browser_tracker_->ContainsHandle(handle)) {
1360 Browser* browser = browser_tracker_->GetResource(handle);
1361 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451362 }
1363}
1364
[email protected]71f65dd2009-02-11 19:14:561365void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561366 *visible = false;
[email protected]855c0142009-09-28 22:35:241367 Browser* browser = browser_tracker_->GetResource(handle);
1368 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581369 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171370 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241371 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291372 }
[email protected]20e93d12008-08-28 16:31:571373}
1374
[email protected]71f65dd2009-02-11 19:14:561375void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1376 int* y) {
[email protected]9e0534b2008-10-21 15:03:011377 gfx::Point position(0, 0);
1378 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291379 if (browser_tracker_->ContainsHandle(handle)) {
1380 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581381 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171382 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581383 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291384 }
[email protected]20e93d12008-08-28 16:31:571385
[email protected]71f65dd2009-02-11 19:14:561386 *x = position.x();
1387 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571388}
1389
[email protected]4512cb52010-04-05 19:50:251390// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1391// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581392void AutomationProvider::GetBookmarkBarVisibility(int handle,
1393 bool* visible,
1394 bool* animating) {
1395 *visible = false;
1396 *animating = false;
1397
1398 if (browser_tracker_->ContainsHandle(handle)) {
1399 Browser* browser = browser_tracker_->GetResource(handle);
1400 if (browser) {
[email protected]ab6ca392010-04-07 00:44:131401#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1402 // IsBookmarkBarVisible() line looks correct but is not
1403 // consistent across platforms. Specifically, on Mac/Linux, it
1404 // returns false if the bar is hidden in a pref (even if visible
1405 // on the NTP). On ChromeOS, it returned true if on NTP
1406 // independent of the pref. Making the code more consistent
1407 // caused a perf bot regression on Windows (which shares views).
1408 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251409 *visible = browser->profile()->GetPrefs()->GetBoolean(
1410 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401411#else
1412 *visible = browser->window()->IsBookmarkBarVisible();
1413#endif
[email protected]c3240722010-03-05 21:52:581414 *animating = browser->window()->IsBookmarkBarAnimating();
1415 }
1416 }
1417}
1418
[email protected]6d8ffc9f2010-03-12 18:27:531419void AutomationProvider::GetBookmarksAsJSON(int handle,
1420 std::string* bookmarks_as_json,
1421 bool *success) {
1422 *success = false;
1423 if (browser_tracker_->ContainsHandle(handle)) {
1424 Browser* browser = browser_tracker_->GetResource(handle);
1425 if (browser) {
1426 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1427 return;
1428 }
1429 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1430 browser->profile(),
1431 browser->profile()->GetBookmarkModel());
1432 *success = storage->SerializeData(bookmarks_as_json);
1433 }
1434 }
1435}
1436
1437void AutomationProvider::WaitForBookmarkModelToLoad(
1438 int handle,
1439 IPC::Message* reply_message) {
1440 if (browser_tracker_->ContainsHandle(handle)) {
1441 Browser* browser = browser_tracker_->GetResource(handle);
1442 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1443 if (model->IsLoaded()) {
1444 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1445 reply_message, true);
1446 Send(reply_message);
1447 } else {
1448 // The observer will delete itself when done.
1449 new AutomationProviderBookmarkModelObserver(this, reply_message,
1450 model);
1451 }
1452 }
1453}
1454
1455void AutomationProvider::AddBookmarkGroup(int handle,
1456 int64 parent_id, int index,
1457 std::wstring title,
1458 bool* success) {
1459 if (browser_tracker_->ContainsHandle(handle)) {
1460 Browser* browser = browser_tracker_->GetResource(handle);
1461 if (browser) {
1462 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1463 if (!model->IsLoaded()) {
1464 *success = false;
1465 return;
1466 }
1467 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1468 DCHECK(parent);
1469 if (parent) {
1470 const BookmarkNode* child = model->AddGroup(parent, index,
1471 WideToUTF16(title));
1472 DCHECK(child);
1473 if (child)
1474 *success = true;
1475 }
1476 }
1477 }
1478 *success = false;
1479}
1480
1481void AutomationProvider::AddBookmarkURL(int handle,
1482 int64 parent_id, int index,
1483 std::wstring title, const GURL& url,
1484 bool* success) {
1485 if (browser_tracker_->ContainsHandle(handle)) {
1486 Browser* browser = browser_tracker_->GetResource(handle);
1487 if (browser) {
1488 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1489 if (!model->IsLoaded()) {
1490 *success = false;
1491 return;
1492 }
1493 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1494 DCHECK(parent);
1495 if (parent) {
1496 const BookmarkNode* child = model->AddURL(parent, index,
1497 WideToUTF16(title), url);
1498 DCHECK(child);
1499 if (child)
1500 *success = true;
1501 }
1502 }
1503 }
1504 *success = false;
1505}
1506
1507void AutomationProvider::ReparentBookmark(int handle,
1508 int64 id, int64 new_parent_id,
1509 int index,
1510 bool* success) {
1511 if (browser_tracker_->ContainsHandle(handle)) {
1512 Browser* browser = browser_tracker_->GetResource(handle);
1513 if (browser) {
1514 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1515 if (!model->IsLoaded()) {
1516 *success = false;
1517 return;
1518 }
1519 const BookmarkNode* node = model->GetNodeByID(id);
1520 DCHECK(node);
1521 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1522 DCHECK(new_parent);
1523 if (node && new_parent) {
1524 model->Move(node, new_parent, index);
1525 *success = true;
1526 }
1527 }
1528 }
1529 *success = false;
1530}
1531
1532void AutomationProvider::SetBookmarkTitle(int handle,
1533 int64 id, std::wstring title,
1534 bool* success) {
1535 if (browser_tracker_->ContainsHandle(handle)) {
1536 Browser* browser = browser_tracker_->GetResource(handle);
1537 if (browser) {
1538 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1539 if (!model->IsLoaded()) {
1540 *success = false;
1541 return;
1542 }
1543 const BookmarkNode* node = model->GetNodeByID(id);
1544 DCHECK(node);
1545 if (node) {
1546 model->SetTitle(node, WideToUTF16(title));
1547 *success = true;
1548 }
1549 }
1550 }
1551 *success = false;
1552}
1553
1554void AutomationProvider::SetBookmarkURL(int handle,
1555 int64 id, const GURL& url,
1556 bool* success) {
1557 if (browser_tracker_->ContainsHandle(handle)) {
1558 Browser* browser = browser_tracker_->GetResource(handle);
1559 if (browser) {
1560 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1561 if (!model->IsLoaded()) {
1562 *success = false;
1563 return;
1564 }
1565 const BookmarkNode* node = model->GetNodeByID(id);
1566 DCHECK(node);
1567 if (node) {
1568 model->SetURL(node, url);
1569 *success = true;
1570 }
1571 }
1572 }
1573 *success = false;
1574}
1575
1576void AutomationProvider::RemoveBookmark(int handle,
1577 int64 id,
1578 bool* success) {
1579 if (browser_tracker_->ContainsHandle(handle)) {
1580 Browser* browser = browser_tracker_->GetResource(handle);
1581 if (browser) {
1582 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1583 if (!model->IsLoaded()) {
1584 *success = false;
1585 return;
1586 }
1587 const BookmarkNode* node = model->GetNodeByID(id);
1588 DCHECK(node);
1589 if (node) {
1590 const BookmarkNode* parent = node->GetParent();
1591 DCHECK(parent);
1592 model->Remove(parent, parent->IndexOfChild(node));
1593 *success = true;
1594 }
1595 }
1596 }
1597 *success = false;
1598}
1599
[email protected]24e2b102010-04-29 17:56:471600// Sample json input: { "command": "GetHistoryInfo",
1601// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361602// Refer chrome/test/pyautolib/history_info.py for sample json output.
1603void AutomationProvider::GetHistoryInfo(
1604 DictionaryValue* args,
1605 IPC::Message* reply_message) {
1606 consumer_.CancelAllRequests();
1607
1608 std::wstring search_text;
1609 args->GetString(L"search_text", &search_text);
1610
1611 // Fetch history.
1612 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1613 history::QueryOptions options;
1614 // The observer owns itself. It deletes itself after it fetches history.
1615 AutomationProviderHistoryObserver* history_observer =
1616 new AutomationProviderHistoryObserver(this, reply_message);
1617 hs->QueryHistory(
1618 search_text,
1619 options,
1620 &consumer_,
1621 NewCallback(history_observer,
1622 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1623}
1624
[email protected]24e2b102010-04-29 17:56:471625// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361626// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]d4adc292010-04-15 18:06:391627void AutomationProvider::GetDownloadsInfo(
1628 DictionaryValue* args,
1629 IPC::Message* reply_message) {
1630 std::string json_return;
1631 bool reply_return = true;
1632 AutomationProviderDownloadManagerObserver observer;
1633 std::vector<DownloadItem*> downloads;
1634 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1635
1636 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471637 json_return = "{\"error\": \"no download manager\"}";
[email protected]d4adc292010-04-15 18:06:391638 reply_return = false;
1639 } else {
1640 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1641 // since that would be transient; a download might enter and empty out
1642 // the current download queue too soon to be noticed.
1643 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1644 downloads = observer.Downloads();
1645 }
1646
1647 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1648 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1649 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1650 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1651 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1652
1653 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1654 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1655 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1656 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1657 std::string("DANGEROUS_BUT_VALIDATED");
1658
1659 ListValue* list_of_downloads = new ListValue;
1660 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1661 it != downloads.end();
1662 it++) { // Fill info about each download item.
1663 DictionaryValue* dl_item_value = new DictionaryValue;
1664 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1665 dl_item_value->SetString(L"url", (*it)->url().spec());
1666 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1667 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1668 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1669 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1670 dl_item_value->SetBoolean(L"open_when_complete",
1671 (*it)->open_when_complete());
1672 dl_item_value->SetBoolean(L"is_extension_install",
1673 (*it)->is_extension_install());
1674 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1675 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1676 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1677 dl_item_value->SetString(L"safety_state",
1678 safety_state_to_string[(*it)->safety_state()]);
1679 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1680 list_of_downloads->Append(dl_item_value);
1681 }
1682 return_value->Set(L"downloads", list_of_downloads);
1683 base::JSONWriter::Write(return_value.get(), false, &json_return);
1684
1685 AutomationMsg_SendJSONRequest::WriteReplyParams(
1686 reply_message, json_return, reply_return);
1687 Send(reply_message);
1688 // All value objects allocated above are owned by |return_value|
1689 // and get freed by it.
1690}
1691
[email protected]59a611242010-04-02 02:24:041692void AutomationProvider::WaitForDownloadsToComplete(
1693 DictionaryValue* args,
1694 IPC::Message* reply_message) {
1695 std::string json_return;
1696 bool reply_return = true;
1697 AutomationProviderDownloadManagerObserver observer;
1698 std::vector<DownloadItem*> downloads;
1699
1700 // Look for a quick return.
1701 if (!profile_->HasCreatedDownloadManager()) {
[email protected]24e2b102010-04-29 17:56:471702 json_return = "{\"error\": \"no download manager\"}";
[email protected]59a611242010-04-02 02:24:041703 reply_return = false;
1704 } else {
1705 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
1706 FilePath());
1707 downloads = observer.Downloads();
1708 if (downloads.size() == 0) {
1709 json_return = "{}";
1710 }
1711 }
1712 if (!json_return.empty()) {
1713 AutomationMsg_SendJSONRequest::WriteReplyParams(
1714 reply_message, json_return, reply_return);
1715 Send(reply_message);
1716 }
1717
1718 // The observer owns itself. When the last observed item pings, it
1719 // deletes itself.
1720 AutomationProviderDownloadItemObserver* item_observer =
1721 new AutomationProviderDownloadItemObserver(
1722 this, reply_message, downloads.size());
1723 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
1724 i != downloads.end();
1725 i++) {
1726 (*i)->AddObserver(item_observer);
1727 }
1728}
1729
[email protected]24e2b102010-04-29 17:56:471730// Sample json input: { "command": "GetPrefsInfo" }
1731// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
1732void AutomationProvider::GetPrefsInfo(DictionaryValue* args,
1733 IPC::Message* reply_message) {
1734 std::string json_return;
1735 bool reply_return = true;
1736
1737 const PrefService::PreferenceSet& prefs =
1738 profile_->GetPrefs()->preference_set();
1739 DictionaryValue* items = new DictionaryValue;
1740 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
1741 it != prefs.end(); ++it) {
1742 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
1743 }
1744 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1745 return_value->Set(L"prefs", items); // return_value owns items.
1746
1747 base::JSONWriter::Write(return_value.get(), false, &json_return);
1748 AutomationMsg_SendJSONRequest::WriteReplyParams(
1749 reply_message, json_return, reply_return);
1750 Send(reply_message);
1751}
1752
1753// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
1754void AutomationProvider::SetPrefs(DictionaryValue* args,
1755 IPC::Message* reply_message) {
1756 bool reply_return = true;
1757 std::string json_return = "{}";
1758 std::wstring path;
1759 Value* val;
1760 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
1761 PrefService* pref_service = profile_->GetPrefs();
1762 const PrefService::Preference* pref =
1763 pref_service->FindPreference(path.c_str());
1764 if (!pref) { // Not a registered pref.
1765 json_return = "{\"error\": \"pref not registered.\"}";
1766 reply_return = false;
1767 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
1768 json_return = "{\"error\": \"pref is managed. cannot be changed.\"}";
1769 reply_return = false;
1770 } else { // Set the pref.
1771 pref_service->Set(path.c_str(), *val);
1772 }
1773 } else {
1774 json_return = "{\"error\": \"no pref path or value given.\"}";
1775 reply_return = false;
1776 }
1777
1778 AutomationMsg_SendJSONRequest::WriteReplyParams(
1779 reply_message, json_return, reply_return);
1780 Send(reply_message);
1781}
1782
[email protected]59a611242010-04-02 02:24:041783void AutomationProvider::SendJSONRequest(
1784 int handle,
1785 std::string json_request,
1786 IPC::Message* reply_message) {
1787 Browser* browser = NULL;
1788 std::string error_string;
1789 scoped_ptr<Value> values;
1790
1791 // Basic error checking.
1792 if (browser_tracker_->ContainsHandle(handle)) {
1793 browser = browser_tracker_->GetResource(handle);
1794 }
1795 if (!browser) {
1796 error_string = "no browser object";
1797 } else {
1798 base::JSONReader reader;
1799 std::string error;
[email protected]ba399672010-04-06 15:42:391800 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
[email protected]59a611242010-04-02 02:24:041801 if (!error.empty()) {
1802 error_string = error;
1803 }
1804 }
1805
1806 // Make sure input is a dict with a string command.
1807 std::string command;
1808 DictionaryValue* dict_value = NULL;
1809 if (error_string.empty()) {
1810 if (values->GetType() != Value::TYPE_DICTIONARY) {
1811 error_string = "not a dict or no command key in dict";
1812 } else {
1813 // Ownership remains with "values" variable.
1814 dict_value = static_cast<DictionaryValue*>(values.get());
1815 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
1816 error_string = "no command key in dict or not a string command";
1817 }
1818 }
1819 }
1820
[email protected]24e2b102010-04-29 17:56:471821 // Map json commands to their handlers.
1822 std::map<std::string, JsonHandler> handler_map;
1823 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
1824 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
1825 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
1826 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
1827 handler_map["WaitForAllDownloadsToComplete"] =
1828 &AutomationProvider::WaitForDownloadsToComplete;
1829
[email protected]59a611242010-04-02 02:24:041830 if (error_string.empty()) {
[email protected]24e2b102010-04-29 17:56:471831 if (handler_map.find(std::string(command)) != handler_map.end()) {
1832 (this->*handler_map[command])(dict_value, reply_message);
[email protected]59a611242010-04-02 02:24:041833 return;
1834 } else {
[email protected]24e2b102010-04-29 17:56:471835 error_string = "Unknown command. Options: ";
1836 for (std::map<std::string, JsonHandler>::const_iterator it =
1837 handler_map.begin(); it != handler_map.end(); ++it) {
1838 error_string += it->first + ", ";
1839 }
[email protected]59a611242010-04-02 02:24:041840 }
1841 }
1842
1843 // If we hit an error, return info.
[email protected]24e2b102010-04-29 17:56:471844 // Return a dict of {"error", "descriptive_string_for_error"}.
[email protected]59a611242010-04-02 02:24:041845 // Else return an empty dict.
1846 std::string json_string;
1847 bool success = true;
1848 if (!error_string.empty()) {
1849 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
1850 dict->SetString(L"error", error_string);
1851 base::JSONWriter::Write(dict.get(), false, &json_string);
1852 success = false;
1853 } else {
1854 json_string = "{}";
1855 }
1856 AutomationMsg_SendJSONRequest::WriteReplyParams(
1857 reply_message, json_string, success);
1858 Send(reply_message);
1859}
1860
initial.commit09911bf2008-07-26 23:55:291861void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561862 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341863 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1864 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561865 DCHECK(reply_message_ == NULL);
1866 reply_message_ = reply_message;
1867
[email protected]d9f9b792009-06-24 13:17:121868 DevToolsManager::GetInstance()->InspectElement(
1869 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291870 } else {
[email protected]71f65dd2009-02-11 19:14:561871 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1872 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291873 }
1874}
1875
1876void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091877 if (reply_message_) {
1878 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1879 num_resources);
1880 Send(reply_message_);
1881 reply_message_ = NULL;
1882 }
initial.commit09911bf2008-07-26 23:55:291883}
1884
[email protected]a7eee32f2009-05-22 18:08:171885class SetProxyConfigTask : public Task {
1886 public:
[email protected]be180c802009-10-23 06:33:311887 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
1888 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:251889 : request_context_getter_(request_context_getter),
1890 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:171891 virtual void Run() {
1892 // First, deserialize the JSON string. If this fails, log and bail.
1893 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:391894 std::string error_msg;
1895 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:171896 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1897 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:391898 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:171899 return;
1900 }
1901
1902 scoped_ptr<DictionaryValue> dict(
1903 static_cast<DictionaryValue*>(root.release()));
1904 // Now put together a proxy configuration from the deserialized string.
1905 net::ProxyConfig pc;
1906 PopulateProxyConfig(*dict.get(), &pc);
1907
[email protected]be180c802009-10-23 06:33:311908 net::ProxyService* proxy_service =
1909 request_context_getter_->GetURLRequestContext()->proxy_service();
1910 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:171911 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1912 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:311913 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:171914 }
1915
1916 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1917 DCHECK(pc);
1918 bool no_proxy = false;
1919 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1920 // Make no changes to the ProxyConfig.
1921 return;
1922 }
1923 bool auto_config;
1924 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:481925 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:171926 }
1927 std::string pac_url;
1928 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:481929 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:171930 }
1931 std::string proxy_bypass_list;
1932 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:481933 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:171934 }
1935 std::string proxy_server;
1936 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:481937 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:171938 }
1939 }
1940
1941 private:
[email protected]be180c802009-10-23 06:33:311942 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:171943 std::string proxy_config_;
1944};
1945
1946
1947void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:311948 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
1949 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:171950 FilePath user_data_dir;
1951 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1952 ProfileManager* profile_manager = g_browser_process->profile_manager();
1953 DCHECK(profile_manager);
1954 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1955 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:311956 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171957 }
[email protected]be180c802009-10-23 06:33:311958 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:171959
[email protected]fae20792009-10-28 20:31:581960 ChromeThread::PostTask(
1961 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:311962 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171963}
1964
[email protected]4f3dc372009-02-24 00:10:291965void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331966 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291967 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291968 if (tab_tracker_->ContainsHandle(handle)) {
1969 NavigationController* tab = tab_tracker_->GetResource(handle);
1970 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1971 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331972 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291973 }
initial.commit09911bf2008-07-26 23:55:291974}
1975
[email protected]6a5670d22009-10-27 16:21:341976void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:141977 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:531978 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
1979 reply_message);
1980}
1981
1982void AutomationProvider::OpenNewBrowserWindowOfType(
1983 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:141984 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291985 // We may have no current browser windows open so don't rely on
1986 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:531987 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
1988 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:051989 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151990 if (show)
[email protected]15952e462008-11-14 00:29:051991 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291992}
1993
[email protected]71f65dd2009-02-11 19:14:561994void AutomationProvider::GetWindowForBrowser(int browser_handle,
1995 bool* success,
1996 int* handle) {
1997 *success = false;
1998 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291999
2000 if (browser_tracker_->ContainsHandle(browser_handle)) {
2001 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202002 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292003 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202004 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562005 *success = true;
initial.commit09911bf2008-07-26 23:55:292006 }
initial.commit09911bf2008-07-26 23:55:292007}
2008
2009void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562010 int browser_handle,
2011 bool* success,
2012 int* autocomplete_edit_handle) {
2013 *success = false;
2014 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292015
2016 if (browser_tracker_->ContainsHandle(browser_handle)) {
2017 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062018 LocationBar* loc_bar = browser->window()->GetLocationBar();
2019 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292020 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562021 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2022 *success = true;
initial.commit09911bf2008-07-26 23:55:292023 }
initial.commit09911bf2008-07-26 23:55:292024}
initial.commit09911bf2008-07-26 23:55:292025
[email protected]71f65dd2009-02-11 19:14:562026void AutomationProvider::ShowInterstitialPage(int tab_handle,
2027 const std::string& html_text,
2028 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292029 if (tab_tracker_->ContainsHandle(tab_handle)) {
2030 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112031 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402032
[email protected]7dad3d5f2010-03-04 00:27:012033 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:402034 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342035 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402036 GURL("about:interstitial"),
2037 html_text);
2038 interstitial->Show();
2039 return;
initial.commit09911bf2008-07-26 23:55:292040 }
[email protected]71f65dd2009-02-11 19:14:562041
[email protected]457f5cf2009-08-18 16:37:522042 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
2043 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562044 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292045}
2046
[email protected]71f65dd2009-02-11 19:14:562047void AutomationProvider::HideInterstitialPage(int tab_handle,
2048 bool* success) {
2049 *success = false;
[email protected]57c6a652009-05-04 07:58:342050 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2051 if (tab_contents && tab_contents->interstitial_page()) {
2052 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562053 *success = true;
initial.commit09911bf2008-07-26 23:55:292054 }
initial.commit09911bf2008-07-26 23:55:292055}
2056
[email protected]71f65dd2009-02-11 19:14:562057void AutomationProvider::CloseTab(int tab_handle,
2058 bool wait_until_closed,
2059 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292060 if (tab_tracker_->ContainsHandle(tab_handle)) {
2061 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2062 int index;
2063 Browser* browser = Browser::GetBrowserForController(controller, &index);
2064 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142065 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112066 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452067 return;
initial.commit09911bf2008-07-26 23:55:292068 }
[email protected]de246f52009-02-25 18:25:452069
2070 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:552071 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292072}
2073
[email protected]71f65dd2009-02-11 19:14:562074void AutomationProvider::CloseBrowser(int browser_handle,
2075 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292076 if (browser_tracker_->ContainsHandle(browser_handle)) {
2077 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562078 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562079 reply_message);
[email protected]f3e99e32008-07-30 04:48:392080 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292081 } else {
2082 NOTREACHED();
2083 }
2084}
2085
[email protected]71f65dd2009-02-11 19:14:562086void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2087 if (browser_tracker_->ContainsHandle(browser_handle)) {
2088 Browser* browser = browser_tracker_->GetResource(browser_handle);
2089 browser->window()->Close();
2090 } else {
2091 NOTREACHED();
2092 }
2093}
2094
[email protected]71f65dd2009-02-11 19:14:562095void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:572096 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:562097 AutomationMsg_NavigationResponseValues* status) {
2098 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:292099
2100 if (tab_tracker_->ContainsHandle(handle)) {
2101 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:572102 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:562103 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:292104 }
initial.commit09911bf2008-07-26 23:55:292105}
2106
[email protected]4150ef02009-08-19 23:14:262107void AutomationProvider::NavigateExternalTabAtIndex(
2108 int handle, int navigation_index,
2109 AutomationMsg_NavigationResponseValues* status) {
2110 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
2111
2112 if (tab_tracker_->ContainsHandle(handle)) {
2113 NavigationController* tab = tab_tracker_->GetResource(handle);
2114 tab->GoToIndex(navigation_index);
2115 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
2116 }
2117}
2118
[email protected]71f65dd2009-02-11 19:14:562119void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2120 IPC::Message* reply_message) {
2121 if (tab_tracker_->ContainsHandle(tab_handle)) {
2122 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2123 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:142124 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:562125 }
2126}
2127
[email protected]71f65dd2009-02-11 19:14:562128void AutomationProvider::GetSecurityState(int handle, bool* success,
2129 SecurityStyle* security_style,
2130 int* ssl_cert_status,
2131 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:292132 if (tab_tracker_->ContainsHandle(handle)) {
2133 NavigationController* tab = tab_tracker_->GetResource(handle);
2134 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562135 *success = true;
2136 *security_style = entry->ssl().security_style();
2137 *ssl_cert_status = entry->ssl().cert_status();
2138 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292139 } else {
[email protected]71f65dd2009-02-11 19:14:562140 *success = false;
2141 *security_style = SECURITY_STYLE_UNKNOWN;
2142 *ssl_cert_status = 0;
2143 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292144 }
2145}
2146
[email protected]71f65dd2009-02-11 19:14:562147void AutomationProvider::GetPageType(int handle, bool* success,
2148 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:292149 if (tab_tracker_->ContainsHandle(handle)) {
2150 NavigationController* tab = tab_tracker_->GetResource(handle);
2151 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562152 *page_type = entry->page_type();
2153 *success = true;
initial.commit09911bf2008-07-26 23:55:292154 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:342155 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:562156 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:342157 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:562158 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292159 } else {
[email protected]71f65dd2009-02-11 19:14:562160 *success = false;
2161 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292162 }
2163}
2164
[email protected]84abba62009-10-07 17:01:442165void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
2166 int* duration_ms) {
2167 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
2168 event_name);
2169}
2170
[email protected]71f65dd2009-02-11 19:14:562171void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
2172 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292173 if (tab_tracker_->ContainsHandle(handle)) {
2174 NavigationController* tab = tab_tracker_->GetResource(handle);
2175 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072176 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:402177 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:472178 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:342179 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:292180 if (ssl_blocking_page) {
2181 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:012182 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:562183 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:292184 return;
2185 }
2186 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:522187 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2188 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:562189 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292190 return;
2191 }
2192 }
2193 }
2194 // We failed.
[email protected]457f5cf2009-08-18 16:37:522195 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
2196 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562197 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292198}
2199
[email protected]71f65dd2009-02-11 19:14:562200void AutomationProvider::BringBrowserToFront(int browser_handle,
2201 bool* success) {
initial.commit09911bf2008-07-26 23:55:292202 if (browser_tracker_->ContainsHandle(browser_handle)) {
2203 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:062204 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:562205 *success = true;
initial.commit09911bf2008-07-26 23:55:292206 } else {
[email protected]71f65dd2009-02-11 19:14:562207 *success = false;
initial.commit09911bf2008-07-26 23:55:292208 }
2209}
2210
[email protected]71f65dd2009-02-11 19:14:562211void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
2212 int message_num,
2213 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:292214 if (browser_tracker_->ContainsHandle(browser_handle)) {
2215 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562216 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:142217 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:292218 } else {
[email protected]71f65dd2009-02-11 19:14:562219 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:292220 }
2221}
2222
[email protected]71f65dd2009-02-11 19:14:562223void AutomationProvider::PrintNow(int tab_handle,
2224 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:442225#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:572226 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:342227 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2228 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:292229 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572230 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:142231 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:342232 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:572233 return;
initial.commit09911bf2008-07-26 23:55:292234 }
[email protected]71f65dd2009-02-11 19:14:562235 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
2236 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:442237#else
2238 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
2239 NOTIMPLEMENTED();
2240#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292241}
[email protected]d301c952009-07-13 15:02:412242
[email protected]71f65dd2009-02-11 19:14:562243void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:112244 const FilePath& file_name,
2245 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:562246 int type,
2247 bool* success) {
initial.commit09911bf2008-07-26 23:55:292248 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562249 *success = false;
initial.commit09911bf2008-07-26 23:55:292250 return;
2251 }
2252
2253 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2254 Browser* browser = FindAndActivateTab(nav);
2255 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142256 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562257 *success = false;
initial.commit09911bf2008-07-26 23:55:292258 return;
2259 }
2260
initial.commit09911bf2008-07-26 23:55:292261 SavePackage::SavePackageType save_type =
2262 static_cast<SavePackage::SavePackageType>(type);
2263 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2264 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:342265 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292266
[email protected]71f65dd2009-02-11 19:14:562267 *success = true;
initial.commit09911bf2008-07-26 23:55:292268}
2269
[email protected]71f65dd2009-02-11 19:14:562270void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2271 bool* success,
2272 std::wstring* text) {
2273 *success = false;
initial.commit09911bf2008-07-26 23:55:292274 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562275 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522276 GetText();
[email protected]71f65dd2009-02-11 19:14:562277 *success = true;
initial.commit09911bf2008-07-26 23:55:292278 }
initial.commit09911bf2008-07-26 23:55:292279}
2280
[email protected]71f65dd2009-02-11 19:14:562281void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2282 const std::wstring& text,
2283 bool* success) {
2284 *success = false;
initial.commit09911bf2008-07-26 23:55:292285 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522286 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2287 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562288 *success = true;
initial.commit09911bf2008-07-26 23:55:292289 }
initial.commit09911bf2008-07-26 23:55:292290}
2291
2292void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562293 int autocomplete_edit_handle,
2294 bool* success,
2295 std::vector<AutocompleteMatchData>* matches) {
2296 *success = false;
initial.commit09911bf2008-07-26 23:55:292297 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272298 const AutocompleteResult& result = autocomplete_edit_tracker_->
2299 GetResource(autocomplete_edit_handle)->model()->result();
2300 for (AutocompleteResult::const_iterator i = result.begin();
2301 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562302 matches->push_back(AutocompleteMatchData(*i));
2303 *success = true;
initial.commit09911bf2008-07-26 23:55:292304 }
initial.commit09911bf2008-07-26 23:55:292305}
2306
2307void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562308 int autocomplete_edit_handle,
2309 bool* success,
2310 bool* query_in_progress) {
2311 *success = false;
2312 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292313 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562314 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522315 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562316 *success = true;
initial.commit09911bf2008-07-26 23:55:292317 }
initial.commit09911bf2008-07-26 23:55:292318}
2319
[email protected]63514af2010-03-30 17:17:232320#if !defined(OS_MACOSX)
[email protected]28790922009-03-09 19:48:372321void AutomationProvider::OnMessageFromExternalHost(int handle,
2322 const std::string& message,
2323 const std::string& origin,
2324 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:192325 RenderViewHost* view_host = GetViewForTab(handle);
2326 if (!view_host) {
2327 return;
[email protected]fa83e762008-08-15 21:41:392328 }
[email protected]f7a68432009-07-29 23:18:192329
2330 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
2331 view_host, message, origin, target)) {
2332 // Message was diverted.
2333 return;
2334 }
2335
2336 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
2337 origin, target, this, view_host, handle)) {
2338 // Message was diverted.
2339 return;
2340 }
2341
2342 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
2343 // Message was diverted.
2344 return;
2345 }
2346
2347 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:392348}
[email protected]a9024892009-06-16 23:13:552349
2350bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
2351 const std::string& message, const std::string& origin,
2352 const std::string& target) {
2353 if (target !=
2354 extension_automation_constants::kAutomationBrowserEventRequestTarget)
2355 return false;
2356
2357 if (origin != extension_automation_constants::kAutomationOrigin) {
2358 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
2359 return false;
2360 }
2361
2362 // The message is a JSON-encoded array with two elements, both strings. The
2363 // first is the name of the event to dispatch. The second is a JSON-encoding
2364 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:202365 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:552366 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
2367 LOG(WARNING) << "Invalid browser event specified through automation";
2368 return false;
2369 }
2370
2371 const ListValue* args = static_cast<const ListValue*>(message_value.get());
2372
2373 std::string event_name;
2374 if (!args->GetString(0, &event_name)) {
2375 LOG(WARNING) << "No browser event name specified through automation";
2376 return false;
2377 }
2378
2379 std::string json_args;
2380 if (!args->GetString(1, &json_args)) {
2381 LOG(WARNING) << "No browser event args specified through automation";
2382 return false;
2383 }
2384
[email protected]7120f132009-07-20 21:05:372385 if (profile()->GetExtensionMessageService()) {
[email protected]db7331a2010-02-25 22:10:502386 profile()->GetExtensionMessageService()->DispatchEventToRenderers(
[email protected]a807bbe2010-04-14 10:51:192387 event_name, json_args, profile()->IsOffTheRecord(), GURL());
[email protected]7120f132009-07-20 21:05:372388 }
[email protected]a9024892009-06-16 23:13:552389
2390 return true;
2391}
[email protected]5ae5bed2009-08-21 18:52:442392#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:392393
[email protected]57c6a652009-05-04 07:58:342394TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572395 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572396 if (tab_tracker_->ContainsHandle(handle)) {
2397 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342398 if (tab)
2399 *tab = nav_controller;
2400 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572401 }
[email protected]57c6a652009-05-04 07:58:342402 return NULL;
[email protected]20e93d12008-08-28 16:31:572403}
2404
initial.commit09911bf2008-07-26 23:55:292405TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2406 : AutomationProvider(profile) {
2407 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142408 registrar_.Add(this, NotificationType::SESSION_END,
2409 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292410}
2411
2412TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292413 BrowserList::RemoveObserver(this);
2414}
2415
2416void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:142417 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:292418 AutomationProvider::OnChannelError();
2419}
2420
2421void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2422 // For backwards compatibility with the testing automation interface, we
2423 // want the automation provider (and hence the process) to go away when the
2424 // last browser goes away.
2425 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292426 // If you change this, update Observer for NotificationType::SESSION_END
2427 // below.
[email protected]295039bd2008-08-15 04:32:572428 MessageLoop::current()->PostTask(FROM_HERE,
2429 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292430 }
2431}
2432
2433void TestingAutomationProvider::Observe(NotificationType type,
2434 const NotificationSource& source,
2435 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562436 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292437 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2438 // before the task runs resulting in this object not being deleted. This
2439 // Release balance out the Release scheduled by OnBrowserRemoving.
2440 Release();
2441}
[email protected]295039bd2008-08-15 04:32:572442
2443void TestingAutomationProvider::OnRemoveProvider() {
2444 AutomationProviderList::GetInstance()->RemoveProvider(this);
2445}
[email protected]8a3422c92008-09-24 17:42:422446
[email protected]816633a2009-11-11 21:48:182447void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:562448 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422449 if (tab_tracker_->ContainsHandle(handle)) {
2450 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342451 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112452 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422453 }
[email protected]8a3422c92008-09-24 17:42:422454}
2455
[email protected]816633a2009-11-11 21:48:182456void AutomationProvider::ClickInfoBarAccept(int handle,
2457 int info_bar_index,
2458 bool wait_for_navigation,
2459 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422460 bool success = false;
2461 if (tab_tracker_->ContainsHandle(handle)) {
2462 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2463 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112464 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422465 if (info_bar_index >= 0 && info_bar_index < count) {
2466 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:012467 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:422468 }
[email protected]eb9ba192008-12-02 02:41:342469 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112470 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342471 info_bar_index);
2472 if (delegate->AsConfirmInfoBarDelegate())
2473 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422474 success = true;
2475 }
2476 }
[email protected]4f3dc372009-02-24 00:10:292477 }
[email protected]58f622a62009-10-04 01:17:552478
2479 // This "!wait_for_navigation || !success condition" logic looks suspicious.
2480 // It will send a failure message when success is true but
2481 // |wait_for_navigation| is false.
2482 // TODO(phajdan.jr): investgate whether the reply param (currently
2483 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:422484 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:182485 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:522486 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:422487}
2488
[email protected]71f65dd2009-02-11 19:14:562489void AutomationProvider::GetLastNavigationTime(int handle,
2490 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422491 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562492 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422493}
2494
[email protected]71f65dd2009-02-11 19:14:562495void AutomationProvider::WaitForNavigation(int handle,
2496 int64 last_navigation_time,
2497 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:252498 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:422499 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:252500
[email protected]8a3422c92008-09-24 17:42:422501 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562502 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:522503 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
2504 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:552505 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:292506 return;
[email protected]8a3422c92008-09-24 17:42:422507 }
2508
[email protected]7dad3d5f2010-03-04 00:27:012509 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:422510}
2511
[email protected]71f65dd2009-02-11 19:14:562512void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162513 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562514 int value,
2515 bool* success) {
2516 *success = false;
[email protected]8a3422c92008-09-24 17:42:422517 if (browser_tracker_->ContainsHandle(handle)) {
2518 Browser* browser = browser_tracker_->GetResource(handle);
2519 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562520 *success = true;
[email protected]8a3422c92008-09-24 17:42:422521 }
[email protected]8a3422c92008-09-24 17:42:422522}
[email protected]97fa6ce32008-12-19 01:48:162523
[email protected]71f65dd2009-02-11 19:14:562524void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162525 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562526 const std::wstring& value,
2527 bool* success) {
2528 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162529 if (browser_tracker_->ContainsHandle(handle)) {
2530 Browser* browser = browser_tracker_->GetResource(handle);
2531 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562532 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162533 }
[email protected]97fa6ce32008-12-19 01:48:162534}
2535
[email protected]71f65dd2009-02-11 19:14:562536void AutomationProvider::GetBooleanPreference(int handle,
2537 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:362538 bool* success,
2539 bool* value) {
[email protected]71f65dd2009-02-11 19:14:562540 *success = false;
2541 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162542 if (browser_tracker_->ContainsHandle(handle)) {
2543 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562544 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2545 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162546 }
[email protected]97fa6ce32008-12-19 01:48:162547}
2548
[email protected]71f65dd2009-02-11 19:14:562549void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162550 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562551 bool value,
2552 bool* success) {
2553 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162554 if (browser_tracker_->ContainsHandle(handle)) {
2555 Browser* browser = browser_tracker_->GetResource(handle);
2556 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562557 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162558 }
[email protected]97fa6ce32008-12-19 01:48:162559}
2560
2561// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562562void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:402563 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162564 if (tab_tracker_->ContainsHandle(tab_handle)) {
2565 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2566 Browser* browser = FindAndActivateTab(nav);
2567 DCHECK(browser);
2568
[email protected]57c6a652009-05-04 07:58:342569 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2570 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162571 }
[email protected]97fa6ce32008-12-19 01:48:162572}
2573
[email protected]b8f48d12009-11-09 20:14:362574// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562575void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402576 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562577 bool* success) {
2578 *success = false;
[email protected]de246f52009-02-25 18:25:452579#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:162580 if (tab_tracker_->ContainsHandle(tab_handle)) {
2581 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:192582 if (!nav)
2583 return;
[email protected]97fa6ce32008-12-19 01:48:162584 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:162585
[email protected]2f2afba2010-04-01 01:53:192586 // If the browser has UI, simulate what a user would do.
2587 // Activate the tab and then click the encoding menu.
2588 if (browser &&
2589 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162590 int selected_encoding_id =
2591 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2592 if (selected_encoding_id) {
2593 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562594 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162595 }
[email protected]2f2afba2010-04-01 01:53:192596 } else {
2597 // There is no UI, Chrome probably runs as Chrome-Frame mode.
2598 // Try to get TabContents and call its override_encoding method.
2599 TabContents* contents = nav->tab_contents();
2600 if (!contents)
2601 return;
2602 const std::string selected_encoding =
2603 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
2604 if (selected_encoding.empty())
2605 return;
2606 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:162607 }
2608 }
[email protected]de246f52009-02-25 18:25:452609#else
2610 // TODO(port): Enable when encoding-related parts of Browser are ported.
2611 NOTIMPLEMENTED();
2612#endif
[email protected]97fa6ce32008-12-19 01:48:162613}
[email protected]5bcdb312009-01-07 21:43:202614
[email protected]4d434a1a2009-02-11 21:06:572615void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202616 SavePackage::SetShouldPromptUser(should_prompt);
2617}
[email protected]87eab222009-03-13 00:47:452618
[email protected]66ba4932009-06-04 19:22:132619void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2620 *count = -1; // -1 is the error code
2621 if (tab_tracker_->ContainsHandle(handle)) {
2622 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2623 TabContents* tab_contents = nav_controller->tab_contents();
2624 if (tab_contents) {
2625 BlockedPopupContainer* container =
2626 tab_contents->blocked_popup_container();
2627 if (container) {
2628 *count = static_cast<int>(container->GetBlockedPopupCount());
2629 } else {
2630 // If we don't have a container, we don't have any blocked popups to
2631 // contain!
2632 *count = 0;
2633 }
2634 }
2635 }
2636}
[email protected]f7a68432009-07-29 23:18:192637
2638void AutomationProvider::SelectAll(int tab_handle) {
2639 RenderViewHost* view = GetViewForTab(tab_handle);
2640 if (!view) {
2641 NOTREACHED();
2642 return;
2643 }
2644
2645 view->SelectAll();
2646}
2647
2648void AutomationProvider::Cut(int tab_handle) {
2649 RenderViewHost* view = GetViewForTab(tab_handle);
2650 if (!view) {
2651 NOTREACHED();
2652 return;
2653 }
2654
2655 view->Cut();
2656}
2657
2658void AutomationProvider::Copy(int tab_handle) {
2659 RenderViewHost* view = GetViewForTab(tab_handle);
2660 if (!view) {
2661 NOTREACHED();
2662 return;
2663 }
2664
2665 view->Copy();
2666}
2667
2668void AutomationProvider::Paste(int tab_handle) {
2669 RenderViewHost* view = GetViewForTab(tab_handle);
2670 if (!view) {
2671 NOTREACHED();
2672 return;
2673 }
2674
2675 view->Paste();
2676}
2677
2678void AutomationProvider::ReloadAsync(int tab_handle) {
2679 if (tab_tracker_->ContainsHandle(tab_handle)) {
2680 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2681 if (!tab) {
2682 NOTREACHED();
2683 return;
2684 }
2685
[email protected]106a0812010-03-18 00:15:122686 const bool check_for_repost = true;
2687 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:192688 }
2689}
2690
2691void AutomationProvider::StopAsync(int tab_handle) {
2692 RenderViewHost* view = GetViewForTab(tab_handle);
2693 if (!view) {
[email protected]8b2b3312009-09-14 18:38:362694 // We tolerate StopAsync being called even before a view has been created.
2695 // So just log a warning instead of a NOTREACHED().
2696 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:192697 return;
2698 }
2699
2700 view->Stop();
2701}
2702
[email protected]1bb5f892009-10-06 01:44:572703void AutomationProvider::OnSetPageFontSize(int tab_handle,
2704 int font_size) {
2705 AutomationPageFontSize automation_font_size =
2706 static_cast<AutomationPageFontSize>(font_size);
2707
2708 if (automation_font_size < SMALLEST_FONT ||
2709 automation_font_size > LARGEST_FONT) {
2710 DLOG(ERROR) << "Invalid font size specified : "
2711 << font_size;
2712 return;
2713 }
2714
2715 if (tab_tracker_->ContainsHandle(tab_handle)) {
2716 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2717 DCHECK(tab != NULL);
2718 if (tab && tab->tab_contents()) {
2719 DCHECK(tab->tab_contents()->profile() != NULL);
2720 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2721 prefs::kWebKitDefaultFontSize, font_size);
2722 }
2723 }
2724}
2725
[email protected]bc73b4e52010-03-26 04:16:202726void AutomationProvider::RemoveBrowsingData(int remove_mask) {
2727 BrowsingDataRemover* remover;
2728 remover = new BrowsingDataRemover(profile(),
2729 BrowsingDataRemover::EVERYTHING, // All time periods.
2730 base::Time());
2731 remover->Remove(remove_mask);
2732 // BrowsingDataRemover deletes itself.
2733}
[email protected]1bb5f892009-10-06 01:44:572734
[email protected]2949e90d2009-08-21 15:32:522735void AutomationProvider::WaitForBrowserWindowCountToBecome(
2736 int target_count, IPC::Message* reply_message) {
2737 if (static_cast<int>(BrowserList::size()) == target_count) {
2738 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2739 reply_message, true);
2740 Send(reply_message);
2741 return;
2742 }
2743
2744 // Set up an observer (it will delete itself).
2745 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2746}
2747
2748void AutomationProvider::WaitForAppModalDialogToBeShown(
2749 IPC::Message* reply_message) {
2750 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2751 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2752 reply_message, true);
2753 Send(reply_message);
2754 return;
2755 }
2756
2757 // Set up an observer (it will delete itself).
2758 new AppModalDialogShownObserver(this, reply_message);
2759}
2760
[email protected]1126a1d32009-08-26 15:39:262761void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2762 int handle, int number_of_navigations, IPC::Message* reply_message) {
2763 if (tab_tracker_->ContainsHandle(handle)) {
2764 NavigationController* tab = tab_tracker_->GetResource(handle);
2765 Browser* browser = FindAndActivateTab(tab);
2766 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:012767 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2768 false);
[email protected]1126a1d32009-08-26 15:39:262769 browser->GoBack(CURRENT_TAB);
2770 return;
2771 }
2772 }
2773
2774 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2775 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2776 Send(reply_message);
2777}
2778
2779void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2780 int handle, int number_of_navigations, IPC::Message* reply_message) {
2781 if (tab_tracker_->ContainsHandle(handle)) {
2782 NavigationController* tab = tab_tracker_->GetResource(handle);
2783 Browser* browser = FindAndActivateTab(tab);
2784 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:012785 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2786 false);
[email protected]1126a1d32009-08-26 15:39:262787 browser->GoForward(CURRENT_TAB);
2788 return;
2789 }
2790 }
2791
2792 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2793 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2794 Send(reply_message);
2795}
2796
[email protected]f7a68432009-07-29 23:18:192797RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2798 if (tab_tracker_->ContainsHandle(tab_handle)) {
2799 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2800 if (!tab) {
2801 NOTREACHED();
2802 return NULL;
2803 }
2804
2805 TabContents* tab_contents = tab->tab_contents();
2806 if (!tab_contents) {
2807 NOTREACHED();
2808 return NULL;
2809 }
2810
2811 RenderViewHost* view_host = tab_contents->render_view_host();
2812 return view_host;
2813 }
2814
2815 return NULL;
2816}
[email protected]675595f2009-08-26 22:32:042817
2818void AutomationProvider::GetBrowserForWindow(int window_handle,
2819 bool* success,
2820 int* browser_handle) {
2821 *success = false;
2822 *browser_handle = 0;
2823
2824 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2825 if (!window)
2826 return;
2827
2828 BrowserList::const_iterator iter = BrowserList::begin();
2829 for (;iter != BrowserList::end(); ++iter) {
2830 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2831 if (window == this_window) {
2832 // Add() returns the existing handle for the resource if any.
2833 *browser_handle = browser_tracker_->Add(*iter);
2834 *success = true;
2835 return;
2836 }
2837 }
2838}
[email protected]d11c8e92009-10-20 23:26:402839
2840void AutomationProvider::InstallExtension(const FilePath& crx_path,
2841 IPC::Message* reply_message) {
2842 ExtensionsService* service = profile_->GetExtensionsService();
2843 if (service) {
2844 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:192845 new ExtensionInstallNotificationObserver(this,
2846 AutomationMsg_InstallExtension::ID,
2847 reply_message);
[email protected]d11c8e92009-10-20 23:26:402848
2849 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:162850 scoped_refptr<CrxInstaller> installer(
2851 new CrxInstaller(install_dir,
2852 service,
2853 NULL)); // silent install, no UI
2854 installer->set_allow_privilege_increase(true);
2855 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:402856 } else {
2857 AutomationMsg_InstallExtension::WriteReplyParams(
2858 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2859 Send(reply_message);
2860 }
2861}
2862
2863void AutomationProvider::LoadExpandedExtension(
2864 const FilePath& extension_dir,
2865 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:382866 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:402867 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:192868 new ExtensionInstallNotificationObserver(
2869 this,
2870 AutomationMsg_LoadExpandedExtension::ID,
2871 reply_message);
[email protected]d11c8e92009-10-20 23:26:402872
2873 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:402874 } else {
2875 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
2876 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2877 Send(reply_message);
2878 }
2879}
[email protected]673fd2c02010-02-04 23:10:002880
[email protected]a1e62d12010-03-16 02:18:432881void AutomationProvider::GetEnabledExtensions(
2882 std::vector<FilePath>* result) {
2883 ExtensionsService* service = profile_->GetExtensionsService();
2884 DCHECK(service);
2885 if (service->extensions_enabled()) {
2886 const ExtensionList* extensions = service->extensions();
2887 DCHECK(extensions);
2888 for (size_t i = 0; i < extensions->size(); ++i) {
2889 Extension* extension = (*extensions)[i];
2890 DCHECK(extension);
[email protected]237f281672010-03-20 12:37:072891 if (extension->location() == Extension::INTERNAL) {
2892 result->push_back(extension->path());
2893 }
[email protected]a1e62d12010-03-16 02:18:432894 }
2895 }
2896}
2897
[email protected]790788ac2010-04-06 17:52:192898void AutomationProvider::WaitForExtensionTestResult(
2899 IPC::Message* reply_message) {
2900 DCHECK(reply_message_ == NULL);
2901 reply_message_ = reply_message;
2902 // Call MaybeSendResult, because the result might have come in before
2903 // we were waiting on it.
2904 extension_test_result_observer_->MaybeSendResult();
2905}
2906
2907void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:092908 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:192909 ExtensionsService* service = profile_->GetExtensionsService();
2910 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
2911 if (service && manager) {
2912 // The observer will delete itself when done.
2913 new ExtensionReadyNotificationObserver(
2914 manager,
2915 this,
2916 AutomationMsg_InstallExtensionAndGetHandle::ID,
2917 reply_message);
2918
[email protected]d7e5525d2010-04-20 14:37:092919 ExtensionInstallUI* client =
2920 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:192921 scoped_refptr<CrxInstaller> installer(
2922 new CrxInstaller(service->install_directory(),
2923 service,
[email protected]d7e5525d2010-04-20 14:37:092924 client));
[email protected]790788ac2010-04-06 17:52:192925 installer->set_allow_privilege_increase(true);
2926 installer->InstallCrx(crx_path);
2927 } else {
2928 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
2929 reply_message, 0);
2930 Send(reply_message);
2931 }
2932}
2933
2934void AutomationProvider::UninstallExtension(int extension_handle,
2935 bool* success) {
2936 *success = false;
2937 Extension* extension = GetExtension(extension_handle);
2938 ExtensionsService* service = profile_->GetExtensionsService();
2939 if (extension && service) {
2940 ExtensionUnloadNotificationObserver observer;
2941 service->UninstallExtension(extension->id(), false);
2942 // The extension unload notification should have been sent synchronously
2943 // with the uninstall. Just to be safe, check that it was received.
2944 *success = observer.did_receive_unload_notification();
2945 }
2946}
2947
2948void AutomationProvider::EnableExtension(int extension_handle,
2949 IPC::Message* reply_message) {
2950 Extension* extension = GetDisabledExtension(extension_handle);
2951 ExtensionsService* service = profile_->GetExtensionsService();
2952 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
2953 // Only enable if this extension is disabled.
2954 if (extension && service && manager) {
2955 // The observer will delete itself when done.
2956 new ExtensionReadyNotificationObserver(
2957 manager,
2958 this,
2959 AutomationMsg_EnableExtension::ID,
2960 reply_message);
2961 service->EnableExtension(extension->id());
2962 } else {
2963 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
2964 Send(reply_message);
2965 }
2966}
2967
2968void AutomationProvider::DisableExtension(int extension_handle,
2969 bool* success) {
2970 *success = false;
2971 Extension* extension = GetEnabledExtension(extension_handle);
2972 ExtensionsService* service = profile_->GetExtensionsService();
2973 if (extension && service) {
2974 ExtensionUnloadNotificationObserver observer;
2975 service->DisableExtension(extension->id());
2976 // The extension unload notification should have been sent synchronously
2977 // with the disable. Just to be safe, check that it was received.
2978 *success = observer.did_receive_unload_notification();
2979 }
2980}
2981
2982void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
2983 int extension_handle, int browser_handle,
2984 IPC::Message* reply_message) {
2985 bool success = false;
2986 Extension* extension = GetEnabledExtension(extension_handle);
2987 ExtensionsService* service = profile_->GetExtensionsService();
2988 ExtensionMessageService* message_service =
2989 profile_->GetExtensionMessageService();
2990 Browser* browser = browser_tracker_->GetResource(browser_handle);
2991 if (extension && service && message_service && browser) {
2992 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
2993 if (extension->page_action()) {
2994 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
2995 browser->profile(), extension->id(), "action", tab_id, "", 1);
2996 success = true;
2997 } else if (extension->browser_action()) {
2998 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
2999 browser->profile(), extension->id(), browser);
3000 success = true;
3001 }
3002 }
3003 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3004 reply_message, success);
3005 Send(reply_message);
3006}
3007
3008void AutomationProvider::MoveExtensionBrowserAction(
3009 int extension_handle, int index, bool* success) {
3010 *success = false;
3011 Extension* extension = GetEnabledExtension(extension_handle);
3012 ExtensionsService* service = profile_->GetExtensionsService();
3013 if (extension && service) {
3014 ExtensionToolbarModel* toolbar = service->toolbar_model();
3015 if (toolbar) {
3016 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3017 toolbar->MoveBrowserAction(extension, index);
3018 *success = true;
3019 } else {
3020 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3021 }
3022 }
3023 }
3024}
3025
3026void AutomationProvider::GetExtensionProperty(
3027 int extension_handle,
3028 AutomationMsg_ExtensionProperty type,
3029 bool* success,
3030 std::string* value) {
3031 *success = false;
3032 Extension* extension = GetExtension(extension_handle);
3033 ExtensionsService* service = profile_->GetExtensionsService();
3034 if (extension && service) {
3035 ExtensionToolbarModel* toolbar = service->toolbar_model();
3036 int found_index = -1;
3037 int index = 0;
3038 switch (type) {
3039 case AUTOMATION_MSG_EXTENSION_ID:
3040 *value = extension->id();
3041 *success = true;
3042 break;
3043 case AUTOMATION_MSG_EXTENSION_NAME:
3044 *value = extension->name();
3045 *success = true;
3046 break;
3047 case AUTOMATION_MSG_EXTENSION_VERSION:
3048 *value = extension->VersionString();
3049 *success = true;
3050 break;
3051 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3052 if (toolbar) {
3053 for (ExtensionList::const_iterator iter = toolbar->begin();
3054 iter != toolbar->end(); iter++) {
3055 // Skip this extension if we are in incognito mode
3056 // and it is not incognito-enabled.
3057 if (profile_->IsOffTheRecord() &&
3058 !service->IsIncognitoEnabled(*iter))
3059 continue;
3060 if (*iter == extension) {
3061 found_index = index;
3062 break;
3063 }
3064 index++;
3065 }
3066 *value = IntToString(found_index);
3067 *success = true;
3068 }
3069 break;
3070 default:
3071 LOG(WARNING) << "Trying to get undefined extension property";
3072 break;
3073 }
3074 }
3075}
3076
[email protected]673fd2c02010-02-04 23:10:003077void AutomationProvider::SaveAsAsync(int tab_handle) {
3078 NavigationController* tab = NULL;
3079 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3080 if (tab_contents)
3081 tab_contents->OnSavePage();
3082}
[email protected]7dad3d5f2010-03-04 00:27:013083
3084void AutomationProvider::SetContentSetting(
3085 int handle,
3086 const std::string& host,
3087 ContentSettingsType content_type,
3088 ContentSetting setting,
3089 bool* success) {
3090 *success = false;
3091 if (browser_tracker_->ContainsHandle(handle)) {
3092 Browser* browser = browser_tracker_->GetResource(handle);
3093 HostContentSettingsMap* map =
3094 browser->profile()->GetHostContentSettingsMap();
3095 if (host.empty()) {
3096 map->SetDefaultContentSetting(content_type, setting);
3097 } else {
[email protected]0314ae02010-04-08 09:18:293098 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3099 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013100 }
3101 *success = true;
3102 }
3103}
[email protected]cc824372010-03-31 15:33:013104
3105#if !defined(TOOLKIT_VIEWS)
3106void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3107 NOTIMPLEMENTED();
3108};
3109
3110void AutomationProvider::WaitForFocusedViewIDToChange(
3111 int handle, int previous_view_id, IPC::Message* reply_message) {
3112 NOTIMPLEMENTED();
3113}
3114
3115void AutomationProvider::StartTrackingPopupMenus(
3116 int browser_handle, bool* success) {
3117 NOTIMPLEMENTED();
3118}
3119
3120void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3121 NOTIMPLEMENTED();
3122}
3123#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:093124
3125void AutomationProvider::ResetToDefaultTheme() {
3126 profile_->ClearTheme();
3127}