blob: c7d73cbb4f719ce56b390a03d6b42b5435f0d598 [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]37126212009-05-06 02:23:319#include "app/message_box_flags.h"
[email protected]2041cf342010-02-19 03:15:5910#include "base/callback.h"
[email protected]7060bb292010-06-24 00:52:4911#include "base/file_path.h"
[email protected]c6cb1992009-04-13 16:45:2912#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2013#include "base/json/json_reader.h"
[email protected]59a611242010-04-02 02:24:0414#include "base/json/json_writer.h"
[email protected]93364da2010-06-29 18:03:4415#include "base/json/string_escape.h"
[email protected]bc1407f2009-09-29 00:33:3516#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3817#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4619#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5020#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2721#include "base/string_util.h"
[email protected]9eaa18e2010-06-29 20:51:0122#include "base/task.h"
[email protected]5fac9622009-02-04 21:49:3823#include "base/thread.h"
[email protected]528c56d2010-07-30 19:28:4424#include "base/string_number_conversions.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 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]7c983cc2010-07-16 11:33:342104 AutomationJSONReply reply(this, reply_message);
[email protected]d4adc292010-04-15 18:06:392105
2106 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:342107 reply.SendError("no download manager");
2108 return;
[email protected]d4adc292010-04-15 18:06:392109 }
[email protected]82f37b02010-07-29 22:04:572110
2111 std::vector<DownloadItem*> downloads;
2112 profile_->GetDownloadManager()->GetAllDownloads(FilePath(), &downloads);
[email protected]d4adc292010-04-15 18:06:392113
2114 std::map<DownloadItem::DownloadState, std::string> state_to_string;
2115 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
2116 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
2117 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
2118 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
2119
2120 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
2121 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
2122 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
2123 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
2124 std::string("DANGEROUS_BUT_VALIDATED");
2125
2126 ListValue* list_of_downloads = new ListValue;
2127 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
2128 it != downloads.end();
2129 it++) { // Fill info about each download item.
2130 DictionaryValue* dl_item_value = new DictionaryValue;
2131 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
2132 dl_item_value->SetString(L"url", (*it)->url().spec());
2133 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
[email protected]2e030682010-07-23 19:45:362134 dl_item_value->SetString(L"file_name", (*it)->GetFileName().value());
[email protected]d4adc292010-04-15 18:06:392135 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
2136 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
2137 dl_item_value->SetBoolean(L"open_when_complete",
2138 (*it)->open_when_complete());
2139 dl_item_value->SetBoolean(L"is_extension_install",
2140 (*it)->is_extension_install());
2141 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
2142 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
2143 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
2144 dl_item_value->SetString(L"safety_state",
2145 safety_state_to_string[(*it)->safety_state()]);
2146 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
2147 list_of_downloads->Append(dl_item_value);
2148 }
2149 return_value->Set(L"downloads", list_of_downloads);
[email protected]d4adc292010-04-15 18:06:392150
[email protected]7c983cc2010-07-16 11:33:342151 reply.SendSuccess(return_value.get());
[email protected]d4adc292010-04-15 18:06:392152 // All value objects allocated above are owned by |return_value|
2153 // and get freed by it.
2154}
2155
[email protected]59a611242010-04-02 02:24:042156void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:582157 Browser* browser,
[email protected]59a611242010-04-02 02:24:042158 DictionaryValue* args,
2159 IPC::Message* reply_message) {
[email protected]7c983cc2010-07-16 11:33:342160 AutomationJSONReply reply(this, reply_message);
[email protected]59a611242010-04-02 02:24:042161
2162 // Look for a quick return.
2163 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:342164 reply.SendSuccess(NULL); // No download manager.
2165 return;
[email protected]59a611242010-04-02 02:24:042166 }
[email protected]82f37b02010-07-29 22:04:572167 std::vector<DownloadItem*> downloads;
2168 profile_->GetDownloadManager()->GetCurrentDownloads(FilePath(), &downloads);
2169 if (downloads.empty()) {
[email protected]7c983cc2010-07-16 11:33:342170 reply.SendSuccess(NULL);
[email protected]f6ff0df2010-07-11 22:41:432171 return;
2172 }
[email protected]59a611242010-04-02 02:24:042173
2174 // The observer owns itself. When the last observed item pings, it
2175 // deletes itself.
2176 AutomationProviderDownloadItemObserver* item_observer =
2177 new AutomationProviderDownloadItemObserver(
2178 this, reply_message, downloads.size());
2179 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
2180 i != downloads.end();
2181 i++) {
2182 (*i)->AddObserver(item_observer);
2183 }
2184}
2185
[email protected]24e2b102010-04-29 17:56:472186// Sample json input: { "command": "GetPrefsInfo" }
2187// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582188void AutomationProvider::GetPrefsInfo(Browser* browser,
2189 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472190 IPC::Message* reply_message) {
[email protected]24e2b102010-04-29 17:56:472191 const PrefService::PreferenceSet& prefs =
2192 profile_->GetPrefs()->preference_set();
2193 DictionaryValue* items = new DictionaryValue;
2194 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
2195 it != prefs.end(); ++it) {
2196 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
2197 }
2198 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2199 return_value->Set(L"prefs", items); // return_value owns items.
[email protected]7c983cc2010-07-16 11:33:342200 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]24e2b102010-04-29 17:56:472201}
2202
2203// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:582204void AutomationProvider::SetPrefs(Browser* browser,
2205 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472206 IPC::Message* reply_message) {
[email protected]24e2b102010-04-29 17:56:472207 std::wstring path;
2208 Value* val;
[email protected]7c983cc2010-07-16 11:33:342209 AutomationJSONReply reply(this, reply_message);
[email protected]24e2b102010-04-29 17:56:472210 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
2211 PrefService* pref_service = profile_->GetPrefs();
2212 const PrefService::Preference* pref =
2213 pref_service->FindPreference(path.c_str());
2214 if (!pref) { // Not a registered pref.
[email protected]7c983cc2010-07-16 11:33:342215 reply.SendError("pref not registered.");
2216 return;
[email protected]24e2b102010-04-29 17:56:472217 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
[email protected]7c983cc2010-07-16 11:33:342218 reply.SendError("pref is managed. cannot be changed.");
2219 return;
[email protected]24e2b102010-04-29 17:56:472220 } else { // Set the pref.
2221 pref_service->Set(path.c_str(), *val);
2222 }
2223 } else {
[email protected]7c983cc2010-07-16 11:33:342224 reply.SendError("no pref path or value given.");
2225 return;
[email protected]24e2b102010-04-29 17:56:472226 }
2227
[email protected]7c983cc2010-07-16 11:33:342228 reply.SendSuccess(NULL);
[email protected]24e2b102010-04-29 17:56:472229}
2230
[email protected]53329582010-05-14 21:10:582231// Sample json input: { "command": "GetOmniboxInfo" }
2232// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
2233void AutomationProvider::GetOmniboxInfo(Browser* browser,
2234 DictionaryValue* args,
2235 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582236 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2237
2238 LocationBar* loc_bar = browser->window()->GetLocationBar();
2239 AutocompleteEditView* edit_view = loc_bar->location_entry();
2240 AutocompleteEditModel* model = edit_view->model();
2241
2242 // Fill up matches.
2243 ListValue* matches = new ListValue;
2244 const AutocompleteResult& result = model->result();
2245 for (AutocompleteResult::const_iterator i = result.begin();
2246 i != result.end(); ++i) {
2247 const AutocompleteMatch& match = *i;
2248 DictionaryValue* item = new DictionaryValue; // owned by return_value
2249 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
2250 item->SetBoolean(L"starred", match.starred);
2251 item->SetString(L"destination_url", match.destination_url.spec());
2252 item->SetString(L"contents", match.contents);
2253 item->SetString(L"description", match.description);
2254 matches->Append(item);
2255 }
2256 return_value->Set(L"matches", matches);
2257
2258 // Fill up other properties.
2259 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2260 properties->SetBoolean(L"has_focus", model->has_focus());
2261 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2262 properties->SetString(L"keyword", model->keyword());
2263 properties->SetString(L"text", edit_view->GetText());
2264 return_value->Set(L"properties", properties);
2265
[email protected]7c983cc2010-07-16 11:33:342266 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]53329582010-05-14 21:10:582267}
2268
2269// Sample json input: { "command": "SetOmniboxText",
2270// "text": "goog" }
2271void AutomationProvider::SetOmniboxText(Browser* browser,
2272 DictionaryValue* args,
2273 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582274 std::wstring text;
[email protected]7c983cc2010-07-16 11:33:342275 AutomationJSONReply reply(this, reply_message);
[email protected]53329582010-05-14 21:10:582276 if (!args->GetString(L"text", &text)) {
[email protected]7c983cc2010-07-16 11:33:342277 reply.SendError("text missing");
2278 return;
[email protected]53329582010-05-14 21:10:582279 }
[email protected]7c983cc2010-07-16 11:33:342280 browser->FocusLocationBar();
2281 LocationBar* loc_bar = browser->window()->GetLocationBar();
2282 AutocompleteEditView* edit_view = loc_bar->location_entry();
2283 edit_view->model()->OnSetFocus(false);
2284 edit_view->SetUserText(text);
2285 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582286}
2287
2288// Sample json input: { "command": "OmniboxMovePopupSelection",
2289// "count": 1 }
2290// Negative count implies up, positive implies down. Count values will be
2291// capped by the size of the popup list.
2292void AutomationProvider::OmniboxMovePopupSelection(
2293 Browser* browser,
2294 DictionaryValue* args,
2295 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582296 int count;
[email protected]7c983cc2010-07-16 11:33:342297 AutomationJSONReply reply(this, reply_message);
[email protected]53329582010-05-14 21:10:582298 if (!args->GetInteger(L"count", &count)) {
[email protected]7c983cc2010-07-16 11:33:342299 reply.SendError("count missing");
2300 return;
[email protected]53329582010-05-14 21:10:582301 }
[email protected]7c983cc2010-07-16 11:33:342302 LocationBar* loc_bar = browser->window()->GetLocationBar();
2303 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2304 model->OnUpOrDownKeyPressed(count);
2305 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582306}
2307
2308// Sample json input: { "command": "OmniboxAcceptInput" }
2309void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2310 DictionaryValue* args,
2311 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282312 NavigationController& controller =
2313 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122314 // Setup observer to wait until the selected item loads.
2315 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282316 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122317 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582318
2319 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582320}
2321
[email protected]a3cd5022010-06-16 18:25:292322// Sample json input: { "command": "GetInitialLoadTimes" }
2323// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2324void AutomationProvider::GetInitialLoadTimes(
2325 Browser*,
2326 DictionaryValue*,
2327 IPC::Message* reply_message) {
2328 scoped_ptr<DictionaryValue> return_value(
2329 initial_load_observer_->GetTimingInformation());
[email protected]f6ff0df2010-07-11 22:41:432330
2331 std::string json_return;
2332 base::JSONWriter::Write(return_value.get(), false, &json_return);
2333 AutomationMsg_SendJSONRequest::WriteReplyParams(
2334 reply_message, json_return, true);
2335 Send(reply_message);
[email protected]a3cd5022010-06-16 18:25:292336}
2337
[email protected]f7d48012010-05-06 08:17:052338// Sample json input: { "command": "GetPluginsInfo" }
2339// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582340void AutomationProvider::GetPluginsInfo(Browser* browser,
2341 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052342 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052343 std::vector<WebPluginInfo> plugins;
2344 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2345 ListValue* items = new ListValue;
2346 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2347 it != plugins.end();
2348 ++it) {
2349 DictionaryValue* item = new DictionaryValue;
[email protected]c9d811372010-06-23 21:44:572350 item->SetStringFromUTF16(L"name", it->name);
[email protected]f7d48012010-05-06 08:17:052351 item->SetString(L"path", it->path.value());
[email protected]c9d811372010-06-23 21:44:572352 item->SetStringFromUTF16(L"version", it->version);
2353 item->SetStringFromUTF16(L"desc", it->desc);
[email protected]f7d48012010-05-06 08:17:052354 item->SetBoolean(L"enabled", it->enabled);
2355 // Add info about mime types.
2356 ListValue* mime_types = new ListValue();
2357 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2358 it->mime_types.begin();
2359 type_it != it->mime_types.end();
2360 ++type_it) {
2361 DictionaryValue* mime_type = new DictionaryValue();
2362 mime_type->SetString(L"mimeType", type_it->mime_type);
[email protected]c9d811372010-06-23 21:44:572363 mime_type->SetStringFromUTF16(L"description", type_it->description);
[email protected]f7d48012010-05-06 08:17:052364
2365 ListValue* file_extensions = new ListValue();
2366 for (std::vector<std::string>::const_iterator ext_it =
2367 type_it->file_extensions.begin();
2368 ext_it != type_it->file_extensions.end();
2369 ++ext_it) {
2370 file_extensions->Append(new StringValue(*ext_it));
2371 }
2372 mime_type->Set(L"fileExtensions", file_extensions);
2373
2374 mime_types->Append(mime_type);
2375 }
2376 item->Set(L"mimeTypes", mime_types);
2377 items->Append(item);
2378 }
2379 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2380 return_value->Set(L"plugins", items); // return_value owns items.
2381
[email protected]7c983cc2010-07-16 11:33:342382 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]f7d48012010-05-06 08:17:052383}
2384
2385// Sample json input:
2386// { "command": "EnablePlugin",
2387// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582388void AutomationProvider::EnablePlugin(Browser* browser,
2389 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052390 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052391 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342392 AutomationJSONReply reply(this, reply_message);
[email protected]f6ff0df2010-07-11 22:41:432393 if (!args->GetString(L"path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342394 reply.SendError("path not specified.");
2395 return;
[email protected]f6ff0df2010-07-11 22:41:432396 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342397 reply.SendError(StringPrintf("Could not enable plugin for path %s.",
2398 path.c_str()));
2399 return;
[email protected]f6ff0df2010-07-11 22:41:432400 }
[email protected]7c983cc2010-07-16 11:33:342401 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052402}
2403
2404// Sample json input:
2405// { "command": "DisablePlugin",
2406// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582407void AutomationProvider::DisablePlugin(Browser* browser,
2408 DictionaryValue* args,
2409 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052410 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342411 AutomationJSONReply reply(this, reply_message);
[email protected]f6ff0df2010-07-11 22:41:432412 if (!args->GetString(L"path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342413 reply.SendError("path not specified.");
2414 return;
[email protected]f6ff0df2010-07-11 22:41:432415 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342416 reply.SendError(StringPrintf("Could not disable plugin for path %s.",
2417 path.c_str()));
2418 return;
[email protected]f6ff0df2010-07-11 22:41:432419 }
[email protected]7c983cc2010-07-16 11:33:342420 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052421}
2422
[email protected]7060bb292010-06-24 00:52:492423// Sample json input:
2424// { "command": "SaveTabContents",
2425// "tab_index": 0,
2426// "filename": <a full pathname> }
2427// Sample json output:
2428// {}
2429void AutomationProvider::SaveTabContents(Browser* browser,
2430 DictionaryValue* args,
2431 IPC::Message* reply_message) {
[email protected]7060bb292010-06-24 00:52:492432 int tab_index = 0;
2433 FilePath::StringType filename;
2434 FilePath::StringType parent_directory;
2435 TabContents* tab_contents = NULL;
2436
2437 if (!args->GetInteger(L"tab_index", &tab_index) ||
2438 !args->GetString(L"filename", &filename)) {
[email protected]26adfd312010-07-20 01:04:462439 AutomationJSONReply(this, reply_message).SendError(
2440 "tab_index or filename param missing");
[email protected]7c983cc2010-07-16 11:33:342441 return;
[email protected]7060bb292010-06-24 00:52:492442 } else {
2443 tab_contents = browser->GetTabContentsAt(tab_index);
2444 if (!tab_contents) {
[email protected]26adfd312010-07-20 01:04:462445 AutomationJSONReply(this, reply_message).SendError(
2446 "no tab at tab_index");
[email protected]7060bb292010-06-24 00:52:492447 return;
2448 }
2449 }
[email protected]7c983cc2010-07-16 11:33:342450 // We're doing a SAVE_AS_ONLY_HTML so the the directory path isn't
2451 // used. Nevertheless, SavePackage requires it be valid. Sigh.
2452 parent_directory = FilePath(filename).DirName().value();
2453 if (!tab_contents->SavePage(FilePath(filename), FilePath(parent_directory),
2454 SavePackage::SAVE_AS_ONLY_HTML)) {
[email protected]26adfd312010-07-20 01:04:462455 AutomationJSONReply(this, reply_message).SendError(
2456 "Could not initiate SavePage");
[email protected]7c983cc2010-07-16 11:33:342457 return;
2458 }
2459 // The observer will delete itself when done.
2460 new SavePackageNotificationObserver(tab_contents->save_package(),
2461 this, reply_message);
[email protected]7060bb292010-06-24 00:52:492462}
2463
[email protected]c5aa5322010-07-15 19:00:072464// Refer to ImportSettings() in chrome/test/pyautolib/pyauto.py for sample
2465// json input.
2466// Sample json output: "{}"
2467void AutomationProvider::ImportSettings(Browser* browser,
2468 DictionaryValue* args,
2469 IPC::Message* reply_message) {
[email protected]c5aa5322010-07-15 19:00:072470 // Map from the json string passed over to the import item masks.
2471 std::map<std::string, ImportItem> string_to_import_item;
2472 string_to_import_item["HISTORY"] = importer::HISTORY;
2473 string_to_import_item["FAVORITES"] = importer::FAVORITES;
2474 string_to_import_item["COOKIES"] = importer::COOKIES;
2475 string_to_import_item["PASSWORDS"] = importer::PASSWORDS;
2476 string_to_import_item["SEARCH_ENGINES"] = importer::SEARCH_ENGINES;
2477 string_to_import_item["HOME_PAGE"] = importer::HOME_PAGE;
2478 string_to_import_item["ALL"] = importer::ALL;
2479
2480 std::wstring browser_name;
2481 int import_items = 0;
2482 ListValue* import_items_list = NULL;
2483 bool first_run;
2484
2485 if (!args->GetString(L"import_from", &browser_name) ||
2486 !args->GetBoolean(L"first_run", &first_run) ||
2487 !args->GetList(L"import_items", &import_items_list)) {
[email protected]7c983cc2010-07-16 11:33:342488 AutomationJSONReply(this, reply_message).SendError(
2489 "Incorrect type for one or more of the arguments.");
[email protected]c5aa5322010-07-15 19:00:072490 return;
2491 }
2492
2493 int num_items = import_items_list->GetSize();
2494 for (int i = 0; i < num_items; i++) {
2495 std::string item;
2496 import_items_list->GetString(i, &item);
2497 // If the provided string is not part of the map, error out.
2498 if (!ContainsKey(string_to_import_item, item)) {
[email protected]7c983cc2010-07-16 11:33:342499 AutomationJSONReply(this, reply_message).SendError(
2500 "Invalid item string found in import_items.");
[email protected]c5aa5322010-07-15 19:00:072501 return;
2502 }
2503 import_items |= string_to_import_item[item];
2504 }
2505
2506 ImporterHost* importer_host = new ImporterHost();
2507 // Get the correct ProfileInfo based on the browser they user provided.
2508 importer::ProfileInfo profile_info;
2509 int num_browsers = importer_host->GetAvailableProfileCount();
2510 int i = 0;
2511 for ( ; i < num_browsers; i++) {
2512 std::wstring name = importer_host->GetSourceProfileNameAt(i);
2513 if (name == browser_name) {
2514 profile_info = importer_host->GetSourceProfileInfoAt(i);
2515 break;
2516 }
2517 }
2518 // If we made it to the end of the loop, then the input was bad.
2519 if (i == num_browsers) {
[email protected]7c983cc2010-07-16 11:33:342520 AutomationJSONReply(this, reply_message).SendError(
2521 "Invalid browser name string found.");
[email protected]c5aa5322010-07-15 19:00:072522 return;
2523 }
2524
2525 Profile* profile = browser->profile();
2526
2527 importer_host->SetObserver(
2528 new AutomationProviderImportSettingsObserver(this, reply_message));
2529 importer_host->StartImportSettings(profile_info, profile, import_items,
2530 new ProfileWriter(profile), first_run);
2531}
2532
[email protected]95222282010-07-26 17:43:022533namespace {
2534
2535// Translates a dictionary password to a PasswordForm struct.
2536webkit_glue::PasswordForm GetPasswordFormFromDict(
2537 const DictionaryValue& password_dict) {
2538
2539 // If the time is specified, change time to the specified time.
2540 base::Time time = base::Time::Now();
2541 int it;
2542 double dt;
2543 if (password_dict.GetInteger(L"time", &it))
2544 time = base::Time::FromTimeT(it);
2545 else if (password_dict.GetReal(L"time", &dt))
2546 time = base::Time::FromDoubleT(dt);
2547
2548 std::string signon_realm;
2549 string16 username_value;
2550 string16 password_value;
2551 string16 origin_url_text;
2552 string16 username_element;
2553 string16 password_element;
2554 string16 submit_element;
2555 string16 action_target_text;
2556 bool blacklist = false;
2557 string16 old_password_element;
2558 string16 old_password_value;
2559
2560 // We don't care if any of these fail - they are either optional or checked
2561 // before this function is called.
2562 password_dict.GetString(L"signon_realm", &signon_realm);
2563 password_dict.GetStringAsUTF16(L"username_value", &username_value);
2564 password_dict.GetStringAsUTF16(L"password_value", &password_value);
2565 password_dict.GetStringAsUTF16(L"origin_url", &origin_url_text);
2566 password_dict.GetStringAsUTF16(L"username_element", &username_element);
2567 password_dict.GetStringAsUTF16(L"password_element", &password_element);
2568 password_dict.GetStringAsUTF16(L"submit_element", &submit_element);
2569 password_dict.GetStringAsUTF16(L"action_target", &action_target_text);
2570 password_dict.GetBoolean(L"blacklist", &blacklist);
2571
2572 GURL origin_gurl(origin_url_text);
2573 GURL action_target(action_target_text);
2574
2575 webkit_glue::PasswordForm password_form;
2576 password_form.signon_realm = signon_realm;
2577 password_form.username_value = username_value;
2578 password_form.password_value = password_value;
2579 password_form.origin = origin_gurl;
2580 password_form.username_element = username_element;
2581 password_form.password_element = password_element;
2582 password_form.submit_element = submit_element;
2583 password_form.action = action_target;
2584 password_form.blacklisted_by_user = blacklist;
2585 password_form.date_created = time;
2586
2587 return password_form;
2588}
2589
2590} // namespace
2591
[email protected]5bcfe1672010-07-16 20:51:572592// See AddSavedPassword() in chrome/test/functional/pyauto.py for sample json
2593// input.
2594// Sample json output: { "password_added": true }
2595void AutomationProvider::AddSavedPassword(Browser* browser,
2596 DictionaryValue* args,
2597 IPC::Message* reply_message) {
[email protected]5bcfe1672010-07-16 20:51:572598 AutomationJSONReply reply(this, reply_message);
[email protected]95222282010-07-26 17:43:022599 DictionaryValue* password_dict = NULL;
[email protected]5bcfe1672010-07-16 20:51:572600
[email protected]95222282010-07-26 17:43:022601 if (!args->GetDictionary(L"password", &password_dict)) {
2602 reply.SendError("Password must be a dictionary.");
[email protected]5bcfe1672010-07-16 20:51:572603 return;
2604 }
2605
[email protected]95222282010-07-26 17:43:022606 // The signon realm is effectively the primary key and must be included.
2607 // Check here before calling GetPasswordFormFromDict.
2608 if (!password_dict->HasKey(L"signon_realm")) {
2609 reply.SendError("Password must include signon_realm.");
2610 return;
2611 }
2612 webkit_glue::PasswordForm new_password =
2613 GetPasswordFormFromDict(*password_dict);
[email protected]5bcfe1672010-07-16 20:51:572614
2615 Profile* profile = browser->profile();
2616 // Use IMPLICIT_ACCESS since new passwords aren't added off the record.
2617 PasswordStore* password_store =
2618 profile->GetPasswordStore(Profile::IMPLICIT_ACCESS);
2619
2620 // Set the return based on whether setting the password succeeded.
2621 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2622
2623 // It will be null if it's accessed in an incognito window.
2624 if (password_store != NULL) {
2625 password_store->AddLogin(new_password);
2626 return_value->SetBoolean(L"password_added", true);
2627 } else {
2628 return_value->SetBoolean(L"password_added", false);
2629 }
2630
2631 reply.SendSuccess(return_value.get());
2632}
2633
[email protected]95222282010-07-26 17:43:022634// See RemoveSavedPassword() in chrome/test/functional/pyauto.py for sample
2635// json input.
2636// Sample json output: {}
2637void AutomationProvider::RemoveSavedPassword(Browser* browser,
2638 DictionaryValue* args,
2639 IPC::Message* reply_message) {
2640 AutomationJSONReply reply(this, reply_message);
2641 DictionaryValue* password_dict = NULL;
2642
2643 if (!args->GetDictionary(L"password", &password_dict)) {
2644 reply.SendError("Password must be a dictionary.");
2645 return;
2646 }
2647
2648 // The signon realm is effectively the primary key and must be included.
2649 // Check here before calling GetPasswordFormFromDict.
2650 if (!password_dict->HasKey(L"signon_realm")) {
2651 reply.SendError("Password must include signon_realm.");
2652 return;
2653 }
2654 webkit_glue::PasswordForm to_remove =
2655 GetPasswordFormFromDict(*password_dict);
2656
2657 Profile* profile = browser->profile();
2658 // Use EXPLICIT_ACCESS since passwords can be removed off the record.
2659 PasswordStore* password_store =
2660 profile->GetPasswordStore(Profile::EXPLICIT_ACCESS);
2661
2662 password_store->RemoveLogin(to_remove);
2663 reply.SendSuccess(NULL);
2664}
2665
[email protected]5bcfe1672010-07-16 20:51:572666// Sample json input: { "command": "GetSavedPasswords" }
2667// Refer to GetSavedPasswords() in chrome/test/pyautolib/pyauto.py for sample
2668// json output.
2669void AutomationProvider::GetSavedPasswords(Browser* browser,
2670 DictionaryValue* args,
2671 IPC::Message* reply_message) {
2672 Profile* profile = browser->profile();
2673 // Use EXPLICIT_ACCESS since saved passwords can be retreived off the record.
2674 PasswordStore* password_store =
2675 profile->GetPasswordStore(Profile::EXPLICIT_ACCESS);
2676 password_store->GetAutofillableLogins(
2677 new AutomationProviderGetPasswordsObserver(this, reply_message));
2678 // Observer deletes itself after returning.
2679}
2680
[email protected]a0fc50d72010-07-14 21:14:192681// Refer to ClearBrowsingData() in chrome/test/pyautolib/pyauto.py for sample
2682// json input.
2683// Sample json output: {}
2684void AutomationProvider::ClearBrowsingData(Browser* browser,
2685 DictionaryValue* args,
2686 IPC::Message* reply_message) {
[email protected]a0fc50d72010-07-14 21:14:192687 std::map<std::string, BrowsingDataRemover::TimePeriod> string_to_time_period;
2688 string_to_time_period["LAST_HOUR"] = BrowsingDataRemover::LAST_HOUR;
2689 string_to_time_period["LAST_DAY"] = BrowsingDataRemover::LAST_DAY;
2690 string_to_time_period["LAST_WEEK"] = BrowsingDataRemover::LAST_WEEK;
2691 string_to_time_period["FOUR_WEEKS"] = BrowsingDataRemover::FOUR_WEEKS;
2692 string_to_time_period["EVERYTHING"] = BrowsingDataRemover::EVERYTHING;
2693
2694 std::map<std::string, int> string_to_mask_value;
2695 string_to_mask_value["HISTORY"] = BrowsingDataRemover::REMOVE_HISTORY;
2696 string_to_mask_value["DOWNLOADS"] = BrowsingDataRemover::REMOVE_DOWNLOADS;
2697 string_to_mask_value["COOKIES"] = BrowsingDataRemover::REMOVE_COOKIES;
2698 string_to_mask_value["PASSWORDS"] = BrowsingDataRemover::REMOVE_PASSWORDS;
2699 string_to_mask_value["FORM_DATA"] = BrowsingDataRemover::REMOVE_FORM_DATA;
2700 string_to_mask_value["CACHE"] = BrowsingDataRemover::REMOVE_CACHE;
2701
2702 std::string time_period;
2703 ListValue* to_remove;
2704 if (!args->GetString(L"time_period", &time_period) ||
2705 !args->GetList(L"to_remove", &to_remove)) {
[email protected]7c983cc2010-07-16 11:33:342706 AutomationJSONReply(this, reply_message).SendError(
2707 "time_period must be a string and to_remove a list.");
[email protected]a0fc50d72010-07-14 21:14:192708 return;
2709 }
2710
2711 int remove_mask = 0;
2712 int num_removals = to_remove->GetSize();
2713 for (int i = 0; i < num_removals; i++) {
2714 std::string removal;
2715 to_remove->GetString(i, &removal);
2716 // If the provided string is not part of the map, then error out.
2717 if (!ContainsKey(string_to_mask_value, removal)) {
[email protected]7c983cc2010-07-16 11:33:342718 AutomationJSONReply(this, reply_message).SendError(
2719 "Invalid browsing data string found in to_remove.");
[email protected]a0fc50d72010-07-14 21:14:192720 return;
2721 }
2722 remove_mask |= string_to_mask_value[removal];
2723 }
2724
2725 if (!ContainsKey(string_to_time_period, time_period)) {
[email protected]7c983cc2010-07-16 11:33:342726 AutomationJSONReply(this, reply_message).SendError(
2727 "Invalid string for time_period.");
[email protected]a0fc50d72010-07-14 21:14:192728 return;
2729 }
2730
2731 BrowsingDataRemover* remover = new BrowsingDataRemover(
2732 profile(), string_to_time_period[time_period], base::Time());
2733
2734 remover->AddObserver(
2735 new AutomationProviderBrowsingDataObserver(this, reply_message));
2736 remover->Remove(remove_mask);
2737 // BrowsingDataRemover deletes itself using DeleteTask.
2738 // The observer also deletes itself after sending the reply.
2739}
2740
[email protected]3dda5b02010-07-27 16:35:422741namespace {
2742
2743 // Get the TabContents from a dictionary of arguments.
2744 TabContents* GetTabContentsFromDict(const Browser* browser,
2745 const DictionaryValue* args,
2746 std::string* error_message) {
2747 int tab_index;
2748 if (!args->GetInteger(L"tab_index", &tab_index)) {
2749 *error_message = "Must include tab_index.";
2750 return NULL;
2751 }
2752
2753 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2754 if (!tab_contents) {
2755 *error_message = StringPrintf("No tab at index %d.", tab_index);
2756 return NULL;
2757 }
2758 return tab_contents;
2759 }
2760
2761 // Get the TranslateInfoBarDelegate from TabContents.
2762 TranslateInfoBarDelegate* GetTranslateInfoBarDelegate(
2763 TabContents* tab_contents) {
2764 for (int i = 0; i < tab_contents->infobar_delegate_count(); i++) {
2765 InfoBarDelegate* infobar = tab_contents->GetInfoBarDelegateAt(i);
2766 if (infobar->AsTranslateInfoBarDelegate())
2767 return infobar->AsTranslateInfoBarDelegate();
2768 }
2769 // No translate infobar.
2770 return NULL;
2771 }
2772
2773} // namespace
2774
2775// See GetTranslateInfo() in chrome/test/pyautolib/pyauto.py for sample json
2776// input and output.
2777void AutomationProvider::GetTranslateInfo(Browser* browser,
2778 DictionaryValue* args,
2779 IPC::Message* reply_message) {
2780 std::string error_message;
2781 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2782 &error_message);
2783 if (!tab_contents) {
2784 AutomationJSONReply(this, reply_message).SendError(error_message);
2785 return;
2786 }
2787
2788 // Get the translate bar if there is one and pass it to the observer.
2789 // The observer will check for null and populate the information accordingly.
2790 TranslateInfoBarDelegate* translate_bar =
2791 GetTranslateInfoBarDelegate(tab_contents);
2792
2793 TabLanguageDeterminedObserver* observer = new TabLanguageDeterminedObserver(
2794 this, reply_message, tab_contents, translate_bar);
2795 // If the language for the page hasn't been loaded yet, then just make
2796 // the observer, otherwise call observe directly.
2797 std::string language = tab_contents->language_state().original_language();
2798 if (!language.empty()) {
2799 observer->Observe(NotificationType::TAB_LANGUAGE_DETERMINED,
2800 Source<TabContents>(tab_contents),
2801 Details<std::string>(&language));
2802 }
2803}
2804
2805// See SelectTranslateOption() in chrome/test/pyautolib/pyauto.py for sample
2806// json input.
2807// Sample json output: {}
2808void AutomationProvider::SelectTranslateOption(Browser* browser,
2809 DictionaryValue* args,
2810 IPC::Message* reply_message) {
2811 std::string option;
2812 std::string error_message;
2813 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2814 &error_message);
2815 if (!tab_contents) {
2816 AutomationJSONReply(this, reply_message).SendError(error_message);
2817 return;
2818 }
2819
2820 TranslateInfoBarDelegate* translate_bar =
2821 GetTranslateInfoBarDelegate(tab_contents);
2822 if (!translate_bar) {
2823 AutomationJSONReply(this, reply_message)
2824 .SendError("There is no translate bar open.");
2825 return;
2826 }
2827
2828 if (!args->GetString(L"option", &option)) {
2829 AutomationJSONReply(this, reply_message).SendError("Must include option");
2830 return;
2831 }
2832
2833 if (option == "translate_page") {
2834 // Make a new notification observer which will send the reply.
2835 new PageTranslatedObserver(this, reply_message, tab_contents);
2836 translate_bar->Translate();
2837 return;
2838 }
2839
2840 AutomationJSONReply reply(this, reply_message);
2841 if (option == "never_translate_language") {
2842 if (translate_bar->IsLanguageBlacklisted()) {
2843 reply.SendError("The language was already blacklisted.");
2844 return;
2845 }
2846 translate_bar->ToggleLanguageBlacklist();
2847 reply.SendSuccess(NULL);
2848 } else if (option == "never_translate_site") {
2849 if (translate_bar->IsSiteBlacklisted()) {
2850 reply.SendError("The site was already blacklisted.");
2851 return;
2852 }
2853 translate_bar->ToggleSiteBlacklist();
2854 reply.SendSuccess(NULL);
2855 } else if (option == "toggle_always_translate") {
2856 translate_bar->ToggleAlwaysTranslate();
2857 reply.SendSuccess(NULL);
2858 } else if (option == "revert_translation") {
2859 translate_bar->RevertTranslation();
2860 reply.SendSuccess(NULL);
2861 } else {
2862 reply.SendError("Invalid string found for option.");
2863 }
2864}
2865
[email protected]f89ee5d2010-08-02 16:39:122866// See WaitUntilTranslateComplete() in chrome/test/pyautolib/pyauto.py for
2867// sample json input and output.
2868void AutomationProvider::WaitUntilTranslateComplete(
2869 Browser* browser, DictionaryValue* args, IPC::Message* reply_message) {
2870 std::string error_message;
2871 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2872 &error_message);
2873 if (!tab_contents) {
2874 AutomationJSONReply(this, reply_message).SendError(error_message);
2875 return;
2876 }
2877
2878 // If the translation is still pending, the observer will wait
2879 // for it to finish and then reply.
2880 if (tab_contents->language_state().translation_pending()) {
2881 new PageTranslatedObserver(this, reply_message, tab_contents);
2882 return;
2883 }
2884 // Otherwise send back the success or failure of the attempted translation
2885 // based on the translate bar state.
2886 AutomationJSONReply reply(this, reply_message);
2887 TranslateInfoBarDelegate* translate_bar =
2888 GetTranslateInfoBarDelegate(tab_contents);
2889 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2890 if (!translate_bar) {
2891 return_value->SetBoolean(L"translation_success", false);
2892 } else {
2893 return_value->SetBoolean(
2894 L"translation_success",
2895 translate_bar->type() == TranslateInfoBarDelegate::AFTER_TRANSLATE);
2896 }
2897 reply.SendSuccess(return_value.get());
2898}
2899
[email protected]4d1929f12010-07-10 00:09:032900// Sample json input: { "command": "GetThemeInfo" }
2901// Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output.
2902void AutomationProvider::GetThemeInfo(Browser* browser,
2903 DictionaryValue* args,
2904 IPC::Message* reply_message) {
2905 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2906 Extension* theme = browser->profile()->GetTheme();
2907 if (theme) {
2908 return_value->SetString(L"name", theme->name());
2909 return_value->Set(L"images", theme->GetThemeImages()->DeepCopy());
2910 return_value->Set(L"colors", theme->GetThemeColors()->DeepCopy());
2911 return_value->Set(L"tints", theme->GetThemeTints()->DeepCopy());
2912 }
[email protected]7c983cc2010-07-16 11:33:342913 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]4d1929f12010-07-10 00:09:032914}
2915
[email protected]55846ad842010-07-09 18:22:562916// Sample json input:
2917// { "command": "GetAutoFillProfile" }
2918// Refer to GetAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2919// json output.
2920void AutomationProvider::GetAutoFillProfile(Browser* browser,
2921 DictionaryValue* args,
2922 IPC::Message* reply_message) {
[email protected]55846ad842010-07-09 18:22:562923 // Get the AutoFillProfiles currently in the database.
2924 int tab_index = 0;
2925 args->GetInteger(L"tab_index", &tab_index);
2926 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
[email protected]7c983cc2010-07-16 11:33:342927 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:562928
2929 if (tab_contents) {
2930 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2931 ->GetPersonalDataManager();
2932 if (pdm) {
2933 std::vector<AutoFillProfile*> autofill_profiles = pdm->profiles();
2934 std::vector<CreditCard*> credit_cards = pdm->credit_cards();
2935
2936 ListValue* profiles = GetListFromAutoFillProfiles(autofill_profiles);
2937 ListValue* cards = GetListFromCreditCards(credit_cards);
2938
2939 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2940
2941 return_value->Set(L"profiles", profiles);
2942 return_value->Set(L"credit_cards", cards);
[email protected]7c983cc2010-07-16 11:33:342943 reply.SendSuccess(return_value.get());
[email protected]55846ad842010-07-09 18:22:562944 } else {
[email protected]7c983cc2010-07-16 11:33:342945 reply.SendError("No PersonalDataManager.");
2946 return;
[email protected]55846ad842010-07-09 18:22:562947 }
2948 } else {
[email protected]7c983cc2010-07-16 11:33:342949 reply.SendError("No tab at that index.");
2950 return;
[email protected]55846ad842010-07-09 18:22:562951 }
[email protected]55846ad842010-07-09 18:22:562952}
2953
2954// Refer to FillAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2955// json input.
2956// Sample json output: {}
2957void AutomationProvider::FillAutoFillProfile(Browser* browser,
2958 DictionaryValue* args,
2959 IPC::Message* reply_message) {
[email protected]7c983cc2010-07-16 11:33:342960 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:562961 ListValue* profiles = NULL;
2962 ListValue* cards = NULL;
2963 args->GetList(L"profiles", &profiles);
2964 args->GetList(L"credit_cards", &cards);
[email protected]7c983cc2010-07-16 11:33:342965 std::string error_mesg;
[email protected]55846ad842010-07-09 18:22:562966
2967 std::vector<AutoFillProfile> autofill_profiles;
2968 std::vector<CreditCard> credit_cards;
2969 // Create an AutoFillProfile for each of the dictionary profiles.
2970 if (profiles) {
[email protected]7c983cc2010-07-16 11:33:342971 autofill_profiles = GetAutoFillProfilesFromList(*profiles, &error_mesg);
[email protected]55846ad842010-07-09 18:22:562972 }
2973 // Create a CreditCard for each of the dictionary values.
2974 if (cards) {
[email protected]7c983cc2010-07-16 11:33:342975 credit_cards = GetCreditCardsFromList(*cards, &error_mesg);
2976 }
2977 if (!error_mesg.empty()) {
2978 reply.SendError(error_mesg);
2979 return;
[email protected]55846ad842010-07-09 18:22:562980 }
2981
2982 // Save the AutoFillProfiles.
2983 int tab_index = 0;
2984 args->GetInteger(L"tab_index", &tab_index);
2985 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2986
2987 if (tab_contents) {
[email protected]ce6b0122010-07-19 19:07:302988 PersonalDataManager* pdm = tab_contents->profile()
[email protected]55846ad842010-07-09 18:22:562989 ->GetPersonalDataManager();
2990 if (pdm) {
2991 pdm->OnAutoFillDialogApply(profiles? &autofill_profiles : NULL,
2992 cards? &credit_cards : NULL);
2993 } else {
[email protected]7c983cc2010-07-16 11:33:342994 reply.SendError("No PersonalDataManager.");
2995 return;
[email protected]55846ad842010-07-09 18:22:562996 }
2997 } else {
[email protected]7c983cc2010-07-16 11:33:342998 reply.SendError("No tab at that index.");
2999 return;
[email protected]55846ad842010-07-09 18:22:563000 }
[email protected]7c983cc2010-07-16 11:33:343001 reply.SendSuccess(NULL);
[email protected]55846ad842010-07-09 18:22:563002}
3003
3004/* static */
3005ListValue* AutomationProvider::GetListFromAutoFillProfiles(
3006 std::vector<AutoFillProfile*> autofill_profiles) {
3007 ListValue* profiles = new ListValue;
3008
3009 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string
3010 = GetAutoFillFieldToStringMap();
3011
3012 // For each AutoFillProfile, transform it to a dictionary object to return.
3013 for (std::vector<AutoFillProfile*>::iterator it = autofill_profiles.begin();
3014 it != autofill_profiles.end(); ++it) {
3015 AutoFillProfile* profile = *it;
3016 DictionaryValue* profile_info = new DictionaryValue;
3017 profile_info->SetStringFromUTF16(L"label", profile->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
3020 type_it = autofill_type_to_string.begin();
3021 type_it != autofill_type_to_string.end(); ++type_it) {
3022 string16 value = profile->GetFieldText(AutoFillType(type_it->first));
3023 if (value.length()) { // If there was something stored for that value.
3024 profile_info->SetStringFromUTF16(type_it->second, value);
3025 }
3026 }
3027 profiles->Append(profile_info);
3028 }
3029 return profiles;
3030}
3031
3032/* static */
3033ListValue* AutomationProvider::GetListFromCreditCards(
3034 std::vector<CreditCard*> credit_cards) {
3035 ListValue* cards = new ListValue;
3036
3037 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
3038 GetCreditCardFieldToStringMap();
3039
3040 // For each AutoFillProfile, transform it to a dictionary object to return.
3041 for (std::vector<CreditCard*>::iterator it = credit_cards.begin();
3042 it != credit_cards.end(); ++it) {
3043 CreditCard* card = *it;
3044 DictionaryValue* card_info = new DictionaryValue;
3045 card_info->SetStringFromUTF16(L"label", card->Label());
3046 // For each of the types, if it has a value, add it to the dictionary.
3047 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
3048 credit_card_type_to_string.begin();
3049 type_it != credit_card_type_to_string.end(); ++type_it) {
3050 string16 value = card->GetFieldText(AutoFillType(type_it->first));
3051 // If there was something stored for that value.
3052 if (value.length()) {
3053 card_info->SetStringFromUTF16(type_it->second, value);
3054 }
3055 }
3056 cards->Append(card_info);
3057 }
3058 return cards;
3059}
3060
3061/* static */
3062std::vector<AutoFillProfile> AutomationProvider::GetAutoFillProfilesFromList(
[email protected]7c983cc2010-07-16 11:33:343063 const ListValue& profiles, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:563064 std::vector<AutoFillProfile> autofill_profiles;
3065 DictionaryValue* profile_info = NULL;
3066 string16 profile_label;
3067 string16 current_value;
3068
3069 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string =
3070 GetAutoFillFieldToStringMap();
3071
3072 int num_profiles = profiles.GetSize();
3073 for (int i = 0; i < num_profiles; i++) {
3074 profiles.GetDictionary(i, &profile_info);
3075 profile_info->GetString("label", &profile_label);
3076 // Choose an id of 0 so that a unique id will be created.
3077 AutoFillProfile profile(profile_label, 0);
3078 // Loop through the possible profile types and add those provided.
3079 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
3080 autofill_type_to_string.begin();
3081 type_it != autofill_type_to_string.end(); ++type_it) {
3082 if (profile_info->HasKey(type_it->second)) {
3083 if (profile_info->GetStringAsUTF16(type_it->second, &current_value)) {
3084 profile.SetInfo(AutoFillType(type_it->first), current_value);
3085 } else {
[email protected]7c983cc2010-07-16 11:33:343086 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:563087 break;
3088 }
3089 }
3090 }
3091 autofill_profiles.push_back(profile);
3092 }
3093 return autofill_profiles;
3094}
3095
3096/* static */
3097std::vector<CreditCard> AutomationProvider::GetCreditCardsFromList(
[email protected]7c983cc2010-07-16 11:33:343098 const ListValue& cards, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:563099 std::vector<CreditCard> credit_cards;
3100 DictionaryValue* card_info = NULL;
3101 string16 card_label;
3102 string16 current_value;
3103
3104 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
3105 GetCreditCardFieldToStringMap();
3106
3107 int num_credit_cards = cards.GetSize();
3108 for (int i = 0; i < num_credit_cards; i++) {
3109 cards.GetDictionary(i, &card_info);
3110 card_info->GetString("label", &card_label);
3111 CreditCard card(card_label, 0);
3112 // Loop through the possible credit card fields and add those provided.
3113 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
3114 credit_card_type_to_string.begin();
3115 type_it != credit_card_type_to_string.end(); ++type_it) {
3116 if (card_info->HasKey(type_it->second)) {
3117 if (card_info->GetStringAsUTF16(type_it->second, &current_value)) {
3118 card.SetInfo(AutoFillType(type_it->first), current_value);
3119 } else {
[email protected]7c983cc2010-07-16 11:33:343120 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:563121 break;
3122 }
3123 }
3124 }
3125 credit_cards.push_back(card);
3126 }
3127 return credit_cards;
3128}
3129
3130/* static */
3131std::map<AutoFillFieldType, std::wstring>
3132 AutomationProvider::GetAutoFillFieldToStringMap() {
3133 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string;
3134 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
3135 autofill_type_to_string[NAME_MIDDLE] = L"NAME_MIDDLE";
3136 autofill_type_to_string[NAME_LAST] = L"NAME_LAST";
3137 autofill_type_to_string[COMPANY_NAME] = L"COMPANY_NAME";
3138 autofill_type_to_string[EMAIL_ADDRESS] = L"EMAIL_ADDRESS";
3139 autofill_type_to_string[ADDRESS_HOME_LINE1] = L"ADDRESS_HOME_LINE1";
3140 autofill_type_to_string[ADDRESS_HOME_LINE2] = L"ADDRESS_HOME_LINE2";
3141 autofill_type_to_string[ADDRESS_HOME_CITY] = L"ADDRESS_HOME_CITY";
3142 autofill_type_to_string[ADDRESS_HOME_STATE] = L"ADDRESS_HOME_STATE";
3143 autofill_type_to_string[ADDRESS_HOME_ZIP] = L"ADDRESS_HOME_ZIP";
3144 autofill_type_to_string[ADDRESS_HOME_COUNTRY] = L"ADDRESS_HOME_COUNTRY";
3145 autofill_type_to_string[PHONE_HOME_NUMBER] = L"PHONE_HOME_NUMBER";
3146 autofill_type_to_string[PHONE_FAX_NUMBER] = L"PHONE_FAX_NUMBER";
3147 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
3148 return autofill_type_to_string;
3149}
3150
3151/* static */
3152std::map<AutoFillFieldType, std::wstring>
3153 AutomationProvider::GetCreditCardFieldToStringMap() {
3154 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string;
3155 credit_card_type_to_string[CREDIT_CARD_NAME] = L"CREDIT_CARD_NAME";
3156 credit_card_type_to_string[CREDIT_CARD_NUMBER] = L"CREDIT_CARD_NUMBER";
3157 credit_card_type_to_string[CREDIT_CARD_TYPE] = L"CREDIT_CARD_TYPE";
3158 credit_card_type_to_string[CREDIT_CARD_EXP_MONTH] = L"CREDIT_CARD_EXP_MONTH";
3159 credit_card_type_to_string[CREDIT_CARD_EXP_4_DIGIT_YEAR] =
3160 L"CREDIT_CARD_EXP_4_DIGIT_YEAR";
3161 return credit_card_type_to_string;
3162}
3163
[email protected]53329582010-05-14 21:10:583164void AutomationProvider::SendJSONRequest(int handle,
3165 std::string json_request,
3166 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:043167 Browser* browser = NULL;
[email protected]59a611242010-04-02 02:24:043168 scoped_ptr<Value> values;
3169
3170 // Basic error checking.
3171 if (browser_tracker_->ContainsHandle(handle)) {
3172 browser = browser_tracker_->GetResource(handle);
3173 }
3174 if (!browser) {
[email protected]7c983cc2010-07-16 11:33:343175 AutomationJSONReply(this, reply_message).SendError("no browser object");
3176 return;
3177 }
3178 base::JSONReader reader;
3179 std::string error;
3180 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
3181 if (!error.empty()) {
3182 AutomationJSONReply(this, reply_message).SendError(error);
3183 return;
[email protected]59a611242010-04-02 02:24:043184 }
3185
3186 // Make sure input is a dict with a string command.
3187 std::string command;
3188 DictionaryValue* dict_value = NULL;
[email protected]7c983cc2010-07-16 11:33:343189 if (values->GetType() != Value::TYPE_DICTIONARY) {
3190 AutomationJSONReply(this, reply_message).SendError("not a dict");
3191 return;
3192 }
3193 // Ownership remains with "values" variable.
3194 dict_value = static_cast<DictionaryValue*>(values.get());
3195 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
3196 AutomationJSONReply(this, reply_message).SendError(
3197 "no command key in dict or not a string command");
3198 return;
[email protected]59a611242010-04-02 02:24:043199 }
3200
[email protected]24e2b102010-04-29 17:56:473201 // Map json commands to their handlers.
3202 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:053203 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
3204 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
3205 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
3206
[email protected]a9ff2c02010-05-13 17:33:053207 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
[email protected]7c983cc2010-07-16 11:33:343208
[email protected]38b5a3852010-07-21 06:49:523209 handler_map["WaitForInfobarCount"] = &AutomationProvider::WaitForInfobarCount;
[email protected]e004a2d2010-07-22 04:55:283210 handler_map["PerformActionOnInfobar"] =
3211 &AutomationProvider::PerformActionOnInfobar;
[email protected]38b5a3852010-07-21 06:49:523212
[email protected]24e2b102010-04-29 17:56:473213 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:293214 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:053215
[email protected]53329582010-05-14 21:10:583216 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
3217 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
3218 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
3219 handler_map["OmniboxMovePopupSelection"] =
3220 &AutomationProvider::OmniboxMovePopupSelection;
3221
[email protected]24e2b102010-04-29 17:56:473222 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
3223 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:053224
[email protected]ef413ca2010-05-25 21:09:143225 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
3226
[email protected]f7d48012010-05-06 08:17:053227 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:473228 handler_map["WaitForAllDownloadsToComplete"] =
3229 &AutomationProvider::WaitForDownloadsToComplete;
3230
[email protected]a3cd5022010-06-16 18:25:293231 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
3232
[email protected]7060bb292010-06-24 00:52:493233 handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents;
3234
[email protected]c5aa5322010-07-15 19:00:073235 handler_map["ImportSettings"] = &AutomationProvider::ImportSettings;
3236
[email protected]5bcfe1672010-07-16 20:51:573237 handler_map["AddSavedPassword"] = &AutomationProvider::AddSavedPassword;
[email protected]95222282010-07-26 17:43:023238 handler_map["RemoveSavedPassword"] =
3239 &AutomationProvider::RemoveSavedPassword;
[email protected]5bcfe1672010-07-16 20:51:573240 handler_map["GetSavedPasswords"] = &AutomationProvider::GetSavedPasswords;
3241
[email protected]a0fc50d72010-07-14 21:14:193242 handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData;
3243
[email protected]4d1929f12010-07-10 00:09:033244 // SetTheme() implemented using InstallExtension().
3245 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo;
3246
[email protected]3dda5b02010-07-27 16:35:423247 handler_map["SelectTranslateOption"] =
3248 &AutomationProvider::SelectTranslateOption;
3249 handler_map["GetTranslateInfo"] = &AutomationProvider::GetTranslateInfo;
[email protected]f89ee5d2010-08-02 16:39:123250 handler_map["WaitUntilTranslateComplete"] =
3251 &AutomationProvider::WaitUntilTranslateComplete;
[email protected]3dda5b02010-07-27 16:35:423252
[email protected]55846ad842010-07-09 18:22:563253 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile;
3254 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile;
3255
[email protected]7c983cc2010-07-16 11:33:343256 if (handler_map.find(std::string(command)) != handler_map.end()) {
3257 (this->*handler_map[command])(browser, dict_value, reply_message);
[email protected]1ac875d22010-07-16 09:57:583258 } else {
[email protected]7c983cc2010-07-16 11:33:343259 std::string error_string = "Unknown command. Options: ";
3260 for (std::map<std::string, JsonHandler>::const_iterator it =
3261 handler_map.begin(); it != handler_map.end(); ++it) {
3262 error_string += it->first + ", ";
3263 }
3264 AutomationJSONReply(this, reply_message).SendError(error_string);
[email protected]1ac875d22010-07-16 09:57:583265 }
[email protected]59a611242010-04-02 02:24:043266}
3267
initial.commit09911bf2008-07-26 23:55:293268void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:563269 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:343270 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
3271 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:563272 DCHECK(reply_message_ == NULL);
3273 reply_message_ = reply_message;
3274
[email protected]d9f9b792009-06-24 13:17:123275 DevToolsManager::GetInstance()->InspectElement(
3276 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:293277 } else {
[email protected]71f65dd2009-02-11 19:14:563278 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
3279 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293280 }
3281}
3282
3283void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:093284 if (reply_message_) {
3285 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
3286 num_resources);
3287 Send(reply_message_);
3288 reply_message_ = NULL;
3289 }
initial.commit09911bf2008-07-26 23:55:293290}
3291
[email protected]a7eee32f2009-05-22 18:08:173292class SetProxyConfigTask : public Task {
3293 public:
[email protected]be180c802009-10-23 06:33:313294 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
3295 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:253296 : request_context_getter_(request_context_getter),
3297 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:173298 virtual void Run() {
3299 // First, deserialize the JSON string. If this fails, log and bail.
3300 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:393301 std::string error_msg;
3302 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:173303 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
3304 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:393305 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:173306 return;
3307 }
3308
3309 scoped_ptr<DictionaryValue> dict(
3310 static_cast<DictionaryValue*>(root.release()));
3311 // Now put together a proxy configuration from the deserialized string.
3312 net::ProxyConfig pc;
3313 PopulateProxyConfig(*dict.get(), &pc);
3314
[email protected]be180c802009-10-23 06:33:313315 net::ProxyService* proxy_service =
3316 request_context_getter_->GetURLRequestContext()->proxy_service();
3317 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:173318 scoped_ptr<net::ProxyConfigService> proxy_config_service(
3319 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:313320 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:173321 }
3322
3323 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
3324 DCHECK(pc);
3325 bool no_proxy = false;
3326 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
3327 // Make no changes to the ProxyConfig.
3328 return;
3329 }
3330 bool auto_config;
3331 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:483332 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:173333 }
3334 std::string pac_url;
3335 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:483336 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:173337 }
3338 std::string proxy_bypass_list;
3339 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:483340 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:173341 }
3342 std::string proxy_server;
3343 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:483344 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:173345 }
3346 }
3347
3348 private:
[email protected]be180c802009-10-23 06:33:313349 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:173350 std::string proxy_config_;
3351};
3352
3353
3354void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:313355 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
3356 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:173357 FilePath user_data_dir;
3358 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
3359 ProfileManager* profile_manager = g_browser_process->profile_manager();
3360 DCHECK(profile_manager);
3361 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
3362 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:313363 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:173364 }
[email protected]be180c802009-10-23 06:33:313365 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:173366
[email protected]fae20792009-10-28 20:31:583367 ChromeThread::PostTask(
3368 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:313369 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:173370}
3371
[email protected]4f3dc372009-02-24 00:10:293372void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:333373 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:293374 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:293375 if (tab_tracker_->ContainsHandle(handle)) {
3376 NavigationController* tab = tab_tracker_->GetResource(handle);
3377 DownloadManager* dlm = tab->profile()->GetDownloadManager();
3378 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:333379 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:293380 }
initial.commit09911bf2008-07-26 23:55:293381}
3382
[email protected]6a5670d22009-10-27 16:21:343383void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:143384 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:533385 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
3386 reply_message);
3387}
3388
3389void AutomationProvider::OpenNewBrowserWindowOfType(
3390 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:143391 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:293392 // We may have no current browser windows open so don't rely on
3393 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:533394 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
3395 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:053396 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:153397 if (show)
[email protected]15952e462008-11-14 00:29:053398 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:293399}
3400
[email protected]71f65dd2009-02-11 19:14:563401void AutomationProvider::GetWindowForBrowser(int browser_handle,
3402 bool* success,
3403 int* handle) {
3404 *success = false;
3405 *handle = 0;
initial.commit09911bf2008-07-26 23:55:293406
3407 if (browser_tracker_->ContainsHandle(browser_handle)) {
3408 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:203409 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:293410 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:203411 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:563412 *success = true;
initial.commit09911bf2008-07-26 23:55:293413 }
initial.commit09911bf2008-07-26 23:55:293414}
3415
3416void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:563417 int browser_handle,
3418 bool* success,
3419 int* autocomplete_edit_handle) {
3420 *success = false;
3421 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:293422
3423 if (browser_tracker_->ContainsHandle(browser_handle)) {
3424 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:063425 LocationBar* loc_bar = browser->window()->GetLocationBar();
3426 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:293427 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:563428 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
3429 *success = true;
initial.commit09911bf2008-07-26 23:55:293430 }
initial.commit09911bf2008-07-26 23:55:293431}
initial.commit09911bf2008-07-26 23:55:293432
[email protected]71f65dd2009-02-11 19:14:563433void AutomationProvider::ShowInterstitialPage(int tab_handle,
3434 const std::string& html_text,
3435 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293436 if (tab_tracker_->ContainsHandle(tab_handle)) {
3437 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:113438 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:403439
[email protected]7dad3d5f2010-03-04 00:27:013440 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:403441 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:343442 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:403443 GURL("about:interstitial"),
3444 html_text);
3445 interstitial->Show();
3446 return;
initial.commit09911bf2008-07-26 23:55:293447 }
[email protected]71f65dd2009-02-11 19:14:563448
[email protected]457f5cf2009-08-18 16:37:523449 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
3450 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563451 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293452}
3453
[email protected]71f65dd2009-02-11 19:14:563454void AutomationProvider::HideInterstitialPage(int tab_handle,
3455 bool* success) {
3456 *success = false;
[email protected]57c6a652009-05-04 07:58:343457 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
3458 if (tab_contents && tab_contents->interstitial_page()) {
3459 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:563460 *success = true;
initial.commit09911bf2008-07-26 23:55:293461 }
initial.commit09911bf2008-07-26 23:55:293462}
3463
[email protected]71f65dd2009-02-11 19:14:563464void AutomationProvider::CloseTab(int tab_handle,
3465 bool wait_until_closed,
3466 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293467 if (tab_tracker_->ContainsHandle(tab_handle)) {
3468 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
3469 int index;
3470 Browser* browser = Browser::GetBrowserForController(controller, &index);
3471 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:143472 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:113473 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:453474 return;
initial.commit09911bf2008-07-26 23:55:293475 }
[email protected]de246f52009-02-25 18:25:453476
3477 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:553478 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293479}
3480
[email protected]71f65dd2009-02-11 19:14:563481void AutomationProvider::CloseBrowser(int browser_handle,
3482 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293483 if (browser_tracker_->ContainsHandle(browser_handle)) {
3484 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563485 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:563486 reply_message);
[email protected]f3e99e32008-07-30 04:48:393487 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:293488 } else {
3489 NOTREACHED();
3490 }
3491}
3492
[email protected]71f65dd2009-02-11 19:14:563493void AutomationProvider::CloseBrowserAsync(int browser_handle) {
3494 if (browser_tracker_->ContainsHandle(browser_handle)) {
3495 Browser* browser = browser_tracker_->GetResource(browser_handle);
3496 browser->window()->Close();
3497 } else {
3498 NOTREACHED();
3499 }
3500}
3501
[email protected]71f65dd2009-02-11 19:14:563502void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
3503 IPC::Message* reply_message) {
3504 if (tab_tracker_->ContainsHandle(tab_handle)) {
3505 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3506 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:143507 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:563508 }
3509}
3510
[email protected]71f65dd2009-02-11 19:14:563511void AutomationProvider::GetSecurityState(int handle, bool* success,
3512 SecurityStyle* security_style,
3513 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:483514 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:293515 if (tab_tracker_->ContainsHandle(handle)) {
3516 NavigationController* tab = tab_tracker_->GetResource(handle);
3517 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563518 *success = true;
3519 *security_style = entry->ssl().security_style();
3520 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:483521 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:293522 } else {
[email protected]71f65dd2009-02-11 19:14:563523 *success = false;
3524 *security_style = SECURITY_STYLE_UNKNOWN;
3525 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:483526 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:293527 }
3528}
3529
[email protected]71f65dd2009-02-11 19:14:563530void AutomationProvider::GetPageType(int handle, bool* success,
3531 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:293532 if (tab_tracker_->ContainsHandle(handle)) {
3533 NavigationController* tab = tab_tracker_->GetResource(handle);
3534 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563535 *page_type = entry->page_type();
3536 *success = true;
initial.commit09911bf2008-07-26 23:55:293537 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:343538 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:563539 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:343540 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:563541 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293542 } else {
[email protected]71f65dd2009-02-11 19:14:563543 *success = false;
3544 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293545 }
3546}
3547
[email protected]84abba62009-10-07 17:01:443548void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
3549 int* duration_ms) {
3550 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
3551 event_name);
3552}
3553
[email protected]71f65dd2009-02-11 19:14:563554void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
3555 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293556 if (tab_tracker_->ContainsHandle(handle)) {
3557 NavigationController* tab = tab_tracker_->GetResource(handle);
3558 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:073559 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:403560 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:473561 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:343562 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:293563 if (ssl_blocking_page) {
3564 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:013565 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:563566 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:293567 return;
3568 }
3569 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:523570 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3571 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:563572 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293573 return;
3574 }
3575 }
3576 }
3577 // We failed.
[email protected]457f5cf2009-08-18 16:37:523578 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3579 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563580 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293581}
3582
[email protected]71f65dd2009-02-11 19:14:563583void AutomationProvider::BringBrowserToFront(int browser_handle,
3584 bool* success) {
initial.commit09911bf2008-07-26 23:55:293585 if (browser_tracker_->ContainsHandle(browser_handle)) {
3586 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:063587 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:563588 *success = true;
initial.commit09911bf2008-07-26 23:55:293589 } else {
[email protected]71f65dd2009-02-11 19:14:563590 *success = false;
initial.commit09911bf2008-07-26 23:55:293591 }
3592}
3593
[email protected]bdb7ff62010-07-20 01:56:523594void AutomationProvider::IsMenuCommandEnabled(int browser_handle,
3595 int message_num,
3596 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:293597 if (browser_tracker_->ContainsHandle(browser_handle)) {
3598 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563599 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:143600 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:293601 } else {
[email protected]71f65dd2009-02-11 19:14:563602 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:293603 }
3604}
3605
[email protected]71f65dd2009-02-11 19:14:563606void AutomationProvider::PrintNow(int tab_handle,
3607 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:573608 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:343609 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3610 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:293611 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:573612 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:143613 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:343614 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:573615 return;
initial.commit09911bf2008-07-26 23:55:293616 }
[email protected]71f65dd2009-02-11 19:14:563617 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
3618 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293619}
[email protected]d301c952009-07-13 15:02:413620
[email protected]71f65dd2009-02-11 19:14:563621void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:113622 const FilePath& file_name,
3623 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:563624 int type,
3625 bool* success) {
initial.commit09911bf2008-07-26 23:55:293626 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:563627 *success = false;
initial.commit09911bf2008-07-26 23:55:293628 return;
3629 }
3630
3631 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3632 Browser* browser = FindAndActivateTab(nav);
3633 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:143634 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:563635 *success = false;
initial.commit09911bf2008-07-26 23:55:293636 return;
3637 }
3638
initial.commit09911bf2008-07-26 23:55:293639 SavePackage::SavePackageType save_type =
3640 static_cast<SavePackage::SavePackageType>(type);
3641 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
3642 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:343643 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:293644
[email protected]71f65dd2009-02-11 19:14:563645 *success = true;
initial.commit09911bf2008-07-26 23:55:293646}
3647
[email protected]71f65dd2009-02-11 19:14:563648void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
3649 bool* success,
3650 std::wstring* text) {
3651 *success = false;
initial.commit09911bf2008-07-26 23:55:293652 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563653 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:523654 GetText();
[email protected]71f65dd2009-02-11 19:14:563655 *success = true;
initial.commit09911bf2008-07-26 23:55:293656 }
initial.commit09911bf2008-07-26 23:55:293657}
3658
[email protected]71f65dd2009-02-11 19:14:563659void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
3660 const std::wstring& text,
3661 bool* success) {
3662 *success = false;
initial.commit09911bf2008-07-26 23:55:293663 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:523664 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
3665 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:563666 *success = true;
initial.commit09911bf2008-07-26 23:55:293667 }
initial.commit09911bf2008-07-26 23:55:293668}
3669
3670void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:563671 int autocomplete_edit_handle,
3672 bool* success,
3673 std::vector<AutocompleteMatchData>* matches) {
3674 *success = false;
initial.commit09911bf2008-07-26 23:55:293675 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:273676 const AutocompleteResult& result = autocomplete_edit_tracker_->
3677 GetResource(autocomplete_edit_handle)->model()->result();
3678 for (AutocompleteResult::const_iterator i = result.begin();
3679 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:563680 matches->push_back(AutocompleteMatchData(*i));
3681 *success = true;
initial.commit09911bf2008-07-26 23:55:293682 }
initial.commit09911bf2008-07-26 23:55:293683}
3684
3685void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:563686 int autocomplete_edit_handle,
3687 bool* success,
3688 bool* query_in_progress) {
3689 *success = false;
3690 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:293691 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563692 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:523693 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:563694 *success = true;
initial.commit09911bf2008-07-26 23:55:293695 }
initial.commit09911bf2008-07-26 23:55:293696}
3697
[email protected]63514af2010-03-30 17:17:233698#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:193699
[email protected]5ae5bed2009-08-21 18:52:443700#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:393701
[email protected]57c6a652009-05-04 07:58:343702TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:573703 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:573704 if (tab_tracker_->ContainsHandle(handle)) {
3705 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:343706 if (tab)
3707 *tab = nav_controller;
3708 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:573709 }
[email protected]57c6a652009-05-04 07:58:343710 return NULL;
[email protected]20e93d12008-08-28 16:31:573711}
3712
initial.commit09911bf2008-07-26 23:55:293713TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
3714 : AutomationProvider(profile) {
3715 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:143716 registrar_.Add(this, NotificationType::SESSION_END,
3717 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:293718}
3719
3720TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:293721 BrowserList::RemoveObserver(this);
3722}
3723
3724void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:143725 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:293726 AutomationProvider::OnChannelError();
3727}
3728
[email protected]679082052010-07-21 21:30:133729void TestingAutomationProvider::OnBrowserAdded(const Browser* browser) {
3730}
3731
initial.commit09911bf2008-07-26 23:55:293732void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
3733 // For backwards compatibility with the testing automation interface, we
3734 // want the automation provider (and hence the process) to go away when the
3735 // last browser goes away.
[email protected]c984d9f2010-07-20 20:52:203736 if (BrowserList::size() == 1 && !CommandLine::ForCurrentProcess()->HasSwitch(
3737 switches::kKeepAliveForTest)) {
[email protected]4f3dc372009-02-24 00:10:293738 // If you change this, update Observer for NotificationType::SESSION_END
3739 // below.
[email protected]295039bd2008-08-15 04:32:573740 MessageLoop::current()->PostTask(FROM_HERE,
3741 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:293742 }
3743}
3744
3745void TestingAutomationProvider::Observe(NotificationType type,
3746 const NotificationSource& source,
3747 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:563748 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:293749 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
3750 // before the task runs resulting in this object not being deleted. This
3751 // Release balance out the Release scheduled by OnBrowserRemoving.
3752 Release();
3753}
[email protected]295039bd2008-08-15 04:32:573754
3755void TestingAutomationProvider::OnRemoveProvider() {
3756 AutomationProviderList::GetInstance()->RemoveProvider(this);
3757}
[email protected]8a3422c92008-09-24 17:42:423758
[email protected]816633a2009-11-11 21:48:183759void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:563760 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:423761 if (tab_tracker_->ContainsHandle(handle)) {
3762 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:343763 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:113764 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423765 }
[email protected]8a3422c92008-09-24 17:42:423766}
3767
[email protected]816633a2009-11-11 21:48:183768void AutomationProvider::ClickInfoBarAccept(int handle,
3769 int info_bar_index,
3770 bool wait_for_navigation,
3771 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:423772 bool success = false;
3773 if (tab_tracker_->ContainsHandle(handle)) {
3774 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3775 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:113776 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423777 if (info_bar_index >= 0 && info_bar_index < count) {
3778 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:013779 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:423780 }
[email protected]eb9ba192008-12-02 02:41:343781 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:113782 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:343783 info_bar_index);
3784 if (delegate->AsConfirmInfoBarDelegate())
3785 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:423786 success = true;
3787 }
3788 }
[email protected]4f3dc372009-02-24 00:10:293789 }
[email protected]58f622a62009-10-04 01:17:553790
3791 // This "!wait_for_navigation || !success condition" logic looks suspicious.
3792 // It will send a failure message when success is true but
3793 // |wait_for_navigation| is false.
3794 // TODO(phajdan.jr): investgate whether the reply param (currently
3795 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:423796 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:183797 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:523798 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:423799}
3800
[email protected]71f65dd2009-02-11 19:14:563801void AutomationProvider::GetLastNavigationTime(int handle,
3802 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:423803 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:563804 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:423805}
3806
[email protected]71f65dd2009-02-11 19:14:563807void AutomationProvider::WaitForNavigation(int handle,
3808 int64 last_navigation_time,
3809 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:253810 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:423811 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:253812
[email protected]8a3422c92008-09-24 17:42:423813 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:563814 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:523815 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
3816 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:553817 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:293818 return;
[email protected]8a3422c92008-09-24 17:42:423819 }
3820
[email protected]7dad3d5f2010-03-04 00:27:013821 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:423822}
3823
[email protected]71f65dd2009-02-11 19:14:563824void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163825 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563826 int value,
3827 bool* success) {
3828 *success = false;
[email protected]8a3422c92008-09-24 17:42:423829 if (browser_tracker_->ContainsHandle(handle)) {
3830 Browser* browser = browser_tracker_->GetResource(handle);
3831 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563832 *success = true;
[email protected]8a3422c92008-09-24 17:42:423833 }
[email protected]8a3422c92008-09-24 17:42:423834}
[email protected]97fa6ce32008-12-19 01:48:163835
[email protected]71f65dd2009-02-11 19:14:563836void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163837 const std::wstring& name,
[email protected]ddd231e2010-06-29 20:35:193838 const std::string& value,
[email protected]71f65dd2009-02-11 19:14:563839 bool* success) {
3840 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163841 if (browser_tracker_->ContainsHandle(handle)) {
3842 Browser* browser = browser_tracker_->GetResource(handle);
3843 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563844 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163845 }
[email protected]97fa6ce32008-12-19 01:48:163846}
3847
[email protected]71f65dd2009-02-11 19:14:563848void AutomationProvider::GetBooleanPreference(int handle,
3849 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:363850 bool* success,
3851 bool* value) {
[email protected]71f65dd2009-02-11 19:14:563852 *success = false;
3853 *value = false;
[email protected]97fa6ce32008-12-19 01:48:163854 if (browser_tracker_->ContainsHandle(handle)) {
3855 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:563856 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
3857 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163858 }
[email protected]97fa6ce32008-12-19 01:48:163859}
3860
[email protected]71f65dd2009-02-11 19:14:563861void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163862 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563863 bool value,
3864 bool* success) {
3865 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163866 if (browser_tracker_->ContainsHandle(handle)) {
3867 Browser* browser = browser_tracker_->GetResource(handle);
3868 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563869 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163870 }
[email protected]97fa6ce32008-12-19 01:48:163871}
3872
3873// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563874void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:403875 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:163876 if (tab_tracker_->ContainsHandle(tab_handle)) {
3877 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3878 Browser* browser = FindAndActivateTab(nav);
3879 DCHECK(browser);
3880
[email protected]57c6a652009-05-04 07:58:343881 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
3882 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:163883 }
[email protected]97fa6ce32008-12-19 01:48:163884}
3885
[email protected]b8f48d12009-11-09 20:14:363886// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563887void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:403888 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:563889 bool* success) {
3890 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163891 if (tab_tracker_->ContainsHandle(tab_handle)) {
3892 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:193893 if (!nav)
3894 return;
[email protected]97fa6ce32008-12-19 01:48:163895 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:163896
[email protected]2f2afba2010-04-01 01:53:193897 // If the browser has UI, simulate what a user would do.
3898 // Activate the tab and then click the encoding menu.
3899 if (browser &&
3900 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:163901 int selected_encoding_id =
3902 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
3903 if (selected_encoding_id) {
3904 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:563905 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163906 }
[email protected]2f2afba2010-04-01 01:53:193907 } else {
3908 // There is no UI, Chrome probably runs as Chrome-Frame mode.
3909 // Try to get TabContents and call its override_encoding method.
3910 TabContents* contents = nav->tab_contents();
3911 if (!contents)
3912 return;
3913 const std::string selected_encoding =
3914 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
3915 if (selected_encoding.empty())
3916 return;
3917 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:163918 }
3919 }
[email protected]97fa6ce32008-12-19 01:48:163920}
[email protected]5bcdb312009-01-07 21:43:203921
[email protected]4d434a1a2009-02-11 21:06:573922void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:203923 SavePackage::SetShouldPromptUser(should_prompt);
3924}
[email protected]87eab222009-03-13 00:47:453925
[email protected]66ba4932009-06-04 19:22:133926void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
3927 *count = -1; // -1 is the error code
3928 if (tab_tracker_->ContainsHandle(handle)) {
3929 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3930 TabContents* tab_contents = nav_controller->tab_contents();
3931 if (tab_contents) {
3932 BlockedPopupContainer* container =
3933 tab_contents->blocked_popup_container();
3934 if (container) {
3935 *count = static_cast<int>(container->GetBlockedPopupCount());
3936 } else {
3937 // If we don't have a container, we don't have any blocked popups to
3938 // contain!
3939 *count = 0;
3940 }
3941 }
3942 }
3943}
[email protected]f7a68432009-07-29 23:18:193944
3945void AutomationProvider::SelectAll(int tab_handle) {
3946 RenderViewHost* view = GetViewForTab(tab_handle);
3947 if (!view) {
3948 NOTREACHED();
3949 return;
3950 }
3951
3952 view->SelectAll();
3953}
3954
3955void AutomationProvider::Cut(int tab_handle) {
3956 RenderViewHost* view = GetViewForTab(tab_handle);
3957 if (!view) {
3958 NOTREACHED();
3959 return;
3960 }
3961
3962 view->Cut();
3963}
3964
3965void AutomationProvider::Copy(int tab_handle) {
3966 RenderViewHost* view = GetViewForTab(tab_handle);
3967 if (!view) {
3968 NOTREACHED();
3969 return;
3970 }
3971
3972 view->Copy();
3973}
3974
3975void AutomationProvider::Paste(int tab_handle) {
3976 RenderViewHost* view = GetViewForTab(tab_handle);
3977 if (!view) {
3978 NOTREACHED();
3979 return;
3980 }
3981
3982 view->Paste();
3983}
3984
3985void AutomationProvider::ReloadAsync(int tab_handle) {
3986 if (tab_tracker_->ContainsHandle(tab_handle)) {
3987 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3988 if (!tab) {
3989 NOTREACHED();
3990 return;
3991 }
3992
[email protected]106a0812010-03-18 00:15:123993 const bool check_for_repost = true;
3994 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193995 }
3996}
3997
3998void AutomationProvider::StopAsync(int tab_handle) {
3999 RenderViewHost* view = GetViewForTab(tab_handle);
4000 if (!view) {
[email protected]8b2b3312009-09-14 18:38:364001 // We tolerate StopAsync being called even before a view has been created.
4002 // So just log a warning instead of a NOTREACHED().
4003 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:194004 return;
4005 }
4006
4007 view->Stop();
4008}
4009
[email protected]1bb5f892009-10-06 01:44:574010void AutomationProvider::OnSetPageFontSize(int tab_handle,
4011 int font_size) {
4012 AutomationPageFontSize automation_font_size =
4013 static_cast<AutomationPageFontSize>(font_size);
4014
4015 if (automation_font_size < SMALLEST_FONT ||
4016 automation_font_size > LARGEST_FONT) {
4017 DLOG(ERROR) << "Invalid font size specified : "
4018 << font_size;
4019 return;
4020 }
4021
4022 if (tab_tracker_->ContainsHandle(tab_handle)) {
4023 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
4024 DCHECK(tab != NULL);
4025 if (tab && tab->tab_contents()) {
4026 DCHECK(tab->tab_contents()->profile() != NULL);
4027 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
4028 prefs::kWebKitDefaultFontSize, font_size);
4029 }
4030 }
4031}
4032
[email protected]bc73b4e52010-03-26 04:16:204033void AutomationProvider::RemoveBrowsingData(int remove_mask) {
4034 BrowsingDataRemover* remover;
4035 remover = new BrowsingDataRemover(profile(),
4036 BrowsingDataRemover::EVERYTHING, // All time periods.
4037 base::Time());
4038 remover->Remove(remove_mask);
4039 // BrowsingDataRemover deletes itself.
4040}
[email protected]1bb5f892009-10-06 01:44:574041
[email protected]2949e90d2009-08-21 15:32:524042void AutomationProvider::WaitForBrowserWindowCountToBecome(
4043 int target_count, IPC::Message* reply_message) {
4044 if (static_cast<int>(BrowserList::size()) == target_count) {
4045 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
4046 reply_message, true);
4047 Send(reply_message);
4048 return;
4049 }
4050
4051 // Set up an observer (it will delete itself).
4052 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
4053}
4054
4055void AutomationProvider::WaitForAppModalDialogToBeShown(
4056 IPC::Message* reply_message) {
4057 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
4058 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
4059 reply_message, true);
4060 Send(reply_message);
4061 return;
4062 }
4063
4064 // Set up an observer (it will delete itself).
4065 new AppModalDialogShownObserver(this, reply_message);
4066}
4067
[email protected]1126a1d32009-08-26 15:39:264068void AutomationProvider::GoBackBlockUntilNavigationsComplete(
4069 int handle, int number_of_navigations, IPC::Message* reply_message) {
4070 if (tab_tracker_->ContainsHandle(handle)) {
4071 NavigationController* tab = tab_tracker_->GetResource(handle);
4072 Browser* browser = FindAndActivateTab(tab);
4073 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:014074 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
4075 false);
[email protected]1126a1d32009-08-26 15:39:264076 browser->GoBack(CURRENT_TAB);
4077 return;
4078 }
4079 }
4080
4081 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
4082 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
4083 Send(reply_message);
4084}
4085
4086void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
4087 int handle, int number_of_navigations, IPC::Message* reply_message) {
4088 if (tab_tracker_->ContainsHandle(handle)) {
4089 NavigationController* tab = tab_tracker_->GetResource(handle);
4090 Browser* browser = FindAndActivateTab(tab);
4091 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:014092 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
4093 false);
[email protected]1126a1d32009-08-26 15:39:264094 browser->GoForward(CURRENT_TAB);
4095 return;
4096 }
4097 }
4098
4099 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
4100 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
4101 Send(reply_message);
4102}
4103
[email protected]f7a68432009-07-29 23:18:194104RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
4105 if (tab_tracker_->ContainsHandle(tab_handle)) {
4106 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
4107 if (!tab) {
4108 NOTREACHED();
4109 return NULL;
4110 }
4111
4112 TabContents* tab_contents = tab->tab_contents();
4113 if (!tab_contents) {
4114 NOTREACHED();
4115 return NULL;
4116 }
4117
4118 RenderViewHost* view_host = tab_contents->render_view_host();
4119 return view_host;
4120 }
4121
4122 return NULL;
4123}
[email protected]675595f2009-08-26 22:32:044124
4125void AutomationProvider::GetBrowserForWindow(int window_handle,
4126 bool* success,
4127 int* browser_handle) {
4128 *success = false;
4129 *browser_handle = 0;
4130
4131 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
4132 if (!window)
4133 return;
4134
4135 BrowserList::const_iterator iter = BrowserList::begin();
4136 for (;iter != BrowserList::end(); ++iter) {
4137 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
4138 if (window == this_window) {
4139 // Add() returns the existing handle for the resource if any.
4140 *browser_handle = browser_tracker_->Add(*iter);
4141 *success = true;
4142 return;
4143 }
4144 }
4145}
[email protected]d11c8e92009-10-20 23:26:404146
4147void AutomationProvider::InstallExtension(const FilePath& crx_path,
4148 IPC::Message* reply_message) {
4149 ExtensionsService* service = profile_->GetExtensionsService();
4150 if (service) {
4151 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:194152 new ExtensionInstallNotificationObserver(this,
4153 AutomationMsg_InstallExtension::ID,
4154 reply_message);
[email protected]d11c8e92009-10-20 23:26:404155
4156 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:164157 scoped_refptr<CrxInstaller> installer(
4158 new CrxInstaller(install_dir,
4159 service,
4160 NULL)); // silent install, no UI
4161 installer->set_allow_privilege_increase(true);
4162 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:404163 } else {
4164 AutomationMsg_InstallExtension::WriteReplyParams(
4165 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
4166 Send(reply_message);
4167 }
4168}
4169
4170void AutomationProvider::LoadExpandedExtension(
4171 const FilePath& extension_dir,
4172 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:384173 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:404174 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:194175 new ExtensionInstallNotificationObserver(
4176 this,
4177 AutomationMsg_LoadExpandedExtension::ID,
4178 reply_message);
[email protected]d11c8e92009-10-20 23:26:404179
4180 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:404181 } else {
4182 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
4183 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
4184 Send(reply_message);
4185 }
4186}
[email protected]673fd2c02010-02-04 23:10:004187
[email protected]a1e62d12010-03-16 02:18:434188void AutomationProvider::GetEnabledExtensions(
4189 std::vector<FilePath>* result) {
4190 ExtensionsService* service = profile_->GetExtensionsService();
4191 DCHECK(service);
4192 if (service->extensions_enabled()) {
4193 const ExtensionList* extensions = service->extensions();
4194 DCHECK(extensions);
4195 for (size_t i = 0; i < extensions->size(); ++i) {
4196 Extension* extension = (*extensions)[i];
4197 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:124198 if (extension->location() == Extension::INTERNAL ||
4199 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:074200 result->push_back(extension->path());
4201 }
[email protected]a1e62d12010-03-16 02:18:434202 }
4203 }
4204}
4205
[email protected]790788ac2010-04-06 17:52:194206void AutomationProvider::WaitForExtensionTestResult(
4207 IPC::Message* reply_message) {
4208 DCHECK(reply_message_ == NULL);
4209 reply_message_ = reply_message;
4210 // Call MaybeSendResult, because the result might have come in before
4211 // we were waiting on it.
4212 extension_test_result_observer_->MaybeSendResult();
4213}
4214
4215void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:094216 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:194217 ExtensionsService* service = profile_->GetExtensionsService();
4218 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
4219 if (service && manager) {
4220 // The observer will delete itself when done.
4221 new ExtensionReadyNotificationObserver(
4222 manager,
4223 this,
4224 AutomationMsg_InstallExtensionAndGetHandle::ID,
4225 reply_message);
4226
[email protected]d7e5525d2010-04-20 14:37:094227 ExtensionInstallUI* client =
4228 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:194229 scoped_refptr<CrxInstaller> installer(
4230 new CrxInstaller(service->install_directory(),
4231 service,
[email protected]d7e5525d2010-04-20 14:37:094232 client));
[email protected]790788ac2010-04-06 17:52:194233 installer->set_allow_privilege_increase(true);
4234 installer->InstallCrx(crx_path);
4235 } else {
4236 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
4237 reply_message, 0);
4238 Send(reply_message);
4239 }
4240}
4241
4242void AutomationProvider::UninstallExtension(int extension_handle,
4243 bool* success) {
4244 *success = false;
4245 Extension* extension = GetExtension(extension_handle);
4246 ExtensionsService* service = profile_->GetExtensionsService();
4247 if (extension && service) {
4248 ExtensionUnloadNotificationObserver observer;
4249 service->UninstallExtension(extension->id(), false);
4250 // The extension unload notification should have been sent synchronously
4251 // with the uninstall. Just to be safe, check that it was received.
4252 *success = observer.did_receive_unload_notification();
4253 }
4254}
4255
4256void AutomationProvider::EnableExtension(int extension_handle,
4257 IPC::Message* reply_message) {
4258 Extension* extension = GetDisabledExtension(extension_handle);
4259 ExtensionsService* service = profile_->GetExtensionsService();
4260 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
4261 // Only enable if this extension is disabled.
4262 if (extension && service && manager) {
4263 // The observer will delete itself when done.
4264 new ExtensionReadyNotificationObserver(
4265 manager,
4266 this,
4267 AutomationMsg_EnableExtension::ID,
4268 reply_message);
4269 service->EnableExtension(extension->id());
4270 } else {
4271 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
4272 Send(reply_message);
4273 }
4274}
4275
4276void AutomationProvider::DisableExtension(int extension_handle,
4277 bool* success) {
4278 *success = false;
4279 Extension* extension = GetEnabledExtension(extension_handle);
4280 ExtensionsService* service = profile_->GetExtensionsService();
4281 if (extension && service) {
4282 ExtensionUnloadNotificationObserver observer;
4283 service->DisableExtension(extension->id());
4284 // The extension unload notification should have been sent synchronously
4285 // with the disable. Just to be safe, check that it was received.
4286 *success = observer.did_receive_unload_notification();
4287 }
4288}
4289
4290void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
4291 int extension_handle, int browser_handle,
4292 IPC::Message* reply_message) {
4293 bool success = false;
4294 Extension* extension = GetEnabledExtension(extension_handle);
4295 ExtensionsService* service = profile_->GetExtensionsService();
4296 ExtensionMessageService* message_service =
4297 profile_->GetExtensionMessageService();
4298 Browser* browser = browser_tracker_->GetResource(browser_handle);
4299 if (extension && service && message_service && browser) {
4300 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
4301 if (extension->page_action()) {
4302 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
4303 browser->profile(), extension->id(), "action", tab_id, "", 1);
4304 success = true;
4305 } else if (extension->browser_action()) {
4306 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
4307 browser->profile(), extension->id(), browser);
4308 success = true;
4309 }
4310 }
4311 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
4312 reply_message, success);
4313 Send(reply_message);
4314}
4315
4316void AutomationProvider::MoveExtensionBrowserAction(
4317 int extension_handle, int index, bool* success) {
4318 *success = false;
4319 Extension* extension = GetEnabledExtension(extension_handle);
4320 ExtensionsService* service = profile_->GetExtensionsService();
4321 if (extension && service) {
4322 ExtensionToolbarModel* toolbar = service->toolbar_model();
4323 if (toolbar) {
4324 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
4325 toolbar->MoveBrowserAction(extension, index);
4326 *success = true;
4327 } else {
4328 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
4329 }
4330 }
4331 }
4332}
4333
4334void AutomationProvider::GetExtensionProperty(
4335 int extension_handle,
4336 AutomationMsg_ExtensionProperty type,
4337 bool* success,
4338 std::string* value) {
4339 *success = false;
4340 Extension* extension = GetExtension(extension_handle);
4341 ExtensionsService* service = profile_->GetExtensionsService();
4342 if (extension && service) {
4343 ExtensionToolbarModel* toolbar = service->toolbar_model();
4344 int found_index = -1;
4345 int index = 0;
4346 switch (type) {
4347 case AUTOMATION_MSG_EXTENSION_ID:
4348 *value = extension->id();
4349 *success = true;
4350 break;
4351 case AUTOMATION_MSG_EXTENSION_NAME:
4352 *value = extension->name();
4353 *success = true;
4354 break;
4355 case AUTOMATION_MSG_EXTENSION_VERSION:
4356 *value = extension->VersionString();
4357 *success = true;
4358 break;
4359 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
4360 if (toolbar) {
4361 for (ExtensionList::const_iterator iter = toolbar->begin();
4362 iter != toolbar->end(); iter++) {
4363 // Skip this extension if we are in incognito mode
4364 // and it is not incognito-enabled.
4365 if (profile_->IsOffTheRecord() &&
4366 !service->IsIncognitoEnabled(*iter))
4367 continue;
4368 if (*iter == extension) {
4369 found_index = index;
4370 break;
4371 }
4372 index++;
4373 }
[email protected]528c56d2010-07-30 19:28:444374 *value = base::IntToString(found_index);
[email protected]790788ac2010-04-06 17:52:194375 *success = true;
4376 }
4377 break;
4378 default:
4379 LOG(WARNING) << "Trying to get undefined extension property";
4380 break;
4381 }
4382 }
4383}
4384
[email protected]673fd2c02010-02-04 23:10:004385void AutomationProvider::SaveAsAsync(int tab_handle) {
4386 NavigationController* tab = NULL;
4387 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
4388 if (tab_contents)
4389 tab_contents->OnSavePage();
4390}
[email protected]7dad3d5f2010-03-04 00:27:014391
4392void AutomationProvider::SetContentSetting(
4393 int handle,
4394 const std::string& host,
4395 ContentSettingsType content_type,
4396 ContentSetting setting,
4397 bool* success) {
4398 *success = false;
4399 if (browser_tracker_->ContainsHandle(handle)) {
4400 Browser* browser = browser_tracker_->GetResource(handle);
4401 HostContentSettingsMap* map =
4402 browser->profile()->GetHostContentSettingsMap();
4403 if (host.empty()) {
4404 map->SetDefaultContentSetting(content_type, setting);
4405 } else {
[email protected]0314ae02010-04-08 09:18:294406 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
4407 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:014408 }
4409 *success = true;
4410 }
4411}
[email protected]cc824372010-03-31 15:33:014412
4413#if !defined(TOOLKIT_VIEWS)
4414void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
4415 NOTIMPLEMENTED();
4416};
4417
4418void AutomationProvider::WaitForFocusedViewIDToChange(
4419 int handle, int previous_view_id, IPC::Message* reply_message) {
4420 NOTIMPLEMENTED();
4421}
4422
4423void AutomationProvider::StartTrackingPopupMenus(
4424 int browser_handle, bool* success) {
4425 NOTIMPLEMENTED();
4426}
4427
4428void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
4429 NOTIMPLEMENTED();
4430}
4431#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:094432
4433void AutomationProvider::ResetToDefaultTheme() {
4434 profile_->ClearTheme();
4435}