blob: ca2897de7d817bd48604abf128aa6739e58e7f37 [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"
[email protected]7c983cc2010-07-16 11:33:3434#include "chrome/browser/automation/automation_provider_json.h"
initial.commit09911bf2008-07-26 23:55:2935#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0836#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]f44265b2009-05-19 18:52:5037#include "chrome/browser/automation/extension_port_container.h"
[email protected]12802702010-07-09 19:43:0938#include "chrome/browser/autocomplete/autocomplete_edit.h"
[email protected]66ba4932009-06-04 19:22:1339#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5340#include "chrome/browser/bookmarks/bookmark_model.h"
41#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]ef413ca2010-05-25 21:09:1442#include "chrome/browser/browser_list.h"
[email protected]5c238752009-06-13 10:29:0743#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3944#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2045#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0546#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5847#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2948#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1249#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5950#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2251#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0552#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4053#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1954#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]ef413ca2010-05-25 21:09:1455#include "chrome/browser/extensions/extension_host.h"
[email protected]d11c8e92009-10-20 23:26:4056#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5557#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1958#include "chrome/browser/extensions/extension_tabs_module.h"
59#include "chrome/browser/extensions/extension_toolbar_model.h"
60#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1661#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5862#include "chrome/browser/find_bar.h"
63#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2964#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0165#include "chrome/browser/host_content_settings_map.h"
[email protected]c5aa5322010-07-15 19:00:0766#include "chrome/browser/importer/importer.h"
67#include "chrome/browser/importer/importer_data_types.h"
[email protected]0ac83682010-01-22 17:46:2768#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0669#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0170#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5371#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2472#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0873#include "chrome/browser/pref_service.h"
[email protected]f83f9102010-05-04 17:01:0574#include "chrome/browser/printing/print_job.h"
[email protected]a7eee32f2009-05-22 18:08:1775#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0776#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2577#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0378#include "chrome/browser/ssl/ssl_manager.h"
79#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]b5558cf22010-07-12 17:30:0680#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]57c6a652009-05-04 07:58:3481#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4582#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1783#include "chrome/common/automation_constants.h"
[email protected]a9ff2c02010-05-13 17:33:0584#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2985#include "chrome/common/chrome_paths.h"
[email protected]790788ac2010-04-06 17:52:1986#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1787#include "chrome/common/json_value_serializer.h"
[email protected]68d2a05f2010-05-07 21:39:5588#include "chrome/common/net/url_request_context_getter.h"
[email protected]1c58a5c2009-05-21 18:47:1489#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5790#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2591#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5692#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5793#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1794#include "net/proxy/proxy_service.h"
95#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2196#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:3097#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:2898#include "views/event.h"
[email protected]f7d48012010-05-06 08:17:0599#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:29100
[email protected]de246f52009-02-25 18:25:45101#if defined(OS_WIN)
[email protected]4bdde602010-06-16 03:17:35102#include "chrome/browser/external_tab_container_win.h"
[email protected]de246f52009-02-25 18:25:45103#endif // defined(OS_WIN)
104
[email protected]e1acf6f2008-10-27 20:43:33105using base::Time;
106
[email protected]cbab76d2008-10-13 22:42:47107class AutomationInterstitialPage : public InterstitialPage {
108 public:
[email protected]57c6a652009-05-04 07:58:34109 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47110 const GURL& url,
111 const std::string& contents)
112 : InterstitialPage(tab, true, url),
113 contents_(contents) {
114 }
115
116 virtual std::string GetHTMLContents() { return contents_; }
117
118 private:
119 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29120
[email protected]cbab76d2008-10-13 22:42:47121 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
122};
123
[email protected]c2cb8542009-08-20 21:16:51124class ClickTask : public Task {
125 public:
[email protected]fc2e0872009-08-21 22:14:41126 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51127 virtual ~ClickTask() {}
128
129 virtual void Run() {
130 ui_controls::MouseButton button = ui_controls::LEFT;
131 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
132 views::Event::EF_LEFT_BUTTON_DOWN) {
133 button = ui_controls::LEFT;
134 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
135 views::Event::EF_RIGHT_BUTTON_DOWN) {
136 button = ui_controls::RIGHT;
137 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
138 views::Event::EF_MIDDLE_BUTTON_DOWN) {
139 button = ui_controls::MIDDLE;
140 } else {
141 NOTREACHED();
142 }
143
[email protected]fc2e0872009-08-21 22:14:41144 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51145 }
146
147 private:
[email protected]c2cb8542009-08-20 21:16:51148 int flags_;
149
150 DISALLOW_COPY_AND_ASSIGN(ClickTask);
151};
[email protected]c2cb8542009-08-20 21:16:51152
initial.commit09911bf2008-07-26 23:55:29153AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57154 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56155 profile_(profile),
[email protected]cc824372010-03-31 15:33:01156 reply_message_(NULL),
157 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29158 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19159 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29160 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20161 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29162 autocomplete_edit_tracker_.reset(
163 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29164 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
165 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44166 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19167 extension_test_result_observer_.reset(
168 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13169 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29170}
171
172AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50173 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
174 port_containers_.end());
175 port_containers_.clear();
176
[email protected]0da050b92008-08-19 19:29:47177 // Make sure that any outstanding NotificationObservers also get destroyed.
178 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31179 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47180 while ((observer = it.GetNext()) != NULL)
181 delete observer;
[email protected]528211a2010-01-14 15:25:13182
183 if (channel_.get()) {
184 channel_->Close();
185 }
186 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29187}
188
[email protected]9a3a293b2009-06-04 22:28:16189void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06190 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57191 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06192 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
193 automation_resource_message_filter_,
194 g_browser_process->io_thread()->message_loop(),
195 true, g_browser_process->shutdown_event()));
[email protected]bcff05a2010-04-14 01:46:43196 scoped_ptr<FileVersionInfo> version_info(
197 chrome_app::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40198 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43199 if (version_info != NULL) {
200 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40201 }
[email protected]c6cb1992009-04-13 16:45:29202
203 // Send a hello message with our current automation protocol version.
204 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29205}
206
207void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
208 if (expected_tabs == 0) {
209 Send(new AutomationMsg_InitialLoadsComplete(0));
210 } else {
211 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
212 }
213}
214
215NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58216 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01217 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29218 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58219 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01220 number_of_navigations,
221 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29222
[email protected]71f65dd2009-02-11 19:14:56223 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29224 return observer;
225}
226
[email protected]faf2ee42010-05-11 14:26:17227void AutomationProvider::RemoveNavigationStatusListener(
228 NotificationObserver* obs) {
229 notification_observer_list_.RemoveObserver(obs);
230}
231
initial.commit09911bf2008-07-26 23:55:29232NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14233 Browser* parent,
234 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56235 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14236 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29237 notification_observer_list_.AddObserver(observer);
238
239 return observer;
240}
241
[email protected]faf2ee42010-05-11 14:26:17242void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
initial.commit09911bf2008-07-26 23:55:29243 notification_observer_list_.RemoveObserver(obs);
244}
245
246void AutomationProvider::AddLoginHandler(NavigationController* tab,
247 LoginHandler* handler) {
248 login_handler_map_[tab] = handler;
249}
250
251void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
252 DCHECK(login_handler_map_[tab]);
253 login_handler_map_.erase(tab);
254}
255
[email protected]f44265b2009-05-19 18:52:50256void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
257 int port_id = port->port_id();
258 DCHECK_NE(-1, port_id);
259 DCHECK(port_containers_.find(port_id) == port_containers_.end());
260
261 port_containers_[port_id] = port;
262}
263
264void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
265 int port_id = port->port_id();
266 DCHECK_NE(-1, port_id);
267
268 PortContainerMap::iterator it = port_containers_.find(port_id);
269 DCHECK(it != port_containers_.end());
270
271 if (it != port_containers_.end()) {
272 delete it->second;
273 port_containers_.erase(it);
274 }
275}
276
277ExtensionPortContainer* AutomationProvider::GetPortContainer(
278 int port_id) const {
279 PortContainerMap::const_iterator it = port_containers_.find(port_id);
280 if (it == port_containers_.end())
281 return NULL;
282
283 return it->second;
284}
285
initial.commit09911bf2008-07-26 23:55:29286int AutomationProvider::GetIndexForNavigationController(
287 const NavigationController* controller, const Browser* parent) const {
288 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12289 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29290}
291
[email protected]790788ac2010-04-06 17:52:19292int AutomationProvider::AddExtension(Extension* extension) {
293 DCHECK(extension);
294 return extension_tracker_->Add(extension);
295}
296
297Extension* AutomationProvider::GetExtension(int extension_handle) {
298 return extension_tracker_->GetResource(extension_handle);
299}
300
301Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
302 Extension* extension = extension_tracker_->GetResource(extension_handle);
303 ExtensionsService* service = profile_->GetExtensionsService();
304 if (extension && service &&
305 service->GetExtensionById(extension->id(), false))
306 return extension;
307 return NULL;
308}
309
310Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
311 Extension* extension = extension_tracker_->GetResource(extension_handle);
312 ExtensionsService* service = profile_->GetExtensionsService();
313 if (extension && service &&
314 service->GetExtensionById(extension->id(), true) &&
315 !service->GetExtensionById(extension->id(), false))
316 return extension;
317 return NULL;
318}
319
initial.commit09911bf2008-07-26 23:55:29320void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
321 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14322 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56323 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
324 CloseBrowserAsync)
325 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
326 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
327 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
328 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
329 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
330 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22331 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]1c58a5c2009-05-21 18:47:14332 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58333 IPC_MESSAGE_HANDLER_DELAY_REPLY(
334 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
335 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56336 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11337 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
338 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56339 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
340 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
341 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
342 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14343 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56344 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
345 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
346 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14347 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29348 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
349 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56350 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36351 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56352 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29353 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56354 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29355 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
356 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56357 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14358 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24359 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44360 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33361 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44362 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33363 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24364 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14365 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24366 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15367 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14368 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24369 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35370 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14371 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30372#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56373 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
374 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30375#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56376 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53377 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56378 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37379#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56380 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45381#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56382 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
383 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03384 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56385 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14386 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09387 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
388 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
389 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29390 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14391 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56392 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
393 ExecuteJavascript)
394 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29395 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14396 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
397 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56398 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
399 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14400 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17401 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14402 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14403 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53404 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
405 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14406 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56407 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14408 GetAutocompleteEditForBrowser)
409 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]71f65dd2009-02-11 19:14:56410 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14411 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56412 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14413 HideInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56414 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
415 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14416 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
417 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44418 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
419 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56420 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
421 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29422 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
423 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
424 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56425 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41426 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56427 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
428 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29429 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56430 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29431 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56432 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29433 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56434 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29435 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56436 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45437 HandleOpenFindInPageRequest)
[email protected]1c58a5c2009-05-21 18:47:14438 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56439 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57440 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56441 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57442 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56443 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
444 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53445 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
446 GetBookmarksAsJSON)
447 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
448 WaitForBookmarkModelToLoad)
449 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
450 AddBookmarkGroup)
451 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
452 AddBookmarkURL)
453 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
454 ReparentBookmark)
455 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
456 SetBookmarkTitle)
457 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
458 SetBookmarkURL)
459 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
460 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04461 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
462 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18463 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
464 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
465 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56466 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42467 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56468 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
469 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14470 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56471 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44472 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56473 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20474 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14475 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56476 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16477 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56478 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16479 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56480 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16481 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14482 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20483 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
484 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14485 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]59560e0b2009-06-04 03:30:22486 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13487 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19488 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
489 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
490 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
491 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
492 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
493 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52494 IPC_MESSAGE_HANDLER_DELAY_REPLY(
495 AutomationMsg_WaitForBrowserWindowCountToBecome,
496 WaitForBrowserWindowCountToBecome)
497 IPC_MESSAGE_HANDLER_DELAY_REPLY(
498 AutomationMsg_WaitForAppModalDialogToBeShown,
499 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26500 IPC_MESSAGE_HANDLER_DELAY_REPLY(
501 AutomationMsg_GoBackBlockUntilNavigationsComplete,
502 GoBackBlockUntilNavigationsComplete)
503 IPC_MESSAGE_HANDLER_DELAY_REPLY(
504 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
505 GoForwardBlockUntilNavigationsComplete)
[email protected]1bb5f892009-10-06 01:44:57506 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40507 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
508 InstallExtension)
509 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
510 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43511 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
512 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19513 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
514 WaitForExtensionTestResult)
515 IPC_MESSAGE_HANDLER_DELAY_REPLY(
516 AutomationMsg_InstallExtensionAndGetHandle,
517 InstallExtensionAndGetHandle)
518 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
519 UninstallExtension)
520 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
521 EnableExtension)
522 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
523 DisableExtension)
524 IPC_MESSAGE_HANDLER_DELAY_REPLY(
525 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
526 ExecuteExtensionActionInActiveTabAsync)
527 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
528 MoveExtensionBrowserAction)
529 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
530 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57531 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
532 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00533 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]7dad3d5f2010-03-04 00:27:01534 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]bc73b4e52010-03-26 04:16:20535 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]bdd5a9c92010-06-14 18:21:00536 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
[email protected]cc824372010-03-31 15:33:01537#if defined(TOOLKIT_VIEWS)
538 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
539 WaitForFocusedViewIDToChange)
540 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
541 StartTrackingPopupMenus)
542 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
543 WaitForPopupMenuToOpen)
[email protected]bdd5a9c92010-06-14 18:21:00544#endif // defined(TOOLKIT_VIEWS)
[email protected]52415f842010-06-10 21:51:52545#if defined(OS_WIN)
546 // These are for use with external tabs.
547 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
548 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
549 ProcessUnhandledAccelerator)
550 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
551 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
552 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
553 OnForwardContextMenuCommandToChrome)
554 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
555 NavigateInExternalTab)
556 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
557 NavigateExternalTabAtIndex)
558 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
559 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
560 SetEnableExtensionAutomation)
561 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
562 OnMessageFromExternalHost)
[email protected]bdd5a9c92010-06-14 18:21:00563 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
[email protected]1f71d5882010-07-15 20:39:07564 IPC_MESSAGE_HANDLER(AutomationMsg_RunUnloadHandlers, OnRunUnloadHandlers)
[email protected]bdd5a9c92010-06-14 18:21:00565#endif // defined(OS_WIN)
566#if defined(OS_CHROMEOS)
567 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
568 LoginWithUserAndPass)
569#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29570 IPC_END_MESSAGE_MAP()
571}
572
[email protected]71f65dd2009-02-11 19:14:56573void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
574 *status = -1;
initial.commit09911bf2008-07-26 23:55:29575 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
576 Browser* browser = browser_tracker_->GetResource(handle);
577 if (at_index >= 0 && at_index < browser->tab_count()) {
578 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56579 *status = 0;
initial.commit09911bf2008-07-26 23:55:29580 }
581 }
initial.commit09911bf2008-07-26 23:55:29582}
583
[email protected]71f65dd2009-02-11 19:14:56584void AutomationProvider::AppendTab(int handle, const GURL& url,
585 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29586 int append_tab_response = -1; // -1 is the error code
587 NotificationObserver* observer = NULL;
588
589 if (browser_tracker_->ContainsHandle(handle)) {
590 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14591 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38592 TabContents* tab_contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39593 url, GURL(), PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED,
594 NULL, std::string());
initial.commit09911bf2008-07-26 23:55:29595 if (tab_contents) {
596 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57597 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29598 }
599 }
600
601 if (append_tab_response < 0) {
602 // The append tab failed. Remove the TabStripObserver
603 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17604 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29605 delete observer;
606 }
607
[email protected]71f65dd2009-02-11 19:14:56608 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
609 append_tab_response);
610 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29611 }
612}
613
[email protected]71f65dd2009-02-11 19:14:56614void AutomationProvider::NavigateToURL(int handle, const GURL& url,
615 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58616 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
617}
618
619void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
620 int handle, const GURL& url, int number_of_navigations,
621 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29622 if (tab_tracker_->ContainsHandle(handle)) {
623 NavigationController* tab = tab_tracker_->GetResource(handle);
624
625 // Simulate what a user would do. Activate the tab and then navigate.
626 // We could allow navigating in a background tab in future.
627 Browser* browser = FindAndActivateTab(tab);
628
629 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01630 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
631 false);
[email protected]71f65dd2009-02-11 19:14:56632
initial.commit09911bf2008-07-26 23:55:29633 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50634 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29635 return;
636 }
637 }
[email protected]71f65dd2009-02-11 19:14:56638
639 AutomationMsg_NavigateToURL::WriteReplyParams(
640 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
641 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29642}
[email protected]2949e90d2009-08-21 15:32:52643
[email protected]c70f9b82010-04-21 07:31:11644void AutomationProvider::NavigationAsync(int handle,
645 const GURL& url,
646 bool* status) {
647 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
648}
649
650void AutomationProvider::NavigationAsyncWithDisposition(
651 int handle,
652 const GURL& url,
653 WindowOpenDisposition disposition,
654 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56655 *status = false;
initial.commit09911bf2008-07-26 23:55:29656
657 if (tab_tracker_->ContainsHandle(handle)) {
658 NavigationController* tab = tab_tracker_->GetResource(handle);
659
660 // Simulate what a user would do. Activate the tab and then navigate.
661 // We could allow navigating in a background tab in future.
662 Browser* browser = FindAndActivateTab(tab);
663
664 if (browser) {
665 // Don't add any listener unless a callback mechanism is desired.
666 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11667 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56668 *status = true;
initial.commit09911bf2008-07-26 23:55:29669 }
670 }
initial.commit09911bf2008-07-26 23:55:29671}
672
[email protected]71f65dd2009-02-11 19:14:56673void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29674 if (tab_tracker_->ContainsHandle(handle)) {
675 NavigationController* tab = tab_tracker_->GetResource(handle);
676 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14677 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01678 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29679 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29680 return;
681 }
682 }
[email protected]71f65dd2009-02-11 19:14:56683
684 AutomationMsg_GoBack::WriteReplyParams(
685 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
686 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29687}
688
[email protected]71f65dd2009-02-11 19:14:56689void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29690 if (tab_tracker_->ContainsHandle(handle)) {
691 NavigationController* tab = tab_tracker_->GetResource(handle);
692 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14693 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01694 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29695 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29696 return;
697 }
698 }
[email protected]71f65dd2009-02-11 19:14:56699
700 AutomationMsg_GoForward::WriteReplyParams(
701 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
702 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29703}
704
[email protected]71f65dd2009-02-11 19:14:56705void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29706 if (tab_tracker_->ContainsHandle(handle)) {
707 NavigationController* tab = tab_tracker_->GetResource(handle);
708 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14709 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01710 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]cb84d642010-06-10 00:56:28711 browser->Reload(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29712 return;
713 }
714 }
[email protected]71f65dd2009-02-11 19:14:56715
716 AutomationMsg_Reload::WriteReplyParams(
717 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
718 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29719}
720
[email protected]71f65dd2009-02-11 19:14:56721void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29722 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56723 const std::wstring& password,
724 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29725 if (tab_tracker_->ContainsHandle(tab_handle)) {
726 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
727 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
728
729 if (iter != login_handler_map_.end()) {
730 // If auth is needed again after this, assume login has failed. This is
731 // not strictly correct, because a navigation can require both proxy and
732 // server auth, but it should be OK for now.
733 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01734 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29735 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52736 return;
initial.commit09911bf2008-07-26 23:55:29737 }
738 }
[email protected]de246f52009-02-25 18:25:45739
[email protected]457f5cf2009-08-18 16:37:52740 AutomationMsg_SetAuth::WriteReplyParams(
741 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
742 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29743}
744
[email protected]71f65dd2009-02-11 19:14:56745void AutomationProvider::CancelAuth(int tab_handle,
746 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29747 if (tab_tracker_->ContainsHandle(tab_handle)) {
748 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
749 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
750
751 if (iter != login_handler_map_.end()) {
752 // If auth is needed again after this, something is screwy.
753 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01754 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29755 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52756 return;
initial.commit09911bf2008-07-26 23:55:29757 }
758 }
[email protected]de246f52009-02-25 18:25:45759
[email protected]457f5cf2009-08-18 16:37:52760 AutomationMsg_CancelAuth::WriteReplyParams(
761 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
762 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29763}
764
[email protected]71f65dd2009-02-11 19:14:56765void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
766 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29767
768 if (tab_tracker_->ContainsHandle(tab_handle)) {
769 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
770 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
771
772 if (iter != login_handler_map_.end()) {
773 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56774 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29775 }
776 }
initial.commit09911bf2008-07-26 23:55:29777}
778
[email protected]71f65dd2009-02-11 19:14:56779void AutomationProvider::GetRedirectsFrom(int tab_handle,
780 const GURL& source_url,
781 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29782 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
783 if (tab_tracker_->ContainsHandle(tab_handle)) {
784 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
785 HistoryService* history_service =
786 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
787
788 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
789 "has no history service";
790 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56791 DCHECK(reply_message_ == NULL);
792 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29793 // Schedule a history query for redirects. The response will be sent
794 // asynchronously from the callback the history system uses to notify us
795 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29796 redirect_query_ = history_service->QueryRedirectsFrom(
797 source_url, &consumer_,
798 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
799 return; // Response will be sent when query completes.
800 }
801 }
802
803 // Send failure response.
[email protected]deb57402009-02-06 01:35:30804 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56805 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
806 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29807}
808
[email protected]71f65dd2009-02-11 19:14:56809void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
810 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29811 if (browser_tracker_->ContainsHandle(handle)) {
812 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56813 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29814 }
initial.commit09911bf2008-07-26 23:55:29815}
816
[email protected]202e7a72009-06-15 03:48:36817void AutomationProvider::GetBrowserLocale(string16* locale) {
818 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06819 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36820}
821
[email protected]71f65dd2009-02-11 19:14:56822void AutomationProvider::GetBrowserWindowCount(int* window_count) {
823 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29824}
825
[email protected]24497032009-05-01 17:00:29826void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
827 *window_count = static_cast<int>(
828 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
829}
830
[email protected]71f65dd2009-02-11 19:14:56831void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
832 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07833 AppModalDialog* dialog_delegate =
834 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50835 *showing_dialog = (dialog_delegate != NULL);
836 if (*showing_dialog)
837 *dialog_button = dialog_delegate->GetDialogButtons();
838 else
[email protected]478ff2ed2009-04-21 23:49:18839 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20840}
841
[email protected]71f65dd2009-02-11 19:14:56842void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
843 *success = false;
[email protected]fad84eab2008-12-05 00:37:20844
[email protected]1f460072009-05-28 17:02:07845 AppModalDialog* dialog_delegate =
846 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50847 if (dialog_delegate &&
848 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18849 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
850 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28851 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56852 *success = true;
[email protected]fad84eab2008-12-05 00:37:20853 }
[email protected]478ff2ed2009-04-21 23:49:18854 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
855 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56856 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28857 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56858 *success = true;
[email protected]fad84eab2008-12-05 00:37:20859 }
860 }
[email protected]c274acc2008-11-11 20:13:44861}
862
[email protected]fedaa7d2010-01-26 20:34:57863void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
864 if (browser_tracker_->ContainsHandle(handle)) {
865 Browser* browser = browser_tracker_->GetResource(handle);
866 browser->profile()->ShutdownSessionService();
867 *result = true;
868 } else {
869 *result = false;
870 }
871}
872
[email protected]71f65dd2009-02-11 19:14:56873void AutomationProvider::GetBrowserWindow(int index, int* handle) {
874 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29875 if (index >= 0) {
876 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]f07467d2010-06-16 14:28:30877 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {}
initial.commit09911bf2008-07-26 23:55:29878 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56879 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29880 }
881 }
initial.commit09911bf2008-07-26 23:55:29882}
883
[email protected]24497032009-05-01 17:00:29884void AutomationProvider::FindNormalBrowserWindow(int* handle) {
885 *handle = 0;
886 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31887 Browser::TYPE_NORMAL,
888 false);
[email protected]24497032009-05-01 17:00:29889 if (browser)
890 *handle = browser_tracker_->Add(browser);
891}
892
[email protected]71f65dd2009-02-11 19:14:56893void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
894 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29895 Browser* browser = BrowserList::GetLastActive();
896 if (browser)
[email protected]71f65dd2009-02-11 19:14:56897 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29898}
899
[email protected]b2aa3ed72010-02-01 18:37:14900#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28901// TODO(estade): use this implementation for all platforms?
902void AutomationProvider::GetActiveWindow(int* handle) {
903 gfx::NativeWindow window =
904 BrowserList::GetLastActive()->window()->GetNativeHandle();
905 *handle = window_tracker_->Add(window);
906}
907#endif
908
[email protected]4f6381ee2009-04-16 02:46:33909void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
910 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56911 *success = false;
[email protected]4ae62752008-08-04 23:28:47912 if (browser_tracker_->ContainsHandle(handle)) {
913 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14914 if (browser->command_updater()->SupportsCommand(command) &&
915 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47916 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56917 *success = true;
[email protected]4ae62752008-08-04 23:28:47918 }
919 }
[email protected]4ae62752008-08-04 23:28:47920}
921
[email protected]4f6381ee2009-04-16 02:46:33922void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56923 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53924 // List of commands which just finish synchronously and don't require
925 // setting up an observer.
926 static const int kSynchronousCommands[] = {
927 IDC_HOME,
928 IDC_SELECT_NEXT_TAB,
929 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25930 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53931 };
[email protected]56e71b7c2009-03-27 03:05:56932 if (browser_tracker_->ContainsHandle(handle)) {
933 Browser* browser = browser_tracker_->GetResource(handle);
934 if (browser->command_updater()->SupportsCommand(command) &&
935 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53936 // First check if we can handle the command without using an observer.
937 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
938 if (command == kSynchronousCommands[i]) {
939 browser->ExecuteCommand(command);
940 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
941 true);
942 Send(reply_message);
943 return;
944 }
945 }
946
947 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42948 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
949 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27950 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42951 return;
952 }
[email protected]56e71b7c2009-03-27 03:05:56953 }
954 }
[email protected]49a14a82009-03-31 04:16:44955 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56956 Send(reply_message);
957}
958
[email protected]fc2e0872009-08-21 22:14:41959// This task just adds another task to the event queue. This is useful if
960// you want to ensure that any tasks added to the event queue after this one
961// have already been processed by the time |task| is run.
962class InvokeTaskLaterTask : public Task {
963 public:
964 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
965 virtual ~InvokeTaskLaterTask() {}
966
967 virtual void Run() {
968 MessageLoop::current()->PostTask(FROM_HERE, task_);
969 }
970
971 private:
972 Task* task_;
973
974 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
975};
976
initial.commit09911bf2008-07-26 23:55:29977void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
978 int handle,
[email protected]d1a5941e2009-08-13 23:34:24979 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29980 int flags) {
[email protected]b410bc32009-08-14 01:11:14981 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51982 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41983 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29984 }
985}
986
[email protected]60507b12009-11-02 23:51:35987void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
988 int handle,
989 const gfx::Point& location) {
990 if (window_tracker_->ContainsHandle(handle))
991 ui_controls::SendMouseMove(location.x(), location.y());
992}
993
initial.commit09911bf2008-07-26 23:55:29994void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
995 int handle,
[email protected]bc1407f2009-09-29 00:33:35996 int key,
initial.commit09911bf2008-07-26 23:55:29997 int flags) {
[email protected]b410bc32009-08-14 01:11:14998 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29999 return;
1000
[email protected]b410bc32009-08-14 01:11:141001 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291002 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:351003 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:381004 ((flags & views::Event::EF_CONTROL_DOWN) ==
1005 views::Event::EF_CONTROL_DOWN),
1006 ((flags & views::Event::EF_SHIFT_DOWN) ==
1007 views::Event::EF_SHIFT_DOWN),
1008 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301009 views::Event::EF_ALT_DOWN),
1010 ((flags & views::Event::EF_COMMAND_DOWN) ==
1011 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291012}
initial.commit09911bf2008-07-26 23:55:291013
[email protected]71f65dd2009-02-11 19:14:561014void AutomationProvider::IsWindowActive(int handle, bool* success,
1015 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291016 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171017 *is_active =
1018 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561019 *success = true;
initial.commit09911bf2008-07-26 23:55:291020 } else {
[email protected]71f65dd2009-02-11 19:14:561021 *success = false;
1022 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291023 }
1024}
1025
[email protected]71f65dd2009-02-11 19:14:561026void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1027 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291028
1029 if (browser_tracker_->ContainsHandle(handle)) {
1030 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561031 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291032 }
initial.commit09911bf2008-07-26 23:55:291033}
1034
[email protected]982921f12009-10-27 21:43:531035void AutomationProvider::GetType(int handle, int* type_as_int) {
1036 *type_as_int = -1; // -1 is the error code
1037
1038 if (browser_tracker_->ContainsHandle(handle)) {
1039 Browser* browser = browser_tracker_->GetResource(handle);
1040 *type_as_int = static_cast<int>(browser->type());
1041 }
1042}
1043
[email protected]71f65dd2009-02-11 19:14:561044void AutomationProvider::GetTab(int win_handle, int tab_index,
1045 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561046 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291047 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1048 Browser* browser = browser_tracker_->GetResource(win_handle);
1049 if (tab_index < browser->tab_count()) {
1050 TabContents* tab_contents =
1051 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571052 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291053 }
1054 }
initial.commit09911bf2008-07-26 23:55:291055}
1056
[email protected]71f65dd2009-02-11 19:14:561057void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1058 std::wstring* title) {
1059 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291060 if (tab_tracker_->ContainsHandle(handle)) {
1061 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361062 NavigationEntry* entry = tab->GetActiveEntry();
1063 if (entry != NULL) {
1064 *title = UTF16ToWideHack(entry->title());
1065 } else {
1066 *title = std::wstring();
1067 }
[email protected]71f65dd2009-02-11 19:14:561068 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291069 }
initial.commit09911bf2008-07-26 23:55:291070}
1071
[email protected]77bc6732009-04-20 22:01:031072void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1073 *tabstrip_index = -1; // -1 is the error code
1074
1075 if (tab_tracker_->ContainsHandle(handle)) {
1076 NavigationController* tab = tab_tracker_->GetResource(handle);
1077 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121078 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031079 }
1080}
1081
initial.commit09911bf2008-07-26 23:55:291082void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1083 if (window_tracker_->ContainsHandle(handle)) {
1084 window_tracker_->Remove(window_tracker_->GetResource(handle));
1085 }
1086}
1087
1088void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161089 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571090 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291091}
1092
1093// TODO(brettw) change this to accept GURLs when history supports it
1094void AutomationProvider::OnRedirectQueryComplete(
1095 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371096 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291097 bool success,
[email protected]379c2b12009-07-01 21:50:331098 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291099 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561100 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291101
[email protected]deb57402009-02-06 01:35:301102 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101103 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291104 if (success) {
initial.commit09911bf2008-07-26 23:55:291105 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301106 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291107 }
1108
[email protected]4f3dc372009-02-24 00:10:291109 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301110
[email protected]71f65dd2009-02-11 19:14:561111 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231112 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561113 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291114}
1115
1116bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571117 DCHECK(channel_.get());
1118 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291119}
1120
1121Browser* AutomationProvider::FindAndActivateTab(
1122 NavigationController* controller) {
1123 int tab_index;
1124 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1125 if (browser)
1126 browser->SelectTabContentsAt(tab_index, true);
1127
1128 return browser;
1129}
1130
[email protected]9eaa18e2010-06-29 20:51:011131namespace {
1132
1133class GetCookiesTask : public Task {
1134 public:
1135 GetCookiesTask(const GURL& url,
1136 URLRequestContextGetter* context_getter,
1137 base::WaitableEvent* event,
1138 std::string* cookies)
1139 : url_(url),
1140 context_getter_(context_getter),
1141 event_(event),
1142 cookies_(cookies) {}
1143
1144 virtual void Run() {
1145 *cookies_ = context_getter_->GetCookieStore()->GetCookies(url_);
1146 event_->Signal();
1147 }
1148
1149 private:
1150 const GURL& url_;
1151 URLRequestContextGetter* const context_getter_;
1152 base::WaitableEvent* const event_;
1153 std::string* const cookies_;
1154
1155 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
1156};
1157
1158std::string GetCookiesForURL(
1159 const GURL& url,
1160 URLRequestContextGetter* context_getter) {
1161 std::string cookies;
1162 base::WaitableEvent event(true /* manual reset */,
1163 false /* not initially signaled */);
1164 CHECK(ChromeThread::PostTask(
1165 ChromeThread::IO, FROM_HERE,
1166 new GetCookiesTask(url, context_getter, &event, &cookies)));
1167 event.Wait();
1168 return cookies;
1169}
1170
1171class SetCookieTask : public Task {
1172 public:
1173 SetCookieTask(const GURL& url,
1174 const std::string& value,
1175 URLRequestContextGetter* context_getter,
1176 base::WaitableEvent* event,
1177 bool* rv)
1178 : url_(url),
1179 value_(value),
1180 context_getter_(context_getter),
1181 event_(event),
1182 rv_(rv) {}
1183
1184 virtual void Run() {
1185 *rv_ = context_getter_->GetCookieStore()->SetCookie(url_, value_);
1186 event_->Signal();
1187 }
1188
1189 private:
1190 const GURL& url_;
1191 const std::string& value_;
1192 URLRequestContextGetter* const context_getter_;
1193 base::WaitableEvent* const event_;
1194 bool* const rv_;
1195
1196 DISALLOW_COPY_AND_ASSIGN(SetCookieTask);
1197};
1198
1199bool SetCookieForURL(
1200 const GURL& url,
1201 const std::string& value,
1202 URLRequestContextGetter* context_getter) {
1203 base::WaitableEvent event(true /* manual reset */,
1204 false /* not initially signaled */);
1205 bool rv = false;
1206 CHECK(ChromeThread::PostTask(
1207 ChromeThread::IO, FROM_HERE,
1208 new SetCookieTask(url, value, context_getter, &event, &rv)));
1209 event.Wait();
1210 return rv;
1211}
1212
1213class DeleteCookieTask : public Task {
1214 public:
1215 DeleteCookieTask(const GURL& url,
1216 const std::string& name,
1217 const scoped_refptr<URLRequestContextGetter>& context_getter)
1218 : url_(url),
1219 name_(name),
1220 context_getter_(context_getter) {}
1221
1222 virtual void Run() {
1223 net::CookieStore* cookie_store = context_getter_->GetCookieStore();
1224 cookie_store->DeleteCookie(url_, name_);
1225 }
1226
1227 private:
1228 const GURL url_;
1229 const std::string name_;
1230 const scoped_refptr<URLRequestContextGetter> context_getter_;
1231
1232 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
1233};
1234
1235} // namespace
1236
[email protected]71f65dd2009-02-11 19:14:561237void AutomationProvider::GetCookies(const GURL& url, int handle,
1238 int* value_size,
1239 std::string* value) {
1240 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291241 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1242 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311243
1244 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001245 scoped_refptr<URLRequestContextGetter> request_context =
1246 tab->tab_contents()->request_context();
1247 if (!request_context.get())
1248 request_context = tab->profile()->GetRequestContext();
1249
[email protected]9eaa18e2010-06-29 20:51:011250 *value = GetCookiesForURL(url, request_context.get());
[email protected]71f65dd2009-02-11 19:14:561251 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291252 }
initial.commit09911bf2008-07-26 23:55:291253}
1254
[email protected]71f65dd2009-02-11 19:14:561255void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291256 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561257 int handle,
1258 int* response_value) {
1259 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291260
1261 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1262 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311263
[email protected]dfa46e5f2009-11-17 18:48:431264 scoped_refptr<URLRequestContextGetter> request_context =
1265 tab->tab_contents()->request_context();
1266 if (!request_context.get())
1267 request_context = tab->profile()->GetRequestContext();
1268
[email protected]9eaa18e2010-06-29 20:51:011269 if (SetCookieForURL(url, value, request_context.get()))
[email protected]71f65dd2009-02-11 19:14:561270 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291271 }
initial.commit09911bf2008-07-26 23:55:291272}
1273
[email protected]5fa57942010-04-21 23:07:221274void AutomationProvider::DeleteCookie(const GURL& url,
1275 const std::string& cookie_name,
1276 int handle, bool* success) {
1277 *success = false;
1278 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1279 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]9eaa18e2010-06-29 20:51:011280 ChromeThread::PostTask(
1281 ChromeThread::IO, FROM_HERE,
1282 new DeleteCookieTask(url, cookie_name,
1283 tab->profile()->GetRequestContext()));
[email protected]5fa57942010-04-21 23:07:221284 *success = true;
1285 }
1286}
1287
[email protected]71f65dd2009-02-11 19:14:561288void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1289 *success = false;
initial.commit09911bf2008-07-26 23:55:291290 if (tab_tracker_->ContainsHandle(handle)) {
1291 NavigationController* tab = tab_tracker_->GetResource(handle);
1292 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541293 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561294 *success = true;
initial.commit09911bf2008-07-26 23:55:291295 }
initial.commit09911bf2008-07-26 23:55:291296}
1297
[email protected]71f65dd2009-02-11 19:14:561298void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1299 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291300
1301 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561302 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341303 TabContents* tab_contents =
1304 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161305 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1306 if (rph)
1307 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291308 }
initial.commit09911bf2008-07-26 23:55:291309}
1310
1311void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331312 NOTREACHED() << "This function has been deprecated. "
1313 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291314}
1315
[email protected]71f65dd2009-02-11 19:14:561316void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291317 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561318 const std::wstring& script,
1319 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291320 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341321 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1322 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571323 // Set the routing id of this message with the controller.
1324 // This routing id needs to be remembered for the reverse
1325 // communication while sending back the response of
1326 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331327 std::wstring set_automation_id;
1328 SStringPrintf(&set_automation_id,
1329 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561330 reply_message->routing_id());
1331
1332 DCHECK(reply_message_ == NULL);
1333 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291334
[email protected]57c6a652009-05-04 07:58:341335 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331336 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341337 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061338 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571339 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291340 }
1341
1342 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561343 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1344 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291345 }
1346}
1347
[email protected]71f65dd2009-02-11 19:14:561348void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1349 *visible = false;
[email protected]20e93d12008-08-28 16:31:571350
[email protected]59560e0b2009-06-04 03:30:221351 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251352#if defined(OS_CHROMEOS)
1353 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1354 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1355 for (BrowserList::const_iterator it = BrowserList::begin();
1356 it != BrowserList::end(); ++it) {
1357 if ((*it)->type() == Browser::TYPE_POPUP) {
1358 const GURL& url =
1359 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1360
1361 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1362 url.host() == chrome::kChromeUIFileBrowseHost) {
1363 *visible = true;
1364 break;
1365 }
1366 }
1367 }
1368#else
[email protected]59560e0b2009-06-04 03:30:221369 Browser* browser = browser_tracker_->GetResource(handle);
1370 if (browser) {
1371 *visible = browser->window()->IsDownloadShelfVisible();
1372 }
[email protected]f5bf8ccf2010-02-05 18:19:251373#endif
[email protected]59560e0b2009-06-04 03:30:221374 }
initial.commit09911bf2008-07-26 23:55:291375}
1376
[email protected]59560e0b2009-06-04 03:30:221377void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1378 if (browser_tracker_->ContainsHandle(handle)) {
1379 Browser* browser = browser_tracker_->GetResource(handle);
1380 if (browser) {
1381 if (visible)
1382 browser->window()->GetDownloadShelf()->Show();
1383 else
1384 browser->window()->GetDownloadShelf()->Close();
1385 }
1386 }
1387}
1388
[email protected]34930432009-11-09 00:12:091389void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1390 *visible = false;
1391
1392 if (browser_tracker_->ContainsHandle(handle)) {
1393 Browser* browser = browser_tracker_->GetResource(handle);
1394 if (browser)
1395 *visible = browser->window()->IsFullscreen();
1396 }
1397}
1398
1399void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1400 bool* visible) {
1401 *visible = false;
1402
1403 if (browser_tracker_->ContainsHandle(handle)) {
1404 Browser* browser = browser_tracker_->GetResource(handle);
1405 if (browser)
1406 *visible = browser->window()->IsFullscreenBubbleVisible();
1407 }
1408}
[email protected]59560e0b2009-06-04 03:30:221409
[email protected]71f65dd2009-02-11 19:14:561410void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1411 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291412 if (tab_tracker_->ContainsHandle(handle)) {
1413 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111414 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291415 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561416 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291417 }
1418 }
initial.commit09911bf2008-07-26 23:55:291419}
1420
initial.commit09911bf2008-07-26 23:55:291421void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561422 int handle, const std::wstring& find_request,
1423 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311424 NOTREACHED() << "This function has been deprecated."
1425 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561426 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311427 return;
1428}
1429
[email protected]4f999132009-03-31 18:08:401430void AutomationProvider::HandleFindRequest(
1431 int handle,
1432 const AutomationMsg_Find_Params& params,
1433 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291434 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561435 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1436 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291437 return;
1438 }
1439
1440 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111441 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291442
1443 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141444 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291445
[email protected]57c6a652009-05-04 07:58:341446 tab_contents->set_current_find_request_id(
1447 FindInPageNotificationObserver::kFindInPageRequestId);
1448 tab_contents->render_view_host()->StartFinding(
1449 FindInPageNotificationObserver::kFindInPageRequestId,
1450 params.search_string, params.forward, params.match_case,
1451 params.find_next);
initial.commit09911bf2008-07-26 23:55:291452}
1453
[email protected]5f8af2a2008-08-06 22:49:451454void AutomationProvider::HandleOpenFindInPageRequest(
1455 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291456 if (browser_tracker_->ContainsHandle(handle)) {
1457 Browser* browser = browser_tracker_->GetResource(handle);
1458 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451459 }
1460}
1461
[email protected]71f65dd2009-02-11 19:14:561462void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561463 *visible = false;
[email protected]855c0142009-09-28 22:35:241464 Browser* browser = browser_tracker_->GetResource(handle);
1465 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581466 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171467 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241468 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291469 }
[email protected]20e93d12008-08-28 16:31:571470}
1471
[email protected]71f65dd2009-02-11 19:14:561472void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1473 int* y) {
[email protected]9e0534b2008-10-21 15:03:011474 gfx::Point position(0, 0);
1475 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291476 if (browser_tracker_->ContainsHandle(handle)) {
1477 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581478 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171479 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581480 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291481 }
[email protected]20e93d12008-08-28 16:31:571482
[email protected]71f65dd2009-02-11 19:14:561483 *x = position.x();
1484 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571485}
1486
[email protected]4512cb52010-04-05 19:50:251487// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1488// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581489void AutomationProvider::GetBookmarkBarVisibility(int handle,
1490 bool* visible,
1491 bool* animating) {
1492 *visible = false;
1493 *animating = false;
1494
1495 if (browser_tracker_->ContainsHandle(handle)) {
1496 Browser* browser = browser_tracker_->GetResource(handle);
1497 if (browser) {
[email protected]472f099b2010-05-27 17:07:121498#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1499 // TODO(jrg): Was removed in rev43789 for perf. Need to investigate.
1500
[email protected]ab6ca392010-04-07 00:44:131501 // IsBookmarkBarVisible() line looks correct but is not
1502 // consistent across platforms. Specifically, on Mac/Linux, it
1503 // returns false if the bar is hidden in a pref (even if visible
1504 // on the NTP). On ChromeOS, it returned true if on NTP
1505 // independent of the pref. Making the code more consistent
1506 // caused a perf bot regression on Windows (which shares views).
1507 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251508 *visible = browser->profile()->GetPrefs()->GetBoolean(
1509 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401510#else
1511 *visible = browser->window()->IsBookmarkBarVisible();
1512#endif
[email protected]c3240722010-03-05 21:52:581513 *animating = browser->window()->IsBookmarkBarAnimating();
1514 }
1515 }
1516}
1517
[email protected]6d8ffc9f2010-03-12 18:27:531518void AutomationProvider::GetBookmarksAsJSON(int handle,
1519 std::string* bookmarks_as_json,
1520 bool *success) {
1521 *success = false;
1522 if (browser_tracker_->ContainsHandle(handle)) {
1523 Browser* browser = browser_tracker_->GetResource(handle);
1524 if (browser) {
1525 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1526 return;
1527 }
1528 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1529 browser->profile(),
1530 browser->profile()->GetBookmarkModel());
1531 *success = storage->SerializeData(bookmarks_as_json);
1532 }
1533 }
1534}
1535
1536void AutomationProvider::WaitForBookmarkModelToLoad(
1537 int handle,
1538 IPC::Message* reply_message) {
1539 if (browser_tracker_->ContainsHandle(handle)) {
1540 Browser* browser = browser_tracker_->GetResource(handle);
1541 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1542 if (model->IsLoaded()) {
1543 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1544 reply_message, true);
1545 Send(reply_message);
1546 } else {
1547 // The observer will delete itself when done.
1548 new AutomationProviderBookmarkModelObserver(this, reply_message,
1549 model);
1550 }
1551 }
1552}
1553
1554void AutomationProvider::AddBookmarkGroup(int handle,
1555 int64 parent_id, int index,
1556 std::wstring title,
1557 bool* success) {
1558 if (browser_tracker_->ContainsHandle(handle)) {
1559 Browser* browser = browser_tracker_->GetResource(handle);
1560 if (browser) {
1561 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1562 if (!model->IsLoaded()) {
1563 *success = false;
1564 return;
1565 }
1566 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1567 DCHECK(parent);
1568 if (parent) {
1569 const BookmarkNode* child = model->AddGroup(parent, index,
1570 WideToUTF16(title));
1571 DCHECK(child);
1572 if (child)
1573 *success = true;
1574 }
1575 }
1576 }
1577 *success = false;
1578}
1579
1580void AutomationProvider::AddBookmarkURL(int handle,
1581 int64 parent_id, int index,
1582 std::wstring title, const GURL& url,
1583 bool* success) {
1584 if (browser_tracker_->ContainsHandle(handle)) {
1585 Browser* browser = browser_tracker_->GetResource(handle);
1586 if (browser) {
1587 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1588 if (!model->IsLoaded()) {
1589 *success = false;
1590 return;
1591 }
1592 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1593 DCHECK(parent);
1594 if (parent) {
1595 const BookmarkNode* child = model->AddURL(parent, index,
1596 WideToUTF16(title), url);
1597 DCHECK(child);
1598 if (child)
1599 *success = true;
1600 }
1601 }
1602 }
1603 *success = false;
1604}
1605
1606void AutomationProvider::ReparentBookmark(int handle,
1607 int64 id, int64 new_parent_id,
1608 int index,
1609 bool* success) {
1610 if (browser_tracker_->ContainsHandle(handle)) {
1611 Browser* browser = browser_tracker_->GetResource(handle);
1612 if (browser) {
1613 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1614 if (!model->IsLoaded()) {
1615 *success = false;
1616 return;
1617 }
1618 const BookmarkNode* node = model->GetNodeByID(id);
1619 DCHECK(node);
1620 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1621 DCHECK(new_parent);
1622 if (node && new_parent) {
1623 model->Move(node, new_parent, index);
1624 *success = true;
1625 }
1626 }
1627 }
1628 *success = false;
1629}
1630
1631void AutomationProvider::SetBookmarkTitle(int handle,
1632 int64 id, std::wstring title,
1633 bool* success) {
1634 if (browser_tracker_->ContainsHandle(handle)) {
1635 Browser* browser = browser_tracker_->GetResource(handle);
1636 if (browser) {
1637 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1638 if (!model->IsLoaded()) {
1639 *success = false;
1640 return;
1641 }
1642 const BookmarkNode* node = model->GetNodeByID(id);
1643 DCHECK(node);
1644 if (node) {
1645 model->SetTitle(node, WideToUTF16(title));
1646 *success = true;
1647 }
1648 }
1649 }
1650 *success = false;
1651}
1652
1653void AutomationProvider::SetBookmarkURL(int handle,
1654 int64 id, const GURL& url,
1655 bool* success) {
1656 if (browser_tracker_->ContainsHandle(handle)) {
1657 Browser* browser = browser_tracker_->GetResource(handle);
1658 if (browser) {
1659 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1660 if (!model->IsLoaded()) {
1661 *success = false;
1662 return;
1663 }
1664 const BookmarkNode* node = model->GetNodeByID(id);
1665 DCHECK(node);
1666 if (node) {
1667 model->SetURL(node, url);
1668 *success = true;
1669 }
1670 }
1671 }
1672 *success = false;
1673}
1674
1675void AutomationProvider::RemoveBookmark(int handle,
1676 int64 id,
1677 bool* success) {
1678 if (browser_tracker_->ContainsHandle(handle)) {
1679 Browser* browser = browser_tracker_->GetResource(handle);
1680 if (browser) {
1681 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1682 if (!model->IsLoaded()) {
1683 *success = false;
1684 return;
1685 }
1686 const BookmarkNode* node = model->GetNodeByID(id);
1687 DCHECK(node);
1688 if (node) {
1689 const BookmarkNode* parent = node->GetParent();
1690 DCHECK(parent);
1691 model->Remove(parent, parent->IndexOfChild(node));
1692 *success = true;
1693 }
1694 }
1695 }
1696 *success = false;
1697}
1698
[email protected]ef413ca2010-05-25 21:09:141699// Sample json input: { "command": "SetWindowDimensions",
1700// "x": 20, # optional
1701// "y": 20, # optional
1702// "width": 800, # optional
1703// "height": 600 } # optional
1704void AutomationProvider::SetWindowDimensions(Browser* browser,
1705 DictionaryValue* args,
1706 IPC::Message* reply_message) {
1707 gfx::Rect rect = browser->window()->GetRestoredBounds();
1708 int x, y, width, height;
1709 if (args->GetInteger(L"x", &x))
1710 rect.set_x(x);
1711 if (args->GetInteger(L"y", &y))
1712 rect.set_y(y);
1713 if (args->GetInteger(L"width", &width))
1714 rect.set_width(width);
1715 if (args->GetInteger(L"height", &height))
1716 rect.set_height(height);
1717 browser->window()->SetBounds(rect);
[email protected]7c983cc2010-07-16 11:33:341718 AutomationJSONReply(this, reply_message).SendSuccess(NULL);
[email protected]ef413ca2010-05-25 21:09:141719}
1720
[email protected]a9ff2c02010-05-13 17:33:051721// Sample json input: { "command": "GetBrowserInfo" }
1722// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1723// sample json output.
[email protected]53329582010-05-14 21:10:581724void AutomationProvider::GetBrowserInfo(Browser* browser,
1725 DictionaryValue* args,
[email protected]a9ff2c02010-05-13 17:33:051726 IPC::Message* reply_message) {
[email protected]a9ff2c02010-05-13 17:33:051727 DictionaryValue* properties = new DictionaryValue;
1728 properties->SetString(L"ChromeVersion", chrome::kChromeVersion);
1729 properties->SetString(L"BrowserProcessExecutableName",
1730 chrome::kBrowserProcessExecutableName);
1731 properties->SetString(L"HelperProcessExecutableName",
1732 chrome::kHelperProcessExecutableName);
1733 properties->SetString(L"BrowserProcessExecutablePath",
1734 chrome::kBrowserProcessExecutablePath);
1735 properties->SetString(L"HelperProcessExecutablePath",
1736 chrome::kHelperProcessExecutablePath);
[email protected]a9ff2c02010-05-13 17:33:051737 properties->SetString(L"command_line_string",
1738 CommandLine::ForCurrentProcess()->command_line_string());
[email protected]44eed9f2010-06-28 22:04:001739
1740 std::string branding;
1741#if defined(GOOGLE_CHROME_BUILD)
1742 branding = "Google Chrome";
1743#elif defined(CHROMIUM_BUILD)
1744 branding = "Chromium";
1745#else
1746 branding = "Unknown Branding";
[email protected]a9ff2c02010-05-13 17:33:051747#endif
[email protected]44eed9f2010-06-28 22:04:001748 properties->SetString(L"branding", branding);
[email protected]a9ff2c02010-05-13 17:33:051749
1750 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1751 return_value->Set(L"properties", properties);
1752
[email protected]ef413ca2010-05-25 21:09:141753 return_value->SetInteger(L"browser_pid", base::GetCurrentProcId());
1754 // Add info about all windows in a list of dictionaries, one dictionary
1755 // item per window.
1756 ListValue* windows = new ListValue;
1757 int windex = 0;
1758 for (BrowserList::const_iterator it = BrowserList::begin();
1759 it != BrowserList::end();
1760 ++it, ++windex) {
1761 DictionaryValue* browser_item = new DictionaryValue;
1762 browser = *it;
1763 browser_item->SetInteger(L"index", windex);
1764 // Window properties
1765 gfx::Rect rect = browser->window()->GetRestoredBounds();
1766 browser_item->SetInteger(L"x", rect.x());
1767 browser_item->SetInteger(L"y", rect.y());
1768 browser_item->SetInteger(L"width", rect.width());
1769 browser_item->SetInteger(L"height", rect.height());
1770 browser_item->SetBoolean(L"fullscreen",
1771 browser->window()->IsFullscreen());
1772 browser_item->SetInteger(L"selected_tab", browser->selected_index());
1773 browser_item->SetBoolean(L"incognito",
1774 browser->profile()->IsOffTheRecord());
1775 // For each window, add info about all tabs in a list of dictionaries,
1776 // one dictionary item per tab.
1777 ListValue* tabs = new ListValue;
1778 for (int i = 0; i < browser->tab_count(); ++i) {
1779 TabContents* tc = browser->GetTabContentsAt(i);
1780 DictionaryValue* tab = new DictionaryValue;
1781 tab->SetInteger(L"index", i);
1782 tab->SetString(L"url", tc->GetURL().spec());
1783 tab->SetInteger(L"renderer_pid",
1784 base::GetProcId(tc->GetRenderProcessHost()->GetHandle()));
1785 tab->SetInteger(L"num_infobars", tc->infobar_delegate_count());
1786 tabs->Append(tab);
1787 }
1788 browser_item->Set(L"tabs", tabs);
1789
1790 windows->Append(browser_item);
1791 }
1792 return_value->Set(L"windows", windows);
1793
1794 return_value->SetString(L"child_process_path",
1795 ChildProcessHost::GetChildPath(true).value());
1796 // Child processes are the processes for plugins and other workers.
1797 // Add all child processes in a list of dictionaries, one dictionary item
1798 // per child process.
1799 ListValue* child_processes = new ListValue;
[email protected]d27893f62010-07-03 05:47:421800 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) {
[email protected]ef413ca2010-05-25 21:09:141801 // Only add processes which are already started, since we need their handle.
1802 if ((*iter)->handle() != base::kNullProcessHandle) {
1803 ChildProcessInfo* info = *iter;
1804 DictionaryValue* item = new DictionaryValue;
1805 item->SetString(L"name", info->name());
1806 item->SetString(L"type",
1807 ChildProcessInfo::GetTypeNameInEnglish(info->type()));
1808 item->SetInteger(L"pid", base::GetProcId(info->handle()));
1809 child_processes->Append(item);
1810 }
1811 }
1812 return_value->Set(L"child_processes", child_processes);
1813
1814 // Add all extension processes in a list of dictionaries, one dictionary
1815 // item per extension process.
1816 ListValue* extension_processes = new ListValue;
1817 ProfileManager* profile_manager = g_browser_process->profile_manager();
1818 for (ProfileManager::const_iterator it = profile_manager->begin();
1819 it != profile_manager->end(); ++it) {
1820 ExtensionProcessManager* process_manager =
1821 (*it)->GetExtensionProcessManager();
1822 ExtensionProcessManager::const_iterator jt;
1823 for (jt = process_manager->begin(); jt != process_manager->end(); ++jt) {
1824 ExtensionHost* ex_host = *jt;
1825 // Don't add dead extension processes.
1826 if (!ex_host->IsRenderViewLive())
1827 continue;
1828 DictionaryValue* item = new DictionaryValue;
1829 item->SetString(L"name", ex_host->extension()->name());
1830 item->SetInteger(
1831 L"pid",
1832 base::GetProcId(ex_host->render_process_host()->GetHandle()));
1833 extension_processes->Append(item);
1834 }
1835 }
1836 return_value->Set(L"extension_processes", extension_processes);
[email protected]7c983cc2010-07-16 11:33:341837 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]a9ff2c02010-05-13 17:33:051838}
1839
[email protected]24e2b102010-04-29 17:56:471840// Sample json input: { "command": "GetHistoryInfo",
1841// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361842// Refer chrome/test/pyautolib/history_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581843void AutomationProvider::GetHistoryInfo(Browser* browser,
1844 DictionaryValue* args,
1845 IPC::Message* reply_message) {
[email protected]e6e376e2010-04-19 21:41:361846 consumer_.CancelAllRequests();
1847
[email protected]e53668962010-06-23 15:35:251848 string16 search_text;
1849 args->GetString("search_text", &search_text);
[email protected]e6e376e2010-04-19 21:41:361850
1851 // Fetch history.
1852 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1853 history::QueryOptions options;
1854 // The observer owns itself. It deletes itself after it fetches history.
1855 AutomationProviderHistoryObserver* history_observer =
1856 new AutomationProviderHistoryObserver(this, reply_message);
1857 hs->QueryHistory(
1858 search_text,
1859 options,
1860 &consumer_,
1861 NewCallback(history_observer,
1862 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1863}
1864
[email protected]bbe6aa02010-05-07 17:27:291865// Sample json input: { "command": "AddHistoryItem",
1866// "item": { "URL": "http://www.google.com",
1867// "title": "Google", # optional
1868// "time": 12345 # optional (time_t)
1869// } }
1870// Refer chrome/test/pyautolib/pyauto.py for details on input.
[email protected]53329582010-05-14 21:10:581871void AutomationProvider::AddHistoryItem(Browser* browser,
1872 DictionaryValue* args,
1873 IPC::Message* reply_message) {
[email protected]bbe6aa02010-05-07 17:27:291874 DictionaryValue* item = NULL;
1875 args->GetDictionary(L"item", &item);
1876 string16 url_text;
[email protected]e53668962010-06-23 15:35:251877 string16 title;
[email protected]bbe6aa02010-05-07 17:27:291878 base::Time time = base::Time::Now();
[email protected]7c983cc2010-07-16 11:33:341879 AutomationJSONReply reply(this, reply_message);
[email protected]bbe6aa02010-05-07 17:27:291880
[email protected]7c983cc2010-07-16 11:33:341881 if (!item->GetString("url", &url_text)) {
1882 reply.SendError("bad args (no URL in dict?)");
1883 return;
[email protected]bbe6aa02010-05-07 17:27:291884 }
[email protected]7c983cc2010-07-16 11:33:341885 GURL gurl(url_text);
1886 item->GetString("title", &title); // Don't care if it fails.
1887 int it;
1888 double dt;
1889 if (item->GetInteger(L"time", &it))
1890 time = base::Time::FromTimeT(it);
1891 else if (item->GetReal(L"time", &dt))
1892 time = base::Time::FromDoubleT(dt);
[email protected]f6ff0df2010-07-11 22:41:431893
[email protected]7c983cc2010-07-16 11:33:341894 // Ideas for "dummy" values (e.g. id_scope) came from
1895 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
1896 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1897 const void* id_scope = reinterpret_cast<void*>(1);
1898 hs->AddPage(gurl, time,
1899 id_scope,
1900 0,
1901 GURL(),
1902 PageTransition::LINK,
1903 history::RedirectList(),
1904 false);
1905 if (title.length())
1906 hs->SetPageTitle(gurl, title);
1907 reply.SendSuccess(NULL);
[email protected]bbe6aa02010-05-07 17:27:291908}
1909
[email protected]24e2b102010-04-29 17:56:471910// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361911// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581912void AutomationProvider::GetDownloadsInfo(Browser* browser,
1913 DictionaryValue* args,
1914 IPC::Message* reply_message) {
[email protected]d4adc292010-04-15 18:06:391915 AutomationProviderDownloadManagerObserver observer;
1916 std::vector<DownloadItem*> downloads;
1917 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
[email protected]7c983cc2010-07-16 11:33:341918 AutomationJSONReply reply(this, reply_message);
[email protected]d4adc292010-04-15 18:06:391919
1920 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:341921 reply.SendError("no download manager");
1922 return;
[email protected]d4adc292010-04-15 18:06:391923 }
[email protected]7c983cc2010-07-16 11:33:341924 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1925 // since that would be transient; a download might enter and empty out
1926 // the current download queue too soon to be noticed.
1927 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1928 downloads = observer.Downloads();
[email protected]d4adc292010-04-15 18:06:391929
1930 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1931 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1932 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1933 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1934 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1935
1936 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1937 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1938 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1939 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1940 std::string("DANGEROUS_BUT_VALIDATED");
1941
1942 ListValue* list_of_downloads = new ListValue;
1943 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1944 it != downloads.end();
1945 it++) { // Fill info about each download item.
1946 DictionaryValue* dl_item_value = new DictionaryValue;
1947 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1948 dl_item_value->SetString(L"url", (*it)->url().spec());
1949 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1950 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1951 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1952 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1953 dl_item_value->SetBoolean(L"open_when_complete",
1954 (*it)->open_when_complete());
1955 dl_item_value->SetBoolean(L"is_extension_install",
1956 (*it)->is_extension_install());
1957 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1958 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1959 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1960 dl_item_value->SetString(L"safety_state",
1961 safety_state_to_string[(*it)->safety_state()]);
1962 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1963 list_of_downloads->Append(dl_item_value);
1964 }
1965 return_value->Set(L"downloads", list_of_downloads);
[email protected]d4adc292010-04-15 18:06:391966
[email protected]7c983cc2010-07-16 11:33:341967 reply.SendSuccess(return_value.get());
[email protected]d4adc292010-04-15 18:06:391968 // All value objects allocated above are owned by |return_value|
1969 // and get freed by it.
1970}
1971
[email protected]59a611242010-04-02 02:24:041972void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:581973 Browser* browser,
[email protected]59a611242010-04-02 02:24:041974 DictionaryValue* args,
1975 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:041976 AutomationProviderDownloadManagerObserver observer;
1977 std::vector<DownloadItem*> downloads;
[email protected]7c983cc2010-07-16 11:33:341978 AutomationJSONReply reply(this, reply_message);
[email protected]59a611242010-04-02 02:24:041979
1980 // Look for a quick return.
1981 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7c983cc2010-07-16 11:33:341982 reply.SendSuccess(NULL); // No download manager.
1983 return;
[email protected]59a611242010-04-02 02:24:041984 }
[email protected]7c983cc2010-07-16 11:33:341985 profile_->GetDownloadManager()->GetCurrentDownloads(&observer, FilePath());
1986 downloads = observer.Downloads();
1987 if (downloads.size() == 0) {
1988 reply.SendSuccess(NULL);
[email protected]f6ff0df2010-07-11 22:41:431989 return;
1990 }
[email protected]59a611242010-04-02 02:24:041991
1992 // The observer owns itself. When the last observed item pings, it
1993 // deletes itself.
1994 AutomationProviderDownloadItemObserver* item_observer =
1995 new AutomationProviderDownloadItemObserver(
1996 this, reply_message, downloads.size());
1997 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
1998 i != downloads.end();
1999 i++) {
2000 (*i)->AddObserver(item_observer);
2001 }
2002}
2003
[email protected]24e2b102010-04-29 17:56:472004// Sample json input: { "command": "GetPrefsInfo" }
2005// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582006void AutomationProvider::GetPrefsInfo(Browser* browser,
2007 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472008 IPC::Message* reply_message) {
[email protected]24e2b102010-04-29 17:56:472009 const PrefService::PreferenceSet& prefs =
2010 profile_->GetPrefs()->preference_set();
2011 DictionaryValue* items = new DictionaryValue;
2012 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
2013 it != prefs.end(); ++it) {
2014 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
2015 }
2016 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2017 return_value->Set(L"prefs", items); // return_value owns items.
[email protected]7c983cc2010-07-16 11:33:342018 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]24e2b102010-04-29 17:56:472019}
2020
2021// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:582022void AutomationProvider::SetPrefs(Browser* browser,
2023 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472024 IPC::Message* reply_message) {
[email protected]24e2b102010-04-29 17:56:472025 std::wstring path;
2026 Value* val;
[email protected]7c983cc2010-07-16 11:33:342027 AutomationJSONReply reply(this, reply_message);
[email protected]24e2b102010-04-29 17:56:472028 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
2029 PrefService* pref_service = profile_->GetPrefs();
2030 const PrefService::Preference* pref =
2031 pref_service->FindPreference(path.c_str());
2032 if (!pref) { // Not a registered pref.
[email protected]7c983cc2010-07-16 11:33:342033 reply.SendError("pref not registered.");
2034 return;
[email protected]24e2b102010-04-29 17:56:472035 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
[email protected]7c983cc2010-07-16 11:33:342036 reply.SendError("pref is managed. cannot be changed.");
2037 return;
[email protected]24e2b102010-04-29 17:56:472038 } else { // Set the pref.
2039 pref_service->Set(path.c_str(), *val);
2040 }
2041 } else {
[email protected]7c983cc2010-07-16 11:33:342042 reply.SendError("no pref path or value given.");
2043 return;
[email protected]24e2b102010-04-29 17:56:472044 }
2045
[email protected]7c983cc2010-07-16 11:33:342046 reply.SendSuccess(NULL);
[email protected]24e2b102010-04-29 17:56:472047}
2048
[email protected]53329582010-05-14 21:10:582049// Sample json input: { "command": "GetOmniboxInfo" }
2050// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
2051void AutomationProvider::GetOmniboxInfo(Browser* browser,
2052 DictionaryValue* args,
2053 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582054 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2055
2056 LocationBar* loc_bar = browser->window()->GetLocationBar();
2057 AutocompleteEditView* edit_view = loc_bar->location_entry();
2058 AutocompleteEditModel* model = edit_view->model();
2059
2060 // Fill up matches.
2061 ListValue* matches = new ListValue;
2062 const AutocompleteResult& result = model->result();
2063 for (AutocompleteResult::const_iterator i = result.begin();
2064 i != result.end(); ++i) {
2065 const AutocompleteMatch& match = *i;
2066 DictionaryValue* item = new DictionaryValue; // owned by return_value
2067 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
2068 item->SetBoolean(L"starred", match.starred);
2069 item->SetString(L"destination_url", match.destination_url.spec());
2070 item->SetString(L"contents", match.contents);
2071 item->SetString(L"description", match.description);
2072 matches->Append(item);
2073 }
2074 return_value->Set(L"matches", matches);
2075
2076 // Fill up other properties.
2077 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2078 properties->SetBoolean(L"has_focus", model->has_focus());
2079 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2080 properties->SetString(L"keyword", model->keyword());
2081 properties->SetString(L"text", edit_view->GetText());
2082 return_value->Set(L"properties", properties);
2083
[email protected]7c983cc2010-07-16 11:33:342084 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]53329582010-05-14 21:10:582085}
2086
2087// Sample json input: { "command": "SetOmniboxText",
2088// "text": "goog" }
2089void AutomationProvider::SetOmniboxText(Browser* browser,
2090 DictionaryValue* args,
2091 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582092 std::wstring text;
[email protected]7c983cc2010-07-16 11:33:342093 AutomationJSONReply reply(this, reply_message);
[email protected]53329582010-05-14 21:10:582094 if (!args->GetString(L"text", &text)) {
[email protected]7c983cc2010-07-16 11:33:342095 reply.SendError("text missing");
2096 return;
[email protected]53329582010-05-14 21:10:582097 }
[email protected]7c983cc2010-07-16 11:33:342098 browser->FocusLocationBar();
2099 LocationBar* loc_bar = browser->window()->GetLocationBar();
2100 AutocompleteEditView* edit_view = loc_bar->location_entry();
2101 edit_view->model()->OnSetFocus(false);
2102 edit_view->SetUserText(text);
2103 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582104}
2105
2106// Sample json input: { "command": "OmniboxMovePopupSelection",
2107// "count": 1 }
2108// Negative count implies up, positive implies down. Count values will be
2109// capped by the size of the popup list.
2110void AutomationProvider::OmniboxMovePopupSelection(
2111 Browser* browser,
2112 DictionaryValue* args,
2113 IPC::Message* reply_message) {
[email protected]53329582010-05-14 21:10:582114 int count;
[email protected]7c983cc2010-07-16 11:33:342115 AutomationJSONReply reply(this, reply_message);
[email protected]53329582010-05-14 21:10:582116 if (!args->GetInteger(L"count", &count)) {
[email protected]7c983cc2010-07-16 11:33:342117 reply.SendError("count missing");
2118 return;
[email protected]53329582010-05-14 21:10:582119 }
[email protected]7c983cc2010-07-16 11:33:342120 LocationBar* loc_bar = browser->window()->GetLocationBar();
2121 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2122 model->OnUpOrDownKeyPressed(count);
2123 reply.SendSuccess(NULL);
[email protected]53329582010-05-14 21:10:582124}
2125
2126// Sample json input: { "command": "OmniboxAcceptInput" }
2127void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2128 DictionaryValue* args,
2129 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282130 NavigationController& controller =
2131 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122132 // Setup observer to wait until the selected item loads.
2133 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282134 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122135 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582136
2137 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582138}
2139
[email protected]a3cd5022010-06-16 18:25:292140// Sample json input: { "command": "GetInitialLoadTimes" }
2141// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2142void AutomationProvider::GetInitialLoadTimes(
2143 Browser*,
2144 DictionaryValue*,
2145 IPC::Message* reply_message) {
2146 scoped_ptr<DictionaryValue> return_value(
2147 initial_load_observer_->GetTimingInformation());
[email protected]f6ff0df2010-07-11 22:41:432148
2149 std::string json_return;
2150 base::JSONWriter::Write(return_value.get(), false, &json_return);
2151 AutomationMsg_SendJSONRequest::WriteReplyParams(
2152 reply_message, json_return, true);
2153 Send(reply_message);
[email protected]a3cd5022010-06-16 18:25:292154}
2155
[email protected]f7d48012010-05-06 08:17:052156// Sample json input: { "command": "GetPluginsInfo" }
2157// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582158void AutomationProvider::GetPluginsInfo(Browser* browser,
2159 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052160 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052161 std::vector<WebPluginInfo> plugins;
2162 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2163 ListValue* items = new ListValue;
2164 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2165 it != plugins.end();
2166 ++it) {
2167 DictionaryValue* item = new DictionaryValue;
[email protected]c9d811372010-06-23 21:44:572168 item->SetStringFromUTF16(L"name", it->name);
[email protected]f7d48012010-05-06 08:17:052169 item->SetString(L"path", it->path.value());
[email protected]c9d811372010-06-23 21:44:572170 item->SetStringFromUTF16(L"version", it->version);
2171 item->SetStringFromUTF16(L"desc", it->desc);
[email protected]f7d48012010-05-06 08:17:052172 item->SetBoolean(L"enabled", it->enabled);
2173 // Add info about mime types.
2174 ListValue* mime_types = new ListValue();
2175 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2176 it->mime_types.begin();
2177 type_it != it->mime_types.end();
2178 ++type_it) {
2179 DictionaryValue* mime_type = new DictionaryValue();
2180 mime_type->SetString(L"mimeType", type_it->mime_type);
[email protected]c9d811372010-06-23 21:44:572181 mime_type->SetStringFromUTF16(L"description", type_it->description);
[email protected]f7d48012010-05-06 08:17:052182
2183 ListValue* file_extensions = new ListValue();
2184 for (std::vector<std::string>::const_iterator ext_it =
2185 type_it->file_extensions.begin();
2186 ext_it != type_it->file_extensions.end();
2187 ++ext_it) {
2188 file_extensions->Append(new StringValue(*ext_it));
2189 }
2190 mime_type->Set(L"fileExtensions", file_extensions);
2191
2192 mime_types->Append(mime_type);
2193 }
2194 item->Set(L"mimeTypes", mime_types);
2195 items->Append(item);
2196 }
2197 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2198 return_value->Set(L"plugins", items); // return_value owns items.
2199
[email protected]7c983cc2010-07-16 11:33:342200 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]f7d48012010-05-06 08:17:052201}
2202
2203// Sample json input:
2204// { "command": "EnablePlugin",
2205// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582206void AutomationProvider::EnablePlugin(Browser* browser,
2207 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052208 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052209 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342210 AutomationJSONReply reply(this, reply_message);
[email protected]f6ff0df2010-07-11 22:41:432211 if (!args->GetString(L"path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342212 reply.SendError("path not specified.");
2213 return;
[email protected]f6ff0df2010-07-11 22:41:432214 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342215 reply.SendError(StringPrintf("Could not enable plugin for path %s.",
2216 path.c_str()));
2217 return;
[email protected]f6ff0df2010-07-11 22:41:432218 }
[email protected]7c983cc2010-07-16 11:33:342219 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052220}
2221
2222// Sample json input:
2223// { "command": "DisablePlugin",
2224// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582225void AutomationProvider::DisablePlugin(Browser* browser,
2226 DictionaryValue* args,
2227 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052228 FilePath::StringType path;
[email protected]7c983cc2010-07-16 11:33:342229 AutomationJSONReply reply(this, reply_message);
[email protected]f6ff0df2010-07-11 22:41:432230 if (!args->GetString(L"path", &path)) {
[email protected]7c983cc2010-07-16 11:33:342231 reply.SendError("path not specified.");
2232 return;
[email protected]f6ff0df2010-07-11 22:41:432233 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
[email protected]7c983cc2010-07-16 11:33:342234 reply.SendError(StringPrintf("Could not disable plugin for path %s.",
2235 path.c_str()));
2236 return;
[email protected]f6ff0df2010-07-11 22:41:432237 }
[email protected]7c983cc2010-07-16 11:33:342238 reply.SendSuccess(NULL);
[email protected]f7d48012010-05-06 08:17:052239}
2240
[email protected]7060bb292010-06-24 00:52:492241// Sample json input:
2242// { "command": "SaveTabContents",
2243// "tab_index": 0,
2244// "filename": <a full pathname> }
2245// Sample json output:
2246// {}
2247void AutomationProvider::SaveTabContents(Browser* browser,
2248 DictionaryValue* args,
2249 IPC::Message* reply_message) {
[email protected]7060bb292010-06-24 00:52:492250 int tab_index = 0;
2251 FilePath::StringType filename;
2252 FilePath::StringType parent_directory;
2253 TabContents* tab_contents = NULL;
[email protected]7c983cc2010-07-16 11:33:342254 AutomationJSONReply reply(this, reply_message);
[email protected]7060bb292010-06-24 00:52:492255
2256 if (!args->GetInteger(L"tab_index", &tab_index) ||
2257 !args->GetString(L"filename", &filename)) {
[email protected]7c983cc2010-07-16 11:33:342258 reply.SendError("tab_index or filename param missing");
2259 return;
[email protected]7060bb292010-06-24 00:52:492260 } else {
2261 tab_contents = browser->GetTabContentsAt(tab_index);
2262 if (!tab_contents) {
[email protected]7c983cc2010-07-16 11:33:342263 reply.SendError("no tab at tab_index");
[email protected]7060bb292010-06-24 00:52:492264 return;
2265 }
2266 }
[email protected]7c983cc2010-07-16 11:33:342267 // We're doing a SAVE_AS_ONLY_HTML so the the directory path isn't
2268 // used. Nevertheless, SavePackage requires it be valid. Sigh.
2269 parent_directory = FilePath(filename).DirName().value();
2270 if (!tab_contents->SavePage(FilePath(filename), FilePath(parent_directory),
2271 SavePackage::SAVE_AS_ONLY_HTML)) {
2272 reply.SendError("Could not initiate SavePage");
2273 return;
2274 }
2275 // The observer will delete itself when done.
2276 new SavePackageNotificationObserver(tab_contents->save_package(),
2277 this, reply_message);
[email protected]7060bb292010-06-24 00:52:492278}
2279
[email protected]c5aa5322010-07-15 19:00:072280// Refer to ImportSettings() in chrome/test/pyautolib/pyauto.py for sample
2281// json input.
2282// Sample json output: "{}"
2283void AutomationProvider::ImportSettings(Browser* browser,
2284 DictionaryValue* args,
2285 IPC::Message* reply_message) {
[email protected]c5aa5322010-07-15 19:00:072286 // Map from the json string passed over to the import item masks.
2287 std::map<std::string, ImportItem> string_to_import_item;
2288 string_to_import_item["HISTORY"] = importer::HISTORY;
2289 string_to_import_item["FAVORITES"] = importer::FAVORITES;
2290 string_to_import_item["COOKIES"] = importer::COOKIES;
2291 string_to_import_item["PASSWORDS"] = importer::PASSWORDS;
2292 string_to_import_item["SEARCH_ENGINES"] = importer::SEARCH_ENGINES;
2293 string_to_import_item["HOME_PAGE"] = importer::HOME_PAGE;
2294 string_to_import_item["ALL"] = importer::ALL;
2295
2296 std::wstring browser_name;
2297 int import_items = 0;
2298 ListValue* import_items_list = NULL;
2299 bool first_run;
2300
2301 if (!args->GetString(L"import_from", &browser_name) ||
2302 !args->GetBoolean(L"first_run", &first_run) ||
2303 !args->GetList(L"import_items", &import_items_list)) {
[email protected]7c983cc2010-07-16 11:33:342304 AutomationJSONReply(this, reply_message).SendError(
2305 "Incorrect type for one or more of the arguments.");
[email protected]c5aa5322010-07-15 19:00:072306 return;
2307 }
2308
2309 int num_items = import_items_list->GetSize();
2310 for (int i = 0; i < num_items; i++) {
2311 std::string item;
2312 import_items_list->GetString(i, &item);
2313 // If the provided string is not part of the map, error out.
2314 if (!ContainsKey(string_to_import_item, item)) {
[email protected]7c983cc2010-07-16 11:33:342315 AutomationJSONReply(this, reply_message).SendError(
2316 "Invalid item string found in import_items.");
[email protected]c5aa5322010-07-15 19:00:072317 return;
2318 }
2319 import_items |= string_to_import_item[item];
2320 }
2321
2322 ImporterHost* importer_host = new ImporterHost();
2323 // Get the correct ProfileInfo based on the browser they user provided.
2324 importer::ProfileInfo profile_info;
2325 int num_browsers = importer_host->GetAvailableProfileCount();
2326 int i = 0;
2327 for ( ; i < num_browsers; i++) {
2328 std::wstring name = importer_host->GetSourceProfileNameAt(i);
2329 if (name == browser_name) {
2330 profile_info = importer_host->GetSourceProfileInfoAt(i);
2331 break;
2332 }
2333 }
2334 // If we made it to the end of the loop, then the input was bad.
2335 if (i == num_browsers) {
[email protected]7c983cc2010-07-16 11:33:342336 AutomationJSONReply(this, reply_message).SendError(
2337 "Invalid browser name string found.");
[email protected]c5aa5322010-07-15 19:00:072338 return;
2339 }
2340
2341 Profile* profile = browser->profile();
2342
2343 importer_host->SetObserver(
2344 new AutomationProviderImportSettingsObserver(this, reply_message));
2345 importer_host->StartImportSettings(profile_info, profile, import_items,
2346 new ProfileWriter(profile), first_run);
2347}
2348
[email protected]a0fc50d72010-07-14 21:14:192349// Refer to ClearBrowsingData() in chrome/test/pyautolib/pyauto.py for sample
2350// json input.
2351// Sample json output: {}
2352void AutomationProvider::ClearBrowsingData(Browser* browser,
2353 DictionaryValue* args,
2354 IPC::Message* reply_message) {
[email protected]a0fc50d72010-07-14 21:14:192355 std::map<std::string, BrowsingDataRemover::TimePeriod> string_to_time_period;
2356 string_to_time_period["LAST_HOUR"] = BrowsingDataRemover::LAST_HOUR;
2357 string_to_time_period["LAST_DAY"] = BrowsingDataRemover::LAST_DAY;
2358 string_to_time_period["LAST_WEEK"] = BrowsingDataRemover::LAST_WEEK;
2359 string_to_time_period["FOUR_WEEKS"] = BrowsingDataRemover::FOUR_WEEKS;
2360 string_to_time_period["EVERYTHING"] = BrowsingDataRemover::EVERYTHING;
2361
2362 std::map<std::string, int> string_to_mask_value;
2363 string_to_mask_value["HISTORY"] = BrowsingDataRemover::REMOVE_HISTORY;
2364 string_to_mask_value["DOWNLOADS"] = BrowsingDataRemover::REMOVE_DOWNLOADS;
2365 string_to_mask_value["COOKIES"] = BrowsingDataRemover::REMOVE_COOKIES;
2366 string_to_mask_value["PASSWORDS"] = BrowsingDataRemover::REMOVE_PASSWORDS;
2367 string_to_mask_value["FORM_DATA"] = BrowsingDataRemover::REMOVE_FORM_DATA;
2368 string_to_mask_value["CACHE"] = BrowsingDataRemover::REMOVE_CACHE;
2369
2370 std::string time_period;
2371 ListValue* to_remove;
2372 if (!args->GetString(L"time_period", &time_period) ||
2373 !args->GetList(L"to_remove", &to_remove)) {
[email protected]7c983cc2010-07-16 11:33:342374 AutomationJSONReply(this, reply_message).SendError(
2375 "time_period must be a string and to_remove a list.");
[email protected]a0fc50d72010-07-14 21:14:192376 return;
2377 }
2378
2379 int remove_mask = 0;
2380 int num_removals = to_remove->GetSize();
2381 for (int i = 0; i < num_removals; i++) {
2382 std::string removal;
2383 to_remove->GetString(i, &removal);
2384 // If the provided string is not part of the map, then error out.
2385 if (!ContainsKey(string_to_mask_value, removal)) {
[email protected]7c983cc2010-07-16 11:33:342386 AutomationJSONReply(this, reply_message).SendError(
2387 "Invalid browsing data string found in to_remove.");
[email protected]a0fc50d72010-07-14 21:14:192388 return;
2389 }
2390 remove_mask |= string_to_mask_value[removal];
2391 }
2392
2393 if (!ContainsKey(string_to_time_period, time_period)) {
[email protected]7c983cc2010-07-16 11:33:342394 AutomationJSONReply(this, reply_message).SendError(
2395 "Invalid string for time_period.");
[email protected]a0fc50d72010-07-14 21:14:192396 return;
2397 }
2398
2399 BrowsingDataRemover* remover = new BrowsingDataRemover(
2400 profile(), string_to_time_period[time_period], base::Time());
2401
2402 remover->AddObserver(
2403 new AutomationProviderBrowsingDataObserver(this, reply_message));
2404 remover->Remove(remove_mask);
2405 // BrowsingDataRemover deletes itself using DeleteTask.
2406 // The observer also deletes itself after sending the reply.
2407}
2408
[email protected]4d1929f12010-07-10 00:09:032409// Sample json input: { "command": "GetThemeInfo" }
2410// Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output.
2411void AutomationProvider::GetThemeInfo(Browser* browser,
2412 DictionaryValue* args,
2413 IPC::Message* reply_message) {
2414 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2415 Extension* theme = browser->profile()->GetTheme();
2416 if (theme) {
2417 return_value->SetString(L"name", theme->name());
2418 return_value->Set(L"images", theme->GetThemeImages()->DeepCopy());
2419 return_value->Set(L"colors", theme->GetThemeColors()->DeepCopy());
2420 return_value->Set(L"tints", theme->GetThemeTints()->DeepCopy());
2421 }
[email protected]7c983cc2010-07-16 11:33:342422 AutomationJSONReply(this, reply_message).SendSuccess(return_value.get());
[email protected]4d1929f12010-07-10 00:09:032423}
2424
[email protected]55846ad842010-07-09 18:22:562425// Sample json input:
2426// { "command": "GetAutoFillProfile" }
2427// Refer to GetAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2428// json output.
2429void AutomationProvider::GetAutoFillProfile(Browser* browser,
2430 DictionaryValue* args,
2431 IPC::Message* reply_message) {
[email protected]55846ad842010-07-09 18:22:562432 // Get the AutoFillProfiles currently in the database.
2433 int tab_index = 0;
2434 args->GetInteger(L"tab_index", &tab_index);
2435 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
[email protected]7c983cc2010-07-16 11:33:342436 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:562437
2438 if (tab_contents) {
2439 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2440 ->GetPersonalDataManager();
2441 if (pdm) {
2442 std::vector<AutoFillProfile*> autofill_profiles = pdm->profiles();
2443 std::vector<CreditCard*> credit_cards = pdm->credit_cards();
2444
2445 ListValue* profiles = GetListFromAutoFillProfiles(autofill_profiles);
2446 ListValue* cards = GetListFromCreditCards(credit_cards);
2447
2448 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2449
2450 return_value->Set(L"profiles", profiles);
2451 return_value->Set(L"credit_cards", cards);
[email protected]7c983cc2010-07-16 11:33:342452 reply.SendSuccess(return_value.get());
[email protected]55846ad842010-07-09 18:22:562453 } else {
[email protected]7c983cc2010-07-16 11:33:342454 reply.SendError("No PersonalDataManager.");
2455 return;
[email protected]55846ad842010-07-09 18:22:562456 }
2457 } else {
[email protected]7c983cc2010-07-16 11:33:342458 reply.SendError("No tab at that index.");
2459 return;
[email protected]55846ad842010-07-09 18:22:562460 }
[email protected]55846ad842010-07-09 18:22:562461}
2462
2463// Refer to FillAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2464// json input.
2465// Sample json output: {}
2466void AutomationProvider::FillAutoFillProfile(Browser* browser,
2467 DictionaryValue* args,
2468 IPC::Message* reply_message) {
[email protected]7c983cc2010-07-16 11:33:342469 AutomationJSONReply reply(this, reply_message);
[email protected]55846ad842010-07-09 18:22:562470 ListValue* profiles = NULL;
2471 ListValue* cards = NULL;
2472 args->GetList(L"profiles", &profiles);
2473 args->GetList(L"credit_cards", &cards);
[email protected]7c983cc2010-07-16 11:33:342474 std::string error_mesg;
[email protected]55846ad842010-07-09 18:22:562475
2476 std::vector<AutoFillProfile> autofill_profiles;
2477 std::vector<CreditCard> credit_cards;
2478 // Create an AutoFillProfile for each of the dictionary profiles.
2479 if (profiles) {
[email protected]7c983cc2010-07-16 11:33:342480 autofill_profiles = GetAutoFillProfilesFromList(*profiles, &error_mesg);
[email protected]55846ad842010-07-09 18:22:562481 }
2482 // Create a CreditCard for each of the dictionary values.
2483 if (cards) {
[email protected]7c983cc2010-07-16 11:33:342484 credit_cards = GetCreditCardsFromList(*cards, &error_mesg);
2485 }
2486 if (!error_mesg.empty()) {
2487 reply.SendError(error_mesg);
2488 return;
[email protected]55846ad842010-07-09 18:22:562489 }
2490
2491 // Save the AutoFillProfiles.
2492 int tab_index = 0;
2493 args->GetInteger(L"tab_index", &tab_index);
2494 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2495
2496 if (tab_contents) {
2497 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2498 ->GetPersonalDataManager();
2499 if (pdm) {
2500 pdm->OnAutoFillDialogApply(profiles? &autofill_profiles : NULL,
2501 cards? &credit_cards : NULL);
2502 } else {
[email protected]7c983cc2010-07-16 11:33:342503 reply.SendError("No PersonalDataManager.");
2504 return;
[email protected]55846ad842010-07-09 18:22:562505 }
2506 } else {
[email protected]7c983cc2010-07-16 11:33:342507 reply.SendError("No tab at that index.");
2508 return;
[email protected]55846ad842010-07-09 18:22:562509 }
[email protected]7c983cc2010-07-16 11:33:342510 reply.SendSuccess(NULL);
[email protected]55846ad842010-07-09 18:22:562511}
2512
2513/* static */
2514ListValue* AutomationProvider::GetListFromAutoFillProfiles(
2515 std::vector<AutoFillProfile*> autofill_profiles) {
2516 ListValue* profiles = new ListValue;
2517
2518 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string
2519 = GetAutoFillFieldToStringMap();
2520
2521 // For each AutoFillProfile, transform it to a dictionary object to return.
2522 for (std::vector<AutoFillProfile*>::iterator it = autofill_profiles.begin();
2523 it != autofill_profiles.end(); ++it) {
2524 AutoFillProfile* profile = *it;
2525 DictionaryValue* profile_info = new DictionaryValue;
2526 profile_info->SetStringFromUTF16(L"label", profile->Label());
2527 // For each of the types, if it has a value, add it to the dictionary.
2528 for (std::map<AutoFillFieldType, std::wstring>::iterator
2529 type_it = autofill_type_to_string.begin();
2530 type_it != autofill_type_to_string.end(); ++type_it) {
2531 string16 value = profile->GetFieldText(AutoFillType(type_it->first));
2532 if (value.length()) { // If there was something stored for that value.
2533 profile_info->SetStringFromUTF16(type_it->second, value);
2534 }
2535 }
2536 profiles->Append(profile_info);
2537 }
2538 return profiles;
2539}
2540
2541/* static */
2542ListValue* AutomationProvider::GetListFromCreditCards(
2543 std::vector<CreditCard*> credit_cards) {
2544 ListValue* cards = new ListValue;
2545
2546 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2547 GetCreditCardFieldToStringMap();
2548
2549 // For each AutoFillProfile, transform it to a dictionary object to return.
2550 for (std::vector<CreditCard*>::iterator it = credit_cards.begin();
2551 it != credit_cards.end(); ++it) {
2552 CreditCard* card = *it;
2553 DictionaryValue* card_info = new DictionaryValue;
2554 card_info->SetStringFromUTF16(L"label", card->Label());
2555 // For each of the types, if it has a value, add it to the dictionary.
2556 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2557 credit_card_type_to_string.begin();
2558 type_it != credit_card_type_to_string.end(); ++type_it) {
2559 string16 value = card->GetFieldText(AutoFillType(type_it->first));
2560 // If there was something stored for that value.
2561 if (value.length()) {
2562 card_info->SetStringFromUTF16(type_it->second, value);
2563 }
2564 }
2565 cards->Append(card_info);
2566 }
2567 return cards;
2568}
2569
2570/* static */
2571std::vector<AutoFillProfile> AutomationProvider::GetAutoFillProfilesFromList(
[email protected]7c983cc2010-07-16 11:33:342572 const ListValue& profiles, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:562573 std::vector<AutoFillProfile> autofill_profiles;
2574 DictionaryValue* profile_info = NULL;
2575 string16 profile_label;
2576 string16 current_value;
2577
2578 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string =
2579 GetAutoFillFieldToStringMap();
2580
2581 int num_profiles = profiles.GetSize();
2582 for (int i = 0; i < num_profiles; i++) {
2583 profiles.GetDictionary(i, &profile_info);
2584 profile_info->GetString("label", &profile_label);
2585 // Choose an id of 0 so that a unique id will be created.
2586 AutoFillProfile profile(profile_label, 0);
2587 // Loop through the possible profile types and add those provided.
2588 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2589 autofill_type_to_string.begin();
2590 type_it != autofill_type_to_string.end(); ++type_it) {
2591 if (profile_info->HasKey(type_it->second)) {
2592 if (profile_info->GetStringAsUTF16(type_it->second, &current_value)) {
2593 profile.SetInfo(AutoFillType(type_it->first), current_value);
2594 } else {
[email protected]7c983cc2010-07-16 11:33:342595 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:562596 break;
2597 }
2598 }
2599 }
2600 autofill_profiles.push_back(profile);
2601 }
2602 return autofill_profiles;
2603}
2604
2605/* static */
2606std::vector<CreditCard> AutomationProvider::GetCreditCardsFromList(
[email protected]7c983cc2010-07-16 11:33:342607 const ListValue& cards, std::string* error_message) {
[email protected]55846ad842010-07-09 18:22:562608 std::vector<CreditCard> credit_cards;
2609 DictionaryValue* card_info = NULL;
2610 string16 card_label;
2611 string16 current_value;
2612
2613 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2614 GetCreditCardFieldToStringMap();
2615
2616 int num_credit_cards = cards.GetSize();
2617 for (int i = 0; i < num_credit_cards; i++) {
2618 cards.GetDictionary(i, &card_info);
2619 card_info->GetString("label", &card_label);
2620 CreditCard card(card_label, 0);
2621 // Loop through the possible credit card fields and add those provided.
2622 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2623 credit_card_type_to_string.begin();
2624 type_it != credit_card_type_to_string.end(); ++type_it) {
2625 if (card_info->HasKey(type_it->second)) {
2626 if (card_info->GetStringAsUTF16(type_it->second, &current_value)) {
2627 card.SetInfo(AutoFillType(type_it->first), current_value);
2628 } else {
[email protected]7c983cc2010-07-16 11:33:342629 *error_message= "All values must be strings";
[email protected]55846ad842010-07-09 18:22:562630 break;
2631 }
2632 }
2633 }
2634 credit_cards.push_back(card);
2635 }
2636 return credit_cards;
2637}
2638
2639/* static */
2640std::map<AutoFillFieldType, std::wstring>
2641 AutomationProvider::GetAutoFillFieldToStringMap() {
2642 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string;
2643 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2644 autofill_type_to_string[NAME_MIDDLE] = L"NAME_MIDDLE";
2645 autofill_type_to_string[NAME_LAST] = L"NAME_LAST";
2646 autofill_type_to_string[COMPANY_NAME] = L"COMPANY_NAME";
2647 autofill_type_to_string[EMAIL_ADDRESS] = L"EMAIL_ADDRESS";
2648 autofill_type_to_string[ADDRESS_HOME_LINE1] = L"ADDRESS_HOME_LINE1";
2649 autofill_type_to_string[ADDRESS_HOME_LINE2] = L"ADDRESS_HOME_LINE2";
2650 autofill_type_to_string[ADDRESS_HOME_CITY] = L"ADDRESS_HOME_CITY";
2651 autofill_type_to_string[ADDRESS_HOME_STATE] = L"ADDRESS_HOME_STATE";
2652 autofill_type_to_string[ADDRESS_HOME_ZIP] = L"ADDRESS_HOME_ZIP";
2653 autofill_type_to_string[ADDRESS_HOME_COUNTRY] = L"ADDRESS_HOME_COUNTRY";
2654 autofill_type_to_string[PHONE_HOME_NUMBER] = L"PHONE_HOME_NUMBER";
2655 autofill_type_to_string[PHONE_FAX_NUMBER] = L"PHONE_FAX_NUMBER";
2656 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2657 return autofill_type_to_string;
2658}
2659
2660/* static */
2661std::map<AutoFillFieldType, std::wstring>
2662 AutomationProvider::GetCreditCardFieldToStringMap() {
2663 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string;
2664 credit_card_type_to_string[CREDIT_CARD_NAME] = L"CREDIT_CARD_NAME";
2665 credit_card_type_to_string[CREDIT_CARD_NUMBER] = L"CREDIT_CARD_NUMBER";
2666 credit_card_type_to_string[CREDIT_CARD_TYPE] = L"CREDIT_CARD_TYPE";
2667 credit_card_type_to_string[CREDIT_CARD_EXP_MONTH] = L"CREDIT_CARD_EXP_MONTH";
2668 credit_card_type_to_string[CREDIT_CARD_EXP_4_DIGIT_YEAR] =
2669 L"CREDIT_CARD_EXP_4_DIGIT_YEAR";
2670 return credit_card_type_to_string;
2671}
2672
[email protected]53329582010-05-14 21:10:582673void AutomationProvider::SendJSONRequest(int handle,
2674 std::string json_request,
2675 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:042676 Browser* browser = NULL;
[email protected]59a611242010-04-02 02:24:042677 scoped_ptr<Value> values;
2678
2679 // Basic error checking.
2680 if (browser_tracker_->ContainsHandle(handle)) {
2681 browser = browser_tracker_->GetResource(handle);
2682 }
2683 if (!browser) {
[email protected]7c983cc2010-07-16 11:33:342684 AutomationJSONReply(this, reply_message).SendError("no browser object");
2685 return;
2686 }
2687 base::JSONReader reader;
2688 std::string error;
2689 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
2690 if (!error.empty()) {
2691 AutomationJSONReply(this, reply_message).SendError(error);
2692 return;
[email protected]59a611242010-04-02 02:24:042693 }
2694
2695 // Make sure input is a dict with a string command.
2696 std::string command;
2697 DictionaryValue* dict_value = NULL;
[email protected]7c983cc2010-07-16 11:33:342698 if (values->GetType() != Value::TYPE_DICTIONARY) {
2699 AutomationJSONReply(this, reply_message).SendError("not a dict");
2700 return;
2701 }
2702 // Ownership remains with "values" variable.
2703 dict_value = static_cast<DictionaryValue*>(values.get());
2704 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
2705 AutomationJSONReply(this, reply_message).SendError(
2706 "no command key in dict or not a string command");
2707 return;
[email protected]59a611242010-04-02 02:24:042708 }
2709
[email protected]24e2b102010-04-29 17:56:472710 // Map json commands to their handlers.
2711 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:052712 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
2713 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
2714 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
2715
[email protected]a9ff2c02010-05-13 17:33:052716 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
[email protected]7c983cc2010-07-16 11:33:342717
[email protected]24e2b102010-04-29 17:56:472718 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:292719 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:052720
[email protected]53329582010-05-14 21:10:582721 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
2722 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
2723 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
2724 handler_map["OmniboxMovePopupSelection"] =
2725 &AutomationProvider::OmniboxMovePopupSelection;
2726
[email protected]24e2b102010-04-29 17:56:472727 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
2728 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:052729
[email protected]ef413ca2010-05-25 21:09:142730 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
2731
[email protected]f7d48012010-05-06 08:17:052732 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:472733 handler_map["WaitForAllDownloadsToComplete"] =
2734 &AutomationProvider::WaitForDownloadsToComplete;
2735
[email protected]a3cd5022010-06-16 18:25:292736 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
2737
[email protected]7060bb292010-06-24 00:52:492738 handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents;
2739
[email protected]c5aa5322010-07-15 19:00:072740 handler_map["ImportSettings"] = &AutomationProvider::ImportSettings;
2741
[email protected]a0fc50d72010-07-14 21:14:192742 handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData;
2743
[email protected]4d1929f12010-07-10 00:09:032744 // SetTheme() implemented using InstallExtension().
2745 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo;
2746
[email protected]55846ad842010-07-09 18:22:562747 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile;
2748 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile;
2749
[email protected]7c983cc2010-07-16 11:33:342750 if (handler_map.find(std::string(command)) != handler_map.end()) {
2751 (this->*handler_map[command])(browser, dict_value, reply_message);
[email protected]1ac875d22010-07-16 09:57:582752 } else {
[email protected]7c983cc2010-07-16 11:33:342753 std::string error_string = "Unknown command. Options: ";
2754 for (std::map<std::string, JsonHandler>::const_iterator it =
2755 handler_map.begin(); it != handler_map.end(); ++it) {
2756 error_string += it->first + ", ";
2757 }
2758 AutomationJSONReply(this, reply_message).SendError(error_string);
[email protected]1ac875d22010-07-16 09:57:582759 }
[email protected]59a611242010-04-02 02:24:042760}
2761
initial.commit09911bf2008-07-26 23:55:292762void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562763 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342764 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2765 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562766 DCHECK(reply_message_ == NULL);
2767 reply_message_ = reply_message;
2768
[email protected]d9f9b792009-06-24 13:17:122769 DevToolsManager::GetInstance()->InspectElement(
2770 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292771 } else {
[email protected]71f65dd2009-02-11 19:14:562772 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2773 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292774 }
2775}
2776
2777void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092778 if (reply_message_) {
2779 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2780 num_resources);
2781 Send(reply_message_);
2782 reply_message_ = NULL;
2783 }
initial.commit09911bf2008-07-26 23:55:292784}
2785
[email protected]a7eee32f2009-05-22 18:08:172786class SetProxyConfigTask : public Task {
2787 public:
[email protected]be180c802009-10-23 06:33:312788 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
2789 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:252790 : request_context_getter_(request_context_getter),
2791 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:172792 virtual void Run() {
2793 // First, deserialize the JSON string. If this fails, log and bail.
2794 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:392795 std::string error_msg;
2796 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:172797 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2798 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:392799 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:172800 return;
2801 }
2802
2803 scoped_ptr<DictionaryValue> dict(
2804 static_cast<DictionaryValue*>(root.release()));
2805 // Now put together a proxy configuration from the deserialized string.
2806 net::ProxyConfig pc;
2807 PopulateProxyConfig(*dict.get(), &pc);
2808
[email protected]be180c802009-10-23 06:33:312809 net::ProxyService* proxy_service =
2810 request_context_getter_->GetURLRequestContext()->proxy_service();
2811 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:172812 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2813 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:312814 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:172815 }
2816
2817 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2818 DCHECK(pc);
2819 bool no_proxy = false;
2820 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2821 // Make no changes to the ProxyConfig.
2822 return;
2823 }
2824 bool auto_config;
2825 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:482826 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:172827 }
2828 std::string pac_url;
2829 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:482830 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:172831 }
2832 std::string proxy_bypass_list;
2833 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:482834 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:172835 }
2836 std::string proxy_server;
2837 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:482838 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:172839 }
2840 }
2841
2842 private:
[email protected]be180c802009-10-23 06:33:312843 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:172844 std::string proxy_config_;
2845};
2846
2847
2848void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:312849 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
2850 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:172851 FilePath user_data_dir;
2852 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2853 ProfileManager* profile_manager = g_browser_process->profile_manager();
2854 DCHECK(profile_manager);
2855 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2856 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:312857 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:172858 }
[email protected]be180c802009-10-23 06:33:312859 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:172860
[email protected]fae20792009-10-28 20:31:582861 ChromeThread::PostTask(
2862 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:312863 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:172864}
2865
[email protected]4f3dc372009-02-24 00:10:292866void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:332867 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:292868 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292869 if (tab_tracker_->ContainsHandle(handle)) {
2870 NavigationController* tab = tab_tracker_->GetResource(handle);
2871 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2872 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:332873 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:292874 }
initial.commit09911bf2008-07-26 23:55:292875}
2876
[email protected]6a5670d22009-10-27 16:21:342877void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:142878 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:532879 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
2880 reply_message);
2881}
2882
2883void AutomationProvider::OpenNewBrowserWindowOfType(
2884 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:142885 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292886 // We may have no current browser windows open so don't rely on
2887 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:532888 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
2889 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:052890 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152891 if (show)
[email protected]15952e462008-11-14 00:29:052892 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292893}
2894
[email protected]71f65dd2009-02-11 19:14:562895void AutomationProvider::GetWindowForBrowser(int browser_handle,
2896 bool* success,
2897 int* handle) {
2898 *success = false;
2899 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292900
2901 if (browser_tracker_->ContainsHandle(browser_handle)) {
2902 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202903 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292904 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202905 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562906 *success = true;
initial.commit09911bf2008-07-26 23:55:292907 }
initial.commit09911bf2008-07-26 23:55:292908}
2909
2910void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562911 int browser_handle,
2912 bool* success,
2913 int* autocomplete_edit_handle) {
2914 *success = false;
2915 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292916
2917 if (browser_tracker_->ContainsHandle(browser_handle)) {
2918 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062919 LocationBar* loc_bar = browser->window()->GetLocationBar();
2920 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292921 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562922 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2923 *success = true;
initial.commit09911bf2008-07-26 23:55:292924 }
initial.commit09911bf2008-07-26 23:55:292925}
initial.commit09911bf2008-07-26 23:55:292926
[email protected]71f65dd2009-02-11 19:14:562927void AutomationProvider::ShowInterstitialPage(int tab_handle,
2928 const std::string& html_text,
2929 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292930 if (tab_tracker_->ContainsHandle(tab_handle)) {
2931 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112932 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402933
[email protected]7dad3d5f2010-03-04 00:27:012934 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:402935 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342936 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402937 GURL("about:interstitial"),
2938 html_text);
2939 interstitial->Show();
2940 return;
initial.commit09911bf2008-07-26 23:55:292941 }
[email protected]71f65dd2009-02-11 19:14:562942
[email protected]457f5cf2009-08-18 16:37:522943 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
2944 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562945 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292946}
2947
[email protected]71f65dd2009-02-11 19:14:562948void AutomationProvider::HideInterstitialPage(int tab_handle,
2949 bool* success) {
2950 *success = false;
[email protected]57c6a652009-05-04 07:58:342951 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2952 if (tab_contents && tab_contents->interstitial_page()) {
2953 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562954 *success = true;
initial.commit09911bf2008-07-26 23:55:292955 }
initial.commit09911bf2008-07-26 23:55:292956}
2957
[email protected]71f65dd2009-02-11 19:14:562958void AutomationProvider::CloseTab(int tab_handle,
2959 bool wait_until_closed,
2960 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292961 if (tab_tracker_->ContainsHandle(tab_handle)) {
2962 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2963 int index;
2964 Browser* browser = Browser::GetBrowserForController(controller, &index);
2965 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142966 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112967 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452968 return;
initial.commit09911bf2008-07-26 23:55:292969 }
[email protected]de246f52009-02-25 18:25:452970
2971 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:552972 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292973}
2974
[email protected]71f65dd2009-02-11 19:14:562975void AutomationProvider::CloseBrowser(int browser_handle,
2976 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292977 if (browser_tracker_->ContainsHandle(browser_handle)) {
2978 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562979 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562980 reply_message);
[email protected]f3e99e32008-07-30 04:48:392981 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292982 } else {
2983 NOTREACHED();
2984 }
2985}
2986
[email protected]71f65dd2009-02-11 19:14:562987void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2988 if (browser_tracker_->ContainsHandle(browser_handle)) {
2989 Browser* browser = browser_tracker_->GetResource(browser_handle);
2990 browser->window()->Close();
2991 } else {
2992 NOTREACHED();
2993 }
2994}
2995
[email protected]71f65dd2009-02-11 19:14:562996void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2997 IPC::Message* reply_message) {
2998 if (tab_tracker_->ContainsHandle(tab_handle)) {
2999 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3000 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:143001 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:563002 }
3003}
3004
[email protected]71f65dd2009-02-11 19:14:563005void AutomationProvider::GetSecurityState(int handle, bool* success,
3006 SecurityStyle* security_style,
3007 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:483008 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:293009 if (tab_tracker_->ContainsHandle(handle)) {
3010 NavigationController* tab = tab_tracker_->GetResource(handle);
3011 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563012 *success = true;
3013 *security_style = entry->ssl().security_style();
3014 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:483015 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:293016 } else {
[email protected]71f65dd2009-02-11 19:14:563017 *success = false;
3018 *security_style = SECURITY_STYLE_UNKNOWN;
3019 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:483020 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:293021 }
3022}
3023
[email protected]71f65dd2009-02-11 19:14:563024void AutomationProvider::GetPageType(int handle, bool* success,
3025 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:293026 if (tab_tracker_->ContainsHandle(handle)) {
3027 NavigationController* tab = tab_tracker_->GetResource(handle);
3028 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563029 *page_type = entry->page_type();
3030 *success = true;
initial.commit09911bf2008-07-26 23:55:293031 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:343032 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:563033 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:343034 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:563035 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293036 } else {
[email protected]71f65dd2009-02-11 19:14:563037 *success = false;
3038 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293039 }
3040}
3041
[email protected]84abba62009-10-07 17:01:443042void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
3043 int* duration_ms) {
3044 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
3045 event_name);
3046}
3047
[email protected]71f65dd2009-02-11 19:14:563048void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
3049 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:293050 if (tab_tracker_->ContainsHandle(handle)) {
3051 NavigationController* tab = tab_tracker_->GetResource(handle);
3052 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:073053 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:403054 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:473055 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:343056 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:293057 if (ssl_blocking_page) {
3058 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:013059 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:563060 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:293061 return;
3062 }
3063 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:523064 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3065 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:563066 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293067 return;
3068 }
3069 }
3070 }
3071 // We failed.
[email protected]457f5cf2009-08-18 16:37:523072 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3073 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563074 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293075}
3076
[email protected]71f65dd2009-02-11 19:14:563077void AutomationProvider::BringBrowserToFront(int browser_handle,
3078 bool* success) {
initial.commit09911bf2008-07-26 23:55:293079 if (browser_tracker_->ContainsHandle(browser_handle)) {
3080 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:063081 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:563082 *success = true;
initial.commit09911bf2008-07-26 23:55:293083 } else {
[email protected]71f65dd2009-02-11 19:14:563084 *success = false;
initial.commit09911bf2008-07-26 23:55:293085 }
3086}
3087
[email protected]71f65dd2009-02-11 19:14:563088void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
3089 int message_num,
3090 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:293091 if (browser_tracker_->ContainsHandle(browser_handle)) {
3092 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563093 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:143094 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:293095 } else {
[email protected]71f65dd2009-02-11 19:14:563096 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:293097 }
3098}
3099
[email protected]71f65dd2009-02-11 19:14:563100void AutomationProvider::PrintNow(int tab_handle,
3101 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:573102 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:343103 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3104 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:293105 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:573106 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:143107 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:343108 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:573109 return;
initial.commit09911bf2008-07-26 23:55:293110 }
[email protected]71f65dd2009-02-11 19:14:563111 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
3112 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293113}
[email protected]d301c952009-07-13 15:02:413114
[email protected]71f65dd2009-02-11 19:14:563115void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:113116 const FilePath& file_name,
3117 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:563118 int type,
3119 bool* success) {
initial.commit09911bf2008-07-26 23:55:293120 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:563121 *success = false;
initial.commit09911bf2008-07-26 23:55:293122 return;
3123 }
3124
3125 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3126 Browser* browser = FindAndActivateTab(nav);
3127 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:143128 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:563129 *success = false;
initial.commit09911bf2008-07-26 23:55:293130 return;
3131 }
3132
initial.commit09911bf2008-07-26 23:55:293133 SavePackage::SavePackageType save_type =
3134 static_cast<SavePackage::SavePackageType>(type);
3135 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
3136 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:343137 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:293138
[email protected]71f65dd2009-02-11 19:14:563139 *success = true;
initial.commit09911bf2008-07-26 23:55:293140}
3141
[email protected]71f65dd2009-02-11 19:14:563142void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
3143 bool* success,
3144 std::wstring* text) {
3145 *success = false;
initial.commit09911bf2008-07-26 23:55:293146 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563147 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:523148 GetText();
[email protected]71f65dd2009-02-11 19:14:563149 *success = true;
initial.commit09911bf2008-07-26 23:55:293150 }
initial.commit09911bf2008-07-26 23:55:293151}
3152
[email protected]71f65dd2009-02-11 19:14:563153void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
3154 const std::wstring& text,
3155 bool* success) {
3156 *success = false;
initial.commit09911bf2008-07-26 23:55:293157 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:523158 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
3159 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:563160 *success = true;
initial.commit09911bf2008-07-26 23:55:293161 }
initial.commit09911bf2008-07-26 23:55:293162}
3163
3164void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:563165 int autocomplete_edit_handle,
3166 bool* success,
3167 std::vector<AutocompleteMatchData>* matches) {
3168 *success = false;
initial.commit09911bf2008-07-26 23:55:293169 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:273170 const AutocompleteResult& result = autocomplete_edit_tracker_->
3171 GetResource(autocomplete_edit_handle)->model()->result();
3172 for (AutocompleteResult::const_iterator i = result.begin();
3173 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:563174 matches->push_back(AutocompleteMatchData(*i));
3175 *success = true;
initial.commit09911bf2008-07-26 23:55:293176 }
initial.commit09911bf2008-07-26 23:55:293177}
3178
3179void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:563180 int autocomplete_edit_handle,
3181 bool* success,
3182 bool* query_in_progress) {
3183 *success = false;
3184 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:293185 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563186 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:523187 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:563188 *success = true;
initial.commit09911bf2008-07-26 23:55:293189 }
initial.commit09911bf2008-07-26 23:55:293190}
3191
[email protected]63514af2010-03-30 17:17:233192#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:193193
[email protected]5ae5bed2009-08-21 18:52:443194#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:393195
[email protected]57c6a652009-05-04 07:58:343196TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:573197 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:573198 if (tab_tracker_->ContainsHandle(handle)) {
3199 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:343200 if (tab)
3201 *tab = nav_controller;
3202 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:573203 }
[email protected]57c6a652009-05-04 07:58:343204 return NULL;
[email protected]20e93d12008-08-28 16:31:573205}
3206
initial.commit09911bf2008-07-26 23:55:293207TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
3208 : AutomationProvider(profile) {
3209 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:143210 registrar_.Add(this, NotificationType::SESSION_END,
3211 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:293212}
3213
3214TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:293215 BrowserList::RemoveObserver(this);
3216}
3217
3218void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:143219 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:293220 AutomationProvider::OnChannelError();
3221}
3222
3223void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
3224 // For backwards compatibility with the testing automation interface, we
3225 // want the automation provider (and hence the process) to go away when the
3226 // last browser goes away.
3227 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:293228 // If you change this, update Observer for NotificationType::SESSION_END
3229 // below.
[email protected]295039bd2008-08-15 04:32:573230 MessageLoop::current()->PostTask(FROM_HERE,
3231 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:293232 }
3233}
3234
3235void TestingAutomationProvider::Observe(NotificationType type,
3236 const NotificationSource& source,
3237 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:563238 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:293239 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
3240 // before the task runs resulting in this object not being deleted. This
3241 // Release balance out the Release scheduled by OnBrowserRemoving.
3242 Release();
3243}
[email protected]295039bd2008-08-15 04:32:573244
3245void TestingAutomationProvider::OnRemoveProvider() {
3246 AutomationProviderList::GetInstance()->RemoveProvider(this);
3247}
[email protected]8a3422c92008-09-24 17:42:423248
[email protected]816633a2009-11-11 21:48:183249void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:563250 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:423251 if (tab_tracker_->ContainsHandle(handle)) {
3252 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:343253 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:113254 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423255 }
[email protected]8a3422c92008-09-24 17:42:423256}
3257
[email protected]816633a2009-11-11 21:48:183258void AutomationProvider::ClickInfoBarAccept(int handle,
3259 int info_bar_index,
3260 bool wait_for_navigation,
3261 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:423262 bool success = false;
3263 if (tab_tracker_->ContainsHandle(handle)) {
3264 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3265 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:113266 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423267 if (info_bar_index >= 0 && info_bar_index < count) {
3268 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:013269 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:423270 }
[email protected]eb9ba192008-12-02 02:41:343271 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:113272 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:343273 info_bar_index);
3274 if (delegate->AsConfirmInfoBarDelegate())
3275 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:423276 success = true;
3277 }
3278 }
[email protected]4f3dc372009-02-24 00:10:293279 }
[email protected]58f622a62009-10-04 01:17:553280
3281 // This "!wait_for_navigation || !success condition" logic looks suspicious.
3282 // It will send a failure message when success is true but
3283 // |wait_for_navigation| is false.
3284 // TODO(phajdan.jr): investgate whether the reply param (currently
3285 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:423286 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:183287 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:523288 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:423289}
3290
[email protected]71f65dd2009-02-11 19:14:563291void AutomationProvider::GetLastNavigationTime(int handle,
3292 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:423293 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:563294 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:423295}
3296
[email protected]71f65dd2009-02-11 19:14:563297void AutomationProvider::WaitForNavigation(int handle,
3298 int64 last_navigation_time,
3299 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:253300 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:423301 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:253302
[email protected]8a3422c92008-09-24 17:42:423303 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:563304 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:523305 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
3306 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:553307 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:293308 return;
[email protected]8a3422c92008-09-24 17:42:423309 }
3310
[email protected]7dad3d5f2010-03-04 00:27:013311 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:423312}
3313
[email protected]71f65dd2009-02-11 19:14:563314void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163315 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563316 int value,
3317 bool* success) {
3318 *success = false;
[email protected]8a3422c92008-09-24 17:42:423319 if (browser_tracker_->ContainsHandle(handle)) {
3320 Browser* browser = browser_tracker_->GetResource(handle);
3321 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563322 *success = true;
[email protected]8a3422c92008-09-24 17:42:423323 }
[email protected]8a3422c92008-09-24 17:42:423324}
[email protected]97fa6ce32008-12-19 01:48:163325
[email protected]71f65dd2009-02-11 19:14:563326void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163327 const std::wstring& name,
[email protected]ddd231e2010-06-29 20:35:193328 const std::string& value,
[email protected]71f65dd2009-02-11 19:14:563329 bool* success) {
3330 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163331 if (browser_tracker_->ContainsHandle(handle)) {
3332 Browser* browser = browser_tracker_->GetResource(handle);
3333 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563334 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163335 }
[email protected]97fa6ce32008-12-19 01:48:163336}
3337
[email protected]71f65dd2009-02-11 19:14:563338void AutomationProvider::GetBooleanPreference(int handle,
3339 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:363340 bool* success,
3341 bool* value) {
[email protected]71f65dd2009-02-11 19:14:563342 *success = false;
3343 *value = false;
[email protected]97fa6ce32008-12-19 01:48:163344 if (browser_tracker_->ContainsHandle(handle)) {
3345 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:563346 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
3347 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163348 }
[email protected]97fa6ce32008-12-19 01:48:163349}
3350
[email protected]71f65dd2009-02-11 19:14:563351void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163352 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563353 bool value,
3354 bool* success) {
3355 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163356 if (browser_tracker_->ContainsHandle(handle)) {
3357 Browser* browser = browser_tracker_->GetResource(handle);
3358 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563359 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163360 }
[email protected]97fa6ce32008-12-19 01:48:163361}
3362
3363// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563364void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:403365 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:163366 if (tab_tracker_->ContainsHandle(tab_handle)) {
3367 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3368 Browser* browser = FindAndActivateTab(nav);
3369 DCHECK(browser);
3370
[email protected]57c6a652009-05-04 07:58:343371 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
3372 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:163373 }
[email protected]97fa6ce32008-12-19 01:48:163374}
3375
[email protected]b8f48d12009-11-09 20:14:363376// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563377void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:403378 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:563379 bool* success) {
3380 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163381 if (tab_tracker_->ContainsHandle(tab_handle)) {
3382 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:193383 if (!nav)
3384 return;
[email protected]97fa6ce32008-12-19 01:48:163385 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:163386
[email protected]2f2afba2010-04-01 01:53:193387 // If the browser has UI, simulate what a user would do.
3388 // Activate the tab and then click the encoding menu.
3389 if (browser &&
3390 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:163391 int selected_encoding_id =
3392 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
3393 if (selected_encoding_id) {
3394 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:563395 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163396 }
[email protected]2f2afba2010-04-01 01:53:193397 } else {
3398 // There is no UI, Chrome probably runs as Chrome-Frame mode.
3399 // Try to get TabContents and call its override_encoding method.
3400 TabContents* contents = nav->tab_contents();
3401 if (!contents)
3402 return;
3403 const std::string selected_encoding =
3404 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
3405 if (selected_encoding.empty())
3406 return;
3407 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:163408 }
3409 }
[email protected]97fa6ce32008-12-19 01:48:163410}
[email protected]5bcdb312009-01-07 21:43:203411
[email protected]4d434a1a2009-02-11 21:06:573412void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:203413 SavePackage::SetShouldPromptUser(should_prompt);
3414}
[email protected]87eab222009-03-13 00:47:453415
[email protected]66ba4932009-06-04 19:22:133416void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
3417 *count = -1; // -1 is the error code
3418 if (tab_tracker_->ContainsHandle(handle)) {
3419 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3420 TabContents* tab_contents = nav_controller->tab_contents();
3421 if (tab_contents) {
3422 BlockedPopupContainer* container =
3423 tab_contents->blocked_popup_container();
3424 if (container) {
3425 *count = static_cast<int>(container->GetBlockedPopupCount());
3426 } else {
3427 // If we don't have a container, we don't have any blocked popups to
3428 // contain!
3429 *count = 0;
3430 }
3431 }
3432 }
3433}
[email protected]f7a68432009-07-29 23:18:193434
3435void AutomationProvider::SelectAll(int tab_handle) {
3436 RenderViewHost* view = GetViewForTab(tab_handle);
3437 if (!view) {
3438 NOTREACHED();
3439 return;
3440 }
3441
3442 view->SelectAll();
3443}
3444
3445void AutomationProvider::Cut(int tab_handle) {
3446 RenderViewHost* view = GetViewForTab(tab_handle);
3447 if (!view) {
3448 NOTREACHED();
3449 return;
3450 }
3451
3452 view->Cut();
3453}
3454
3455void AutomationProvider::Copy(int tab_handle) {
3456 RenderViewHost* view = GetViewForTab(tab_handle);
3457 if (!view) {
3458 NOTREACHED();
3459 return;
3460 }
3461
3462 view->Copy();
3463}
3464
3465void AutomationProvider::Paste(int tab_handle) {
3466 RenderViewHost* view = GetViewForTab(tab_handle);
3467 if (!view) {
3468 NOTREACHED();
3469 return;
3470 }
3471
3472 view->Paste();
3473}
3474
3475void AutomationProvider::ReloadAsync(int tab_handle) {
3476 if (tab_tracker_->ContainsHandle(tab_handle)) {
3477 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3478 if (!tab) {
3479 NOTREACHED();
3480 return;
3481 }
3482
[email protected]106a0812010-03-18 00:15:123483 const bool check_for_repost = true;
3484 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193485 }
3486}
3487
3488void AutomationProvider::StopAsync(int tab_handle) {
3489 RenderViewHost* view = GetViewForTab(tab_handle);
3490 if (!view) {
[email protected]8b2b3312009-09-14 18:38:363491 // We tolerate StopAsync being called even before a view has been created.
3492 // So just log a warning instead of a NOTREACHED().
3493 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:193494 return;
3495 }
3496
3497 view->Stop();
3498}
3499
[email protected]1bb5f892009-10-06 01:44:573500void AutomationProvider::OnSetPageFontSize(int tab_handle,
3501 int font_size) {
3502 AutomationPageFontSize automation_font_size =
3503 static_cast<AutomationPageFontSize>(font_size);
3504
3505 if (automation_font_size < SMALLEST_FONT ||
3506 automation_font_size > LARGEST_FONT) {
3507 DLOG(ERROR) << "Invalid font size specified : "
3508 << font_size;
3509 return;
3510 }
3511
3512 if (tab_tracker_->ContainsHandle(tab_handle)) {
3513 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3514 DCHECK(tab != NULL);
3515 if (tab && tab->tab_contents()) {
3516 DCHECK(tab->tab_contents()->profile() != NULL);
3517 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
3518 prefs::kWebKitDefaultFontSize, font_size);
3519 }
3520 }
3521}
3522
[email protected]bc73b4e52010-03-26 04:16:203523void AutomationProvider::RemoveBrowsingData(int remove_mask) {
3524 BrowsingDataRemover* remover;
3525 remover = new BrowsingDataRemover(profile(),
3526 BrowsingDataRemover::EVERYTHING, // All time periods.
3527 base::Time());
3528 remover->Remove(remove_mask);
3529 // BrowsingDataRemover deletes itself.
3530}
[email protected]1bb5f892009-10-06 01:44:573531
[email protected]2949e90d2009-08-21 15:32:523532void AutomationProvider::WaitForBrowserWindowCountToBecome(
3533 int target_count, IPC::Message* reply_message) {
3534 if (static_cast<int>(BrowserList::size()) == target_count) {
3535 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
3536 reply_message, true);
3537 Send(reply_message);
3538 return;
3539 }
3540
3541 // Set up an observer (it will delete itself).
3542 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
3543}
3544
3545void AutomationProvider::WaitForAppModalDialogToBeShown(
3546 IPC::Message* reply_message) {
3547 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
3548 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
3549 reply_message, true);
3550 Send(reply_message);
3551 return;
3552 }
3553
3554 // Set up an observer (it will delete itself).
3555 new AppModalDialogShownObserver(this, reply_message);
3556}
3557
[email protected]1126a1d32009-08-26 15:39:263558void AutomationProvider::GoBackBlockUntilNavigationsComplete(
3559 int handle, int number_of_navigations, IPC::Message* reply_message) {
3560 if (tab_tracker_->ContainsHandle(handle)) {
3561 NavigationController* tab = tab_tracker_->GetResource(handle);
3562 Browser* browser = FindAndActivateTab(tab);
3563 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:013564 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3565 false);
[email protected]1126a1d32009-08-26 15:39:263566 browser->GoBack(CURRENT_TAB);
3567 return;
3568 }
3569 }
3570
3571 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
3572 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3573 Send(reply_message);
3574}
3575
3576void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
3577 int handle, int number_of_navigations, IPC::Message* reply_message) {
3578 if (tab_tracker_->ContainsHandle(handle)) {
3579 NavigationController* tab = tab_tracker_->GetResource(handle);
3580 Browser* browser = FindAndActivateTab(tab);
3581 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:013582 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3583 false);
[email protected]1126a1d32009-08-26 15:39:263584 browser->GoForward(CURRENT_TAB);
3585 return;
3586 }
3587 }
3588
3589 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
3590 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3591 Send(reply_message);
3592}
3593
[email protected]f7a68432009-07-29 23:18:193594RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
3595 if (tab_tracker_->ContainsHandle(tab_handle)) {
3596 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3597 if (!tab) {
3598 NOTREACHED();
3599 return NULL;
3600 }
3601
3602 TabContents* tab_contents = tab->tab_contents();
3603 if (!tab_contents) {
3604 NOTREACHED();
3605 return NULL;
3606 }
3607
3608 RenderViewHost* view_host = tab_contents->render_view_host();
3609 return view_host;
3610 }
3611
3612 return NULL;
3613}
[email protected]675595f2009-08-26 22:32:043614
3615void AutomationProvider::GetBrowserForWindow(int window_handle,
3616 bool* success,
3617 int* browser_handle) {
3618 *success = false;
3619 *browser_handle = 0;
3620
3621 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
3622 if (!window)
3623 return;
3624
3625 BrowserList::const_iterator iter = BrowserList::begin();
3626 for (;iter != BrowserList::end(); ++iter) {
3627 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
3628 if (window == this_window) {
3629 // Add() returns the existing handle for the resource if any.
3630 *browser_handle = browser_tracker_->Add(*iter);
3631 *success = true;
3632 return;
3633 }
3634 }
3635}
[email protected]d11c8e92009-10-20 23:26:403636
3637void AutomationProvider::InstallExtension(const FilePath& crx_path,
3638 IPC::Message* reply_message) {
3639 ExtensionsService* service = profile_->GetExtensionsService();
3640 if (service) {
3641 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193642 new ExtensionInstallNotificationObserver(this,
3643 AutomationMsg_InstallExtension::ID,
3644 reply_message);
[email protected]d11c8e92009-10-20 23:26:403645
3646 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:163647 scoped_refptr<CrxInstaller> installer(
3648 new CrxInstaller(install_dir,
3649 service,
3650 NULL)); // silent install, no UI
3651 installer->set_allow_privilege_increase(true);
3652 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:403653 } else {
3654 AutomationMsg_InstallExtension::WriteReplyParams(
3655 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3656 Send(reply_message);
3657 }
3658}
3659
3660void AutomationProvider::LoadExpandedExtension(
3661 const FilePath& extension_dir,
3662 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:383663 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:403664 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193665 new ExtensionInstallNotificationObserver(
3666 this,
3667 AutomationMsg_LoadExpandedExtension::ID,
3668 reply_message);
[email protected]d11c8e92009-10-20 23:26:403669
3670 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:403671 } else {
3672 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
3673 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3674 Send(reply_message);
3675 }
3676}
[email protected]673fd2c02010-02-04 23:10:003677
[email protected]a1e62d12010-03-16 02:18:433678void AutomationProvider::GetEnabledExtensions(
3679 std::vector<FilePath>* result) {
3680 ExtensionsService* service = profile_->GetExtensionsService();
3681 DCHECK(service);
3682 if (service->extensions_enabled()) {
3683 const ExtensionList* extensions = service->extensions();
3684 DCHECK(extensions);
3685 for (size_t i = 0; i < extensions->size(); ++i) {
3686 Extension* extension = (*extensions)[i];
3687 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:123688 if (extension->location() == Extension::INTERNAL ||
3689 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:073690 result->push_back(extension->path());
3691 }
[email protected]a1e62d12010-03-16 02:18:433692 }
3693 }
3694}
3695
[email protected]790788ac2010-04-06 17:52:193696void AutomationProvider::WaitForExtensionTestResult(
3697 IPC::Message* reply_message) {
3698 DCHECK(reply_message_ == NULL);
3699 reply_message_ = reply_message;
3700 // Call MaybeSendResult, because the result might have come in before
3701 // we were waiting on it.
3702 extension_test_result_observer_->MaybeSendResult();
3703}
3704
3705void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:093706 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:193707 ExtensionsService* service = profile_->GetExtensionsService();
3708 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3709 if (service && manager) {
3710 // The observer will delete itself when done.
3711 new ExtensionReadyNotificationObserver(
3712 manager,
3713 this,
3714 AutomationMsg_InstallExtensionAndGetHandle::ID,
3715 reply_message);
3716
[email protected]d7e5525d2010-04-20 14:37:093717 ExtensionInstallUI* client =
3718 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:193719 scoped_refptr<CrxInstaller> installer(
3720 new CrxInstaller(service->install_directory(),
3721 service,
[email protected]d7e5525d2010-04-20 14:37:093722 client));
[email protected]790788ac2010-04-06 17:52:193723 installer->set_allow_privilege_increase(true);
3724 installer->InstallCrx(crx_path);
3725 } else {
3726 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
3727 reply_message, 0);
3728 Send(reply_message);
3729 }
3730}
3731
3732void AutomationProvider::UninstallExtension(int extension_handle,
3733 bool* success) {
3734 *success = false;
3735 Extension* extension = GetExtension(extension_handle);
3736 ExtensionsService* service = profile_->GetExtensionsService();
3737 if (extension && service) {
3738 ExtensionUnloadNotificationObserver observer;
3739 service->UninstallExtension(extension->id(), false);
3740 // The extension unload notification should have been sent synchronously
3741 // with the uninstall. Just to be safe, check that it was received.
3742 *success = observer.did_receive_unload_notification();
3743 }
3744}
3745
3746void AutomationProvider::EnableExtension(int extension_handle,
3747 IPC::Message* reply_message) {
3748 Extension* extension = GetDisabledExtension(extension_handle);
3749 ExtensionsService* service = profile_->GetExtensionsService();
3750 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3751 // Only enable if this extension is disabled.
3752 if (extension && service && manager) {
3753 // The observer will delete itself when done.
3754 new ExtensionReadyNotificationObserver(
3755 manager,
3756 this,
3757 AutomationMsg_EnableExtension::ID,
3758 reply_message);
3759 service->EnableExtension(extension->id());
3760 } else {
3761 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
3762 Send(reply_message);
3763 }
3764}
3765
3766void AutomationProvider::DisableExtension(int extension_handle,
3767 bool* success) {
3768 *success = false;
3769 Extension* extension = GetEnabledExtension(extension_handle);
3770 ExtensionsService* service = profile_->GetExtensionsService();
3771 if (extension && service) {
3772 ExtensionUnloadNotificationObserver observer;
3773 service->DisableExtension(extension->id());
3774 // The extension unload notification should have been sent synchronously
3775 // with the disable. Just to be safe, check that it was received.
3776 *success = observer.did_receive_unload_notification();
3777 }
3778}
3779
3780void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
3781 int extension_handle, int browser_handle,
3782 IPC::Message* reply_message) {
3783 bool success = false;
3784 Extension* extension = GetEnabledExtension(extension_handle);
3785 ExtensionsService* service = profile_->GetExtensionsService();
3786 ExtensionMessageService* message_service =
3787 profile_->GetExtensionMessageService();
3788 Browser* browser = browser_tracker_->GetResource(browser_handle);
3789 if (extension && service && message_service && browser) {
3790 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
3791 if (extension->page_action()) {
3792 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
3793 browser->profile(), extension->id(), "action", tab_id, "", 1);
3794 success = true;
3795 } else if (extension->browser_action()) {
3796 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
3797 browser->profile(), extension->id(), browser);
3798 success = true;
3799 }
3800 }
3801 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3802 reply_message, success);
3803 Send(reply_message);
3804}
3805
3806void AutomationProvider::MoveExtensionBrowserAction(
3807 int extension_handle, int index, bool* success) {
3808 *success = false;
3809 Extension* extension = GetEnabledExtension(extension_handle);
3810 ExtensionsService* service = profile_->GetExtensionsService();
3811 if (extension && service) {
3812 ExtensionToolbarModel* toolbar = service->toolbar_model();
3813 if (toolbar) {
3814 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3815 toolbar->MoveBrowserAction(extension, index);
3816 *success = true;
3817 } else {
3818 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3819 }
3820 }
3821 }
3822}
3823
3824void AutomationProvider::GetExtensionProperty(
3825 int extension_handle,
3826 AutomationMsg_ExtensionProperty type,
3827 bool* success,
3828 std::string* value) {
3829 *success = false;
3830 Extension* extension = GetExtension(extension_handle);
3831 ExtensionsService* service = profile_->GetExtensionsService();
3832 if (extension && service) {
3833 ExtensionToolbarModel* toolbar = service->toolbar_model();
3834 int found_index = -1;
3835 int index = 0;
3836 switch (type) {
3837 case AUTOMATION_MSG_EXTENSION_ID:
3838 *value = extension->id();
3839 *success = true;
3840 break;
3841 case AUTOMATION_MSG_EXTENSION_NAME:
3842 *value = extension->name();
3843 *success = true;
3844 break;
3845 case AUTOMATION_MSG_EXTENSION_VERSION:
3846 *value = extension->VersionString();
3847 *success = true;
3848 break;
3849 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3850 if (toolbar) {
3851 for (ExtensionList::const_iterator iter = toolbar->begin();
3852 iter != toolbar->end(); iter++) {
3853 // Skip this extension if we are in incognito mode
3854 // and it is not incognito-enabled.
3855 if (profile_->IsOffTheRecord() &&
3856 !service->IsIncognitoEnabled(*iter))
3857 continue;
3858 if (*iter == extension) {
3859 found_index = index;
3860 break;
3861 }
3862 index++;
3863 }
3864 *value = IntToString(found_index);
3865 *success = true;
3866 }
3867 break;
3868 default:
3869 LOG(WARNING) << "Trying to get undefined extension property";
3870 break;
3871 }
3872 }
3873}
3874
[email protected]673fd2c02010-02-04 23:10:003875void AutomationProvider::SaveAsAsync(int tab_handle) {
3876 NavigationController* tab = NULL;
3877 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3878 if (tab_contents)
3879 tab_contents->OnSavePage();
3880}
[email protected]7dad3d5f2010-03-04 00:27:013881
3882void AutomationProvider::SetContentSetting(
3883 int handle,
3884 const std::string& host,
3885 ContentSettingsType content_type,
3886 ContentSetting setting,
3887 bool* success) {
3888 *success = false;
3889 if (browser_tracker_->ContainsHandle(handle)) {
3890 Browser* browser = browser_tracker_->GetResource(handle);
3891 HostContentSettingsMap* map =
3892 browser->profile()->GetHostContentSettingsMap();
3893 if (host.empty()) {
3894 map->SetDefaultContentSetting(content_type, setting);
3895 } else {
[email protected]0314ae02010-04-08 09:18:293896 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3897 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013898 }
3899 *success = true;
3900 }
3901}
[email protected]cc824372010-03-31 15:33:013902
3903#if !defined(TOOLKIT_VIEWS)
3904void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3905 NOTIMPLEMENTED();
3906};
3907
3908void AutomationProvider::WaitForFocusedViewIDToChange(
3909 int handle, int previous_view_id, IPC::Message* reply_message) {
3910 NOTIMPLEMENTED();
3911}
3912
3913void AutomationProvider::StartTrackingPopupMenus(
3914 int browser_handle, bool* success) {
3915 NOTIMPLEMENTED();
3916}
3917
3918void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3919 NOTIMPLEMENTED();
3920}
3921#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:093922
3923void AutomationProvider::ResetToDefaultTheme() {
3924 profile_->ClearTheme();
3925}