blob: 95b524fbe143b6cd6526d7b3b46662ac748036c2 [file] [log] [blame]
[email protected]3b5f7022010-03-25 20:37:401// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/automation/automation_provider.h"
6
[email protected]5ae5bed2009-08-21 18:52:447#include <set>
8
[email protected]202e7a72009-06-15 03:48:369#include "app/l10n_util.h"
[email protected]37126212009-05-06 02:23:3110#include "app/message_box_flags.h"
[email protected]2041cf342010-02-19 03:15:5911#include "base/callback.h"
[email protected]7060bb292010-06-24 00:52:4912#include "base/file_path.h"
[email protected]c6cb1992009-04-13 16:45:2913#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2014#include "base/json/json_reader.h"
[email protected]59a611242010-04-02 02:24:0415#include "base/json/json_writer.h"
[email protected]93364da2010-06-29 18:03:4416#include "base/json/string_escape.h"
[email protected]bc1407f2009-09-29 00:33:3517#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3818#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4620#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5021#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2722#include "base/string_util.h"
[email protected]9eaa18e2010-06-29 20:51:0123#include "base/task.h"
[email protected]5fac9622009-02-04 21:49:3824#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5325#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1726#include "base/values.h"
[email protected]9eaa18e2010-06-29 20:51:0127#include "base/waitable_event.h"
[email protected]4f3dc372009-02-24 00:10:2928#include "chrome/app/chrome_dll_resource.h"
[email protected]bcff05a2010-04-14 01:46:4329#include "chrome/app/chrome_version_info.h"
[email protected]0bfa713f2009-04-07 20:18:2830#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0931#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]55846ad842010-07-09 18:22:5632#include "chrome/browser/autofill/autofill_manager.h"
[email protected]790788ac2010-04-06 17:52:1933#include "chrome/browser/automation/automation_extension_tracker.h"
initial.commit09911bf2008-07-26 23:55:2934#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0835#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]f44265b2009-05-19 18:52:5036#include "chrome/browser/automation/extension_port_container.h"
[email protected]12802702010-07-09 19:43:0937#include "chrome/browser/autocomplete/autocomplete_edit.h"
[email protected]66ba4932009-06-04 19:22:1338#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5339#include "chrome/browser/bookmarks/bookmark_model.h"
40#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]ef413ca2010-05-25 21:09:1441#include "chrome/browser/browser_list.h"
[email protected]5c238752009-06-13 10:29:0742#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3943#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2044#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0545#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5846#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2947#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1248#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5949#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2250#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0551#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4052#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1953#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]ef413ca2010-05-25 21:09:1454#include "chrome/browser/extensions/extension_host.h"
[email protected]d11c8e92009-10-20 23:26:4055#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5556#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1957#include "chrome/browser/extensions/extension_tabs_module.h"
58#include "chrome/browser/extensions/extension_toolbar_model.h"
59#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1660#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5861#include "chrome/browser/find_bar.h"
62#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2963#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0164#include "chrome/browser/host_content_settings_map.h"
[email protected]0ac83682010-01-22 17:46:2765#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0666#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0167#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5368#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2469#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0870#include "chrome/browser/pref_service.h"
[email protected]f83f9102010-05-04 17:01:0571#include "chrome/browser/printing/print_job.h"
[email protected]a7eee32f2009-05-22 18:08:1772#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0773#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2574#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0375#include "chrome/browser/ssl/ssl_manager.h"
76#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]b5558cf22010-07-12 17:30:0677#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3478#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4579#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1780#include "chrome/common/automation_constants.h"
[email protected]a9ff2c02010-05-13 17:33:0581#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2982#include "chrome/common/chrome_paths.h"
[email protected]790788ac2010-04-06 17:52:1983#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1784#include "chrome/common/json_value_serializer.h"
[email protected]68d2a05f2010-05-07 21:39:5585#include "chrome/common/net/url_request_context_getter.h"
[email protected]1c58a5c2009-05-21 18:47:1486#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5787#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2588#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5689#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5790#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1791#include "net/proxy/proxy_service.h"
92#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2193#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:3094#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:2895#include "views/event.h"
[email protected]f7d48012010-05-06 08:17:0596#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:2997
[email protected]de246f52009-02-25 18:25:4598#if defined(OS_WIN)
[email protected]4bdde602010-06-16 03:17:3599#include "chrome/browser/external_tab_container_win.h"
[email protected]de246f52009-02-25 18:25:45100#endif // defined(OS_WIN)
101
[email protected]e1acf6f2008-10-27 20:43:33102using base::Time;
103
[email protected]cbab76d2008-10-13 22:42:47104class AutomationInterstitialPage : public InterstitialPage {
105 public:
[email protected]57c6a652009-05-04 07:58:34106 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47107 const GURL& url,
108 const std::string& contents)
109 : InterstitialPage(tab, true, url),
110 contents_(contents) {
111 }
112
113 virtual std::string GetHTMLContents() { return contents_; }
114
115 private:
116 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29117
[email protected]cbab76d2008-10-13 22:42:47118 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
119};
120
[email protected]c2cb8542009-08-20 21:16:51121class ClickTask : public Task {
122 public:
[email protected]fc2e0872009-08-21 22:14:41123 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51124 virtual ~ClickTask() {}
125
126 virtual void Run() {
127 ui_controls::MouseButton button = ui_controls::LEFT;
128 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
129 views::Event::EF_LEFT_BUTTON_DOWN) {
130 button = ui_controls::LEFT;
131 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
132 views::Event::EF_RIGHT_BUTTON_DOWN) {
133 button = ui_controls::RIGHT;
134 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
135 views::Event::EF_MIDDLE_BUTTON_DOWN) {
136 button = ui_controls::MIDDLE;
137 } else {
138 NOTREACHED();
139 }
140
[email protected]fc2e0872009-08-21 22:14:41141 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51142 }
143
144 private:
[email protected]c2cb8542009-08-20 21:16:51145 int flags_;
146
147 DISALLOW_COPY_AND_ASSIGN(ClickTask);
148};
[email protected]c2cb8542009-08-20 21:16:51149
initial.commit09911bf2008-07-26 23:55:29150AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57151 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56152 profile_(profile),
[email protected]cc824372010-03-31 15:33:01153 reply_message_(NULL),
154 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29155 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19156 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29157 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20158 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29159 autocomplete_edit_tracker_.reset(
160 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29161 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
162 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44163 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19164 extension_test_result_observer_.reset(
165 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13166 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29167}
168
169AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50170 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
171 port_containers_.end());
172 port_containers_.clear();
173
[email protected]0da050b92008-08-19 19:29:47174 // Make sure that any outstanding NotificationObservers also get destroyed.
175 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31176 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47177 while ((observer = it.GetNext()) != NULL)
178 delete observer;
[email protected]528211a2010-01-14 15:25:13179
180 if (channel_.get()) {
181 channel_->Close();
182 }
183 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29184}
185
[email protected]9a3a293b2009-06-04 22:28:16186void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06187 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57188 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06189 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
190 automation_resource_message_filter_,
191 g_browser_process->io_thread()->message_loop(),
192 true, g_browser_process->shutdown_event()));
[email protected]bcff05a2010-04-14 01:46:43193 scoped_ptr<FileVersionInfo> version_info(
194 chrome_app::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40195 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43196 if (version_info != NULL) {
197 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40198 }
[email protected]c6cb1992009-04-13 16:45:29199
200 // Send a hello message with our current automation protocol version.
201 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29202}
203
204void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
205 if (expected_tabs == 0) {
206 Send(new AutomationMsg_InitialLoadsComplete(0));
207 } else {
208 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
209 }
210}
211
212NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58213 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01214 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29215 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58216 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01217 number_of_navigations,
218 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29219
[email protected]71f65dd2009-02-11 19:14:56220 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29221 return observer;
222}
223
[email protected]faf2ee42010-05-11 14:26:17224void AutomationProvider::RemoveNavigationStatusListener(
225 NotificationObserver* obs) {
226 notification_observer_list_.RemoveObserver(obs);
227}
228
initial.commit09911bf2008-07-26 23:55:29229NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14230 Browser* parent,
231 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56232 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14233 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29234 notification_observer_list_.AddObserver(observer);
235
236 return observer;
237}
238
[email protected]faf2ee42010-05-11 14:26:17239void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
initial.commit09911bf2008-07-26 23:55:29240 notification_observer_list_.RemoveObserver(obs);
241}
242
243void AutomationProvider::AddLoginHandler(NavigationController* tab,
244 LoginHandler* handler) {
245 login_handler_map_[tab] = handler;
246}
247
248void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
249 DCHECK(login_handler_map_[tab]);
250 login_handler_map_.erase(tab);
251}
252
[email protected]f44265b2009-05-19 18:52:50253void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
254 int port_id = port->port_id();
255 DCHECK_NE(-1, port_id);
256 DCHECK(port_containers_.find(port_id) == port_containers_.end());
257
258 port_containers_[port_id] = port;
259}
260
261void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
262 int port_id = port->port_id();
263 DCHECK_NE(-1, port_id);
264
265 PortContainerMap::iterator it = port_containers_.find(port_id);
266 DCHECK(it != port_containers_.end());
267
268 if (it != port_containers_.end()) {
269 delete it->second;
270 port_containers_.erase(it);
271 }
272}
273
274ExtensionPortContainer* AutomationProvider::GetPortContainer(
275 int port_id) const {
276 PortContainerMap::const_iterator it = port_containers_.find(port_id);
277 if (it == port_containers_.end())
278 return NULL;
279
280 return it->second;
281}
282
initial.commit09911bf2008-07-26 23:55:29283int AutomationProvider::GetIndexForNavigationController(
284 const NavigationController* controller, const Browser* parent) const {
285 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12286 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29287}
288
[email protected]790788ac2010-04-06 17:52:19289int AutomationProvider::AddExtension(Extension* extension) {
290 DCHECK(extension);
291 return extension_tracker_->Add(extension);
292}
293
294Extension* AutomationProvider::GetExtension(int extension_handle) {
295 return extension_tracker_->GetResource(extension_handle);
296}
297
298Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
299 Extension* extension = extension_tracker_->GetResource(extension_handle);
300 ExtensionsService* service = profile_->GetExtensionsService();
301 if (extension && service &&
302 service->GetExtensionById(extension->id(), false))
303 return extension;
304 return NULL;
305}
306
307Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
308 Extension* extension = extension_tracker_->GetResource(extension_handle);
309 ExtensionsService* service = profile_->GetExtensionsService();
310 if (extension && service &&
311 service->GetExtensionById(extension->id(), true) &&
312 !service->GetExtensionById(extension->id(), false))
313 return extension;
314 return NULL;
315}
316
initial.commit09911bf2008-07-26 23:55:29317void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
318 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14319 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56320 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
321 CloseBrowserAsync)
322 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
323 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
324 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
325 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
326 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
327 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22328 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]1c58a5c2009-05-21 18:47:14329 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58330 IPC_MESSAGE_HANDLER_DELAY_REPLY(
331 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
332 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56333 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11334 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
335 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56336 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
337 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
338 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
339 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14340 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56341 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
342 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
343 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14344 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29345 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
346 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56347 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36348 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56349 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29350 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56351 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29352 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
353 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56354 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14355 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24356 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44357 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33358 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44359 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33360 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24361 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14362 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24363 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15364 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14365 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24366 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35367 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14368 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30369#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56370 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
371 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30372#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56373 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53374 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56375 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37376#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56377 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45378#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56379 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
380 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03381 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56382 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14383 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09384 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
385 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
386 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29387 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14388 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56389 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
390 ExecuteJavascript)
391 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29392 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14393 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
394 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56395 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
396 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14397 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17398 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14399 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14400 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53401 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
402 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14403 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56404 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14405 GetAutocompleteEditForBrowser)
406 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]71f65dd2009-02-11 19:14:56407 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14408 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56409 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14410 HideInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56411 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
412 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14413 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
414 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44415 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
416 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56417 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
418 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29419 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
420 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
421 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56422 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41423 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56424 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
425 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29426 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56427 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29428 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56429 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29430 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56431 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29432 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56433 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45434 HandleOpenFindInPageRequest)
[email protected]1c58a5c2009-05-21 18:47:14435 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56436 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57437 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56438 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57439 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56440 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
441 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53442 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
443 GetBookmarksAsJSON)
444 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
445 WaitForBookmarkModelToLoad)
446 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
447 AddBookmarkGroup)
448 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
449 AddBookmarkURL)
450 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
451 ReparentBookmark)
452 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
453 SetBookmarkTitle)
454 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
455 SetBookmarkURL)
456 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
457 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04458 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
459 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18460 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
461 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
462 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56463 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42464 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56465 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
466 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14467 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56468 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44469 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56470 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20471 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14472 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56473 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16474 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56475 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16476 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56477 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16478 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14479 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20480 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
481 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14482 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]59560e0b2009-06-04 03:30:22483 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13484 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19485 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
486 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
487 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
488 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
489 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
490 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52491 IPC_MESSAGE_HANDLER_DELAY_REPLY(
492 AutomationMsg_WaitForBrowserWindowCountToBecome,
493 WaitForBrowserWindowCountToBecome)
494 IPC_MESSAGE_HANDLER_DELAY_REPLY(
495 AutomationMsg_WaitForAppModalDialogToBeShown,
496 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26497 IPC_MESSAGE_HANDLER_DELAY_REPLY(
498 AutomationMsg_GoBackBlockUntilNavigationsComplete,
499 GoBackBlockUntilNavigationsComplete)
500 IPC_MESSAGE_HANDLER_DELAY_REPLY(
501 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
502 GoForwardBlockUntilNavigationsComplete)
[email protected]1bb5f892009-10-06 01:44:57503 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40504 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
505 InstallExtension)
506 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
507 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43508 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
509 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19510 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
511 WaitForExtensionTestResult)
512 IPC_MESSAGE_HANDLER_DELAY_REPLY(
513 AutomationMsg_InstallExtensionAndGetHandle,
514 InstallExtensionAndGetHandle)
515 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
516 UninstallExtension)
517 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
518 EnableExtension)
519 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
520 DisableExtension)
521 IPC_MESSAGE_HANDLER_DELAY_REPLY(
522 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
523 ExecuteExtensionActionInActiveTabAsync)
524 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
525 MoveExtensionBrowserAction)
526 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
527 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57528 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
529 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00530 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]7dad3d5f2010-03-04 00:27:01531 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]bc73b4e52010-03-26 04:16:20532 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]bdd5a9c92010-06-14 18:21:00533 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
[email protected]cc824372010-03-31 15:33:01534#if defined(TOOLKIT_VIEWS)
535 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
536 WaitForFocusedViewIDToChange)
537 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
538 StartTrackingPopupMenus)
539 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
540 WaitForPopupMenuToOpen)
[email protected]bdd5a9c92010-06-14 18:21:00541#endif // defined(TOOLKIT_VIEWS)
[email protected]52415f842010-06-10 21:51:52542#if defined(OS_WIN)
543 // These are for use with external tabs.
544 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
545 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
546 ProcessUnhandledAccelerator)
547 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
548 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
549 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
550 OnForwardContextMenuCommandToChrome)
551 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
552 NavigateInExternalTab)
553 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
554 NavigateExternalTabAtIndex)
555 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
556 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
557 SetEnableExtensionAutomation)
558 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
559 OnMessageFromExternalHost)
[email protected]bdd5a9c92010-06-14 18:21:00560 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
561#endif // defined(OS_WIN)
562#if defined(OS_CHROMEOS)
563 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
564 LoginWithUserAndPass)
565#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29566 IPC_END_MESSAGE_MAP()
567}
568
[email protected]71f65dd2009-02-11 19:14:56569void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
570 *status = -1;
initial.commit09911bf2008-07-26 23:55:29571 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
572 Browser* browser = browser_tracker_->GetResource(handle);
573 if (at_index >= 0 && at_index < browser->tab_count()) {
574 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56575 *status = 0;
initial.commit09911bf2008-07-26 23:55:29576 }
577 }
initial.commit09911bf2008-07-26 23:55:29578}
579
[email protected]71f65dd2009-02-11 19:14:56580void AutomationProvider::AppendTab(int handle, const GURL& url,
581 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29582 int append_tab_response = -1; // -1 is the error code
583 NotificationObserver* observer = NULL;
584
585 if (browser_tracker_->ContainsHandle(handle)) {
586 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14587 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38588 TabContents* tab_contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39589 url, GURL(), PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED,
590 NULL, std::string());
initial.commit09911bf2008-07-26 23:55:29591 if (tab_contents) {
592 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57593 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29594 }
595 }
596
597 if (append_tab_response < 0) {
598 // The append tab failed. Remove the TabStripObserver
599 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17600 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29601 delete observer;
602 }
603
[email protected]71f65dd2009-02-11 19:14:56604 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
605 append_tab_response);
606 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29607 }
608}
609
[email protected]71f65dd2009-02-11 19:14:56610void AutomationProvider::NavigateToURL(int handle, const GURL& url,
611 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58612 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
613}
614
615void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
616 int handle, const GURL& url, int number_of_navigations,
617 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29618 if (tab_tracker_->ContainsHandle(handle)) {
619 NavigationController* tab = tab_tracker_->GetResource(handle);
620
621 // Simulate what a user would do. Activate the tab and then navigate.
622 // We could allow navigating in a background tab in future.
623 Browser* browser = FindAndActivateTab(tab);
624
625 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01626 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
627 false);
[email protected]71f65dd2009-02-11 19:14:56628
initial.commit09911bf2008-07-26 23:55:29629 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50630 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29631 return;
632 }
633 }
[email protected]71f65dd2009-02-11 19:14:56634
635 AutomationMsg_NavigateToURL::WriteReplyParams(
636 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
637 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29638}
[email protected]2949e90d2009-08-21 15:32:52639
[email protected]c70f9b82010-04-21 07:31:11640void AutomationProvider::NavigationAsync(int handle,
641 const GURL& url,
642 bool* status) {
643 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
644}
645
646void AutomationProvider::NavigationAsyncWithDisposition(
647 int handle,
648 const GURL& url,
649 WindowOpenDisposition disposition,
650 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56651 *status = false;
initial.commit09911bf2008-07-26 23:55:29652
653 if (tab_tracker_->ContainsHandle(handle)) {
654 NavigationController* tab = tab_tracker_->GetResource(handle);
655
656 // Simulate what a user would do. Activate the tab and then navigate.
657 // We could allow navigating in a background tab in future.
658 Browser* browser = FindAndActivateTab(tab);
659
660 if (browser) {
661 // Don't add any listener unless a callback mechanism is desired.
662 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11663 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56664 *status = true;
initial.commit09911bf2008-07-26 23:55:29665 }
666 }
initial.commit09911bf2008-07-26 23:55:29667}
668
[email protected]71f65dd2009-02-11 19:14:56669void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29670 if (tab_tracker_->ContainsHandle(handle)) {
671 NavigationController* tab = tab_tracker_->GetResource(handle);
672 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14673 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01674 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29675 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29676 return;
677 }
678 }
[email protected]71f65dd2009-02-11 19:14:56679
680 AutomationMsg_GoBack::WriteReplyParams(
681 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
682 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29683}
684
[email protected]71f65dd2009-02-11 19:14:56685void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29686 if (tab_tracker_->ContainsHandle(handle)) {
687 NavigationController* tab = tab_tracker_->GetResource(handle);
688 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14689 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01690 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29691 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29692 return;
693 }
694 }
[email protected]71f65dd2009-02-11 19:14:56695
696 AutomationMsg_GoForward::WriteReplyParams(
697 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
698 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29699}
700
[email protected]71f65dd2009-02-11 19:14:56701void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29702 if (tab_tracker_->ContainsHandle(handle)) {
703 NavigationController* tab = tab_tracker_->GetResource(handle);
704 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14705 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01706 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]cb84d642010-06-10 00:56:28707 browser->Reload(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29708 return;
709 }
710 }
[email protected]71f65dd2009-02-11 19:14:56711
712 AutomationMsg_Reload::WriteReplyParams(
713 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
714 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29715}
716
[email protected]71f65dd2009-02-11 19:14:56717void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29718 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56719 const std::wstring& password,
720 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29721 if (tab_tracker_->ContainsHandle(tab_handle)) {
722 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
723 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
724
725 if (iter != login_handler_map_.end()) {
726 // If auth is needed again after this, assume login has failed. This is
727 // not strictly correct, because a navigation can require both proxy and
728 // server auth, but it should be OK for now.
729 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01730 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29731 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52732 return;
initial.commit09911bf2008-07-26 23:55:29733 }
734 }
[email protected]de246f52009-02-25 18:25:45735
[email protected]457f5cf2009-08-18 16:37:52736 AutomationMsg_SetAuth::WriteReplyParams(
737 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
738 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29739}
740
[email protected]71f65dd2009-02-11 19:14:56741void AutomationProvider::CancelAuth(int tab_handle,
742 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29743 if (tab_tracker_->ContainsHandle(tab_handle)) {
744 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
745 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
746
747 if (iter != login_handler_map_.end()) {
748 // If auth is needed again after this, something is screwy.
749 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01750 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29751 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52752 return;
initial.commit09911bf2008-07-26 23:55:29753 }
754 }
[email protected]de246f52009-02-25 18:25:45755
[email protected]457f5cf2009-08-18 16:37:52756 AutomationMsg_CancelAuth::WriteReplyParams(
757 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
758 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29759}
760
[email protected]71f65dd2009-02-11 19:14:56761void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
762 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29763
764 if (tab_tracker_->ContainsHandle(tab_handle)) {
765 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
766 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
767
768 if (iter != login_handler_map_.end()) {
769 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56770 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29771 }
772 }
initial.commit09911bf2008-07-26 23:55:29773}
774
[email protected]71f65dd2009-02-11 19:14:56775void AutomationProvider::GetRedirectsFrom(int tab_handle,
776 const GURL& source_url,
777 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29778 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
779 if (tab_tracker_->ContainsHandle(tab_handle)) {
780 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
781 HistoryService* history_service =
782 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
783
784 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
785 "has no history service";
786 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56787 DCHECK(reply_message_ == NULL);
788 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29789 // Schedule a history query for redirects. The response will be sent
790 // asynchronously from the callback the history system uses to notify us
791 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29792 redirect_query_ = history_service->QueryRedirectsFrom(
793 source_url, &consumer_,
794 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
795 return; // Response will be sent when query completes.
796 }
797 }
798
799 // Send failure response.
[email protected]deb57402009-02-06 01:35:30800 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56801 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
802 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29803}
804
[email protected]71f65dd2009-02-11 19:14:56805void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
806 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29807 if (browser_tracker_->ContainsHandle(handle)) {
808 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56809 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29810 }
initial.commit09911bf2008-07-26 23:55:29811}
812
[email protected]202e7a72009-06-15 03:48:36813void AutomationProvider::GetBrowserLocale(string16* locale) {
814 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06815 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36816}
817
[email protected]71f65dd2009-02-11 19:14:56818void AutomationProvider::GetBrowserWindowCount(int* window_count) {
819 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29820}
821
[email protected]24497032009-05-01 17:00:29822void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
823 *window_count = static_cast<int>(
824 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
825}
826
[email protected]71f65dd2009-02-11 19:14:56827void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
828 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07829 AppModalDialog* dialog_delegate =
830 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50831 *showing_dialog = (dialog_delegate != NULL);
832 if (*showing_dialog)
833 *dialog_button = dialog_delegate->GetDialogButtons();
834 else
[email protected]478ff2ed2009-04-21 23:49:18835 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20836}
837
[email protected]71f65dd2009-02-11 19:14:56838void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
839 *success = false;
[email protected]fad84eab2008-12-05 00:37:20840
[email protected]1f460072009-05-28 17:02:07841 AppModalDialog* dialog_delegate =
842 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50843 if (dialog_delegate &&
844 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18845 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
846 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28847 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56848 *success = true;
[email protected]fad84eab2008-12-05 00:37:20849 }
[email protected]478ff2ed2009-04-21 23:49:18850 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
851 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56852 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28853 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56854 *success = true;
[email protected]fad84eab2008-12-05 00:37:20855 }
856 }
[email protected]c274acc2008-11-11 20:13:44857}
858
[email protected]fedaa7d2010-01-26 20:34:57859void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
860 if (browser_tracker_->ContainsHandle(handle)) {
861 Browser* browser = browser_tracker_->GetResource(handle);
862 browser->profile()->ShutdownSessionService();
863 *result = true;
864 } else {
865 *result = false;
866 }
867}
868
[email protected]71f65dd2009-02-11 19:14:56869void AutomationProvider::GetBrowserWindow(int index, int* handle) {
870 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29871 if (index >= 0) {
872 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]f07467d2010-06-16 14:28:30873 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {}
initial.commit09911bf2008-07-26 23:55:29874 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56875 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29876 }
877 }
initial.commit09911bf2008-07-26 23:55:29878}
879
[email protected]24497032009-05-01 17:00:29880void AutomationProvider::FindNormalBrowserWindow(int* handle) {
881 *handle = 0;
882 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31883 Browser::TYPE_NORMAL,
884 false);
[email protected]24497032009-05-01 17:00:29885 if (browser)
886 *handle = browser_tracker_->Add(browser);
887}
888
[email protected]71f65dd2009-02-11 19:14:56889void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
890 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29891 Browser* browser = BrowserList::GetLastActive();
892 if (browser)
[email protected]71f65dd2009-02-11 19:14:56893 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29894}
895
[email protected]b2aa3ed72010-02-01 18:37:14896#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28897// TODO(estade): use this implementation for all platforms?
898void AutomationProvider::GetActiveWindow(int* handle) {
899 gfx::NativeWindow window =
900 BrowserList::GetLastActive()->window()->GetNativeHandle();
901 *handle = window_tracker_->Add(window);
902}
903#endif
904
[email protected]4f6381ee2009-04-16 02:46:33905void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
906 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56907 *success = false;
[email protected]4ae62752008-08-04 23:28:47908 if (browser_tracker_->ContainsHandle(handle)) {
909 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14910 if (browser->command_updater()->SupportsCommand(command) &&
911 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47912 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56913 *success = true;
[email protected]4ae62752008-08-04 23:28:47914 }
915 }
[email protected]4ae62752008-08-04 23:28:47916}
917
[email protected]4f6381ee2009-04-16 02:46:33918void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56919 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53920 // List of commands which just finish synchronously and don't require
921 // setting up an observer.
922 static const int kSynchronousCommands[] = {
923 IDC_HOME,
924 IDC_SELECT_NEXT_TAB,
925 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25926 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53927 };
[email protected]56e71b7c2009-03-27 03:05:56928 if (browser_tracker_->ContainsHandle(handle)) {
929 Browser* browser = browser_tracker_->GetResource(handle);
930 if (browser->command_updater()->SupportsCommand(command) &&
931 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53932 // First check if we can handle the command without using an observer.
933 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
934 if (command == kSynchronousCommands[i]) {
935 browser->ExecuteCommand(command);
936 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
937 true);
938 Send(reply_message);
939 return;
940 }
941 }
942
943 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42944 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
945 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27946 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42947 return;
948 }
[email protected]56e71b7c2009-03-27 03:05:56949 }
950 }
[email protected]49a14a82009-03-31 04:16:44951 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56952 Send(reply_message);
953}
954
[email protected]fc2e0872009-08-21 22:14:41955// This task just adds another task to the event queue. This is useful if
956// you want to ensure that any tasks added to the event queue after this one
957// have already been processed by the time |task| is run.
958class InvokeTaskLaterTask : public Task {
959 public:
960 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
961 virtual ~InvokeTaskLaterTask() {}
962
963 virtual void Run() {
964 MessageLoop::current()->PostTask(FROM_HERE, task_);
965 }
966
967 private:
968 Task* task_;
969
970 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
971};
972
initial.commit09911bf2008-07-26 23:55:29973void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
974 int handle,
[email protected]d1a5941e2009-08-13 23:34:24975 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29976 int flags) {
[email protected]b410bc32009-08-14 01:11:14977 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51978 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41979 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29980 }
981}
982
[email protected]60507b12009-11-02 23:51:35983void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
984 int handle,
985 const gfx::Point& location) {
986 if (window_tracker_->ContainsHandle(handle))
987 ui_controls::SendMouseMove(location.x(), location.y());
988}
989
initial.commit09911bf2008-07-26 23:55:29990void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
991 int handle,
[email protected]bc1407f2009-09-29 00:33:35992 int key,
initial.commit09911bf2008-07-26 23:55:29993 int flags) {
[email protected]b410bc32009-08-14 01:11:14994 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29995 return;
996
[email protected]b410bc32009-08-14 01:11:14997 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29998 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35999 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:381000 ((flags & views::Event::EF_CONTROL_DOWN) ==
1001 views::Event::EF_CONTROL_DOWN),
1002 ((flags & views::Event::EF_SHIFT_DOWN) ==
1003 views::Event::EF_SHIFT_DOWN),
1004 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301005 views::Event::EF_ALT_DOWN),
1006 ((flags & views::Event::EF_COMMAND_DOWN) ==
1007 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291008}
initial.commit09911bf2008-07-26 23:55:291009
[email protected]71f65dd2009-02-11 19:14:561010void AutomationProvider::IsWindowActive(int handle, bool* success,
1011 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291012 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171013 *is_active =
1014 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561015 *success = true;
initial.commit09911bf2008-07-26 23:55:291016 } else {
[email protected]71f65dd2009-02-11 19:14:561017 *success = false;
1018 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291019 }
1020}
1021
[email protected]71f65dd2009-02-11 19:14:561022void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1023 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291024
1025 if (browser_tracker_->ContainsHandle(handle)) {
1026 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561027 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291028 }
initial.commit09911bf2008-07-26 23:55:291029}
1030
[email protected]982921f12009-10-27 21:43:531031void AutomationProvider::GetType(int handle, int* type_as_int) {
1032 *type_as_int = -1; // -1 is the error code
1033
1034 if (browser_tracker_->ContainsHandle(handle)) {
1035 Browser* browser = browser_tracker_->GetResource(handle);
1036 *type_as_int = static_cast<int>(browser->type());
1037 }
1038}
1039
[email protected]71f65dd2009-02-11 19:14:561040void AutomationProvider::GetTab(int win_handle, int tab_index,
1041 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561042 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291043 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1044 Browser* browser = browser_tracker_->GetResource(win_handle);
1045 if (tab_index < browser->tab_count()) {
1046 TabContents* tab_contents =
1047 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571048 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291049 }
1050 }
initial.commit09911bf2008-07-26 23:55:291051}
1052
[email protected]71f65dd2009-02-11 19:14:561053void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1054 std::wstring* title) {
1055 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291056 if (tab_tracker_->ContainsHandle(handle)) {
1057 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361058 NavigationEntry* entry = tab->GetActiveEntry();
1059 if (entry != NULL) {
1060 *title = UTF16ToWideHack(entry->title());
1061 } else {
1062 *title = std::wstring();
1063 }
[email protected]71f65dd2009-02-11 19:14:561064 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291065 }
initial.commit09911bf2008-07-26 23:55:291066}
1067
[email protected]77bc6732009-04-20 22:01:031068void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1069 *tabstrip_index = -1; // -1 is the error code
1070
1071 if (tab_tracker_->ContainsHandle(handle)) {
1072 NavigationController* tab = tab_tracker_->GetResource(handle);
1073 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121074 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031075 }
1076}
1077
initial.commit09911bf2008-07-26 23:55:291078void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1079 if (window_tracker_->ContainsHandle(handle)) {
1080 window_tracker_->Remove(window_tracker_->GetResource(handle));
1081 }
1082}
1083
1084void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161085 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571086 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291087}
1088
1089// TODO(brettw) change this to accept GURLs when history supports it
1090void AutomationProvider::OnRedirectQueryComplete(
1091 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371092 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291093 bool success,
[email protected]379c2b12009-07-01 21:50:331094 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291095 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561096 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291097
[email protected]deb57402009-02-06 01:35:301098 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101099 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291100 if (success) {
initial.commit09911bf2008-07-26 23:55:291101 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301102 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291103 }
1104
[email protected]4f3dc372009-02-24 00:10:291105 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301106
[email protected]71f65dd2009-02-11 19:14:561107 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231108 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561109 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291110}
1111
1112bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571113 DCHECK(channel_.get());
1114 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291115}
1116
1117Browser* AutomationProvider::FindAndActivateTab(
1118 NavigationController* controller) {
1119 int tab_index;
1120 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1121 if (browser)
1122 browser->SelectTabContentsAt(tab_index, true);
1123
1124 return browser;
1125}
1126
[email protected]9eaa18e2010-06-29 20:51:011127namespace {
1128
1129class GetCookiesTask : public Task {
1130 public:
1131 GetCookiesTask(const GURL& url,
1132 URLRequestContextGetter* context_getter,
1133 base::WaitableEvent* event,
1134 std::string* cookies)
1135 : url_(url),
1136 context_getter_(context_getter),
1137 event_(event),
1138 cookies_(cookies) {}
1139
1140 virtual void Run() {
1141 *cookies_ = context_getter_->GetCookieStore()->GetCookies(url_);
1142 event_->Signal();
1143 }
1144
1145 private:
1146 const GURL& url_;
1147 URLRequestContextGetter* const context_getter_;
1148 base::WaitableEvent* const event_;
1149 std::string* const cookies_;
1150
1151 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
1152};
1153
1154std::string GetCookiesForURL(
1155 const GURL& url,
1156 URLRequestContextGetter* context_getter) {
1157 std::string cookies;
1158 base::WaitableEvent event(true /* manual reset */,
1159 false /* not initially signaled */);
1160 CHECK(ChromeThread::PostTask(
1161 ChromeThread::IO, FROM_HERE,
1162 new GetCookiesTask(url, context_getter, &event, &cookies)));
1163 event.Wait();
1164 return cookies;
1165}
1166
1167class SetCookieTask : public Task {
1168 public:
1169 SetCookieTask(const GURL& url,
1170 const std::string& value,
1171 URLRequestContextGetter* context_getter,
1172 base::WaitableEvent* event,
1173 bool* rv)
1174 : url_(url),
1175 value_(value),
1176 context_getter_(context_getter),
1177 event_(event),
1178 rv_(rv) {}
1179
1180 virtual void Run() {
1181 *rv_ = context_getter_->GetCookieStore()->SetCookie(url_, value_);
1182 event_->Signal();
1183 }
1184
1185 private:
1186 const GURL& url_;
1187 const std::string& value_;
1188 URLRequestContextGetter* const context_getter_;
1189 base::WaitableEvent* const event_;
1190 bool* const rv_;
1191
1192 DISALLOW_COPY_AND_ASSIGN(SetCookieTask);
1193};
1194
1195bool SetCookieForURL(
1196 const GURL& url,
1197 const std::string& value,
1198 URLRequestContextGetter* context_getter) {
1199 base::WaitableEvent event(true /* manual reset */,
1200 false /* not initially signaled */);
1201 bool rv = false;
1202 CHECK(ChromeThread::PostTask(
1203 ChromeThread::IO, FROM_HERE,
1204 new SetCookieTask(url, value, context_getter, &event, &rv)));
1205 event.Wait();
1206 return rv;
1207}
1208
1209class DeleteCookieTask : public Task {
1210 public:
1211 DeleteCookieTask(const GURL& url,
1212 const std::string& name,
1213 const scoped_refptr<URLRequestContextGetter>& context_getter)
1214 : url_(url),
1215 name_(name),
1216 context_getter_(context_getter) {}
1217
1218 virtual void Run() {
1219 net::CookieStore* cookie_store = context_getter_->GetCookieStore();
1220 cookie_store->DeleteCookie(url_, name_);
1221 }
1222
1223 private:
1224 const GURL url_;
1225 const std::string name_;
1226 const scoped_refptr<URLRequestContextGetter> context_getter_;
1227
1228 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
1229};
1230
1231} // namespace
1232
[email protected]71f65dd2009-02-11 19:14:561233void AutomationProvider::GetCookies(const GURL& url, int handle,
1234 int* value_size,
1235 std::string* value) {
1236 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291237 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1238 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311239
1240 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001241 scoped_refptr<URLRequestContextGetter> request_context =
1242 tab->tab_contents()->request_context();
1243 if (!request_context.get())
1244 request_context = tab->profile()->GetRequestContext();
1245
[email protected]9eaa18e2010-06-29 20:51:011246 *value = GetCookiesForURL(url, request_context.get());
[email protected]71f65dd2009-02-11 19:14:561247 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291248 }
initial.commit09911bf2008-07-26 23:55:291249}
1250
[email protected]71f65dd2009-02-11 19:14:561251void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291252 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561253 int handle,
1254 int* response_value) {
1255 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291256
1257 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1258 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311259
[email protected]dfa46e5f2009-11-17 18:48:431260 scoped_refptr<URLRequestContextGetter> request_context =
1261 tab->tab_contents()->request_context();
1262 if (!request_context.get())
1263 request_context = tab->profile()->GetRequestContext();
1264
[email protected]9eaa18e2010-06-29 20:51:011265 if (SetCookieForURL(url, value, request_context.get()))
[email protected]71f65dd2009-02-11 19:14:561266 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291267 }
initial.commit09911bf2008-07-26 23:55:291268}
1269
[email protected]5fa57942010-04-21 23:07:221270void AutomationProvider::DeleteCookie(const GURL& url,
1271 const std::string& cookie_name,
1272 int handle, bool* success) {
1273 *success = false;
1274 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1275 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]9eaa18e2010-06-29 20:51:011276 ChromeThread::PostTask(
1277 ChromeThread::IO, FROM_HERE,
1278 new DeleteCookieTask(url, cookie_name,
1279 tab->profile()->GetRequestContext()));
[email protected]5fa57942010-04-21 23:07:221280 *success = true;
1281 }
1282}
1283
[email protected]71f65dd2009-02-11 19:14:561284void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1285 *success = false;
initial.commit09911bf2008-07-26 23:55:291286 if (tab_tracker_->ContainsHandle(handle)) {
1287 NavigationController* tab = tab_tracker_->GetResource(handle);
1288 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541289 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561290 *success = true;
initial.commit09911bf2008-07-26 23:55:291291 }
initial.commit09911bf2008-07-26 23:55:291292}
1293
[email protected]71f65dd2009-02-11 19:14:561294void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1295 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291296
1297 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561298 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341299 TabContents* tab_contents =
1300 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161301 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1302 if (rph)
1303 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291304 }
initial.commit09911bf2008-07-26 23:55:291305}
1306
1307void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331308 NOTREACHED() << "This function has been deprecated. "
1309 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291310}
1311
[email protected]71f65dd2009-02-11 19:14:561312void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291313 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561314 const std::wstring& script,
1315 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291316 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341317 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1318 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571319 // Set the routing id of this message with the controller.
1320 // This routing id needs to be remembered for the reverse
1321 // communication while sending back the response of
1322 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331323 std::wstring set_automation_id;
1324 SStringPrintf(&set_automation_id,
1325 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561326 reply_message->routing_id());
1327
1328 DCHECK(reply_message_ == NULL);
1329 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291330
[email protected]57c6a652009-05-04 07:58:341331 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331332 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341333 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061334 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571335 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291336 }
1337
1338 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561339 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1340 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291341 }
1342}
1343
[email protected]71f65dd2009-02-11 19:14:561344void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1345 *visible = false;
[email protected]20e93d12008-08-28 16:31:571346
[email protected]59560e0b2009-06-04 03:30:221347 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251348#if defined(OS_CHROMEOS)
1349 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1350 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1351 for (BrowserList::const_iterator it = BrowserList::begin();
1352 it != BrowserList::end(); ++it) {
1353 if ((*it)->type() == Browser::TYPE_POPUP) {
1354 const GURL& url =
1355 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1356
1357 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1358 url.host() == chrome::kChromeUIFileBrowseHost) {
1359 *visible = true;
1360 break;
1361 }
1362 }
1363 }
1364#else
[email protected]59560e0b2009-06-04 03:30:221365 Browser* browser = browser_tracker_->GetResource(handle);
1366 if (browser) {
1367 *visible = browser->window()->IsDownloadShelfVisible();
1368 }
[email protected]f5bf8ccf2010-02-05 18:19:251369#endif
[email protected]59560e0b2009-06-04 03:30:221370 }
initial.commit09911bf2008-07-26 23:55:291371}
1372
[email protected]59560e0b2009-06-04 03:30:221373void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1374 if (browser_tracker_->ContainsHandle(handle)) {
1375 Browser* browser = browser_tracker_->GetResource(handle);
1376 if (browser) {
1377 if (visible)
1378 browser->window()->GetDownloadShelf()->Show();
1379 else
1380 browser->window()->GetDownloadShelf()->Close();
1381 }
1382 }
1383}
1384
[email protected]34930432009-11-09 00:12:091385void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1386 *visible = false;
1387
1388 if (browser_tracker_->ContainsHandle(handle)) {
1389 Browser* browser = browser_tracker_->GetResource(handle);
1390 if (browser)
1391 *visible = browser->window()->IsFullscreen();
1392 }
1393}
1394
1395void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1396 bool* visible) {
1397 *visible = false;
1398
1399 if (browser_tracker_->ContainsHandle(handle)) {
1400 Browser* browser = browser_tracker_->GetResource(handle);
1401 if (browser)
1402 *visible = browser->window()->IsFullscreenBubbleVisible();
1403 }
1404}
[email protected]59560e0b2009-06-04 03:30:221405
[email protected]71f65dd2009-02-11 19:14:561406void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1407 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291408 if (tab_tracker_->ContainsHandle(handle)) {
1409 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111410 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291411 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561412 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291413 }
1414 }
initial.commit09911bf2008-07-26 23:55:291415}
1416
initial.commit09911bf2008-07-26 23:55:291417void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561418 int handle, const std::wstring& find_request,
1419 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311420 NOTREACHED() << "This function has been deprecated."
1421 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561422 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311423 return;
1424}
1425
[email protected]4f999132009-03-31 18:08:401426void AutomationProvider::HandleFindRequest(
1427 int handle,
1428 const AutomationMsg_Find_Params& params,
1429 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291430 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561431 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1432 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291433 return;
1434 }
1435
1436 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111437 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291438
1439 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141440 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291441
[email protected]57c6a652009-05-04 07:58:341442 tab_contents->set_current_find_request_id(
1443 FindInPageNotificationObserver::kFindInPageRequestId);
1444 tab_contents->render_view_host()->StartFinding(
1445 FindInPageNotificationObserver::kFindInPageRequestId,
1446 params.search_string, params.forward, params.match_case,
1447 params.find_next);
initial.commit09911bf2008-07-26 23:55:291448}
1449
[email protected]5f8af2a2008-08-06 22:49:451450void AutomationProvider::HandleOpenFindInPageRequest(
1451 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291452 if (browser_tracker_->ContainsHandle(handle)) {
1453 Browser* browser = browser_tracker_->GetResource(handle);
1454 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451455 }
1456}
1457
[email protected]71f65dd2009-02-11 19:14:561458void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561459 *visible = false;
[email protected]855c0142009-09-28 22:35:241460 Browser* browser = browser_tracker_->GetResource(handle);
1461 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581462 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171463 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241464 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291465 }
[email protected]20e93d12008-08-28 16:31:571466}
1467
[email protected]71f65dd2009-02-11 19:14:561468void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1469 int* y) {
[email protected]9e0534b2008-10-21 15:03:011470 gfx::Point position(0, 0);
1471 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291472 if (browser_tracker_->ContainsHandle(handle)) {
1473 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581474 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171475 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581476 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291477 }
[email protected]20e93d12008-08-28 16:31:571478
[email protected]71f65dd2009-02-11 19:14:561479 *x = position.x();
1480 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571481}
1482
[email protected]4512cb52010-04-05 19:50:251483// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1484// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581485void AutomationProvider::GetBookmarkBarVisibility(int handle,
1486 bool* visible,
1487 bool* animating) {
1488 *visible = false;
1489 *animating = false;
1490
1491 if (browser_tracker_->ContainsHandle(handle)) {
1492 Browser* browser = browser_tracker_->GetResource(handle);
1493 if (browser) {
[email protected]472f099b2010-05-27 17:07:121494#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1495 // TODO(jrg): Was removed in rev43789 for perf. Need to investigate.
1496
[email protected]ab6ca392010-04-07 00:44:131497 // IsBookmarkBarVisible() line looks correct but is not
1498 // consistent across platforms. Specifically, on Mac/Linux, it
1499 // returns false if the bar is hidden in a pref (even if visible
1500 // on the NTP). On ChromeOS, it returned true if on NTP
1501 // independent of the pref. Making the code more consistent
1502 // caused a perf bot regression on Windows (which shares views).
1503 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251504 *visible = browser->profile()->GetPrefs()->GetBoolean(
1505 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401506#else
1507 *visible = browser->window()->IsBookmarkBarVisible();
1508#endif
[email protected]c3240722010-03-05 21:52:581509 *animating = browser->window()->IsBookmarkBarAnimating();
1510 }
1511 }
1512}
1513
[email protected]6d8ffc9f2010-03-12 18:27:531514void AutomationProvider::GetBookmarksAsJSON(int handle,
1515 std::string* bookmarks_as_json,
1516 bool *success) {
1517 *success = false;
1518 if (browser_tracker_->ContainsHandle(handle)) {
1519 Browser* browser = browser_tracker_->GetResource(handle);
1520 if (browser) {
1521 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1522 return;
1523 }
1524 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1525 browser->profile(),
1526 browser->profile()->GetBookmarkModel());
1527 *success = storage->SerializeData(bookmarks_as_json);
1528 }
1529 }
1530}
1531
1532void AutomationProvider::WaitForBookmarkModelToLoad(
1533 int handle,
1534 IPC::Message* reply_message) {
1535 if (browser_tracker_->ContainsHandle(handle)) {
1536 Browser* browser = browser_tracker_->GetResource(handle);
1537 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1538 if (model->IsLoaded()) {
1539 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1540 reply_message, true);
1541 Send(reply_message);
1542 } else {
1543 // The observer will delete itself when done.
1544 new AutomationProviderBookmarkModelObserver(this, reply_message,
1545 model);
1546 }
1547 }
1548}
1549
1550void AutomationProvider::AddBookmarkGroup(int handle,
1551 int64 parent_id, int index,
1552 std::wstring title,
1553 bool* success) {
1554 if (browser_tracker_->ContainsHandle(handle)) {
1555 Browser* browser = browser_tracker_->GetResource(handle);
1556 if (browser) {
1557 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1558 if (!model->IsLoaded()) {
1559 *success = false;
1560 return;
1561 }
1562 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1563 DCHECK(parent);
1564 if (parent) {
1565 const BookmarkNode* child = model->AddGroup(parent, index,
1566 WideToUTF16(title));
1567 DCHECK(child);
1568 if (child)
1569 *success = true;
1570 }
1571 }
1572 }
1573 *success = false;
1574}
1575
1576void AutomationProvider::AddBookmarkURL(int handle,
1577 int64 parent_id, int index,
1578 std::wstring title, const GURL& url,
1579 bool* success) {
1580 if (browser_tracker_->ContainsHandle(handle)) {
1581 Browser* browser = browser_tracker_->GetResource(handle);
1582 if (browser) {
1583 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1584 if (!model->IsLoaded()) {
1585 *success = false;
1586 return;
1587 }
1588 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1589 DCHECK(parent);
1590 if (parent) {
1591 const BookmarkNode* child = model->AddURL(parent, index,
1592 WideToUTF16(title), url);
1593 DCHECK(child);
1594 if (child)
1595 *success = true;
1596 }
1597 }
1598 }
1599 *success = false;
1600}
1601
1602void AutomationProvider::ReparentBookmark(int handle,
1603 int64 id, int64 new_parent_id,
1604 int index,
1605 bool* success) {
1606 if (browser_tracker_->ContainsHandle(handle)) {
1607 Browser* browser = browser_tracker_->GetResource(handle);
1608 if (browser) {
1609 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1610 if (!model->IsLoaded()) {
1611 *success = false;
1612 return;
1613 }
1614 const BookmarkNode* node = model->GetNodeByID(id);
1615 DCHECK(node);
1616 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1617 DCHECK(new_parent);
1618 if (node && new_parent) {
1619 model->Move(node, new_parent, index);
1620 *success = true;
1621 }
1622 }
1623 }
1624 *success = false;
1625}
1626
1627void AutomationProvider::SetBookmarkTitle(int handle,
1628 int64 id, std::wstring title,
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 if (node) {
1641 model->SetTitle(node, WideToUTF16(title));
1642 *success = true;
1643 }
1644 }
1645 }
1646 *success = false;
1647}
1648
1649void AutomationProvider::SetBookmarkURL(int handle,
1650 int64 id, const GURL& url,
1651 bool* success) {
1652 if (browser_tracker_->ContainsHandle(handle)) {
1653 Browser* browser = browser_tracker_->GetResource(handle);
1654 if (browser) {
1655 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1656 if (!model->IsLoaded()) {
1657 *success = false;
1658 return;
1659 }
1660 const BookmarkNode* node = model->GetNodeByID(id);
1661 DCHECK(node);
1662 if (node) {
1663 model->SetURL(node, url);
1664 *success = true;
1665 }
1666 }
1667 }
1668 *success = false;
1669}
1670
1671void AutomationProvider::RemoveBookmark(int handle,
1672 int64 id,
1673 bool* success) {
1674 if (browser_tracker_->ContainsHandle(handle)) {
1675 Browser* browser = browser_tracker_->GetResource(handle);
1676 if (browser) {
1677 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1678 if (!model->IsLoaded()) {
1679 *success = false;
1680 return;
1681 }
1682 const BookmarkNode* node = model->GetNodeByID(id);
1683 DCHECK(node);
1684 if (node) {
1685 const BookmarkNode* parent = node->GetParent();
1686 DCHECK(parent);
1687 model->Remove(parent, parent->IndexOfChild(node));
1688 *success = true;
1689 }
1690 }
1691 }
1692 *success = false;
1693}
1694
[email protected]ef413ca2010-05-25 21:09:141695// Sample json input: { "command": "SetWindowDimensions",
1696// "x": 20, # optional
1697// "y": 20, # optional
1698// "width": 800, # optional
1699// "height": 600 } # optional
1700void AutomationProvider::SetWindowDimensions(Browser* browser,
1701 DictionaryValue* args,
1702 IPC::Message* reply_message) {
1703 gfx::Rect rect = browser->window()->GetRestoredBounds();
1704 int x, y, width, height;
1705 if (args->GetInteger(L"x", &x))
1706 rect.set_x(x);
1707 if (args->GetInteger(L"y", &y))
1708 rect.set_y(y);
1709 if (args->GetInteger(L"width", &width))
1710 rect.set_width(width);
1711 if (args->GetInteger(L"height", &height))
1712 rect.set_height(height);
1713 browser->window()->SetBounds(rect);
[email protected]f6ff0df2010-07-11 22:41:431714 AutomationMsg_SendJSONRequest::WriteReplyParams(
1715 reply_message, std::string("{}"), true);
1716 Send(reply_message);
[email protected]ef413ca2010-05-25 21:09:141717}
1718
[email protected]a9ff2c02010-05-13 17:33:051719// Sample json input: { "command": "GetBrowserInfo" }
1720// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1721// sample json output.
[email protected]53329582010-05-14 21:10:581722void AutomationProvider::GetBrowserInfo(Browser* browser,
1723 DictionaryValue* args,
[email protected]a9ff2c02010-05-13 17:33:051724 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:431725 std::string json_return;
1726 bool reply_return = true;
1727
[email protected]a9ff2c02010-05-13 17:33:051728 DictionaryValue* properties = new DictionaryValue;
1729 properties->SetString(L"ChromeVersion", chrome::kChromeVersion);
1730 properties->SetString(L"BrowserProcessExecutableName",
1731 chrome::kBrowserProcessExecutableName);
1732 properties->SetString(L"HelperProcessExecutableName",
1733 chrome::kHelperProcessExecutableName);
1734 properties->SetString(L"BrowserProcessExecutablePath",
1735 chrome::kBrowserProcessExecutablePath);
1736 properties->SetString(L"HelperProcessExecutablePath",
1737 chrome::kHelperProcessExecutablePath);
[email protected]a9ff2c02010-05-13 17:33:051738 properties->SetString(L"command_line_string",
1739 CommandLine::ForCurrentProcess()->command_line_string());
[email protected]44eed9f2010-06-28 22:04:001740
1741 std::string branding;
1742#if defined(GOOGLE_CHROME_BUILD)
1743 branding = "Google Chrome";
1744#elif defined(CHROMIUM_BUILD)
1745 branding = "Chromium";
1746#else
1747 branding = "Unknown Branding";
[email protected]a9ff2c02010-05-13 17:33:051748#endif
[email protected]44eed9f2010-06-28 22:04:001749 properties->SetString(L"branding", branding);
[email protected]a9ff2c02010-05-13 17:33:051750
1751 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1752 return_value->Set(L"properties", properties);
1753
[email protected]ef413ca2010-05-25 21:09:141754 return_value->SetInteger(L"browser_pid", base::GetCurrentProcId());
1755 // Add info about all windows in a list of dictionaries, one dictionary
1756 // item per window.
1757 ListValue* windows = new ListValue;
1758 int windex = 0;
1759 for (BrowserList::const_iterator it = BrowserList::begin();
1760 it != BrowserList::end();
1761 ++it, ++windex) {
1762 DictionaryValue* browser_item = new DictionaryValue;
1763 browser = *it;
1764 browser_item->SetInteger(L"index", windex);
1765 // Window properties
1766 gfx::Rect rect = browser->window()->GetRestoredBounds();
1767 browser_item->SetInteger(L"x", rect.x());
1768 browser_item->SetInteger(L"y", rect.y());
1769 browser_item->SetInteger(L"width", rect.width());
1770 browser_item->SetInteger(L"height", rect.height());
1771 browser_item->SetBoolean(L"fullscreen",
1772 browser->window()->IsFullscreen());
1773 browser_item->SetInteger(L"selected_tab", browser->selected_index());
1774 browser_item->SetBoolean(L"incognito",
1775 browser->profile()->IsOffTheRecord());
1776 // For each window, add info about all tabs in a list of dictionaries,
1777 // one dictionary item per tab.
1778 ListValue* tabs = new ListValue;
1779 for (int i = 0; i < browser->tab_count(); ++i) {
1780 TabContents* tc = browser->GetTabContentsAt(i);
1781 DictionaryValue* tab = new DictionaryValue;
1782 tab->SetInteger(L"index", i);
1783 tab->SetString(L"url", tc->GetURL().spec());
1784 tab->SetInteger(L"renderer_pid",
1785 base::GetProcId(tc->GetRenderProcessHost()->GetHandle()));
1786 tab->SetInteger(L"num_infobars", tc->infobar_delegate_count());
1787 tabs->Append(tab);
1788 }
1789 browser_item->Set(L"tabs", tabs);
1790
1791 windows->Append(browser_item);
1792 }
1793 return_value->Set(L"windows", windows);
1794
1795 return_value->SetString(L"child_process_path",
1796 ChildProcessHost::GetChildPath(true).value());
1797 // Child processes are the processes for plugins and other workers.
1798 // Add all child processes in a list of dictionaries, one dictionary item
1799 // per child process.
1800 ListValue* child_processes = new ListValue;
[email protected]d27893f62010-07-03 05:47:421801 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
[email protected]ef413ca2010-05-25 21:09:141802 // Only add processes which are already started, since we need their handle.
1803 if ((*iter)->handle() != base::kNullProcessHandle) {
1804 ChildProcessInfo* info = *iter;
1805 DictionaryValue* item = new DictionaryValue;
1806 item->SetString(L"name", info->name());
1807 item->SetString(L"type",
1808 ChildProcessInfo::GetTypeNameInEnglish(info->type()));
1809 item->SetInteger(L"pid", base::GetProcId(info->handle()));
1810 child_processes->Append(item);
1811 }
1812 }
1813 return_value->Set(L"child_processes", child_processes);
1814
1815 // Add all extension processes in a list of dictionaries, one dictionary
1816 // item per extension process.
1817 ListValue* extension_processes = new ListValue;
1818 ProfileManager* profile_manager = g_browser_process->profile_manager();
1819 for (ProfileManager::const_iterator it = profile_manager->begin();
1820 it != profile_manager->end(); ++it) {
1821 ExtensionProcessManager* process_manager =
1822 (*it)->GetExtensionProcessManager();
1823 ExtensionProcessManager::const_iterator jt;
1824 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) {
1825 ExtensionHost* ex_host = *jt;
1826 // Don't add dead extension processes.
1827 if (!ex_host->IsRenderViewLive())
1828 continue;
1829 DictionaryValue* item = new DictionaryValue;
1830 item->SetString(L"name", ex_host->extension()->name());
1831 item->SetInteger(
1832 L"pid",
1833 base::GetProcId(ex_host->render_process_host()->GetHandle()));
1834 extension_processes->Append(item);
1835 }
1836 }
1837 return_value->Set(L"extension_processes", extension_processes);
[email protected]f6ff0df2010-07-11 22:41:431838
1839 base::JSONWriter::Write(return_value.get(), false, &json_return);
1840 AutomationMsg_SendJSONRequest::WriteReplyParams(
1841 reply_message, json_return, reply_return);
1842 Send(reply_message);
[email protected]a9ff2c02010-05-13 17:33:051843}
1844
[email protected]24e2b102010-04-29 17:56:471845// Sample json input: { "command": "GetHistoryInfo",
1846// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361847// Refer chrome/test/pyautolib/history_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581848void AutomationProvider::GetHistoryInfo(Browser* browser,
1849 DictionaryValue* args,
1850 IPC::Message* reply_message) {
[email protected]e6e376e2010-04-19 21:41:361851 consumer_.CancelAllRequests();
1852
[email protected]e53668962010-06-23 15:35:251853 string16 search_text;
1854 args->GetString("search_text", &search_text);
[email protected]e6e376e2010-04-19 21:41:361855
1856 // Fetch history.
1857 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1858 history::QueryOptions options;
1859 // The observer owns itself. It deletes itself after it fetches history.
1860 AutomationProviderHistoryObserver* history_observer =
1861 new AutomationProviderHistoryObserver(this, reply_message);
1862 hs->QueryHistory(
1863 search_text,
1864 options,
1865 &consumer_,
1866 NewCallback(history_observer,
1867 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1868}
1869
[email protected]bbe6aa02010-05-07 17:27:291870// Sample json input: { "command": "AddHistoryItem",
1871// "item": { "URL": "http://www.google.com",
1872// "title": "Google", # optional
1873// "time": 12345 # optional (time_t)
1874// } }
1875// Refer chrome/test/pyautolib/pyauto.py for details on input.
[email protected]53329582010-05-14 21:10:581876void AutomationProvider::AddHistoryItem(Browser* browser,
1877 DictionaryValue* args,
1878 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:431879 bool reply_return = true;
1880 std::string json_return = "{}";
1881
[email protected]bbe6aa02010-05-07 17:27:291882 DictionaryValue* item = NULL;
1883 args->GetDictionary(L"item", &item);
1884 string16 url_text;
[email protected]e53668962010-06-23 15:35:251885 string16 title;
[email protected]bbe6aa02010-05-07 17:27:291886 base::Time time = base::Time::Now();
[email protected]bbe6aa02010-05-07 17:27:291887
1888 if (item->GetString("url", &url_text)) {
1889 GURL gurl(url_text);
[email protected]e53668962010-06-23 15:35:251890 item->GetString("title", &title); // Don't care if it fails.
[email protected]bbe6aa02010-05-07 17:27:291891 int it;
1892 double dt;
1893 if (item->GetInteger(L"time", &it))
1894 time = base::Time::FromTimeT(it);
1895 else if (item->GetReal(L"time", &dt))
1896 time = base::Time::FromDoubleT(dt);
1897
1898 // Ideas for "dummy" values (e.g. id_scope) came from
1899 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
1900 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1901 const void* id_scope = reinterpret_cast<void*>(1);
1902 hs->AddPage(gurl, time,
1903 id_scope,
1904 0,
1905 GURL(),
1906 PageTransition::LINK,
1907 history::RedirectList(),
1908 false);
[email protected]e53668962010-06-23 15:35:251909 if (title.length())
[email protected]bbe6aa02010-05-07 17:27:291910 hs->SetPageTitle(gurl, title);
[email protected]bbe6aa02010-05-07 17:27:291911 } else {
[email protected]f6ff0df2010-07-11 22:41:431912 json_return = JSONErrorString("bad args (no URL in dict?)");
1913 reply_return = false;
[email protected]bbe6aa02010-05-07 17:27:291914 }
[email protected]f6ff0df2010-07-11 22:41:431915
1916 AutomationMsg_SendJSONRequest::WriteReplyParams(
1917 reply_message, json_return, reply_return);
1918 Send(reply_message);
[email protected]bbe6aa02010-05-07 17:27:291919}
1920
[email protected]24e2b102010-04-29 17:56:471921// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361922// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581923void AutomationProvider::GetDownloadsInfo(Browser* browser,
1924 DictionaryValue* args,
1925 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:431926 std::string json_return;
1927 bool reply_return = true;
[email protected]d4adc292010-04-15 18:06:391928 AutomationProviderDownloadManagerObserver observer;
1929 std::vector<DownloadItem*> downloads;
1930 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]d4adc292010-04-15 18:06:391931
1932 if (!profile_->HasCreatedDownloadManager()) {
[email protected]f6ff0df2010-07-11 22:41:431933 json_return = JSONErrorString("no download manager");
1934 reply_return = false;
[email protected]d4adc292010-04-15 18:06:391935 } else {
1936 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1937 // since that would be transient; a download might enter and empty out
1938 // the current download queue too soon to be noticed.
1939 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1940 downloads = observer.Downloads();
1941 }
1942
1943 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1944 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1945 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1946 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1947 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1948
1949 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1950 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1951 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1952 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1953 std::string("DANGEROUS_BUT_VALIDATED");
1954
1955 ListValue* list_of_downloads = new ListValue;
1956 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1957 it != downloads.end();
1958 it++) { // Fill info about each download item.
1959 DictionaryValue* dl_item_value = new DictionaryValue;
1960 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1961 dl_item_value->SetString(L"url", (*it)->url().spec());
1962 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1963 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1964 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1965 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1966 dl_item_value->SetBoolean(L"open_when_complete",
1967 (*it)->open_when_complete());
1968 dl_item_value->SetBoolean(L"is_extension_install",
1969 (*it)->is_extension_install());
1970 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1971 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1972 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1973 dl_item_value->SetString(L"safety_state",
1974 safety_state_to_string[(*it)->safety_state()]);
1975 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1976 list_of_downloads->Append(dl_item_value);
1977 }
1978 return_value->Set(L"downloads", list_of_downloads);
[email protected]f6ff0df2010-07-11 22:41:431979 base::JSONWriter::Write(return_value.get(), false, &json_return);
[email protected]d4adc292010-04-15 18:06:391980
[email protected]f6ff0df2010-07-11 22:41:431981 AutomationMsg_SendJSONRequest::WriteReplyParams(
1982 reply_message, json_return, reply_return);
1983 Send(reply_message);
[email protected]d4adc292010-04-15 18:06:391984 // All value objects allocated above are owned by |return_value|
1985 // and get freed by it.
1986}
1987
[email protected]59a611242010-04-02 02:24:041988void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:581989 Browser* browser,
[email protected]59a611242010-04-02 02:24:041990 DictionaryValue* args,
1991 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:431992 std::string json_return;
1993 bool reply_return = true;
[email protected]59a611242010-04-02 02:24:041994 AutomationProviderDownloadManagerObserver observer;
1995 std::vector<DownloadItem*> downloads;
[email protected]59a611242010-04-02 02:24:041996
1997 // Look for a quick return.
1998 if (!profile_->HasCreatedDownloadManager()) {
[email protected]f6ff0df2010-07-11 22:41:431999 json_return = JSONErrorString("no download manager");
2000 reply_return = false;
[email protected]59a611242010-04-02 02:24:042001 } else {
[email protected]f6ff0df2010-07-11 22:41:432002 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
2003 FilePath());
[email protected]59a611242010-04-02 02:24:042004 downloads = observer.Downloads();
2005 if (downloads.size() == 0) {
[email protected]f6ff0df2010-07-11 22:41:432006 json_return = "{}";
[email protected]59a611242010-04-02 02:24:042007 }
2008 }
[email protected]f6ff0df2010-07-11 22:41:432009 if (!json_return.empty()) {
2010 AutomationMsg_SendJSONRequest::WriteReplyParams(
2011 reply_message, json_return, reply_return);
2012 Send(reply_message);
2013 return;
2014 }
[email protected]59a611242010-04-02 02:24:042015
2016 // The observer owns itself. When the last observed item pings, it
2017 // deletes itself.
2018 AutomationProviderDownloadItemObserver* item_observer =
2019 new AutomationProviderDownloadItemObserver(
2020 this, reply_message, downloads.size());
2021 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
2022 i != downloads.end();
2023 i++) {
2024 (*i)->AddObserver(item_observer);
2025 }
2026}
2027
[email protected]24e2b102010-04-29 17:56:472028// Sample json input: { "command": "GetPrefsInfo" }
2029// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582030void AutomationProvider::GetPrefsInfo(Browser* browser,
2031 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472032 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432033 std::string json_return;
2034 bool reply_return = true;
2035
[email protected]24e2b102010-04-29 17:56:472036 const PrefService::PreferenceSet& prefs =
2037 profile_->GetPrefs()->preference_set();
2038 DictionaryValue* items = new DictionaryValue;
2039 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
2040 it != prefs.end(); ++it) {
2041 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
2042 }
2043 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2044 return_value->Set(L"prefs", items); // return_value owns items.
[email protected]f6ff0df2010-07-11 22:41:432045
2046 base::JSONWriter::Write(return_value.get(), false, &json_return);
2047 AutomationMsg_SendJSONRequest::WriteReplyParams(
2048 reply_message, json_return, reply_return);
2049 Send(reply_message);
[email protected]24e2b102010-04-29 17:56:472050}
2051
2052// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:582053void AutomationProvider::SetPrefs(Browser* browser,
2054 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472055 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432056 bool reply_return = true;
2057 std::string json_return = "{}";
[email protected]24e2b102010-04-29 17:56:472058 std::wstring path;
2059 Value* val;
[email protected]24e2b102010-04-29 17:56:472060 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
2061 PrefService* pref_service = profile_->GetPrefs();
2062 const PrefService::Preference* pref =
2063 pref_service->FindPreference(path.c_str());
2064 if (!pref) { // Not a registered pref.
[email protected]f6ff0df2010-07-11 22:41:432065 json_return = JSONErrorString("pref not registered.");
2066 reply_return = false;
[email protected]24e2b102010-04-29 17:56:472067 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
[email protected]f6ff0df2010-07-11 22:41:432068 json_return = JSONErrorString("pref is managed. cannot be changed.");
2069 reply_return = false;
[email protected]24e2b102010-04-29 17:56:472070 } else { // Set the pref.
2071 pref_service->Set(path.c_str(), *val);
2072 }
2073 } else {
[email protected]f6ff0df2010-07-11 22:41:432074 json_return = JSONErrorString("no pref path or value given.");
2075 reply_return = false;
[email protected]24e2b102010-04-29 17:56:472076 }
2077
[email protected]f6ff0df2010-07-11 22:41:432078 AutomationMsg_SendJSONRequest::WriteReplyParams(
2079 reply_message, json_return, reply_return);
2080 Send(reply_message);
[email protected]24e2b102010-04-29 17:56:472081}
2082
[email protected]53329582010-05-14 21:10:582083// Sample json input: { "command": "GetOmniboxInfo" }
2084// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
2085void AutomationProvider::GetOmniboxInfo(Browser* browser,
2086 DictionaryValue* args,
2087 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432088 std::string json_return;
2089 bool reply_return = true;
[email protected]53329582010-05-14 21:10:582090 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2091
2092 LocationBar* loc_bar = browser->window()->GetLocationBar();
2093 AutocompleteEditView* edit_view = loc_bar->location_entry();
2094 AutocompleteEditModel* model = edit_view->model();
2095
2096 // Fill up matches.
2097 ListValue* matches = new ListValue;
2098 const AutocompleteResult& result = model->result();
2099 for (AutocompleteResult::const_iterator i = result.begin();
2100 i != result.end(); ++i) {
2101 const AutocompleteMatch& match = *i;
2102 DictionaryValue* item = new DictionaryValue; // owned by return_value
2103 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
2104 item->SetBoolean(L"starred", match.starred);
2105 item->SetString(L"destination_url", match.destination_url.spec());
2106 item->SetString(L"contents", match.contents);
2107 item->SetString(L"description", match.description);
2108 matches->Append(item);
2109 }
2110 return_value->Set(L"matches", matches);
2111
2112 // Fill up other properties.
2113 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2114 properties->SetBoolean(L"has_focus", model->has_focus());
2115 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2116 properties->SetString(L"keyword", model->keyword());
2117 properties->SetString(L"text", edit_view->GetText());
2118 return_value->Set(L"properties", properties);
2119
[email protected]f6ff0df2010-07-11 22:41:432120 base::JSONWriter::Write(return_value.get(), false, &json_return);
2121 AutomationMsg_SendJSONRequest::WriteReplyParams(
2122 reply_message, json_return, reply_return);
2123 Send(reply_message);
[email protected]53329582010-05-14 21:10:582124}
2125
2126// Sample json input: { "command": "SetOmniboxText",
2127// "text": "goog" }
2128void AutomationProvider::SetOmniboxText(Browser* browser,
2129 DictionaryValue* args,
2130 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432131 std::string json_return = "{}";
2132 bool reply_return = true;
[email protected]53329582010-05-14 21:10:582133 std::wstring text;
[email protected]f6ff0df2010-07-11 22:41:432134
[email protected]53329582010-05-14 21:10:582135 if (!args->GetString(L"text", &text)) {
[email protected]f6ff0df2010-07-11 22:41:432136 json_return = JSONErrorString("text missing");
2137 reply_return = false;
[email protected]53329582010-05-14 21:10:582138 } else {
2139 browser->FocusLocationBar();
2140 LocationBar* loc_bar = browser->window()->GetLocationBar();
2141 AutocompleteEditView* edit_view = loc_bar->location_entry();
2142 edit_view->model()->OnSetFocus(false);
2143 edit_view->SetUserText(text);
2144 }
2145
[email protected]f6ff0df2010-07-11 22:41:432146 AutomationMsg_SendJSONRequest::WriteReplyParams(
2147 reply_message, json_return, reply_return);
2148 Send(reply_message);
[email protected]53329582010-05-14 21:10:582149}
2150
2151// Sample json input: { "command": "OmniboxMovePopupSelection",
2152// "count": 1 }
2153// Negative count implies up, positive implies down. Count values will be
2154// capped by the size of the popup list.
2155void AutomationProvider::OmniboxMovePopupSelection(
2156 Browser* browser,
2157 DictionaryValue* args,
2158 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432159 std::string json_return = "{}";
2160 bool reply_return = true;
[email protected]53329582010-05-14 21:10:582161 int count;
[email protected]f6ff0df2010-07-11 22:41:432162
[email protected]53329582010-05-14 21:10:582163 if (!args->GetInteger(L"count", &count)) {
[email protected]f6ff0df2010-07-11 22:41:432164 json_return = JSONErrorString("count missing");
2165 reply_return = false;
[email protected]53329582010-05-14 21:10:582166 } else {
2167 LocationBar* loc_bar = browser->window()->GetLocationBar();
2168 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2169 model->OnUpOrDownKeyPressed(count);
2170 }
2171
[email protected]f6ff0df2010-07-11 22:41:432172 AutomationMsg_SendJSONRequest::WriteReplyParams(
2173 reply_message, json_return, reply_return);
2174 Send(reply_message);
[email protected]53329582010-05-14 21:10:582175}
2176
2177// Sample json input: { "command": "OmniboxAcceptInput" }
2178void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2179 DictionaryValue* args,
2180 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282181 NavigationController& controller =
2182 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122183 // Setup observer to wait until the selected item loads.
2184 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282185 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122186 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582187
2188 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582189}
2190
[email protected]a3cd5022010-06-16 18:25:292191// Sample json input: { "command": "GetInitialLoadTimes" }
2192// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2193void AutomationProvider::GetInitialLoadTimes(
2194 Browser*,
2195 DictionaryValue*,
2196 IPC::Message* reply_message) {
2197 scoped_ptr<DictionaryValue> return_value(
2198 initial_load_observer_->GetTimingInformation());
[email protected]f6ff0df2010-07-11 22:41:432199
2200 std::string json_return;
2201 base::JSONWriter::Write(return_value.get(), false, &json_return);
2202 AutomationMsg_SendJSONRequest::WriteReplyParams(
2203 reply_message, json_return, true);
2204 Send(reply_message);
[email protected]a3cd5022010-06-16 18:25:292205}
2206
[email protected]f7d48012010-05-06 08:17:052207// Sample json input: { "command": "GetPluginsInfo" }
2208// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582209void AutomationProvider::GetPluginsInfo(Browser* browser,
2210 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052211 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432212 std::string json_return;
2213 bool reply_return = true;
2214
[email protected]f7d48012010-05-06 08:17:052215 std::vector<WebPluginInfo> plugins;
2216 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2217 ListValue* items = new ListValue;
2218 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2219 it != plugins.end();
2220 ++it) {
2221 DictionaryValue* item = new DictionaryValue;
[email protected]c9d811372010-06-23 21:44:572222 item->SetStringFromUTF16(L"name", it->name);
[email protected]f7d48012010-05-06 08:17:052223 item->SetString(L"path", it->path.value());
[email protected]c9d811372010-06-23 21:44:572224 item->SetStringFromUTF16(L"version", it->version);
2225 item->SetStringFromUTF16(L"desc", it->desc);
[email protected]f7d48012010-05-06 08:17:052226 item->SetBoolean(L"enabled", it->enabled);
2227 // Add info about mime types.
2228 ListValue* mime_types = new ListValue();
2229 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2230 it->mime_types.begin();
2231 type_it != it->mime_types.end();
2232 ++type_it) {
2233 DictionaryValue* mime_type = new DictionaryValue();
2234 mime_type->SetString(L"mimeType", type_it->mime_type);
[email protected]c9d811372010-06-23 21:44:572235 mime_type->SetStringFromUTF16(L"description", type_it->description);
[email protected]f7d48012010-05-06 08:17:052236
2237 ListValue* file_extensions = new ListValue();
2238 for (std::vector<std::string>::const_iterator ext_it =
2239 type_it->file_extensions.begin();
2240 ext_it != type_it->file_extensions.end();
2241 ++ext_it) {
2242 file_extensions->Append(new StringValue(*ext_it));
2243 }
2244 mime_type->Set(L"fileExtensions", file_extensions);
2245
2246 mime_types->Append(mime_type);
2247 }
2248 item->Set(L"mimeTypes", mime_types);
2249 items->Append(item);
2250 }
2251 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2252 return_value->Set(L"plugins", items); // return_value owns items.
2253
[email protected]f6ff0df2010-07-11 22:41:432254 base::JSONWriter::Write(return_value.get(), false, &json_return);
2255 AutomationMsg_SendJSONRequest::WriteReplyParams(
2256 reply_message, json_return, reply_return);
2257 Send(reply_message);
[email protected]f7d48012010-05-06 08:17:052258}
2259
2260// Sample json input:
2261// { "command": "EnablePlugin",
2262// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582263void AutomationProvider::EnablePlugin(Browser* browser,
2264 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052265 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432266 std::string json_return = "{}";
2267 bool reply_return = true;
[email protected]f7d48012010-05-06 08:17:052268 FilePath::StringType path;
[email protected]f6ff0df2010-07-11 22:41:432269 if (!args->GetString(L"path", &path)) {
2270 json_return = JSONErrorString("path not specified.");
2271 reply_return = false;
2272 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
2273 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2274 " for path %s.\"}", path.c_str());
2275 reply_return = false;
2276 }
2277
2278 AutomationMsg_SendJSONRequest::WriteReplyParams(
2279 reply_message, json_return, reply_return);
2280 Send(reply_message);
[email protected]f7d48012010-05-06 08:17:052281}
2282
2283// Sample json input:
2284// { "command": "DisablePlugin",
2285// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582286void AutomationProvider::DisablePlugin(Browser* browser,
2287 DictionaryValue* args,
2288 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432289 std::string json_return = "{}";
2290 bool reply_return = true;
[email protected]f7d48012010-05-06 08:17:052291 FilePath::StringType path;
[email protected]f6ff0df2010-07-11 22:41:432292 if (!args->GetString(L"path", &path)) {
2293 json_return = JSONErrorString("path not specified.");
2294 reply_return = false;
2295 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
2296 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2297 " for path %s.\"}", path.c_str());
2298 reply_return = false;
2299 }
2300
2301 AutomationMsg_SendJSONRequest::WriteReplyParams(
2302 reply_message, json_return, reply_return);
2303 Send(reply_message);
[email protected]f7d48012010-05-06 08:17:052304}
2305
[email protected]7060bb292010-06-24 00:52:492306// Sample json input:
2307// { "command": "SaveTabContents",
2308// "tab_index": 0,
2309// "filename": <a full pathname> }
2310// Sample json output:
2311// {}
2312void AutomationProvider::SaveTabContents(Browser* browser,
2313 DictionaryValue* args,
2314 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432315 std::string json_return;
[email protected]7060bb292010-06-24 00:52:492316 int tab_index = 0;
2317 FilePath::StringType filename;
2318 FilePath::StringType parent_directory;
2319 TabContents* tab_contents = NULL;
[email protected]7060bb292010-06-24 00:52:492320
2321 if (!args->GetInteger(L"tab_index", &tab_index) ||
2322 !args->GetString(L"filename", &filename)) {
[email protected]f6ff0df2010-07-11 22:41:432323 json_return = JSONErrorString("tab_index or filename param missing");
[email protected]7060bb292010-06-24 00:52:492324 } else {
2325 tab_contents = browser->GetTabContentsAt(tab_index);
2326 if (!tab_contents) {
[email protected]f6ff0df2010-07-11 22:41:432327 json_return = JSONErrorString("no tab at tab_index");
2328 }
2329 }
2330 if (tab_contents) {
2331 // We're doing a SAVE_AS_ONLY_HTML so the the directory path isn't
2332 // used. Nevertheless, SavePackage requires it be valid. Sigh.
2333 parent_directory = FilePath(filename).DirName().value();
2334 if (!tab_contents->SavePage(FilePath(filename), FilePath(parent_directory),
2335 SavePackage::SAVE_AS_ONLY_HTML)) {
2336 json_return = JSONErrorString("Could not initiate SavePage");
2337 } else {
2338 // The observer will delete itself when done.
2339 new SavePackageNotificationObserver(tab_contents->save_package(),
2340 this, reply_message);
[email protected]7060bb292010-06-24 00:52:492341 return;
2342 }
2343 }
[email protected]f6ff0df2010-07-11 22:41:432344
2345 // If we get here, error.
2346 DCHECK(!json_return.empty());
2347 AutomationMsg_SendJSONRequest::WriteReplyParams(
2348 reply_message, json_return, false);
2349 Send(reply_message);
[email protected]7060bb292010-06-24 00:52:492350}
2351
[email protected]a0fc50d72010-07-14 21:14:192352// Refer to ClearBrowsingData() in chrome/test/pyautolib/pyauto.py for sample
2353// json input.
2354// Sample json output: {}
2355void AutomationProvider::ClearBrowsingData(Browser* browser,
2356 DictionaryValue* args,
2357 IPC::Message* reply_message) {
2358 std::string json_return = "{}";
2359
2360 std::map<std::string, BrowsingDataRemover::TimePeriod> string_to_time_period;
2361 string_to_time_period["LAST_HOUR"] = BrowsingDataRemover::LAST_HOUR;
2362 string_to_time_period["LAST_DAY"] = BrowsingDataRemover::LAST_DAY;
2363 string_to_time_period["LAST_WEEK"] = BrowsingDataRemover::LAST_WEEK;
2364 string_to_time_period["FOUR_WEEKS"] = BrowsingDataRemover::FOUR_WEEKS;
2365 string_to_time_period["EVERYTHING"] = BrowsingDataRemover::EVERYTHING;
2366
2367 std::map<std::string, int> string_to_mask_value;
2368 string_to_mask_value["HISTORY"] = BrowsingDataRemover::REMOVE_HISTORY;
2369 string_to_mask_value["DOWNLOADS"] = BrowsingDataRemover::REMOVE_DOWNLOADS;
2370 string_to_mask_value["COOKIES"] = BrowsingDataRemover::REMOVE_COOKIES;
2371 string_to_mask_value["PASSWORDS"] = BrowsingDataRemover::REMOVE_PASSWORDS;
2372 string_to_mask_value["FORM_DATA"] = BrowsingDataRemover::REMOVE_FORM_DATA;
2373 string_to_mask_value["CACHE"] = BrowsingDataRemover::REMOVE_CACHE;
2374
2375 std::string time_period;
2376 ListValue* to_remove;
2377 if (!args->GetString(L"time_period", &time_period) ||
2378 !args->GetList(L"to_remove", &to_remove)) {
2379 // TODO(nirnimesh): Here and below refactor returns with pending CL.
2380 std::string json_return =
2381 JSONErrorString("time_period must be a string and to_remove a list.");
2382 AutomationMsg_SendJSONRequest::WriteReplyParams(
2383 reply_message, json_return, false);
2384 Send(reply_message);
2385 return;
2386 }
2387
2388 int remove_mask = 0;
2389 int num_removals = to_remove->GetSize();
2390 for (int i = 0; i < num_removals; i++) {
2391 std::string removal;
2392 to_remove->GetString(i, &removal);
2393 // If the provided string is not part of the map, then error out.
2394 if (!ContainsKey(string_to_mask_value, removal)) {
2395 std::string json_return =
2396 JSONErrorString("Invalid browsing data string found in to_remove.");
2397 AutomationMsg_SendJSONRequest::WriteReplyParams(
2398 reply_message, json_return, false);
2399 Send(reply_message);
2400 return;
2401 }
2402 remove_mask |= string_to_mask_value[removal];
2403 }
2404
2405 if (!ContainsKey(string_to_time_period, time_period)) {
2406 std::string json_return =
2407 JSONErrorString("Invalid string for time_period.");
2408 AutomationMsg_SendJSONRequest::WriteReplyParams(
2409 reply_message, json_return, false);
2410 Send(reply_message);
2411 return;
2412 }
2413
2414 BrowsingDataRemover* remover = new BrowsingDataRemover(
2415 profile(), string_to_time_period[time_period], base::Time());
2416
2417 remover->AddObserver(
2418 new AutomationProviderBrowsingDataObserver(this, reply_message));
2419 remover->Remove(remove_mask);
2420 // BrowsingDataRemover deletes itself using DeleteTask.
2421 // The observer also deletes itself after sending the reply.
2422}
2423
[email protected]4d1929f12010-07-10 00:09:032424// Sample json input: { "command": "GetThemeInfo" }
2425// Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output.
2426void AutomationProvider::GetThemeInfo(Browser* browser,
2427 DictionaryValue* args,
2428 IPC::Message* reply_message) {
2429 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2430 Extension* theme = browser->profile()->GetTheme();
2431 if (theme) {
2432 return_value->SetString(L"name", theme->name());
2433 return_value->Set(L"images", theme->GetThemeImages()->DeepCopy());
2434 return_value->Set(L"colors", theme->GetThemeColors()->DeepCopy());
2435 return_value->Set(L"tints", theme->GetThemeTints()->DeepCopy());
2436 }
[email protected]f6ff0df2010-07-11 22:41:432437
2438 std::string json_return;
2439 base::JSONWriter::Write(return_value.get(), false, &json_return);
2440 AutomationMsg_SendJSONRequest::WriteReplyParams(
2441 reply_message, json_return, true);
2442 Send(reply_message);
[email protected]4d1929f12010-07-10 00:09:032443}
2444
[email protected]55846ad842010-07-09 18:22:562445// Sample json input:
2446// { "command": "GetAutoFillProfile" }
2447// Refer to GetAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2448// json output.
2449void AutomationProvider::GetAutoFillProfile(Browser* browser,
2450 DictionaryValue* args,
2451 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432452 std::string json_return;
2453 bool reply_return = true;
2454
[email protected]55846ad842010-07-09 18:22:562455 // Get the AutoFillProfiles currently in the database.
2456 int tab_index = 0;
2457 args->GetInteger(L"tab_index", &tab_index);
2458 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
[email protected]55846ad842010-07-09 18:22:562459
2460 if (tab_contents) {
2461 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2462 ->GetPersonalDataManager();
2463 if (pdm) {
2464 std::vector<AutoFillProfile*> autofill_profiles = pdm->profiles();
2465 std::vector<CreditCard*> credit_cards = pdm->credit_cards();
2466
2467 ListValue* profiles = GetListFromAutoFillProfiles(autofill_profiles);
2468 ListValue* cards = GetListFromCreditCards(credit_cards);
2469
2470 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2471
2472 return_value->Set(L"profiles", profiles);
2473 return_value->Set(L"credit_cards", cards);
[email protected]f6ff0df2010-07-11 22:41:432474
2475 base::JSONWriter::Write(return_value.get(), false, &json_return);
[email protected]55846ad842010-07-09 18:22:562476 } else {
[email protected]f6ff0df2010-07-11 22:41:432477 json_return = JSONErrorString("No PersonalDataManager.");
2478 reply_return = false;
[email protected]55846ad842010-07-09 18:22:562479 }
2480 } else {
[email protected]f6ff0df2010-07-11 22:41:432481 json_return = JSONErrorString("No tab at that index.");
2482 reply_return = false;
[email protected]55846ad842010-07-09 18:22:562483 }
[email protected]f6ff0df2010-07-11 22:41:432484
2485 AutomationMsg_SendJSONRequest::WriteReplyParams(reply_message, json_return,
2486 reply_return);
2487 Send(reply_message);
[email protected]55846ad842010-07-09 18:22:562488}
2489
2490// Refer to FillAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2491// json input.
2492// Sample json output: {}
2493void AutomationProvider::FillAutoFillProfile(Browser* browser,
2494 DictionaryValue* args,
2495 IPC::Message* reply_message) {
[email protected]f6ff0df2010-07-11 22:41:432496 std::string json_return = "{}";
2497 bool reply_return = true;
2498
[email protected]55846ad842010-07-09 18:22:562499 ListValue* profiles = NULL;
2500 ListValue* cards = NULL;
2501 args->GetList(L"profiles", &profiles);
2502 args->GetList(L"credit_cards", &cards);
[email protected]55846ad842010-07-09 18:22:562503
2504 std::vector<AutoFillProfile> autofill_profiles;
2505 std::vector<CreditCard> credit_cards;
2506 // Create an AutoFillProfile for each of the dictionary profiles.
2507 if (profiles) {
[email protected]f6ff0df2010-07-11 22:41:432508 autofill_profiles = GetAutoFillProfilesFromList(*profiles, &json_return);
[email protected]55846ad842010-07-09 18:22:562509 }
2510 // Create a CreditCard for each of the dictionary values.
2511 if (cards) {
[email protected]f6ff0df2010-07-11 22:41:432512 credit_cards = GetCreditCardsFromList(*cards, &json_return);
[email protected]55846ad842010-07-09 18:22:562513 }
2514
2515 // Save the AutoFillProfiles.
2516 int tab_index = 0;
2517 args->GetInteger(L"tab_index", &tab_index);
2518 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2519
2520 if (tab_contents) {
2521 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2522 ->GetPersonalDataManager();
2523 if (pdm) {
2524 pdm->OnAutoFillDialogApply(profiles? &autofill_profiles : NULL,
2525 cards? &credit_cards : NULL);
2526 } else {
[email protected]f6ff0df2010-07-11 22:41:432527 json_return = JSONErrorString("No PersonalDataManager.");
2528 reply_return = false;
[email protected]55846ad842010-07-09 18:22:562529 }
2530 } else {
[email protected]f6ff0df2010-07-11 22:41:432531 json_return = JSONErrorString("No tab at that index.");
2532 reply_return = false;
[email protected]55846ad842010-07-09 18:22:562533 }
[email protected]f6ff0df2010-07-11 22:41:432534 AutomationMsg_SendJSONRequest::WriteReplyParams(
2535 reply_message, json_return, reply_return);
2536 Send(reply_message);
[email protected]55846ad842010-07-09 18:22:562537}
2538
2539/* static */
2540ListValue* AutomationProvider::GetListFromAutoFillProfiles(
2541 std::vector<AutoFillProfile*> autofill_profiles) {
2542 ListValue* profiles = new ListValue;
2543
2544 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string
2545 = GetAutoFillFieldToStringMap();
2546
2547 // For each AutoFillProfile, transform it to a dictionary object to return.
2548 for (std::vector<AutoFillProfile*>::iterator it = autofill_profiles.begin();
2549 it != autofill_profiles.end(); ++it) {
2550 AutoFillProfile* profile = *it;
2551 DictionaryValue* profile_info = new DictionaryValue;
2552 profile_info->SetStringFromUTF16(L"label", profile->Label());
2553 // For each of the types, if it has a value, add it to the dictionary.
2554 for (std::map<AutoFillFieldType, std::wstring>::iterator
2555 type_it = autofill_type_to_string.begin();
2556 type_it != autofill_type_to_string.end(); ++type_it) {
2557 string16 value = profile->GetFieldText(AutoFillType(type_it->first));
2558 if (value.length()) { // If there was something stored for that value.
2559 profile_info->SetStringFromUTF16(type_it->second, value);
2560 }
2561 }
2562 profiles->Append(profile_info);
2563 }
2564 return profiles;
2565}
2566
2567/* static */
2568ListValue* AutomationProvider::GetListFromCreditCards(
2569 std::vector<CreditCard*> credit_cards) {
2570 ListValue* cards = new ListValue;
2571
2572 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2573 GetCreditCardFieldToStringMap();
2574
2575 // For each AutoFillProfile, transform it to a dictionary object to return.
2576 for (std::vector<CreditCard*>::iterator it = credit_cards.begin();
2577 it != credit_cards.end(); ++it) {
2578 CreditCard* card = *it;
2579 DictionaryValue* card_info = new DictionaryValue;
2580 card_info->SetStringFromUTF16(L"label", card->Label());
2581 // For each of the types, if it has a value, add it to the dictionary.
2582 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2583 credit_card_type_to_string.begin();
2584 type_it != credit_card_type_to_string.end(); ++type_it) {
2585 string16 value = card->GetFieldText(AutoFillType(type_it->first));
2586 // If there was something stored for that value.
2587 if (value.length()) {
2588 card_info->SetStringFromUTF16(type_it->second, value);
2589 }
2590 }
2591 cards->Append(card_info);
2592 }
2593 return cards;
2594}
2595
2596/* static */
2597std::vector<AutoFillProfile> AutomationProvider::GetAutoFillProfilesFromList(
[email protected]f6ff0df2010-07-11 22:41:432598 const ListValue& profiles, std::string* json_return) {
[email protected]55846ad842010-07-09 18:22:562599 std::vector<AutoFillProfile> autofill_profiles;
2600 DictionaryValue* profile_info = NULL;
2601 string16 profile_label;
2602 string16 current_value;
2603
2604 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string =
2605 GetAutoFillFieldToStringMap();
2606
2607 int num_profiles = profiles.GetSize();
2608 for (int i = 0; i < num_profiles; i++) {
2609 profiles.GetDictionary(i, &profile_info);
2610 profile_info->GetString("label", &profile_label);
2611 // Choose an id of 0 so that a unique id will be created.
2612 AutoFillProfile profile(profile_label, 0);
2613 // Loop through the possible profile types and add those provided.
2614 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2615 autofill_type_to_string.begin();
2616 type_it != autofill_type_to_string.end(); ++type_it) {
2617 if (profile_info->HasKey(type_it->second)) {
2618 if (profile_info->GetStringAsUTF16(type_it->second, &current_value)) {
2619 profile.SetInfo(AutoFillType(type_it->first), current_value);
2620 } else {
[email protected]f6ff0df2010-07-11 22:41:432621 *json_return = JSONErrorString("All values must be strings");
[email protected]55846ad842010-07-09 18:22:562622 break;
2623 }
2624 }
2625 }
2626 autofill_profiles.push_back(profile);
2627 }
2628 return autofill_profiles;
2629}
2630
2631/* static */
2632std::vector<CreditCard> AutomationProvider::GetCreditCardsFromList(
[email protected]f6ff0df2010-07-11 22:41:432633 const ListValue& cards, std::string* json_return) {
[email protected]55846ad842010-07-09 18:22:562634 std::vector<CreditCard> credit_cards;
2635 DictionaryValue* card_info = NULL;
2636 string16 card_label;
2637 string16 current_value;
2638
2639 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2640 GetCreditCardFieldToStringMap();
2641
2642 int num_credit_cards = cards.GetSize();
2643 for (int i = 0; i < num_credit_cards; i++) {
2644 cards.GetDictionary(i, &card_info);
2645 card_info->GetString("label", &card_label);
2646 CreditCard card(card_label, 0);
2647 // Loop through the possible credit card fields and add those provided.
2648 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2649 credit_card_type_to_string.begin();
2650 type_it != credit_card_type_to_string.end(); ++type_it) {
2651 if (card_info->HasKey(type_it->second)) {
2652 if (card_info->GetStringAsUTF16(type_it->second, &current_value)) {
2653 card.SetInfo(AutoFillType(type_it->first), current_value);
2654 } else {
[email protected]f6ff0df2010-07-11 22:41:432655 *json_return = JSONErrorString("All values must be strings");
[email protected]55846ad842010-07-09 18:22:562656 break;
2657 }
2658 }
2659 }
2660 credit_cards.push_back(card);
2661 }
2662 return credit_cards;
2663}
2664
2665/* static */
2666std::map<AutoFillFieldType, std::wstring>
2667 AutomationProvider::GetAutoFillFieldToStringMap() {
2668 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string;
2669 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2670 autofill_type_to_string[NAME_MIDDLE] = L"NAME_MIDDLE";
2671 autofill_type_to_string[NAME_LAST] = L"NAME_LAST";
2672 autofill_type_to_string[COMPANY_NAME] = L"COMPANY_NAME";
2673 autofill_type_to_string[EMAIL_ADDRESS] = L"EMAIL_ADDRESS";
2674 autofill_type_to_string[ADDRESS_HOME_LINE1] = L"ADDRESS_HOME_LINE1";
2675 autofill_type_to_string[ADDRESS_HOME_LINE2] = L"ADDRESS_HOME_LINE2";
2676 autofill_type_to_string[ADDRESS_HOME_CITY] = L"ADDRESS_HOME_CITY";
2677 autofill_type_to_string[ADDRESS_HOME_STATE] = L"ADDRESS_HOME_STATE";
2678 autofill_type_to_string[ADDRESS_HOME_ZIP] = L"ADDRESS_HOME_ZIP";
2679 autofill_type_to_string[ADDRESS_HOME_COUNTRY] = L"ADDRESS_HOME_COUNTRY";
2680 autofill_type_to_string[PHONE_HOME_NUMBER] = L"PHONE_HOME_NUMBER";
2681 autofill_type_to_string[PHONE_FAX_NUMBER] = L"PHONE_FAX_NUMBER";
2682 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2683 return autofill_type_to_string;
2684}
2685
2686/* static */
2687std::map<AutoFillFieldType, std::wstring>
2688 AutomationProvider::GetCreditCardFieldToStringMap() {
2689 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string;
2690 credit_card_type_to_string[CREDIT_CARD_NAME] = L"CREDIT_CARD_NAME";
2691 credit_card_type_to_string[CREDIT_CARD_NUMBER] = L"CREDIT_CARD_NUMBER";
2692 credit_card_type_to_string[CREDIT_CARD_TYPE] = L"CREDIT_CARD_TYPE";
2693 credit_card_type_to_string[CREDIT_CARD_EXP_MONTH] = L"CREDIT_CARD_EXP_MONTH";
2694 credit_card_type_to_string[CREDIT_CARD_EXP_4_DIGIT_YEAR] =
2695 L"CREDIT_CARD_EXP_4_DIGIT_YEAR";
2696 return credit_card_type_to_string;
2697}
2698
[email protected]f6ff0df2010-07-11 22:41:432699/* static */
2700std::string AutomationProvider::JSONErrorString(const std::string& err) {
2701 std::string prefix = "{\"error\": \"";
2702 std::string no_quote_err;
2703 std::string suffix = "\"}";
2704
2705 base::JsonDoubleQuote(err, false, &no_quote_err);
2706 return prefix + no_quote_err + suffix;
2707}
2708
[email protected]53329582010-05-14 21:10:582709void AutomationProvider::SendJSONRequest(int handle,
2710 std::string json_request,
2711 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:042712 Browser* browser = NULL;
[email protected]f6ff0df2010-07-11 22:41:432713 std::string error_string;
[email protected]59a611242010-04-02 02:24:042714 scoped_ptr<Value> values;
[email protected]59a611242010-04-02 02:24:042715
2716 // Basic error checking.
2717 if (browser_tracker_->ContainsHandle(handle)) {
2718 browser = browser_tracker_->GetResource(handle);
2719 }
2720 if (!browser) {
[email protected]f6ff0df2010-07-11 22:41:432721 error_string = "no browser object";
[email protected]59a611242010-04-02 02:24:042722 } else {
2723 base::JSONReader reader;
2724 std::string error;
[email protected]ba399672010-04-06 15:42:392725 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
[email protected]59a611242010-04-02 02:24:042726 if (!error.empty()) {
[email protected]f6ff0df2010-07-11 22:41:432727 error_string = error;
[email protected]59a611242010-04-02 02:24:042728 }
2729 }
2730
2731 // Make sure input is a dict with a string command.
2732 std::string command;
2733 DictionaryValue* dict_value = NULL;
[email protected]f6ff0df2010-07-11 22:41:432734 if (error_string.empty()) {
2735 if (values->GetType() != Value::TYPE_DICTIONARY) {
2736 error_string = "not a dict or no command key in dict";
2737 } else {
2738 // Ownership remains with "values" variable.
2739 dict_value = static_cast<DictionaryValue*>(values.get());
2740 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
2741 error_string = "no command key in dict or not a string command";
2742 }
[email protected]59a611242010-04-02 02:24:042743 }
2744 }
2745
[email protected]24e2b102010-04-29 17:56:472746 // Map json commands to their handlers.
2747 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:052748 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
2749 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
2750 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
2751
[email protected]a9ff2c02010-05-13 17:33:052752 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
[email protected]24e2b102010-04-29 17:56:472753 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:292754 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:052755
[email protected]53329582010-05-14 21:10:582756 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
2757 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
2758 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
2759 handler_map["OmniboxMovePopupSelection"] =
2760 &AutomationProvider::OmniboxMovePopupSelection;
2761
[email protected]24e2b102010-04-29 17:56:472762 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
2763 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:052764
[email protected]ef413ca2010-05-25 21:09:142765 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
2766
[email protected]f7d48012010-05-06 08:17:052767 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:472768 handler_map["WaitForAllDownloadsToComplete"] =
2769 &AutomationProvider::WaitForDownloadsToComplete;
2770
[email protected]a3cd5022010-06-16 18:25:292771 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
2772
[email protected]7060bb292010-06-24 00:52:492773 handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents;
2774
[email protected]a0fc50d72010-07-14 21:14:192775 handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData;
2776
[email protected]4d1929f12010-07-10 00:09:032777 // SetTheme() implemented using InstallExtension().
2778 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo;
2779
[email protected]55846ad842010-07-09 18:22:562780 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile;
2781 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile;
2782
[email protected]f6ff0df2010-07-11 22:41:432783 if (error_string.empty()) {
2784 if (handler_map.find(std::string(command)) != handler_map.end()) {
2785 (this->*handler_map[command])(browser, dict_value, reply_message);
2786 return;
2787 } else {
2788 error_string = "Unknown command. Options: ";
2789 for (std::map<std::string, JsonHandler>::const_iterator it =
2790 handler_map.begin(); it != handler_map.end(); ++it) {
2791 error_string += it->first + ", ";
2792 }
[email protected]d1c458a22010-07-11 06:48:402793 }
[email protected]59a611242010-04-02 02:24:042794 }
[email protected]f6ff0df2010-07-11 22:41:432795
2796 // If we hit an error, return info.
2797 // Return a dict of {"error", "descriptive_string_for_error"}.
2798 // Else return an empty dict.
2799 std::string json_string;
2800 bool success = true;
2801 if (!error_string.empty()) {
2802 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
2803 dict->SetString(L"error", error_string);
2804 base::JSONWriter::Write(dict.get(), false, &json_string);
2805 success = false;
2806 } else {
2807 json_string = "{}";
2808 }
2809 AutomationMsg_SendJSONRequest::WriteReplyParams(
2810 reply_message, json_string, success);
2811 Send(reply_message);
[email protected]59a611242010-04-02 02:24:042812}
2813
initial.commit09911bf2008-07-26 23:55:292814void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562815 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342816 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2817 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562818 DCHECK(reply_message_ == NULL);
2819 reply_message_ = reply_message;
2820
[email protected]d9f9b792009-06-24 13:17:122821 DevToolsManager::GetInstance()->InspectElement(
2822 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292823 } else {
[email protected]71f65dd2009-02-11 19:14:562824 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2825 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292826 }
2827}
2828
2829void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092830 if (reply_message_) {
2831 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2832 num_resources);
2833 Send(reply_message_);
2834 reply_message_ = NULL;
2835 }
initial.commit09911bf2008-07-26 23:55:292836}
2837
[email protected]a7eee32f2009-05-22 18:08:172838class SetProxyConfigTask : public Task {
2839 public:
[email protected]be180c802009-10-23 06:33:312840 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
2841 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:252842 : request_context_getter_(request_context_getter),
2843 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:172844 virtual void Run() {
2845 // First, deserialize the JSON string. If this fails, log and bail.
2846 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:392847 std::string error_msg;
2848 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:172849 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2850 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:392851 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:172852 return;
2853 }
2854
2855 scoped_ptr<DictionaryValue> dict(
2856 static_cast<DictionaryValue*>(root.release()));
2857 // Now put together a proxy configuration from the deserialized string.
2858 net::ProxyConfig pc;
2859 PopulateProxyConfig(*dict.get(), &pc);
2860
[email protected]be180c802009-10-23 06:33:312861 net::ProxyService* proxy_service =
2862 request_context_getter_->GetURLRequestContext()->proxy_service();
2863 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:172864 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2865 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:312866 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:172867 }
2868
2869 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2870 DCHECK(pc);
2871 bool no_proxy = false;
2872 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2873 // Make no changes to the ProxyConfig.
2874 return;
2875 }
2876 bool auto_config;
2877 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:482878 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:172879 }
2880 std::string pac_url;
2881 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:482882 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:172883 }
2884 std::string proxy_bypass_list;
2885 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:482886 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:172887 }
2888 std::string proxy_server;
2889 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:482890 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:172891 }
2892 }
2893
2894 private:
[email protected]be180c802009-10-23 06:33:312895 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:172896 std::string proxy_config_;
2897};
2898
2899
2900void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:312901 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
2902 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:172903 FilePath user_data_dir;
2904 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2905 ProfileManager* profile_manager = g_browser_process->profile_manager();
2906 DCHECK(profile_manager);
2907 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2908 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:312909 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:172910 }
[email protected]be180c802009-10-23 06:33:312911 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:172912
[email protected]fae20792009-10-28 20:31:582913 ChromeThread::PostTask(
2914 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:312915 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:172916}
2917
[email protected]4f3dc372009-02-24 00:10:292918void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:332919 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:292920 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292921 if (tab_tracker_->ContainsHandle(handle)) {
2922 NavigationController* tab = tab_tracker_->GetResource(handle);
2923 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2924 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:332925 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:292926 }
initial.commit09911bf2008-07-26 23:55:292927}
2928
[email protected]6a5670d22009-10-27 16:21:342929void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:142930 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:532931 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
2932 reply_message);
2933}
2934
2935void AutomationProvider::OpenNewBrowserWindowOfType(
2936 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:142937 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292938 // We may have no current browser windows open so don't rely on
2939 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:532940 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
2941 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:052942 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152943 if (show)
[email protected]15952e462008-11-14 00:29:052944 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292945}
2946
[email protected]71f65dd2009-02-11 19:14:562947void AutomationProvider::GetWindowForBrowser(int browser_handle,
2948 bool* success,
2949 int* handle) {
2950 *success = false;
2951 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292952
2953 if (browser_tracker_->ContainsHandle(browser_handle)) {
2954 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202955 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292956 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202957 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562958 *success = true;
initial.commit09911bf2008-07-26 23:55:292959 }
initial.commit09911bf2008-07-26 23:55:292960}
2961
2962void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562963 int browser_handle,
2964 bool* success,
2965 int* autocomplete_edit_handle) {
2966 *success = false;
2967 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292968
2969 if (browser_tracker_->ContainsHandle(browser_handle)) {
2970 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062971 LocationBar* loc_bar = browser->window()->GetLocationBar();
2972 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292973 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562974 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2975 *success = true;
initial.commit09911bf2008-07-26 23:55:292976 }
initial.commit09911bf2008-07-26 23:55:292977}
initial.commit09911bf2008-07-26 23:55:292978
[email protected]71f65dd2009-02-11 19:14:562979void AutomationProvider::ShowInterstitialPage(int tab_handle,
2980 const std::string& html_text,
2981 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292982 if (tab_tracker_->ContainsHandle(tab_handle)) {
2983 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112984 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402985
[email protected]7dad3d5f2010-03-04 00:27:012986 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:402987 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342988 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402989 GURL("about:interstitial"),
2990 html_text);
2991 interstitial->Show();
2992 return;
initial.commit09911bf2008-07-26 23:55:292993 }
[email protected]71f65dd2009-02-11 19:14:562994
[email protected]457f5cf2009-08-18 16:37:522995 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
2996 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562997 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292998}
2999
[email protected]71f65dd2009-02-11 19:14:563000void AutomationProvider::HideInterstitialPage(int tab_handle,
3001 bool* success) {
3002 *success = false;
[email protected]57c6a652009-05-04 07:58:343003 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
3004 if (tab_contents && tab_contents->interstitial_page()) {
3005 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:563006 *success = true;
initial.commit09911bf2008-07-26 23:55:293007 }
initial.commit09911bf2008-07-26 23:55:293008}
3009
[email protected]71f65dd2009-02-11 19:14:563010void AutomationProvider::CloseTab(int tab_handle,
3011 bool wait_until_closed,
3012 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293013 if (tab_tracker_->ContainsHandle(tab_handle)) {
3014 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
3015 int index;
3016 Browser* browser = Browser::GetBrowserForController(controller, &index);
3017 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:143018 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:113019 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:453020 return;
initial.commit09911bf2008-07-26 23:55:293021 }
[email protected]de246f52009-02-25 18:25:453022
3023 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:553024 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293025}
3026
[email protected]71f65dd2009-02-11 19:14:563027void AutomationProvider::CloseBrowser(int browser_handle,
3028 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293029 if (browser_tracker_->ContainsHandle(browser_handle)) {
3030 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563031 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:563032 reply_message);
[email protected]f3e99e32008-07-30 04:48:393033 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:293034 } else {
3035 NOTREACHED();
3036 }
3037}
3038
[email protected]71f65dd2009-02-11 19:14:563039void AutomationProvider::CloseBrowserAsync(int browser_handle) {
3040 if (browser_tracker_->ContainsHandle(browser_handle)) {
3041 Browser* browser = browser_tracker_->GetResource(browser_handle);
3042 browser->window()->Close();
3043 } else {
3044 NOTREACHED();
3045 }
3046}
3047
[email protected]71f65dd2009-02-11 19:14:563048void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
3049 IPC::Message* reply_message) {
3050 if (tab_tracker_->ContainsHandle(tab_handle)) {
3051 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3052 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:143053 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:563054 }
3055}
3056
[email protected]71f65dd2009-02-11 19:14:563057void AutomationProvider::GetSecurityState(int handle, bool* success,
3058 SecurityStyle* security_style,
3059 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:483060 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:293061 if (tab_tracker_->ContainsHandle(handle)) {
3062 NavigationController* tab = tab_tracker_->GetResource(handle);
3063 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563064 *success = true;
3065 *security_style = entry->ssl().security_style();
3066 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:483067 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:293068 } else {
[email protected]71f65dd2009-02-11 19:14:563069 *success = false;
3070 *security_style = SECURITY_STYLE_UNKNOWN;
3071 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:483072 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:293073 }
3074}
3075
[email protected]71f65dd2009-02-11 19:14:563076void AutomationProvider::GetPageType(int handle, bool* success,
3077 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:293078 if (tab_tracker_->ContainsHandle(handle)) {
3079 NavigationController* tab = tab_tracker_->GetResource(handle);
3080 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563081 *page_type = entry->page_type();
3082 *success = true;
initial.commit09911bf2008-07-26 23:55:293083 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:343084 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:563085 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:343086 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:563087 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293088 } else {
[email protected]71f65dd2009-02-11 19:14:563089 *success = false;
3090 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293091 }
3092}
3093
[email protected]84abba62009-10-07 17:01:443094void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
3095 int* duration_ms) {
3096 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
3097 event_name);
3098}
3099
[email protected]71f65dd2009-02-11 19:14:563100void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
3101 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293102 if (tab_tracker_->ContainsHandle(handle)) {
3103 NavigationController* tab = tab_tracker_->GetResource(handle);
3104 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:073105 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:403106 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:473107 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:343108 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:293109 if (ssl_blocking_page) {
3110 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:013111 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:563112 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:293113 return;
3114 }
3115 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:523116 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3117 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:563118 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293119 return;
3120 }
3121 }
3122 }
3123 // We failed.
[email protected]457f5cf2009-08-18 16:37:523124 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3125 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563126 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293127}
3128
[email protected]71f65dd2009-02-11 19:14:563129void AutomationProvider::BringBrowserToFront(int browser_handle,
3130 bool* success) {
initial.commit09911bf2008-07-26 23:55:293131 if (browser_tracker_->ContainsHandle(browser_handle)) {
3132 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:063133 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:563134 *success = true;
initial.commit09911bf2008-07-26 23:55:293135 } else {
[email protected]71f65dd2009-02-11 19:14:563136 *success = false;
initial.commit09911bf2008-07-26 23:55:293137 }
3138}
3139
[email protected]71f65dd2009-02-11 19:14:563140void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
3141 int message_num,
3142 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:293143 if (browser_tracker_->ContainsHandle(browser_handle)) {
3144 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563145 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:143146 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:293147 } else {
[email protected]71f65dd2009-02-11 19:14:563148 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:293149 }
3150}
3151
[email protected]71f65dd2009-02-11 19:14:563152void AutomationProvider::PrintNow(int tab_handle,
3153 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:573154 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:343155 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3156 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:293157 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:573158 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:143159 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:343160 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:573161 return;
initial.commit09911bf2008-07-26 23:55:293162 }
[email protected]71f65dd2009-02-11 19:14:563163 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
3164 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293165}
[email protected]d301c952009-07-13 15:02:413166
[email protected]71f65dd2009-02-11 19:14:563167void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:113168 const FilePath& file_name,
3169 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:563170 int type,
3171 bool* success) {
initial.commit09911bf2008-07-26 23:55:293172 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:563173 *success = false;
initial.commit09911bf2008-07-26 23:55:293174 return;
3175 }
3176
3177 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3178 Browser* browser = FindAndActivateTab(nav);
3179 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:143180 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:563181 *success = false;
initial.commit09911bf2008-07-26 23:55:293182 return;
3183 }
3184
initial.commit09911bf2008-07-26 23:55:293185 SavePackage::SavePackageType save_type =
3186 static_cast<SavePackage::SavePackageType>(type);
3187 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
3188 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:343189 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:293190
[email protected]71f65dd2009-02-11 19:14:563191 *success = true;
initial.commit09911bf2008-07-26 23:55:293192}
3193
[email protected]71f65dd2009-02-11 19:14:563194void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
3195 bool* success,
3196 std::wstring* text) {
3197 *success = false;
initial.commit09911bf2008-07-26 23:55:293198 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563199 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:523200 GetText();
[email protected]71f65dd2009-02-11 19:14:563201 *success = true;
initial.commit09911bf2008-07-26 23:55:293202 }
initial.commit09911bf2008-07-26 23:55:293203}
3204
[email protected]71f65dd2009-02-11 19:14:563205void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
3206 const std::wstring& text,
3207 bool* success) {
3208 *success = false;
initial.commit09911bf2008-07-26 23:55:293209 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:523210 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
3211 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:563212 *success = true;
initial.commit09911bf2008-07-26 23:55:293213 }
initial.commit09911bf2008-07-26 23:55:293214}
3215
3216void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:563217 int autocomplete_edit_handle,
3218 bool* success,
3219 std::vector<AutocompleteMatchData>* matches) {
3220 *success = false;
initial.commit09911bf2008-07-26 23:55:293221 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:273222 const AutocompleteResult& result = autocomplete_edit_tracker_->
3223 GetResource(autocomplete_edit_handle)->model()->result();
3224 for (AutocompleteResult::const_iterator i = result.begin();
3225 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:563226 matches->push_back(AutocompleteMatchData(*i));
3227 *success = true;
initial.commit09911bf2008-07-26 23:55:293228 }
initial.commit09911bf2008-07-26 23:55:293229}
3230
3231void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:563232 int autocomplete_edit_handle,
3233 bool* success,
3234 bool* query_in_progress) {
3235 *success = false;
3236 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:293237 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563238 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:523239 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:563240 *success = true;
initial.commit09911bf2008-07-26 23:55:293241 }
initial.commit09911bf2008-07-26 23:55:293242}
3243
[email protected]63514af2010-03-30 17:17:233244#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:193245
[email protected]5ae5bed2009-08-21 18:52:443246#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:393247
[email protected]57c6a652009-05-04 07:58:343248TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:573249 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:573250 if (tab_tracker_->ContainsHandle(handle)) {
3251 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:343252 if (tab)
3253 *tab = nav_controller;
3254 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:573255 }
[email protected]57c6a652009-05-04 07:58:343256 return NULL;
[email protected]20e93d12008-08-28 16:31:573257}
3258
initial.commit09911bf2008-07-26 23:55:293259TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
3260 : AutomationProvider(profile) {
3261 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:143262 registrar_.Add(this, NotificationType::SESSION_END,
3263 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:293264}
3265
3266TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:293267 BrowserList::RemoveObserver(this);
3268}
3269
3270void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:143271 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:293272 AutomationProvider::OnChannelError();
3273}
3274
3275void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
3276 // For backwards compatibility with the testing automation interface, we
3277 // want the automation provider (and hence the process) to go away when the
3278 // last browser goes away.
3279 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:293280 // If you change this, update Observer for NotificationType::SESSION_END
3281 // below.
[email protected]295039bd2008-08-15 04:32:573282 MessageLoop::current()->PostTask(FROM_HERE,
3283 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:293284 }
3285}
3286
3287void TestingAutomationProvider::Observe(NotificationType type,
3288 const NotificationSource& source,
3289 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:563290 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:293291 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
3292 // before the task runs resulting in this object not being deleted. This
3293 // Release balance out the Release scheduled by OnBrowserRemoving.
3294 Release();
3295}
[email protected]295039bd2008-08-15 04:32:573296
3297void TestingAutomationProvider::OnRemoveProvider() {
3298 AutomationProviderList::GetInstance()->RemoveProvider(this);
3299}
[email protected]8a3422c92008-09-24 17:42:423300
[email protected]816633a2009-11-11 21:48:183301void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:563302 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:423303 if (tab_tracker_->ContainsHandle(handle)) {
3304 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:343305 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:113306 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423307 }
[email protected]8a3422c92008-09-24 17:42:423308}
3309
[email protected]816633a2009-11-11 21:48:183310void AutomationProvider::ClickInfoBarAccept(int handle,
3311 int info_bar_index,
3312 bool wait_for_navigation,
3313 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:423314 bool success = false;
3315 if (tab_tracker_->ContainsHandle(handle)) {
3316 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3317 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:113318 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423319 if (info_bar_index >= 0 && info_bar_index < count) {
3320 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:013321 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:423322 }
[email protected]eb9ba192008-12-02 02:41:343323 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:113324 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:343325 info_bar_index);
3326 if (delegate->AsConfirmInfoBarDelegate())
3327 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:423328 success = true;
3329 }
3330 }
[email protected]4f3dc372009-02-24 00:10:293331 }
[email protected]58f622a62009-10-04 01:17:553332
3333 // This "!wait_for_navigation || !success condition" logic looks suspicious.
3334 // It will send a failure message when success is true but
3335 // |wait_for_navigation| is false.
3336 // TODO(phajdan.jr): investgate whether the reply param (currently
3337 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:423338 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:183339 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:523340 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:423341}
3342
[email protected]71f65dd2009-02-11 19:14:563343void AutomationProvider::GetLastNavigationTime(int handle,
3344 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:423345 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:563346 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:423347}
3348
[email protected]71f65dd2009-02-11 19:14:563349void AutomationProvider::WaitForNavigation(int handle,
3350 int64 last_navigation_time,
3351 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:253352 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:423353 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:253354
[email protected]8a3422c92008-09-24 17:42:423355 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:563356 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:523357 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
3358 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:553359 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:293360 return;
[email protected]8a3422c92008-09-24 17:42:423361 }
3362
[email protected]7dad3d5f2010-03-04 00:27:013363 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:423364}
3365
[email protected]71f65dd2009-02-11 19:14:563366void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163367 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563368 int value,
3369 bool* success) {
3370 *success = false;
[email protected]8a3422c92008-09-24 17:42:423371 if (browser_tracker_->ContainsHandle(handle)) {
3372 Browser* browser = browser_tracker_->GetResource(handle);
3373 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563374 *success = true;
[email protected]8a3422c92008-09-24 17:42:423375 }
[email protected]8a3422c92008-09-24 17:42:423376}
[email protected]97fa6ce32008-12-19 01:48:163377
[email protected]71f65dd2009-02-11 19:14:563378void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163379 const std::wstring& name,
[email protected]ddd231e2010-06-29 20:35:193380 const std::string& value,
[email protected]71f65dd2009-02-11 19:14:563381 bool* success) {
3382 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163383 if (browser_tracker_->ContainsHandle(handle)) {
3384 Browser* browser = browser_tracker_->GetResource(handle);
3385 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563386 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163387 }
[email protected]97fa6ce32008-12-19 01:48:163388}
3389
[email protected]71f65dd2009-02-11 19:14:563390void AutomationProvider::GetBooleanPreference(int handle,
3391 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:363392 bool* success,
3393 bool* value) {
[email protected]71f65dd2009-02-11 19:14:563394 *success = false;
3395 *value = false;
[email protected]97fa6ce32008-12-19 01:48:163396 if (browser_tracker_->ContainsHandle(handle)) {
3397 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:563398 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
3399 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163400 }
[email protected]97fa6ce32008-12-19 01:48:163401}
3402
[email protected]71f65dd2009-02-11 19:14:563403void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163404 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563405 bool value,
3406 bool* success) {
3407 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163408 if (browser_tracker_->ContainsHandle(handle)) {
3409 Browser* browser = browser_tracker_->GetResource(handle);
3410 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563411 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163412 }
[email protected]97fa6ce32008-12-19 01:48:163413}
3414
3415// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563416void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:403417 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:163418 if (tab_tracker_->ContainsHandle(tab_handle)) {
3419 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3420 Browser* browser = FindAndActivateTab(nav);
3421 DCHECK(browser);
3422
[email protected]57c6a652009-05-04 07:58:343423 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
3424 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:163425 }
[email protected]97fa6ce32008-12-19 01:48:163426}
3427
[email protected]b8f48d12009-11-09 20:14:363428// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563429void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:403430 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:563431 bool* success) {
3432 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163433 if (tab_tracker_->ContainsHandle(tab_handle)) {
3434 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:193435 if (!nav)
3436 return;
[email protected]97fa6ce32008-12-19 01:48:163437 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:163438
[email protected]2f2afba2010-04-01 01:53:193439 // If the browser has UI, simulate what a user would do.
3440 // Activate the tab and then click the encoding menu.
3441 if (browser &&
3442 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:163443 int selected_encoding_id =
3444 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
3445 if (selected_encoding_id) {
3446 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:563447 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163448 }
[email protected]2f2afba2010-04-01 01:53:193449 } else {
3450 // There is no UI, Chrome probably runs as Chrome-Frame mode.
3451 // Try to get TabContents and call its override_encoding method.
3452 TabContents* contents = nav->tab_contents();
3453 if (!contents)
3454 return;
3455 const std::string selected_encoding =
3456 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
3457 if (selected_encoding.empty())
3458 return;
3459 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:163460 }
3461 }
[email protected]97fa6ce32008-12-19 01:48:163462}
[email protected]5bcdb312009-01-07 21:43:203463
[email protected]4d434a1a2009-02-11 21:06:573464void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:203465 SavePackage::SetShouldPromptUser(should_prompt);
3466}
[email protected]87eab222009-03-13 00:47:453467
[email protected]66ba4932009-06-04 19:22:133468void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
3469 *count = -1; // -1 is the error code
3470 if (tab_tracker_->ContainsHandle(handle)) {
3471 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3472 TabContents* tab_contents = nav_controller->tab_contents();
3473 if (tab_contents) {
3474 BlockedPopupContainer* container =
3475 tab_contents->blocked_popup_container();
3476 if (container) {
3477 *count = static_cast<int>(container->GetBlockedPopupCount());
3478 } else {
3479 // If we don't have a container, we don't have any blocked popups to
3480 // contain!
3481 *count = 0;
3482 }
3483 }
3484 }
3485}
[email protected]f7a68432009-07-29 23:18:193486
3487void AutomationProvider::SelectAll(int tab_handle) {
3488 RenderViewHost* view = GetViewForTab(tab_handle);
3489 if (!view) {
3490 NOTREACHED();
3491 return;
3492 }
3493
3494 view->SelectAll();
3495}
3496
3497void AutomationProvider::Cut(int tab_handle) {
3498 RenderViewHost* view = GetViewForTab(tab_handle);
3499 if (!view) {
3500 NOTREACHED();
3501 return;
3502 }
3503
3504 view->Cut();
3505}
3506
3507void AutomationProvider::Copy(int tab_handle) {
3508 RenderViewHost* view = GetViewForTab(tab_handle);
3509 if (!view) {
3510 NOTREACHED();
3511 return;
3512 }
3513
3514 view->Copy();
3515}
3516
3517void AutomationProvider::Paste(int tab_handle) {
3518 RenderViewHost* view = GetViewForTab(tab_handle);
3519 if (!view) {
3520 NOTREACHED();
3521 return;
3522 }
3523
3524 view->Paste();
3525}
3526
3527void AutomationProvider::ReloadAsync(int tab_handle) {
3528 if (tab_tracker_->ContainsHandle(tab_handle)) {
3529 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3530 if (!tab) {
3531 NOTREACHED();
3532 return;
3533 }
3534
[email protected]106a0812010-03-18 00:15:123535 const bool check_for_repost = true;
3536 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193537 }
3538}
3539
3540void AutomationProvider::StopAsync(int tab_handle) {
3541 RenderViewHost* view = GetViewForTab(tab_handle);
3542 if (!view) {
[email protected]8b2b3312009-09-14 18:38:363543 // We tolerate StopAsync being called even before a view has been created.
3544 // So just log a warning instead of a NOTREACHED().
3545 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:193546 return;
3547 }
3548
3549 view->Stop();
3550}
3551
[email protected]1bb5f892009-10-06 01:44:573552void AutomationProvider::OnSetPageFontSize(int tab_handle,
3553 int font_size) {
3554 AutomationPageFontSize automation_font_size =
3555 static_cast<AutomationPageFontSize>(font_size);
3556
3557 if (automation_font_size < SMALLEST_FONT ||
3558 automation_font_size > LARGEST_FONT) {
3559 DLOG(ERROR) << "Invalid font size specified : "
3560 << font_size;
3561 return;
3562 }
3563
3564 if (tab_tracker_->ContainsHandle(tab_handle)) {
3565 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3566 DCHECK(tab != NULL);
3567 if (tab && tab->tab_contents()) {
3568 DCHECK(tab->tab_contents()->profile() != NULL);
3569 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
3570 prefs::kWebKitDefaultFontSize, font_size);
3571 }
3572 }
3573}
3574
[email protected]bc73b4e52010-03-26 04:16:203575void AutomationProvider::RemoveBrowsingData(int remove_mask) {
3576 BrowsingDataRemover* remover;
3577 remover = new BrowsingDataRemover(profile(),
3578 BrowsingDataRemover::EVERYTHING, // All time periods.
3579 base::Time());
3580 remover->Remove(remove_mask);
3581 // BrowsingDataRemover deletes itself.
3582}
[email protected]1bb5f892009-10-06 01:44:573583
[email protected]2949e90d2009-08-21 15:32:523584void AutomationProvider::WaitForBrowserWindowCountToBecome(
3585 int target_count, IPC::Message* reply_message) {
3586 if (static_cast<int>(BrowserList::size()) == target_count) {
3587 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
3588 reply_message, true);
3589 Send(reply_message);
3590 return;
3591 }
3592
3593 // Set up an observer (it will delete itself).
3594 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
3595}
3596
3597void AutomationProvider::WaitForAppModalDialogToBeShown(
3598 IPC::Message* reply_message) {
3599 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
3600 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
3601 reply_message, true);
3602 Send(reply_message);
3603 return;
3604 }
3605
3606 // Set up an observer (it will delete itself).
3607 new AppModalDialogShownObserver(this, reply_message);
3608}
3609
[email protected]1126a1d32009-08-26 15:39:263610void AutomationProvider::GoBackBlockUntilNavigationsComplete(
3611 int handle, int number_of_navigations, IPC::Message* reply_message) {
3612 if (tab_tracker_->ContainsHandle(handle)) {
3613 NavigationController* tab = tab_tracker_->GetResource(handle);
3614 Browser* browser = FindAndActivateTab(tab);
3615 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:013616 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3617 false);
[email protected]1126a1d32009-08-26 15:39:263618 browser->GoBack(CURRENT_TAB);
3619 return;
3620 }
3621 }
3622
3623 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
3624 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3625 Send(reply_message);
3626}
3627
3628void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
3629 int handle, int number_of_navigations, IPC::Message* reply_message) {
3630 if (tab_tracker_->ContainsHandle(handle)) {
3631 NavigationController* tab = tab_tracker_->GetResource(handle);
3632 Browser* browser = FindAndActivateTab(tab);
3633 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:013634 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3635 false);
[email protected]1126a1d32009-08-26 15:39:263636 browser->GoForward(CURRENT_TAB);
3637 return;
3638 }
3639 }
3640
3641 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
3642 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3643 Send(reply_message);
3644}
3645
[email protected]f7a68432009-07-29 23:18:193646RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
3647 if (tab_tracker_->ContainsHandle(tab_handle)) {
3648 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3649 if (!tab) {
3650 NOTREACHED();
3651 return NULL;
3652 }
3653
3654 TabContents* tab_contents = tab->tab_contents();
3655 if (!tab_contents) {
3656 NOTREACHED();
3657 return NULL;
3658 }
3659
3660 RenderViewHost* view_host = tab_contents->render_view_host();
3661 return view_host;
3662 }
3663
3664 return NULL;
3665}
[email protected]675595f2009-08-26 22:32:043666
3667void AutomationProvider::GetBrowserForWindow(int window_handle,
3668 bool* success,
3669 int* browser_handle) {
3670 *success = false;
3671 *browser_handle = 0;
3672
3673 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
3674 if (!window)
3675 return;
3676
3677 BrowserList::const_iterator iter = BrowserList::begin();
3678 for (;iter != BrowserList::end(); ++iter) {
3679 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
3680 if (window == this_window) {
3681 // Add() returns the existing handle for the resource if any.
3682 *browser_handle = browser_tracker_->Add(*iter);
3683 *success = true;
3684 return;
3685 }
3686 }
3687}
[email protected]d11c8e92009-10-20 23:26:403688
3689void AutomationProvider::InstallExtension(const FilePath& crx_path,
3690 IPC::Message* reply_message) {
3691 ExtensionsService* service = profile_->GetExtensionsService();
3692 if (service) {
3693 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193694 new ExtensionInstallNotificationObserver(this,
3695 AutomationMsg_InstallExtension::ID,
3696 reply_message);
[email protected]d11c8e92009-10-20 23:26:403697
3698 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:163699 scoped_refptr<CrxInstaller> installer(
3700 new CrxInstaller(install_dir,
3701 service,
3702 NULL)); // silent install, no UI
3703 installer->set_allow_privilege_increase(true);
3704 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:403705 } else {
3706 AutomationMsg_InstallExtension::WriteReplyParams(
3707 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3708 Send(reply_message);
3709 }
3710}
3711
3712void AutomationProvider::LoadExpandedExtension(
3713 const FilePath& extension_dir,
3714 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:383715 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:403716 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193717 new ExtensionInstallNotificationObserver(
3718 this,
3719 AutomationMsg_LoadExpandedExtension::ID,
3720 reply_message);
[email protected]d11c8e92009-10-20 23:26:403721
3722 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:403723 } else {
3724 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
3725 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3726 Send(reply_message);
3727 }
3728}
[email protected]673fd2c02010-02-04 23:10:003729
[email protected]a1e62d12010-03-16 02:18:433730void AutomationProvider::GetEnabledExtensions(
3731 std::vector<FilePath>* result) {
3732 ExtensionsService* service = profile_->GetExtensionsService();
3733 DCHECK(service);
3734 if (service->extensions_enabled()) {
3735 const ExtensionList* extensions = service->extensions();
3736 DCHECK(extensions);
3737 for (size_t i = 0; i < extensions->size(); ++i) {
3738 Extension* extension = (*extensions)[i];
3739 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:123740 if (extension->location() == Extension::INTERNAL ||
3741 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:073742 result->push_back(extension->path());
3743 }
[email protected]a1e62d12010-03-16 02:18:433744 }
3745 }
3746}
3747
[email protected]790788ac2010-04-06 17:52:193748void AutomationProvider::WaitForExtensionTestResult(
3749 IPC::Message* reply_message) {
3750 DCHECK(reply_message_ == NULL);
3751 reply_message_ = reply_message;
3752 // Call MaybeSendResult, because the result might have come in before
3753 // we were waiting on it.
3754 extension_test_result_observer_->MaybeSendResult();
3755}
3756
3757void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:093758 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:193759 ExtensionsService* service = profile_->GetExtensionsService();
3760 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3761 if (service && manager) {
3762 // The observer will delete itself when done.
3763 new ExtensionReadyNotificationObserver(
3764 manager,
3765 this,
3766 AutomationMsg_InstallExtensionAndGetHandle::ID,
3767 reply_message);
3768
[email protected]d7e5525d2010-04-20 14:37:093769 ExtensionInstallUI* client =
3770 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:193771 scoped_refptr<CrxInstaller> installer(
3772 new CrxInstaller(service->install_directory(),
3773 service,
[email protected]d7e5525d2010-04-20 14:37:093774 client));
[email protected]790788ac2010-04-06 17:52:193775 installer->set_allow_privilege_increase(true);
3776 installer->InstallCrx(crx_path);
3777 } else {
3778 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
3779 reply_message, 0);
3780 Send(reply_message);
3781 }
3782}
3783
3784void AutomationProvider::UninstallExtension(int extension_handle,
3785 bool* success) {
3786 *success = false;
3787 Extension* extension = GetExtension(extension_handle);
3788 ExtensionsService* service = profile_->GetExtensionsService();
3789 if (extension && service) {
3790 ExtensionUnloadNotificationObserver observer;
3791 service->UninstallExtension(extension->id(), false);
3792 // The extension unload notification should have been sent synchronously
3793 // with the uninstall. Just to be safe, check that it was received.
3794 *success = observer.did_receive_unload_notification();
3795 }
3796}
3797
3798void AutomationProvider::EnableExtension(int extension_handle,
3799 IPC::Message* reply_message) {
3800 Extension* extension = GetDisabledExtension(extension_handle);
3801 ExtensionsService* service = profile_->GetExtensionsService();
3802 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3803 // Only enable if this extension is disabled.
3804 if (extension && service && manager) {
3805 // The observer will delete itself when done.
3806 new ExtensionReadyNotificationObserver(
3807 manager,
3808 this,
3809 AutomationMsg_EnableExtension::ID,
3810 reply_message);
3811 service->EnableExtension(extension->id());
3812 } else {
3813 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
3814 Send(reply_message);
3815 }
3816}
3817
3818void AutomationProvider::DisableExtension(int extension_handle,
3819 bool* success) {
3820 *success = false;
3821 Extension* extension = GetEnabledExtension(extension_handle);
3822 ExtensionsService* service = profile_->GetExtensionsService();
3823 if (extension && service) {
3824 ExtensionUnloadNotificationObserver observer;
3825 service->DisableExtension(extension->id());
3826 // The extension unload notification should have been sent synchronously
3827 // with the disable. Just to be safe, check that it was received.
3828 *success = observer.did_receive_unload_notification();
3829 }
3830}
3831
3832void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
3833 int extension_handle, int browser_handle,
3834 IPC::Message* reply_message) {
3835 bool success = false;
3836 Extension* extension = GetEnabledExtension(extension_handle);
3837 ExtensionsService* service = profile_->GetExtensionsService();
3838 ExtensionMessageService* message_service =
3839 profile_->GetExtensionMessageService();
3840 Browser* browser = browser_tracker_->GetResource(browser_handle);
3841 if (extension && service && message_service && browser) {
3842 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
3843 if (extension->page_action()) {
3844 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
3845 browser->profile(), extension->id(), "action", tab_id, "", 1);
3846 success = true;
3847 } else if (extension->browser_action()) {
3848 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
3849 browser->profile(), extension->id(), browser);
3850 success = true;
3851 }
3852 }
3853 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3854 reply_message, success);
3855 Send(reply_message);
3856}
3857
3858void AutomationProvider::MoveExtensionBrowserAction(
3859 int extension_handle, int index, bool* success) {
3860 *success = false;
3861 Extension* extension = GetEnabledExtension(extension_handle);
3862 ExtensionsService* service = profile_->GetExtensionsService();
3863 if (extension && service) {
3864 ExtensionToolbarModel* toolbar = service->toolbar_model();
3865 if (toolbar) {
3866 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3867 toolbar->MoveBrowserAction(extension, index);
3868 *success = true;
3869 } else {
3870 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3871 }
3872 }
3873 }
3874}
3875
3876void AutomationProvider::GetExtensionProperty(
3877 int extension_handle,
3878 AutomationMsg_ExtensionProperty type,
3879 bool* success,
3880 std::string* value) {
3881 *success = false;
3882 Extension* extension = GetExtension(extension_handle);
3883 ExtensionsService* service = profile_->GetExtensionsService();
3884 if (extension && service) {
3885 ExtensionToolbarModel* toolbar = service->toolbar_model();
3886 int found_index = -1;
3887 int index = 0;
3888 switch (type) {
3889 case AUTOMATION_MSG_EXTENSION_ID:
3890 *value = extension->id();
3891 *success = true;
3892 break;
3893 case AUTOMATION_MSG_EXTENSION_NAME:
3894 *value = extension->name();
3895 *success = true;
3896 break;
3897 case AUTOMATION_MSG_EXTENSION_VERSION:
3898 *value = extension->VersionString();
3899 *success = true;
3900 break;
3901 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3902 if (toolbar) {
3903 for (ExtensionList::const_iterator iter = toolbar->begin();
3904 iter != toolbar->end(); iter++) {
3905 // Skip this extension if we are in incognito mode
3906 // and it is not incognito-enabled.
3907 if (profile_->IsOffTheRecord() &&
3908 !service->IsIncognitoEnabled(*iter))
3909 continue;
3910 if (*iter == extension) {
3911 found_index = index;
3912 break;
3913 }
3914 index++;
3915 }
3916 *value = IntToString(found_index);
3917 *success = true;
3918 }
3919 break;
3920 default:
3921 LOG(WARNING) << "Trying to get undefined extension property";
3922 break;
3923 }
3924 }
3925}
3926
[email protected]673fd2c02010-02-04 23:10:003927void AutomationProvider::SaveAsAsync(int tab_handle) {
3928 NavigationController* tab = NULL;
3929 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3930 if (tab_contents)
3931 tab_contents->OnSavePage();
3932}
[email protected]7dad3d5f2010-03-04 00:27:013933
3934void AutomationProvider::SetContentSetting(
3935 int handle,
3936 const std::string& host,
3937 ContentSettingsType content_type,
3938 ContentSetting setting,
3939 bool* success) {
3940 *success = false;
3941 if (browser_tracker_->ContainsHandle(handle)) {
3942 Browser* browser = browser_tracker_->GetResource(handle);
3943 HostContentSettingsMap* map =
3944 browser->profile()->GetHostContentSettingsMap();
3945 if (host.empty()) {
3946 map->SetDefaultContentSetting(content_type, setting);
3947 } else {
[email protected]0314ae02010-04-08 09:18:293948 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3949 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013950 }
3951 *success = true;
3952 }
3953}
[email protected]cc824372010-03-31 15:33:013954
3955#if !defined(TOOLKIT_VIEWS)
3956void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3957 NOTIMPLEMENTED();
3958};
3959
3960void AutomationProvider::WaitForFocusedViewIDToChange(
3961 int handle, int previous_view_id, IPC::Message* reply_message) {
3962 NOTIMPLEMENTED();
3963}
3964
3965void AutomationProvider::StartTrackingPopupMenus(
3966 int browser_handle, bool* success) {
3967 NOTIMPLEMENTED();
3968}
3969
3970void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3971 NOTIMPLEMENTED();
3972}
3973#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:093974
3975void AutomationProvider::ResetToDefaultTheme() {
3976 profile_->ClearTheme();
3977}