blob: e708147b3251d3ac1c73de22587885a043c2a4e1 [file] [log] [blame]
[email protected]3b5f7022010-03-25 20:37:401// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/automation/automation_provider.h"
6
[email protected]5ae5bed2009-08-21 18:52:447#include <set>
8
[email protected]202e7a72009-06-15 03:48:369#include "app/l10n_util.h"
[email protected]37126212009-05-06 02:23:3110#include "app/message_box_flags.h"
[email protected]2041cf342010-02-19 03:15:5911#include "base/callback.h"
[email protected]7060bb292010-06-24 00:52:4912#include "base/file_path.h"
[email protected]c6cb1992009-04-13 16:45:2913#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2014#include "base/json/json_reader.h"
[email protected]59a611242010-04-02 02:24:0415#include "base/json/json_writer.h"
[email protected]93364da2010-06-29 18:03:4416#include "base/json/string_escape.h"
[email protected]bc1407f2009-09-29 00:33:3517#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3818#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4620#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5021#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2722#include "base/string_util.h"
[email protected]9eaa18e2010-06-29 20:51:0123#include "base/task.h"
[email protected]5fac9622009-02-04 21:49:3824#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5325#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1726#include "base/values.h"
[email protected]9eaa18e2010-06-29 20:51:0127#include "base/waitable_event.h"
[email protected]4f3dc372009-02-24 00:10:2928#include "chrome/app/chrome_dll_resource.h"
[email protected]bcff05a2010-04-14 01:46:4329#include "chrome/app/chrome_version_info.h"
[email protected]0bfa713f2009-04-07 20:18:2830#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0931#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]55846ad842010-07-09 18:22:5632#include "chrome/browser/autofill/autofill_manager.h"
[email protected]790788ac2010-04-06 17:52:1933#include "chrome/browser/automation/automation_extension_tracker.h"
initial.commit09911bf2008-07-26 23:55:2934#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0835#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]f44265b2009-05-19 18:52:5036#include "chrome/browser/automation/extension_port_container.h"
[email protected]12802702010-07-09 19:43:0937#include "chrome/browser/autocomplete/autocomplete_edit.h"
[email protected]66ba4932009-06-04 19:22:1338#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5339#include "chrome/browser/bookmarks/bookmark_model.h"
40#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]ef413ca2010-05-25 21:09:1441#include "chrome/browser/browser_list.h"
[email protected]5c238752009-06-13 10:29:0742#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3943#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2044#include "chrome/browser/browsing_data_remover.h"
[email protected]f83f9102010-05-04 17:01:0545#include "chrome/browser/character_encoding.h"
[email protected]fae20792009-10-28 20:31:5846#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2947#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1248#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5949#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2250#include "chrome/browser/download/download_shelf.h"
[email protected]f83f9102010-05-04 17:01:0551#include "chrome/browser/download/save_package.h"
[email protected]d11c8e92009-10-20 23:26:4052#include "chrome/browser/extensions/crx_installer.h"
[email protected]790788ac2010-04-06 17:52:1953#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]ef413ca2010-05-25 21:09:1454#include "chrome/browser/extensions/extension_host.h"
[email protected]d11c8e92009-10-20 23:26:4055#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5556#include "chrome/browser/extensions/extension_message_service.h"
[email protected]790788ac2010-04-06 17:52:1957#include "chrome/browser/extensions/extension_tabs_module.h"
58#include "chrome/browser/extensions/extension_toolbar_model.h"
59#include "chrome/browser/extensions/extensions_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1660#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5861#include "chrome/browser/find_bar.h"
62#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2963#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0164#include "chrome/browser/host_content_settings_map.h"
[email protected]0ac83682010-01-22 17:46:2765#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0666#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0167#include "chrome/browser/login_prompt.h"
[email protected]f732c1e2009-07-30 15:48:5368#include "chrome/browser/net/url_request_mock_util.h"
[email protected]14a000d2010-04-29 21:44:2469#include "chrome/browser/platform_util.h"
[email protected]052313b2010-02-19 09:43:0870#include "chrome/browser/pref_service.h"
[email protected]f83f9102010-05-04 17:01:0571#include "chrome/browser/printing/print_job.h"
[email protected]a7eee32f2009-05-22 18:08:1772#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0773#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2574#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0375#include "chrome/browser/ssl/ssl_manager.h"
76#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3477#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4578#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1779#include "chrome/common/automation_constants.h"
[email protected]a9ff2c02010-05-13 17:33:0580#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2981#include "chrome/common/chrome_paths.h"
[email protected]790788ac2010-04-06 17:52:1982#include "chrome/common/extensions/extension.h"
[email protected]a7eee32f2009-05-22 18:08:1783#include "chrome/common/json_value_serializer.h"
[email protected]68d2a05f2010-05-07 21:39:5584#include "chrome/common/net/url_request_context_getter.h"
[email protected]1c58a5c2009-05-21 18:47:1485#include "chrome/common/notification_service.h"
[email protected]1bb5f892009-10-06 01:44:5786#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2587#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5688#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5789#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1790#include "net/proxy/proxy_service.h"
91#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2192#include "net/url_request/url_request_context.h"
[email protected]1b5a48c2010-04-29 23:08:3093#include "chrome/browser/automation/ui_controls.h"
[email protected]9a08bcf2009-08-12 19:56:2894#include "views/event.h"
[email protected]f7d48012010-05-06 08:17:0595#include "webkit/glue/plugins/plugin_list.h"
initial.commit09911bf2008-07-26 23:55:2996
[email protected]de246f52009-02-25 18:25:4597#if defined(OS_WIN)
[email protected]4bdde602010-06-16 03:17:3598#include "chrome/browser/external_tab_container_win.h"
[email protected]de246f52009-02-25 18:25:4599#endif // defined(OS_WIN)
100
[email protected]e1acf6f2008-10-27 20:43:33101using base::Time;
102
[email protected]cbab76d2008-10-13 22:42:47103class AutomationInterstitialPage : public InterstitialPage {
104 public:
[email protected]57c6a652009-05-04 07:58:34105 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47106 const GURL& url,
107 const std::string& contents)
108 : InterstitialPage(tab, true, url),
109 contents_(contents) {
110 }
111
112 virtual std::string GetHTMLContents() { return contents_; }
113
114 private:
115 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29116
[email protected]cbab76d2008-10-13 22:42:47117 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
118};
119
[email protected]c2cb8542009-08-20 21:16:51120class ClickTask : public Task {
121 public:
[email protected]fc2e0872009-08-21 22:14:41122 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51123 virtual ~ClickTask() {}
124
125 virtual void Run() {
126 ui_controls::MouseButton button = ui_controls::LEFT;
127 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
128 views::Event::EF_LEFT_BUTTON_DOWN) {
129 button = ui_controls::LEFT;
130 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
131 views::Event::EF_RIGHT_BUTTON_DOWN) {
132 button = ui_controls::RIGHT;
133 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
134 views::Event::EF_MIDDLE_BUTTON_DOWN) {
135 button = ui_controls::MIDDLE;
136 } else {
137 NOTREACHED();
138 }
139
[email protected]fc2e0872009-08-21 22:14:41140 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51141 }
142
143 private:
[email protected]c2cb8542009-08-20 21:16:51144 int flags_;
145
146 DISALLOW_COPY_AND_ASSIGN(ClickTask);
147};
[email protected]c2cb8542009-08-20 21:16:51148
initial.commit09911bf2008-07-26 23:55:29149AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57150 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56151 profile_(profile),
[email protected]cc824372010-03-31 15:33:01152 reply_message_(NULL),
153 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29154 browser_tracker_.reset(new AutomationBrowserTracker(this));
[email protected]790788ac2010-04-06 17:52:19155 extension_tracker_.reset(new AutomationExtensionTracker(this));
initial.commit09911bf2008-07-26 23:55:29156 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20157 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29158 autocomplete_edit_tracker_.reset(
159 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29160 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
161 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44162 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]790788ac2010-04-06 17:52:19163 extension_test_result_observer_.reset(
164 new ExtensionTestResultNotificationObserver(this));
[email protected]528211a2010-01-14 15:25:13165 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29166}
167
168AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50169 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
170 port_containers_.end());
171 port_containers_.clear();
172
[email protected]0da050b92008-08-19 19:29:47173 // Make sure that any outstanding NotificationObservers also get destroyed.
174 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31175 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47176 while ((observer = it.GetNext()) != NULL)
177 delete observer;
[email protected]528211a2010-01-14 15:25:13178
179 if (channel_.get()) {
180 channel_->Close();
181 }
182 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29183}
184
[email protected]9a3a293b2009-06-04 22:28:16185void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06186 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57187 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06188 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
189 automation_resource_message_filter_,
190 g_browser_process->io_thread()->message_loop(),
191 true, g_browser_process->shutdown_event()));
[email protected]bcff05a2010-04-14 01:46:43192 scoped_ptr<FileVersionInfo> version_info(
193 chrome_app::GetChromeVersionInfo());
[email protected]cf620752009-04-24 17:05:40194 std::string version_string;
[email protected]bcff05a2010-04-14 01:46:43195 if (version_info != NULL) {
196 version_string = WideToASCII(version_info->file_version());
[email protected]cf620752009-04-24 17:05:40197 }
[email protected]c6cb1992009-04-13 16:45:29198
199 // Send a hello message with our current automation protocol version.
200 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29201}
202
203void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
204 if (expected_tabs == 0) {
205 Send(new AutomationMsg_InitialLoadsComplete(0));
206 } else {
207 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
208 }
209}
210
211NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58212 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01213 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29214 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58215 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01216 number_of_navigations,
217 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29218
[email protected]71f65dd2009-02-11 19:14:56219 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29220 return observer;
221}
222
[email protected]faf2ee42010-05-11 14:26:17223void AutomationProvider::RemoveNavigationStatusListener(
224 NotificationObserver* obs) {
225 notification_observer_list_.RemoveObserver(obs);
226}
227
initial.commit09911bf2008-07-26 23:55:29228NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14229 Browser* parent,
230 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56231 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14232 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29233 notification_observer_list_.AddObserver(observer);
234
235 return observer;
236}
237
[email protected]faf2ee42010-05-11 14:26:17238void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
initial.commit09911bf2008-07-26 23:55:29239 notification_observer_list_.RemoveObserver(obs);
240}
241
242void AutomationProvider::AddLoginHandler(NavigationController* tab,
243 LoginHandler* handler) {
244 login_handler_map_[tab] = handler;
245}
246
247void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
248 DCHECK(login_handler_map_[tab]);
249 login_handler_map_.erase(tab);
250}
251
[email protected]f44265b2009-05-19 18:52:50252void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
253 int port_id = port->port_id();
254 DCHECK_NE(-1, port_id);
255 DCHECK(port_containers_.find(port_id) == port_containers_.end());
256
257 port_containers_[port_id] = port;
258}
259
260void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
261 int port_id = port->port_id();
262 DCHECK_NE(-1, port_id);
263
264 PortContainerMap::iterator it = port_containers_.find(port_id);
265 DCHECK(it != port_containers_.end());
266
267 if (it != port_containers_.end()) {
268 delete it->second;
269 port_containers_.erase(it);
270 }
271}
272
273ExtensionPortContainer* AutomationProvider::GetPortContainer(
274 int port_id) const {
275 PortContainerMap::const_iterator it = port_containers_.find(port_id);
276 if (it == port_containers_.end())
277 return NULL;
278
279 return it->second;
280}
281
initial.commit09911bf2008-07-26 23:55:29282int AutomationProvider::GetIndexForNavigationController(
283 const NavigationController* controller, const Browser* parent) const {
284 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12285 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29286}
287
[email protected]790788ac2010-04-06 17:52:19288int AutomationProvider::AddExtension(Extension* extension) {
289 DCHECK(extension);
290 return extension_tracker_->Add(extension);
291}
292
293Extension* AutomationProvider::GetExtension(int extension_handle) {
294 return extension_tracker_->GetResource(extension_handle);
295}
296
297Extension* AutomationProvider::GetEnabledExtension(int extension_handle) {
298 Extension* extension = extension_tracker_->GetResource(extension_handle);
299 ExtensionsService* service = profile_->GetExtensionsService();
300 if (extension && service &&
301 service->GetExtensionById(extension->id(), false))
302 return extension;
303 return NULL;
304}
305
306Extension* AutomationProvider::GetDisabledExtension(int extension_handle) {
307 Extension* extension = extension_tracker_->GetResource(extension_handle);
308 ExtensionsService* service = profile_->GetExtensionsService();
309 if (extension && service &&
310 service->GetExtensionById(extension->id(), true) &&
311 !service->GetExtensionById(extension->id(), false))
312 return extension;
313 return NULL;
314}
315
initial.commit09911bf2008-07-26 23:55:29316void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
317 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14318 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56319 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
320 CloseBrowserAsync)
321 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
322 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
323 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
324 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
325 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
326 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]5fa57942010-04-21 23:07:22327 IPC_MESSAGE_HANDLER(AutomationMsg_DeleteCookie, DeleteCookie)
[email protected]1c58a5c2009-05-21 18:47:14328 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58329 IPC_MESSAGE_HANDLER_DELAY_REPLY(
330 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
331 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56332 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
[email protected]c70f9b82010-04-21 07:31:11333 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncWithDisposition,
334 NavigationAsyncWithDisposition)
[email protected]71f65dd2009-02-11 19:14:56335 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
336 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
337 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
338 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14339 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56340 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
341 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
342 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14343 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29344 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
345 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56346 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36347 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56348 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29349 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56350 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29351 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
352 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56353 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14354 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24355 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44356 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33357 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44358 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33359 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24360 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14361 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24362 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15363 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14364 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]d1a5941e2009-08-13 23:34:24365 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35366 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14367 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]1b5a48c2010-04-29 23:08:30368#if !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56369 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
370 WindowSimulateDrag)
[email protected]1b5a48c2010-04-29 23:08:30371#endif // !defined(OS_MACOSX)
[email protected]71f65dd2009-02-11 19:14:56372 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53373 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56374 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37375#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56376 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45377#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56378 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
379 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03380 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56381 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14382 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09383 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
384 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
385 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29386 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14387 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56388 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
389 ExecuteJavascript)
390 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29391 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14392 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
393 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56394 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
395 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14396 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17397 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14398 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14399 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53400 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
401 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14402 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56403 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14404 GetAutocompleteEditForBrowser)
405 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]71f65dd2009-02-11 19:14:56406 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14407 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56408 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14409 HideInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56410 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
411 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14412 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
413 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44414 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
415 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56416 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
417 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29418 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
419 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
420 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56421 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41422 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56423 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
424 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29425 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56426 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29427 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56428 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29429 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56430 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29431 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56432 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45433 HandleOpenFindInPageRequest)
[email protected]1c58a5c2009-05-21 18:47:14434 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56435 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57436 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56437 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57438 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56439 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
440 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53441 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
442 GetBookmarksAsJSON)
443 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
444 WaitForBookmarkModelToLoad)
445 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
446 AddBookmarkGroup)
447 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
448 AddBookmarkURL)
449 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
450 ReparentBookmark)
451 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
452 SetBookmarkTitle)
453 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
454 SetBookmarkURL)
455 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
456 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04457 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
458 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18459 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
460 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
461 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56462 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42463 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56464 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
465 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14466 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56467 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44468 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56469 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20470 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14471 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56472 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16473 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56474 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16475 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56476 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16477 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14478 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20479 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
480 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14481 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]59560e0b2009-06-04 03:30:22482 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13483 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19484 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
485 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
486 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
487 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
488 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
489 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52490 IPC_MESSAGE_HANDLER_DELAY_REPLY(
491 AutomationMsg_WaitForBrowserWindowCountToBecome,
492 WaitForBrowserWindowCountToBecome)
493 IPC_MESSAGE_HANDLER_DELAY_REPLY(
494 AutomationMsg_WaitForAppModalDialogToBeShown,
495 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26496 IPC_MESSAGE_HANDLER_DELAY_REPLY(
497 AutomationMsg_GoBackBlockUntilNavigationsComplete,
498 GoBackBlockUntilNavigationsComplete)
499 IPC_MESSAGE_HANDLER_DELAY_REPLY(
500 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
501 GoForwardBlockUntilNavigationsComplete)
[email protected]1bb5f892009-10-06 01:44:57502 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40503 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
504 InstallExtension)
505 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
506 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43507 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
508 GetEnabledExtensions)
[email protected]790788ac2010-04-06 17:52:19509 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult,
510 WaitForExtensionTestResult)
511 IPC_MESSAGE_HANDLER_DELAY_REPLY(
512 AutomationMsg_InstallExtensionAndGetHandle,
513 InstallExtensionAndGetHandle)
514 IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension,
515 UninstallExtension)
516 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension,
517 EnableExtension)
518 IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension,
519 DisableExtension)
520 IPC_MESSAGE_HANDLER_DELAY_REPLY(
521 AutomationMsg_ExecuteExtensionActionInActiveTabAsync,
522 ExecuteExtensionActionInActiveTabAsync)
523 IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction,
524 MoveExtensionBrowserAction)
525 IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty,
526 GetExtensionProperty)
[email protected]fedaa7d2010-01-26 20:34:57527 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
528 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00529 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]7dad3d5f2010-03-04 00:27:01530 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]bc73b4e52010-03-26 04:16:20531 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]bdd5a9c92010-06-14 18:21:00532 IPC_MESSAGE_HANDLER(AutomationMsg_ResetToDefaultTheme, ResetToDefaultTheme)
[email protected]cc824372010-03-31 15:33:01533#if defined(TOOLKIT_VIEWS)
534 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
535 WaitForFocusedViewIDToChange)
536 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
537 StartTrackingPopupMenus)
538 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
539 WaitForPopupMenuToOpen)
[email protected]bdd5a9c92010-06-14 18:21:00540#endif // defined(TOOLKIT_VIEWS)
[email protected]52415f842010-06-10 21:51:52541#if defined(OS_WIN)
542 // These are for use with external tabs.
543 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
544 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
545 ProcessUnhandledAccelerator)
546 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
547 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
548 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
549 OnForwardContextMenuCommandToChrome)
550 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
551 NavigateInExternalTab)
552 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
553 NavigateExternalTabAtIndex)
554 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
555 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
556 SetEnableExtensionAutomation)
557 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
558 OnMessageFromExternalHost)
[email protected]bdd5a9c92010-06-14 18:21:00559 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
560#endif // defined(OS_WIN)
561#if defined(OS_CHROMEOS)
562 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
563 LoginWithUserAndPass)
564#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29565 IPC_END_MESSAGE_MAP()
566}
567
[email protected]71f65dd2009-02-11 19:14:56568void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
569 *status = -1;
initial.commit09911bf2008-07-26 23:55:29570 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
571 Browser* browser = browser_tracker_->GetResource(handle);
572 if (at_index >= 0 && at_index < browser->tab_count()) {
573 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56574 *status = 0;
initial.commit09911bf2008-07-26 23:55:29575 }
576 }
initial.commit09911bf2008-07-26 23:55:29577}
578
[email protected]71f65dd2009-02-11 19:14:56579void AutomationProvider::AppendTab(int handle, const GURL& url,
580 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29581 int append_tab_response = -1; // -1 is the error code
582 NotificationObserver* observer = NULL;
583
584 if (browser_tracker_->ContainsHandle(handle)) {
585 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14586 observer = AddTabStripObserver(browser, reply_message);
[email protected]715af7e2010-04-29 01:55:38587 TabContents* tab_contents = browser->AddTabWithURL(
[email protected]4a1665442010-06-28 16:09:39588 url, GURL(), PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED,
589 NULL, std::string());
initial.commit09911bf2008-07-26 23:55:29590 if (tab_contents) {
591 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57592 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29593 }
594 }
595
596 if (append_tab_response < 0) {
597 // The append tab failed. Remove the TabStripObserver
598 if (observer) {
[email protected]faf2ee42010-05-11 14:26:17599 RemoveTabStripObserver(observer);
initial.commit09911bf2008-07-26 23:55:29600 delete observer;
601 }
602
[email protected]71f65dd2009-02-11 19:14:56603 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
604 append_tab_response);
605 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29606 }
607}
608
[email protected]71f65dd2009-02-11 19:14:56609void AutomationProvider::NavigateToURL(int handle, const GURL& url,
610 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58611 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
612}
613
614void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
615 int handle, const GURL& url, int number_of_navigations,
616 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29617 if (tab_tracker_->ContainsHandle(handle)) {
618 NavigationController* tab = tab_tracker_->GetResource(handle);
619
620 // Simulate what a user would do. Activate the tab and then navigate.
621 // We could allow navigating in a background tab in future.
622 Browser* browser = FindAndActivateTab(tab);
623
624 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01625 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
626 false);
[email protected]71f65dd2009-02-11 19:14:56627
initial.commit09911bf2008-07-26 23:55:29628 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50629 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29630 return;
631 }
632 }
[email protected]71f65dd2009-02-11 19:14:56633
634 AutomationMsg_NavigateToURL::WriteReplyParams(
635 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
636 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29637}
[email protected]2949e90d2009-08-21 15:32:52638
[email protected]c70f9b82010-04-21 07:31:11639void AutomationProvider::NavigationAsync(int handle,
640 const GURL& url,
641 bool* status) {
642 NavigationAsyncWithDisposition(handle, url, CURRENT_TAB, status);
643}
644
645void AutomationProvider::NavigationAsyncWithDisposition(
646 int handle,
647 const GURL& url,
648 WindowOpenDisposition disposition,
649 bool* status) {
[email protected]71f65dd2009-02-11 19:14:56650 *status = false;
initial.commit09911bf2008-07-26 23:55:29651
652 if (tab_tracker_->ContainsHandle(handle)) {
653 NavigationController* tab = tab_tracker_->GetResource(handle);
654
655 // Simulate what a user would do. Activate the tab and then navigate.
656 // We could allow navigating in a background tab in future.
657 Browser* browser = FindAndActivateTab(tab);
658
659 if (browser) {
660 // Don't add any listener unless a callback mechanism is desired.
661 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c70f9b82010-04-21 07:31:11662 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56663 *status = true;
initial.commit09911bf2008-07-26 23:55:29664 }
665 }
initial.commit09911bf2008-07-26 23:55:29666}
667
[email protected]71f65dd2009-02-11 19:14:56668void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29669 if (tab_tracker_->ContainsHandle(handle)) {
670 NavigationController* tab = tab_tracker_->GetResource(handle);
671 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14672 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01673 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29674 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29675 return;
676 }
677 }
[email protected]71f65dd2009-02-11 19:14:56678
679 AutomationMsg_GoBack::WriteReplyParams(
680 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
681 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29682}
683
[email protected]71f65dd2009-02-11 19:14:56684void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29685 if (tab_tracker_->ContainsHandle(handle)) {
686 NavigationController* tab = tab_tracker_->GetResource(handle);
687 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14688 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01689 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29690 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29691 return;
692 }
693 }
[email protected]71f65dd2009-02-11 19:14:56694
695 AutomationMsg_GoForward::WriteReplyParams(
696 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
697 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29698}
699
[email protected]71f65dd2009-02-11 19:14:56700void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29701 if (tab_tracker_->ContainsHandle(handle)) {
702 NavigationController* tab = tab_tracker_->GetResource(handle);
703 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14704 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01705 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]cb84d642010-06-10 00:56:28706 browser->Reload(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29707 return;
708 }
709 }
[email protected]71f65dd2009-02-11 19:14:56710
711 AutomationMsg_Reload::WriteReplyParams(
712 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
713 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29714}
715
[email protected]71f65dd2009-02-11 19:14:56716void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29717 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56718 const std::wstring& password,
719 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29720 if (tab_tracker_->ContainsHandle(tab_handle)) {
721 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
722 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
723
724 if (iter != login_handler_map_.end()) {
725 // If auth is needed again after this, assume login has failed. This is
726 // not strictly correct, because a navigation can require both proxy and
727 // server auth, but it should be OK for now.
728 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01729 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29730 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52731 return;
initial.commit09911bf2008-07-26 23:55:29732 }
733 }
[email protected]de246f52009-02-25 18:25:45734
[email protected]457f5cf2009-08-18 16:37:52735 AutomationMsg_SetAuth::WriteReplyParams(
736 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
737 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29738}
739
[email protected]71f65dd2009-02-11 19:14:56740void AutomationProvider::CancelAuth(int tab_handle,
741 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29742 if (tab_tracker_->ContainsHandle(tab_handle)) {
743 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
744 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
745
746 if (iter != login_handler_map_.end()) {
747 // If auth is needed again after this, something is screwy.
748 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01749 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29750 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52751 return;
initial.commit09911bf2008-07-26 23:55:29752 }
753 }
[email protected]de246f52009-02-25 18:25:45754
[email protected]457f5cf2009-08-18 16:37:52755 AutomationMsg_CancelAuth::WriteReplyParams(
756 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
757 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29758}
759
[email protected]71f65dd2009-02-11 19:14:56760void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
761 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29762
763 if (tab_tracker_->ContainsHandle(tab_handle)) {
764 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
765 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
766
767 if (iter != login_handler_map_.end()) {
768 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56769 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29770 }
771 }
initial.commit09911bf2008-07-26 23:55:29772}
773
[email protected]71f65dd2009-02-11 19:14:56774void AutomationProvider::GetRedirectsFrom(int tab_handle,
775 const GURL& source_url,
776 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29777 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
778 if (tab_tracker_->ContainsHandle(tab_handle)) {
779 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
780 HistoryService* history_service =
781 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
782
783 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
784 "has no history service";
785 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56786 DCHECK(reply_message_ == NULL);
787 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29788 // Schedule a history query for redirects. The response will be sent
789 // asynchronously from the callback the history system uses to notify us
790 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29791 redirect_query_ = history_service->QueryRedirectsFrom(
792 source_url, &consumer_,
793 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
794 return; // Response will be sent when query completes.
795 }
796 }
797
798 // Send failure response.
[email protected]deb57402009-02-06 01:35:30799 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56800 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
801 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29802}
803
[email protected]71f65dd2009-02-11 19:14:56804void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
805 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29806 if (browser_tracker_->ContainsHandle(handle)) {
807 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56808 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29809 }
initial.commit09911bf2008-07-26 23:55:29810}
811
[email protected]202e7a72009-06-15 03:48:36812void AutomationProvider::GetBrowserLocale(string16* locale) {
813 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06814 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36815}
816
[email protected]71f65dd2009-02-11 19:14:56817void AutomationProvider::GetBrowserWindowCount(int* window_count) {
818 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29819}
820
[email protected]24497032009-05-01 17:00:29821void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
822 *window_count = static_cast<int>(
823 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
824}
825
[email protected]71f65dd2009-02-11 19:14:56826void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
827 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07828 AppModalDialog* dialog_delegate =
829 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50830 *showing_dialog = (dialog_delegate != NULL);
831 if (*showing_dialog)
832 *dialog_button = dialog_delegate->GetDialogButtons();
833 else
[email protected]478ff2ed2009-04-21 23:49:18834 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20835}
836
[email protected]71f65dd2009-02-11 19:14:56837void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
838 *success = false;
[email protected]fad84eab2008-12-05 00:37:20839
[email protected]1f460072009-05-28 17:02:07840 AppModalDialog* dialog_delegate =
841 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50842 if (dialog_delegate &&
843 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18844 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
845 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28846 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56847 *success = true;
[email protected]fad84eab2008-12-05 00:37:20848 }
[email protected]478ff2ed2009-04-21 23:49:18849 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
850 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56851 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28852 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56853 *success = true;
[email protected]fad84eab2008-12-05 00:37:20854 }
855 }
[email protected]c274acc2008-11-11 20:13:44856}
857
[email protected]fedaa7d2010-01-26 20:34:57858void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
859 if (browser_tracker_->ContainsHandle(handle)) {
860 Browser* browser = browser_tracker_->GetResource(handle);
861 browser->profile()->ShutdownSessionService();
862 *result = true;
863 } else {
864 *result = false;
865 }
866}
867
[email protected]71f65dd2009-02-11 19:14:56868void AutomationProvider::GetBrowserWindow(int index, int* handle) {
869 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29870 if (index >= 0) {
871 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]f07467d2010-06-16 14:28:30872 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index) {}
initial.commit09911bf2008-07-26 23:55:29873 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56874 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29875 }
876 }
initial.commit09911bf2008-07-26 23:55:29877}
878
[email protected]24497032009-05-01 17:00:29879void AutomationProvider::FindNormalBrowserWindow(int* handle) {
880 *handle = 0;
881 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31882 Browser::TYPE_NORMAL,
883 false);
[email protected]24497032009-05-01 17:00:29884 if (browser)
885 *handle = browser_tracker_->Add(browser);
886}
887
[email protected]71f65dd2009-02-11 19:14:56888void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
889 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29890 Browser* browser = BrowserList::GetLastActive();
891 if (browser)
[email protected]71f65dd2009-02-11 19:14:56892 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29893}
894
[email protected]b2aa3ed72010-02-01 18:37:14895#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28896// TODO(estade): use this implementation for all platforms?
897void AutomationProvider::GetActiveWindow(int* handle) {
898 gfx::NativeWindow window =
899 BrowserList::GetLastActive()->window()->GetNativeHandle();
900 *handle = window_tracker_->Add(window);
901}
902#endif
903
[email protected]4f6381ee2009-04-16 02:46:33904void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
905 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56906 *success = false;
[email protected]4ae62752008-08-04 23:28:47907 if (browser_tracker_->ContainsHandle(handle)) {
908 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14909 if (browser->command_updater()->SupportsCommand(command) &&
910 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47911 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56912 *success = true;
[email protected]4ae62752008-08-04 23:28:47913 }
914 }
[email protected]4ae62752008-08-04 23:28:47915}
916
[email protected]4f6381ee2009-04-16 02:46:33917void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56918 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53919 // List of commands which just finish synchronously and don't require
920 // setting up an observer.
921 static const int kSynchronousCommands[] = {
922 IDC_HOME,
923 IDC_SELECT_NEXT_TAB,
924 IDC_SELECT_PREVIOUS_TAB,
[email protected]2aa336e2010-04-06 21:05:25925 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53926 };
[email protected]56e71b7c2009-03-27 03:05:56927 if (browser_tracker_->ContainsHandle(handle)) {
928 Browser* browser = browser_tracker_->GetResource(handle);
929 if (browser->command_updater()->SupportsCommand(command) &&
930 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53931 // First check if we can handle the command without using an observer.
932 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
933 if (command == kSynchronousCommands[i]) {
934 browser->ExecuteCommand(command);
935 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
936 true);
937 Send(reply_message);
938 return;
939 }
940 }
941
942 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42943 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
944 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27945 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42946 return;
947 }
[email protected]56e71b7c2009-03-27 03:05:56948 }
949 }
[email protected]49a14a82009-03-31 04:16:44950 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56951 Send(reply_message);
952}
953
[email protected]fc2e0872009-08-21 22:14:41954// This task just adds another task to the event queue. This is useful if
955// you want to ensure that any tasks added to the event queue after this one
956// have already been processed by the time |task| is run.
957class InvokeTaskLaterTask : public Task {
958 public:
959 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
960 virtual ~InvokeTaskLaterTask() {}
961
962 virtual void Run() {
963 MessageLoop::current()->PostTask(FROM_HERE, task_);
964 }
965
966 private:
967 Task* task_;
968
969 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
970};
971
initial.commit09911bf2008-07-26 23:55:29972void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
973 int handle,
[email protected]d1a5941e2009-08-13 23:34:24974 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29975 int flags) {
[email protected]b410bc32009-08-14 01:11:14976 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51977 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41978 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29979 }
980}
981
[email protected]60507b12009-11-02 23:51:35982void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
983 int handle,
984 const gfx::Point& location) {
985 if (window_tracker_->ContainsHandle(handle))
986 ui_controls::SendMouseMove(location.x(), location.y());
987}
988
initial.commit09911bf2008-07-26 23:55:29989void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
990 int handle,
[email protected]bc1407f2009-09-29 00:33:35991 int key,
initial.commit09911bf2008-07-26 23:55:29992 int flags) {
[email protected]b410bc32009-08-14 01:11:14993 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29994 return;
995
[email protected]b410bc32009-08-14 01:11:14996 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29997 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35998 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38999 ((flags & views::Event::EF_CONTROL_DOWN) ==
1000 views::Event::EF_CONTROL_DOWN),
1001 ((flags & views::Event::EF_SHIFT_DOWN) ==
1002 views::Event::EF_SHIFT_DOWN),
1003 ((flags & views::Event::EF_ALT_DOWN) ==
[email protected]1b5a48c2010-04-29 23:08:301004 views::Event::EF_ALT_DOWN),
1005 ((flags & views::Event::EF_COMMAND_DOWN) ==
1006 views::Event::EF_COMMAND_DOWN));
initial.commit09911bf2008-07-26 23:55:291007}
initial.commit09911bf2008-07-26 23:55:291008
[email protected]71f65dd2009-02-11 19:14:561009void AutomationProvider::IsWindowActive(int handle, bool* success,
1010 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291011 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171012 *is_active =
1013 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561014 *success = true;
initial.commit09911bf2008-07-26 23:55:291015 } else {
[email protected]71f65dd2009-02-11 19:14:561016 *success = false;
1017 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291018 }
1019}
1020
[email protected]71f65dd2009-02-11 19:14:561021void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1022 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291023
1024 if (browser_tracker_->ContainsHandle(handle)) {
1025 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561026 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291027 }
initial.commit09911bf2008-07-26 23:55:291028}
1029
[email protected]982921f12009-10-27 21:43:531030void AutomationProvider::GetType(int handle, int* type_as_int) {
1031 *type_as_int = -1; // -1 is the error code
1032
1033 if (browser_tracker_->ContainsHandle(handle)) {
1034 Browser* browser = browser_tracker_->GetResource(handle);
1035 *type_as_int = static_cast<int>(browser->type());
1036 }
1037}
1038
[email protected]71f65dd2009-02-11 19:14:561039void AutomationProvider::GetTab(int win_handle, int tab_index,
1040 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561041 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291042 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1043 Browser* browser = browser_tracker_->GetResource(win_handle);
1044 if (tab_index < browser->tab_count()) {
1045 TabContents* tab_contents =
1046 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571047 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291048 }
1049 }
initial.commit09911bf2008-07-26 23:55:291050}
1051
[email protected]71f65dd2009-02-11 19:14:561052void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1053 std::wstring* title) {
1054 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291055 if (tab_tracker_->ContainsHandle(handle)) {
1056 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361057 NavigationEntry* entry = tab->GetActiveEntry();
1058 if (entry != NULL) {
1059 *title = UTF16ToWideHack(entry->title());
1060 } else {
1061 *title = std::wstring();
1062 }
[email protected]71f65dd2009-02-11 19:14:561063 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291064 }
initial.commit09911bf2008-07-26 23:55:291065}
1066
[email protected]77bc6732009-04-20 22:01:031067void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1068 *tabstrip_index = -1; // -1 is the error code
1069
1070 if (tab_tracker_->ContainsHandle(handle)) {
1071 NavigationController* tab = tab_tracker_->GetResource(handle);
1072 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121073 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031074 }
1075}
1076
initial.commit09911bf2008-07-26 23:55:291077void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1078 if (window_tracker_->ContainsHandle(handle)) {
1079 window_tracker_->Remove(window_tracker_->GetResource(handle));
1080 }
1081}
1082
1083void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161084 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571085 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291086}
1087
1088// TODO(brettw) change this to accept GURLs when history supports it
1089void AutomationProvider::OnRedirectQueryComplete(
1090 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371091 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291092 bool success,
[email protected]379c2b12009-07-01 21:50:331093 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291094 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561095 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291096
[email protected]deb57402009-02-06 01:35:301097 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101098 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291099 if (success) {
initial.commit09911bf2008-07-26 23:55:291100 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301101 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291102 }
1103
[email protected]4f3dc372009-02-24 00:10:291104 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301105
[email protected]71f65dd2009-02-11 19:14:561106 Send(reply_message_);
[email protected]6a329462010-05-06 19:22:231107 redirect_query_ = 0;
[email protected]71f65dd2009-02-11 19:14:561108 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291109}
1110
1111bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571112 DCHECK(channel_.get());
1113 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291114}
1115
1116Browser* AutomationProvider::FindAndActivateTab(
1117 NavigationController* controller) {
1118 int tab_index;
1119 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1120 if (browser)
1121 browser->SelectTabContentsAt(tab_index, true);
1122
1123 return browser;
1124}
1125
[email protected]9eaa18e2010-06-29 20:51:011126namespace {
1127
1128class GetCookiesTask : public Task {
1129 public:
1130 GetCookiesTask(const GURL& url,
1131 URLRequestContextGetter* context_getter,
1132 base::WaitableEvent* event,
1133 std::string* cookies)
1134 : url_(url),
1135 context_getter_(context_getter),
1136 event_(event),
1137 cookies_(cookies) {}
1138
1139 virtual void Run() {
1140 *cookies_ = context_getter_->GetCookieStore()->GetCookies(url_);
1141 event_->Signal();
1142 }
1143
1144 private:
1145 const GURL& url_;
1146 URLRequestContextGetter* const context_getter_;
1147 base::WaitableEvent* const event_;
1148 std::string* const cookies_;
1149
1150 DISALLOW_COPY_AND_ASSIGN(GetCookiesTask);
1151};
1152
1153std::string GetCookiesForURL(
1154 const GURL& url,
1155 URLRequestContextGetter* context_getter) {
1156 std::string cookies;
1157 base::WaitableEvent event(true /* manual reset */,
1158 false /* not initially signaled */);
1159 CHECK(ChromeThread::PostTask(
1160 ChromeThread::IO, FROM_HERE,
1161 new GetCookiesTask(url, context_getter, &event, &cookies)));
1162 event.Wait();
1163 return cookies;
1164}
1165
1166class SetCookieTask : public Task {
1167 public:
1168 SetCookieTask(const GURL& url,
1169 const std::string& value,
1170 URLRequestContextGetter* context_getter,
1171 base::WaitableEvent* event,
1172 bool* rv)
1173 : url_(url),
1174 value_(value),
1175 context_getter_(context_getter),
1176 event_(event),
1177 rv_(rv) {}
1178
1179 virtual void Run() {
1180 *rv_ = context_getter_->GetCookieStore()->SetCookie(url_, value_);
1181 event_->Signal();
1182 }
1183
1184 private:
1185 const GURL& url_;
1186 const std::string& value_;
1187 URLRequestContextGetter* const context_getter_;
1188 base::WaitableEvent* const event_;
1189 bool* const rv_;
1190
1191 DISALLOW_COPY_AND_ASSIGN(SetCookieTask);
1192};
1193
1194bool SetCookieForURL(
1195 const GURL& url,
1196 const std::string& value,
1197 URLRequestContextGetter* context_getter) {
1198 base::WaitableEvent event(true /* manual reset */,
1199 false /* not initially signaled */);
1200 bool rv = false;
1201 CHECK(ChromeThread::PostTask(
1202 ChromeThread::IO, FROM_HERE,
1203 new SetCookieTask(url, value, context_getter, &event, &rv)));
1204 event.Wait();
1205 return rv;
1206}
1207
1208class DeleteCookieTask : public Task {
1209 public:
1210 DeleteCookieTask(const GURL& url,
1211 const std::string& name,
1212 const scoped_refptr<URLRequestContextGetter>& context_getter)
1213 : url_(url),
1214 name_(name),
1215 context_getter_(context_getter) {}
1216
1217 virtual void Run() {
1218 net::CookieStore* cookie_store = context_getter_->GetCookieStore();
1219 cookie_store->DeleteCookie(url_, name_);
1220 }
1221
1222 private:
1223 const GURL url_;
1224 const std::string name_;
1225 const scoped_refptr<URLRequestContextGetter> context_getter_;
1226
1227 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
1228};
1229
1230} // namespace
1231
[email protected]71f65dd2009-02-11 19:14:561232void AutomationProvider::GetCookies(const GURL& url, int handle,
1233 int* value_size,
1234 std::string* value) {
1235 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291236 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1237 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311238
1239 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001240 scoped_refptr<URLRequestContextGetter> request_context =
1241 tab->tab_contents()->request_context();
1242 if (!request_context.get())
1243 request_context = tab->profile()->GetRequestContext();
1244
[email protected]9eaa18e2010-06-29 20:51:011245 *value = GetCookiesForURL(url, request_context.get());
[email protected]71f65dd2009-02-11 19:14:561246 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291247 }
initial.commit09911bf2008-07-26 23:55:291248}
1249
[email protected]71f65dd2009-02-11 19:14:561250void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291251 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561252 int handle,
1253 int* response_value) {
1254 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291255
1256 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1257 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311258
[email protected]dfa46e5f2009-11-17 18:48:431259 scoped_refptr<URLRequestContextGetter> request_context =
1260 tab->tab_contents()->request_context();
1261 if (!request_context.get())
1262 request_context = tab->profile()->GetRequestContext();
1263
[email protected]9eaa18e2010-06-29 20:51:011264 if (SetCookieForURL(url, value, request_context.get()))
[email protected]71f65dd2009-02-11 19:14:561265 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291266 }
initial.commit09911bf2008-07-26 23:55:291267}
1268
[email protected]5fa57942010-04-21 23:07:221269void AutomationProvider::DeleteCookie(const GURL& url,
1270 const std::string& cookie_name,
1271 int handle, bool* success) {
1272 *success = false;
1273 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1274 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]9eaa18e2010-06-29 20:51:011275 ChromeThread::PostTask(
1276 ChromeThread::IO, FROM_HERE,
1277 new DeleteCookieTask(url, cookie_name,
1278 tab->profile()->GetRequestContext()));
[email protected]5fa57942010-04-21 23:07:221279 *success = true;
1280 }
1281}
1282
[email protected]71f65dd2009-02-11 19:14:561283void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1284 *success = false;
initial.commit09911bf2008-07-26 23:55:291285 if (tab_tracker_->ContainsHandle(handle)) {
1286 NavigationController* tab = tab_tracker_->GetResource(handle);
1287 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541288 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561289 *success = true;
initial.commit09911bf2008-07-26 23:55:291290 }
initial.commit09911bf2008-07-26 23:55:291291}
1292
[email protected]71f65dd2009-02-11 19:14:561293void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1294 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291295
1296 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561297 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341298 TabContents* tab_contents =
1299 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161300 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1301 if (rph)
1302 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291303 }
initial.commit09911bf2008-07-26 23:55:291304}
1305
1306void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331307 NOTREACHED() << "This function has been deprecated. "
1308 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291309}
1310
[email protected]71f65dd2009-02-11 19:14:561311void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291312 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561313 const std::wstring& script,
1314 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291315 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341316 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1317 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571318 // Set the routing id of this message with the controller.
1319 // This routing id needs to be remembered for the reverse
1320 // communication while sending back the response of
1321 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331322 std::wstring set_automation_id;
1323 SStringPrintf(&set_automation_id,
1324 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561325 reply_message->routing_id());
1326
1327 DCHECK(reply_message_ == NULL);
1328 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291329
[email protected]57c6a652009-05-04 07:58:341330 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331331 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341332 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061333 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571334 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291335 }
1336
1337 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561338 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1339 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291340 }
1341}
1342
[email protected]71f65dd2009-02-11 19:14:561343void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1344 *visible = false;
[email protected]20e93d12008-08-28 16:31:571345
[email protected]59560e0b2009-06-04 03:30:221346 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251347#if defined(OS_CHROMEOS)
1348 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1349 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1350 for (BrowserList::const_iterator it = BrowserList::begin();
1351 it != BrowserList::end(); ++it) {
1352 if ((*it)->type() == Browser::TYPE_POPUP) {
1353 const GURL& url =
1354 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1355
1356 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1357 url.host() == chrome::kChromeUIFileBrowseHost) {
1358 *visible = true;
1359 break;
1360 }
1361 }
1362 }
1363#else
[email protected]59560e0b2009-06-04 03:30:221364 Browser* browser = browser_tracker_->GetResource(handle);
1365 if (browser) {
1366 *visible = browser->window()->IsDownloadShelfVisible();
1367 }
[email protected]f5bf8ccf2010-02-05 18:19:251368#endif
[email protected]59560e0b2009-06-04 03:30:221369 }
initial.commit09911bf2008-07-26 23:55:291370}
1371
[email protected]59560e0b2009-06-04 03:30:221372void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1373 if (browser_tracker_->ContainsHandle(handle)) {
1374 Browser* browser = browser_tracker_->GetResource(handle);
1375 if (browser) {
1376 if (visible)
1377 browser->window()->GetDownloadShelf()->Show();
1378 else
1379 browser->window()->GetDownloadShelf()->Close();
1380 }
1381 }
1382}
1383
[email protected]34930432009-11-09 00:12:091384void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1385 *visible = false;
1386
1387 if (browser_tracker_->ContainsHandle(handle)) {
1388 Browser* browser = browser_tracker_->GetResource(handle);
1389 if (browser)
1390 *visible = browser->window()->IsFullscreen();
1391 }
1392}
1393
1394void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1395 bool* visible) {
1396 *visible = false;
1397
1398 if (browser_tracker_->ContainsHandle(handle)) {
1399 Browser* browser = browser_tracker_->GetResource(handle);
1400 if (browser)
1401 *visible = browser->window()->IsFullscreenBubbleVisible();
1402 }
1403}
[email protected]59560e0b2009-06-04 03:30:221404
[email protected]71f65dd2009-02-11 19:14:561405void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1406 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291407 if (tab_tracker_->ContainsHandle(handle)) {
1408 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111409 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291410 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561411 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291412 }
1413 }
initial.commit09911bf2008-07-26 23:55:291414}
1415
initial.commit09911bf2008-07-26 23:55:291416void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561417 int handle, const std::wstring& find_request,
1418 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311419 NOTREACHED() << "This function has been deprecated."
1420 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561421 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311422 return;
1423}
1424
[email protected]4f999132009-03-31 18:08:401425void AutomationProvider::HandleFindRequest(
1426 int handle,
1427 const AutomationMsg_Find_Params& params,
1428 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291429 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561430 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1431 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291432 return;
1433 }
1434
1435 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111436 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291437
1438 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141439 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291440
[email protected]57c6a652009-05-04 07:58:341441 tab_contents->set_current_find_request_id(
1442 FindInPageNotificationObserver::kFindInPageRequestId);
1443 tab_contents->render_view_host()->StartFinding(
1444 FindInPageNotificationObserver::kFindInPageRequestId,
1445 params.search_string, params.forward, params.match_case,
1446 params.find_next);
initial.commit09911bf2008-07-26 23:55:291447}
1448
[email protected]5f8af2a2008-08-06 22:49:451449void AutomationProvider::HandleOpenFindInPageRequest(
1450 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291451 if (browser_tracker_->ContainsHandle(handle)) {
1452 Browser* browser = browser_tracker_->GetResource(handle);
1453 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451454 }
1455}
1456
[email protected]71f65dd2009-02-11 19:14:561457void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561458 *visible = false;
[email protected]855c0142009-09-28 22:35:241459 Browser* browser = browser_tracker_->GetResource(handle);
1460 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581461 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171462 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241463 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291464 }
[email protected]20e93d12008-08-28 16:31:571465}
1466
[email protected]71f65dd2009-02-11 19:14:561467void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1468 int* y) {
[email protected]9e0534b2008-10-21 15:03:011469 gfx::Point position(0, 0);
1470 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291471 if (browser_tracker_->ContainsHandle(handle)) {
1472 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581473 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171474 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581475 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291476 }
[email protected]20e93d12008-08-28 16:31:571477
[email protected]71f65dd2009-02-11 19:14:561478 *x = position.x();
1479 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571480}
1481
[email protected]4512cb52010-04-05 19:50:251482// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1483// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581484void AutomationProvider::GetBookmarkBarVisibility(int handle,
1485 bool* visible,
1486 bool* animating) {
1487 *visible = false;
1488 *animating = false;
1489
1490 if (browser_tracker_->ContainsHandle(handle)) {
1491 Browser* browser = browser_tracker_->GetResource(handle);
1492 if (browser) {
[email protected]472f099b2010-05-27 17:07:121493#if 0 // defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
1494 // TODO(jrg): Was removed in rev43789 for perf. Need to investigate.
1495
[email protected]ab6ca392010-04-07 00:44:131496 // IsBookmarkBarVisible() line looks correct but is not
1497 // consistent across platforms. Specifically, on Mac/Linux, it
1498 // returns false if the bar is hidden in a pref (even if visible
1499 // on the NTP). On ChromeOS, it returned true if on NTP
1500 // independent of the pref. Making the code more consistent
1501 // caused a perf bot regression on Windows (which shares views).
1502 // See http://crbug.com/40225
[email protected]4512cb52010-04-05 19:50:251503 *visible = browser->profile()->GetPrefs()->GetBoolean(
1504 prefs::kShowBookmarkBar);
[email protected]7e4cd4e82010-04-05 20:59:401505#else
1506 *visible = browser->window()->IsBookmarkBarVisible();
1507#endif
[email protected]c3240722010-03-05 21:52:581508 *animating = browser->window()->IsBookmarkBarAnimating();
1509 }
1510 }
1511}
1512
[email protected]6d8ffc9f2010-03-12 18:27:531513void AutomationProvider::GetBookmarksAsJSON(int handle,
1514 std::string* bookmarks_as_json,
1515 bool *success) {
1516 *success = false;
1517 if (browser_tracker_->ContainsHandle(handle)) {
1518 Browser* browser = browser_tracker_->GetResource(handle);
1519 if (browser) {
1520 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1521 return;
1522 }
1523 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1524 browser->profile(),
1525 browser->profile()->GetBookmarkModel());
1526 *success = storage->SerializeData(bookmarks_as_json);
1527 }
1528 }
1529}
1530
1531void AutomationProvider::WaitForBookmarkModelToLoad(
1532 int handle,
1533 IPC::Message* reply_message) {
1534 if (browser_tracker_->ContainsHandle(handle)) {
1535 Browser* browser = browser_tracker_->GetResource(handle);
1536 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1537 if (model->IsLoaded()) {
1538 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1539 reply_message, true);
1540 Send(reply_message);
1541 } else {
1542 // The observer will delete itself when done.
1543 new AutomationProviderBookmarkModelObserver(this, reply_message,
1544 model);
1545 }
1546 }
1547}
1548
1549void AutomationProvider::AddBookmarkGroup(int handle,
1550 int64 parent_id, int index,
1551 std::wstring title,
1552 bool* success) {
1553 if (browser_tracker_->ContainsHandle(handle)) {
1554 Browser* browser = browser_tracker_->GetResource(handle);
1555 if (browser) {
1556 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1557 if (!model->IsLoaded()) {
1558 *success = false;
1559 return;
1560 }
1561 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1562 DCHECK(parent);
1563 if (parent) {
1564 const BookmarkNode* child = model->AddGroup(parent, index,
1565 WideToUTF16(title));
1566 DCHECK(child);
1567 if (child)
1568 *success = true;
1569 }
1570 }
1571 }
1572 *success = false;
1573}
1574
1575void AutomationProvider::AddBookmarkURL(int handle,
1576 int64 parent_id, int index,
1577 std::wstring title, const GURL& url,
1578 bool* success) {
1579 if (browser_tracker_->ContainsHandle(handle)) {
1580 Browser* browser = browser_tracker_->GetResource(handle);
1581 if (browser) {
1582 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1583 if (!model->IsLoaded()) {
1584 *success = false;
1585 return;
1586 }
1587 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1588 DCHECK(parent);
1589 if (parent) {
1590 const BookmarkNode* child = model->AddURL(parent, index,
1591 WideToUTF16(title), url);
1592 DCHECK(child);
1593 if (child)
1594 *success = true;
1595 }
1596 }
1597 }
1598 *success = false;
1599}
1600
1601void AutomationProvider::ReparentBookmark(int handle,
1602 int64 id, int64 new_parent_id,
1603 int index,
1604 bool* success) {
1605 if (browser_tracker_->ContainsHandle(handle)) {
1606 Browser* browser = browser_tracker_->GetResource(handle);
1607 if (browser) {
1608 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1609 if (!model->IsLoaded()) {
1610 *success = false;
1611 return;
1612 }
1613 const BookmarkNode* node = model->GetNodeByID(id);
1614 DCHECK(node);
1615 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1616 DCHECK(new_parent);
1617 if (node && new_parent) {
1618 model->Move(node, new_parent, index);
1619 *success = true;
1620 }
1621 }
1622 }
1623 *success = false;
1624}
1625
1626void AutomationProvider::SetBookmarkTitle(int handle,
1627 int64 id, std::wstring title,
1628 bool* success) {
1629 if (browser_tracker_->ContainsHandle(handle)) {
1630 Browser* browser = browser_tracker_->GetResource(handle);
1631 if (browser) {
1632 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1633 if (!model->IsLoaded()) {
1634 *success = false;
1635 return;
1636 }
1637 const BookmarkNode* node = model->GetNodeByID(id);
1638 DCHECK(node);
1639 if (node) {
1640 model->SetTitle(node, WideToUTF16(title));
1641 *success = true;
1642 }
1643 }
1644 }
1645 *success = false;
1646}
1647
1648void AutomationProvider::SetBookmarkURL(int handle,
1649 int64 id, const GURL& url,
1650 bool* success) {
1651 if (browser_tracker_->ContainsHandle(handle)) {
1652 Browser* browser = browser_tracker_->GetResource(handle);
1653 if (browser) {
1654 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1655 if (!model->IsLoaded()) {
1656 *success = false;
1657 return;
1658 }
1659 const BookmarkNode* node = model->GetNodeByID(id);
1660 DCHECK(node);
1661 if (node) {
1662 model->SetURL(node, url);
1663 *success = true;
1664 }
1665 }
1666 }
1667 *success = false;
1668}
1669
1670void AutomationProvider::RemoveBookmark(int handle,
1671 int64 id,
1672 bool* success) {
1673 if (browser_tracker_->ContainsHandle(handle)) {
1674 Browser* browser = browser_tracker_->GetResource(handle);
1675 if (browser) {
1676 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1677 if (!model->IsLoaded()) {
1678 *success = false;
1679 return;
1680 }
1681 const BookmarkNode* node = model->GetNodeByID(id);
1682 DCHECK(node);
1683 if (node) {
1684 const BookmarkNode* parent = node->GetParent();
1685 DCHECK(parent);
1686 model->Remove(parent, parent->IndexOfChild(node));
1687 *success = true;
1688 }
1689 }
1690 }
1691 *success = false;
1692}
1693
[email protected]ef413ca2010-05-25 21:09:141694// Sample json input: { "command": "SetWindowDimensions",
1695// "x": 20, # optional
1696// "y": 20, # optional
1697// "width": 800, # optional
1698// "height": 600 } # optional
1699void AutomationProvider::SetWindowDimensions(Browser* browser,
1700 DictionaryValue* args,
1701 IPC::Message* reply_message) {
1702 gfx::Rect rect = browser->window()->GetRestoredBounds();
1703 int x, y, width, height;
1704 if (args->GetInteger(L"x", &x))
1705 rect.set_x(x);
1706 if (args->GetInteger(L"y", &y))
1707 rect.set_y(y);
1708 if (args->GetInteger(L"width", &width))
1709 rect.set_width(width);
1710 if (args->GetInteger(L"height", &height))
1711 rect.set_height(height);
1712 browser->window()->SetBounds(rect);
1713 AutomationMsg_SendJSONRequest::WriteReplyParams(
1714 reply_message, std::string("{}"), true);
1715 Send(reply_message);
1716}
1717
[email protected]a9ff2c02010-05-13 17:33:051718// Sample json input: { "command": "GetBrowserInfo" }
1719// Refer to GetBrowserInfo() in chrome/test/pyautolib/pyauto.py for
1720// sample json output.
[email protected]53329582010-05-14 21:10:581721void AutomationProvider::GetBrowserInfo(Browser* browser,
1722 DictionaryValue* args,
[email protected]a9ff2c02010-05-13 17:33:051723 IPC::Message* reply_message) {
1724 std::string json_return;
1725 bool reply_return = true;
1726
1727 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);
1837
[email protected]a9ff2c02010-05-13 17:33:051838 base::JSONWriter::Write(return_value.get(), false, &json_return);
1839 AutomationMsg_SendJSONRequest::WriteReplyParams(
1840 reply_message, json_return, reply_return);
1841 Send(reply_message);
1842}
1843
[email protected]24e2b102010-04-29 17:56:471844// Sample json input: { "command": "GetHistoryInfo",
1845// "search_text": "some text" }
[email protected]e6e376e2010-04-19 21:41:361846// Refer chrome/test/pyautolib/history_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581847void AutomationProvider::GetHistoryInfo(Browser* browser,
1848 DictionaryValue* args,
1849 IPC::Message* reply_message) {
[email protected]e6e376e2010-04-19 21:41:361850 consumer_.CancelAllRequests();
1851
[email protected]e53668962010-06-23 15:35:251852 string16 search_text;
1853 args->GetString("search_text", &search_text);
[email protected]e6e376e2010-04-19 21:41:361854
1855 // Fetch history.
1856 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1857 history::QueryOptions options;
1858 // The observer owns itself. It deletes itself after it fetches history.
1859 AutomationProviderHistoryObserver* history_observer =
1860 new AutomationProviderHistoryObserver(this, reply_message);
1861 hs->QueryHistory(
1862 search_text,
1863 options,
1864 &consumer_,
1865 NewCallback(history_observer,
1866 &AutomationProviderHistoryObserver::HistoryQueryComplete));
1867}
1868
[email protected]bbe6aa02010-05-07 17:27:291869// Sample json input: { "command": "AddHistoryItem",
1870// "item": { "URL": "http://www.google.com",
1871// "title": "Google", # optional
1872// "time": 12345 # optional (time_t)
1873// } }
1874// Refer chrome/test/pyautolib/pyauto.py for details on input.
[email protected]53329582010-05-14 21:10:581875void AutomationProvider::AddHistoryItem(Browser* browser,
1876 DictionaryValue* args,
1877 IPC::Message* reply_message) {
[email protected]bbe6aa02010-05-07 17:27:291878 bool reply_return = true;
1879 std::string json_return = "{}";
1880
1881 DictionaryValue* item = NULL;
1882 args->GetDictionary(L"item", &item);
1883 string16 url_text;
[email protected]e53668962010-06-23 15:35:251884 string16 title;
[email protected]bbe6aa02010-05-07 17:27:291885 base::Time time = base::Time::Now();
1886
1887 if (item->GetString("url", &url_text)) {
1888 GURL gurl(url_text);
[email protected]e53668962010-06-23 15:35:251889 item->GetString("title", &title); // Don't care if it fails.
[email protected]bbe6aa02010-05-07 17:27:291890 int it;
1891 double dt;
1892 if (item->GetInteger(L"time", &it))
1893 time = base::Time::FromTimeT(it);
1894 else if (item->GetReal(L"time", &dt))
1895 time = base::Time::FromDoubleT(dt);
1896
1897 // Ideas for "dummy" values (e.g. id_scope) came from
1898 // chrome/browser/autocomplete/history_contents_provider_unittest.cc
1899 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1900 const void* id_scope = reinterpret_cast<void*>(1);
1901 hs->AddPage(gurl, time,
1902 id_scope,
1903 0,
1904 GURL(),
1905 PageTransition::LINK,
1906 history::RedirectList(),
1907 false);
[email protected]e53668962010-06-23 15:35:251908 if (title.length())
[email protected]bbe6aa02010-05-07 17:27:291909 hs->SetPageTitle(gurl, title);
[email protected]bbe6aa02010-05-07 17:27:291910 } else {
[email protected]7060bb292010-06-24 00:52:491911 json_return = JSONErrorString("bad args (no URL in dict?)");
[email protected]bbe6aa02010-05-07 17:27:291912 reply_return = false;
1913 }
1914
1915 AutomationMsg_SendJSONRequest::WriteReplyParams(
1916 reply_message, json_return, reply_return);
1917 Send(reply_message);
1918}
1919
[email protected]24e2b102010-04-29 17:56:471920// Sample json input: { "command": "GetDownloadsInfo" }
[email protected]e6e376e2010-04-19 21:41:361921// Refer chrome/test/pyautolib/download_info.py for sample json output.
[email protected]53329582010-05-14 21:10:581922void AutomationProvider::GetDownloadsInfo(Browser* browser,
1923 DictionaryValue* args,
1924 IPC::Message* reply_message) {
[email protected]d4adc292010-04-15 18:06:391925 std::string json_return;
1926 bool reply_return = true;
1927 AutomationProviderDownloadManagerObserver observer;
1928 std::vector<DownloadItem*> downloads;
1929 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
1930
1931 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7060bb292010-06-24 00:52:491932 json_return = JSONErrorString("no download manager");
[email protected]d4adc292010-04-15 18:06:391933 reply_return = false;
1934 } else {
1935 // Use DownloadManager's GetDownloads() method and not GetCurrentDownloads()
1936 // since that would be transient; a download might enter and empty out
1937 // the current download queue too soon to be noticed.
1938 profile_->GetDownloadManager()->GetDownloads(&observer, L"");
1939 downloads = observer.Downloads();
1940 }
1941
1942 std::map<DownloadItem::DownloadState, std::string> state_to_string;
1943 state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS");
1944 state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED");
1945 state_to_string[DownloadItem::REMOVING] = std::string("REMOVING");
1946 state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE");
1947
1948 std::map<DownloadItem::SafetyState, std::string> safety_state_to_string;
1949 safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE");
1950 safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS");
1951 safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] =
1952 std::string("DANGEROUS_BUT_VALIDATED");
1953
1954 ListValue* list_of_downloads = new ListValue;
1955 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
1956 it != downloads.end();
1957 it++) { // Fill info about each download item.
1958 DictionaryValue* dl_item_value = new DictionaryValue;
1959 dl_item_value->SetInteger(L"id", static_cast<int>((*it)->id()));
1960 dl_item_value->SetString(L"url", (*it)->url().spec());
1961 dl_item_value->SetString(L"referrer_url", (*it)->referrer_url().spec());
1962 dl_item_value->SetString(L"file_name", (*it)->file_name().value());
1963 dl_item_value->SetString(L"full_path", (*it)->full_path().value());
1964 dl_item_value->SetBoolean(L"is_paused", (*it)->is_paused());
1965 dl_item_value->SetBoolean(L"open_when_complete",
1966 (*it)->open_when_complete());
1967 dl_item_value->SetBoolean(L"is_extension_install",
1968 (*it)->is_extension_install());
1969 dl_item_value->SetBoolean(L"is_temporary", (*it)->is_temporary());
1970 dl_item_value->SetBoolean(L"is_otr", (*it)->is_otr()); // off-the-record
1971 dl_item_value->SetString(L"state", state_to_string[(*it)->state()]);
1972 dl_item_value->SetString(L"safety_state",
1973 safety_state_to_string[(*it)->safety_state()]);
1974 dl_item_value->SetInteger(L"PercentComplete", (*it)->PercentComplete());
1975 list_of_downloads->Append(dl_item_value);
1976 }
1977 return_value->Set(L"downloads", list_of_downloads);
1978 base::JSONWriter::Write(return_value.get(), false, &json_return);
1979
1980 AutomationMsg_SendJSONRequest::WriteReplyParams(
1981 reply_message, json_return, reply_return);
1982 Send(reply_message);
1983 // All value objects allocated above are owned by |return_value|
1984 // and get freed by it.
1985}
1986
[email protected]59a611242010-04-02 02:24:041987void AutomationProvider::WaitForDownloadsToComplete(
[email protected]53329582010-05-14 21:10:581988 Browser* browser,
[email protected]59a611242010-04-02 02:24:041989 DictionaryValue* args,
1990 IPC::Message* reply_message) {
1991 std::string json_return;
1992 bool reply_return = true;
1993 AutomationProviderDownloadManagerObserver observer;
1994 std::vector<DownloadItem*> downloads;
1995
1996 // Look for a quick return.
1997 if (!profile_->HasCreatedDownloadManager()) {
[email protected]7060bb292010-06-24 00:52:491998 json_return = JSONErrorString("no download manager");
[email protected]59a611242010-04-02 02:24:041999 reply_return = false;
2000 } else {
2001 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
2002 FilePath());
2003 downloads = observer.Downloads();
2004 if (downloads.size() == 0) {
2005 json_return = "{}";
2006 }
2007 }
2008 if (!json_return.empty()) {
2009 AutomationMsg_SendJSONRequest::WriteReplyParams(
2010 reply_message, json_return, reply_return);
2011 Send(reply_message);
2012 }
2013
2014 // The observer owns itself. When the last observed item pings, it
2015 // deletes itself.
2016 AutomationProviderDownloadItemObserver* item_observer =
2017 new AutomationProviderDownloadItemObserver(
2018 this, reply_message, downloads.size());
2019 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
2020 i != downloads.end();
2021 i++) {
2022 (*i)->AddObserver(item_observer);
2023 }
2024}
2025
[email protected]24e2b102010-04-29 17:56:472026// Sample json input: { "command": "GetPrefsInfo" }
2027// Refer chrome/test/pyautolib/prefs_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582028void AutomationProvider::GetPrefsInfo(Browser* browser,
2029 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472030 IPC::Message* reply_message) {
2031 std::string json_return;
2032 bool reply_return = true;
2033
2034 const PrefService::PreferenceSet& prefs =
2035 profile_->GetPrefs()->preference_set();
2036 DictionaryValue* items = new DictionaryValue;
2037 for (PrefService::PreferenceSet::const_iterator it = prefs.begin();
2038 it != prefs.end(); ++it) {
2039 items->Set((*it)->name(), (*it)->GetValue()->DeepCopy());
2040 }
2041 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2042 return_value->Set(L"prefs", items); // return_value owns items.
2043
2044 base::JSONWriter::Write(return_value.get(), false, &json_return);
2045 AutomationMsg_SendJSONRequest::WriteReplyParams(
2046 reply_message, json_return, reply_return);
2047 Send(reply_message);
2048}
2049
2050// Sample json input: { "command": "SetPrefs", "path": path, "value": value }
[email protected]53329582010-05-14 21:10:582051void AutomationProvider::SetPrefs(Browser* browser,
2052 DictionaryValue* args,
[email protected]24e2b102010-04-29 17:56:472053 IPC::Message* reply_message) {
2054 bool reply_return = true;
2055 std::string json_return = "{}";
2056 std::wstring path;
2057 Value* val;
2058 if (args->GetString(L"path", &path) && args->Get(L"value", &val)) {
2059 PrefService* pref_service = profile_->GetPrefs();
2060 const PrefService::Preference* pref =
2061 pref_service->FindPreference(path.c_str());
2062 if (!pref) { // Not a registered pref.
[email protected]7060bb292010-06-24 00:52:492063 json_return = JSONErrorString("pref not registered.");
[email protected]24e2b102010-04-29 17:56:472064 reply_return = false;
2065 } else if (pref->IsManaged()) { // Do not attempt to change a managed pref.
[email protected]7060bb292010-06-24 00:52:492066 json_return = JSONErrorString("pref is managed. cannot be changed.");
[email protected]24e2b102010-04-29 17:56:472067 reply_return = false;
2068 } else { // Set the pref.
2069 pref_service->Set(path.c_str(), *val);
2070 }
2071 } else {
[email protected]7060bb292010-06-24 00:52:492072 json_return = JSONErrorString("no pref path or value given.");
[email protected]24e2b102010-04-29 17:56:472073 reply_return = false;
2074 }
2075
2076 AutomationMsg_SendJSONRequest::WriteReplyParams(
2077 reply_message, json_return, reply_return);
2078 Send(reply_message);
2079}
2080
[email protected]53329582010-05-14 21:10:582081// Sample json input: { "command": "GetOmniboxInfo" }
2082// Refer chrome/test/pyautolib/omnibox_info.py for sample json output.
2083void AutomationProvider::GetOmniboxInfo(Browser* browser,
2084 DictionaryValue* args,
2085 IPC::Message* reply_message) {
2086 std::string json_return;
2087 bool reply_return = true;
2088 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2089
2090 LocationBar* loc_bar = browser->window()->GetLocationBar();
2091 AutocompleteEditView* edit_view = loc_bar->location_entry();
2092 AutocompleteEditModel* model = edit_view->model();
2093
2094 // Fill up matches.
2095 ListValue* matches = new ListValue;
2096 const AutocompleteResult& result = model->result();
2097 for (AutocompleteResult::const_iterator i = result.begin();
2098 i != result.end(); ++i) {
2099 const AutocompleteMatch& match = *i;
2100 DictionaryValue* item = new DictionaryValue; // owned by return_value
2101 item->SetString(L"type", AutocompleteMatch::TypeToString(match.type));
2102 item->SetBoolean(L"starred", match.starred);
2103 item->SetString(L"destination_url", match.destination_url.spec());
2104 item->SetString(L"contents", match.contents);
2105 item->SetString(L"description", match.description);
2106 matches->Append(item);
2107 }
2108 return_value->Set(L"matches", matches);
2109
2110 // Fill up other properties.
2111 DictionaryValue* properties = new DictionaryValue; // owned by return_value
2112 properties->SetBoolean(L"has_focus", model->has_focus());
2113 properties->SetBoolean(L"query_in_progress", model->query_in_progress());
2114 properties->SetString(L"keyword", model->keyword());
2115 properties->SetString(L"text", edit_view->GetText());
2116 return_value->Set(L"properties", properties);
2117
2118 base::JSONWriter::Write(return_value.get(), false, &json_return);
2119 AutomationMsg_SendJSONRequest::WriteReplyParams(
2120 reply_message, json_return, reply_return);
2121 Send(reply_message);
2122}
2123
2124// Sample json input: { "command": "SetOmniboxText",
2125// "text": "goog" }
2126void AutomationProvider::SetOmniboxText(Browser* browser,
2127 DictionaryValue* args,
2128 IPC::Message* reply_message) {
2129 std::string json_return = "{}";
2130 bool reply_return = true;
2131 std::wstring text;
2132
2133 if (!args->GetString(L"text", &text)) {
[email protected]7060bb292010-06-24 00:52:492134 json_return = JSONErrorString("text missing");
[email protected]53329582010-05-14 21:10:582135 reply_return = false;
2136 } else {
2137 browser->FocusLocationBar();
2138 LocationBar* loc_bar = browser->window()->GetLocationBar();
2139 AutocompleteEditView* edit_view = loc_bar->location_entry();
2140 edit_view->model()->OnSetFocus(false);
2141 edit_view->SetUserText(text);
2142 }
2143
2144 AutomationMsg_SendJSONRequest::WriteReplyParams(
2145 reply_message, json_return, reply_return);
2146 Send(reply_message);
2147}
2148
2149// Sample json input: { "command": "OmniboxMovePopupSelection",
2150// "count": 1 }
2151// Negative count implies up, positive implies down. Count values will be
2152// capped by the size of the popup list.
2153void AutomationProvider::OmniboxMovePopupSelection(
2154 Browser* browser,
2155 DictionaryValue* args,
2156 IPC::Message* reply_message) {
2157 std::string json_return = "{}";
2158 bool reply_return = true;
2159 int count;
2160
2161 if (!args->GetInteger(L"count", &count)) {
[email protected]7060bb292010-06-24 00:52:492162 json_return = JSONErrorString("count missing");
[email protected]53329582010-05-14 21:10:582163 reply_return = false;
2164 } else {
2165 LocationBar* loc_bar = browser->window()->GetLocationBar();
2166 AutocompleteEditModel* model = loc_bar->location_entry()->model();
2167 model->OnUpOrDownKeyPressed(count);
2168 }
2169
2170 AutomationMsg_SendJSONRequest::WriteReplyParams(
2171 reply_message, json_return, reply_return);
2172 Send(reply_message);
2173}
2174
2175// Sample json input: { "command": "OmniboxAcceptInput" }
2176void AutomationProvider::OmniboxAcceptInput(Browser* browser,
2177 DictionaryValue* args,
2178 IPC::Message* reply_message) {
[email protected]cb84d642010-06-10 00:56:282179 NavigationController& controller =
2180 browser->GetSelectedTabContents()->controller();
[email protected]c1654832010-05-17 23:22:122181 // Setup observer to wait until the selected item loads.
2182 NotificationObserver* observer =
[email protected]cb84d642010-06-10 00:56:282183 new OmniboxAcceptNotificationObserver(&controller, this, reply_message);
[email protected]c1654832010-05-17 23:22:122184 notification_observer_list_.AddObserver(observer);
[email protected]53329582010-05-14 21:10:582185
2186 browser->window()->GetLocationBar()->AcceptInput();
[email protected]53329582010-05-14 21:10:582187}
2188
[email protected]a3cd5022010-06-16 18:25:292189// Sample json input: { "command": "GetInitialLoadTimes" }
2190// Refer to InitialLoadObserver::GetTimingInformation() for sample output.
2191void AutomationProvider::GetInitialLoadTimes(
2192 Browser*,
2193 DictionaryValue*,
2194 IPC::Message* reply_message) {
2195 scoped_ptr<DictionaryValue> return_value(
2196 initial_load_observer_->GetTimingInformation());
2197
2198 std::string json_return;
2199 base::JSONWriter::Write(return_value.get(), false, &json_return);
2200 AutomationMsg_SendJSONRequest::WriteReplyParams(
2201 reply_message, json_return, true);
2202 Send(reply_message);
2203}
2204
[email protected]f7d48012010-05-06 08:17:052205// Sample json input: { "command": "GetPluginsInfo" }
2206// Refer chrome/test/pyautolib/plugins_info.py for sample json output.
[email protected]53329582010-05-14 21:10:582207void AutomationProvider::GetPluginsInfo(Browser* browser,
2208 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052209 IPC::Message* reply_message) {
2210 std::string json_return;
2211 bool reply_return = true;
2212
2213 std::vector<WebPluginInfo> plugins;
2214 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins);
2215 ListValue* items = new ListValue;
2216 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin();
2217 it != plugins.end();
2218 ++it) {
2219 DictionaryValue* item = new DictionaryValue;
[email protected]c9d811372010-06-23 21:44:572220 item->SetStringFromUTF16(L"name", it->name);
[email protected]f7d48012010-05-06 08:17:052221 item->SetString(L"path", it->path.value());
[email protected]c9d811372010-06-23 21:44:572222 item->SetStringFromUTF16(L"version", it->version);
2223 item->SetStringFromUTF16(L"desc", it->desc);
[email protected]f7d48012010-05-06 08:17:052224 item->SetBoolean(L"enabled", it->enabled);
2225 // Add info about mime types.
2226 ListValue* mime_types = new ListValue();
2227 for (std::vector<WebPluginMimeType>::const_iterator type_it =
2228 it->mime_types.begin();
2229 type_it != it->mime_types.end();
2230 ++type_it) {
2231 DictionaryValue* mime_type = new DictionaryValue();
2232 mime_type->SetString(L"mimeType", type_it->mime_type);
[email protected]c9d811372010-06-23 21:44:572233 mime_type->SetStringFromUTF16(L"description", type_it->description);
[email protected]f7d48012010-05-06 08:17:052234
2235 ListValue* file_extensions = new ListValue();
2236 for (std::vector<std::string>::const_iterator ext_it =
2237 type_it->file_extensions.begin();
2238 ext_it != type_it->file_extensions.end();
2239 ++ext_it) {
2240 file_extensions->Append(new StringValue(*ext_it));
2241 }
2242 mime_type->Set(L"fileExtensions", file_extensions);
2243
2244 mime_types->Append(mime_type);
2245 }
2246 item->Set(L"mimeTypes", mime_types);
2247 items->Append(item);
2248 }
2249 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2250 return_value->Set(L"plugins", items); // return_value owns items.
2251
2252 base::JSONWriter::Write(return_value.get(), false, &json_return);
2253 AutomationMsg_SendJSONRequest::WriteReplyParams(
2254 reply_message, json_return, reply_return);
2255 Send(reply_message);
2256}
2257
2258// Sample json input:
2259// { "command": "EnablePlugin",
2260// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582261void AutomationProvider::EnablePlugin(Browser* browser,
2262 DictionaryValue* args,
[email protected]f7d48012010-05-06 08:17:052263 IPC::Message* reply_message) {
2264 std::string json_return = "{}";
2265 bool reply_return = true;
2266 FilePath::StringType path;
2267 if (!args->GetString(L"path", &path)) {
[email protected]7060bb292010-06-24 00:52:492268 json_return = JSONErrorString("path not specified.");
[email protected]f7d48012010-05-06 08:17:052269 reply_return = false;
2270 } else if (!NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(path))) {
2271 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2272 " for path %s.\"}", path.c_str());
2273 reply_return = false;
2274 }
2275
2276 AutomationMsg_SendJSONRequest::WriteReplyParams(
2277 reply_message, json_return, reply_return);
2278 Send(reply_message);
2279}
2280
2281// Sample json input:
2282// { "command": "DisablePlugin",
2283// "path": "/Library/Internet Plug-Ins/Flash Player.plugin" }
[email protected]53329582010-05-14 21:10:582284void AutomationProvider::DisablePlugin(Browser* browser,
2285 DictionaryValue* args,
2286 IPC::Message* reply_message) {
[email protected]f7d48012010-05-06 08:17:052287 std::string json_return = "{}";
2288 bool reply_return = true;
2289 FilePath::StringType path;
2290 if (!args->GetString(L"path", &path)) {
[email protected]7060bb292010-06-24 00:52:492291 json_return = JSONErrorString("path not specified.");
[email protected]f7d48012010-05-06 08:17:052292 reply_return = false;
2293 } else if (!NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path))) {
2294 json_return = StringPrintf("{\"error\": \"Could not enable plugin"
2295 " for path %s.\"}", path.c_str());
2296 reply_return = false;
2297 }
2298
2299 AutomationMsg_SendJSONRequest::WriteReplyParams(
2300 reply_message, json_return, reply_return);
2301 Send(reply_message);
2302}
2303
[email protected]7060bb292010-06-24 00:52:492304// Sample json input:
2305// { "command": "SaveTabContents",
2306// "tab_index": 0,
2307// "filename": <a full pathname> }
2308// Sample json output:
2309// {}
2310void AutomationProvider::SaveTabContents(Browser* browser,
2311 DictionaryValue* args,
2312 IPC::Message* reply_message) {
2313 std::string json_return;
2314 int tab_index = 0;
2315 FilePath::StringType filename;
2316 FilePath::StringType parent_directory;
2317 TabContents* tab_contents = NULL;
2318
2319 if (!args->GetInteger(L"tab_index", &tab_index) ||
2320 !args->GetString(L"filename", &filename)) {
2321 json_return = JSONErrorString("tab_index or filename param missing");
2322 } else {
2323 tab_contents = browser->GetTabContentsAt(tab_index);
2324 if (!tab_contents) {
2325 json_return = JSONErrorString("no tab at tab_index");
2326 }
2327 }
2328 if (tab_contents) {
2329 // We're doing a SAVE_AS_ONLY_HTML so the the directory path isn't
2330 // used. Nevertheless, SavePackage requires it be valid. Sigh.
2331 parent_directory = FilePath(filename).DirName().value();
2332 if (!tab_contents->SavePage(FilePath(filename), FilePath(parent_directory),
2333 SavePackage::SAVE_AS_ONLY_HTML)) {
2334 json_return = JSONErrorString("Could not initiate SavePage");
2335 } else {
2336 // The observer will delete itself when done.
2337 new SavePackageNotificationObserver(tab_contents->save_package(),
2338 this, reply_message);
2339 return;
2340 }
2341 }
2342
[email protected]93364da2010-06-29 18:03:442343 // If we get here, error.
[email protected]7060bb292010-06-24 00:52:492344 DCHECK(!json_return.empty());
2345 AutomationMsg_SendJSONRequest::WriteReplyParams(
2346 reply_message, json_return, false);
2347 Send(reply_message);
2348}
2349
[email protected]4d1929f12010-07-10 00:09:032350// Sample json input: { "command": "GetThemeInfo" }
2351// Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output.
2352void AutomationProvider::GetThemeInfo(Browser* browser,
2353 DictionaryValue* args,
2354 IPC::Message* reply_message) {
2355 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2356 Extension* theme = browser->profile()->GetTheme();
2357 if (theme) {
2358 return_value->SetString(L"name", theme->name());
2359 return_value->Set(L"images", theme->GetThemeImages()->DeepCopy());
2360 return_value->Set(L"colors", theme->GetThemeColors()->DeepCopy());
2361 return_value->Set(L"tints", theme->GetThemeTints()->DeepCopy());
2362 }
2363
2364 std::string json_return;
2365 base::JSONWriter::Write(return_value.get(), false, &json_return);
2366 AutomationMsg_SendJSONRequest::WriteReplyParams(
2367 reply_message, json_return, true);
2368 Send(reply_message);
2369}
2370
[email protected]55846ad842010-07-09 18:22:562371// Sample json input:
2372// { "command": "GetAutoFillProfile" }
2373// Refer to GetAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2374// json output.
2375void AutomationProvider::GetAutoFillProfile(Browser* browser,
2376 DictionaryValue* args,
2377 IPC::Message* reply_message) {
2378 std::string json_return;
2379 bool reply_return = true;
2380
2381 // Get the AutoFillProfiles currently in the database.
2382 int tab_index = 0;
2383 args->GetInteger(L"tab_index", &tab_index);
2384 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2385
2386 if (tab_contents) {
2387 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2388 ->GetPersonalDataManager();
2389 if (pdm) {
2390 std::vector<AutoFillProfile*> autofill_profiles = pdm->profiles();
2391 std::vector<CreditCard*> credit_cards = pdm->credit_cards();
2392
2393 ListValue* profiles = GetListFromAutoFillProfiles(autofill_profiles);
2394 ListValue* cards = GetListFromCreditCards(credit_cards);
2395
2396 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2397
2398 return_value->Set(L"profiles", profiles);
2399 return_value->Set(L"credit_cards", cards);
2400
2401 base::JSONWriter::Write(return_value.get(), false, &json_return);
2402 } else {
2403 json_return = JSONErrorString("No PersonalDataManager.");
2404 reply_return = false;
2405 }
2406 } else {
2407 json_return = JSONErrorString("No tab at that index.");
2408 reply_return = false;
2409 }
2410
2411 AutomationMsg_SendJSONRequest::WriteReplyParams(reply_message, json_return,
2412 reply_return);
2413 Send(reply_message);
2414}
2415
2416// Refer to FillAutoFillProfile() in chrome/test/pyautolib/pyauto.py for sample
2417// json input.
2418// Sample json output: {}
2419void AutomationProvider::FillAutoFillProfile(Browser* browser,
2420 DictionaryValue* args,
2421 IPC::Message* reply_message) {
2422 std::string json_return = "{}";
2423 bool reply_return = true;
2424
2425 ListValue* profiles = NULL;
2426 ListValue* cards = NULL;
2427 args->GetList(L"profiles", &profiles);
2428 args->GetList(L"credit_cards", &cards);
2429
2430 std::vector<AutoFillProfile> autofill_profiles;
2431 std::vector<CreditCard> credit_cards;
2432 // Create an AutoFillProfile for each of the dictionary profiles.
2433 if (profiles) {
2434 autofill_profiles = GetAutoFillProfilesFromList(*profiles, &json_return);
2435 }
2436 // Create a CreditCard for each of the dictionary values.
2437 if (cards) {
2438 credit_cards = GetCreditCardsFromList(*cards, &json_return);
2439 }
2440
2441 // Save the AutoFillProfiles.
2442 int tab_index = 0;
2443 args->GetInteger(L"tab_index", &tab_index);
2444 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2445
2446 if (tab_contents) {
2447 PersonalDataManager* pdm = tab_contents->profile()->GetOriginalProfile()
2448 ->GetPersonalDataManager();
2449 if (pdm) {
2450 pdm->OnAutoFillDialogApply(profiles? &autofill_profiles : NULL,
2451 cards? &credit_cards : NULL);
2452 } else {
2453 json_return = JSONErrorString("No PersonalDataManager.");
2454 reply_return = false;
2455 }
2456 } else {
2457 json_return = JSONErrorString("No tab at that index.");
2458 reply_return = false;
2459 }
2460 AutomationMsg_SendJSONRequest::WriteReplyParams(
2461 reply_message, json_return, reply_return);
2462 Send(reply_message);
2463}
2464
2465/* static */
2466ListValue* AutomationProvider::GetListFromAutoFillProfiles(
2467 std::vector<AutoFillProfile*> autofill_profiles) {
2468 ListValue* profiles = new ListValue;
2469
2470 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string
2471 = GetAutoFillFieldToStringMap();
2472
2473 // For each AutoFillProfile, transform it to a dictionary object to return.
2474 for (std::vector<AutoFillProfile*>::iterator it = autofill_profiles.begin();
2475 it != autofill_profiles.end(); ++it) {
2476 AutoFillProfile* profile = *it;
2477 DictionaryValue* profile_info = new DictionaryValue;
2478 profile_info->SetStringFromUTF16(L"label", profile->Label());
2479 // For each of the types, if it has a value, add it to the dictionary.
2480 for (std::map<AutoFillFieldType, std::wstring>::iterator
2481 type_it = autofill_type_to_string.begin();
2482 type_it != autofill_type_to_string.end(); ++type_it) {
2483 string16 value = profile->GetFieldText(AutoFillType(type_it->first));
2484 if (value.length()) { // If there was something stored for that value.
2485 profile_info->SetStringFromUTF16(type_it->second, value);
2486 }
2487 }
2488 profiles->Append(profile_info);
2489 }
2490 return profiles;
2491}
2492
2493/* static */
2494ListValue* AutomationProvider::GetListFromCreditCards(
2495 std::vector<CreditCard*> credit_cards) {
2496 ListValue* cards = new ListValue;
2497
2498 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2499 GetCreditCardFieldToStringMap();
2500
2501 // For each AutoFillProfile, transform it to a dictionary object to return.
2502 for (std::vector<CreditCard*>::iterator it = credit_cards.begin();
2503 it != credit_cards.end(); ++it) {
2504 CreditCard* card = *it;
2505 DictionaryValue* card_info = new DictionaryValue;
2506 card_info->SetStringFromUTF16(L"label", card->Label());
2507 // For each of the types, if it has a value, add it to the dictionary.
2508 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2509 credit_card_type_to_string.begin();
2510 type_it != credit_card_type_to_string.end(); ++type_it) {
2511 string16 value = card->GetFieldText(AutoFillType(type_it->first));
2512 // If there was something stored for that value.
2513 if (value.length()) {
2514 card_info->SetStringFromUTF16(type_it->second, value);
2515 }
2516 }
2517 cards->Append(card_info);
2518 }
2519 return cards;
2520}
2521
2522/* static */
2523std::vector<AutoFillProfile> AutomationProvider::GetAutoFillProfilesFromList(
2524 const ListValue& profiles, std::string* json_return) {
2525 std::vector<AutoFillProfile> autofill_profiles;
2526 DictionaryValue* profile_info = NULL;
2527 string16 profile_label;
2528 string16 current_value;
2529
2530 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string =
2531 GetAutoFillFieldToStringMap();
2532
2533 int num_profiles = profiles.GetSize();
2534 for (int i = 0; i < num_profiles; i++) {
2535 profiles.GetDictionary(i, &profile_info);
2536 profile_info->GetString("label", &profile_label);
2537 // Choose an id of 0 so that a unique id will be created.
2538 AutoFillProfile profile(profile_label, 0);
2539 // Loop through the possible profile types and add those provided.
2540 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2541 autofill_type_to_string.begin();
2542 type_it != autofill_type_to_string.end(); ++type_it) {
2543 if (profile_info->HasKey(type_it->second)) {
2544 if (profile_info->GetStringAsUTF16(type_it->second, &current_value)) {
2545 profile.SetInfo(AutoFillType(type_it->first), current_value);
2546 } else {
2547 *json_return = JSONErrorString("All values must be strings");
2548 break;
2549 }
2550 }
2551 }
2552 autofill_profiles.push_back(profile);
2553 }
2554 return autofill_profiles;
2555}
2556
2557/* static */
2558std::vector<CreditCard> AutomationProvider::GetCreditCardsFromList(
2559 const ListValue& cards, std::string* json_return) {
2560 std::vector<CreditCard> credit_cards;
2561 DictionaryValue* card_info = NULL;
2562 string16 card_label;
2563 string16 current_value;
2564
2565 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string =
2566 GetCreditCardFieldToStringMap();
2567
2568 int num_credit_cards = cards.GetSize();
2569 for (int i = 0; i < num_credit_cards; i++) {
2570 cards.GetDictionary(i, &card_info);
2571 card_info->GetString("label", &card_label);
2572 CreditCard card(card_label, 0);
2573 // Loop through the possible credit card fields and add those provided.
2574 for (std::map<AutoFillFieldType, std::wstring>::iterator type_it =
2575 credit_card_type_to_string.begin();
2576 type_it != credit_card_type_to_string.end(); ++type_it) {
2577 if (card_info->HasKey(type_it->second)) {
2578 if (card_info->GetStringAsUTF16(type_it->second, &current_value)) {
2579 card.SetInfo(AutoFillType(type_it->first), current_value);
2580 } else {
2581 *json_return = JSONErrorString("All values must be strings");
2582 break;
2583 }
2584 }
2585 }
2586 credit_cards.push_back(card);
2587 }
2588 return credit_cards;
2589}
2590
2591/* static */
2592std::map<AutoFillFieldType, std::wstring>
2593 AutomationProvider::GetAutoFillFieldToStringMap() {
2594 std::map<AutoFillFieldType, std::wstring> autofill_type_to_string;
2595 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2596 autofill_type_to_string[NAME_MIDDLE] = L"NAME_MIDDLE";
2597 autofill_type_to_string[NAME_LAST] = L"NAME_LAST";
2598 autofill_type_to_string[COMPANY_NAME] = L"COMPANY_NAME";
2599 autofill_type_to_string[EMAIL_ADDRESS] = L"EMAIL_ADDRESS";
2600 autofill_type_to_string[ADDRESS_HOME_LINE1] = L"ADDRESS_HOME_LINE1";
2601 autofill_type_to_string[ADDRESS_HOME_LINE2] = L"ADDRESS_HOME_LINE2";
2602 autofill_type_to_string[ADDRESS_HOME_CITY] = L"ADDRESS_HOME_CITY";
2603 autofill_type_to_string[ADDRESS_HOME_STATE] = L"ADDRESS_HOME_STATE";
2604 autofill_type_to_string[ADDRESS_HOME_ZIP] = L"ADDRESS_HOME_ZIP";
2605 autofill_type_to_string[ADDRESS_HOME_COUNTRY] = L"ADDRESS_HOME_COUNTRY";
2606 autofill_type_to_string[PHONE_HOME_NUMBER] = L"PHONE_HOME_NUMBER";
2607 autofill_type_to_string[PHONE_FAX_NUMBER] = L"PHONE_FAX_NUMBER";
2608 autofill_type_to_string[NAME_FIRST] = L"NAME_FIRST";
2609 return autofill_type_to_string;
2610}
2611
2612/* static */
2613std::map<AutoFillFieldType, std::wstring>
2614 AutomationProvider::GetCreditCardFieldToStringMap() {
2615 std::map<AutoFillFieldType, std::wstring> credit_card_type_to_string;
2616 credit_card_type_to_string[CREDIT_CARD_NAME] = L"CREDIT_CARD_NAME";
2617 credit_card_type_to_string[CREDIT_CARD_NUMBER] = L"CREDIT_CARD_NUMBER";
2618 credit_card_type_to_string[CREDIT_CARD_TYPE] = L"CREDIT_CARD_TYPE";
2619 credit_card_type_to_string[CREDIT_CARD_EXP_MONTH] = L"CREDIT_CARD_EXP_MONTH";
2620 credit_card_type_to_string[CREDIT_CARD_EXP_4_DIGIT_YEAR] =
2621 L"CREDIT_CARD_EXP_4_DIGIT_YEAR";
2622 return credit_card_type_to_string;
2623}
2624
[email protected]93364da2010-06-29 18:03:442625/* static */
2626std::string AutomationProvider::JSONErrorString(const std::string& err) {
2627 std::string prefix = "{\"error\": \"";
2628 std::string no_quote_err;
2629 std::string suffix = "\"}";
2630
2631 base::JsonDoubleQuote(err, false, &no_quote_err);
2632 return prefix + no_quote_err + suffix;
2633}
2634
[email protected]53329582010-05-14 21:10:582635void AutomationProvider::SendJSONRequest(int handle,
2636 std::string json_request,
2637 IPC::Message* reply_message) {
[email protected]59a611242010-04-02 02:24:042638 Browser* browser = NULL;
2639 std::string error_string;
2640 scoped_ptr<Value> values;
2641
2642 // Basic error checking.
2643 if (browser_tracker_->ContainsHandle(handle)) {
2644 browser = browser_tracker_->GetResource(handle);
2645 }
2646 if (!browser) {
2647 error_string = "no browser object";
2648 } else {
2649 base::JSONReader reader;
2650 std::string error;
[email protected]ba399672010-04-06 15:42:392651 values.reset(reader.ReadAndReturnError(json_request, true, NULL, &error));
[email protected]59a611242010-04-02 02:24:042652 if (!error.empty()) {
2653 error_string = error;
2654 }
2655 }
2656
2657 // Make sure input is a dict with a string command.
2658 std::string command;
2659 DictionaryValue* dict_value = NULL;
2660 if (error_string.empty()) {
2661 if (values->GetType() != Value::TYPE_DICTIONARY) {
2662 error_string = "not a dict or no command key in dict";
2663 } else {
2664 // Ownership remains with "values" variable.
2665 dict_value = static_cast<DictionaryValue*>(values.get());
2666 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
2667 error_string = "no command key in dict or not a string command";
2668 }
2669 }
2670 }
2671
[email protected]24e2b102010-04-29 17:56:472672 // Map json commands to their handlers.
2673 std::map<std::string, JsonHandler> handler_map;
[email protected]f7d48012010-05-06 08:17:052674 handler_map["DisablePlugin"] = &AutomationProvider::DisablePlugin;
2675 handler_map["EnablePlugin"] = &AutomationProvider::EnablePlugin;
2676 handler_map["GetPluginsInfo"] = &AutomationProvider::GetPluginsInfo;
2677
[email protected]a9ff2c02010-05-13 17:33:052678 handler_map["GetBrowserInfo"] = &AutomationProvider::GetBrowserInfo;
[email protected]24e2b102010-04-29 17:56:472679 handler_map["GetHistoryInfo"] = &AutomationProvider::GetHistoryInfo;
[email protected]bbe6aa02010-05-07 17:27:292680 handler_map["AddHistoryItem"] = &AutomationProvider::AddHistoryItem;
[email protected]f7d48012010-05-06 08:17:052681
[email protected]53329582010-05-14 21:10:582682 handler_map["GetOmniboxInfo"] = &AutomationProvider::GetOmniboxInfo;
2683 handler_map["SetOmniboxText"] = &AutomationProvider::SetOmniboxText;
2684 handler_map["OmniboxAcceptInput"] = &AutomationProvider::OmniboxAcceptInput;
2685 handler_map["OmniboxMovePopupSelection"] =
2686 &AutomationProvider::OmniboxMovePopupSelection;
2687
[email protected]24e2b102010-04-29 17:56:472688 handler_map["GetPrefsInfo"] = &AutomationProvider::GetPrefsInfo;
2689 handler_map["SetPrefs"] = &AutomationProvider::SetPrefs;
[email protected]f7d48012010-05-06 08:17:052690
[email protected]ef413ca2010-05-25 21:09:142691 handler_map["SetWindowDimensions"] = &AutomationProvider::SetWindowDimensions;
2692
[email protected]f7d48012010-05-06 08:17:052693 handler_map["GetDownloadsInfo"] = &AutomationProvider::GetDownloadsInfo;
[email protected]24e2b102010-04-29 17:56:472694 handler_map["WaitForAllDownloadsToComplete"] =
2695 &AutomationProvider::WaitForDownloadsToComplete;
2696
[email protected]a3cd5022010-06-16 18:25:292697 handler_map["GetInitialLoadTimes"] = &AutomationProvider::GetInitialLoadTimes;
2698
[email protected]7060bb292010-06-24 00:52:492699 handler_map["SaveTabContents"] = &AutomationProvider::SaveTabContents;
2700
[email protected]4d1929f12010-07-10 00:09:032701 // SetTheme() implemented using InstallExtension().
2702 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo;
2703
[email protected]55846ad842010-07-09 18:22:562704 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile;
2705 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile;
2706
[email protected]59a611242010-04-02 02:24:042707 if (error_string.empty()) {
[email protected]24e2b102010-04-29 17:56:472708 if (handler_map.find(std::string(command)) != handler_map.end()) {
[email protected]53329582010-05-14 21:10:582709 (this->*handler_map[command])(browser, dict_value, reply_message);
[email protected]59a611242010-04-02 02:24:042710 return;
2711 } else {
[email protected]24e2b102010-04-29 17:56:472712 error_string = "Unknown command. Options: ";
2713 for (std::map<std::string, JsonHandler>::const_iterator it =
2714 handler_map.begin(); it != handler_map.end(); ++it) {
2715 error_string += it->first + ", ";
2716 }
[email protected]59a611242010-04-02 02:24:042717 }
2718 }
2719
2720 // If we hit an error, return info.
[email protected]24e2b102010-04-29 17:56:472721 // Return a dict of {"error", "descriptive_string_for_error"}.
[email protected]59a611242010-04-02 02:24:042722 // Else return an empty dict.
2723 std::string json_string;
2724 bool success = true;
2725 if (!error_string.empty()) {
2726 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
2727 dict->SetString(L"error", error_string);
2728 base::JSONWriter::Write(dict.get(), false, &json_string);
2729 success = false;
2730 } else {
2731 json_string = "{}";
2732 }
2733 AutomationMsg_SendJSONRequest::WriteReplyParams(
2734 reply_message, json_string, success);
2735 Send(reply_message);
2736}
2737
initial.commit09911bf2008-07-26 23:55:292738void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562739 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342740 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2741 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562742 DCHECK(reply_message_ == NULL);
2743 reply_message_ = reply_message;
2744
[email protected]d9f9b792009-06-24 13:17:122745 DevToolsManager::GetInstance()->InspectElement(
2746 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292747 } else {
[email protected]71f65dd2009-02-11 19:14:562748 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2749 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292750 }
2751}
2752
2753void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092754 if (reply_message_) {
2755 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2756 num_resources);
2757 Send(reply_message_);
2758 reply_message_ = NULL;
2759 }
initial.commit09911bf2008-07-26 23:55:292760}
2761
[email protected]a7eee32f2009-05-22 18:08:172762class SetProxyConfigTask : public Task {
2763 public:
[email protected]be180c802009-10-23 06:33:312764 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
2765 const std::string& new_proxy_config)
[email protected]2aa336e2010-04-06 21:05:252766 : request_context_getter_(request_context_getter),
2767 proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:172768 virtual void Run() {
2769 // First, deserialize the JSON string. If this fails, log and bail.
2770 JSONStringValueSerializer deserializer(proxy_config_);
[email protected]ba399672010-04-06 15:42:392771 std::string error_msg;
2772 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg));
[email protected]a7eee32f2009-05-22 18:08:172773 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2774 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
[email protected]ba399672010-04-06 15:42:392775 << error_msg;
[email protected]a7eee32f2009-05-22 18:08:172776 return;
2777 }
2778
2779 scoped_ptr<DictionaryValue> dict(
2780 static_cast<DictionaryValue*>(root.release()));
2781 // Now put together a proxy configuration from the deserialized string.
2782 net::ProxyConfig pc;
2783 PopulateProxyConfig(*dict.get(), &pc);
2784
[email protected]be180c802009-10-23 06:33:312785 net::ProxyService* proxy_service =
2786 request_context_getter_->GetURLRequestContext()->proxy_service();
2787 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:172788 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2789 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:312790 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:172791 }
2792
2793 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2794 DCHECK(pc);
2795 bool no_proxy = false;
2796 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2797 // Make no changes to the ProxyConfig.
2798 return;
2799 }
2800 bool auto_config;
2801 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:482802 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:172803 }
2804 std::string pac_url;
2805 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:482806 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:172807 }
2808 std::string proxy_bypass_list;
2809 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:482810 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:172811 }
2812 std::string proxy_server;
2813 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:482814 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:172815 }
2816 }
2817
2818 private:
[email protected]be180c802009-10-23 06:33:312819 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:172820 std::string proxy_config_;
2821};
2822
2823
2824void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:312825 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
2826 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:172827 FilePath user_data_dir;
2828 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2829 ProfileManager* profile_manager = g_browser_process->profile_manager();
2830 DCHECK(profile_manager);
2831 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2832 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:312833 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:172834 }
[email protected]be180c802009-10-23 06:33:312835 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:172836
[email protected]fae20792009-10-28 20:31:582837 ChromeThread::PostTask(
2838 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:312839 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:172840}
2841
[email protected]4f3dc372009-02-24 00:10:292842void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:332843 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:292844 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292845 if (tab_tracker_->ContainsHandle(handle)) {
2846 NavigationController* tab = tab_tracker_->GetResource(handle);
2847 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2848 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:332849 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:292850 }
initial.commit09911bf2008-07-26 23:55:292851}
2852
[email protected]6a5670d22009-10-27 16:21:342853void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:142854 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:532855 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
2856 reply_message);
2857}
2858
2859void AutomationProvider::OpenNewBrowserWindowOfType(
2860 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:142861 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292862 // We may have no current browser windows open so don't rely on
2863 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:532864 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
2865 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:052866 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152867 if (show)
[email protected]15952e462008-11-14 00:29:052868 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292869}
2870
[email protected]71f65dd2009-02-11 19:14:562871void AutomationProvider::GetWindowForBrowser(int browser_handle,
2872 bool* success,
2873 int* handle) {
2874 *success = false;
2875 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292876
2877 if (browser_tracker_->ContainsHandle(browser_handle)) {
2878 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202879 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292880 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202881 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562882 *success = true;
initial.commit09911bf2008-07-26 23:55:292883 }
initial.commit09911bf2008-07-26 23:55:292884}
2885
2886void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562887 int browser_handle,
2888 bool* success,
2889 int* autocomplete_edit_handle) {
2890 *success = false;
2891 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292892
2893 if (browser_tracker_->ContainsHandle(browser_handle)) {
2894 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062895 LocationBar* loc_bar = browser->window()->GetLocationBar();
2896 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292897 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562898 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2899 *success = true;
initial.commit09911bf2008-07-26 23:55:292900 }
initial.commit09911bf2008-07-26 23:55:292901}
initial.commit09911bf2008-07-26 23:55:292902
[email protected]71f65dd2009-02-11 19:14:562903void AutomationProvider::ShowInterstitialPage(int tab_handle,
2904 const std::string& html_text,
2905 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292906 if (tab_tracker_->ContainsHandle(tab_handle)) {
2907 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112908 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402909
[email protected]7dad3d5f2010-03-04 00:27:012910 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:402911 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342912 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402913 GURL("about:interstitial"),
2914 html_text);
2915 interstitial->Show();
2916 return;
initial.commit09911bf2008-07-26 23:55:292917 }
[email protected]71f65dd2009-02-11 19:14:562918
[email protected]457f5cf2009-08-18 16:37:522919 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
2920 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:562921 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292922}
2923
[email protected]71f65dd2009-02-11 19:14:562924void AutomationProvider::HideInterstitialPage(int tab_handle,
2925 bool* success) {
2926 *success = false;
[email protected]57c6a652009-05-04 07:58:342927 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2928 if (tab_contents && tab_contents->interstitial_page()) {
2929 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562930 *success = true;
initial.commit09911bf2008-07-26 23:55:292931 }
initial.commit09911bf2008-07-26 23:55:292932}
2933
[email protected]71f65dd2009-02-11 19:14:562934void AutomationProvider::CloseTab(int tab_handle,
2935 bool wait_until_closed,
2936 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292937 if (tab_tracker_->ContainsHandle(tab_handle)) {
2938 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2939 int index;
2940 Browser* browser = Browser::GetBrowserForController(controller, &index);
2941 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142942 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112943 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452944 return;
initial.commit09911bf2008-07-26 23:55:292945 }
[email protected]de246f52009-02-25 18:25:452946
2947 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:552948 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292949}
2950
[email protected]71f65dd2009-02-11 19:14:562951void AutomationProvider::CloseBrowser(int browser_handle,
2952 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292953 if (browser_tracker_->ContainsHandle(browser_handle)) {
2954 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562955 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562956 reply_message);
[email protected]f3e99e32008-07-30 04:48:392957 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292958 } else {
2959 NOTREACHED();
2960 }
2961}
2962
[email protected]71f65dd2009-02-11 19:14:562963void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2964 if (browser_tracker_->ContainsHandle(browser_handle)) {
2965 Browser* browser = browser_tracker_->GetResource(browser_handle);
2966 browser->window()->Close();
2967 } else {
2968 NOTREACHED();
2969 }
2970}
2971
[email protected]71f65dd2009-02-11 19:14:562972void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2973 IPC::Message* reply_message) {
2974 if (tab_tracker_->ContainsHandle(tab_handle)) {
2975 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2976 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:142977 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:562978 }
2979}
2980
[email protected]71f65dd2009-02-11 19:14:562981void AutomationProvider::GetSecurityState(int handle, bool* success,
2982 SecurityStyle* security_style,
2983 int* ssl_cert_status,
[email protected]b4e75c12010-05-18 18:28:482984 int* insecure_content_status) {
initial.commit09911bf2008-07-26 23:55:292985 if (tab_tracker_->ContainsHandle(handle)) {
2986 NavigationController* tab = tab_tracker_->GetResource(handle);
2987 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562988 *success = true;
2989 *security_style = entry->ssl().security_style();
2990 *ssl_cert_status = entry->ssl().cert_status();
[email protected]b4e75c12010-05-18 18:28:482991 *insecure_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292992 } else {
[email protected]71f65dd2009-02-11 19:14:562993 *success = false;
2994 *security_style = SECURITY_STYLE_UNKNOWN;
2995 *ssl_cert_status = 0;
[email protected]b4e75c12010-05-18 18:28:482996 *insecure_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292997 }
2998}
2999
[email protected]71f65dd2009-02-11 19:14:563000void AutomationProvider::GetPageType(int handle, bool* success,
3001 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:293002 if (tab_tracker_->ContainsHandle(handle)) {
3003 NavigationController* tab = tab_tracker_->GetResource(handle);
3004 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:563005 *page_type = entry->page_type();
3006 *success = true;
initial.commit09911bf2008-07-26 23:55:293007 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:343008 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:563009 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:343010 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:563011 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293012 } else {
[email protected]71f65dd2009-02-11 19:14:563013 *success = false;
3014 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:293015 }
3016}
3017
[email protected]84abba62009-10-07 17:01:443018void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
3019 int* duration_ms) {
3020 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
3021 event_name);
3022}
3023
[email protected]71f65dd2009-02-11 19:14:563024void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
3025 IPC::Message* reply_message) {
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]1e5645ff2008-08-27 18:09:073029 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:403030 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:473031 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:343032 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:293033 if (ssl_blocking_page) {
3034 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:013035 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:563036 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:293037 return;
3038 }
3039 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:523040 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3041 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:563042 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293043 return;
3044 }
3045 }
3046 }
3047 // We failed.
[email protected]457f5cf2009-08-18 16:37:523048 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
3049 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:563050 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293051}
3052
[email protected]71f65dd2009-02-11 19:14:563053void AutomationProvider::BringBrowserToFront(int browser_handle,
3054 bool* success) {
initial.commit09911bf2008-07-26 23:55:293055 if (browser_tracker_->ContainsHandle(browser_handle)) {
3056 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:063057 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:563058 *success = true;
initial.commit09911bf2008-07-26 23:55:293059 } else {
[email protected]71f65dd2009-02-11 19:14:563060 *success = false;
initial.commit09911bf2008-07-26 23:55:293061 }
3062}
3063
[email protected]71f65dd2009-02-11 19:14:563064void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
3065 int message_num,
3066 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:293067 if (browser_tracker_->ContainsHandle(browser_handle)) {
3068 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:563069 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:143070 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:293071 } else {
[email protected]71f65dd2009-02-11 19:14:563072 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:293073 }
3074}
3075
[email protected]71f65dd2009-02-11 19:14:563076void AutomationProvider::PrintNow(int tab_handle,
3077 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:573078 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:343079 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3080 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:293081 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:573082 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:143083 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:343084 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:573085 return;
initial.commit09911bf2008-07-26 23:55:293086 }
[email protected]71f65dd2009-02-11 19:14:563087 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
3088 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:293089}
[email protected]d301c952009-07-13 15:02:413090
[email protected]71f65dd2009-02-11 19:14:563091void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:113092 const FilePath& file_name,
3093 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:563094 int type,
3095 bool* success) {
initial.commit09911bf2008-07-26 23:55:293096 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:563097 *success = false;
initial.commit09911bf2008-07-26 23:55:293098 return;
3099 }
3100
3101 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3102 Browser* browser = FindAndActivateTab(nav);
3103 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:143104 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:563105 *success = false;
initial.commit09911bf2008-07-26 23:55:293106 return;
3107 }
3108
initial.commit09911bf2008-07-26 23:55:293109 SavePackage::SavePackageType save_type =
3110 static_cast<SavePackage::SavePackageType>(type);
3111 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
3112 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:343113 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:293114
[email protected]71f65dd2009-02-11 19:14:563115 *success = true;
initial.commit09911bf2008-07-26 23:55:293116}
3117
[email protected]71f65dd2009-02-11 19:14:563118void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
3119 bool* success,
3120 std::wstring* text) {
3121 *success = false;
initial.commit09911bf2008-07-26 23:55:293122 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563123 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:523124 GetText();
[email protected]71f65dd2009-02-11 19:14:563125 *success = true;
initial.commit09911bf2008-07-26 23:55:293126 }
initial.commit09911bf2008-07-26 23:55:293127}
3128
[email protected]71f65dd2009-02-11 19:14:563129void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
3130 const std::wstring& text,
3131 bool* success) {
3132 *success = false;
initial.commit09911bf2008-07-26 23:55:293133 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:523134 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
3135 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:563136 *success = true;
initial.commit09911bf2008-07-26 23:55:293137 }
initial.commit09911bf2008-07-26 23:55:293138}
3139
3140void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:563141 int autocomplete_edit_handle,
3142 bool* success,
3143 std::vector<AutocompleteMatchData>* matches) {
3144 *success = false;
initial.commit09911bf2008-07-26 23:55:293145 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:273146 const AutocompleteResult& result = autocomplete_edit_tracker_->
3147 GetResource(autocomplete_edit_handle)->model()->result();
3148 for (AutocompleteResult::const_iterator i = result.begin();
3149 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:563150 matches->push_back(AutocompleteMatchData(*i));
3151 *success = true;
initial.commit09911bf2008-07-26 23:55:293152 }
initial.commit09911bf2008-07-26 23:55:293153}
3154
3155void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:563156 int autocomplete_edit_handle,
3157 bool* success,
3158 bool* query_in_progress) {
3159 *success = false;
3160 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:293161 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:563162 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:523163 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:563164 *success = true;
initial.commit09911bf2008-07-26 23:55:293165 }
initial.commit09911bf2008-07-26 23:55:293166}
3167
[email protected]63514af2010-03-30 17:17:233168#if !defined(OS_MACOSX)
[email protected]f7a68432009-07-29 23:18:193169
[email protected]5ae5bed2009-08-21 18:52:443170#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:393171
[email protected]57c6a652009-05-04 07:58:343172TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:573173 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:573174 if (tab_tracker_->ContainsHandle(handle)) {
3175 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:343176 if (tab)
3177 *tab = nav_controller;
3178 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:573179 }
[email protected]57c6a652009-05-04 07:58:343180 return NULL;
[email protected]20e93d12008-08-28 16:31:573181}
3182
initial.commit09911bf2008-07-26 23:55:293183TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
3184 : AutomationProvider(profile) {
3185 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:143186 registrar_.Add(this, NotificationType::SESSION_END,
3187 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:293188}
3189
3190TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:293191 BrowserList::RemoveObserver(this);
3192}
3193
3194void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:143195 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:293196 AutomationProvider::OnChannelError();
3197}
3198
3199void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
3200 // For backwards compatibility with the testing automation interface, we
3201 // want the automation provider (and hence the process) to go away when the
3202 // last browser goes away.
3203 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:293204 // If you change this, update Observer for NotificationType::SESSION_END
3205 // below.
[email protected]295039bd2008-08-15 04:32:573206 MessageLoop::current()->PostTask(FROM_HERE,
3207 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:293208 }
3209}
3210
3211void TestingAutomationProvider::Observe(NotificationType type,
3212 const NotificationSource& source,
3213 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:563214 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:293215 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
3216 // before the task runs resulting in this object not being deleted. This
3217 // Release balance out the Release scheduled by OnBrowserRemoving.
3218 Release();
3219}
[email protected]295039bd2008-08-15 04:32:573220
3221void TestingAutomationProvider::OnRemoveProvider() {
3222 AutomationProviderList::GetInstance()->RemoveProvider(this);
3223}
[email protected]8a3422c92008-09-24 17:42:423224
[email protected]816633a2009-11-11 21:48:183225void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:563226 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:423227 if (tab_tracker_->ContainsHandle(handle)) {
3228 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:343229 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:113230 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423231 }
[email protected]8a3422c92008-09-24 17:42:423232}
3233
[email protected]816633a2009-11-11 21:48:183234void AutomationProvider::ClickInfoBarAccept(int handle,
3235 int info_bar_index,
3236 bool wait_for_navigation,
3237 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:423238 bool success = false;
3239 if (tab_tracker_->ContainsHandle(handle)) {
3240 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3241 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:113242 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:423243 if (info_bar_index >= 0 && info_bar_index < count) {
3244 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:013245 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:423246 }
[email protected]eb9ba192008-12-02 02:41:343247 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:113248 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:343249 info_bar_index);
3250 if (delegate->AsConfirmInfoBarDelegate())
3251 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:423252 success = true;
3253 }
3254 }
[email protected]4f3dc372009-02-24 00:10:293255 }
[email protected]58f622a62009-10-04 01:17:553256
3257 // This "!wait_for_navigation || !success condition" logic looks suspicious.
3258 // It will send a failure message when success is true but
3259 // |wait_for_navigation| is false.
3260 // TODO(phajdan.jr): investgate whether the reply param (currently
3261 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:423262 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:183263 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:523264 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:423265}
3266
[email protected]71f65dd2009-02-11 19:14:563267void AutomationProvider::GetLastNavigationTime(int handle,
3268 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:423269 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:563270 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:423271}
3272
[email protected]71f65dd2009-02-11 19:14:563273void AutomationProvider::WaitForNavigation(int handle,
3274 int64 last_navigation_time,
3275 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:253276 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:423277 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:253278
[email protected]8a3422c92008-09-24 17:42:423279 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:563280 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:523281 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
3282 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:553283 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:293284 return;
[email protected]8a3422c92008-09-24 17:42:423285 }
3286
[email protected]7dad3d5f2010-03-04 00:27:013287 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:423288}
3289
[email protected]71f65dd2009-02-11 19:14:563290void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163291 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563292 int value,
3293 bool* success) {
3294 *success = false;
[email protected]8a3422c92008-09-24 17:42:423295 if (browser_tracker_->ContainsHandle(handle)) {
3296 Browser* browser = browser_tracker_->GetResource(handle);
3297 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563298 *success = true;
[email protected]8a3422c92008-09-24 17:42:423299 }
[email protected]8a3422c92008-09-24 17:42:423300}
[email protected]97fa6ce32008-12-19 01:48:163301
[email protected]71f65dd2009-02-11 19:14:563302void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163303 const std::wstring& name,
[email protected]ddd231e2010-06-29 20:35:193304 const std::string& value,
[email protected]71f65dd2009-02-11 19:14:563305 bool* success) {
3306 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163307 if (browser_tracker_->ContainsHandle(handle)) {
3308 Browser* browser = browser_tracker_->GetResource(handle);
3309 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563310 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163311 }
[email protected]97fa6ce32008-12-19 01:48:163312}
3313
[email protected]71f65dd2009-02-11 19:14:563314void AutomationProvider::GetBooleanPreference(int handle,
3315 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:363316 bool* success,
3317 bool* value) {
[email protected]71f65dd2009-02-11 19:14:563318 *success = false;
3319 *value = false;
[email protected]97fa6ce32008-12-19 01:48:163320 if (browser_tracker_->ContainsHandle(handle)) {
3321 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:563322 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
3323 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163324 }
[email protected]97fa6ce32008-12-19 01:48:163325}
3326
[email protected]71f65dd2009-02-11 19:14:563327void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:163328 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:563329 bool value,
3330 bool* success) {
3331 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163332 if (browser_tracker_->ContainsHandle(handle)) {
3333 Browser* browser = browser_tracker_->GetResource(handle);
3334 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:563335 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163336 }
[email protected]97fa6ce32008-12-19 01:48:163337}
3338
3339// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563340void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:403341 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:163342 if (tab_tracker_->ContainsHandle(tab_handle)) {
3343 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
3344 Browser* browser = FindAndActivateTab(nav);
3345 DCHECK(browser);
3346
[email protected]57c6a652009-05-04 07:58:343347 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
3348 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:163349 }
[email protected]97fa6ce32008-12-19 01:48:163350}
3351
[email protected]b8f48d12009-11-09 20:14:363352// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:563353void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:403354 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:563355 bool* success) {
3356 *success = false;
[email protected]97fa6ce32008-12-19 01:48:163357 if (tab_tracker_->ContainsHandle(tab_handle)) {
3358 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:193359 if (!nav)
3360 return;
[email protected]97fa6ce32008-12-19 01:48:163361 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:163362
[email protected]2f2afba2010-04-01 01:53:193363 // If the browser has UI, simulate what a user would do.
3364 // Activate the tab and then click the encoding menu.
3365 if (browser &&
3366 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:163367 int selected_encoding_id =
3368 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
3369 if (selected_encoding_id) {
3370 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:563371 *success = true;
[email protected]97fa6ce32008-12-19 01:48:163372 }
[email protected]2f2afba2010-04-01 01:53:193373 } else {
3374 // There is no UI, Chrome probably runs as Chrome-Frame mode.
3375 // Try to get TabContents and call its override_encoding method.
3376 TabContents* contents = nav->tab_contents();
3377 if (!contents)
3378 return;
3379 const std::string selected_encoding =
3380 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
3381 if (selected_encoding.empty())
3382 return;
3383 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:163384 }
3385 }
[email protected]97fa6ce32008-12-19 01:48:163386}
[email protected]5bcdb312009-01-07 21:43:203387
[email protected]4d434a1a2009-02-11 21:06:573388void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:203389 SavePackage::SetShouldPromptUser(should_prompt);
3390}
[email protected]87eab222009-03-13 00:47:453391
[email protected]66ba4932009-06-04 19:22:133392void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
3393 *count = -1; // -1 is the error code
3394 if (tab_tracker_->ContainsHandle(handle)) {
3395 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3396 TabContents* tab_contents = nav_controller->tab_contents();
3397 if (tab_contents) {
3398 BlockedPopupContainer* container =
3399 tab_contents->blocked_popup_container();
3400 if (container) {
3401 *count = static_cast<int>(container->GetBlockedPopupCount());
3402 } else {
3403 // If we don't have a container, we don't have any blocked popups to
3404 // contain!
3405 *count = 0;
3406 }
3407 }
3408 }
3409}
[email protected]f7a68432009-07-29 23:18:193410
3411void AutomationProvider::SelectAll(int tab_handle) {
3412 RenderViewHost* view = GetViewForTab(tab_handle);
3413 if (!view) {
3414 NOTREACHED();
3415 return;
3416 }
3417
3418 view->SelectAll();
3419}
3420
3421void AutomationProvider::Cut(int tab_handle) {
3422 RenderViewHost* view = GetViewForTab(tab_handle);
3423 if (!view) {
3424 NOTREACHED();
3425 return;
3426 }
3427
3428 view->Cut();
3429}
3430
3431void AutomationProvider::Copy(int tab_handle) {
3432 RenderViewHost* view = GetViewForTab(tab_handle);
3433 if (!view) {
3434 NOTREACHED();
3435 return;
3436 }
3437
3438 view->Copy();
3439}
3440
3441void AutomationProvider::Paste(int tab_handle) {
3442 RenderViewHost* view = GetViewForTab(tab_handle);
3443 if (!view) {
3444 NOTREACHED();
3445 return;
3446 }
3447
3448 view->Paste();
3449}
3450
3451void AutomationProvider::ReloadAsync(int tab_handle) {
3452 if (tab_tracker_->ContainsHandle(tab_handle)) {
3453 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3454 if (!tab) {
3455 NOTREACHED();
3456 return;
3457 }
3458
[email protected]106a0812010-03-18 00:15:123459 const bool check_for_repost = true;
3460 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:193461 }
3462}
3463
3464void AutomationProvider::StopAsync(int tab_handle) {
3465 RenderViewHost* view = GetViewForTab(tab_handle);
3466 if (!view) {
[email protected]8b2b3312009-09-14 18:38:363467 // We tolerate StopAsync being called even before a view has been created.
3468 // So just log a warning instead of a NOTREACHED().
3469 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:193470 return;
3471 }
3472
3473 view->Stop();
3474}
3475
[email protected]1bb5f892009-10-06 01:44:573476void AutomationProvider::OnSetPageFontSize(int tab_handle,
3477 int font_size) {
3478 AutomationPageFontSize automation_font_size =
3479 static_cast<AutomationPageFontSize>(font_size);
3480
3481 if (automation_font_size < SMALLEST_FONT ||
3482 automation_font_size > LARGEST_FONT) {
3483 DLOG(ERROR) << "Invalid font size specified : "
3484 << font_size;
3485 return;
3486 }
3487
3488 if (tab_tracker_->ContainsHandle(tab_handle)) {
3489 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3490 DCHECK(tab != NULL);
3491 if (tab && tab->tab_contents()) {
3492 DCHECK(tab->tab_contents()->profile() != NULL);
3493 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
3494 prefs::kWebKitDefaultFontSize, font_size);
3495 }
3496 }
3497}
3498
[email protected]bc73b4e52010-03-26 04:16:203499void AutomationProvider::RemoveBrowsingData(int remove_mask) {
3500 BrowsingDataRemover* remover;
3501 remover = new BrowsingDataRemover(profile(),
3502 BrowsingDataRemover::EVERYTHING, // All time periods.
3503 base::Time());
3504 remover->Remove(remove_mask);
3505 // BrowsingDataRemover deletes itself.
3506}
[email protected]1bb5f892009-10-06 01:44:573507
[email protected]2949e90d2009-08-21 15:32:523508void AutomationProvider::WaitForBrowserWindowCountToBecome(
3509 int target_count, IPC::Message* reply_message) {
3510 if (static_cast<int>(BrowserList::size()) == target_count) {
3511 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
3512 reply_message, true);
3513 Send(reply_message);
3514 return;
3515 }
3516
3517 // Set up an observer (it will delete itself).
3518 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
3519}
3520
3521void AutomationProvider::WaitForAppModalDialogToBeShown(
3522 IPC::Message* reply_message) {
3523 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
3524 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
3525 reply_message, true);
3526 Send(reply_message);
3527 return;
3528 }
3529
3530 // Set up an observer (it will delete itself).
3531 new AppModalDialogShownObserver(this, reply_message);
3532}
3533
[email protected]1126a1d32009-08-26 15:39:263534void AutomationProvider::GoBackBlockUntilNavigationsComplete(
3535 int handle, int number_of_navigations, IPC::Message* reply_message) {
3536 if (tab_tracker_->ContainsHandle(handle)) {
3537 NavigationController* tab = tab_tracker_->GetResource(handle);
3538 Browser* browser = FindAndActivateTab(tab);
3539 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:013540 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3541 false);
[email protected]1126a1d32009-08-26 15:39:263542 browser->GoBack(CURRENT_TAB);
3543 return;
3544 }
3545 }
3546
3547 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
3548 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3549 Send(reply_message);
3550}
3551
3552void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
3553 int handle, int number_of_navigations, IPC::Message* reply_message) {
3554 if (tab_tracker_->ContainsHandle(handle)) {
3555 NavigationController* tab = tab_tracker_->GetResource(handle);
3556 Browser* browser = FindAndActivateTab(tab);
3557 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:013558 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
3559 false);
[email protected]1126a1d32009-08-26 15:39:263560 browser->GoForward(CURRENT_TAB);
3561 return;
3562 }
3563 }
3564
3565 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
3566 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
3567 Send(reply_message);
3568}
3569
[email protected]f7a68432009-07-29 23:18:193570RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
3571 if (tab_tracker_->ContainsHandle(tab_handle)) {
3572 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
3573 if (!tab) {
3574 NOTREACHED();
3575 return NULL;
3576 }
3577
3578 TabContents* tab_contents = tab->tab_contents();
3579 if (!tab_contents) {
3580 NOTREACHED();
3581 return NULL;
3582 }
3583
3584 RenderViewHost* view_host = tab_contents->render_view_host();
3585 return view_host;
3586 }
3587
3588 return NULL;
3589}
[email protected]675595f2009-08-26 22:32:043590
3591void AutomationProvider::GetBrowserForWindow(int window_handle,
3592 bool* success,
3593 int* browser_handle) {
3594 *success = false;
3595 *browser_handle = 0;
3596
3597 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
3598 if (!window)
3599 return;
3600
3601 BrowserList::const_iterator iter = BrowserList::begin();
3602 for (;iter != BrowserList::end(); ++iter) {
3603 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
3604 if (window == this_window) {
3605 // Add() returns the existing handle for the resource if any.
3606 *browser_handle = browser_tracker_->Add(*iter);
3607 *success = true;
3608 return;
3609 }
3610 }
3611}
[email protected]d11c8e92009-10-20 23:26:403612
3613void AutomationProvider::InstallExtension(const FilePath& crx_path,
3614 IPC::Message* reply_message) {
3615 ExtensionsService* service = profile_->GetExtensionsService();
3616 if (service) {
3617 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193618 new ExtensionInstallNotificationObserver(this,
3619 AutomationMsg_InstallExtension::ID,
3620 reply_message);
[email protected]d11c8e92009-10-20 23:26:403621
3622 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:163623 scoped_refptr<CrxInstaller> installer(
3624 new CrxInstaller(install_dir,
3625 service,
3626 NULL)); // silent install, no UI
3627 installer->set_allow_privilege_increase(true);
3628 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:403629 } else {
3630 AutomationMsg_InstallExtension::WriteReplyParams(
3631 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3632 Send(reply_message);
3633 }
3634}
3635
3636void AutomationProvider::LoadExpandedExtension(
3637 const FilePath& extension_dir,
3638 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:383639 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:403640 // The observer will delete itself when done.
[email protected]790788ac2010-04-06 17:52:193641 new ExtensionInstallNotificationObserver(
3642 this,
3643 AutomationMsg_LoadExpandedExtension::ID,
3644 reply_message);
[email protected]d11c8e92009-10-20 23:26:403645
3646 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:403647 } else {
3648 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
3649 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
3650 Send(reply_message);
3651 }
3652}
[email protected]673fd2c02010-02-04 23:10:003653
[email protected]a1e62d12010-03-16 02:18:433654void AutomationProvider::GetEnabledExtensions(
3655 std::vector<FilePath>* result) {
3656 ExtensionsService* service = profile_->GetExtensionsService();
3657 DCHECK(service);
3658 if (service->extensions_enabled()) {
3659 const ExtensionList* extensions = service->extensions();
3660 DCHECK(extensions);
3661 for (size_t i = 0; i < extensions->size(); ++i) {
3662 Extension* extension = (*extensions)[i];
3663 DCHECK(extension);
[email protected]472f099b2010-05-27 17:07:123664 if (extension->location() == Extension::INTERNAL ||
3665 extension->location() == Extension::LOAD) {
[email protected]237f281672010-03-20 12:37:073666 result->push_back(extension->path());
3667 }
[email protected]a1e62d12010-03-16 02:18:433668 }
3669 }
3670}
3671
[email protected]790788ac2010-04-06 17:52:193672void AutomationProvider::WaitForExtensionTestResult(
3673 IPC::Message* reply_message) {
3674 DCHECK(reply_message_ == NULL);
3675 reply_message_ = reply_message;
3676 // Call MaybeSendResult, because the result might have come in before
3677 // we were waiting on it.
3678 extension_test_result_observer_->MaybeSendResult();
3679}
3680
3681void AutomationProvider::InstallExtensionAndGetHandle(
[email protected]d7e5525d2010-04-20 14:37:093682 const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) {
[email protected]790788ac2010-04-06 17:52:193683 ExtensionsService* service = profile_->GetExtensionsService();
3684 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3685 if (service && manager) {
3686 // The observer will delete itself when done.
3687 new ExtensionReadyNotificationObserver(
3688 manager,
3689 this,
3690 AutomationMsg_InstallExtensionAndGetHandle::ID,
3691 reply_message);
3692
[email protected]d7e5525d2010-04-20 14:37:093693 ExtensionInstallUI* client =
3694 (with_ui ? new ExtensionInstallUI(profile_) : NULL);
[email protected]790788ac2010-04-06 17:52:193695 scoped_refptr<CrxInstaller> installer(
3696 new CrxInstaller(service->install_directory(),
3697 service,
[email protected]d7e5525d2010-04-20 14:37:093698 client));
[email protected]790788ac2010-04-06 17:52:193699 installer->set_allow_privilege_increase(true);
3700 installer->InstallCrx(crx_path);
3701 } else {
3702 AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams(
3703 reply_message, 0);
3704 Send(reply_message);
3705 }
3706}
3707
3708void AutomationProvider::UninstallExtension(int extension_handle,
3709 bool* success) {
3710 *success = false;
3711 Extension* extension = GetExtension(extension_handle);
3712 ExtensionsService* service = profile_->GetExtensionsService();
3713 if (extension && service) {
3714 ExtensionUnloadNotificationObserver observer;
3715 service->UninstallExtension(extension->id(), false);
3716 // The extension unload notification should have been sent synchronously
3717 // with the uninstall. Just to be safe, check that it was received.
3718 *success = observer.did_receive_unload_notification();
3719 }
3720}
3721
3722void AutomationProvider::EnableExtension(int extension_handle,
3723 IPC::Message* reply_message) {
3724 Extension* extension = GetDisabledExtension(extension_handle);
3725 ExtensionsService* service = profile_->GetExtensionsService();
3726 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
3727 // Only enable if this extension is disabled.
3728 if (extension && service && manager) {
3729 // The observer will delete itself when done.
3730 new ExtensionReadyNotificationObserver(
3731 manager,
3732 this,
3733 AutomationMsg_EnableExtension::ID,
3734 reply_message);
3735 service->EnableExtension(extension->id());
3736 } else {
3737 AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false);
3738 Send(reply_message);
3739 }
3740}
3741
3742void AutomationProvider::DisableExtension(int extension_handle,
3743 bool* success) {
3744 *success = false;
3745 Extension* extension = GetEnabledExtension(extension_handle);
3746 ExtensionsService* service = profile_->GetExtensionsService();
3747 if (extension && service) {
3748 ExtensionUnloadNotificationObserver observer;
3749 service->DisableExtension(extension->id());
3750 // The extension unload notification should have been sent synchronously
3751 // with the disable. Just to be safe, check that it was received.
3752 *success = observer.did_receive_unload_notification();
3753 }
3754}
3755
3756void AutomationProvider::ExecuteExtensionActionInActiveTabAsync(
3757 int extension_handle, int browser_handle,
3758 IPC::Message* reply_message) {
3759 bool success = false;
3760 Extension* extension = GetEnabledExtension(extension_handle);
3761 ExtensionsService* service = profile_->GetExtensionsService();
3762 ExtensionMessageService* message_service =
3763 profile_->GetExtensionMessageService();
3764 Browser* browser = browser_tracker_->GetResource(browser_handle);
3765 if (extension && service && message_service && browser) {
3766 int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents());
3767 if (extension->page_action()) {
3768 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
3769 browser->profile(), extension->id(), "action", tab_id, "", 1);
3770 success = true;
3771 } else if (extension->browser_action()) {
3772 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
3773 browser->profile(), extension->id(), browser);
3774 success = true;
3775 }
3776 }
3777 AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams(
3778 reply_message, success);
3779 Send(reply_message);
3780}
3781
3782void AutomationProvider::MoveExtensionBrowserAction(
3783 int extension_handle, int index, bool* success) {
3784 *success = false;
3785 Extension* extension = GetEnabledExtension(extension_handle);
3786 ExtensionsService* service = profile_->GetExtensionsService();
3787 if (extension && service) {
3788 ExtensionToolbarModel* toolbar = service->toolbar_model();
3789 if (toolbar) {
3790 if (index >= 0 && index < static_cast<int>(toolbar->size())) {
3791 toolbar->MoveBrowserAction(extension, index);
3792 *success = true;
3793 } else {
3794 DLOG(WARNING) << "Attempted to move browser action to invalid index.";
3795 }
3796 }
3797 }
3798}
3799
3800void AutomationProvider::GetExtensionProperty(
3801 int extension_handle,
3802 AutomationMsg_ExtensionProperty type,
3803 bool* success,
3804 std::string* value) {
3805 *success = false;
3806 Extension* extension = GetExtension(extension_handle);
3807 ExtensionsService* service = profile_->GetExtensionsService();
3808 if (extension && service) {
3809 ExtensionToolbarModel* toolbar = service->toolbar_model();
3810 int found_index = -1;
3811 int index = 0;
3812 switch (type) {
3813 case AUTOMATION_MSG_EXTENSION_ID:
3814 *value = extension->id();
3815 *success = true;
3816 break;
3817 case AUTOMATION_MSG_EXTENSION_NAME:
3818 *value = extension->name();
3819 *success = true;
3820 break;
3821 case AUTOMATION_MSG_EXTENSION_VERSION:
3822 *value = extension->VersionString();
3823 *success = true;
3824 break;
3825 case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX:
3826 if (toolbar) {
3827 for (ExtensionList::const_iterator iter = toolbar->begin();
3828 iter != toolbar->end(); iter++) {
3829 // Skip this extension if we are in incognito mode
3830 // and it is not incognito-enabled.
3831 if (profile_->IsOffTheRecord() &&
3832 !service->IsIncognitoEnabled(*iter))
3833 continue;
3834 if (*iter == extension) {
3835 found_index = index;
3836 break;
3837 }
3838 index++;
3839 }
3840 *value = IntToString(found_index);
3841 *success = true;
3842 }
3843 break;
3844 default:
3845 LOG(WARNING) << "Trying to get undefined extension property";
3846 break;
3847 }
3848 }
3849}
3850
[email protected]673fd2c02010-02-04 23:10:003851void AutomationProvider::SaveAsAsync(int tab_handle) {
3852 NavigationController* tab = NULL;
3853 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
3854 if (tab_contents)
3855 tab_contents->OnSavePage();
3856}
[email protected]7dad3d5f2010-03-04 00:27:013857
3858void AutomationProvider::SetContentSetting(
3859 int handle,
3860 const std::string& host,
3861 ContentSettingsType content_type,
3862 ContentSetting setting,
3863 bool* success) {
3864 *success = false;
3865 if (browser_tracker_->ContainsHandle(handle)) {
3866 Browser* browser = browser_tracker_->GetResource(handle);
3867 HostContentSettingsMap* map =
3868 browser->profile()->GetHostContentSettingsMap();
3869 if (host.empty()) {
3870 map->SetDefaultContentSetting(content_type, setting);
3871 } else {
[email protected]0314ae02010-04-08 09:18:293872 map->SetContentSetting(HostContentSettingsMap::Pattern(host),
3873 content_type, setting);
[email protected]7dad3d5f2010-03-04 00:27:013874 }
3875 *success = true;
3876 }
3877}
[email protected]cc824372010-03-31 15:33:013878
3879#if !defined(TOOLKIT_VIEWS)
3880void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
3881 NOTIMPLEMENTED();
3882};
3883
3884void AutomationProvider::WaitForFocusedViewIDToChange(
3885 int handle, int previous_view_id, IPC::Message* reply_message) {
3886 NOTIMPLEMENTED();
3887}
3888
3889void AutomationProvider::StartTrackingPopupMenus(
3890 int browser_handle, bool* success) {
3891 NOTIMPLEMENTED();
3892}
3893
3894void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
3895 NOTIMPLEMENTED();
3896}
3897#endif // !defined(TOOLKIT_VIEWS)
[email protected]d7e5525d2010-04-20 14:37:093898
3899void AutomationProvider::ResetToDefaultTheme() {
3900 profile_->ClearTheme();
3901}