blob: e29f025d63742c5ff8fa692066d54421132a63cb [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]7060bb292010-06-24 00:52:4912#include "base/file_path.h"
[email protected]c6cb1992009-04-13 16:45:2913#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2014#include "base/json/json_reader.h"
[email protected]59a611242010-04-02 02:24:0415#include "base/json/json_writer.h"
[email protected]93364da2010-06-29 18:03:4416#include "base/json/string_escape.h"
[email protected]bc1407f2009-09-29 00:33:3517#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3818#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4620#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5021#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2722#include "base/string_util.h"
[email protected]9eaa18e2010-06-29 20:51:0123#include "base/task.h"
[email protected]5fac9622009-02-04 21:49:3824#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5325#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1726#include "base/values.h"
[email protected]9eaa18e2010-06-29 20:51:0127#include "base/waitable_event.h"
[email protected]4f3dc372009-02-24 00:10:2928#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2829#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0930#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]55846ad842010-07-09 18:22:5631#include "chrome/browser/autofill/autofill_manager.h"
[email protected]679082052010-07-21 21:30:1332#include "chrome/browser/automation/automation_autocomplete_edit_tracker.h"
33#include "chrome/browser/automation/automation_browser_tracker.h"
[email protected]790788ac2010-04-06 17:52:1934#include "chrome/browser/automation/automation_extension_tracker.h"
[email protected]7c983cc2010-07-16 11:33:3435#include "chrome/browser/automation/automation_provider_json.h"
initial.commit09911bf2008-07-26 23:55:2936#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0837#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]679082052010-07-21 21:30:1338#include "chrome/browser/automation/automation_resource_message_filter.h"
39#include "chrome/browser/automation/automation_tab_tracker.h"
40#include "chrome/browser/automation/automation_window_tracker.h"
[email protected]f44265b2009-05-19 18:52:5041#include "chrome/browser/automation/extension_port_container.h"
[email protected]12802702010-07-09 19:43:0942#include "chrome/browser/autocomplete/autocomplete_edit.h"
[email protected]66ba4932009-06-04 19:22:1343#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5344#include "chrome/browser/bookmarks/bookmark_model.h"
45#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]ef413ca2010-05-25 21:09:1446#include "chrome/browser/browser_list.h"
[email protected]5c238752009-06-13 10:29:0747#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3948#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2049#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0550#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5851#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2952#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1253#include "chrome/browser/debugger/devtools_manager.h"
[email protected]6c69796d2010-07-16 21:41:1654#include "chrome/browser/download/download_item.h"
[email protected]59560e0b2009-06-04 03:30:2255#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0556#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4057#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1958#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]ef413ca2010-05-25 21:09:1459#include "chrome/browser/extensions/extension_host.h"
[email protected]d11c8e92009-10-20 23:26:4060#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5561#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1962#include "chrome/browser/extensions/extension_tabs_module.h"
63#include "chrome/browser/extensions/extension_toolbar_model.h"
64#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1665#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5866#include "chrome/browser/find_bar.h"
67#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2968#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0169#include "chrome/browser/host_content_settings_map.h"
[email protected]c5aa5322010-07-15 19:00:0770#include "chrome/browser/importer/importer.h"
71#include "chrome/browser/importer/importer_data_types.h"
[email protected]0ac83682010-01-22 17:46:2772#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0673#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0174#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5375#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2476#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0877#include "chrome/browser/pref_service.h"
[email protected]f83f9102010-05-04 17:01:0578#include "chrome/browser/printing/print_job.h"
[email protected]a7eee32f2009-05-22 18:08:1779#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0780#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2581#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0382#include "chrome/browser/ssl/ssl_manager.h"
83#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]b5558cf22010-07-12 17:30:0684#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3485#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4586#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]38b5a3852010-07-21 06:49:5287#include "chrome/browser/translate/translate_infobar_delegate.h"
[email protected]a7eee32f2009-05-22 18:08:1788#include "chrome/common/automation_constants.h"
[email protected]a9ff2c02010-05-13 17:33:0589#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2990#include "chrome/common/chrome_paths.h"
[email protected]c984d9f2010-07-20 20:52:2091#include "chrome/common/chrome_switches.h"
[email protected]1eeb5e02010-07-20 23:02:1192#include "chrome/common/chrome_version_info.h"
[email protected]790788ac2010-04-06 17:52:1993#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1794#include "chrome/common/json_value_serializer.h"
[email protected]68d2a05f2010-05-07 21:39:5595#include "chrome/common/net/url_request_context_getter.h"
[email protected]1c58a5c2009-05-21 18:47:1496#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5797#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2598#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5699#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:57100#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:17101#include "net/proxy/proxy_service.h"
102#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:21103#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:30104#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:28105#include "views/event.h"
[email protected]5bcfe1672010-07-16 20:51:57106#include "webkit/glue/password_form.h"
[email protected]f7d48012010-05-06 08:17:05107#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:29108
[email protected]de246f52009-02-25 18:25:45109#if defined(OS_WIN)
[email protected]4bdde602010-06-16 03:17:35110#include "chrome/browser/external_tab_container_win.h"
[email protected]de246f52009-02-25 18:25:45111#endif // defined(OS_WIN)
112
[email protected]e1acf6f2008-10-27 20:43:33113using base::Time;
114
[email protected]cbab76d2008-10-13 22:42:47115class AutomationInterstitialPage : public InterstitialPage {
116 public:
[email protected]57c6a652009-05-04 07:58:34117 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47118 const GURL& url,
119 const std::string& contents)
120 : InterstitialPage(tab, true, url),
121 contents_(contents) {
122 }
123
124 virtual std::string GetHTMLContents() { return contents_; }
125
126 private:
127 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29128
[email protected]cbab76d2008-10-13 22:42:47129 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
130};
131
[email protected]c2cb8542009-08-20 21:16:51132class ClickTask : public Task {
133 public:
[email protected]fc2e0872009-08-21 22:14:41134 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51135 virtual ~ClickTask() {}
136
137 virtual void Run() {
138 ui_controls::MouseButton button = ui_controls::LEFT;
139 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
140 views::Event::EF_LEFT_BUTTON_DOWN) {
141 button = ui_controls::LEFT;
142 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
143 views::Event::EF_RIGHT_BUTTON_DOWN) {
144 button = ui_controls::RIGHT;
145 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
146 views::Event::EF_MIDDLE_BUTTON_DOWN) {
147 button = ui_controls::MIDDLE;
148 } else {
149 NOTREACHED();
150 }
151
[email protected]fc2e0872009-08-21 22:14:41152 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51153 }
154
155 private:
[email protected]c2cb8542009-08-20 21:16:51156 int flags_;
157
158 DISALLOW_COPY_AND_ASSIGN(ClickTask);
159};
[email protected]c2cb8542009-08-20 21:16:51160
initial.commit09911bf2008-07-26 23:55:29161AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57162 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56163 profile_(profile),
[email protected]cc824372010-03-31 15:33:01164 reply_message_(NULL),
165 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29166 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19167 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29168 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20169 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29170 autocomplete_edit_tracker_.reset(
171 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29172 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
173 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44174 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19175 extension_test_result_observer_.reset(
176 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13177 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29178}
179
180AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50181 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
182 port_containers_.end());
183 port_containers_.clear();
184
[email protected]0da050b92008-08-19 19:29:47185 // Make sure that any outstanding NotificationObservers also get destroyed.
186 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31187 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47188 while ((observer = it.GetNext()) != NULL)
189 delete observer;
[email protected]528211a2010-01-14 15:25:13190
191 if (channel_.get()) {
192 channel_->Close();
193 }
194 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29195}
196
[email protected]9a3a293b2009-06-04 22:28:16197void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06198 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57199 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06200 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
201 automation_resource_message_filter_,
202 g_browser_process->io_thread()->message_loop(),
203 true, g_browser_process->shutdown_event()));
[email protected]1eeb5e02010-07-20 23:02:11204 scoped_ptr<FileVersionInfo> version_info(chrome::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40205 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43206 if (version_info != NULL) {
207 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40208 }
[email protected]c6cb1992009-04-13 16:45:29209
210 // Send a hello message with our current automation protocol version.
211 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29212}
213
214void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
215 if (expected_tabs == 0) {
216 Send(new AutomationMsg_InitialLoadsComplete(0));
217 } else {
218 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
219 }
220}
221
222NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58223 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01224 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29225 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58226 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01227 number_of_navigations,
228 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29229
[email protected]71f65dd2009-02-11 19:14:56230 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29231 return observer;
232}
233
[email protected]faf2ee42010-05-11 14:26:17234void AutomationProvider::RemoveNavigationStatusListener(
235 NotificationObserver* obs) {
236 notification_observer_list_.RemoveObserver(obs);
237}
238
initial.commit09911bf2008-07-26 23:55:29239NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14240 Browser* parent,
241 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56242 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14243 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29244 notification_observer_list_.AddObserver(observer);
245
246 return observer;
247}
248
[email protected]faf2ee42010-05-11 14:26:17249void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
initial.commit09911bf2008-07-26 23:55:29250 notification_observer_list_.RemoveObserver(obs);
251}
252
253void AutomationProvider::AddLoginHandler(NavigationController* tab,
254 LoginHandler* handler) {
255 login_handler_map_[tab] = handler;
256}
257
258void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
259 DCHECK(login_handler_map_[tab]);
260 login_handler_map_.erase(tab);
261}
262
[email protected]f44265b2009-05-19 18:52:50263void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
264 int port_id = port->port_id();
265 DCHECK_NE(-1, port_id);
266 DCHECK(port_containers_.find(port_id) == port_containers_.end());
267
268 port_containers_[port_id] = port;
269}
270
271void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
272 int port_id = port->port_id();
273 DCHECK_NE(-1, port_id);
274
275 PortContainerMap::iterator it = port_containers_.find(port_id);
276 DCHECK(it != port_containers_.end());
277
278 if (it != port_containers_.end()) {
279 delete it->second;
280 port_containers_.erase(it);
281 }
282}
283
284ExtensionPortContainer* AutomationProvider::GetPortContainer(
285 int port_id) const {
286 PortContainerMap::const_iterator it = port_containers_.find(port_id);
287 if (it == port_containers_.end())
288 return NULL;
289
290 return it->second;
291}
292
initial.commit09911bf2008-07-26 23:55:29293int AutomationProvider::GetIndexForNavigationController(
294 const NavigationController* controller, const Browser* parent) const {
295 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12296 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29297}
298
[email protected]790788ac2010-04-06 17:52:19299int AutomationProvider::AddExtension(Extension* extension) {
300 DCHECK(extension);
301 return extension_tracker_->Add(extension);
302}
303
304Extension* AutomationProvider::GetExtension(int extension_handle) {
305 return extension_tracker_->GetResource(extension_handle);
306}
307
308Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
309 Extension* extension = extension_tracker_->GetResource(extension_handle);
310 ExtensionsService* service = profile_->GetExtensionsService();
311 if (extension && service &&
312 service->GetExtensionById(extension->id(), false))
313 return extension;
314 return NULL;
315}
316
317Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
318 Extension* extension = extension_tracker_->GetResource(extension_handle);
319 ExtensionsService* service = profile_->GetExtensionsService();
320 if (extension && service &&
321 service->GetExtensionById(extension->id(), true) &&
322 !service->GetExtensionById(extension->id(), false))
323 return extension;
324 return NULL;
325}
326
initial.commit09911bf2008-07-26 23:55:29327void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
328 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14329 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56330 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
331 CloseBrowserAsync)
332 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
333 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
334 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
335 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
336 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
337 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22338 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]a503c97c2010-07-16 13:05:48339 IPC_MESSAGE_HANDLER(AutomationMsg_ShowCollectedCookiesDialog,
340 ShowCollectedCookiesDialog)
[email protected]1c58a5c2009-05-21 18:47:14341 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58342 IPC_MESSAGE_HANDLER_DELAY_REPLY(
343 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
344 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56345 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11346 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
347 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56348 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
349 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
350 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
351 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14352 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56353 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
354 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
355 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14356 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29357 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
358 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56359 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36360 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56361 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29362 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56363 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29364 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
365 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56366 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14367 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24368 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44369 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33370 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44371 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33372 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24373 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14374 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24375 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15376 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14377 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24378 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35379 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14380 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30381#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56382 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
383 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30384#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56385 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53386 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56387 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37388#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56389 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45390#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56391 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
392 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03393 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56394 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14395 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09396 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
397 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
398 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29399 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14400 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56401 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
402 ExecuteJavascript)
403 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29404 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14405 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
406 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56407 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
408 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14409 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17410 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14411 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14412 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53413 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
414 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14415 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56416 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14417 GetAutocompleteEditForBrowser)
418 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]71f65dd2009-02-11 19:14:56419 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14420 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56421 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14422 HideInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56423 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
424 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14425 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
426 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44427 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
428 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56429 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
430 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29431 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
[email protected]bdb7ff62010-07-20 01:56:52432 IPC_MESSAGE_HANDLER(AutomationMsg_IsMenuCommandEnabled,
433 IsMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56434 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41435 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56436 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
437 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29438 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56439 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29440 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56441 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29442 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56443 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29444 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56445 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45446 HandleOpenFindInPageRequest)
[email protected]1c58a5c2009-05-21 18:47:14447 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56448 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57449 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56450 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57451 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56452 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
453 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53454 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
455 GetBookmarksAsJSON)
456 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
457 WaitForBookmarkModelToLoad)
458 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
459 AddBookmarkGroup)
460 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
461 AddBookmarkURL)
462 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
463 ReparentBookmark)
464 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
465 SetBookmarkTitle)
466 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
467 SetBookmarkURL)
468 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
469 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04470 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
471 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18472 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
473 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
474 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56475 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42476 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56477 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
478 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14479 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56480 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44481 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56482 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20483 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14484 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56485 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16486 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56487 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16488 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56489 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16490 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14491 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20492 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
493 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14494 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]59560e0b2009-06-04 03:30:22495 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13496 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19497 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
498 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
499 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
500 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
501 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
502 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52503 IPC_MESSAGE_HANDLER_DELAY_REPLY(
504 AutomationMsg_WaitForBrowserWindowCountToBecome,
505 WaitForBrowserWindowCountToBecome)
506 IPC_MESSAGE_HANDLER_DELAY_REPLY(
507 AutomationMsg_WaitForAppModalDialogToBeShown,
508 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26509 IPC_MESSAGE_HANDLER_DELAY_REPLY(
510 AutomationMsg_GoBackBlockUntilNavigationsComplete,
511 GoBackBlockUntilNavigationsComplete)
512 IPC_MESSAGE_HANDLER_DELAY_REPLY(
513 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
514 GoForwardBlockUntilNavigationsComplete)
[email protected]1bb5f892009-10-06 01:44:57515 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40516 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
517 InstallExtension)
518 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
519 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43520 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
521 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19522 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
523 WaitForExtensionTestResult)
524 IPC_MESSAGE_HANDLER_DELAY_REPLY(
525 AutomationMsg_InstallExtensionAndGetHandle,
526 InstallExtensionAndGetHandle)
527 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
528 UninstallExtension)
529 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
530 EnableExtension)
531 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
532 DisableExtension)
533 IPC_MESSAGE_HANDLER_DELAY_REPLY(
534 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
535 ExecuteExtensionActionInActiveTabAsync)
536 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
537 MoveExtensionBrowserAction)
538 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
539 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57540 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
541 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00542 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]7dad3d5f2010-03-04 00:27:01543 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]bc73b4e52010-03-26 04:16:20544 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]bdd5a9c92010-06-14 18:21:00545 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
[email protected]cc824372010-03-31 15:33:01546#if defined(TOOLKIT_VIEWS)
547 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
548 WaitForFocusedViewIDToChange)
549 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
550 StartTrackingPopupMenus)
551 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
552 WaitForPopupMenuToOpen)
[email protected]bdd5a9c92010-06-14 18:21:00553#endif // defined(TOOLKIT_VIEWS)
[email protected]52415f842010-06-10 21:51:52554#if defined(OS_WIN)
555 // These are for use with external tabs.
556 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
557 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
558 ProcessUnhandledAccelerator)
559 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
560 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
561 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
562 OnForwardContextMenuCommandToChrome)
563 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
564 NavigateInExternalTab)
565 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
566 NavigateExternalTabAtIndex)
567 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
568 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
569 SetEnableExtensionAutomation)
570 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
571 OnMessageFromExternalHost)
[email protected]bdd5a9c92010-06-14 18:21:00572 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
[email protected]1f71d5882010-07-15 20:39:07573 IPC_MESSAGE_HANDLER(AutomationMsg_RunUnloadHandlers, OnRunUnloadHandlers)
[email protected]bdd5a9c92010-06-14 18:21:00574#endif // defined(OS_WIN)
575#if defined(OS_CHROMEOS)
576 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
577 LoginWithUserAndPass)
578#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29579 IPC_END_MESSAGE_MAP()
580}
581
[email protected]71f65dd2009-02-11 19:14:56582void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
583 *status = -1;
initial.commit09911bf2008-07-26 23:55:29584 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
585 Browser* browser = browser_tracker_->GetResource(handle);
586 if (at_index >= 0 && at_index < browser->tab_count()) {
587 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56588 *status = 0;
initial.commit09911bf2008-07-26 23:55:29589 }
590 }
initial.commit09911bf2008-07-26 23:55:29591}
592
[email protected]71f65dd2009-02-11 19:14:56593void AutomationProvider::AppendTab(int handle, const GURL& url,
594 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29595 int append_tab_response = -1; // -1 is the error code
596 NotificationObserver* observer = NULL;
597
598 if (browser_tracker_->ContainsHandle(handle)) {
599 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14600 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38601 TabContents* tab_contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39602 url, GURL(), PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED,
603 NULL, std::string());
initial.commit09911bf2008-07-26 23:55:29604 if (tab_contents) {
605 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57606 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29607 }
608 }
609
610 if (append_tab_response < 0) {
611 // The append tab failed. Remove the TabStripObserver
612 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17613 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29614 delete observer;
615 }
616
[email protected]71f65dd2009-02-11 19:14:56617 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
618 append_tab_response);
619 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29620 }
621}
622
[email protected]71f65dd2009-02-11 19:14:56623void AutomationProvider::NavigateToURL(int handle, const GURL& url,
624 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58625 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
626}
627
628void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
629 int handle, const GURL& url, int number_of_navigations,
630 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29631 if (tab_tracker_->ContainsHandle(handle)) {
632 NavigationController* tab = tab_tracker_->GetResource(handle);
633
634 // Simulate what a user would do. Activate the tab and then navigate.
635 // We could allow navigating in a background tab in future.
636 Browser* browser = FindAndActivateTab(tab);
637
638 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01639 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
640 false);
[email protected]71f65dd2009-02-11 19:14:56641
initial.commit09911bf2008-07-26 23:55:29642 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50643 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29644 return;
645 }
646 }
[email protected]71f65dd2009-02-11 19:14:56647
648 AutomationMsg_NavigateToURL::WriteReplyParams(
649 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
650 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29651}
[email protected]2949e90d2009-08-21 15:32:52652
[email protected]c70f9b82010-04-21 07:31:11653void AutomationProvider::NavigationAsync(int handle,
654 const GURL& url,
655 bool* status) {
656 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
657}
658
659void AutomationProvider::NavigationAsyncWithDisposition(
660 int handle,
661 const GURL& url,
662 WindowOpenDisposition disposition,
663 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56664 *status = false;
initial.commit09911bf2008-07-26 23:55:29665
666 if (tab_tracker_->ContainsHandle(handle)) {
667 NavigationController* tab = tab_tracker_->GetResource(handle);
668
669 // Simulate what a user would do. Activate the tab and then navigate.
670 // We could allow navigating in a background tab in future.
671 Browser* browser = FindAndActivateTab(tab);
672
673 if (browser) {
674 // Don't add any listener unless a callback mechanism is desired.
675 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11676 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56677 *status = true;
initial.commit09911bf2008-07-26 23:55:29678 }
679 }
initial.commit09911bf2008-07-26 23:55:29680}
681
[email protected]71f65dd2009-02-11 19:14:56682void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29683 if (tab_tracker_->ContainsHandle(handle)) {
684 NavigationController* tab = tab_tracker_->GetResource(handle);
685 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14686 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01687 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29688 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29689 return;
690 }
691 }
[email protected]71f65dd2009-02-11 19:14:56692
693 AutomationMsg_GoBack::WriteReplyParams(
694 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
695 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29696}
697
[email protected]71f65dd2009-02-11 19:14:56698void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29699 if (tab_tracker_->ContainsHandle(handle)) {
700 NavigationController* tab = tab_tracker_->GetResource(handle);
701 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14702 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01703 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29704 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29705 return;
706 }
707 }
[email protected]71f65dd2009-02-11 19:14:56708
709 AutomationMsg_GoForward::WriteReplyParams(
710 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
711 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29712}
713
[email protected]71f65dd2009-02-11 19:14:56714void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29715 if (tab_tracker_->ContainsHandle(handle)) {
716 NavigationController* tab = tab_tracker_->GetResource(handle);
717 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14718 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01719 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]cb84d642010-06-10 00:56:28720 browser->Reload(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29721 return;
722 }
723 }
[email protected]71f65dd2009-02-11 19:14:56724
725 AutomationMsg_Reload::WriteReplyParams(
726 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
727 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29728}
729
[email protected]71f65dd2009-02-11 19:14:56730void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29731 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56732 const std::wstring& password,
733 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29734 if (tab_tracker_->ContainsHandle(tab_handle)) {
735 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
736 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
737
738 if (iter != login_handler_map_.end()) {
739 // If auth is needed again after this, assume login has failed. This is
740 // not strictly correct, because a navigation can require both proxy and
741 // server auth, but it should be OK for now.
742 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01743 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29744 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52745 return;
initial.commit09911bf2008-07-26 23:55:29746 }
747 }
[email protected]de246f52009-02-25 18:25:45748
[email protected]457f5cf2009-08-18 16:37:52749 AutomationMsg_SetAuth::WriteReplyParams(
750 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
751 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29752}
753
[email protected]71f65dd2009-02-11 19:14:56754void AutomationProvider::CancelAuth(int tab_handle,
755 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29756 if (tab_tracker_->ContainsHandle(tab_handle)) {
757 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
758 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
759
760 if (iter != login_handler_map_.end()) {
761 // If auth is needed again after this, something is screwy.
762 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01763 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29764 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52765 return;
initial.commit09911bf2008-07-26 23:55:29766 }
767 }
[email protected]de246f52009-02-25 18:25:45768
[email protected]457f5cf2009-08-18 16:37:52769 AutomationMsg_CancelAuth::WriteReplyParams(
770 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
771 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29772}
773
[email protected]71f65dd2009-02-11 19:14:56774void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
775 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29776
777 if (tab_tracker_->ContainsHandle(tab_handle)) {
778 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
779 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
780
781 if (iter != login_handler_map_.end()) {
782 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56783 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29784 }
785 }
initial.commit09911bf2008-07-26 23:55:29786}
787
[email protected]71f65dd2009-02-11 19:14:56788void AutomationProvider::GetRedirectsFrom(int tab_handle,
789 const GURL& source_url,
790 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29791 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
792 if (tab_tracker_->ContainsHandle(tab_handle)) {
793 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
794 HistoryService* history_service =
795 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
796
797 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
798 "has no history service";
799 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56800 DCHECK(reply_message_ == NULL);
801 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29802 // Schedule a history query for redirects. The response will be sent
803 // asynchronously from the callback the history system uses to notify us
804 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29805 redirect_query_ = history_service->QueryRedirectsFrom(
806 source_url, &consumer_,
807 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
808 return; // Response will be sent when query completes.
809 }
810 }
811
812 // Send failure response.
[email protected]deb57402009-02-06 01:35:30813 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56814 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
815 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29816}
817
[email protected]71f65dd2009-02-11 19:14:56818void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
819 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29820 if (browser_tracker_->ContainsHandle(handle)) {
821 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56822 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29823 }
initial.commit09911bf2008-07-26 23:55:29824}
825
[email protected]202e7a72009-06-15 03:48:36826void AutomationProvider::GetBrowserLocale(string16* locale) {
827 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06828 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36829}
830
[email protected]71f65dd2009-02-11 19:14:56831void AutomationProvider::GetBrowserWindowCount(int* window_count) {
832 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29833}
834
[email protected]24497032009-05-01 17:00:29835void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
836 *window_count = static_cast<int>(
837 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
838}
839
[email protected]71f65dd2009-02-11 19:14:56840void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
841 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07842 AppModalDialog* dialog_delegate =
843 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50844 *showing_dialog = (dialog_delegate != NULL);
845 if (*showing_dialog)
846 *dialog_button = dialog_delegate->GetDialogButtons();
847 else
[email protected]478ff2ed2009-04-21 23:49:18848 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20849}
850
[email protected]71f65dd2009-02-11 19:14:56851void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
852 *success = false;
[email protected]fad84eab2008-12-05 00:37:20853
[email protected]1f460072009-05-28 17:02:07854 AppModalDialog* dialog_delegate =
855 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50856 if (dialog_delegate &&
857 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18858 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
859 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28860 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56861 *success = true;
[email protected]fad84eab2008-12-05 00:37:20862 }
[email protected]478ff2ed2009-04-21 23:49:18863 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
864 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56865 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28866 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56867 *success = true;
[email protected]fad84eab2008-12-05 00:37:20868 }
869 }
[email protected]c274acc2008-11-11 20:13:44870}
871
[email protected]fedaa7d2010-01-26 20:34:57872void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
873 if (browser_tracker_->ContainsHandle(handle)) {
874 Browser* browser = browser_tracker_->GetResource(handle);
875 browser->profile()->ShutdownSessionService();
876 *result = true;
877 } else {
878 *result = false;
879 }
880}
881
[email protected]71f65dd2009-02-11 19:14:56882void AutomationProvider::GetBrowserWindow(int index, int* handle) {
883 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29884 if (index >= 0) {
885 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]f07467d2010-06-16 14:28:30886 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {}
initial.commit09911bf2008-07-26 23:55:29887 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56888 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29889 }
890 }
initial.commit09911bf2008-07-26 23:55:29891}
892
[email protected]24497032009-05-01 17:00:29893void AutomationProvider::FindNormalBrowserWindow(int* handle) {
894 *handle = 0;
895 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31896 Browser::TYPE_NORMAL,
897 false);
[email protected]24497032009-05-01 17:00:29898 if (browser)
899 *handle = browser_tracker_->Add(browser);
900}
901
[email protected]71f65dd2009-02-11 19:14:56902void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
903 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29904 Browser* browser = BrowserList::GetLastActive();
905 if (browser)
[email protected]71f65dd2009-02-11 19:14:56906 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29907}
908
[email protected]b2aa3ed72010-02-01 18:37:14909#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28910// TODO(estade): use this implementation for all platforms?
911void AutomationProvider::GetActiveWindow(int* handle) {
912 gfx::NativeWindow window =
913 BrowserList::GetLastActive()->window()->GetNativeHandle();
914 *handle = window_tracker_->Add(window);
915}
916#endif
917
[email protected]4f6381ee2009-04-16 02:46:33918void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
919 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56920 *success = false;
[email protected]4ae62752008-08-04 23:28:47921 if (browser_tracker_->ContainsHandle(handle)) {
922 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14923 if (browser->command_updater()->SupportsCommand(command) &&
924 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47925 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56926 *success = true;
[email protected]4ae62752008-08-04 23:28:47927 }
928 }
[email protected]4ae62752008-08-04 23:28:47929}
930
[email protected]4f6381ee2009-04-16 02:46:33931void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56932 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53933 // List of commands which just finish synchronously and don't require
934 // setting up an observer.
935 static const int kSynchronousCommands[] = {
936 IDC_HOME,
937 IDC_SELECT_NEXT_TAB,
938 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25939 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53940 };
[email protected]56e71b7c2009-03-27 03:05:56941 if (browser_tracker_->ContainsHandle(handle)) {
942 Browser* browser = browser_tracker_->GetResource(handle);
943 if (browser->command_updater()->SupportsCommand(command) &&
944 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53945 // First check if we can handle the command without using an observer.
946 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
947 if (command == kSynchronousCommands[i]) {
948 browser->ExecuteCommand(command);
949 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
950 true);
951 Send(reply_message);
952 return;
953 }
954 }
955
956 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42957 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
958 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27959 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42960 return;
961 }
[email protected]56e71b7c2009-03-27 03:05:56962 }
963 }
[email protected]49a14a82009-03-31 04:16:44964 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56965 Send(reply_message);
966}
967
[email protected]fc2e0872009-08-21 22:14:41968// This task just adds another task to the event queue. This is useful if
969// you want to ensure that any tasks added to the event queue after this one
970// have already been processed by the time |task| is run.
971class InvokeTaskLaterTask : public Task {
972 public:
973 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
974 virtual ~InvokeTaskLaterTask() {}
975
976 virtual void Run() {
977 MessageLoop::current()->PostTask(FROM_HERE, task_);
978 }
979
980 private:
981 Task* task_;
982
983 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
984};
985
initial.commit09911bf2008-07-26 23:55:29986void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
987 int handle,
[email protected]d1a5941e2009-08-13 23:34:24988 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29989 int flags) {
[email protected]b410bc32009-08-14 01:11:14990 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51991 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41992 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29993 }
994}
995
[email protected]60507b12009-11-02 23:51:35996void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
997 int handle,
998 const gfx::Point& location) {
999 if (window_tracker_->ContainsHandle(handle))
1000 ui_controls::SendMouseMove(location.x(), location.y());
1001}
1002
initial.commit09911bf2008-07-26 23:55:291003void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
1004 int handle,
[email protected]bc1407f2009-09-29 00:33:351005 int key,
initial.commit09911bf2008-07-26 23:55:291006 int flags) {
[email protected]b410bc32009-08-14 01:11:141007 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:291008 return;
1009
[email protected]b410bc32009-08-14 01:11:141010 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291011 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:351012 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:381013 ((flags & views::Event::EF_CONTROL_DOWN) ==
1014 views::Event::EF_CONTROL_DOWN),
1015 ((flags & views::Event::EF_SHIFT_DOWN) ==
1016 views::Event::EF_SHIFT_DOWN),
1017 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301018 views::Event::EF_ALT_DOWN),
1019 ((flags & views::Event::EF_COMMAND_DOWN) ==
1020 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291021}
initial.commit09911bf2008-07-26 23:55:291022
[email protected]71f65dd2009-02-11 19:14:561023void AutomationProvider::IsWindowActive(int handle, bool* success,
1024 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291025 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171026 *is_active =
1027 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561028 *success = true;
initial.commit09911bf2008-07-26 23:55:291029 } else {
[email protected]71f65dd2009-02-11 19:14:561030 *success = false;
1031 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291032 }
1033}
1034
[email protected]71f65dd2009-02-11 19:14:561035void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1036 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291037
1038 if (browser_tracker_->ContainsHandle(handle)) {
1039 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561040 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291041 }
initial.commit09911bf2008-07-26 23:55:291042}
1043
[email protected]982921f12009-10-27 21:43:531044void AutomationProvider::GetType(int handle, int* type_as_int) {
1045 *type_as_int = -1; // -1 is the error code
1046
1047 if (browser_tracker_->ContainsHandle(handle)) {
1048 Browser* browser = browser_tracker_->GetResource(handle);
1049 *type_as_int = static_cast<int>(browser->type());
1050 }
1051}
1052
[email protected]71f65dd2009-02-11 19:14:561053void AutomationProvider::GetTab(int win_handle, int tab_index,
1054 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561055 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291056 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1057 Browser* browser = browser_tracker_->GetResource(win_handle);
1058 if (tab_index < browser->tab_count()) {
1059 TabContents* tab_contents =
1060 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571061 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291062 }
1063 }
initial.commit09911bf2008-07-26 23:55:291064}
1065
[email protected]71f65dd2009-02-11 19:14:561066void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1067 std::wstring* title) {
1068 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291069 if (tab_tracker_->ContainsHandle(handle)) {
1070 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361071 NavigationEntry* entry = tab->GetActiveEntry();
1072 if (entry != NULL) {
1073 *title = UTF16ToWideHack(entry->title());
1074 } else {
1075 *title = std::wstring();
1076 }
[email protected]71f65dd2009-02-11 19:14:561077 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291078 }
initial.commit09911bf2008-07-26 23:55:291079}
1080
[email protected]77bc6732009-04-20 22:01:031081void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1082 *tabstrip_index = -1; // -1 is the error code
1083
1084 if (tab_tracker_->ContainsHandle(handle)) {
1085 NavigationController* tab = tab_tracker_->GetResource(handle);
1086 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121087 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031088 }
1089}
1090
initial.commit09911bf2008-07-26 23:55:291091void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1092 if (window_tracker_->ContainsHandle(handle)) {
1093 window_tracker_->Remove(window_tracker_->GetResource(handle));
1094 }
1095}
1096
1097void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161098 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571099 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291100}
1101
1102// TODO(brettw) change this to accept GURLs when history supports it
1103void AutomationProvider::OnRedirectQueryComplete(
1104 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371105 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291106 bool success,
[email protected]379c2b12009-07-01 21:50:331107 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291108 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561109 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291110
[email protected]deb57402009-02-06 01:35:301111 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101112 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291113 if (success) {
initial.commit09911bf2008-07-26 23:55:291114 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301115 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291116 }
1117
[email protected]4f3dc372009-02-24 00:10:291118 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301119
[email protected]71f65dd2009-02-11 19:14:561120 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231121 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561122 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291123}
1124
1125bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571126 DCHECK(channel_.get());
1127 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291128}
1129
1130Browser* AutomationProvider::FindAndActivateTab(
1131 NavigationController* controller) {
1132 int tab_index;
1133 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1134 if (browser)
1135 browser->SelectTabContentsAt(tab_index, true);
1136
1137 return browser;
1138}
1139
[email protected]9eaa18e2010-06-29 20:51:011140namespace {
1141
1142class GetCookiesTask : public Task {
1143 public:
1144 GetCookiesTask(const GURL& url,
1145 URLRequestContextGetter* context_getter,
1146 base::WaitableEvent* event,
1147 std::string* cookies)
1148 : url_(url),
1149 context_getter_(context_getter),
1150 event_(event),
1151 cookies_(cookies) {}
1152
1153 virtual void Run() {
1154 *cookies_ = context_getter_->GetCookieStore()->GetCookies(url_);
1155 event_->Signal();
1156 }
1157
1158 private:
1159 const GURL& url_;
1160 URLRequestContextGetter* const context_getter_;
1161 base::WaitableEvent* const event_;
1162 std::string* const cookies_;
1163
1164 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
1165};
1166
1167std::string GetCookiesForURL(
1168 const GURL& url,
1169 URLRequestContextGetter* context_getter) {
1170 std::string cookies;
1171 base::WaitableEvent event(true /* manual reset */,
1172 false /* not initially signaled */);
1173 CHECK(ChromeThread::PostTask(
1174 ChromeThread::IO, FROM_HERE,
1175 new GetCookiesTask(url, context_getter, &event, &cookies)));
1176 event.Wait();
1177 return cookies;
1178}
1179
1180class SetCookieTask : public Task {
1181 public:
1182 SetCookieTask(const GURL& url,
1183 const std::string& value,
1184 URLRequestContextGetter* context_getter,
1185 base::WaitableEvent* event,
1186 bool* rv)
1187 : url_(url),
1188 value_(value),
1189 context_getter_(context_getter),
1190 event_(event),
1191 rv_(rv) {}
1192
1193 virtual void Run() {
1194 *rv_ = context_getter_->GetCookieStore()->SetCookie(url_, value_);
1195 event_->Signal();
1196 }
1197
1198 private:
1199 const GURL& url_;
1200 const std::string& value_;
1201 URLRequestContextGetter* const context_getter_;
1202 base::WaitableEvent* const event_;
1203 bool* const rv_;
1204
1205 DISALLOW_COPY_AND_ASSIGN(SetCookieTask);
1206};
1207
1208bool SetCookieForURL(
1209 const GURL& url,
1210 const std::string& value,
1211 URLRequestContextGetter* context_getter) {
1212 base::WaitableEvent event(true /* manual reset */,
1213 false /* not initially signaled */);
1214 bool rv = false;
1215 CHECK(ChromeThread::PostTask(
1216 ChromeThread::IO, FROM_HERE,
1217 new SetCookieTask(url, value, context_getter, &event, &rv)));
1218 event.Wait();
1219 return rv;
1220}
1221
1222class DeleteCookieTask : public Task {
1223 public:
1224 DeleteCookieTask(const GURL& url,
1225 const std::string& name,
1226 const scoped_refptr<URLRequestContextGetter>& context_getter)
1227 : url_(url),
1228 name_(name),
1229 context_getter_(context_getter) {}
1230
1231 virtual void Run() {
1232 net::CookieStore* cookie_store = context_getter_->GetCookieStore();
1233 cookie_store->DeleteCookie(url_, name_);
1234 }
1235
1236 private:
1237 const GURL url_;
1238 const std::string name_;
1239 const scoped_refptr<URLRequestContextGetter> context_getter_;
1240
1241 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
1242};
1243
1244} // namespace
1245
[email protected]71f65dd2009-02-11 19:14:561246void AutomationProvider::GetCookies(const GURL& url, int handle,
1247 int* value_size,
1248 std::string* value) {
1249 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291250 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1251 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311252
1253 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001254 scoped_refptr<URLRequestContextGetter> request_context =
1255 tab->tab_contents()->request_context();
1256 if (!request_context.get())
1257 request_context = tab->profile()->GetRequestContext();
1258
[email protected]9eaa18e2010-06-29 20:51:011259 *value = GetCookiesForURL(url, request_context.get());
[email protected]71f65dd2009-02-11 19:14:561260 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291261 }
initial.commit09911bf2008-07-26 23:55:291262}
1263
[email protected]71f65dd2009-02-11 19:14:561264void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291265 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561266 int handle,
1267 int* response_value) {
1268 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291269
1270 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1271 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311272
[email protected]dfa46e5f2009-11-17 18:48:431273 scoped_refptr<URLRequestContextGetter> request_context =
1274 tab->tab_contents()->request_context();
1275 if (!request_context.get())
1276 request_context = tab->profile()->GetRequestContext();
1277
[email protected]9eaa18e2010-06-29 20:51:011278 if (SetCookieForURL(url, value, request_context.get()))
[email protected]71f65dd2009-02-11 19:14:561279 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291280 }
initial.commit09911bf2008-07-26 23:55:291281}
1282
[email protected]5fa57942010-04-21 23:07:221283void AutomationProvider::DeleteCookie(const GURL& url,
1284 const std::string& cookie_name,
1285 int handle, bool* success) {
1286 *success = false;
1287 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1288 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]9eaa18e2010-06-29 20:51:011289 ChromeThread::PostTask(
1290 ChromeThread::IO, FROM_HERE,
1291 new DeleteCookieTask(url, cookie_name,
1292 tab->profile()->GetRequestContext()));
[email protected]5fa57942010-04-21 23:07:221293 *success = true;
1294 }
1295}
1296
[email protected]a503c97c2010-07-16 13:05:481297void AutomationProvider::ShowCollectedCookiesDialog(
1298 int handle, bool* success) {
1299 *success = false;
1300 if (tab_tracker_->ContainsHandle(handle)) {
1301 TabContents* tab_contents =
1302 tab_tracker_->GetResource(handle)->tab_contents();
1303 tab_contents->delegate()->ShowCollectedCookiesDialog(tab_contents);
1304 *success = true;
1305 }
1306}
1307
[email protected]71f65dd2009-02-11 19:14:561308void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1309 *success = false;
initial.commit09911bf2008-07-26 23:55:291310 if (tab_tracker_->ContainsHandle(handle)) {
1311 NavigationController* tab = tab_tracker_->GetResource(handle);
1312 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541313 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561314 *success = true;
initial.commit09911bf2008-07-26 23:55:291315 }
initial.commit09911bf2008-07-26 23:55:291316}
1317
[email protected]71f65dd2009-02-11 19:14:561318void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1319 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291320
1321 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561322 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341323 TabContents* tab_contents =
1324 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161325 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1326 if (rph)
1327 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291328 }
initial.commit09911bf2008-07-26 23:55:291329}
1330
1331void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331332 NOTREACHED() << "This function has been deprecated. "
1333 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291334}
1335
[email protected]71f65dd2009-02-11 19:14:561336void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291337 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561338 const std::wstring& script,
1339 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291340 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341341 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1342 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571343 // Set the routing id of this message with the controller.
1344 // This routing id needs to be remembered for the reverse
1345 // communication while sending back the response of
1346 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331347 std::wstring set_automation_id;
1348 SStringPrintf(&set_automation_id,
1349 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561350 reply_message->routing_id());
1351
1352 DCHECK(reply_message_ == NULL);
1353 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291354
[email protected]57c6a652009-05-04 07:58:341355 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331356 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341357 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061358 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571359 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291360 }
1361
1362 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561363 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1364 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291365 }
1366}
1367
[email protected]71f65dd2009-02-11 19:14:561368void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1369 *visible = false;
[email protected]20e93d12008-08-28 16:31:571370
[email protected]59560e0b2009-06-04 03:30:221371 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251372#if defined(OS_CHROMEOS)
1373 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1374 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1375 for (BrowserList::const_iterator it = BrowserList::begin();
1376 it != BrowserList::end(); ++it) {
1377 if ((*it)->type() == Browser::TYPE_POPUP) {
1378 const GURL& url =
1379 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1380
1381 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1382 url.host() == chrome::kChromeUIFileBrowseHost) {
1383 *visible = true;
1384 break;
1385 }
1386 }
1387 }
1388#else
[email protected]59560e0b2009-06-04 03:30:221389 Browser* browser = browser_tracker_->GetResource(handle);
1390 if (browser) {
1391 *visible = browser->window()->IsDownloadShelfVisible();
1392 }
[email protected]f5bf8ccf2010-02-05 18:19:251393#endif
[email protected]59560e0b2009-06-04 03:30:221394 }
initial.commit09911bf2008-07-26 23:55:291395}
1396
[email protected]59560e0b2009-06-04 03:30:221397void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1398 if (browser_tracker_->ContainsHandle(handle)) {
1399 Browser* browser = browser_tracker_->GetResource(handle);
1400 if (browser) {
1401 if (visible)
1402 browser->window()->GetDownloadShelf()->Show();
1403 else
1404 browser->window()->GetDownloadShelf()->Close();
1405 }
1406 }
1407}
1408
[email protected]34930432009-11-09 00:12:091409void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1410 *visible = false;
1411
1412 if (browser_tracker_->ContainsHandle(handle)) {
1413 Browser* browser = browser_tracker_->GetResource(handle);
1414 if (browser)
1415 *visible = browser->window()->IsFullscreen();
1416 }
1417}
1418
1419void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1420 bool* visible) {
1421 *visible = false;
1422
1423 if (browser_tracker_->ContainsHandle(handle)) {
1424 Browser* browser = browser_tracker_->GetResource(handle);
1425 if (browser)
1426 *visible = browser->window()->IsFullscreenBubbleVisible();
1427 }
1428}
[email protected]59560e0b2009-06-04 03:30:221429
[email protected]71f65dd2009-02-11 19:14:561430void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1431 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291432 if (tab_tracker_->ContainsHandle(handle)) {
1433 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111434 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291435 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561436 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291437 }
1438 }
initial.commit09911bf2008-07-26 23:55:291439}
1440
initial.commit09911bf2008-07-26 23:55:291441void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561442 int handle, const std::wstring& find_request,
1443 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311444 NOTREACHED() << "This function has been deprecated."
1445 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561446 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311447 return;
1448}
1449
[email protected]4f999132009-03-31 18:08:401450void AutomationProvider::HandleFindRequest(
1451 int handle,
1452 const AutomationMsg_Find_Params& params,
1453 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291454 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561455 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1456 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291457 return;
1458 }
1459
1460 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111461 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291462
1463 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141464 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291465
[email protected]57c6a652009-05-04 07:58:341466 tab_contents->set_current_find_request_id(
1467 FindInPageNotificationObserver::kFindInPageRequestId);
1468 tab_contents->render_view_host()->StartFinding(
1469 FindInPageNotificationObserver::kFindInPageRequestId,
1470 params.search_string, params.forward, params.match_case,
1471 params.find_next);
initial.commit09911bf2008-07-26 23:55:291472}
1473
[email protected]5f8af2a2008-08-06 22:49:451474void AutomationProvider::HandleOpenFindInPageRequest(
1475 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291476 if (browser_tracker_->ContainsHandle(handle)) {
1477 Browser* browser = browser_tracker_->GetResource(handle);
1478 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451479 }
1480}
1481
[email protected]71f65dd2009-02-11 19:14:561482void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561483 *visible = false;
[email protected]855c0142009-09-28 22:35:241484 Browser* browser = browser_tracker_->GetResource(handle);
1485 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581486 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171487 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241488 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291489 }
[email protected]20e93d12008-08-28 16:31:571490}
1491
[email protected]71f65dd2009-02-11 19:14:561492void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1493 int* y) {
[email protected]9e0534b2008-10-21 15:03:011494 gfx::Point position(0, 0);
1495 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291496 if (browser_tracker_->ContainsHandle(handle)) {
1497 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581498 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171499 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581500 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291501 }
[email protected]20e93d12008-08-28 16:31:571502
[email protected]71f65dd2009-02-11 19:14:561503 *x = position.x();
1504 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571505}
1506
[email protected]4512cb52010-04-05 19:50:251507// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1508// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581509void AutomationProvider::GetBookmarkBarVisibility(int handle,
1510 bool* visible,
1511 bool* animating) {
1512 *visible = false;
1513 *animating = false;
1514
1515 if (browser_tracker_->ContainsHandle(handle)) {
1516 Browser* browser = browser_tracker_->GetResource(handle);
1517 if (browser) {
[email protected]472f099b2010-05-27 17:07:121518#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1519 // TODO(jrg): Was removed in rev43789 for perf. Need to investigate.
1520
[email protected]ab6ca392010-04-07 00:44:131521 // IsBookmarkBarVisible() line looks correct but is not
1522 // consistent across platforms. Specifically, on Mac/Linux, it
1523 // returns false if the bar is hidden in a pref (even if visible
1524 // on the NTP). On ChromeOS, it returned true if on NTP
1525 // independent of the pref. Making the code more consistent
1526 // caused a perf bot regression on Windows (which shares views).
1527 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251528 *visible = browser->profile()->GetPrefs()->GetBoolean(
1529 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401530#else
1531 *visible = browser->window()->IsBookmarkBarVisible();
1532#endif
[email protected]c3240722010-03-05 21:52:581533 *animating = browser->window()->IsBookmarkBarAnimating();
1534 }
1535 }
1536}
1537
[email protected]6d8ffc9f2010-03-12 18:27:531538void AutomationProvider::GetBookmarksAsJSON(int handle,
1539 std::string* bookmarks_as_json,
1540 bool *success) {
1541 *success = false;
1542 if (browser_tracker_->ContainsHandle(handle)) {
1543 Browser* browser = browser_tracker_->GetResource(handle);
1544 if (browser) {
1545 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1546 return;
1547 }
1548 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1549 browser->profile(),
1550 browser->profile()->GetBookmarkModel());
1551 *success = storage->SerializeData(bookmarks_as_json);
1552 }
1553 }
1554}
1555
1556void AutomationProvider::WaitForBookmarkModelToLoad(
1557 int handle,
1558 IPC::Message* reply_message) {
1559 if (browser_tracker_->ContainsHandle(handle)) {
1560 Browser* browser = browser_tracker_->GetResource(handle);
1561 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1562 if (model->IsLoaded()) {
1563 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1564 reply_message, true);
1565 Send(reply_message);
1566 } else {
1567 // The observer will delete itself when done.
1568 new AutomationProviderBookmarkModelObserver(this, reply_message,
1569 model);
1570 }
1571 }
1572}
1573
1574void AutomationProvider::AddBookmarkGroup(int handle,
1575 int64 parent_id, int index,
1576 std::wstring title,
1577 bool* success) {
1578 if (browser_tracker_->ContainsHandle(handle)) {
1579 Browser* browser = browser_tracker_->GetResource(handle);
1580 if (browser) {
1581 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1582 if (!model->IsLoaded()) {
1583 *success = false;
1584 return;
1585 }
1586 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1587 DCHECK(parent);
1588 if (parent) {
1589 const BookmarkNode* child = model->AddGroup(parent, index,
1590 WideToUTF16(title));
1591 DCHECK(child);
1592 if (child)
1593 *success = true;
1594 }
1595 }
1596 }
1597 *success = false;
1598}
1599
1600void AutomationProvider::AddBookmarkURL(int handle,
1601 int64 parent_id, int index,
1602 std::wstring title, const GURL& url,
1603 bool* success) {
1604 if (browser_tracker_->ContainsHandle(handle)) {
1605 Browser* browser = browser_tracker_->GetResource(handle);
1606 if (browser) {
1607 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1608 if (!model->IsLoaded()) {
1609 *success = false;
1610 return;
1611 }
1612 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1613 DCHECK(parent);
1614 if (parent) {
1615 const BookmarkNode* child = model->AddURL(parent, index,
1616 WideToUTF16(title), url);
1617 DCHECK(child);
1618 if (child)
1619 *success = true;
1620 }
1621 }
1622 }
1623 *success = false;
1624}
1625
1626void AutomationProvider::ReparentBookmark(int handle,
1627 int64 id, int64 new_parent_id,
1628 int index,
1629 bool* success) {
1630 if (browser_tracker_->ContainsHandle(handle)) {
1631 Browser* browser = browser_tracker_->GetResource(handle);
1632 if (browser) {
1633 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1634 if (!model->IsLoaded()) {
1635 *success = false;
1636 return;
1637 }
1638 const BookmarkNode* node = model->GetNodeByID(id);
1639 DCHECK(node);
1640 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1641 DCHECK(new_parent);
1642 if (node && new_parent) {
1643 model->Move(node, new_parent, index);
1644 *success = true;
1645 }
1646 }
1647 }
1648 *success = false;
1649}
1650
1651void AutomationProvider::SetBookmarkTitle(int handle,
1652 int64 id, std::wstring title,
1653 bool* success) {
1654 if (browser_tracker_->ContainsHandle(handle)) {
1655 Browser* browser = browser_tracker_->GetResource(handle);
1656 if (browser) {
1657 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1658 if (!model->IsLoaded()) {
1659 *success = false;
1660 return;
1661 }
1662 const BookmarkNode* node = model->GetNodeByID(id);
1663 DCHECK(node);
1664 if (node) {
1665 model->SetTitle(node, WideToUTF16(title));
1666 *success = true;
1667 }
1668 }
1669 }
1670 *success = false;
1671}
1672
1673void AutomationProvider::SetBookmarkURL(int handle,
1674 int64 id, const GURL& url,
1675 bool* success) {
1676 if (browser_tracker_->ContainsHandle(handle)) {
1677 Browser* browser = browser_tracker_->GetResource(handle);
1678 if (browser) {
1679 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1680 if (!model->IsLoaded()) {
1681 *success = false;
1682 return;
1683 }
1684 const BookmarkNode* node = model->GetNodeByID(id);
1685 DCHECK(node);
1686 if (node) {
1687 model->SetURL(node, url);
1688 *success = true;
1689 }
1690 }
1691 }
1692 *success = false;
1693}
1694
1695void AutomationProvider::RemoveBookmark(int handle,
1696 int64 id,
1697 bool* success) {
1698 if (browser_tracker_->ContainsHandle(handle)) {
1699 Browser* browser = browser_tracker_->GetResource(handle);
1700 if (browser) {
1701 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1702 if (!model->IsLoaded()) {
1703 *success = false;
1704 return;
1705 }
1706 const BookmarkNode* node = model->GetNodeByID(id);
1707 DCHECK(node);
1708 if (node) {
1709 const BookmarkNode* parent = node->GetParent();
1710 DCHECK(parent);
1711 model->Remove(parent, parent->IndexOfChild(node));
1712 *success = true;
1713 }
1714 }
1715 }
1716 *success = false;
1717}
1718
[email protected]ef413ca2010-05-25 21:09:141719// Sample json input: { "command": "SetWindowDimensions",
1720// "x": 20, # optional
1721// "y": 20, # optional
1722// "width": 800, # optional
1723// "height": 600 } # optional
1724void AutomationProvider::SetWindowDimensions(Browser* browser,
1725 DictionaryValue* args,
1726 IPC::Message* reply_message) {
1727 gfx::Rect rect = browser->window()->GetRestoredBounds();
1728 int x, y, width, height;
1729 if (args->GetInteger(L"x", &x))
1730 rect.set_x(x);
1731 if (args->GetInteger(L"y", &y))
1732 rect.set_y(y);
1733 if (args->GetInteger(L"width", &width))
1734 rect.set_width(width);
1735 if (args->GetInteger(L"height", &height))
1736 rect.set_height(height);
1737 browser->window()->SetBounds(rect);
[email protected]7c983cc2010-07-16 11:33:341738 AutomationJSONReply(this, reply_message).SendSuccess(NULL);
[email protected]ef413ca2010-05-25 21:09:141739}
1740
[email protected]38b5a3852010-07-21 06:49:521741ListValue* AutomationProvider::GetInfobarsInfo(TabContents* tc) {
1742 // Each infobar may have different properties depending on the type.
1743 ListValue* infobars = new ListValue;
1744 for (int infobar_index = 0;
1745 infobar_index < tc->infobar_delegate_count();
1746 ++infobar_index) {
1747 DictionaryValue* infobar_item = new DictionaryValue;
1748 InfoBarDelegate* infobar = tc->GetInfoBarDelegateAt(infobar_index);
1749 if (infobar->AsConfirmInfoBarDelegate()) {
1750 // Also covers ThemeInstalledInfoBarDelegate and
1751 // CrashedExtensionInfoBarDelegate.
1752 infobar_item->SetString(L"type", "confirm_infobar");
1753 ConfirmInfoBarDelegate* confirm_infobar =
1754 infobar->AsConfirmInfoBarDelegate();
1755 infobar_item->SetString(L"text", confirm_infobar->GetMessageText());
1756 infobar_item->SetString(L"link_text", confirm_infobar->GetLinkText());
1757 ListValue* buttons_list = new ListValue;
1758 int buttons = confirm_infobar->GetButtons();
1759 if (ConfirmInfoBarDelegate::BUTTON_OK & buttons) {
1760 StringValue* button_label = new StringValue(
1761 confirm_infobar->GetButtonLabel(
1762 ConfirmInfoBarDelegate::BUTTON_OK));
1763 buttons_list->Append(button_label);
1764 }
1765 if (ConfirmInfoBarDelegate::BUTTON_CANCEL & buttons) {
1766 StringValue* button_label = new StringValue(
1767 confirm_infobar->GetButtonLabel(
1768 ConfirmInfoBarDelegate::BUTTON_CANCEL));
1769 buttons_list->Append(button_label);
1770 }
1771 infobar_item->Set(L"buttons", buttons_list);
1772 } else if (infobar->AsAlertInfoBarDelegate()) {
1773 infobar_item->SetString(L"type", "alert_infobar");
1774 AlertInfoBarDelegate* alert_infobar =
1775 infobar->AsAlertInfoBarDelegate();
1776 infobar_item->SetString(L"text", alert_infobar->GetMessageText());
1777 } else if (infobar->AsLinkInfoBarDelegate()) {
1778 infobar_item->SetString(L"type", "link_infobar");
1779 LinkInfoBarDelegate* link_infobar = infobar->AsLinkInfoBarDelegate();
1780 infobar_item->SetString(L"link_text", link_infobar->GetLinkText());
1781 } else if (infobar->AsTranslateInfoBarDelegate()) {
1782 infobar_item->SetString(L"type", "translate_infobar");
1783 TranslateInfoBarDelegate* translate_infobar =
1784 infobar->AsTranslateInfoBarDelegate();
1785 infobar_item->SetString(L"original_lang_code",
1786 translate_infobar->GetOriginalLanguageCode());
1787 infobar_item->SetString(L"target_lang_code",
1788 translate_infobar->GetTargetLanguageCode());
1789 } else if (infobar->AsExtensionInfoBarDelegate()) {
1790 infobar_item->SetString(L"type", "extension_infobar");
1791 } else {
1792 infobar_item->SetString(L"type", "unknown_infobar");
1793 }
1794 infobars->Append(infobar_item);
1795 }
1796 return infobars;
1797}
1798
1799// Sample json input: { "command": "WaitForInfobarCount",
1800// "count": COUNT,
1801// "tab_index": INDEX }
1802// Sample output: {}
1803void AutomationProvider::WaitForInfobarCount(Browser* browser,
1804 DictionaryValue* args,
1805 IPC::Message* reply_message) {
1806 int tab_index;
1807 int count;
1808 if (!args->GetInteger(L"count", &count) || count < 0 ||
1809 !args->GetInteger(L"tab_index", &tab_index) || tab_index < 0) {
1810 AutomationJSONReply(this, reply_message).SendError(
1811 "Missing or invalid args: 'count', 'tab_index'.");
1812 return;
1813 }
1814
1815 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
1816 // Observer deletes itself.
1817 new WaitForInfobarCountObserver(this, reply_message, tab_contents, count);
1818}
1819
[email protected]e004a2d2010-07-22 04:55:281820// Sample json input: { "command": "PerformActionOnInfobar",
1821// "action": "dismiss",
1822// "infobar_index": 0,
1823// "tab_index": 0 }
1824// Sample output: {}
1825void AutomationProvider::PerformActionOnInfobar(Browser* browser,
1826 DictionaryValue* args,
1827 IPC::Message* reply_message) {
1828 AutomationJSONReply reply(this, reply_message);
1829 int tab_index;
1830 int infobar_index;
1831 std::string action;
1832 if (!args->GetInteger(L"tab_index", &tab_index) ||
1833 !args->GetInteger(L"infobar_index", &infobar_index) ||
1834 !args->GetString(L"action", &action)) {
1835 reply.SendError("Invalid or missing args");
1836 return;
1837 }
1838 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
1839 if (!tab_contents) {
1840 reply.SendError(StringPrintf("No such tab at index %d", tab_index));
1841 return;
1842 }
1843 InfoBarDelegate* infobar = NULL;
1844 if (infobar_index < 0 ||
1845 infobar_index >= tab_contents->infobar_delegate_count() ||
1846 !(infobar = tab_contents->GetInfoBarDelegateAt(infobar_index))) {
1847 reply.SendError(StringPrintf("No such infobar at index %d", infobar_index));
1848 return;
1849 }
1850 if ("dismiss" == action) {
1851 infobar->InfoBarDismissed();
1852 tab_contents->RemoveInfoBar(infobar);
1853 reply.SendSuccess(NULL);
1854 return;
1855 }
1856 if ("accept" == action || "cancel" == action) {
1857 ConfirmInfoBarDelegate* confirm_infobar;
1858 if (!(confirm_infobar = infobar->AsConfirmInfoBarDelegate())) {
1859 reply.SendError("Not a confirm infobar");
1860 return;
1861 }
1862 if ("accept" == action) {
1863 if (confirm_infobar->Accept())
1864 tab_contents->RemoveInfoBar(infobar);
1865 } else if ("cancel" == action) {
1866 if (confirm_infobar->Cancel())
1867 tab_contents->RemoveInfoBar(infobar);
1868 }
1869 reply.SendSuccess(NULL);
1870 return;
1871 }
1872 reply.SendError("Invalid action");
1873}
1874
[email protected]26adfd312010-07-20 01:04:461875namespace {
1876
1877// Task to get info about BrowserChildProcessHost. Must run on IO thread to
1878// honor the semantics of BrowserChildProcessHost.
1879// Used by AutomationProvider::GetBrowserInfo().
1880class GetChildProcessHostInfoTask : public Task {
1881 public:
1882 GetChildProcessHostInfoTask(base::WaitableEvent* event,
1883 ListValue* child_processes)
1884 : event_(event),
1885 child_processes_(child_processes) {}
1886
1887 virtual void Run() {
1888 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
1889 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
1890 // Only add processes which are already started,
1891 // since we need their handle.
1892 if ((*iter)->handle() == base::kNullProcessHandle) {
1893 continue;
1894 }
1895 ChildProcessInfo* info = *iter;
1896 DictionaryValue* item = new DictionaryValue;
1897 item->SetString(L"name", info->name());
1898 item->SetString(L"type",
1899 ChildProcessInfo::GetTypeNameInEnglish(info->type()));
1900 item->SetInteger(L"pid", base::GetProcId(info->handle()));
1901 child_processes_->Append(item);
1902 }
1903 event_->Signal();
1904 }
1905
1906 private:
1907 base::WaitableEvent* const event_; // weak
1908 ListValue* child_processes_;
1909
1910 DISALLOW_COPY_AND_ASSIGN(GetChildProcessHostInfoTask);
1911};
1912
1913} // namespace
1914
[email protected]a9ff2c02010-05-13 17:33:051915// Sample json input: { "command": "GetBrowserInfo" }
1916// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1917// sample json output.
[email protected]53329582010-05-14 21:10:581918void AutomationProvider::GetBrowserInfo(Browser* browser,
1919 DictionaryValue* args,
[email protected]a9ff2c02010-05-13 17:33:051920 IPC::Message* reply_message) {
[email protected]a9ff2c02010-05-13 17:33:051921 DictionaryValue* properties = new DictionaryValue;
1922 properties->SetString(L"ChromeVersion", chrome::kChromeVersion);
1923 properties->SetString(L"BrowserProcessExecutableName",
1924 chrome::kBrowserProcessExecutableName);
1925 properties->SetString(L"HelperProcessExecutableName",
1926 chrome::kHelperProcessExecutableName);
1927 properties->SetString(L"BrowserProcessExecutablePath",
1928 chrome::kBrowserProcessExecutablePath);
1929 properties->SetString(L"HelperProcessExecutablePath",
1930 chrome::kHelperProcessExecutablePath);
[email protected]a9ff2c02010-05-13 17:33:051931 properties->SetString(L"command_line_string",
1932 CommandLine::ForCurrentProcess()->command_line_string());
[email protected]44eed9f2010-06-28 22:04:001933
1934 std::string branding;
1935#if defined(GOOGLE_CHROME_BUILD)
1936 branding = "Google Chrome";
1937#elif defined(CHROMIUM_BUILD)
1938 branding = "Chromium";
1939#else
1940 branding = "Unknown Branding";
[email protected]a9ff2c02010-05-13 17:33:051941#endif
[email protected]44eed9f2010-06-28 22:04:001942 properties->SetString(L"branding", branding);
[email protected]a9ff2c02010-05-13 17:33:051943
1944 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1945 return_value->Set(L"properties", properties);
1946
[email protected]ef413ca2010-05-25 21:09:141947 return_value->SetInteger(L"browser_pid", base::GetCurrentProcId());
1948 // Add info about all windows in a list of dictionaries, one dictionary
1949 // item per window.
1950 ListValue* windows = new ListValue;
1951 int windex = 0;
1952 for (BrowserList::const_iterator it = BrowserList::begin();
1953 it != BrowserList::end();
1954 ++it, ++windex) {
1955 DictionaryValue* browser_item = new DictionaryValue;
1956 browser = *it;
1957 browser_item->SetInteger(L"index", windex);
1958 // Window properties
1959 gfx::Rect rect = browser->window()->GetRestoredBounds();
1960 browser_item->SetInteger(L"x", rect.x());
1961 browser_item->SetInteger(L"y", rect.y());
1962 browser_item->SetInteger(L"width", rect.width());
1963 browser_item->SetInteger(L"height", rect.height());
1964 browser_item->SetBoolean(L"fullscreen",
1965 browser->window()->IsFullscreen());
1966 browser_item->SetInteger(L"selected_tab", browser->selected_index());
1967 browser_item->SetBoolean(L"incognito",
1968 browser->profile()->IsOffTheRecord());
1969 // For each window, add info about all tabs in a list of dictionaries,
1970 // one dictionary item per tab.
1971 ListValue* tabs = new ListValue;
1972 for (int i = 0; i < browser->tab_count(); ++i) {
1973 TabContents* tc = browser->GetTabContentsAt(i);
1974 DictionaryValue* tab = new DictionaryValue;
1975 tab->SetInteger(L"index", i);
1976 tab->SetString(L"url", tc->GetURL().spec());
1977 tab->SetInteger(L"renderer_pid",
1978 base::GetProcId(tc->GetRenderProcessHost()->GetHandle()));
[email protected]38b5a3852010-07-21 06:49:521979 tab->Set(L"infobars", GetInfobarsInfo(tc));
[email protected]ef413ca2010-05-25 21:09:141980 tabs->Append(tab);
1981 }
1982 browser_item->Set(L"tabs", tabs);
1983
1984 windows->Append(browser_item);
1985 }
1986 return_value->Set(L"windows", windows);
1987
1988 return_value->SetString(L"child_process_path",
1989 ChildProcessHost::GetChildPath(true).value());
1990 // Child processes are the processes for plugins and other workers.
1991 // Add all child processes in a list of dictionaries, one dictionary item
1992 // per child process.
1993 ListValue* child_processes = new ListValue;
[email protected]26adfd312010-07-20 01:04:461994 base::WaitableEvent event(true /* manual reset */,
1995 false /* not initially signaled */);
1996 CHECK(ChromeThread::PostTask(
1997 ChromeThread::IO, FROM_HERE,
1998 new GetChildProcessHostInfoTask(&event, child_processes)));
1999 event.Wait();
[email protected]ef413ca2010-05-25 21:09:142000 return_value->Set(L"child_processes", child_processes);
2001
2002 // Add all extension processes in a list of dictionaries, one dictionary
2003 // item per extension process.
2004 ListValue* extension_processes = new ListValue;
2005 ProfileManager* profile_manager = g_browser_process->profile_manager();
2006 for (ProfileManager::const_iterator it = profile_manager->begin();
2007 it != profile_manager->end(); ++it) {
2008 ExtensionProcessManager* process_manager =
2009 (*it)->GetExtensionProcessManager();
2010 ExtensionProcessManager::const_iterator jt;
2011 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) {
2012 ExtensionHost* ex_host = *jt;
2013 // Don't add dead extension processes.
2014 if (!ex_host->IsRenderViewLive())
2015 continue;
2016 DictionaryValue* item = new DictionaryValue;
2017 item->SetString(L"name", ex_host->extension()->name());
2018 item->SetInteger(
2019 L"pid",
2020 base::GetProcId(ex_host->render_process_host()->GetHandle()));
2021 extension_processes->Append(item);
2022 }
2023 }
2024 return_value->Set(L"extension_processes", extension_processes);
[email protected]7c983cc2010-07-16 11:33:342025 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]a9ff2c02010-05-13 17:33:052026}
2027
[email protected]24e2b102010-04-29 17:56:472028// Sample json input: { "command": "GetHistoryInfo",
2029// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:362030// Refer chrome/test/pyautolib/history_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582031void AutomationProvider::GetHistoryInfo(Browser* browser,
2032 DictionaryValue* args,
2033 IPC::Message* reply_message) {
[email protected]e6e376e2010-04-19 21:41:362034 consumer_.CancelAllRequests();
2035
[email protected]e53668962010-06-23 15:35:252036 string16 search_text;
2037 args->GetString("search_text", &search_text);
[email protected]e6e376e2010-04-19 21:41:362038
2039 // Fetch history.
2040 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
2041 history::QueryOptions options;
2042 // The observer owns itself. It deletes itself after it fetches history.
2043 AutomationProviderHistoryObserver* history_observer =
2044 new AutomationProviderHistoryObserver(this, reply_message);
2045 hs->QueryHistory(
2046 search_text,
2047 options,
2048 &consumer_,
2049 NewCallback(history_observer,
2050 &AutomationProviderHistoryObserver::HistoryQueryComplete));
2051}
2052
[email protected]bbe6aa02010-05-07 17:27:292053// Sample json input: { "command": "AddHistoryItem",
2054// "item": { "URL": "http://www.google.com",
2055// "title": "Google", # optional
2056// "time": 12345 # optional (time_t)
2057// } }
2058// Refer chrome/test/pyautolib/pyauto.py for details on input.
[email protected]53329582010-05-14 21:10:582059void AutomationProvider::AddHistoryItem(Browser* browser,
2060 DictionaryValue* args,
2061 IPC::Message* reply_message) {
[email protected]bbe6aa02010-05-07 17:27:292062 DictionaryValue* item = NULL;
2063 args->GetDictionary(L"item", &item);
2064 string16 url_text;
[email protected]e53668962010-06-23 15:35:252065 string16 title;
[email protected]bbe6aa02010-05-07 17:27:292066 base::Time time = base::Time::Now();
[email protected]7c983cc2010-07-16 11:33:342067 AutomationJSONReply reply(this, reply_message);
[email protected]bbe6aa02010-05-07 17:27:292068
[email protected]7c983cc2010-07-16 11:33:342069 if (!item->GetString("url", &url_text)) {
2070 reply.SendError("bad args (no URL in dict?)");
2071 return;
[email protected]bbe6aa02010-05-07 17:27:292072 }
[email protected]7c983cc2010-07-16 11:33:342073 GURL gurl(url_text);
2074 item->GetString("title", &title); // Don't care if it fails.
2075 int it;
2076 double dt;
2077 if (item->GetInteger(L"time", &it))
2078 time = base::Time::FromTimeT(it);
2079 else if (item->GetReal(L"time", &dt))
2080 time = base::Time::FromDoubleT(dt);
[email protected]f6ff0df2010-07-11 22:41:432081
[email protected]7c983cc2010-07-16 11:33:342082 // Ideas for "dummy" values (e.g. id_scope) came from
2083 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
2084 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
2085 const void* id_scope = reinterpret_cast<void*>(1);
2086 hs->AddPage(gurl, time,
2087 id_scope,
2088 0,
2089 GURL(),
2090 PageTransition::LINK,
2091 history::RedirectList(),
2092 false);
2093 if (title.length())
2094 hs->SetPageTitle(gurl, title);
2095 reply.SendSuccess(NULL);
[email protected]bbe6aa02010-05-07 17:27:292096}
2097
[email protected]24e2b102010-04-29 17:56:472098// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:362099// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582100void AutomationProvider::GetDownloadsInfo(Browser* browser,
2101 DictionaryValue* args,
2102 IPC::Message* reply_message) {
[email protected]d4adc292010-04-15 18:06:392103 AutomationProviderDownloadManagerObserver observer;
2104 std::vector<DownloadItem*> downloads;
2105 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]7c983cc2010-07-16 11:33:342106 AutomationJSONReply reply(this, reply_message);
[email protected]d4adc292010-04-15 18:06:392107
2108 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:342109 reply.SendError("no download manager");
2110 return;
[email protected]d4adc292010-04-15 18:06:392111 }
[email protected]7c983cc2010-07-16 11:33:342112 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
2113 // since that would be transient; a download might enter and empty out
2114 // the current download queue too soon to be noticed.
2115 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
2116 downloads = observer.Downloads();
[email protected]d4adc292010-04-15 18:06:392117
2118 std::map<DownloadItem::DownloadState, std::string> state_to_string;
2119 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
2120 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
2121 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
2122 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
2123
2124 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
2125 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
2126 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
2127 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
2128 std::string("DANGEROUS_BUT_VALIDATED");
2129
2130 ListValue* list_of_downloads = new ListValue;
2131 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
2132 it != downloads.end();
2133 it++) { // Fill info about each download item.
2134 DictionaryValue* dl_item_value = new DictionaryValue;
2135 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
2136 dl_item_value->SetString(L"url", (*it)->url().spec());
2137 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
[email protected]2e030682010-07-23 19:45:362138 dl_item_value->SetString(L"file_name", (*it)->GetFileName().value());
[email protected]d4adc292010-04-15 18:06:392139 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
2140 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
2141 dl_item_value->SetBoolean(L"open_when_complete",
2142 (*it)->open_when_complete());
2143 dl_item_value->SetBoolean(L"is_extension_install",
2144 (*it)->is_extension_install());
2145 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
2146 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
2147 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
2148 dl_item_value->SetString(L"safety_state",
2149 safety_state_to_string[(*it)->safety_state()]);
2150 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
2151 list_of_downloads->Append(dl_item_value);
2152 }
2153 return_value->Set(L"downloads", list_of_downloads);
[email protected]d4adc292010-04-15 18:06:392154
[email protected]7c983cc2010-07-16 11:33:342155 reply.SendSuccess(return_value.get());
[email protected]d4adc292010-04-15 18:06:392156 // All value objects allocated above are owned by |return_value|
2157 // and get freed by it.
2158}
2159
[email protected]59a611242010-04-02 02:24:042160void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:582161 Browser* browser,
[email protected]59a611242010-04-02 02:24:042162 DictionaryValue* args,
2163 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:042164 AutomationProviderDownloadManagerObserver observer;
2165 std::vector<DownloadItem*> downloads;
[email protected]7c983cc2010-07-16 11:33:342166 AutomationJSONReply reply(this, reply_message);
[email protected]59a611242010-04-02 02:24:042167
2168 // Look for a quick return.
2169 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:342170 reply.SendSuccess(NULL); // No download manager.
2171 return;
[email protected]59a611242010-04-02 02:24:042172 }
[email protected]7c983cc2010-07-16 11:33:342173 profile_->GetDownloadManager()->GetCurrentDownloads(&observer, FilePath());
2174 downloads = observer.Downloads();
2175 if (downloads.size() == 0) {
2176 reply.SendSuccess(NULL);
[email protected]f6ff0df2010-07-11 22:41:432177 return;
2178 }
[email protected]59a611242010-04-02 02:24:042179
2180 // The observer owns itself. When the last observed item pings, it
2181 // deletes itself.
2182 AutomationProviderDownloadItemObserver* item_observer =
2183 new AutomationProviderDownloadItemObserver(
2184 this, reply_message, downloads.size());
2185 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
2186 i != downloads.end();
2187 i++) {
2188 (*i)->AddObserver(item_observer);
2189 }
2190}
2191
[email protected]24e2b102010-04-29 17:56:472192// Sample json input: { "command": "GetPrefsInfo" }
2193// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582194void AutomationProvider::GetPrefsInfo(Browser* browser,
2195 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472196 IPC::Message* reply_message) {
[email protected]24e2b102010-04-29 17:56:472197 const PrefService::PreferenceSet& prefs =
2198 profile_->GetPrefs()->preference_set();
2199 DictionaryValue* items = new DictionaryValue;
2200 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
2201 it != prefs.end(); ++it) {
2202 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
2203 }
2204 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2205 return_value->Set(L"prefs", items); // return_value owns items.
[email protected]7c983cc2010-07-16 11:33:342206 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]24e2b102010-04-29 17:56:472207}
2208
2209// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:582210void AutomationProvider::SetPrefs(Browser* browser,
2211 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472212 IPC::Message* reply_message) {
[email protected]24e2b102010-04-29 17:56:472213 std::wstring path;
2214 Value* val;
[email protected]7c983cc2010-07-16 11:33:342215 AutomationJSONReply reply(this, reply_message);
[email protected]24e2b102010-04-29 17:56:472216 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
2217 PrefService* pref_service = profile_->GetPrefs();
2218 const PrefService::Preference* pref =
2219 pref_service->FindPreference(path.c_str());
2220 if (!pref) { // Not a registered pref.
[email protected]7c983cc2010-07-16 11:33:342221 reply.SendError("pref not registered.");
2222 return;
[email protected]24e2b102010-04-29 17:56:472223 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
[email protected]7c983cc2010-07-16 11:33:342224 reply.SendError("pref is managed. cannot be changed.");
2225 return;
[email protected]24e2b102010-04-29 17:56:472226 } else { // Set the pref.
2227 pref_service->Set(path.c_str(), *val);
2228 }
2229 } else {
[email protected]7c983cc2010-07-16 11:33:342230 reply.SendError("no pref path or value given.");
2231 return;
[email protected]24e2b102010-04-29 17:56:472232 }
2233
[email protected]7c983cc2010-07-16 11:33:342234 reply.SendSuccess(NULL);
[email protected]24e2b102010-04-29 17:56:472235}
2236
[email protected]53329582010-05-14 21:10:582237// Sample json input: { "command": "GetOmniboxInfo" }
2238// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
2239void AutomationProvider::GetOmniboxInfo(Browser* browser,
2240 DictionaryValue* args,
2241 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582242 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2243
2244 LocationBar* loc_bar = browser->window()->GetLocationBar();
2245 AutocompleteEditView* edit_view = loc_bar->location_entry();
2246 AutocompleteEditModel* model = edit_view->model();
2247
2248 // Fill up matches.
2249 ListValue* matches = new ListValue;
2250 const AutocompleteResult& result = model->result();
2251 for (AutocompleteResult::const_iterator i = result.begin();
2252 i != result.end(); ++i) {
2253 const AutocompleteMatch& match = *i;
2254 DictionaryValue* item = new DictionaryValue; // owned by return_value
2255 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
2256 item->SetBoolean(L"starred", match.starred);
2257 item->SetString(L"destination_url", match.destination_url.spec());
2258 item->SetString(L"contents", match.contents);
2259 item->SetString(L"description", match.description);
2260 matches->Append(item);
2261 }
2262 return_value->Set(L"matches", matches);
2263
2264 // Fill up other properties.
2265 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2266 properties->SetBoolean(L"has_focus", model->has_focus());
2267 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2268 properties->SetString(L"keyword", model->keyword());
2269 properties->SetString(L"text", edit_view->GetText());
2270 return_value->Set(L"properties", properties);
2271
[email protected]7c983cc2010-07-16 11:33:342272 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]53329582010-05-14 21:10:582273}
2274
2275// Sample json input: { "command": "SetOmniboxText",
2276// "text": "goog" }
2277void AutomationProvider::SetOmniboxText(Browser* browser,
2278 DictionaryValue* args,
2279 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582280 std::wstring text;
[email protected]7c983cc2010-07-16 11:33:342281 AutomationJSONReply reply(this, reply_message);
[email protected]53329582010-05-14 21:10:582282 if (!args->GetString(L"text", &text)) {
[email protected]7c983cc2010-07-16 11:33:342283 reply.SendError("text missing");
2284 return;
[email protected]53329582010-05-14 21:10:582285 }
[email protected]7c983cc2010-07-16 11:33:342286 browser->FocusLocationBar();
2287 LocationBar* loc_bar = browser->window()->GetLocationBar();
2288 AutocompleteEditView* edit_view = loc_bar->location_entry();
2289 edit_view->model()->OnSetFocus(false);
2290 edit_view->SetUserText(text);
2291 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582292}
2293
2294// Sample json input: { "command": "OmniboxMovePopupSelection",
2295// "count": 1 }
2296// Negative count implies up, positive implies down. Count values will be
2297// capped by the size of the popup list.
2298void AutomationProvider::OmniboxMovePopupSelection(
2299 Browser* browser,
2300 DictionaryValue* args,
2301 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582302 int count;
[email protected]7c983cc2010-07-16 11:33:342303 AutomationJSONReply reply(this, reply_message);
[email protected]53329582010-05-14 21:10:582304 if (!args->GetInteger(L"count", &count)) {
[email protected]7c983cc2010-07-16 11:33:342305 reply.SendError("count missing");
2306 return;
[email protected]53329582010-05-14 21:10:582307 }
[email protected]7c983cc2010-07-16 11:33:342308 LocationBar* loc_bar = browser->window()->GetLocationBar();
2309 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2310 model->OnUpOrDownKeyPressed(count);
2311 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582312}
2313
2314// Sample json input: { "command": "OmniboxAcceptInput" }
2315void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2316 DictionaryValue* args,
2317 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282318 NavigationController& controller =
2319 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122320 // Setup observer to wait until the selected item loads.
2321 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282322 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122323 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582324
2325 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582326}
2327
[email protected]a3cd5022010-06-16 18:25:292328// Sample json input: { "command": "GetInitialLoadTimes" }
2329// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2330void AutomationProvider::GetInitialLoadTimes(
2331 Browser*,
2332 DictionaryValue*,
2333 IPC::Message* reply_message) {
2334 scoped_ptr<DictionaryValue> return_value(
2335 initial_load_observer_->GetTimingInformation());
[email protected]f6ff0df2010-07-11 22:41:432336
2337 std::string json_return;
2338 base::JSONWriter::Write(return_value.get(), false, &json_return);
2339 AutomationMsg_SendJSONRequest::WriteReplyParams(
2340 reply_message, json_return, true);
2341 Send(reply_message);
[email protected]a3cd5022010-06-16 18:25:292342}
2343
[email protected]f7d48012010-05-06 08:17:052344// Sample json input: { "command": "GetPluginsInfo" }
2345// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582346void AutomationProvider::GetPluginsInfo(Browser* browser,
2347 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052348 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052349 std::vector<WebPluginInfo> plugins;
2350 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2351 ListValue* items = new ListValue;
2352 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2353 it != plugins.end();
2354 ++it) {
2355 DictionaryValue* item = new DictionaryValue;
[email protected]c9d811372010-06-23 21:44:572356 item->SetStringFromUTF16(L"name", it->name);
[email protected]f7d48012010-05-06 08:17:052357 item->SetString(L"path", it->path.value());
[email protected]c9d811372010-06-23 21:44:572358 item->SetStringFromUTF16(L"version", it->version);
2359 item->SetStringFromUTF16(L"desc", it->desc);
[email protected]f7d48012010-05-06 08:17:052360 item->SetBoolean(L"enabled", it->enabled);
2361 // Add info about mime types.
2362 ListValue* mime_types = new ListValue();
2363 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2364 it->mime_types.begin();
2365 type_it != it->mime_types.end();
2366 ++type_it) {
2367 DictionaryValue* mime_type = new DictionaryValue();
2368 mime_type->SetString(L"mimeType", type_it->mime_type);
[email protected]c9d811372010-06-23 21:44:572369 mime_type->SetStringFromUTF16(L"description", type_it->description);
[email protected]f7d48012010-05-06 08:17:052370
2371 ListValue* file_extensions = new ListValue();
2372 for (std::vector<std::string>::const_iterator ext_it =
2373 type_it->file_extensions.begin();
2374 ext_it != type_it->file_extensions.end();
2375 ++ext_it) {
2376 file_extensions->Append(new StringValue(*ext_it));
2377 }
2378 mime_type->Set(L"fileExtensions", file_extensions);
2379
2380 mime_types->Append(mime_type);
2381 }
2382 item->Set(L"mimeTypes", mime_types);
2383 items->Append(item);
2384 }
2385 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2386 return_value->Set(L"plugins", items); // return_value owns items.
2387
[email protected]7c983cc2010-07-16 11:33:342388 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]f7d48012010-05-06 08:17:052389}
2390
2391// Sample json input:
2392// { "command": "EnablePlugin",
2393// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582394void AutomationProvider::EnablePlugin(Browser* browser,
2395 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052396 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052397 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342398 AutomationJSONReply reply(this, reply_message);
[email protected]f6ff0df2010-07-11 22:41:432399 if (!args->GetString(L"path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342400 reply.SendError("path not specified.");
2401 return;
[email protected]f6ff0df2010-07-11 22:41:432402 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342403 reply.SendError(StringPrintf("Could not enable plugin for path %s.",
2404 path.c_str()));
2405 return;
[email protected]f6ff0df2010-07-11 22:41:432406 }
[email protected]7c983cc2010-07-16 11:33:342407 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052408}
2409
2410// Sample json input:
2411// { "command": "DisablePlugin",
2412// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582413void AutomationProvider::DisablePlugin(Browser* browser,
2414 DictionaryValue* args,
2415 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052416 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342417 AutomationJSONReply reply(this, reply_message);
[email protected]f6ff0df2010-07-11 22:41:432418 if (!args->GetString(L"path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342419 reply.SendError("path not specified.");
2420 return;
[email protected]f6ff0df2010-07-11 22:41:432421 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342422 reply.SendError(StringPrintf("Could not disable plugin for path %s.",
2423 path.c_str()));
2424 return;
[email protected]f6ff0df2010-07-11 22:41:432425 }
[email protected]7c983cc2010-07-16 11:33:342426 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052427}
2428
[email protected]7060bb292010-06-24 00:52:492429// Sample json input:
2430// { "command": "SaveTabContents",
2431// "tab_index": 0,
2432// "filename": <a full pathname> }
2433// Sample json output:
2434// {}
2435void AutomationProvider::SaveTabContents(Browser* browser,
2436 DictionaryValue* args,
2437 IPC::Message* reply_message) {
[email protected]7060bb292010-06-24 00:52:492438 int tab_index = 0;
2439 FilePath::StringType filename;
2440 FilePath::StringType parent_directory;
2441 TabContents* tab_contents = NULL;
2442
2443 if (!args->GetInteger(L"tab_index", &tab_index) ||
2444 !args->GetString(L"filename", &filename)) {
[email protected]26adfd312010-07-20 01:04:462445 AutomationJSONReply(this, reply_message).SendError(
2446 "tab_index or filename param missing");
[email protected]7c983cc2010-07-16 11:33:342447 return;
[email protected]7060bb292010-06-24 00:52:492448 } else {
2449 tab_contents = browser->GetTabContentsAt(tab_index);
2450 if (!tab_contents) {
[email protected]26adfd312010-07-20 01:04:462451 AutomationJSONReply(this, reply_message).SendError(
2452 "no tab at tab_index");
[email protected]7060bb292010-06-24 00:52:492453 return;
2454 }
2455 }
[email protected]7c983cc2010-07-16 11:33:342456 // We're doing a SAVE_AS_ONLY_HTML so the the directory path isn't
2457 // used. Nevertheless, SavePackage requires it be valid. Sigh.
2458 parent_directory = FilePath(filename).DirName().value();
2459 if (!tab_contents->SavePage(FilePath(filename), FilePath(parent_directory),
2460 SavePackage::SAVE_AS_ONLY_HTML)) {
[email protected]26adfd312010-07-20 01:04:462461 AutomationJSONReply(this, reply_message).SendError(
2462 "Could not initiate SavePage");
[email protected]7c983cc2010-07-16 11:33:342463 return;
2464 }
2465 // The observer will delete itself when done.
2466 new SavePackageNotificationObserver(tab_contents->save_package(),
2467 this, reply_message);
[email protected]7060bb292010-06-24 00:52:492468}
2469
[email protected]c5aa5322010-07-15 19:00:072470// Refer to ImportSettings() in chrome/test/pyautolib/pyauto.py for sample
2471// json input.
2472// Sample json output: "{}"
2473void AutomationProvider::ImportSettings(Browser* browser,
2474 DictionaryValue* args,
2475 IPC::Message* reply_message) {
[email protected]c5aa5322010-07-15 19:00:072476 // Map from the json string passed over to the import item masks.
2477 std::map<std::string, ImportItem> string_to_import_item;
2478 string_to_import_item["HISTORY"] = importer::HISTORY;
2479 string_to_import_item["FAVORITES"] = importer::FAVORITES;
2480 string_to_import_item["COOKIES"] = importer::COOKIES;
2481 string_to_import_item["PASSWORDS"] = importer::PASSWORDS;
2482 string_to_import_item["SEARCH_ENGINES"] = importer::SEARCH_ENGINES;
2483 string_to_import_item["HOME_PAGE"] = importer::HOME_PAGE;
2484 string_to_import_item["ALL"] = importer::ALL;
2485
2486 std::wstring browser_name;
2487 int import_items = 0;
2488 ListValue* import_items_list = NULL;
2489 bool first_run;
2490
2491 if (!args->GetString(L"import_from", &browser_name) ||
2492 !args->GetBoolean(L"first_run", &first_run) ||
2493 !args->GetList(L"import_items", &import_items_list)) {
[email protected]7c983cc2010-07-16 11:33:342494 AutomationJSONReply(this, reply_message).SendError(
2495 "Incorrect type for one or more of the arguments.");
[email protected]c5aa5322010-07-15 19:00:072496 return;
2497 }
2498
2499 int num_items = import_items_list->GetSize();
2500 for (int i = 0; i < num_items; i++) {
2501 std::string item;
2502 import_items_list->GetString(i, &item);
2503 // If the provided string is not part of the map, error out.
2504 if (!ContainsKey(string_to_import_item, item)) {
[email protected]7c983cc2010-07-16 11:33:342505 AutomationJSONReply(this, reply_message).SendError(
2506 "Invalid item string found in import_items.");
[email protected]c5aa5322010-07-15 19:00:072507 return;
2508 }
2509 import_items |= string_to_import_item[item];
2510 }
2511
2512 ImporterHost* importer_host = new ImporterHost();
2513 // Get the correct ProfileInfo based on the browser they user provided.
2514 importer::ProfileInfo profile_info;
2515 int num_browsers = importer_host->GetAvailableProfileCount();
2516 int i = 0;
2517 for ( ; i < num_browsers; i++) {
2518 std::wstring name = importer_host->GetSourceProfileNameAt(i);
2519 if (name == browser_name) {
2520 profile_info = importer_host->GetSourceProfileInfoAt(i);
2521 break;
2522 }
2523 }
2524 // If we made it to the end of the loop, then the input was bad.
2525 if (i == num_browsers) {
[email protected]7c983cc2010-07-16 11:33:342526 AutomationJSONReply(this, reply_message).SendError(
2527 "Invalid browser name string found.");
[email protected]c5aa5322010-07-15 19:00:072528 return;
2529 }
2530
2531 Profile* profile = browser->profile();
2532
2533 importer_host->SetObserver(
2534 new AutomationProviderImportSettingsObserver(this, reply_message));
2535 importer_host->StartImportSettings(profile_info, profile, import_items,
2536 new ProfileWriter(profile), first_run);
2537}
2538
[email protected]95222282010-07-26 17:43:022539namespace {
2540
2541// Translates a dictionary password to a PasswordForm struct.
2542webkit_glue::PasswordForm GetPasswordFormFromDict(
2543 const DictionaryValue& password_dict) {
2544
2545 // If the time is specified, change time to the specified time.
2546 base::Time time = base::Time::Now();
2547 int it;
2548 double dt;
2549 if (password_dict.GetInteger(L"time", &it))
2550 time = base::Time::FromTimeT(it);
2551 else if (password_dict.GetReal(L"time", &dt))
2552 time = base::Time::FromDoubleT(dt);
2553
2554 std::string signon_realm;
2555 string16 username_value;
2556 string16 password_value;
2557 string16 origin_url_text;
2558 string16 username_element;
2559 string16 password_element;
2560 string16 submit_element;
2561 string16 action_target_text;
2562 bool blacklist = false;
2563 string16 old_password_element;
2564 string16 old_password_value;
2565
2566 // We don't care if any of these fail - they are either optional or checked
2567 // before this function is called.
2568 password_dict.GetString(L"signon_realm", &signon_realm);
2569 password_dict.GetStringAsUTF16(L"username_value", &username_value);
2570 password_dict.GetStringAsUTF16(L"password_value", &password_value);
2571 password_dict.GetStringAsUTF16(L"origin_url", &origin_url_text);
2572 password_dict.GetStringAsUTF16(L"username_element", &username_element);
2573 password_dict.GetStringAsUTF16(L"password_element", &password_element);
2574 password_dict.GetStringAsUTF16(L"submit_element", &submit_element);
2575 password_dict.GetStringAsUTF16(L"action_target", &action_target_text);
2576 password_dict.GetBoolean(L"blacklist", &blacklist);
2577
2578 GURL origin_gurl(origin_url_text);
2579 GURL action_target(action_target_text);
2580
2581 webkit_glue::PasswordForm password_form;
2582 password_form.signon_realm = signon_realm;
2583 password_form.username_value = username_value;
2584 password_form.password_value = password_value;
2585 password_form.origin = origin_gurl;
2586 password_form.username_element = username_element;
2587 password_form.password_element = password_element;
2588 password_form.submit_element = submit_element;
2589 password_form.action = action_target;
2590 password_form.blacklisted_by_user = blacklist;
2591 password_form.date_created = time;
2592
2593 return password_form;
2594}
2595
2596} // namespace
2597
[email protected]5bcfe1672010-07-16 20:51:572598// See AddSavedPassword() in chrome/test/functional/pyauto.py for sample json
2599// input.
2600// Sample json output: { "password_added": true }
2601void AutomationProvider::AddSavedPassword(Browser* browser,
2602 DictionaryValue* args,
2603 IPC::Message* reply_message) {
[email protected]5bcfe1672010-07-16 20:51:572604 AutomationJSONReply reply(this, reply_message);
[email protected]95222282010-07-26 17:43:022605 DictionaryValue* password_dict = NULL;
[email protected]5bcfe1672010-07-16 20:51:572606
[email protected]95222282010-07-26 17:43:022607 if (!args->GetDictionary(L"password", &password_dict)) {
2608 reply.SendError("Password must be a dictionary.");
[email protected]5bcfe1672010-07-16 20:51:572609 return;
2610 }
2611
[email protected]95222282010-07-26 17:43:022612 // The signon realm is effectively the primary key and must be included.
2613 // Check here before calling GetPasswordFormFromDict.
2614 if (!password_dict->HasKey(L"signon_realm")) {
2615 reply.SendError("Password must include signon_realm.");
2616 return;
2617 }
2618 webkit_glue::PasswordForm new_password =
2619 GetPasswordFormFromDict(*password_dict);
[email protected]5bcfe1672010-07-16 20:51:572620
2621 Profile* profile = browser->profile();
2622 // Use IMPLICIT_ACCESS since new passwords aren't added off the record.
2623 PasswordStore* password_store =
2624 profile->GetPasswordStore(Profile::IMPLICIT_ACCESS);
2625
2626 // Set the return based on whether setting the password succeeded.
2627 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2628
2629 // It will be null if it's accessed in an incognito window.
2630 if (password_store != NULL) {
2631 password_store->AddLogin(new_password);
2632 return_value->SetBoolean(L"password_added", true);
2633 } else {
2634 return_value->SetBoolean(L"password_added", false);
2635 }
2636
2637 reply.SendSuccess(return_value.get());
2638}
2639
[email protected]95222282010-07-26 17:43:022640// See RemoveSavedPassword() in chrome/test/functional/pyauto.py for sample
2641// json input.
2642// Sample json output: {}
2643void AutomationProvider::RemoveSavedPassword(Browser* browser,
2644 DictionaryValue* args,
2645 IPC::Message* reply_message) {
2646 AutomationJSONReply reply(this, reply_message);
2647 DictionaryValue* password_dict = NULL;
2648
2649 if (!args->GetDictionary(L"password", &password_dict)) {
2650 reply.SendError("Password must be a dictionary.");
2651 return;
2652 }
2653
2654 // The signon realm is effectively the primary key and must be included.
2655 // Check here before calling GetPasswordFormFromDict.
2656 if (!password_dict->HasKey(L"signon_realm")) {
2657 reply.SendError("Password must include signon_realm.");
2658 return;
2659 }
2660 webkit_glue::PasswordForm to_remove =
2661 GetPasswordFormFromDict(*password_dict);
2662
2663 Profile* profile = browser->profile();
2664 // Use EXPLICIT_ACCESS since passwords can be removed off the record.
2665 PasswordStore* password_store =
2666 profile->GetPasswordStore(Profile::EXPLICIT_ACCESS);
2667
2668 password_store->RemoveLogin(to_remove);
2669 reply.SendSuccess(NULL);
2670}
2671
[email protected]5bcfe1672010-07-16 20:51:572672// Sample json input: { "command": "GetSavedPasswords" }
2673// Refer to GetSavedPasswords() in chrome/test/pyautolib/pyauto.py for sample
2674// json output.
2675void AutomationProvider::GetSavedPasswords(Browser* browser,
2676 DictionaryValue* args,
2677 IPC::Message* reply_message) {
2678 Profile* profile = browser->profile();
2679 // Use EXPLICIT_ACCESS since saved passwords can be retreived off the record.
2680 PasswordStore* password_store =
2681 profile->GetPasswordStore(Profile::EXPLICIT_ACCESS);
2682 password_store->GetAutofillableLogins(
2683 new AutomationProviderGetPasswordsObserver(this, reply_message));
2684 // Observer deletes itself after returning.
2685}
2686
[email protected]a0fc50d72010-07-14 21:14:192687// Refer to ClearBrowsingData() in chrome/test/pyautolib/pyauto.py for sample
2688// json input.
2689// Sample json output: {}
2690void AutomationProvider::ClearBrowsingData(Browser* browser,
2691 DictionaryValue* args,
2692 IPC::Message* reply_message) {
[email protected]a0fc50d72010-07-14 21:14:192693 std::map<std::string, BrowsingDataRemover::TimePeriod> string_to_time_period;
2694 string_to_time_period["LAST_HOUR"] = BrowsingDataRemover::LAST_HOUR;
2695 string_to_time_period["LAST_DAY"] = BrowsingDataRemover::LAST_DAY;
2696 string_to_time_period["LAST_WEEK"] = BrowsingDataRemover::LAST_WEEK;
2697 string_to_time_period["FOUR_WEEKS"] = BrowsingDataRemover::FOUR_WEEKS;
2698 string_to_time_period["EVERYTHING"] = BrowsingDataRemover::EVERYTHING;
2699
2700 std::map<std::string, int> string_to_mask_value;
2701 string_to_mask_value["HISTORY"] = BrowsingDataRemover::REMOVE_HISTORY;
2702 string_to_mask_value["DOWNLOADS"] = BrowsingDataRemover::REMOVE_DOWNLOADS;
2703 string_to_mask_value["COOKIES"] = BrowsingDataRemover::REMOVE_COOKIES;
2704 string_to_mask_value["PASSWORDS"] = BrowsingDataRemover::REMOVE_PASSWORDS;
2705 string_to_mask_value["FORM_DATA"] = BrowsingDataRemover::REMOVE_FORM_DATA;
2706 string_to_mask_value["CACHE"] = BrowsingDataRemover::REMOVE_CACHE;
2707
2708 std::string time_period;
2709 ListValue* to_remove;
2710 if (!args->GetString(L"time_period", &time_period) ||
2711 !args->GetList(L"to_remove", &to_remove)) {
[email protected]7c983cc2010-07-16 11:33:342712 AutomationJSONReply(this, reply_message).SendError(
2713 "time_period must be a string and to_remove a list.");
[email protected]a0fc50d72010-07-14 21:14:192714 return;
2715 }
2716
2717 int remove_mask = 0;
2718 int num_removals = to_remove->GetSize();
2719 for (int i = 0; i < num_removals; i++) {
2720 std::string removal;
2721 to_remove->GetString(i, &removal);
2722 // If the provided string is not part of the map, then error out.
2723 if (!ContainsKey(string_to_mask_value, removal)) {
[email protected]7c983cc2010-07-16 11:33:342724 AutomationJSONReply(this, reply_message).SendError(
2725 "Invalid browsing data string found in to_remove.");
[email protected]a0fc50d72010-07-14 21:14:192726 return;
2727 }
2728 remove_mask |= string_to_mask_value[removal];
2729 }
2730
2731 if (!ContainsKey(string_to_time_period, time_period)) {
[email protected]7c983cc2010-07-16 11:33:342732 AutomationJSONReply(this, reply_message).SendError(
2733 "Invalid string for time_period.");
[email protected]a0fc50d72010-07-14 21:14:192734 return;
2735 }
2736
2737 BrowsingDataRemover* remover = new BrowsingDataRemover(
2738 profile(), string_to_time_period[time_period], base::Time());
2739
2740 remover->AddObserver(
2741 new AutomationProviderBrowsingDataObserver(this, reply_message));
2742 remover->Remove(remove_mask);
2743 // BrowsingDataRemover deletes itself using DeleteTask.
2744 // The observer also deletes itself after sending the reply.
2745}
2746
[email protected]3dda5b02010-07-27 16:35:422747namespace {
2748
2749 // Get the TabContents from a dictionary of arguments.
2750 TabContents* GetTabContentsFromDict(const Browser* browser,
2751 const DictionaryValue* args,
2752 std::string* error_message) {
2753 int tab_index;
2754 if (!args->GetInteger(L"tab_index", &tab_index)) {
2755 *error_message = "Must include tab_index.";
2756 return NULL;
2757 }
2758
2759 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2760 if (!tab_contents) {
2761 *error_message = StringPrintf("No tab at index %d.", tab_index);
2762 return NULL;
2763 }
2764 return tab_contents;
2765 }
2766
2767 // Get the TranslateInfoBarDelegate from TabContents.
2768 TranslateInfoBarDelegate* GetTranslateInfoBarDelegate(
2769 TabContents* tab_contents) {
2770 for (int i = 0; i < tab_contents->infobar_delegate_count(); i++) {
2771 InfoBarDelegate* infobar = tab_contents->GetInfoBarDelegateAt(i);
2772 if (infobar->AsTranslateInfoBarDelegate())
2773 return infobar->AsTranslateInfoBarDelegate();
2774 }
2775 // No translate infobar.
2776 return NULL;
2777 }
2778
2779} // namespace
2780
2781// See GetTranslateInfo() in chrome/test/pyautolib/pyauto.py for sample json
2782// input and output.
2783void AutomationProvider::GetTranslateInfo(Browser* browser,
2784 DictionaryValue* args,
2785 IPC::Message* reply_message) {
2786 std::string error_message;
2787 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2788 &error_message);
2789 if (!tab_contents) {
2790 AutomationJSONReply(this, reply_message).SendError(error_message);
2791 return;
2792 }
2793
2794 // Get the translate bar if there is one and pass it to the observer.
2795 // The observer will check for null and populate the information accordingly.
2796 TranslateInfoBarDelegate* translate_bar =
2797 GetTranslateInfoBarDelegate(tab_contents);
2798
2799 TabLanguageDeterminedObserver* observer = new TabLanguageDeterminedObserver(
2800 this, reply_message, tab_contents, translate_bar);
2801 // If the language for the page hasn't been loaded yet, then just make
2802 // the observer, otherwise call observe directly.
2803 std::string language = tab_contents->language_state().original_language();
2804 if (!language.empty()) {
2805 observer->Observe(NotificationType::TAB_LANGUAGE_DETERMINED,
2806 Source<TabContents>(tab_contents),
2807 Details<std::string>(&language));
2808 }
2809}
2810
2811// See SelectTranslateOption() in chrome/test/pyautolib/pyauto.py for sample
2812// json input.
2813// Sample json output: {}
2814void AutomationProvider::SelectTranslateOption(Browser* browser,
2815 DictionaryValue* args,
2816 IPC::Message* reply_message) {
2817 std::string option;
2818 std::string error_message;
2819 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2820 &error_message);
2821 if (!tab_contents) {
2822 AutomationJSONReply(this, reply_message).SendError(error_message);
2823 return;
2824 }
2825
2826 TranslateInfoBarDelegate* translate_bar =
2827 GetTranslateInfoBarDelegate(tab_contents);
2828 if (!translate_bar) {
2829 AutomationJSONReply(this, reply_message)
2830 .SendError("There is no translate bar open.");
2831 return;
2832 }
2833
2834 if (!args->GetString(L"option", &option)) {
2835 AutomationJSONReply(this, reply_message).SendError("Must include option");
2836 return;
2837 }
2838
2839 if (option == "translate_page") {
2840 // Make a new notification observer which will send the reply.
2841 new PageTranslatedObserver(this, reply_message, tab_contents);
2842 translate_bar->Translate();
2843 return;
2844 }
2845
2846 AutomationJSONReply reply(this, reply_message);
2847 if (option == "never_translate_language") {
2848 if (translate_bar->IsLanguageBlacklisted()) {
2849 reply.SendError("The language was already blacklisted.");
2850 return;
2851 }
2852 translate_bar->ToggleLanguageBlacklist();
2853 reply.SendSuccess(NULL);
2854 } else if (option == "never_translate_site") {
2855 if (translate_bar->IsSiteBlacklisted()) {
2856 reply.SendError("The site was already blacklisted.");
2857 return;
2858 }
2859 translate_bar->ToggleSiteBlacklist();
2860 reply.SendSuccess(NULL);
2861 } else if (option == "toggle_always_translate") {
2862 translate_bar->ToggleAlwaysTranslate();
2863 reply.SendSuccess(NULL);
2864 } else if (option == "revert_translation") {
2865 translate_bar->RevertTranslation();
2866 reply.SendSuccess(NULL);
2867 } else {
2868 reply.SendError("Invalid string found for option.");
2869 }
2870}
2871
[email protected]4d1929f12010-07-10 00:09:032872// Sample json input: { "command": "GetThemeInfo" }
2873// Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output.
2874void AutomationProvider::GetThemeInfo(Browser* browser,
2875 DictionaryValue* args,
2876 IPC::Message* reply_message) {
2877 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2878 Extension* theme = browser->profile()->GetTheme();
2879 if (theme) {
2880 return_value->SetString(L"name", theme->name());
2881 return_value->Set(L"images", theme->GetThemeImages()->DeepCopy());
2882 return_value->Set(L"colors", theme->GetThemeColors()->DeepCopy());
2883 return_value->Set(L"tints", theme->GetThemeTints()->DeepCopy());
2884 }
[email protected]7c983cc2010-07-16 11:33:342885 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]4d1929f12010-07-10 00:09:032886}
2887
[email protected]55846ad842010-07-09 18:22:562888// Sample json input:
2889// { "command": "GetAutoFillProfile" }
2890// Refer to GetAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2891// json output.
2892void AutomationProvider::GetAutoFillProfile(Browser* browser,
2893 DictionaryValue* args,
2894 IPC::Message* reply_message) {
[email protected]55846ad842010-07-09 18:22:562895 // Get the AutoFillProfiles currently in the database.
2896 int tab_index = 0;
2897 args->GetInteger(L"tab_index", &tab_index);
2898 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
[email protected]7c983cc2010-07-16 11:33:342899 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:562900
2901 if (tab_contents) {
2902 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2903 ->GetPersonalDataManager();
2904 if (pdm) {
2905 std::vector<AutoFillProfile*> autofill_profiles = pdm->profiles();
2906 std::vector<CreditCard*> credit_cards = pdm->credit_cards();
2907
2908 ListValue* profiles = GetListFromAutoFillProfiles(autofill_profiles);
2909 ListValue* cards = GetListFromCreditCards(credit_cards);
2910
2911 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2912
2913 return_value->Set(L"profiles", profiles);
2914 return_value->Set(L"credit_cards", cards);
[email protected]7c983cc2010-07-16 11:33:342915 reply.SendSuccess(return_value.get());
[email protected]55846ad842010-07-09 18:22:562916 } else {
[email protected]7c983cc2010-07-16 11:33:342917 reply.SendError("No PersonalDataManager.");
2918 return;
[email protected]55846ad842010-07-09 18:22:562919 }
2920 } else {
[email protected]7c983cc2010-07-16 11:33:342921 reply.SendError("No tab at that index.");
2922 return;
[email protected]55846ad842010-07-09 18:22:562923 }
[email protected]55846ad842010-07-09 18:22:562924}
2925
2926// Refer to FillAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2927// json input.
2928// Sample json output: {}
2929void AutomationProvider::FillAutoFillProfile(Browser* browser,
2930 DictionaryValue* args,
2931 IPC::Message* reply_message) {
[email protected]7c983cc2010-07-16 11:33:342932 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:562933 ListValue* profiles = NULL;
2934 ListValue* cards = NULL;
2935 args->GetList(L"profiles", &profiles);
2936 args->GetList(L"credit_cards", &cards);
[email protected]7c983cc2010-07-16 11:33:342937 std::string error_mesg;
[email protected]55846ad842010-07-09 18:22:562938
2939 std::vector<AutoFillProfile> autofill_profiles;
2940 std::vector<CreditCard> credit_cards;
2941 // Create an AutoFillProfile for each of the dictionary profiles.
2942 if (profiles) {
[email protected]7c983cc2010-07-16 11:33:342943 autofill_profiles = GetAutoFillProfilesFromList(*profiles, &error_mesg);
[email protected]55846ad842010-07-09 18:22:562944 }
2945 // Create a CreditCard for each of the dictionary values.
2946 if (cards) {
[email protected]7c983cc2010-07-16 11:33:342947 credit_cards = GetCreditCardsFromList(*cards, &error_mesg);
2948 }
2949 if (!error_mesg.empty()) {
2950 reply.SendError(error_mesg);
2951 return;
[email protected]55846ad842010-07-09 18:22:562952 }
2953
2954 // Save the AutoFillProfiles.
2955 int tab_index = 0;
2956 args->GetInteger(L"tab_index", &tab_index);
2957 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2958
2959 if (tab_contents) {
[email protected]ce6b0122010-07-19 19:07:302960 PersonalDataManager* pdm = tab_contents->profile()
[email protected]55846ad842010-07-09 18:22:562961 ->GetPersonalDataManager();
2962 if (pdm) {
2963 pdm->OnAutoFillDialogApply(profiles? &autofill_profiles : NULL,
2964 cards? &credit_cards : NULL);
2965 } else {
[email protected]7c983cc2010-07-16 11:33:342966 reply.SendError("No PersonalDataManager.");
2967 return;
[email protected]55846ad842010-07-09 18:22:562968 }
2969 } else {
[email protected]7c983cc2010-07-16 11:33:342970 reply.SendError("No tab at that index.");
2971 return;
[email protected]55846ad842010-07-09 18:22:562972 }
[email protected]7c983cc2010-07-16 11:33:342973 reply.SendSuccess(NULL);
[email protected]55846ad842010-07-09 18:22:562974}
2975
2976/* static */
2977ListValue* AutomationProvider::GetListFromAutoFillProfiles(
2978 std::vector<AutoFillProfile*> autofill_profiles) {
2979 ListValue* profiles = new ListValue;
2980
2981 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string
2982 = GetAutoFillFieldToStringMap();
2983
2984 // For each AutoFillProfile, transform it to a dictionary object to return.
2985 for (std::vector<AutoFillProfile*>::iterator it = autofill_profiles.begin();
2986 it != autofill_profiles.end(); ++it) {
2987 AutoFillProfile* profile = *it;
2988 DictionaryValue* profile_info = new DictionaryValue;
2989 profile_info->SetStringFromUTF16(L"label", profile->Label());
2990 // For each of the types, if it has a value, add it to the dictionary.
2991 for (std::map<AutoFillFieldType, std::wstring>::iterator
2992 type_it = autofill_type_to_string.begin();
2993 type_it != autofill_type_to_string.end(); ++type_it) {
2994 string16 value = profile->GetFieldText(AutoFillType(type_it->first));
2995 if (value.length()) { // If there was something stored for that value.
2996 profile_info->SetStringFromUTF16(type_it->second, value);
2997 }
2998 }
2999 profiles->Append(profile_info);
3000 }
3001 return profiles;
3002}
3003
3004/* static */
3005ListValue* AutomationProvider::GetListFromCreditCards(
3006 std::vector<CreditCard*> credit_cards) {
3007 ListValue* cards = new ListValue;
3008
3009 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
3010 GetCreditCardFieldToStringMap();
3011
3012 // For each AutoFillProfile, transform it to a dictionary object to return.
3013 for (std::vector<CreditCard*>::iterator it = credit_cards.begin();
3014 it != credit_cards.end(); ++it) {
3015 CreditCard* card = *it;
3016 DictionaryValue* card_info = new DictionaryValue;
3017 card_info->SetStringFromUTF16(L"label", card->Label());
3018 // For each of the types, if it has a value, add it to the dictionary.
3019 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
3020 credit_card_type_to_string.begin();
3021 type_it != credit_card_type_to_string.end(); ++type_it) {
3022 string16 value = card->GetFieldText(AutoFillType(type_it->first));
3023 // If there was something stored for that value.
3024 if (value.length()) {
3025 card_info->SetStringFromUTF16(type_it->second, value);
3026 }
3027 }
3028 cards->Append(card_info);
3029 }
3030 return cards;
3031}
3032
3033/* static */
3034std::vector<AutoFillProfile> AutomationProvider::GetAutoFillProfilesFromList(
[email protected]7c983cc2010-07-16 11:33:343035 const ListValue& profiles, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:563036 std::vector<AutoFillProfile> autofill_profiles;
3037 DictionaryValue* profile_info = NULL;
3038 string16 profile_label;
3039 string16 current_value;
3040
3041 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string =
3042 GetAutoFillFieldToStringMap();
3043
3044 int num_profiles = profiles.GetSize();
3045 for (int i = 0; i < num_profiles; i++) {
3046 profiles.GetDictionary(i, &profile_info);
3047 profile_info->GetString("label", &profile_label);
3048 // Choose an id of 0 so that a unique id will be created.
3049 AutoFillProfile profile(profile_label, 0);
3050 // Loop through the possible profile types and add those provided.
3051 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
3052 autofill_type_to_string.begin();
3053 type_it != autofill_type_to_string.end(); ++type_it) {
3054 if (profile_info->HasKey(type_it->second)) {
3055 if (profile_info->GetStringAsUTF16(type_it->second, &current_value)) {
3056 profile.SetInfo(AutoFillType(type_it->first), current_value);
3057 } else {
[email protected]7c983cc2010-07-16 11:33:343058 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:563059 break;
3060 }
3061 }
3062 }
3063 autofill_profiles.push_back(profile);
3064 }
3065 return autofill_profiles;
3066}
3067
3068/* static */
3069std::vector<CreditCard> AutomationProvider::GetCreditCardsFromList(
[email protected]7c983cc2010-07-16 11:33:343070 const ListValue& cards, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:563071 std::vector<CreditCard> credit_cards;
3072 DictionaryValue* card_info = NULL;
3073 string16 card_label;
3074 string16 current_value;
3075
3076 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
3077 GetCreditCardFieldToStringMap();
3078
3079 int num_credit_cards = cards.GetSize();
3080 for (int i = 0; i < num_credit_cards; i++) {
3081 cards.GetDictionary(i, &card_info);
3082 card_info->GetString("label", &card_label);
3083 CreditCard card(card_label, 0);
3084 // Loop through the possible credit card fields and add those provided.
3085 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
3086 credit_card_type_to_string.begin();
3087 type_it != credit_card_type_to_string.end(); ++type_it) {
3088 if (card_info->HasKey(type_it->second)) {
3089 if (card_info->GetStringAsUTF16(type_it->second, &current_value)) {
3090 card.SetInfo(AutoFillType(type_it->first), current_value);
3091 } else {
[email protected]7c983cc2010-07-16 11:33:343092 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:563093 break;
3094 }
3095 }
3096 }
3097 credit_cards.push_back(card);
3098 }
3099 return credit_cards;
3100}
3101
3102/* static */
3103std::map<AutoFillFieldType, std::wstring>
3104 AutomationProvider::GetAutoFillFieldToStringMap() {
3105 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string;
3106 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
3107 autofill_type_to_string[NAME_MIDDLE] = L"NAME_MIDDLE";
3108 autofill_type_to_string[NAME_LAST] = L"NAME_LAST";
3109 autofill_type_to_string[COMPANY_NAME] = L"COMPANY_NAME";
3110 autofill_type_to_string[EMAIL_ADDRESS] = L"EMAIL_ADDRESS";
3111 autofill_type_to_string[ADDRESS_HOME_LINE1] = L"ADDRESS_HOME_LINE1";
3112 autofill_type_to_string[ADDRESS_HOME_LINE2] = L"ADDRESS_HOME_LINE2";
3113 autofill_type_to_string[ADDRESS_HOME_CITY] = L"ADDRESS_HOME_CITY";
3114 autofill_type_to_string[ADDRESS_HOME_STATE] = L"ADDRESS_HOME_STATE";
3115 autofill_type_to_string[ADDRESS_HOME_ZIP] = L"ADDRESS_HOME_ZIP";
3116 autofill_type_to_string[ADDRESS_HOME_COUNTRY] = L"ADDRESS_HOME_COUNTRY";
3117 autofill_type_to_string[PHONE_HOME_NUMBER] = L"PHONE_HOME_NUMBER";
3118 autofill_type_to_string[PHONE_FAX_NUMBER] = L"PHONE_FAX_NUMBER";
3119 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
3120 return autofill_type_to_string;
3121}
3122
3123/* static */
3124std::map<AutoFillFieldType, std::wstring>
3125 AutomationProvider::GetCreditCardFieldToStringMap() {
3126 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string;
3127 credit_card_type_to_string[CREDIT_CARD_NAME] = L"CREDIT_CARD_NAME";
3128 credit_card_type_to_string[CREDIT_CARD_NUMBER] = L"CREDIT_CARD_NUMBER";
3129 credit_card_type_to_string[CREDIT_CARD_TYPE] = L"CREDIT_CARD_TYPE";
3130 credit_card_type_to_string[CREDIT_CARD_EXP_MONTH] = L"CREDIT_CARD_EXP_MONTH";
3131 credit_card_type_to_string[CREDIT_CARD_EXP_4_DIGIT_YEAR] =
3132 L"CREDIT_CARD_EXP_4_DIGIT_YEAR";
3133 return credit_card_type_to_string;
3134}
3135
[email protected]53329582010-05-14 21:10:583136void AutomationProvider::SendJSONRequest(int handle,
3137 std::string json_request,
3138 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:043139 Browser* browser = NULL;
[email protected]59a611242010-04-02 02:24:043140 scoped_ptr<Value> values;
3141
3142 // Basic error checking.
3143 if (browser_tracker_->ContainsHandle(handle)) {
3144 browser = browser_tracker_->GetResource(handle);
3145 }
3146 if (!browser) {
[email protected]7c983cc2010-07-16 11:33:343147 AutomationJSONReply(this, reply_message).SendError("no browser object");
3148 return;
3149 }
3150 base::JSONReader reader;
3151 std::string error;
3152 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
3153 if (!error.empty()) {
3154 AutomationJSONReply(this, reply_message).SendError(error);
3155 return;
[email protected]59a611242010-04-02 02:24:043156 }
3157
3158 // Make sure input is a dict with a string command.
3159 std::string command;
3160 DictionaryValue* dict_value = NULL;
[email protected]7c983cc2010-07-16 11:33:343161 if (values->GetType() != Value::TYPE_DICTIONARY) {
3162 AutomationJSONReply(this, reply_message).SendError("not a dict");
3163 return;
3164 }
3165 // Ownership remains with "values" variable.
3166 dict_value = static_cast<DictionaryValue*>(values.get());
3167 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
3168 AutomationJSONReply(this, reply_message).SendError(
3169 "no command key in dict or not a string command");
3170 return;
[email protected]59a611242010-04-02 02:24:043171 }
3172
[email protected]24e2b102010-04-29 17:56:473173 // Map json commands to their handlers.
3174 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:053175 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
3176 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
3177 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
3178
[email protected]a9ff2c02010-05-13 17:33:053179 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
[email protected]7c983cc2010-07-16 11:33:343180
[email protected]38b5a3852010-07-21 06:49:523181 handler_map["WaitForInfobarCount"] = &AutomationProvider::WaitForInfobarCount;
[email protected]e004a2d2010-07-22 04:55:283182 handler_map["PerformActionOnInfobar"] =
3183 &AutomationProvider::PerformActionOnInfobar;
[email protected]38b5a3852010-07-21 06:49:523184
[email protected]24e2b102010-04-29 17:56:473185 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:293186 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:053187
[email protected]53329582010-05-14 21:10:583188 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
3189 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
3190 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
3191 handler_map["OmniboxMovePopupSelection"] =
3192 &AutomationProvider::OmniboxMovePopupSelection;
3193
[email protected]24e2b102010-04-29 17:56:473194 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
3195 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:053196
[email protected]ef413ca2010-05-25 21:09:143197 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
3198
[email protected]f7d48012010-05-06 08:17:053199 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:473200 handler_map["WaitForAllDownloadsToComplete"] =
3201 &AutomationProvider::WaitForDownloadsToComplete;
3202
[email protected]a3cd5022010-06-16 18:25:293203 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
3204
[email protected]7060bb292010-06-24 00:52:493205 handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents;
3206
[email protected]c5aa5322010-07-15 19:00:073207 handler_map["ImportSettings"] = &AutomationProvider::ImportSettings;
3208
[email protected]5bcfe1672010-07-16 20:51:573209 handler_map["AddSavedPassword"] = &AutomationProvider::AddSavedPassword;
[email protected]95222282010-07-26 17:43:023210 handler_map["RemoveSavedPassword"] =
3211 &AutomationProvider::RemoveSavedPassword;
[email protected]5bcfe1672010-07-16 20:51:573212 handler_map["GetSavedPasswords"] = &AutomationProvider::GetSavedPasswords;
3213
[email protected]a0fc50d72010-07-14 21:14:193214 handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData;
3215
[email protected]4d1929f12010-07-10 00:09:033216 // SetTheme() implemented using InstallExtension().
3217 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo;
3218
[email protected]3dda5b02010-07-27 16:35:423219 handler_map["SelectTranslateOption"] =
3220 &AutomationProvider::SelectTranslateOption;
3221 handler_map["GetTranslateInfo"] = &AutomationProvider::GetTranslateInfo;
3222
[email protected]55846ad842010-07-09 18:22:563223 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile;
3224 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile;
3225
[email protected]7c983cc2010-07-16 11:33:343226 if (handler_map.find(std::string(command)) != handler_map.end()) {
3227 (this->*handler_map[command])(browser, dict_value, reply_message);
[email protected]1ac875d22010-07-16 09:57:583228 } else {
[email protected]7c983cc2010-07-16 11:33:343229 std::string error_string = "Unknown command. Options: ";
3230 for (std::map<std::string, JsonHandler>::const_iterator it =
3231 handler_map.begin(); it != handler_map.end(); ++it) {
3232 error_string += it->first + ", ";
3233 }
3234 AutomationJSONReply(this, reply_message).SendError(error_string);
[email protected]1ac875d22010-07-16 09:57:583235 }
[email protected]59a611242010-04-02 02:24:043236}
3237
initial.commit09911bf2008-07-26 23:55:293238void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:563239 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:343240 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
3241 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:563242 DCHECK(reply_message_ == NULL);
3243 reply_message_ = reply_message;
3244
[email protected]d9f9b792009-06-24 13:17:123245 DevToolsManager::GetInstance()->InspectElement(
3246 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:293247 } else {
[email protected]71f65dd2009-02-11 19:14:563248 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
3249 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293250 }
3251}
3252
3253void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:093254 if (reply_message_) {
3255 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
3256 num_resources);
3257 Send(reply_message_);
3258 reply_message_ = NULL;
3259 }
initial.commit09911bf2008-07-26 23:55:293260}
3261
[email protected]a7eee32f2009-05-22 18:08:173262class SetProxyConfigTask : public Task {
3263 public:
[email protected]be180c802009-10-23 06:33:313264 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
3265 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:253266 : request_context_getter_(request_context_getter),
3267 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:173268 virtual void Run() {
3269 // First, deserialize the JSON string. If this fails, log and bail.
3270 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:393271 std::string error_msg;
3272 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:173273 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
3274 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:393275 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:173276 return;
3277 }
3278
3279 scoped_ptr<DictionaryValue> dict(
3280 static_cast<DictionaryValue*>(root.release()));
3281 // Now put together a proxy configuration from the deserialized string.
3282 net::ProxyConfig pc;
3283 PopulateProxyConfig(*dict.get(), &pc);
3284
[email protected]be180c802009-10-23 06:33:313285 net::ProxyService* proxy_service =
3286 request_context_getter_->GetURLRequestContext()->proxy_service();
3287 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:173288 scoped_ptr<net::ProxyConfigService> proxy_config_service(
3289 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:313290 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:173291 }
3292
3293 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
3294 DCHECK(pc);
3295 bool no_proxy = false;
3296 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
3297 // Make no changes to the ProxyConfig.
3298 return;
3299 }
3300 bool auto_config;
3301 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:483302 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:173303 }
3304 std::string pac_url;
3305 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:483306 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:173307 }
3308 std::string proxy_bypass_list;
3309 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:483310 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:173311 }
3312 std::string proxy_server;
3313 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:483314 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:173315 }
3316 }
3317
3318 private:
[email protected]be180c802009-10-23 06:33:313319 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:173320 std::string proxy_config_;
3321};
3322
3323
3324void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:313325 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
3326 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:173327 FilePath user_data_dir;
3328 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
3329 ProfileManager* profile_manager = g_browser_process->profile_manager();
3330 DCHECK(profile_manager);
3331 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
3332 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:313333 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:173334 }
[email protected]be180c802009-10-23 06:33:313335 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:173336
[email protected]fae20792009-10-28 20:31:583337 ChromeThread::PostTask(
3338 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:313339 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:173340}
3341
[email protected]4f3dc372009-02-24 00:10:293342void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:333343 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:293344 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:293345 if (tab_tracker_->ContainsHandle(handle)) {
3346 NavigationController* tab = tab_tracker_->GetResource(handle);
3347 DownloadManager* dlm = tab->profile()->GetDownloadManager();
3348 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:333349 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:293350 }
initial.commit09911bf2008-07-26 23:55:293351}
3352
[email protected]6a5670d22009-10-27 16:21:343353void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:143354 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:533355 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
3356 reply_message);
3357}
3358
3359void AutomationProvider::OpenNewBrowserWindowOfType(
3360 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:143361 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:293362 // We may have no current browser windows open so don't rely on
3363 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:533364 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
3365 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:053366 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:153367 if (show)
[email protected]15952e462008-11-14 00:29:053368 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:293369}
3370
[email protected]71f65dd2009-02-11 19:14:563371void AutomationProvider::GetWindowForBrowser(int browser_handle,
3372 bool* success,
3373 int* handle) {
3374 *success = false;
3375 *handle = 0;
initial.commit09911bf2008-07-26 23:55:293376
3377 if (browser_tracker_->ContainsHandle(browser_handle)) {
3378 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:203379 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:293380 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:203381 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:563382 *success = true;
initial.commit09911bf2008-07-26 23:55:293383 }
initial.commit09911bf2008-07-26 23:55:293384}
3385
3386void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:563387 int browser_handle,
3388 bool* success,
3389 int* autocomplete_edit_handle) {
3390 *success = false;
3391 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:293392
3393 if (browser_tracker_->ContainsHandle(browser_handle)) {
3394 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:063395 LocationBar* loc_bar = browser->window()->GetLocationBar();
3396 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:293397 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:563398 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
3399 *success = true;
initial.commit09911bf2008-07-26 23:55:293400 }
initial.commit09911bf2008-07-26 23:55:293401}
initial.commit09911bf2008-07-26 23:55:293402
[email protected]71f65dd2009-02-11 19:14:563403void AutomationProvider::ShowInterstitialPage(int tab_handle,
3404 const std::string& html_text,
3405 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293406 if (tab_tracker_->ContainsHandle(tab_handle)) {
3407 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:113408 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:403409
[email protected]7dad3d5f2010-03-04 00:27:013410 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:403411 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:343412 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:403413 GURL("about:interstitial"),
3414 html_text);
3415 interstitial->Show();
3416 return;
initial.commit09911bf2008-07-26 23:55:293417 }
[email protected]71f65dd2009-02-11 19:14:563418
[email protected]457f5cf2009-08-18 16:37:523419 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
3420 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563421 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293422}
3423
[email protected]71f65dd2009-02-11 19:14:563424void AutomationProvider::HideInterstitialPage(int tab_handle,
3425 bool* success) {
3426 *success = false;
[email protected]57c6a652009-05-04 07:58:343427 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
3428 if (tab_contents && tab_contents->interstitial_page()) {
3429 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:563430 *success = true;
initial.commit09911bf2008-07-26 23:55:293431 }
initial.commit09911bf2008-07-26 23:55:293432}
3433
[email protected]71f65dd2009-02-11 19:14:563434void AutomationProvider::CloseTab(int tab_handle,
3435 bool wait_until_closed,
3436 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293437 if (tab_tracker_->ContainsHandle(tab_handle)) {
3438 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
3439 int index;
3440 Browser* browser = Browser::GetBrowserForController(controller, &index);
3441 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:143442 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:113443 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:453444 return;
initial.commit09911bf2008-07-26 23:55:293445 }
[email protected]de246f52009-02-25 18:25:453446
3447 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:553448 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293449}
3450
[email protected]71f65dd2009-02-11 19:14:563451void AutomationProvider::CloseBrowser(int browser_handle,
3452 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293453 if (browser_tracker_->ContainsHandle(browser_handle)) {
3454 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563455 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:563456 reply_message);
[email protected]f3e99e32008-07-30 04:48:393457 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:293458 } else {
3459 NOTREACHED();
3460 }
3461}
3462
[email protected]71f65dd2009-02-11 19:14:563463void AutomationProvider::CloseBrowserAsync(int browser_handle) {
3464 if (browser_tracker_->ContainsHandle(browser_handle)) {
3465 Browser* browser = browser_tracker_->GetResource(browser_handle);
3466 browser->window()->Close();
3467 } else {
3468 NOTREACHED();
3469 }
3470}
3471
[email protected]71f65dd2009-02-11 19:14:563472void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
3473 IPC::Message* reply_message) {
3474 if (tab_tracker_->ContainsHandle(tab_handle)) {
3475 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3476 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:143477 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:563478 }
3479}
3480
[email protected]71f65dd2009-02-11 19:14:563481void AutomationProvider::GetSecurityState(int handle, bool* success,
3482 SecurityStyle* security_style,
3483 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:483484 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:293485 if (tab_tracker_->ContainsHandle(handle)) {
3486 NavigationController* tab = tab_tracker_->GetResource(handle);
3487 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563488 *success = true;
3489 *security_style = entry->ssl().security_style();
3490 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:483491 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:293492 } else {
[email protected]71f65dd2009-02-11 19:14:563493 *success = false;
3494 *security_style = SECURITY_STYLE_UNKNOWN;
3495 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:483496 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:293497 }
3498}
3499
[email protected]71f65dd2009-02-11 19:14:563500void AutomationProvider::GetPageType(int handle, bool* success,
3501 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:293502 if (tab_tracker_->ContainsHandle(handle)) {
3503 NavigationController* tab = tab_tracker_->GetResource(handle);
3504 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563505 *page_type = entry->page_type();
3506 *success = true;
initial.commit09911bf2008-07-26 23:55:293507 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:343508 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:563509 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:343510 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:563511 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293512 } else {
[email protected]71f65dd2009-02-11 19:14:563513 *success = false;
3514 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293515 }
3516}
3517
[email protected]84abba62009-10-07 17:01:443518void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
3519 int* duration_ms) {
3520 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
3521 event_name);
3522}
3523
[email protected]71f65dd2009-02-11 19:14:563524void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
3525 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293526 if (tab_tracker_->ContainsHandle(handle)) {
3527 NavigationController* tab = tab_tracker_->GetResource(handle);
3528 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:073529 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:403530 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:473531 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:343532 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:293533 if (ssl_blocking_page) {
3534 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:013535 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:563536 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:293537 return;
3538 }
3539 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:523540 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3541 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:563542 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293543 return;
3544 }
3545 }
3546 }
3547 // We failed.
[email protected]457f5cf2009-08-18 16:37:523548 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3549 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563550 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293551}
3552
[email protected]71f65dd2009-02-11 19:14:563553void AutomationProvider::BringBrowserToFront(int browser_handle,
3554 bool* success) {
initial.commit09911bf2008-07-26 23:55:293555 if (browser_tracker_->ContainsHandle(browser_handle)) {
3556 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:063557 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:563558 *success = true;
initial.commit09911bf2008-07-26 23:55:293559 } else {
[email protected]71f65dd2009-02-11 19:14:563560 *success = false;
initial.commit09911bf2008-07-26 23:55:293561 }
3562}
3563
[email protected]bdb7ff62010-07-20 01:56:523564void AutomationProvider::IsMenuCommandEnabled(int browser_handle,
3565 int message_num,
3566 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:293567 if (browser_tracker_->ContainsHandle(browser_handle)) {
3568 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563569 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:143570 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:293571 } else {
[email protected]71f65dd2009-02-11 19:14:563572 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:293573 }
3574}
3575
[email protected]71f65dd2009-02-11 19:14:563576void AutomationProvider::PrintNow(int tab_handle,
3577 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:573578 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:343579 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3580 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:293581 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:573582 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:143583 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:343584 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:573585 return;
initial.commit09911bf2008-07-26 23:55:293586 }
[email protected]71f65dd2009-02-11 19:14:563587 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
3588 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293589}
[email protected]d301c952009-07-13 15:02:413590
[email protected]71f65dd2009-02-11 19:14:563591void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:113592 const FilePath& file_name,
3593 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:563594 int type,
3595 bool* success) {
initial.commit09911bf2008-07-26 23:55:293596 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:563597 *success = false;
initial.commit09911bf2008-07-26 23:55:293598 return;
3599 }
3600
3601 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3602 Browser* browser = FindAndActivateTab(nav);
3603 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:143604 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:563605 *success = false;
initial.commit09911bf2008-07-26 23:55:293606 return;
3607 }
3608
initial.commit09911bf2008-07-26 23:55:293609 SavePackage::SavePackageType save_type =
3610 static_cast<SavePackage::SavePackageType>(type);
3611 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
3612 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:343613 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:293614
[email protected]71f65dd2009-02-11 19:14:563615 *success = true;
initial.commit09911bf2008-07-26 23:55:293616}
3617
[email protected]71f65dd2009-02-11 19:14:563618void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
3619 bool* success,
3620 std::wstring* text) {
3621 *success = false;
initial.commit09911bf2008-07-26 23:55:293622 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563623 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:523624 GetText();
[email protected]71f65dd2009-02-11 19:14:563625 *success = true;
initial.commit09911bf2008-07-26 23:55:293626 }
initial.commit09911bf2008-07-26 23:55:293627}
3628
[email protected]71f65dd2009-02-11 19:14:563629void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
3630 const std::wstring& text,
3631 bool* success) {
3632 *success = false;
initial.commit09911bf2008-07-26 23:55:293633 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:523634 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
3635 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:563636 *success = true;
initial.commit09911bf2008-07-26 23:55:293637 }
initial.commit09911bf2008-07-26 23:55:293638}
3639
3640void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:563641 int autocomplete_edit_handle,
3642 bool* success,
3643 std::vector<AutocompleteMatchData>* matches) {
3644 *success = false;
initial.commit09911bf2008-07-26 23:55:293645 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:273646 const AutocompleteResult& result = autocomplete_edit_tracker_->
3647 GetResource(autocomplete_edit_handle)->model()->result();
3648 for (AutocompleteResult::const_iterator i = result.begin();
3649 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:563650 matches->push_back(AutocompleteMatchData(*i));
3651 *success = true;
initial.commit09911bf2008-07-26 23:55:293652 }
initial.commit09911bf2008-07-26 23:55:293653}
3654
3655void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:563656 int autocomplete_edit_handle,
3657 bool* success,
3658 bool* query_in_progress) {
3659 *success = false;
3660 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:293661 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563662 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:523663 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:563664 *success = true;
initial.commit09911bf2008-07-26 23:55:293665 }
initial.commit09911bf2008-07-26 23:55:293666}
3667
[email protected]63514af2010-03-30 17:17:233668#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:193669
[email protected]5ae5bed2009-08-21 18:52:443670#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:393671
[email protected]57c6a652009-05-04 07:58:343672TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:573673 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:573674 if (tab_tracker_->ContainsHandle(handle)) {
3675 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:343676 if (tab)
3677 *tab = nav_controller;
3678 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:573679 }
[email protected]57c6a652009-05-04 07:58:343680 return NULL;
[email protected]20e93d12008-08-28 16:31:573681}
3682
initial.commit09911bf2008-07-26 23:55:293683TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
3684 : AutomationProvider(profile) {
3685 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:143686 registrar_.Add(this, NotificationType::SESSION_END,
3687 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:293688}
3689
3690TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:293691 BrowserList::RemoveObserver(this);
3692}
3693
3694void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:143695 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:293696 AutomationProvider::OnChannelError();
3697}
3698
[email protected]679082052010-07-21 21:30:133699void TestingAutomationProvider::OnBrowserAdded(const Browser* browser) {
3700}
3701
initial.commit09911bf2008-07-26 23:55:293702void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
3703 // For backwards compatibility with the testing automation interface, we
3704 // want the automation provider (and hence the process) to go away when the
3705 // last browser goes away.
[email protected]c984d9f2010-07-20 20:52:203706 if (BrowserList::size() == 1 && !CommandLine::ForCurrentProcess()->HasSwitch(
3707 switches::kKeepAliveForTest)) {
[email protected]4f3dc372009-02-24 00:10:293708 // If you change this, update Observer for NotificationType::SESSION_END
3709 // below.
[email protected]295039bd2008-08-15 04:32:573710 MessageLoop::current()->PostTask(FROM_HERE,
3711 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:293712 }
3713}
3714
3715void TestingAutomationProvider::Observe(NotificationType type,
3716 const NotificationSource& source,
3717 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:563718 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:293719 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
3720 // before the task runs resulting in this object not being deleted. This
3721 // Release balance out the Release scheduled by OnBrowserRemoving.
3722 Release();
3723}
[email protected]295039bd2008-08-15 04:32:573724
3725void TestingAutomationProvider::OnRemoveProvider() {
3726 AutomationProviderList::GetInstance()->RemoveProvider(this);
3727}
[email protected]8a3422c92008-09-24 17:42:423728
[email protected]816633a2009-11-11 21:48:183729void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:563730 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:423731 if (tab_tracker_->ContainsHandle(handle)) {
3732 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:343733 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:113734 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423735 }
[email protected]8a3422c92008-09-24 17:42:423736}
3737
[email protected]816633a2009-11-11 21:48:183738void AutomationProvider::ClickInfoBarAccept(int handle,
3739 int info_bar_index,
3740 bool wait_for_navigation,
3741 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:423742 bool success = false;
3743 if (tab_tracker_->ContainsHandle(handle)) {
3744 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3745 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:113746 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423747 if (info_bar_index >= 0 && info_bar_index < count) {
3748 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:013749 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:423750 }
[email protected]eb9ba192008-12-02 02:41:343751 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:113752 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:343753 info_bar_index);
3754 if (delegate->AsConfirmInfoBarDelegate())
3755 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:423756 success = true;
3757 }
3758 }
[email protected]4f3dc372009-02-24 00:10:293759 }
[email protected]58f622a62009-10-04 01:17:553760
3761 // This "!wait_for_navigation || !success condition" logic looks suspicious.
3762 // It will send a failure message when success is true but
3763 // |wait_for_navigation| is false.
3764 // TODO(phajdan.jr): investgate whether the reply param (currently
3765 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:423766 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:183767 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:523768 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:423769}
3770
[email protected]71f65dd2009-02-11 19:14:563771void AutomationProvider::GetLastNavigationTime(int handle,
3772 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:423773 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:563774 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:423775}
3776
[email protected]71f65dd2009-02-11 19:14:563777void AutomationProvider::WaitForNavigation(int handle,
3778 int64 last_navigation_time,
3779 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:253780 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:423781 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:253782
[email protected]8a3422c92008-09-24 17:42:423783 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:563784 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:523785 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
3786 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:553787 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:293788 return;
[email protected]8a3422c92008-09-24 17:42:423789 }
3790
[email protected]7dad3d5f2010-03-04 00:27:013791 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:423792}
3793
[email protected]71f65dd2009-02-11 19:14:563794void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163795 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563796 int value,
3797 bool* success) {
3798 *success = false;
[email protected]8a3422c92008-09-24 17:42:423799 if (browser_tracker_->ContainsHandle(handle)) {
3800 Browser* browser = browser_tracker_->GetResource(handle);
3801 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563802 *success = true;
[email protected]8a3422c92008-09-24 17:42:423803 }
[email protected]8a3422c92008-09-24 17:42:423804}
[email protected]97fa6ce32008-12-19 01:48:163805
[email protected]71f65dd2009-02-11 19:14:563806void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163807 const std::wstring& name,
[email protected]ddd231e2010-06-29 20:35:193808 const std::string& value,
[email protected]71f65dd2009-02-11 19:14:563809 bool* success) {
3810 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163811 if (browser_tracker_->ContainsHandle(handle)) {
3812 Browser* browser = browser_tracker_->GetResource(handle);
3813 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563814 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163815 }
[email protected]97fa6ce32008-12-19 01:48:163816}
3817
[email protected]71f65dd2009-02-11 19:14:563818void AutomationProvider::GetBooleanPreference(int handle,
3819 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:363820 bool* success,
3821 bool* value) {
[email protected]71f65dd2009-02-11 19:14:563822 *success = false;
3823 *value = false;
[email protected]97fa6ce32008-12-19 01:48:163824 if (browser_tracker_->ContainsHandle(handle)) {
3825 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:563826 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
3827 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163828 }
[email protected]97fa6ce32008-12-19 01:48:163829}
3830
[email protected]71f65dd2009-02-11 19:14:563831void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163832 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563833 bool value,
3834 bool* success) {
3835 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163836 if (browser_tracker_->ContainsHandle(handle)) {
3837 Browser* browser = browser_tracker_->GetResource(handle);
3838 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563839 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163840 }
[email protected]97fa6ce32008-12-19 01:48:163841}
3842
3843// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563844void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:403845 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:163846 if (tab_tracker_->ContainsHandle(tab_handle)) {
3847 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3848 Browser* browser = FindAndActivateTab(nav);
3849 DCHECK(browser);
3850
[email protected]57c6a652009-05-04 07:58:343851 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
3852 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:163853 }
[email protected]97fa6ce32008-12-19 01:48:163854}
3855
[email protected]b8f48d12009-11-09 20:14:363856// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563857void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:403858 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:563859 bool* success) {
3860 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163861 if (tab_tracker_->ContainsHandle(tab_handle)) {
3862 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:193863 if (!nav)
3864 return;
[email protected]97fa6ce32008-12-19 01:48:163865 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:163866
[email protected]2f2afba2010-04-01 01:53:193867 // If the browser has UI, simulate what a user would do.
3868 // Activate the tab and then click the encoding menu.
3869 if (browser &&
3870 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:163871 int selected_encoding_id =
3872 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
3873 if (selected_encoding_id) {
3874 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:563875 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163876 }
[email protected]2f2afba2010-04-01 01:53:193877 } else {
3878 // There is no UI, Chrome probably runs as Chrome-Frame mode.
3879 // Try to get TabContents and call its override_encoding method.
3880 TabContents* contents = nav->tab_contents();
3881 if (!contents)
3882 return;
3883 const std::string selected_encoding =
3884 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
3885 if (selected_encoding.empty())
3886 return;
3887 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:163888 }
3889 }
[email protected]97fa6ce32008-12-19 01:48:163890}
[email protected]5bcdb312009-01-07 21:43:203891
[email protected]4d434a1a2009-02-11 21:06:573892void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:203893 SavePackage::SetShouldPromptUser(should_prompt);
3894}
[email protected]87eab222009-03-13 00:47:453895
[email protected]66ba4932009-06-04 19:22:133896void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
3897 *count = -1; // -1 is the error code
3898 if (tab_tracker_->ContainsHandle(handle)) {
3899 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3900 TabContents* tab_contents = nav_controller->tab_contents();
3901 if (tab_contents) {
3902 BlockedPopupContainer* container =
3903 tab_contents->blocked_popup_container();
3904 if (container) {
3905 *count = static_cast<int>(container->GetBlockedPopupCount());
3906 } else {
3907 // If we don't have a container, we don't have any blocked popups to
3908 // contain!
3909 *count = 0;
3910 }
3911 }
3912 }
3913}
[email protected]f7a68432009-07-29 23:18:193914
3915void AutomationProvider::SelectAll(int tab_handle) {
3916 RenderViewHost* view = GetViewForTab(tab_handle);
3917 if (!view) {
3918 NOTREACHED();
3919 return;
3920 }
3921
3922 view->SelectAll();
3923}
3924
3925void AutomationProvider::Cut(int tab_handle) {
3926 RenderViewHost* view = GetViewForTab(tab_handle);
3927 if (!view) {
3928 NOTREACHED();
3929 return;
3930 }
3931
3932 view->Cut();
3933}
3934
3935void AutomationProvider::Copy(int tab_handle) {
3936 RenderViewHost* view = GetViewForTab(tab_handle);
3937 if (!view) {
3938 NOTREACHED();
3939 return;
3940 }
3941
3942 view->Copy();
3943}
3944
3945void AutomationProvider::Paste(int tab_handle) {
3946 RenderViewHost* view = GetViewForTab(tab_handle);
3947 if (!view) {
3948 NOTREACHED();
3949 return;
3950 }
3951
3952 view->Paste();
3953}
3954
3955void AutomationProvider::ReloadAsync(int tab_handle) {
3956 if (tab_tracker_->ContainsHandle(tab_handle)) {
3957 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3958 if (!tab) {
3959 NOTREACHED();
3960 return;
3961 }
3962
[email protected]106a0812010-03-18 00:15:123963 const bool check_for_repost = true;
3964 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193965 }
3966}
3967
3968void AutomationProvider::StopAsync(int tab_handle) {
3969 RenderViewHost* view = GetViewForTab(tab_handle);
3970 if (!view) {
[email protected]8b2b3312009-09-14 18:38:363971 // We tolerate StopAsync being called even before a view has been created.
3972 // So just log a warning instead of a NOTREACHED().
3973 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:193974 return;
3975 }
3976
3977 view->Stop();
3978}
3979
[email protected]1bb5f892009-10-06 01:44:573980void AutomationProvider::OnSetPageFontSize(int tab_handle,
3981 int font_size) {
3982 AutomationPageFontSize automation_font_size =
3983 static_cast<AutomationPageFontSize>(font_size);
3984
3985 if (automation_font_size < SMALLEST_FONT ||
3986 automation_font_size > LARGEST_FONT) {
3987 DLOG(ERROR) << "Invalid font size specified : "
3988 << font_size;
3989 return;
3990 }
3991
3992 if (tab_tracker_->ContainsHandle(tab_handle)) {
3993 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3994 DCHECK(tab != NULL);
3995 if (tab && tab->tab_contents()) {
3996 DCHECK(tab->tab_contents()->profile() != NULL);
3997 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
3998 prefs::kWebKitDefaultFontSize, font_size);
3999 }
4000 }
4001}
4002
[email protected]bc73b4e52010-03-26 04:16:204003void AutomationProvider::RemoveBrowsingData(int remove_mask) {
4004 BrowsingDataRemover* remover;
4005 remover = new BrowsingDataRemover(profile(),
4006 BrowsingDataRemover::EVERYTHING, // All time periods.
4007 base::Time());
4008 remover->Remove(remove_mask);
4009 // BrowsingDataRemover deletes itself.
4010}
[email protected]1bb5f892009-10-06 01:44:574011
[email protected]2949e90d2009-08-21 15:32:524012void AutomationProvider::WaitForBrowserWindowCountToBecome(
4013 int target_count, IPC::Message* reply_message) {
4014 if (static_cast<int>(BrowserList::size()) == target_count) {
4015 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
4016 reply_message, true);
4017 Send(reply_message);
4018 return;
4019 }
4020
4021 // Set up an observer (it will delete itself).
4022 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
4023}
4024
4025void AutomationProvider::WaitForAppModalDialogToBeShown(
4026 IPC::Message* reply_message) {
4027 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
4028 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
4029 reply_message, true);
4030 Send(reply_message);
4031 return;
4032 }
4033
4034 // Set up an observer (it will delete itself).
4035 new AppModalDialogShownObserver(this, reply_message);
4036}
4037
[email protected]1126a1d32009-08-26 15:39:264038void AutomationProvider::GoBackBlockUntilNavigationsComplete(
4039 int handle, int number_of_navigations, IPC::Message* reply_message) {
4040 if (tab_tracker_->ContainsHandle(handle)) {
4041 NavigationController* tab = tab_tracker_->GetResource(handle);
4042 Browser* browser = FindAndActivateTab(tab);
4043 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:014044 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
4045 false);
[email protected]1126a1d32009-08-26 15:39:264046 browser->GoBack(CURRENT_TAB);
4047 return;
4048 }
4049 }
4050
4051 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
4052 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
4053 Send(reply_message);
4054}
4055
4056void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
4057 int handle, int number_of_navigations, IPC::Message* reply_message) {
4058 if (tab_tracker_->ContainsHandle(handle)) {
4059 NavigationController* tab = tab_tracker_->GetResource(handle);
4060 Browser* browser = FindAndActivateTab(tab);
4061 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:014062 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
4063 false);
[email protected]1126a1d32009-08-26 15:39:264064 browser->GoForward(CURRENT_TAB);
4065 return;
4066 }
4067 }
4068
4069 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
4070 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
4071 Send(reply_message);
4072}
4073
[email protected]f7a68432009-07-29 23:18:194074RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
4075 if (tab_tracker_->ContainsHandle(tab_handle)) {
4076 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
4077 if (!tab) {
4078 NOTREACHED();
4079 return NULL;
4080 }
4081
4082 TabContents* tab_contents = tab->tab_contents();
4083 if (!tab_contents) {
4084 NOTREACHED();
4085 return NULL;
4086 }
4087
4088 RenderViewHost* view_host = tab_contents->render_view_host();
4089 return view_host;
4090 }
4091
4092 return NULL;
4093}
[email protected]675595f2009-08-26 22:32:044094
4095void AutomationProvider::GetBrowserForWindow(int window_handle,
4096 bool* success,
4097 int* browser_handle) {
4098 *success = false;
4099 *browser_handle = 0;
4100
4101 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
4102 if (!window)
4103 return;
4104
4105 BrowserList::const_iterator iter = BrowserList::begin();
4106 for (;iter != BrowserList::end(); ++iter) {
4107 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
4108 if (window == this_window) {
4109 // Add() returns the existing handle for the resource if any.
4110 *browser_handle = browser_tracker_->Add(*iter);
4111 *success = true;
4112 return;
4113 }
4114 }
4115}
[email protected]d11c8e92009-10-20 23:26:404116
4117void AutomationProvider::InstallExtension(const FilePath& crx_path,
4118 IPC::Message* reply_message) {
4119 ExtensionsService* service = profile_->GetExtensionsService();
4120 if (service) {
4121 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:194122 new ExtensionInstallNotificationObserver(this,
4123 AutomationMsg_InstallExtension::ID,
4124 reply_message);
[email protected]d11c8e92009-10-20 23:26:404125
4126 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:164127 scoped_refptr<CrxInstaller> installer(
4128 new CrxInstaller(install_dir,
4129 service,
4130 NULL)); // silent install, no UI
4131 installer->set_allow_privilege_increase(true);
4132 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:404133 } else {
4134 AutomationMsg_InstallExtension::WriteReplyParams(
4135 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
4136 Send(reply_message);
4137 }
4138}
4139
4140void AutomationProvider::LoadExpandedExtension(
4141 const FilePath& extension_dir,
4142 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:384143 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:404144 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:194145 new ExtensionInstallNotificationObserver(
4146 this,
4147 AutomationMsg_LoadExpandedExtension::ID,
4148 reply_message);
[email protected]d11c8e92009-10-20 23:26:404149
4150 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:404151 } else {
4152 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
4153 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
4154 Send(reply_message);
4155 }
4156}
[email protected]673fd2c02010-02-04 23:10:004157
[email protected]a1e62d12010-03-16 02:18:434158void AutomationProvider::GetEnabledExtensions(
4159 std::vector<FilePath>* result) {
4160 ExtensionsService* service = profile_->GetExtensionsService();
4161 DCHECK(service);
4162 if (service->extensions_enabled()) {
4163 const ExtensionList* extensions = service->extensions();
4164 DCHECK(extensions);
4165 for (size_t i = 0; i < extensions->size(); ++i) {
4166 Extension* extension = (*extensions)[i];
4167 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:124168 if (extension->location() == Extension::INTERNAL ||
4169 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:074170 result->push_back(extension->path());
4171 }
[email protected]a1e62d12010-03-16 02:18:434172 }
4173 }
4174}
4175
[email protected]790788ac2010-04-06 17:52:194176void AutomationProvider::WaitForExtensionTestResult(
4177 IPC::Message* reply_message) {
4178 DCHECK(reply_message_ == NULL);
4179 reply_message_ = reply_message;
4180 // Call MaybeSendResult, because the result might have come in before
4181 // we were waiting on it.
4182 extension_test_result_observer_->MaybeSendResult();
4183}
4184
4185void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:094186 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:194187 ExtensionsService* service = profile_->GetExtensionsService();
4188 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
4189 if (service && manager) {
4190 // The observer will delete itself when done.
4191 new ExtensionReadyNotificationObserver(
4192 manager,
4193 this,
4194 AutomationMsg_InstallExtensionAndGetHandle::ID,
4195 reply_message);
4196
[email protected]d7e5525d2010-04-20 14:37:094197 ExtensionInstallUI* client =
4198 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:194199 scoped_refptr<CrxInstaller> installer(
4200 new CrxInstaller(service->install_directory(),
4201 service,
[email protected]d7e5525d2010-04-20 14:37:094202 client));
[email protected]790788ac2010-04-06 17:52:194203 installer->set_allow_privilege_increase(true);
4204 installer->InstallCrx(crx_path);
4205 } else {
4206 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
4207 reply_message, 0);
4208 Send(reply_message);
4209 }
4210}
4211
4212void AutomationProvider::UninstallExtension(int extension_handle,
4213 bool* success) {
4214 *success = false;
4215 Extension* extension = GetExtension(extension_handle);
4216 ExtensionsService* service = profile_->GetExtensionsService();
4217 if (extension && service) {
4218 ExtensionUnloadNotificationObserver observer;
4219 service->UninstallExtension(extension->id(), false);
4220 // The extension unload notification should have been sent synchronously
4221 // with the uninstall. Just to be safe, check that it was received.
4222 *success = observer.did_receive_unload_notification();
4223 }
4224}
4225
4226void AutomationProvider::EnableExtension(int extension_handle,
4227 IPC::Message* reply_message) {
4228 Extension* extension = GetDisabledExtension(extension_handle);
4229 ExtensionsService* service = profile_->GetExtensionsService();
4230 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
4231 // Only enable if this extension is disabled.
4232 if (extension && service && manager) {
4233 // The observer will delete itself when done.
4234 new ExtensionReadyNotificationObserver(
4235 manager,
4236 this,
4237 AutomationMsg_EnableExtension::ID,
4238 reply_message);
4239 service->EnableExtension(extension->id());
4240 } else {
4241 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
4242 Send(reply_message);
4243 }
4244}
4245
4246void AutomationProvider::DisableExtension(int extension_handle,
4247 bool* success) {
4248 *success = false;
4249 Extension* extension = GetEnabledExtension(extension_handle);
4250 ExtensionsService* service = profile_->GetExtensionsService();
4251 if (extension && service) {
4252 ExtensionUnloadNotificationObserver observer;
4253 service->DisableExtension(extension->id());
4254 // The extension unload notification should have been sent synchronously
4255 // with the disable. Just to be safe, check that it was received.
4256 *success = observer.did_receive_unload_notification();
4257 }
4258}
4259
4260void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
4261 int extension_handle, int browser_handle,
4262 IPC::Message* reply_message) {
4263 bool success = false;
4264 Extension* extension = GetEnabledExtension(extension_handle);
4265 ExtensionsService* service = profile_->GetExtensionsService();
4266 ExtensionMessageService* message_service =
4267 profile_->GetExtensionMessageService();
4268 Browser* browser = browser_tracker_->GetResource(browser_handle);
4269 if (extension && service && message_service && browser) {
4270 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
4271 if (extension->page_action()) {
4272 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
4273 browser->profile(), extension->id(), "action", tab_id, "", 1);
4274 success = true;
4275 } else if (extension->browser_action()) {
4276 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
4277 browser->profile(), extension->id(), browser);
4278 success = true;
4279 }
4280 }
4281 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
4282 reply_message, success);
4283 Send(reply_message);
4284}
4285
4286void AutomationProvider::MoveExtensionBrowserAction(
4287 int extension_handle, int index, bool* success) {
4288 *success = false;
4289 Extension* extension = GetEnabledExtension(extension_handle);
4290 ExtensionsService* service = profile_->GetExtensionsService();
4291 if (extension && service) {
4292 ExtensionToolbarModel* toolbar = service->toolbar_model();
4293 if (toolbar) {
4294 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
4295 toolbar->MoveBrowserAction(extension, index);
4296 *success = true;
4297 } else {
4298 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
4299 }
4300 }
4301 }
4302}
4303
4304void AutomationProvider::GetExtensionProperty(
4305 int extension_handle,
4306 AutomationMsg_ExtensionProperty type,
4307 bool* success,
4308 std::string* value) {
4309 *success = false;
4310 Extension* extension = GetExtension(extension_handle);
4311 ExtensionsService* service = profile_->GetExtensionsService();
4312 if (extension && service) {
4313 ExtensionToolbarModel* toolbar = service->toolbar_model();
4314 int found_index = -1;
4315 int index = 0;
4316 switch (type) {
4317 case AUTOMATION_MSG_EXTENSION_ID:
4318 *value = extension->id();
4319 *success = true;
4320 break;
4321 case AUTOMATION_MSG_EXTENSION_NAME:
4322 *value = extension->name();
4323 *success = true;
4324 break;
4325 case AUTOMATION_MSG_EXTENSION_VERSION:
4326 *value = extension->VersionString();
4327 *success = true;
4328 break;
4329 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
4330 if (toolbar) {
4331 for (ExtensionList::const_iterator iter = toolbar->begin();
4332 iter != toolbar->end(); iter++) {
4333 // Skip this extension if we are in incognito mode
4334 // and it is not incognito-enabled.
4335 if (profile_->IsOffTheRecord() &&
4336 !service->IsIncognitoEnabled(*iter))
4337 continue;
4338 if (*iter == extension) {
4339 found_index = index;
4340 break;
4341 }
4342 index++;
4343 }
4344 *value = IntToString(found_index);
4345 *success = true;
4346 }
4347 break;
4348 default:
4349 LOG(WARNING) << "Trying to get undefined extension property";
4350 break;
4351 }
4352 }
4353}
4354
[email protected]673fd2c02010-02-04 23:10:004355void AutomationProvider::SaveAsAsync(int tab_handle) {
4356 NavigationController* tab = NULL;
4357 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
4358 if (tab_contents)
4359 tab_contents->OnSavePage();
4360}
[email protected]7dad3d5f2010-03-04 00:27:014361
4362void AutomationProvider::SetContentSetting(
4363 int handle,
4364 const std::string& host,
4365 ContentSettingsType content_type,
4366 ContentSetting setting,
4367 bool* success) {
4368 *success = false;
4369 if (browser_tracker_->ContainsHandle(handle)) {
4370 Browser* browser = browser_tracker_->GetResource(handle);
4371 HostContentSettingsMap* map =
4372 browser->profile()->GetHostContentSettingsMap();
4373 if (host.empty()) {
4374 map->SetDefaultContentSetting(content_type, setting);
4375 } else {
[email protected]0314ae02010-04-08 09:18:294376 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
4377 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:014378 }
4379 *success = true;
4380 }
4381}
[email protected]cc824372010-03-31 15:33:014382
4383#if !defined(TOOLKIT_VIEWS)
4384void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
4385 NOTIMPLEMENTED();
4386};
4387
4388void AutomationProvider::WaitForFocusedViewIDToChange(
4389 int handle, int previous_view_id, IPC::Message* reply_message) {
4390 NOTIMPLEMENTED();
4391}
4392
4393void AutomationProvider::StartTrackingPopupMenus(
4394 int browser_handle, bool* success) {
4395 NOTIMPLEMENTED();
4396}
4397
4398void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
4399 NOTIMPLEMENTED();
4400}
4401#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:094402
4403void AutomationProvider::ResetToDefaultTheme() {
4404 profile_->ClearTheme();
4405}