blob: aac48f9d438b5816b12a4437f8c543a53c57cddb [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]c6cb1992009-04-13 16:45:2912#include "base/file_version_info.h"
[email protected]93d49d72009-10-23 20:00:2013#include "base/json/json_reader.h"
[email protected]59a611242010-04-02 02:24:0414#include "base/json/json_writer.h"
[email protected]bc1407f2009-09-29 00:33:3515#include "base/keyboard_codes.h"
[email protected]5fac9622009-02-04 21:49:3816#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/path_service.h"
[email protected]201b2732009-11-13 18:57:4618#include "base/process_util.h"
[email protected]f44265b2009-05-19 18:52:5019#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2720#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3821#include "base/thread.h"
[email protected]6d8ffc9f2010-03-12 18:27:5322#include "base/utf_string_conversions.h"
[email protected]a7eee32f2009-05-22 18:08:1723#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2924#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2825#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0926#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3327#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2928#include "chrome/browser/automation/automation_provider_list.h"
[email protected]e12de87e2009-08-28 00:02:0829#include "chrome/browser/automation/automation_provider_observers.h"
[email protected]a9024892009-06-16 23:13:5530#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5031#include "chrome/browser/automation/extension_port_container.h"
[email protected]66ba4932009-06-04 19:22:1332#include "chrome/browser/blocked_popup_container.h"
[email protected]6d8ffc9f2010-03-12 18:27:5333#include "chrome/browser/bookmarks/bookmark_model.h"
34#include "chrome/browser/bookmarks/bookmark_storage.h"
[email protected]5c238752009-06-13 10:29:0735#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3936#include "chrome/browser/browser_window.h"
[email protected]bc73b4e52010-03-26 04:16:2037#include "chrome/browser/browsing_data_remover.h"
[email protected]fae20792009-10-28 20:31:5838#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2939#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1240#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5941#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2242#include "chrome/browser/download/download_shelf.h"
[email protected]d11c8e92009-10-20 23:26:4043#include "chrome/browser/extensions/crx_installer.h"
44#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]a9024892009-06-16 23:13:5545#include "chrome/browser/extensions/extension_message_service.h"
[email protected]8cb5d5b2010-02-09 11:36:1646#include "chrome/browser/extensions/user_script_master.h"
[email protected]4801ecc2009-04-05 04:52:5847#include "chrome/browser/find_bar.h"
48#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2949#include "chrome/browser/find_notification_details.h"
[email protected]7dad3d5f2010-03-04 00:27:0150#include "chrome/browser/host_content_settings_map.h"
[email protected]0ac83682010-01-22 17:46:2751#include "chrome/browser/io_thread.h"
[email protected]13869dd2009-05-05 00:40:0652#include "chrome/browser/location_bar.h"
[email protected]3fcac682009-08-13 02:28:0153#include "chrome/browser/login_prompt.h"
[email protected]be180c802009-10-23 06:33:3154#include "chrome/browser/net/url_request_context_getter.h"
[email protected]f732c1e2009-07-30 15:48:5355#include "chrome/browser/net/url_request_mock_util.h"
[email protected]052313b2010-02-19 09:43:0856#include "chrome/browser/pref_service.h"
[email protected]a7eee32f2009-05-22 18:08:1757#include "chrome/browser/profile_manager.h"
[email protected]1db6ff152009-10-12 15:32:0758#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]6524b5f92009-01-22 17:48:2559#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0360#include "chrome/browser/ssl/ssl_manager.h"
61#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3462#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4563#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1764#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2965#include "chrome/common/chrome_paths.h"
[email protected]a7eee32f2009-05-22 18:08:1766#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1467#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4768#include "chrome/common/platform_util.h"
[email protected]1bb5f892009-10-06 01:44:5769#include "chrome/common/pref_names.h"
[email protected]f5bf8ccf2010-02-05 18:19:2570#include "chrome/common/url_constants.h"
[email protected]71f65dd2009-02-11 19:14:5671#include "chrome/test/automation/automation_messages.h"
[email protected]1bb5f892009-10-06 01:44:5772#include "chrome/test/automation/tab_proxy.h"
[email protected]a7eee32f2009-05-22 18:08:1773#include "net/proxy/proxy_service.h"
74#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2175#include "net/url_request/url_request_context.h"
[email protected]9a08bcf2009-08-12 19:56:2876#include "views/event.h"
initial.commit09911bf2008-07-26 23:55:2977
[email protected]de246f52009-02-25 18:25:4578#if defined(OS_WIN)
79// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4580#include "chrome/browser/character_encoding.h"
81#include "chrome/browser/download/save_package.h"
82#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4583#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4584#endif // defined(OS_WIN)
85
[email protected]5ae5bed2009-08-21 18:52:4486#if !defined(OS_MACOSX)
[email protected]e8382172009-06-19 22:16:2887// TODO(port): Port these to the mac.
[email protected]9a08bcf2009-08-12 19:56:2888#include "chrome/browser/automation/ui_controls.h"
[email protected]5ae5bed2009-08-21 18:52:4489#endif // !defined(OS_MACOSX)
[email protected]13869dd2009-05-05 00:40:0690
[email protected]e1acf6f2008-10-27 20:43:3391using base::Time;
92
[email protected]cbab76d2008-10-13 22:42:4793class AutomationInterstitialPage : public InterstitialPage {
94 public:
[email protected]57c6a652009-05-04 07:58:3495 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:4796 const GURL& url,
97 const std::string& contents)
98 : InterstitialPage(tab, true, url),
99 contents_(contents) {
100 }
101
102 virtual std::string GetHTMLContents() { return contents_; }
103
104 private:
105 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29106
[email protected]cbab76d2008-10-13 22:42:47107 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
108};
109
[email protected]c2cb8542009-08-20 21:16:51110#if !defined(OS_MACOSX)
111class ClickTask : public Task {
112 public:
[email protected]fc2e0872009-08-21 22:14:41113 explicit ClickTask(int flags) : flags_(flags) {}
[email protected]c2cb8542009-08-20 21:16:51114 virtual ~ClickTask() {}
115
116 virtual void Run() {
117 ui_controls::MouseButton button = ui_controls::LEFT;
118 if ((flags_ & views::Event::EF_LEFT_BUTTON_DOWN) ==
119 views::Event::EF_LEFT_BUTTON_DOWN) {
120 button = ui_controls::LEFT;
121 } else if ((flags_ & views::Event::EF_RIGHT_BUTTON_DOWN) ==
122 views::Event::EF_RIGHT_BUTTON_DOWN) {
123 button = ui_controls::RIGHT;
124 } else if ((flags_ & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
125 views::Event::EF_MIDDLE_BUTTON_DOWN) {
126 button = ui_controls::MIDDLE;
127 } else {
128 NOTREACHED();
129 }
130
[email protected]fc2e0872009-08-21 22:14:41131 ui_controls::SendMouseClick(button);
[email protected]c2cb8542009-08-20 21:16:51132 }
133
134 private:
[email protected]c2cb8542009-08-20 21:16:51135 int flags_;
136
137 DISALLOW_COPY_AND_ASSIGN(ClickTask);
138};
139#endif
140
initial.commit09911bf2008-07-26 23:55:29141AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57142 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56143 profile_(profile),
[email protected]cc824372010-03-31 15:33:01144 reply_message_(NULL),
145 popup_menu_waiter_(NULL) {
initial.commit09911bf2008-07-26 23:55:29146 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29147 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20148 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29149 autocomplete_edit_tracker_.reset(
150 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29151 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
152 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
[email protected]84abba62009-10-07 17:01:44153 metric_event_duration_observer_.reset(new MetricEventDurationObserver());
[email protected]528211a2010-01-14 15:25:13154 g_browser_process->AddRefModule();
initial.commit09911bf2008-07-26 23:55:29155}
156
157AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50158 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
159 port_containers_.end());
160 port_containers_.clear();
161
[email protected]0da050b92008-08-19 19:29:47162 // Make sure that any outstanding NotificationObservers also get destroyed.
163 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31164 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47165 while ((observer = it.GetNext()) != NULL)
166 delete observer;
[email protected]528211a2010-01-14 15:25:13167
168 if (channel_.get()) {
169 channel_->Close();
170 }
171 g_browser_process->ReleaseModule();
initial.commit09911bf2008-07-26 23:55:29172}
173
[email protected]9a3a293b2009-06-04 22:28:16174void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]2e4633c2009-07-09 16:58:06175 automation_resource_message_filter_ = new AutomationResourceMessageFilter;
[email protected]295039bd2008-08-15 04:32:57176 channel_.reset(
[email protected]2e4633c2009-07-09 16:58:06177 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this,
178 automation_resource_message_filter_,
179 g_browser_process->io_thread()->message_loop(),
180 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05181 scoped_ptr<FileVersionInfo> file_version_info(
182 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40183 std::string version_string;
184 if (file_version_info != NULL) {
185 version_string = WideToASCII(file_version_info->file_version());
186 }
[email protected]c6cb1992009-04-13 16:45:29187
188 // Send a hello message with our current automation protocol version.
189 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29190}
191
192void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
193 if (expected_tabs == 0) {
194 Send(new AutomationMsg_InitialLoadsComplete(0));
195 } else {
196 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
197 }
198}
199
200NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]2e028a082009-08-19 20:32:58201 NavigationController* tab, IPC::Message* reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01202 int number_of_navigations, bool include_current_navigation) {
initial.commit09911bf2008-07-26 23:55:29203 NotificationObserver* observer =
[email protected]2e028a082009-08-19 20:32:58204 new NavigationNotificationObserver(tab, this, reply_message,
[email protected]7dad3d5f2010-03-04 00:27:01205 number_of_navigations,
206 include_current_navigation);
initial.commit09911bf2008-07-26 23:55:29207
[email protected]71f65dd2009-02-11 19:14:56208 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29209 return observer;
210}
211
212void AutomationProvider::RemoveNavigationStatusListener(
213 NotificationObserver* obs) {
214 notification_observer_list_.RemoveObserver(obs);
215}
216
217NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14218 Browser* parent,
219 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56220 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14221 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29222 notification_observer_list_.AddObserver(observer);
223
224 return observer;
225}
226
227void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
228 notification_observer_list_.RemoveObserver(obs);
229}
230
231void AutomationProvider::AddLoginHandler(NavigationController* tab,
232 LoginHandler* handler) {
233 login_handler_map_[tab] = handler;
234}
235
236void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
237 DCHECK(login_handler_map_[tab]);
238 login_handler_map_.erase(tab);
239}
240
[email protected]f44265b2009-05-19 18:52:50241void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
242 int port_id = port->port_id();
243 DCHECK_NE(-1, port_id);
244 DCHECK(port_containers_.find(port_id) == port_containers_.end());
245
246 port_containers_[port_id] = port;
247}
248
249void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
250 int port_id = port->port_id();
251 DCHECK_NE(-1, port_id);
252
253 PortContainerMap::iterator it = port_containers_.find(port_id);
254 DCHECK(it != port_containers_.end());
255
256 if (it != port_containers_.end()) {
257 delete it->second;
258 port_containers_.erase(it);
259 }
260}
261
262ExtensionPortContainer* AutomationProvider::GetPortContainer(
263 int port_id) const {
264 PortContainerMap::const_iterator it = port_containers_.find(port_id);
265 if (it == port_containers_.end())
266 return NULL;
267
268 return it->second;
269}
270
initial.commit09911bf2008-07-26 23:55:29271int AutomationProvider::GetIndexForNavigationController(
272 const NavigationController* controller, const Browser* parent) const {
273 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12274 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29275}
276
277void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
278 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14279 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56280 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
281 CloseBrowserAsync)
282 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
283 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
284 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
285 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
286 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
287 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14288 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]2e028a082009-08-19 20:32:58289 IPC_MESSAGE_HANDLER_DELAY_REPLY(
290 AutomationMsg_NavigateToURLBlockUntilNavigationsComplete,
291 NavigateToURLBlockUntilNavigationsComplete)
[email protected]71f65dd2009-02-11 19:14:56292 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
293 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
294 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
295 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
296 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14297 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56298 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
299 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
300 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14301 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29302 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
303 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56304 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36305 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56306 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29307 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56308 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29309 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
310 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56311 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14312 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]8dd404bb2009-09-22 19:57:24313 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowMaximized, IsWindowMaximized)
[email protected]49a14a82009-03-31 04:16:44314 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33315 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44316 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33317 ExecuteBrowserCommand)
[email protected]8dd404bb2009-09-22 19:57:24318 IPC_MESSAGE_HANDLER(AutomationMsg_TerminateSession, TerminateSession)
[email protected]1c58a5c2009-05-21 18:47:14319 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
[email protected]8dd404bb2009-09-22 19:57:24320 IPC_MESSAGE_HANDLER(AutomationMsg_GetWindowBounds, GetWindowBounds)
[email protected]8f04ff92009-07-08 02:37:15321 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowBounds, SetWindowBounds)
[email protected]1c58a5c2009-05-21 18:47:14322 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]5ae5bed2009-08-21 18:52:44323#if !defined(OS_MACOSX)
[email protected]d1a5941e2009-08-13 23:34:24324 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]60507b12009-11-02 23:51:35325 IPC_MESSAGE_HANDLER(AutomationMsg_WindowMouseMove, WindowSimulateMouseMove)
[email protected]1c58a5c2009-05-21 18:47:14326 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]71f65dd2009-02-11 19:14:56327 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
328 WindowSimulateDrag)
[email protected]b2aa3ed72010-02-01 18:37:14329#endif
[email protected]71f65dd2009-02-11 19:14:56330 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
[email protected]982921f12009-10-27 21:43:53331 IPC_MESSAGE_HANDLER(AutomationMsg_Type, GetType)
[email protected]71f65dd2009-02-11 19:14:56332 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37333#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56334 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45335#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56336 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
337 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03338 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56339 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14340 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
[email protected]34930432009-11-09 00:12:09341 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreen, IsFullscreen)
342 IPC_MESSAGE_HANDLER(AutomationMsg_IsFullscreenBubbleVisible,
343 GetFullscreenBubbleVisibility)
initial.commit09911bf2008-07-26 23:55:29344 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14345 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56346 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
347 ExecuteJavascript)
348 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29349 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14350 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
351 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56352 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
353 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14354 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17355 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14356 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14357 OpenNewBrowserWindow)
[email protected]982921f12009-10-27 21:43:53358 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindowOfType,
359 OpenNewBrowserWindowOfType)
[email protected]1c58a5c2009-05-21 18:47:14360 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56361 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14362 GetAutocompleteEditForBrowser)
363 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45364#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29365 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17366#endif
[email protected]71f65dd2009-02-11 19:14:56367 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29368 NavigateInExternalTab)
[email protected]4150ef02009-08-19 23:14:26369 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex,
370 NavigateExternalTabAtIndex)
[email protected]71f65dd2009-02-11 19:14:56371 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14372 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56373 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14374 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17375#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29376 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
377 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:17378#endif
[email protected]71f65dd2009-02-11 19:14:56379 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
380 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:14381 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:17382#if defined(OS_WIN)
[email protected]5a9708432009-09-23 22:15:04383 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:39384 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
385 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:17386#endif
[email protected]1c58a5c2009-05-21 18:47:14387 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
388 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]84abba62009-10-07 17:01:44389 IPC_MESSAGE_HANDLER(AutomationMsg_GetMetricEventDuration,
390 GetMetricEventDuration)
[email protected]71f65dd2009-02-11 19:14:56391 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
392 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29393 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
394 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
395 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56396 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
[email protected]d301c952009-07-13 15:02:41397 IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync)
[email protected]71f65dd2009-02-11 19:14:56398 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
399 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29400 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56401 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29402 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56403 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29404 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56405 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29406 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56407 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45408 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45409 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
410 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:14411 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:56412 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57413 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56414 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57415 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56416 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
417 GetBookmarkBarVisibility)
[email protected]6d8ffc9f2010-03-12 18:27:53418 IPC_MESSAGE_HANDLER(AutomationMsg_GetBookmarksAsJSON,
419 GetBookmarksAsJSON)
420 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForBookmarkModelToLoad,
421 WaitForBookmarkModelToLoad)
422 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkGroup,
423 AddBookmarkGroup)
424 IPC_MESSAGE_HANDLER(AutomationMsg_AddBookmarkURL,
425 AddBookmarkURL)
426 IPC_MESSAGE_HANDLER(AutomationMsg_ReparentBookmark,
427 ReparentBookmark)
428 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkTitle,
429 SetBookmarkTitle)
430 IPC_MESSAGE_HANDLER(AutomationMsg_SetBookmarkURL,
431 SetBookmarkURL)
432 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBookmark,
433 RemoveBookmark)
[email protected]59a611242010-04-02 02:24:04434 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SendJSONRequest,
435 SendJSONRequest)
[email protected]816633a2009-11-11 21:48:18436 IPC_MESSAGE_HANDLER(AutomationMsg_GetInfoBarCount, GetInfoBarCount)
437 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickInfoBarAccept,
438 ClickInfoBarAccept)
[email protected]71f65dd2009-02-11 19:14:56439 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42440 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56441 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
442 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:14443 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56444 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44445 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56446 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20447 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:14448 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56449 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16450 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56451 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16452 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56453 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16454 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:14455 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:20456 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
457 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:14458 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]4cfc1d922009-11-08 14:02:58459#if defined(OS_WIN)
460 // Depends on ExternalTabContainer, so Windows-only
[email protected]b83e4602009-05-15 22:58:33461 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
462 SetEnableExtensionAutomation)
[email protected]4cfc1d922009-11-08 14:02:58463#endif
[email protected]59560e0b2009-06-04 03:30:22464 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:13465 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
[email protected]f7a68432009-07-29 23:18:19466 IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll)
467 IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut)
468 IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy)
469 IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste)
470 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync)
471 IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync)
[email protected]2949e90d2009-08-21 15:32:52472 IPC_MESSAGE_HANDLER_DELAY_REPLY(
473 AutomationMsg_WaitForBrowserWindowCountToBecome,
474 WaitForBrowserWindowCountToBecome)
475 IPC_MESSAGE_HANDLER_DELAY_REPLY(
476 AutomationMsg_WaitForAppModalDialogToBeShown,
477 WaitForAppModalDialogToBeShown)
[email protected]1126a1d32009-08-26 15:39:26478 IPC_MESSAGE_HANDLER_DELAY_REPLY(
479 AutomationMsg_GoBackBlockUntilNavigationsComplete,
480 GoBackBlockUntilNavigationsComplete)
481 IPC_MESSAGE_HANDLER_DELAY_REPLY(
482 AutomationMsg_GoForwardBlockUntilNavigationsComplete,
483 GoForwardBlockUntilNavigationsComplete)
[email protected]632fbb12009-09-06 15:27:14484#if defined(OS_WIN)
485 IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab)
486#endif
[email protected]1bb5f892009-10-06 01:44:57487 IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize)
[email protected]d11c8e92009-10-20 23:26:40488 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension,
489 InstallExtension)
490 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension,
491 LoadExpandedExtension)
[email protected]a1e62d12010-03-16 02:18:43492 IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions,
493 GetEnabledExtensions)
[email protected]fedaa7d2010-01-26 20:34:57494 IPC_MESSAGE_HANDLER(AutomationMsg_ShutdownSessionService,
495 ShutdownSessionService)
[email protected]673fd2c02010-02-04 23:10:00496 IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync)
[email protected]2b19e2fe2010-02-16 02:24:18497#if defined(OS_WIN)
498 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved)
499#endif
[email protected]7dad3d5f2010-03-04 00:27:01500 IPC_MESSAGE_HANDLER(AutomationMsg_SetContentSetting, SetContentSetting)
[email protected]566a0f762010-03-10 04:14:57501#if defined(OS_CHROMEOS)
502 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass,
503 LoginWithUserAndPass)
504#endif
[email protected]bc73b4e52010-03-26 04:16:20505 IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData)
[email protected]cc824372010-03-31 15:33:01506#if defined(TOOLKIT_VIEWS)
507 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange,
508 WaitForFocusedViewIDToChange)
509 IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus,
510 StartTrackingPopupMenus)
511 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen,
512 WaitForPopupMenuToOpen)
513#endif
initial.commit09911bf2008-07-26 23:55:29514 IPC_END_MESSAGE_MAP()
515}
516
[email protected]71f65dd2009-02-11 19:14:56517void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
518 *status = -1;
initial.commit09911bf2008-07-26 23:55:29519 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
520 Browser* browser = browser_tracker_->GetResource(handle);
521 if (at_index >= 0 && at_index < browser->tab_count()) {
522 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:56523 *status = 0;
initial.commit09911bf2008-07-26 23:55:29524 }
525 }
initial.commit09911bf2008-07-26 23:55:29526}
527
[email protected]71f65dd2009-02-11 19:14:56528void AutomationProvider::AppendTab(int handle, const GURL& url,
529 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29530 int append_tab_response = -1; // -1 is the error code
531 NotificationObserver* observer = NULL;
532
533 if (browser_tracker_->ContainsHandle(handle)) {
534 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:14535 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:58536 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
537 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:39538 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:29539 if (tab_contents) {
540 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:57541 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:29542 }
543 }
544
545 if (append_tab_response < 0) {
546 // The append tab failed. Remove the TabStripObserver
547 if (observer) {
548 RemoveTabStripObserver(observer);
549 delete observer;
550 }
551
[email protected]71f65dd2009-02-11 19:14:56552 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
553 append_tab_response);
554 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29555 }
556}
557
[email protected]71f65dd2009-02-11 19:14:56558void AutomationProvider::NavigateToURL(int handle, const GURL& url,
559 IPC::Message* reply_message) {
[email protected]2e028a082009-08-19 20:32:58560 NavigateToURLBlockUntilNavigationsComplete(handle, url, 1, reply_message);
561}
562
563void AutomationProvider::NavigateToURLBlockUntilNavigationsComplete(
564 int handle, const GURL& url, int number_of_navigations,
565 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29566 if (tab_tracker_->ContainsHandle(handle)) {
567 NavigationController* tab = tab_tracker_->GetResource(handle);
568
569 // Simulate what a user would do. Activate the tab and then navigate.
570 // We could allow navigating in a background tab in future.
571 Browser* browser = FindAndActivateTab(tab);
572
573 if (browser) {
[email protected]7dad3d5f2010-03-04 00:27:01574 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
575 false);
[email protected]71f65dd2009-02-11 19:14:56576
initial.commit09911bf2008-07-26 23:55:29577 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50578 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29579 return;
580 }
581 }
[email protected]71f65dd2009-02-11 19:14:56582
583 AutomationMsg_NavigateToURL::WriteReplyParams(
584 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
585 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29586}
[email protected]2949e90d2009-08-21 15:32:52587
[email protected]71f65dd2009-02-11 19:14:56588void AutomationProvider::NavigationAsync(int handle, const GURL& url,
589 bool* status) {
590 *status = false;
initial.commit09911bf2008-07-26 23:55:29591
592 if (tab_tracker_->ContainsHandle(handle)) {
593 NavigationController* tab = tab_tracker_->GetResource(handle);
594
595 // Simulate what a user would do. Activate the tab and then navigate.
596 // We could allow navigating in a background tab in future.
597 Browser* browser = FindAndActivateTab(tab);
598
599 if (browser) {
600 // Don't add any listener unless a callback mechanism is desired.
601 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50602 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:56603 *status = true;
initial.commit09911bf2008-07-26 23:55:29604 }
605 }
initial.commit09911bf2008-07-26 23:55:29606}
607
[email protected]71f65dd2009-02-11 19:14:56608void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29609 if (tab_tracker_->ContainsHandle(handle)) {
610 NavigationController* tab = tab_tracker_->GetResource(handle);
611 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14612 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:01613 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29614 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29615 return;
616 }
617 }
[email protected]71f65dd2009-02-11 19:14:56618
619 AutomationMsg_GoBack::WriteReplyParams(
620 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
621 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29622}
623
[email protected]71f65dd2009-02-11 19:14:56624void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29625 if (tab_tracker_->ContainsHandle(handle)) {
626 NavigationController* tab = tab_tracker_->GetResource(handle);
627 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14628 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:01629 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]485fba42009-03-24 23:27:29630 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:29631 return;
632 }
633 }
[email protected]71f65dd2009-02-11 19:14:56634
635 AutomationMsg_GoForward::WriteReplyParams(
636 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
637 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29638}
639
[email protected]71f65dd2009-02-11 19:14:56640void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29641 if (tab_tracker_->ContainsHandle(handle)) {
642 NavigationController* tab = tab_tracker_->GetResource(handle);
643 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:14644 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]7dad3d5f2010-03-04 00:27:01645 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29646 browser->Reload();
647 return;
648 }
649 }
[email protected]71f65dd2009-02-11 19:14:56650
651 AutomationMsg_Reload::WriteReplyParams(
652 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
653 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29654}
655
[email protected]71f65dd2009-02-11 19:14:56656void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:29657 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:56658 const std::wstring& password,
659 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29660 if (tab_tracker_->ContainsHandle(tab_handle)) {
661 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
662 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
663
664 if (iter != login_handler_map_.end()) {
665 // If auth is needed again after this, assume login has failed. This is
666 // not strictly correct, because a navigation can require both proxy and
667 // server auth, but it should be OK for now.
668 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01669 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29670 handler->SetAuth(username, password);
[email protected]457f5cf2009-08-18 16:37:52671 return;
initial.commit09911bf2008-07-26 23:55:29672 }
673 }
[email protected]de246f52009-02-25 18:25:45674
[email protected]457f5cf2009-08-18 16:37:52675 AutomationMsg_SetAuth::WriteReplyParams(
676 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
677 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29678}
679
[email protected]71f65dd2009-02-11 19:14:56680void AutomationProvider::CancelAuth(int tab_handle,
681 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29682 if (tab_tracker_->ContainsHandle(tab_handle)) {
683 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
684 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
685
686 if (iter != login_handler_map_.end()) {
687 // If auth is needed again after this, something is screwy.
688 LoginHandler* handler = iter->second;
[email protected]7dad3d5f2010-03-04 00:27:01689 AddNavigationStatusListener(tab, reply_message, 1, false);
initial.commit09911bf2008-07-26 23:55:29690 handler->CancelAuth();
[email protected]457f5cf2009-08-18 16:37:52691 return;
initial.commit09911bf2008-07-26 23:55:29692 }
693 }
[email protected]de246f52009-02-25 18:25:45694
[email protected]457f5cf2009-08-18 16:37:52695 AutomationMsg_CancelAuth::WriteReplyParams(
696 reply_message, AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED);
697 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29698}
699
[email protected]71f65dd2009-02-11 19:14:56700void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
701 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:29702
703 if (tab_tracker_->ContainsHandle(tab_handle)) {
704 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
705 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
706
707 if (iter != login_handler_map_.end()) {
708 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:56709 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:29710 }
711 }
initial.commit09911bf2008-07-26 23:55:29712}
713
[email protected]71f65dd2009-02-11 19:14:56714void AutomationProvider::GetRedirectsFrom(int tab_handle,
715 const GURL& source_url,
716 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:29717 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
718 if (tab_tracker_->ContainsHandle(tab_handle)) {
719 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
720 HistoryService* history_service =
721 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
722
723 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
724 "has no history service";
725 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:56726 DCHECK(reply_message_ == NULL);
727 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:29728 // Schedule a history query for redirects. The response will be sent
729 // asynchronously from the callback the history system uses to notify us
730 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:29731 redirect_query_ = history_service->QueryRedirectsFrom(
732 source_url, &consumer_,
733 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
734 return; // Response will be sent when query completes.
735 }
736 }
737
738 // Send failure response.
[email protected]deb57402009-02-06 01:35:30739 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:56740 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
741 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29742}
743
[email protected]71f65dd2009-02-11 19:14:56744void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
745 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29746 if (browser_tracker_->ContainsHandle(handle)) {
747 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56748 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:29749 }
initial.commit09911bf2008-07-26 23:55:29750}
751
[email protected]202e7a72009-06-15 03:48:36752void AutomationProvider::GetBrowserLocale(string16* locale) {
753 DCHECK(g_browser_process);
[email protected]d70539de2009-06-24 22:17:06754 *locale = ASCIIToUTF16(g_browser_process->GetApplicationLocale());
[email protected]202e7a72009-06-15 03:48:36755}
756
[email protected]71f65dd2009-02-11 19:14:56757void AutomationProvider::GetBrowserWindowCount(int* window_count) {
758 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:29759}
760
[email protected]24497032009-05-01 17:00:29761void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
762 *window_count = static_cast<int>(
763 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
764}
765
[email protected]71f65dd2009-02-11 19:14:56766void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
767 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:07768 AppModalDialog* dialog_delegate =
769 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50770 *showing_dialog = (dialog_delegate != NULL);
771 if (*showing_dialog)
772 *dialog_button = dialog_delegate->GetDialogButtons();
773 else
[email protected]478ff2ed2009-04-21 23:49:18774 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:20775}
776
[email protected]71f65dd2009-02-11 19:14:56777void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
778 *success = false;
[email protected]fad84eab2008-12-05 00:37:20779
[email protected]1f460072009-05-28 17:02:07780 AppModalDialog* dialog_delegate =
781 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:50782 if (dialog_delegate &&
783 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:18784 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
785 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:28786 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:56787 *success = true;
[email protected]fad84eab2008-12-05 00:37:20788 }
[email protected]478ff2ed2009-04-21 23:49:18789 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
790 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:56791 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:28792 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:56793 *success = true;
[email protected]fad84eab2008-12-05 00:37:20794 }
795 }
[email protected]c274acc2008-11-11 20:13:44796}
797
[email protected]fedaa7d2010-01-26 20:34:57798void AutomationProvider::ShutdownSessionService(int handle, bool* result) {
799 if (browser_tracker_->ContainsHandle(handle)) {
800 Browser* browser = browser_tracker_->GetResource(handle);
801 browser->profile()->ShutdownSessionService();
802 *result = true;
803 } else {
804 *result = false;
805 }
806}
807
[email protected]71f65dd2009-02-11 19:14:56808void AutomationProvider::GetBrowserWindow(int index, int* handle) {
809 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29810 if (index >= 0) {
811 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:29812 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:29813 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:56814 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:29815 }
816 }
initial.commit09911bf2008-07-26 23:55:29817}
818
[email protected]24497032009-05-01 17:00:29819void AutomationProvider::FindNormalBrowserWindow(int* handle) {
820 *handle = 0;
821 Browser* browser = BrowserList::FindBrowserWithType(profile_,
[email protected]62b0b532010-03-26 22:44:31822 Browser::TYPE_NORMAL,
823 false);
[email protected]24497032009-05-01 17:00:29824 if (browser)
825 *handle = browser_tracker_->Add(browser);
826}
827
[email protected]71f65dd2009-02-11 19:14:56828void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
829 *handle = 0;
initial.commit09911bf2008-07-26 23:55:29830 Browser* browser = BrowserList::GetLastActive();
831 if (browser)
[email protected]71f65dd2009-02-11 19:14:56832 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:29833}
834
[email protected]b2aa3ed72010-02-01 18:37:14835#if defined(OS_POSIX)
[email protected]9a08bcf2009-08-12 19:56:28836// TODO(estade): use this implementation for all platforms?
837void AutomationProvider::GetActiveWindow(int* handle) {
838 gfx::NativeWindow window =
839 BrowserList::GetLastActive()->window()->GetNativeHandle();
840 *handle = window_tracker_->Add(window);
841}
842#endif
843
[email protected]4f6381ee2009-04-16 02:46:33844void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
845 bool* success) {
[email protected]71f65dd2009-02-11 19:14:56846 *success = false;
[email protected]4ae62752008-08-04 23:28:47847 if (browser_tracker_->ContainsHandle(handle)) {
848 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:14849 if (browser->command_updater()->SupportsCommand(command) &&
850 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:47851 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:56852 *success = true;
[email protected]4ae62752008-08-04 23:28:47853 }
854 }
[email protected]4ae62752008-08-04 23:28:47855}
856
[email protected]4f6381ee2009-04-16 02:46:33857void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:56858 int handle, int command, IPC::Message* reply_message) {
[email protected]12887da72009-09-16 19:15:53859 // List of commands which just finish synchronously and don't require
860 // setting up an observer.
861 static const int kSynchronousCommands[] = {
862 IDC_HOME,
863 IDC_SELECT_NEXT_TAB,
864 IDC_SELECT_PREVIOUS_TAB,
865 IDC_SHOW_DOWNLOADS,
866 IDC_SHOW_HISTORY,
[email protected]3b5f7022010-03-25 20:37:40867 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]12887da72009-09-16 19:15:53868 };
[email protected]56e71b7c2009-03-27 03:05:56869 if (browser_tracker_->ContainsHandle(handle)) {
870 Browser* browser = browser_tracker_->GetResource(handle);
871 if (browser->command_updater()->SupportsCommand(command) &&
872 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]12887da72009-09-16 19:15:53873 // First check if we can handle the command without using an observer.
874 for (size_t i = 0; i < arraysize(kSynchronousCommands); i++) {
875 if (command == kSynchronousCommands[i]) {
876 browser->ExecuteCommand(command);
877 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message,
878 true);
879 Send(reply_message);
880 return;
881 }
882 }
883
884 // Use an observer if we have one, otherwise fail.
[email protected]d79ffea2009-05-07 20:51:42885 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
886 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:27887 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:42888 return;
889 }
[email protected]56e71b7c2009-03-27 03:05:56890 }
891 }
[email protected]49a14a82009-03-31 04:16:44892 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:56893 Send(reply_message);
894}
895
[email protected]fc2e0872009-08-21 22:14:41896// This task just adds another task to the event queue. This is useful if
897// you want to ensure that any tasks added to the event queue after this one
898// have already been processed by the time |task| is run.
899class InvokeTaskLaterTask : public Task {
900 public:
901 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
902 virtual ~InvokeTaskLaterTask() {}
903
904 virtual void Run() {
905 MessageLoop::current()->PostTask(FROM_HERE, task_);
906 }
907
908 private:
909 Task* task_;
910
911 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
912};
913
[email protected]b2aa3ed72010-02-01 18:37:14914#if !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29915void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
916 int handle,
[email protected]d1a5941e2009-08-13 23:34:24917 const gfx::Point& click,
initial.commit09911bf2008-07-26 23:55:29918 int flags) {
[email protected]b410bc32009-08-14 01:11:14919 if (window_tracker_->ContainsHandle(handle)) {
[email protected]c2cb8542009-08-20 21:16:51920 ui_controls::SendMouseMoveNotifyWhenDone(click.x(), click.y(),
[email protected]fc2e0872009-08-21 22:14:41921 new ClickTask(flags));
initial.commit09911bf2008-07-26 23:55:29922 }
923}
924
[email protected]60507b12009-11-02 23:51:35925void AutomationProvider::WindowSimulateMouseMove(const IPC::Message& message,
926 int handle,
927 const gfx::Point& location) {
928 if (window_tracker_->ContainsHandle(handle))
929 ui_controls::SendMouseMove(location.x(), location.y());
930}
931
initial.commit09911bf2008-07-26 23:55:29932void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
933 int handle,
[email protected]bc1407f2009-09-29 00:33:35934 int key,
initial.commit09911bf2008-07-26 23:55:29935 int flags) {
[email protected]b410bc32009-08-14 01:11:14936 if (!window_tracker_->ContainsHandle(handle))
initial.commit09911bf2008-07-26 23:55:29937 return;
938
[email protected]b410bc32009-08-14 01:11:14939 gfx::NativeWindow window = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:29940 // The key event is sent to whatever window is active.
[email protected]bc1407f2009-09-29 00:33:35941 ui_controls::SendKeyPress(window, static_cast<base::KeyboardCode>(key),
[email protected]c2dacc92008-10-16 23:51:38942 ((flags & views::Event::EF_CONTROL_DOWN) ==
943 views::Event::EF_CONTROL_DOWN),
944 ((flags & views::Event::EF_SHIFT_DOWN) ==
945 views::Event::EF_SHIFT_DOWN),
946 ((flags & views::Event::EF_ALT_DOWN) ==
947 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:29948}
[email protected]b2aa3ed72010-02-01 18:37:14949#endif // !defined(OS_MACOSX)
initial.commit09911bf2008-07-26 23:55:29950
[email protected]71f65dd2009-02-11 19:14:56951void AutomationProvider::IsWindowActive(int handle, bool* success,
952 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:29953 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:17954 *is_active =
955 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:56956 *success = true;
initial.commit09911bf2008-07-26 23:55:29957 } else {
[email protected]71f65dd2009-02-11 19:14:56958 *success = false;
959 *is_active = false;
initial.commit09911bf2008-07-26 23:55:29960 }
961}
962
[email protected]71f65dd2009-02-11 19:14:56963void AutomationProvider::GetTabCount(int handle, int* tab_count) {
964 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29965
966 if (browser_tracker_->ContainsHandle(handle)) {
967 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:56968 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:29969 }
initial.commit09911bf2008-07-26 23:55:29970}
971
[email protected]982921f12009-10-27 21:43:53972void AutomationProvider::GetType(int handle, int* type_as_int) {
973 *type_as_int = -1; // -1 is the error code
974
975 if (browser_tracker_->ContainsHandle(handle)) {
976 Browser* browser = browser_tracker_->GetResource(handle);
977 *type_as_int = static_cast<int>(browser->type());
978 }
979}
980
[email protected]71f65dd2009-02-11 19:14:56981void AutomationProvider::GetTab(int win_handle, int tab_index,
982 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:56983 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:29984 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
985 Browser* browser = browser_tracker_->GetResource(win_handle);
986 if (tab_index < browser->tab_count()) {
987 TabContents* tab_contents =
988 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:57989 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:29990 }
991 }
initial.commit09911bf2008-07-26 23:55:29992}
993
[email protected]71f65dd2009-02-11 19:14:56994void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
995 std::wstring* title) {
996 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:29997 if (tab_tracker_->ContainsHandle(handle)) {
998 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:36999 NavigationEntry* entry = tab->GetActiveEntry();
1000 if (entry != NULL) {
1001 *title = UTF16ToWideHack(entry->title());
1002 } else {
1003 *title = std::wstring();
1004 }
[email protected]71f65dd2009-02-11 19:14:561005 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291006 }
initial.commit09911bf2008-07-26 23:55:291007}
1008
[email protected]77bc6732009-04-20 22:01:031009void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1010 *tabstrip_index = -1; // -1 is the error code
1011
1012 if (tab_tracker_->ContainsHandle(handle)) {
1013 NavigationController* tab = tab_tracker_->GetResource(handle);
1014 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121015 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031016 }
1017}
1018
initial.commit09911bf2008-07-26 23:55:291019void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1020 if (window_tracker_->ContainsHandle(handle)) {
1021 window_tracker_->Remove(window_tracker_->GetResource(handle));
1022 }
1023}
1024
1025void AutomationProvider::OnChannelError() {
[email protected]2947cdcd2009-12-03 21:05:161026 LOG(INFO) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571027 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291028}
1029
1030// TODO(brettw) change this to accept GURLs when history supports it
1031void AutomationProvider::OnRedirectQueryComplete(
1032 HistoryService::Handle request_handle,
[email protected]3e377c52009-08-06 07:46:371033 GURL from_url,
initial.commit09911bf2008-07-26 23:55:291034 bool success,
[email protected]379c2b12009-07-01 21:50:331035 history::RedirectList* redirects) {
initial.commit09911bf2008-07-26 23:55:291036 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561037 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291038
[email protected]deb57402009-02-06 01:35:301039 std::vector<GURL> redirects_gurl;
[email protected]0bc24482010-03-05 00:33:101040 reply_message_->WriteBool(success);
initial.commit09911bf2008-07-26 23:55:291041 if (success) {
initial.commit09911bf2008-07-26 23:55:291042 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301043 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291044 }
1045
[email protected]4f3dc372009-02-24 00:10:291046 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301047
[email protected]71f65dd2009-02-11 19:14:561048 Send(reply_message_);
[email protected]5a3b9142009-08-28 21:03:171049 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561050 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291051}
1052
1053bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571054 DCHECK(channel_.get());
1055 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291056}
1057
1058Browser* AutomationProvider::FindAndActivateTab(
1059 NavigationController* controller) {
1060 int tab_index;
1061 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1062 if (browser)
1063 browser->SelectTabContentsAt(tab_index, true);
1064
1065 return browser;
1066}
1067
[email protected]71f65dd2009-02-11 19:14:561068void AutomationProvider::GetCookies(const GURL& url, int handle,
1069 int* value_size,
1070 std::string* value) {
1071 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291072 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1073 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311074
1075 // Since we are running on the UI thread don't call GetURLRequestContext().
[email protected]70daf0b2010-03-02 19:13:001076 scoped_refptr<URLRequestContextGetter> request_context =
1077 tab->tab_contents()->request_context();
1078 if (!request_context.get())
1079 request_context = tab->profile()->GetRequestContext();
1080
1081 net::CookieStore* cookie_store = request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311082
1083 *value = cookie_store->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561084 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291085 }
initial.commit09911bf2008-07-26 23:55:291086}
1087
[email protected]71f65dd2009-02-11 19:14:561088void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291089 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561090 int handle,
1091 int* response_value) {
1092 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291093
1094 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1095 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]be180c802009-10-23 06:33:311096
[email protected]dfa46e5f2009-11-17 18:48:431097 scoped_refptr<URLRequestContextGetter> request_context =
1098 tab->tab_contents()->request_context();
1099 if (!request_context.get())
1100 request_context = tab->profile()->GetRequestContext();
1101
[email protected]be180c802009-10-23 06:33:311102 // Since we are running on the UI thread don't call GetURLRequestContext().
1103 scoped_refptr<net::CookieStore> cookie_store =
[email protected]dfa46e5f2009-11-17 18:48:431104 request_context->GetCookieStore();
[email protected]be180c802009-10-23 06:33:311105
1106 if (cookie_store->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561107 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291108 }
initial.commit09911bf2008-07-26 23:55:291109}
1110
[email protected]71f65dd2009-02-11 19:14:561111void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1112 *success = false;
initial.commit09911bf2008-07-26 23:55:291113 if (tab_tracker_->ContainsHandle(handle)) {
1114 NavigationController* tab = tab_tracker_->GetResource(handle);
1115 // Return what the user would see in the location bar.
[email protected]ebe89e062009-08-13 23:16:541116 *url = tab->GetActiveEntry()->virtual_url();
[email protected]71f65dd2009-02-11 19:14:561117 *success = true;
initial.commit09911bf2008-07-26 23:55:291118 }
initial.commit09911bf2008-07-26 23:55:291119}
1120
[email protected]71f65dd2009-02-11 19:14:561121void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1122 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291123
1124 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561125 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341126 TabContents* tab_contents =
1127 tab_tracker_->GetResource(handle)->tab_contents();
[email protected]8cb5d5b2010-02-09 11:36:161128 RenderProcessHost* rph = tab_contents->GetRenderProcessHost();
1129 if (rph)
1130 *process_id = base::GetProcId(rph->GetHandle());
initial.commit09911bf2008-07-26 23:55:291131 }
initial.commit09911bf2008-07-26 23:55:291132}
1133
1134void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331135 NOTREACHED() << "This function has been deprecated. "
1136 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291137}
1138
[email protected]71f65dd2009-02-11 19:14:561139void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291140 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561141 const std::wstring& script,
1142 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291143 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341144 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1145 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571146 // Set the routing id of this message with the controller.
1147 // This routing id needs to be remembered for the reverse
1148 // communication while sending back the response of
1149 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331150 std::wstring set_automation_id;
1151 SStringPrintf(&set_automation_id,
1152 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561153 reply_message->routing_id());
1154
1155 DCHECK(reply_message_ == NULL);
1156 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291157
[email protected]57c6a652009-05-04 07:58:341158 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331159 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341160 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061161 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571162 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291163 }
1164
1165 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561166 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1167 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291168 }
1169}
1170
[email protected]71f65dd2009-02-11 19:14:561171void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1172 *visible = false;
[email protected]20e93d12008-08-28 16:31:571173
[email protected]59560e0b2009-06-04 03:30:221174 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]f5bf8ccf2010-02-05 18:19:251175#if defined(OS_CHROMEOS)
1176 // Chromium OS shows FileBrowse ui rather than download shelf. So we
1177 // enumerate all browsers and look for a chrome://filebrowse... pop up.
1178 for (BrowserList::const_iterator it = BrowserList::begin();
1179 it != BrowserList::end(); ++it) {
1180 if ((*it)->type() == Browser::TYPE_POPUP) {
1181 const GURL& url =
1182 (*it)->GetTabContentsAt((*it)->selected_index())->GetURL();
1183
1184 if (url.SchemeIs(chrome::kChromeUIScheme) &&
1185 url.host() == chrome::kChromeUIFileBrowseHost) {
1186 *visible = true;
1187 break;
1188 }
1189 }
1190 }
1191#else
[email protected]59560e0b2009-06-04 03:30:221192 Browser* browser = browser_tracker_->GetResource(handle);
1193 if (browser) {
1194 *visible = browser->window()->IsDownloadShelfVisible();
1195 }
[email protected]f5bf8ccf2010-02-05 18:19:251196#endif
[email protected]59560e0b2009-06-04 03:30:221197 }
initial.commit09911bf2008-07-26 23:55:291198}
1199
[email protected]59560e0b2009-06-04 03:30:221200void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1201 if (browser_tracker_->ContainsHandle(handle)) {
1202 Browser* browser = browser_tracker_->GetResource(handle);
1203 if (browser) {
1204 if (visible)
1205 browser->window()->GetDownloadShelf()->Show();
1206 else
1207 browser->window()->GetDownloadShelf()->Close();
1208 }
1209 }
1210}
1211
[email protected]34930432009-11-09 00:12:091212void AutomationProvider::IsFullscreen(int handle, bool* visible) {
1213 *visible = false;
1214
1215 if (browser_tracker_->ContainsHandle(handle)) {
1216 Browser* browser = browser_tracker_->GetResource(handle);
1217 if (browser)
1218 *visible = browser->window()->IsFullscreen();
1219 }
1220}
1221
1222void AutomationProvider::GetFullscreenBubbleVisibility(int handle,
1223 bool* visible) {
1224 *visible = false;
1225
1226 if (browser_tracker_->ContainsHandle(handle)) {
1227 Browser* browser = browser_tracker_->GetResource(handle);
1228 if (browser)
1229 *visible = browser->window()->IsFullscreenBubbleVisible();
1230 }
1231}
[email protected]59560e0b2009-06-04 03:30:221232
[email protected]71f65dd2009-02-11 19:14:561233void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1234 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291235 if (tab_tracker_->ContainsHandle(handle)) {
1236 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111237 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291238 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561239 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291240 }
1241 }
initial.commit09911bf2008-07-26 23:55:291242}
1243
initial.commit09911bf2008-07-26 23:55:291244void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561245 int handle, const std::wstring& find_request,
1246 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311247 NOTREACHED() << "This function has been deprecated."
1248 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561249 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311250 return;
1251}
1252
[email protected]4f999132009-03-31 18:08:401253void AutomationProvider::HandleFindRequest(
1254 int handle,
1255 const AutomationMsg_Find_Params& params,
1256 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291257 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561258 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1259 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291260 return;
1261 }
1262
1263 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111264 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291265
1266 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:141267 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:291268
[email protected]57c6a652009-05-04 07:58:341269 tab_contents->set_current_find_request_id(
1270 FindInPageNotificationObserver::kFindInPageRequestId);
1271 tab_contents->render_view_host()->StartFinding(
1272 FindInPageNotificationObserver::kFindInPageRequestId,
1273 params.search_string, params.forward, params.match_case,
1274 params.find_next);
initial.commit09911bf2008-07-26 23:55:291275}
1276
[email protected]5f8af2a2008-08-06 22:49:451277void AutomationProvider::HandleOpenFindInPageRequest(
1278 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291279 if (browser_tracker_->ContainsHandle(handle)) {
1280 Browser* browser = browser_tracker_->GetResource(handle);
1281 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:451282 }
1283}
1284
[email protected]71f65dd2009-02-11 19:14:561285void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]71f65dd2009-02-11 19:14:561286 *visible = false;
[email protected]855c0142009-09-28 22:35:241287 Browser* browser = browser_tracker_->GetResource(handle);
1288 if (browser) {
[email protected]4801ecc2009-04-05 04:52:581289 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171290 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]855c0142009-09-28 22:35:241291 find_bar->GetFindBarWindowInfo(NULL, visible);
[email protected]4f3dc372009-02-24 00:10:291292 }
[email protected]20e93d12008-08-28 16:31:571293}
1294
[email protected]71f65dd2009-02-11 19:14:561295void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1296 int* y) {
[email protected]9e0534b2008-10-21 15:03:011297 gfx::Point position(0, 0);
1298 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:291299 if (browser_tracker_->ContainsHandle(handle)) {
1300 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581301 FindBarTesting* find_bar =
[email protected]b77cb302009-10-29 04:09:171302 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
[email protected]4801ecc2009-04-05 04:52:581303 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:291304 }
[email protected]20e93d12008-08-28 16:31:571305
[email protected]71f65dd2009-02-11 19:14:561306 *x = position.x();
1307 *y = position.y();
[email protected]20e93d12008-08-28 16:31:571308}
1309
[email protected]4512cb52010-04-05 19:50:251310// Bookmark bar visibility is based on the pref (e.g. is it in the toolbar).
1311// Presence in the NTP is NOT considered visible by this call.
[email protected]c3240722010-03-05 21:52:581312void AutomationProvider::GetBookmarkBarVisibility(int handle,
1313 bool* visible,
1314 bool* animating) {
1315 *visible = false;
1316 *animating = false;
1317
1318 if (browser_tracker_->ContainsHandle(handle)) {
1319 Browser* browser = browser_tracker_->GetResource(handle);
1320 if (browser) {
[email protected]4512cb52010-04-05 19:50:251321 // The commented-out IsBookmarkBarVisible() line looks correct
1322 // but is not consistent across platforms. Specifically, on
1323 // Mac/Linux, it returns false if the bar is hidden in a pref
1324 // (even if visible on the NTP). On ChromeOS, it returned true
1325 // if on NTP independent of the pref. Making the code more
1326 // consistent caused a perf bot regression on Windows (which
1327 // shares views). See http://crbug.com/40225
1328 // // *visible = browser->window()->IsBookmarkBarVisible();
1329 *visible = browser->profile()->GetPrefs()->GetBoolean(
1330 prefs::kShowBookmarkBar);
[email protected]c3240722010-03-05 21:52:581331 *animating = browser->window()->IsBookmarkBarAnimating();
1332 }
1333 }
1334}
1335
[email protected]6d8ffc9f2010-03-12 18:27:531336void AutomationProvider::GetBookmarksAsJSON(int handle,
1337 std::string* bookmarks_as_json,
1338 bool *success) {
1339 *success = false;
1340 if (browser_tracker_->ContainsHandle(handle)) {
1341 Browser* browser = browser_tracker_->GetResource(handle);
1342 if (browser) {
1343 if (!browser->profile()->GetBookmarkModel()->IsLoaded()) {
1344 return;
1345 }
1346 scoped_refptr<BookmarkStorage> storage = new BookmarkStorage(
1347 browser->profile(),
1348 browser->profile()->GetBookmarkModel());
1349 *success = storage->SerializeData(bookmarks_as_json);
1350 }
1351 }
1352}
1353
1354void AutomationProvider::WaitForBookmarkModelToLoad(
1355 int handle,
1356 IPC::Message* reply_message) {
1357 if (browser_tracker_->ContainsHandle(handle)) {
1358 Browser* browser = browser_tracker_->GetResource(handle);
1359 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1360 if (model->IsLoaded()) {
1361 AutomationMsg_WaitForBookmarkModelToLoad::WriteReplyParams(
1362 reply_message, true);
1363 Send(reply_message);
1364 } else {
1365 // The observer will delete itself when done.
1366 new AutomationProviderBookmarkModelObserver(this, reply_message,
1367 model);
1368 }
1369 }
1370}
1371
1372void AutomationProvider::AddBookmarkGroup(int handle,
1373 int64 parent_id, int index,
1374 std::wstring title,
1375 bool* success) {
1376 if (browser_tracker_->ContainsHandle(handle)) {
1377 Browser* browser = browser_tracker_->GetResource(handle);
1378 if (browser) {
1379 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1380 if (!model->IsLoaded()) {
1381 *success = false;
1382 return;
1383 }
1384 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1385 DCHECK(parent);
1386 if (parent) {
1387 const BookmarkNode* child = model->AddGroup(parent, index,
1388 WideToUTF16(title));
1389 DCHECK(child);
1390 if (child)
1391 *success = true;
1392 }
1393 }
1394 }
1395 *success = false;
1396}
1397
1398void AutomationProvider::AddBookmarkURL(int handle,
1399 int64 parent_id, int index,
1400 std::wstring title, const GURL& url,
1401 bool* success) {
1402 if (browser_tracker_->ContainsHandle(handle)) {
1403 Browser* browser = browser_tracker_->GetResource(handle);
1404 if (browser) {
1405 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1406 if (!model->IsLoaded()) {
1407 *success = false;
1408 return;
1409 }
1410 const BookmarkNode* parent = model->GetNodeByID(parent_id);
1411 DCHECK(parent);
1412 if (parent) {
1413 const BookmarkNode* child = model->AddURL(parent, index,
1414 WideToUTF16(title), url);
1415 DCHECK(child);
1416 if (child)
1417 *success = true;
1418 }
1419 }
1420 }
1421 *success = false;
1422}
1423
1424void AutomationProvider::ReparentBookmark(int handle,
1425 int64 id, int64 new_parent_id,
1426 int index,
1427 bool* success) {
1428 if (browser_tracker_->ContainsHandle(handle)) {
1429 Browser* browser = browser_tracker_->GetResource(handle);
1430 if (browser) {
1431 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1432 if (!model->IsLoaded()) {
1433 *success = false;
1434 return;
1435 }
1436 const BookmarkNode* node = model->GetNodeByID(id);
1437 DCHECK(node);
1438 const BookmarkNode* new_parent = model->GetNodeByID(new_parent_id);
1439 DCHECK(new_parent);
1440 if (node && new_parent) {
1441 model->Move(node, new_parent, index);
1442 *success = true;
1443 }
1444 }
1445 }
1446 *success = false;
1447}
1448
1449void AutomationProvider::SetBookmarkTitle(int handle,
1450 int64 id, std::wstring title,
1451 bool* success) {
1452 if (browser_tracker_->ContainsHandle(handle)) {
1453 Browser* browser = browser_tracker_->GetResource(handle);
1454 if (browser) {
1455 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1456 if (!model->IsLoaded()) {
1457 *success = false;
1458 return;
1459 }
1460 const BookmarkNode* node = model->GetNodeByID(id);
1461 DCHECK(node);
1462 if (node) {
1463 model->SetTitle(node, WideToUTF16(title));
1464 *success = true;
1465 }
1466 }
1467 }
1468 *success = false;
1469}
1470
1471void AutomationProvider::SetBookmarkURL(int handle,
1472 int64 id, const GURL& url,
1473 bool* success) {
1474 if (browser_tracker_->ContainsHandle(handle)) {
1475 Browser* browser = browser_tracker_->GetResource(handle);
1476 if (browser) {
1477 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1478 if (!model->IsLoaded()) {
1479 *success = false;
1480 return;
1481 }
1482 const BookmarkNode* node = model->GetNodeByID(id);
1483 DCHECK(node);
1484 if (node) {
1485 model->SetURL(node, url);
1486 *success = true;
1487 }
1488 }
1489 }
1490 *success = false;
1491}
1492
1493void AutomationProvider::RemoveBookmark(int handle,
1494 int64 id,
1495 bool* success) {
1496 if (browser_tracker_->ContainsHandle(handle)) {
1497 Browser* browser = browser_tracker_->GetResource(handle);
1498 if (browser) {
1499 BookmarkModel* model = browser->profile()->GetBookmarkModel();
1500 if (!model->IsLoaded()) {
1501 *success = false;
1502 return;
1503 }
1504 const BookmarkNode* node = model->GetNodeByID(id);
1505 DCHECK(node);
1506 if (node) {
1507 const BookmarkNode* parent = node->GetParent();
1508 DCHECK(parent);
1509 model->Remove(parent, parent->IndexOfChild(node));
1510 *success = true;
1511 }
1512 }
1513 }
1514 *success = false;
1515}
1516
[email protected]59a611242010-04-02 02:24:041517void AutomationProvider::WaitForDownloadsToComplete(
1518 DictionaryValue* args,
1519 IPC::Message* reply_message) {
1520 std::string json_return;
1521 bool reply_return = true;
1522 AutomationProviderDownloadManagerObserver observer;
1523 std::vector<DownloadItem*> downloads;
1524
1525 // Look for a quick return.
1526 if (!profile_->HasCreatedDownloadManager()) {
1527 json_return = "{'error': 'no download manager'}";
1528 reply_return = false;
1529 } else {
1530 profile_->GetDownloadManager()->GetCurrentDownloads(&observer,
1531 FilePath());
1532 downloads = observer.Downloads();
1533 if (downloads.size() == 0) {
1534 json_return = "{}";
1535 }
1536 }
1537 if (!json_return.empty()) {
1538 AutomationMsg_SendJSONRequest::WriteReplyParams(
1539 reply_message, json_return, reply_return);
1540 Send(reply_message);
1541 }
1542
1543 // The observer owns itself. When the last observed item pings, it
1544 // deletes itself.
1545 AutomationProviderDownloadItemObserver* item_observer =
1546 new AutomationProviderDownloadItemObserver(
1547 this, reply_message, downloads.size());
1548 for (std::vector<DownloadItem*>::iterator i = downloads.begin();
1549 i != downloads.end();
1550 i++) {
1551 (*i)->AddObserver(item_observer);
1552 }
1553}
1554
1555void AutomationProvider::SendJSONRequest(
1556 int handle,
1557 std::string json_request,
1558 IPC::Message* reply_message) {
1559 Browser* browser = NULL;
1560 std::string error_string;
1561 scoped_ptr<Value> values;
1562
1563 // Basic error checking.
1564 if (browser_tracker_->ContainsHandle(handle)) {
1565 browser = browser_tracker_->GetResource(handle);
1566 }
1567 if (!browser) {
1568 error_string = "no browser object";
1569 } else {
1570 base::JSONReader reader;
1571 std::string error;
1572 values.reset(reader.ReadAndReturnError(json_request, true, &error));
1573 if (!error.empty()) {
1574 error_string = error;
1575 }
1576 }
1577
1578 // Make sure input is a dict with a string command.
1579 std::string command;
1580 DictionaryValue* dict_value = NULL;
1581 if (error_string.empty()) {
1582 if (values->GetType() != Value::TYPE_DICTIONARY) {
1583 error_string = "not a dict or no command key in dict";
1584 } else {
1585 // Ownership remains with "values" variable.
1586 dict_value = static_cast<DictionaryValue*>(values.get());
1587 if (!dict_value->GetStringASCII(std::string("command"), &command)) {
1588 error_string = "no command key in dict or not a string command";
1589 }
1590 }
1591 }
1592
1593 if (error_string.empty()) {
1594 // TODO(jrg): table of calls; async gets passed reply_message,
1595 // sync methods gets passed an output json dict which we package
1596 // up and send. Right now we only have one.
1597 if (command == "WaitForAllDownloadsToComplete") {
1598 this->WaitForDownloadsToComplete(dict_value, reply_message);
1599 return;
1600 } else {
1601 error_string = "unknown command";
1602 }
1603 }
1604
1605 // If we hit an error, return info.
1606 // Return a dict of {'error', 'descriptive_string_for_error'}.
1607 // Else return an empty dict.
1608 std::string json_string;
1609 bool success = true;
1610 if (!error_string.empty()) {
1611 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
1612 dict->SetString(L"error", error_string);
1613 base::JSONWriter::Write(dict.get(), false, &json_string);
1614 success = false;
1615 } else {
1616 json_string = "{}";
1617 }
1618 AutomationMsg_SendJSONRequest::WriteReplyParams(
1619 reply_message, json_string, success);
1620 Send(reply_message);
1621}
1622
initial.commit09911bf2008-07-26 23:55:291623void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:561624 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:341625 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1626 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561627 DCHECK(reply_message_ == NULL);
1628 reply_message_ = reply_message;
1629
[email protected]d9f9b792009-06-24 13:17:121630 DevToolsManager::GetInstance()->InspectElement(
1631 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:291632 } else {
[email protected]71f65dd2009-02-11 19:14:561633 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
1634 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291635 }
1636}
1637
1638void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:091639 if (reply_message_) {
1640 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
1641 num_resources);
1642 Send(reply_message_);
1643 reply_message_ = NULL;
1644 }
initial.commit09911bf2008-07-26 23:55:291645}
1646
[email protected]a7eee32f2009-05-22 18:08:171647class SetProxyConfigTask : public Task {
1648 public:
[email protected]be180c802009-10-23 06:33:311649 SetProxyConfigTask(URLRequestContextGetter* request_context_getter,
1650 const std::string& new_proxy_config)
1651 : request_context_getter_(request_context_getter), proxy_config_(new_proxy_config) {}
[email protected]a7eee32f2009-05-22 18:08:171652 virtual void Run() {
1653 // First, deserialize the JSON string. If this fails, log and bail.
1654 JSONStringValueSerializer deserializer(proxy_config_);
1655 std::string error_message;
1656 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
1657 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
1658 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
1659 << error_message;
1660 return;
1661 }
1662
1663 scoped_ptr<DictionaryValue> dict(
1664 static_cast<DictionaryValue*>(root.release()));
1665 // Now put together a proxy configuration from the deserialized string.
1666 net::ProxyConfig pc;
1667 PopulateProxyConfig(*dict.get(), &pc);
1668
[email protected]be180c802009-10-23 06:33:311669 net::ProxyService* proxy_service =
1670 request_context_getter_->GetURLRequestContext()->proxy_service();
1671 DCHECK(proxy_service);
[email protected]a7eee32f2009-05-22 18:08:171672 scoped_ptr<net::ProxyConfigService> proxy_config_service(
1673 new net::ProxyConfigServiceFixed(pc));
[email protected]be180c802009-10-23 06:33:311674 proxy_service->ResetConfigService(proxy_config_service.release());
[email protected]a7eee32f2009-05-22 18:08:171675 }
1676
1677 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
1678 DCHECK(pc);
1679 bool no_proxy = false;
1680 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
1681 // Make no changes to the ProxyConfig.
1682 return;
1683 }
1684 bool auto_config;
1685 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
[email protected]ed4ed0f2010-02-24 00:20:481686 pc->set_auto_detect(true);
[email protected]a7eee32f2009-05-22 18:08:171687 }
1688 std::string pac_url;
1689 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
[email protected]ed4ed0f2010-02-24 00:20:481690 pc->set_pac_url(GURL(pac_url));
[email protected]a7eee32f2009-05-22 18:08:171691 }
1692 std::string proxy_bypass_list;
1693 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
[email protected]ed4ed0f2010-02-24 00:20:481694 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list);
[email protected]a7eee32f2009-05-22 18:08:171695 }
1696 std::string proxy_server;
1697 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
[email protected]ed4ed0f2010-02-24 00:20:481698 pc->proxy_rules().ParseFromString(proxy_server);
[email protected]a7eee32f2009-05-22 18:08:171699 }
1700 }
1701
1702 private:
[email protected]be180c802009-10-23 06:33:311703 scoped_refptr<URLRequestContextGetter> request_context_getter_;
[email protected]a7eee32f2009-05-22 18:08:171704 std::string proxy_config_;
1705};
1706
1707
1708void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
[email protected]be180c802009-10-23 06:33:311709 URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
1710 if (!context_getter) {
[email protected]a7eee32f2009-05-22 18:08:171711 FilePath user_data_dir;
1712 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1713 ProfileManager* profile_manager = g_browser_process->profile_manager();
1714 DCHECK(profile_manager);
1715 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
1716 DCHECK(profile);
[email protected]be180c802009-10-23 06:33:311717 context_getter = profile->GetRequestContext();
[email protected]a7eee32f2009-05-22 18:08:171718 }
[email protected]be180c802009-10-23 06:33:311719 DCHECK(context_getter);
[email protected]a7eee32f2009-05-22 18:08:171720
[email protected]fae20792009-10-28 20:31:581721 ChromeThread::PostTask(
1722 ChromeThread::IO, FROM_HERE,
[email protected]be180c802009-10-23 06:33:311723 new SetProxyConfigTask(context_getter, new_proxy_config));
[email protected]a7eee32f2009-05-22 18:08:171724}
1725
[email protected]4f3dc372009-02-24 00:10:291726void AutomationProvider::GetDownloadDirectory(
[email protected]1f733cf2009-09-30 20:46:331727 int handle, FilePath* download_directory) {
initial.commit09911bf2008-07-26 23:55:291728 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:291729 if (tab_tracker_->ContainsHandle(handle)) {
1730 NavigationController* tab = tab_tracker_->GetResource(handle);
1731 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1732 DCHECK(dlm);
[email protected]1f733cf2009-09-30 20:46:331733 *download_directory = dlm->download_path();
initial.commit09911bf2008-07-26 23:55:291734 }
initial.commit09911bf2008-07-26 23:55:291735}
1736
[email protected]6a5670d22009-10-27 16:21:341737void AutomationProvider::OpenNewBrowserWindow(bool show,
[email protected]14c0a032009-04-13 18:15:141738 IPC::Message* reply_message) {
[email protected]982921f12009-10-27 21:43:531739 OpenNewBrowserWindowOfType(static_cast<int>(Browser::TYPE_NORMAL), show,
1740 reply_message);
1741}
1742
1743void AutomationProvider::OpenNewBrowserWindowOfType(
1744 int type, bool show, IPC::Message* reply_message) {
[email protected]14c0a032009-04-13 18:15:141745 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:291746 // We may have no current browser windows open so don't rely on
1747 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]982921f12009-10-27 21:43:531748 Browser* browser = new Browser(static_cast<Browser::Type>(type), profile_);
1749 browser->CreateBrowserWindow();
[email protected]15952e462008-11-14 00:29:051750 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:151751 if (show)
[email protected]15952e462008-11-14 00:29:051752 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:291753}
1754
[email protected]71f65dd2009-02-11 19:14:561755void AutomationProvider::GetWindowForBrowser(int browser_handle,
1756 bool* success,
1757 int* handle) {
1758 *success = false;
1759 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291760
1761 if (browser_tracker_->ContainsHandle(browser_handle)) {
1762 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:201763 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:291764 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:201765 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:561766 *success = true;
initial.commit09911bf2008-07-26 23:55:291767 }
initial.commit09911bf2008-07-26 23:55:291768}
1769
initial.commit09911bf2008-07-26 23:55:291770void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:561771 int browser_handle,
1772 bool* success,
1773 int* autocomplete_edit_handle) {
1774 *success = false;
1775 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:291776
1777 if (browser_tracker_->ContainsHandle(browser_handle)) {
1778 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:061779 LocationBar* loc_bar = browser->window()->GetLocationBar();
1780 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:291781 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:561782 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
1783 *success = true;
initial.commit09911bf2008-07-26 23:55:291784 }
initial.commit09911bf2008-07-26 23:55:291785}
initial.commit09911bf2008-07-26 23:55:291786
[email protected]71f65dd2009-02-11 19:14:561787void AutomationProvider::ShowInterstitialPage(int tab_handle,
1788 const std::string& html_text,
1789 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291790 if (tab_tracker_->ContainsHandle(tab_handle)) {
1791 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:111792 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:401793
[email protected]7dad3d5f2010-03-04 00:27:011794 AddNavigationStatusListener(controller, reply_message, 1, false);
[email protected]965524b2009-04-04 21:32:401795 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:341796 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:401797 GURL("about:interstitial"),
1798 html_text);
1799 interstitial->Show();
1800 return;
initial.commit09911bf2008-07-26 23:55:291801 }
[email protected]71f65dd2009-02-11 19:14:561802
[email protected]457f5cf2009-08-18 16:37:521803 AutomationMsg_ShowInterstitialPage::WriteReplyParams(
1804 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561805 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291806}
1807
[email protected]71f65dd2009-02-11 19:14:561808void AutomationProvider::HideInterstitialPage(int tab_handle,
1809 bool* success) {
1810 *success = false;
[email protected]57c6a652009-05-04 07:58:341811 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
1812 if (tab_contents && tab_contents->interstitial_page()) {
1813 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:561814 *success = true;
initial.commit09911bf2008-07-26 23:55:291815 }
initial.commit09911bf2008-07-26 23:55:291816}
1817
[email protected]71f65dd2009-02-11 19:14:561818void AutomationProvider::CloseTab(int tab_handle,
1819 bool wait_until_closed,
1820 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291821 if (tab_tracker_->ContainsHandle(tab_handle)) {
1822 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1823 int index;
1824 Browser* browser = Browser::GetBrowserForController(controller, &index);
1825 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:141826 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:111827 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:451828 return;
initial.commit09911bf2008-07-26 23:55:291829 }
[email protected]de246f52009-02-25 18:25:451830
1831 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
[email protected]58f622a62009-10-04 01:17:551832 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291833}
1834
[email protected]71f65dd2009-02-11 19:14:561835void AutomationProvider::CloseBrowser(int browser_handle,
1836 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291837 if (browser_tracker_->ContainsHandle(browser_handle)) {
1838 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561839 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:561840 reply_message);
[email protected]f3e99e32008-07-30 04:48:391841 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291842 } else {
1843 NOTREACHED();
1844 }
1845}
1846
[email protected]71f65dd2009-02-11 19:14:561847void AutomationProvider::CloseBrowserAsync(int browser_handle) {
1848 if (browser_tracker_->ContainsHandle(browser_handle)) {
1849 Browser* browser = browser_tracker_->GetResource(browser_handle);
1850 browser->window()->Close();
1851 } else {
1852 NOTREACHED();
1853 }
1854}
1855
[email protected]71f65dd2009-02-11 19:14:561856void AutomationProvider::NavigateInExternalTab(
[email protected]b36a9f92009-10-19 17:34:571857 int handle, const GURL& url, const GURL& referrer,
[email protected]71f65dd2009-02-11 19:14:561858 AutomationMsg_NavigationResponseValues* status) {
1859 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:291860
1861 if (tab_tracker_->ContainsHandle(handle)) {
1862 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]b36a9f92009-10-19 17:34:571863 tab->LoadURL(url, referrer, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561864 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:291865 }
initial.commit09911bf2008-07-26 23:55:291866}
1867
[email protected]4150ef02009-08-19 23:14:261868void AutomationProvider::NavigateExternalTabAtIndex(
1869 int handle, int navigation_index,
1870 AutomationMsg_NavigationResponseValues* status) {
1871 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
1872
1873 if (tab_tracker_->ContainsHandle(handle)) {
1874 NavigationController* tab = tab_tracker_->GetResource(handle);
1875 tab->GoToIndex(navigation_index);
1876 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
1877 }
1878}
1879
[email protected]71f65dd2009-02-11 19:14:561880void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
1881 IPC::Message* reply_message) {
1882 if (tab_tracker_->ContainsHandle(tab_handle)) {
1883 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1884 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:141885 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:561886 }
1887}
1888
[email protected]71f65dd2009-02-11 19:14:561889void AutomationProvider::GetSecurityState(int handle, bool* success,
1890 SecurityStyle* security_style,
1891 int* ssl_cert_status,
1892 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:291893 if (tab_tracker_->ContainsHandle(handle)) {
1894 NavigationController* tab = tab_tracker_->GetResource(handle);
1895 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561896 *success = true;
1897 *security_style = entry->ssl().security_style();
1898 *ssl_cert_status = entry->ssl().cert_status();
1899 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:291900 } else {
[email protected]71f65dd2009-02-11 19:14:561901 *success = false;
1902 *security_style = SECURITY_STYLE_UNKNOWN;
1903 *ssl_cert_status = 0;
1904 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:291905 }
1906}
1907
[email protected]71f65dd2009-02-11 19:14:561908void AutomationProvider::GetPageType(int handle, bool* success,
1909 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:291910 if (tab_tracker_->ContainsHandle(handle)) {
1911 NavigationController* tab = tab_tracker_->GetResource(handle);
1912 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:561913 *page_type = entry->page_type();
1914 *success = true;
initial.commit09911bf2008-07-26 23:55:291915 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:341916 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:561917 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:341918 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:561919 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291920 } else {
[email protected]71f65dd2009-02-11 19:14:561921 *success = false;
1922 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:291923 }
1924}
1925
[email protected]84abba62009-10-07 17:01:441926void AutomationProvider::GetMetricEventDuration(const std::string& event_name,
1927 int* duration_ms) {
1928 *duration_ms = metric_event_duration_observer_->GetEventDurationMs(
1929 event_name);
1930}
1931
[email protected]71f65dd2009-02-11 19:14:561932void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
1933 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291934 if (tab_tracker_->ContainsHandle(handle)) {
1935 NavigationController* tab = tab_tracker_->GetResource(handle);
1936 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:071937 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:401938 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:471939 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:341940 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:291941 if (ssl_blocking_page) {
1942 if (proceed) {
[email protected]7dad3d5f2010-03-04 00:27:011943 AddNavigationStatusListener(tab, reply_message, 1, false);
[email protected]71f65dd2009-02-11 19:14:561944 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:291945 return;
1946 }
1947 ssl_blocking_page->DontProceed();
[email protected]457f5cf2009-08-18 16:37:521948 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1949 reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]71f65dd2009-02-11 19:14:561950 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291951 return;
1952 }
1953 }
1954 }
1955 // We failed.
[email protected]457f5cf2009-08-18 16:37:521956 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(
1957 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]71f65dd2009-02-11 19:14:561958 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291959}
1960
[email protected]71f65dd2009-02-11 19:14:561961void AutomationProvider::BringBrowserToFront(int browser_handle,
1962 bool* success) {
initial.commit09911bf2008-07-26 23:55:291963 if (browser_tracker_->ContainsHandle(browser_handle)) {
1964 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:061965 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:561966 *success = true;
initial.commit09911bf2008-07-26 23:55:291967 } else {
[email protected]71f65dd2009-02-11 19:14:561968 *success = false;
initial.commit09911bf2008-07-26 23:55:291969 }
1970}
1971
[email protected]71f65dd2009-02-11 19:14:561972void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
1973 int message_num,
1974 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:291975 if (browser_tracker_->ContainsHandle(browser_handle)) {
1976 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:561977 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:141978 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:291979 } else {
[email protected]71f65dd2009-02-11 19:14:561980 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:291981 }
1982}
1983
[email protected]71f65dd2009-02-11 19:14:561984void AutomationProvider::PrintNow(int tab_handle,
1985 IPC::Message* reply_message) {
[email protected]5ae5bed2009-08-21 18:52:441986#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571987 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:341988 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
1989 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:291990 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:571991 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:141992 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:341993 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:571994 return;
initial.commit09911bf2008-07-26 23:55:291995 }
[email protected]71f65dd2009-02-11 19:14:561996 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
1997 Send(reply_message);
[email protected]5ae5bed2009-08-21 18:52:441998#else
1999 // TODO(port): Remove once DocumentPrintedNotificationObserver is implemented.
2000 NOTIMPLEMENTED();
2001#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292002}
[email protected]d301c952009-07-13 15:02:412003
[email protected]71f65dd2009-02-11 19:14:562004void AutomationProvider::SavePage(int tab_handle,
[email protected]828cabe2009-09-26 22:47:112005 const FilePath& file_name,
2006 const FilePath& dir_path,
[email protected]71f65dd2009-02-11 19:14:562007 int type,
2008 bool* success) {
initial.commit09911bf2008-07-26 23:55:292009 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562010 *success = false;
initial.commit09911bf2008-07-26 23:55:292011 return;
2012 }
2013
2014 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2015 Browser* browser = FindAndActivateTab(nav);
2016 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142017 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562018 *success = false;
initial.commit09911bf2008-07-26 23:55:292019 return;
2020 }
2021
initial.commit09911bf2008-07-26 23:55:292022 SavePackage::SavePackageType save_type =
2023 static_cast<SavePackage::SavePackageType>(type);
2024 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2025 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:342026 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292027
[email protected]71f65dd2009-02-11 19:14:562028 *success = true;
initial.commit09911bf2008-07-26 23:55:292029}
2030
[email protected]71f65dd2009-02-11 19:14:562031void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2032 bool* success,
2033 std::wstring* text) {
2034 *success = false;
initial.commit09911bf2008-07-26 23:55:292035 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562036 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522037 GetText();
[email protected]71f65dd2009-02-11 19:14:562038 *success = true;
initial.commit09911bf2008-07-26 23:55:292039 }
initial.commit09911bf2008-07-26 23:55:292040}
2041
[email protected]71f65dd2009-02-11 19:14:562042void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2043 const std::wstring& text,
2044 bool* success) {
2045 *success = false;
initial.commit09911bf2008-07-26 23:55:292046 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522047 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2048 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562049 *success = true;
initial.commit09911bf2008-07-26 23:55:292050 }
initial.commit09911bf2008-07-26 23:55:292051}
2052
2053void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562054 int autocomplete_edit_handle,
2055 bool* success,
2056 std::vector<AutocompleteMatchData>* matches) {
2057 *success = false;
initial.commit09911bf2008-07-26 23:55:292058 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272059 const AutocompleteResult& result = autocomplete_edit_tracker_->
2060 GetResource(autocomplete_edit_handle)->model()->result();
2061 for (AutocompleteResult::const_iterator i = result.begin();
2062 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562063 matches->push_back(AutocompleteMatchData(*i));
2064 *success = true;
initial.commit09911bf2008-07-26 23:55:292065 }
initial.commit09911bf2008-07-26 23:55:292066}
2067
2068void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562069 int autocomplete_edit_handle,
2070 bool* success,
2071 bool* query_in_progress) {
2072 *success = false;
2073 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292074 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562075 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522076 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562077 *success = true;
initial.commit09911bf2008-07-26 23:55:292078 }
initial.commit09911bf2008-07-26 23:55:292079}
2080
[email protected]63514af2010-03-30 17:17:232081#if !defined(OS_MACOSX)
[email protected]28790922009-03-09 19:48:372082void AutomationProvider::OnMessageFromExternalHost(int handle,
2083 const std::string& message,
2084 const std::string& origin,
2085 const std::string& target) {
[email protected]f7a68432009-07-29 23:18:192086 RenderViewHost* view_host = GetViewForTab(handle);
2087 if (!view_host) {
2088 return;
[email protected]fa83e762008-08-15 21:41:392089 }
[email protected]f7a68432009-07-29 23:18:192090
2091 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
2092 view_host, message, origin, target)) {
2093 // Message was diverted.
2094 return;
2095 }
2096
2097 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
2098 origin, target, this, view_host, handle)) {
2099 // Message was diverted.
2100 return;
2101 }
2102
2103 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
2104 // Message was diverted.
2105 return;
2106 }
2107
2108 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:392109}
[email protected]a9024892009-06-16 23:13:552110
2111bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
2112 const std::string& message, const std::string& origin,
2113 const std::string& target) {
2114 if (target !=
2115 extension_automation_constants::kAutomationBrowserEventRequestTarget)
2116 return false;
2117
2118 if (origin != extension_automation_constants::kAutomationOrigin) {
2119 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
2120 return false;
2121 }
2122
2123 // The message is a JSON-encoded array with two elements, both strings. The
2124 // first is the name of the event to dispatch. The second is a JSON-encoding
2125 // of the arguments specific to that event.
[email protected]93d49d72009-10-23 20:00:202126 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false));
[email protected]a9024892009-06-16 23:13:552127 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
2128 LOG(WARNING) << "Invalid browser event specified through automation";
2129 return false;
2130 }
2131
2132 const ListValue* args = static_cast<const ListValue*>(message_value.get());
2133
2134 std::string event_name;
2135 if (!args->GetString(0, &event_name)) {
2136 LOG(WARNING) << "No browser event name specified through automation";
2137 return false;
2138 }
2139
2140 std::string json_args;
2141 if (!args->GetString(1, &json_args)) {
2142 LOG(WARNING) << "No browser event args specified through automation";
2143 return false;
2144 }
2145
[email protected]7120f132009-07-20 21:05:372146 if (profile()->GetExtensionMessageService()) {
[email protected]db7331a2010-02-25 22:10:502147 profile()->GetExtensionMessageService()->DispatchEventToRenderers(
2148 event_name, json_args, profile()->IsOffTheRecord());
[email protected]7120f132009-07-20 21:05:372149 }
[email protected]a9024892009-06-16 23:13:552150
2151 return true;
2152}
[email protected]5ae5bed2009-08-21 18:52:442153#endif // !defined(OS_MACOSX)
[email protected]fa83e762008-08-15 21:41:392154
[email protected]57c6a652009-05-04 07:58:342155TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572156 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572157 if (tab_tracker_->ContainsHandle(handle)) {
2158 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342159 if (tab)
2160 *tab = nav_controller;
2161 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572162 }
[email protected]57c6a652009-05-04 07:58:342163 return NULL;
[email protected]20e93d12008-08-28 16:31:572164}
2165
initial.commit09911bf2008-07-26 23:55:292166TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2167 : AutomationProvider(profile) {
2168 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142169 registrar_.Add(this, NotificationType::SESSION_END,
2170 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292171}
2172
2173TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292174 BrowserList::RemoveObserver(this);
2175}
2176
2177void TestingAutomationProvider::OnChannelError() {
[email protected]a9324442009-10-12 04:32:142178 BrowserList::CloseAllBrowsersAndExit();
initial.commit09911bf2008-07-26 23:55:292179 AutomationProvider::OnChannelError();
2180}
2181
2182void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2183 // For backwards compatibility with the testing automation interface, we
2184 // want the automation provider (and hence the process) to go away when the
2185 // last browser goes away.
2186 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292187 // If you change this, update Observer for NotificationType::SESSION_END
2188 // below.
[email protected]295039bd2008-08-15 04:32:572189 MessageLoop::current()->PostTask(FROM_HERE,
2190 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292191 }
2192}
2193
2194void TestingAutomationProvider::Observe(NotificationType type,
2195 const NotificationSource& source,
2196 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562197 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292198 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2199 // before the task runs resulting in this object not being deleted. This
2200 // Release balance out the Release scheduled by OnBrowserRemoving.
2201 Release();
2202}
[email protected]295039bd2008-08-15 04:32:572203
2204void TestingAutomationProvider::OnRemoveProvider() {
2205 AutomationProviderList::GetInstance()->RemoveProvider(this);
2206}
[email protected]8a3422c92008-09-24 17:42:422207
[email protected]816633a2009-11-11 21:48:182208void AutomationProvider::GetInfoBarCount(int handle, int* count) {
[email protected]71f65dd2009-02-11 19:14:562209 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422210 if (tab_tracker_->ContainsHandle(handle)) {
2211 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342212 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112213 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422214 }
[email protected]8a3422c92008-09-24 17:42:422215}
2216
[email protected]816633a2009-11-11 21:48:182217void AutomationProvider::ClickInfoBarAccept(int handle,
2218 int info_bar_index,
2219 bool wait_for_navigation,
2220 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422221 bool success = false;
2222 if (tab_tracker_->ContainsHandle(handle)) {
2223 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2224 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112225 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422226 if (info_bar_index >= 0 && info_bar_index < count) {
2227 if (wait_for_navigation) {
[email protected]7dad3d5f2010-03-04 00:27:012228 AddNavigationStatusListener(nav_controller, reply_message, 1, false);
[email protected]8a3422c92008-09-24 17:42:422229 }
[email protected]eb9ba192008-12-02 02:41:342230 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112231 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342232 info_bar_index);
2233 if (delegate->AsConfirmInfoBarDelegate())
2234 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422235 success = true;
2236 }
2237 }
[email protected]4f3dc372009-02-24 00:10:292238 }
[email protected]58f622a62009-10-04 01:17:552239
2240 // This "!wait_for_navigation || !success condition" logic looks suspicious.
2241 // It will send a failure message when success is true but
2242 // |wait_for_navigation| is false.
2243 // TODO(phajdan.jr): investgate whether the reply param (currently
2244 // AUTOMATION_MSG_NAVIGATION_ERROR) should depend on success.
[email protected]8a3422c92008-09-24 17:42:422245 if (!wait_for_navigation || !success)
[email protected]816633a2009-11-11 21:48:182246 AutomationMsg_ClickInfoBarAccept::WriteReplyParams(
[email protected]457f5cf2009-08-18 16:37:522247 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]8a3422c92008-09-24 17:42:422248}
2249
[email protected]71f65dd2009-02-11 19:14:562250void AutomationProvider::GetLastNavigationTime(int handle,
2251 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422252 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562253 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422254}
2255
[email protected]71f65dd2009-02-11 19:14:562256void AutomationProvider::WaitForNavigation(int handle,
2257 int64 last_navigation_time,
2258 IPC::Message* reply_message) {
[email protected]5fa7acd2009-09-25 20:04:252259 NavigationController* controller = tab_tracker_->GetResource(handle);
[email protected]8a3422c92008-09-24 17:42:422260 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]5fa7acd2009-09-25 20:04:252261
[email protected]8a3422c92008-09-24 17:42:422262 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562263 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
[email protected]457f5cf2009-08-18 16:37:522264 controller == NULL ? AUTOMATION_MSG_NAVIGATION_ERROR :
2265 AUTOMATION_MSG_NAVIGATION_SUCCESS);
[email protected]58f622a62009-10-04 01:17:552266 Send(reply_message);
[email protected]4f3dc372009-02-24 00:10:292267 return;
[email protected]8a3422c92008-09-24 17:42:422268 }
2269
[email protected]7dad3d5f2010-03-04 00:27:012270 AddNavigationStatusListener(controller, reply_message, 1, true);
[email protected]8a3422c92008-09-24 17:42:422271}
2272
[email protected]71f65dd2009-02-11 19:14:562273void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162274 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562275 int value,
2276 bool* success) {
2277 *success = false;
[email protected]8a3422c92008-09-24 17:42:422278 if (browser_tracker_->ContainsHandle(handle)) {
2279 Browser* browser = browser_tracker_->GetResource(handle);
2280 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562281 *success = true;
[email protected]8a3422c92008-09-24 17:42:422282 }
[email protected]8a3422c92008-09-24 17:42:422283}
[email protected]97fa6ce32008-12-19 01:48:162284
[email protected]71f65dd2009-02-11 19:14:562285void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162286 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562287 const std::wstring& value,
2288 bool* success) {
2289 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162290 if (browser_tracker_->ContainsHandle(handle)) {
2291 Browser* browser = browser_tracker_->GetResource(handle);
2292 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562293 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162294 }
[email protected]97fa6ce32008-12-19 01:48:162295}
2296
[email protected]71f65dd2009-02-11 19:14:562297void AutomationProvider::GetBooleanPreference(int handle,
2298 const std::wstring& name,
[email protected]b8f48d12009-11-09 20:14:362299 bool* success,
2300 bool* value) {
[email protected]71f65dd2009-02-11 19:14:562301 *success = false;
2302 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162303 if (browser_tracker_->ContainsHandle(handle)) {
2304 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562305 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2306 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162307 }
[email protected]97fa6ce32008-12-19 01:48:162308}
2309
[email protected]71f65dd2009-02-11 19:14:562310void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162311 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562312 bool value,
2313 bool* success) {
2314 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162315 if (browser_tracker_->ContainsHandle(handle)) {
2316 Browser* browser = browser_tracker_->GetResource(handle);
2317 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562318 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162319 }
[email protected]97fa6ce32008-12-19 01:48:162320}
2321
2322// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562323void AutomationProvider::GetPageCurrentEncoding(
[email protected]41fc0322009-09-04 22:23:402324 int tab_handle, std::string* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162325 if (tab_tracker_->ContainsHandle(tab_handle)) {
2326 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2327 Browser* browser = FindAndActivateTab(nav);
2328 DCHECK(browser);
2329
[email protected]57c6a652009-05-04 07:58:342330 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2331 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162332 }
[email protected]97fa6ce32008-12-19 01:48:162333}
2334
[email protected]b8f48d12009-11-09 20:14:362335// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562336void AutomationProvider::OverrideEncoding(int tab_handle,
[email protected]41fc0322009-09-04 22:23:402337 const std::string& encoding_name,
[email protected]71f65dd2009-02-11 19:14:562338 bool* success) {
2339 *success = false;
[email protected]de246f52009-02-25 18:25:452340#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:162341 if (tab_tracker_->ContainsHandle(tab_handle)) {
2342 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
[email protected]2f2afba2010-04-01 01:53:192343 if (!nav)
2344 return;
[email protected]97fa6ce32008-12-19 01:48:162345 Browser* browser = FindAndActivateTab(nav);
[email protected]97fa6ce32008-12-19 01:48:162346
[email protected]2f2afba2010-04-01 01:53:192347 // If the browser has UI, simulate what a user would do.
2348 // Activate the tab and then click the encoding menu.
2349 if (browser &&
2350 browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162351 int selected_encoding_id =
2352 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2353 if (selected_encoding_id) {
2354 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562355 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162356 }
[email protected]2f2afba2010-04-01 01:53:192357 } else {
2358 // There is no UI, Chrome probably runs as Chrome-Frame mode.
2359 // Try to get TabContents and call its override_encoding method.
2360 TabContents* contents = nav->tab_contents();
2361 if (!contents)
2362 return;
2363 const std::string selected_encoding =
2364 CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name);
2365 if (selected_encoding.empty())
2366 return;
2367 contents->SetOverrideEncoding(selected_encoding);
[email protected]97fa6ce32008-12-19 01:48:162368 }
2369 }
[email protected]de246f52009-02-25 18:25:452370#else
2371 // TODO(port): Enable when encoding-related parts of Browser are ported.
2372 NOTIMPLEMENTED();
2373#endif
[email protected]97fa6ce32008-12-19 01:48:162374}
[email protected]5bcdb312009-01-07 21:43:202375
[email protected]4d434a1a2009-02-11 21:06:572376void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202377 SavePackage::SetShouldPromptUser(should_prompt);
2378}
[email protected]87eab222009-03-13 00:47:452379
[email protected]66ba4932009-06-04 19:22:132380void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
2381 *count = -1; // -1 is the error code
2382 if (tab_tracker_->ContainsHandle(handle)) {
2383 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2384 TabContents* tab_contents = nav_controller->tab_contents();
2385 if (tab_contents) {
2386 BlockedPopupContainer* container =
2387 tab_contents->blocked_popup_container();
2388 if (container) {
2389 *count = static_cast<int>(container->GetBlockedPopupCount());
2390 } else {
2391 // If we don't have a container, we don't have any blocked popups to
2392 // contain!
2393 *count = 0;
2394 }
2395 }
2396 }
2397}
[email protected]f7a68432009-07-29 23:18:192398
2399void AutomationProvider::SelectAll(int tab_handle) {
2400 RenderViewHost* view = GetViewForTab(tab_handle);
2401 if (!view) {
2402 NOTREACHED();
2403 return;
2404 }
2405
2406 view->SelectAll();
2407}
2408
2409void AutomationProvider::Cut(int tab_handle) {
2410 RenderViewHost* view = GetViewForTab(tab_handle);
2411 if (!view) {
2412 NOTREACHED();
2413 return;
2414 }
2415
2416 view->Cut();
2417}
2418
2419void AutomationProvider::Copy(int tab_handle) {
2420 RenderViewHost* view = GetViewForTab(tab_handle);
2421 if (!view) {
2422 NOTREACHED();
2423 return;
2424 }
2425
2426 view->Copy();
2427}
2428
2429void AutomationProvider::Paste(int tab_handle) {
2430 RenderViewHost* view = GetViewForTab(tab_handle);
2431 if (!view) {
2432 NOTREACHED();
2433 return;
2434 }
2435
2436 view->Paste();
2437}
2438
2439void AutomationProvider::ReloadAsync(int tab_handle) {
2440 if (tab_tracker_->ContainsHandle(tab_handle)) {
2441 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2442 if (!tab) {
2443 NOTREACHED();
2444 return;
2445 }
2446
[email protected]106a0812010-03-18 00:15:122447 const bool check_for_repost = true;
2448 tab->Reload(check_for_repost);
[email protected]f7a68432009-07-29 23:18:192449 }
2450}
2451
2452void AutomationProvider::StopAsync(int tab_handle) {
2453 RenderViewHost* view = GetViewForTab(tab_handle);
2454 if (!view) {
[email protected]8b2b3312009-09-14 18:38:362455 // We tolerate StopAsync being called even before a view has been created.
2456 // So just log a warning instead of a NOTREACHED().
2457 DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle;
[email protected]f7a68432009-07-29 23:18:192458 return;
2459 }
2460
2461 view->Stop();
2462}
2463
[email protected]1bb5f892009-10-06 01:44:572464void AutomationProvider::OnSetPageFontSize(int tab_handle,
2465 int font_size) {
2466 AutomationPageFontSize automation_font_size =
2467 static_cast<AutomationPageFontSize>(font_size);
2468
2469 if (automation_font_size < SMALLEST_FONT ||
2470 automation_font_size > LARGEST_FONT) {
2471 DLOG(ERROR) << "Invalid font size specified : "
2472 << font_size;
2473 return;
2474 }
2475
2476 if (tab_tracker_->ContainsHandle(tab_handle)) {
2477 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2478 DCHECK(tab != NULL);
2479 if (tab && tab->tab_contents()) {
2480 DCHECK(tab->tab_contents()->profile() != NULL);
2481 tab->tab_contents()->profile()->GetPrefs()->SetInteger(
2482 prefs::kWebKitDefaultFontSize, font_size);
2483 }
2484 }
2485}
2486
[email protected]bc73b4e52010-03-26 04:16:202487void AutomationProvider::RemoveBrowsingData(int remove_mask) {
2488 BrowsingDataRemover* remover;
2489 remover = new BrowsingDataRemover(profile(),
2490 BrowsingDataRemover::EVERYTHING, // All time periods.
2491 base::Time());
2492 remover->Remove(remove_mask);
2493 // BrowsingDataRemover deletes itself.
2494}
[email protected]1bb5f892009-10-06 01:44:572495
[email protected]2949e90d2009-08-21 15:32:522496void AutomationProvider::WaitForBrowserWindowCountToBecome(
2497 int target_count, IPC::Message* reply_message) {
2498 if (static_cast<int>(BrowserList::size()) == target_count) {
2499 AutomationMsg_WaitForBrowserWindowCountToBecome::WriteReplyParams(
2500 reply_message, true);
2501 Send(reply_message);
2502 return;
2503 }
2504
2505 // Set up an observer (it will delete itself).
2506 new BrowserCountChangeNotificationObserver(target_count, this, reply_message);
2507}
2508
2509void AutomationProvider::WaitForAppModalDialogToBeShown(
2510 IPC::Message* reply_message) {
2511 if (Singleton<AppModalDialogQueue>()->HasActiveDialog()) {
2512 AutomationMsg_WaitForAppModalDialogToBeShown::WriteReplyParams(
2513 reply_message, true);
2514 Send(reply_message);
2515 return;
2516 }
2517
2518 // Set up an observer (it will delete itself).
2519 new AppModalDialogShownObserver(this, reply_message);
2520}
2521
[email protected]1126a1d32009-08-26 15:39:262522void AutomationProvider::GoBackBlockUntilNavigationsComplete(
2523 int handle, int number_of_navigations, IPC::Message* reply_message) {
2524 if (tab_tracker_->ContainsHandle(handle)) {
2525 NavigationController* tab = tab_tracker_->GetResource(handle);
2526 Browser* browser = FindAndActivateTab(tab);
2527 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]7dad3d5f2010-03-04 00:27:012528 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2529 false);
[email protected]1126a1d32009-08-26 15:39:262530 browser->GoBack(CURRENT_TAB);
2531 return;
2532 }
2533 }
2534
2535 AutomationMsg_GoBackBlockUntilNavigationsComplete::WriteReplyParams(
2536 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2537 Send(reply_message);
2538}
2539
2540void AutomationProvider::GoForwardBlockUntilNavigationsComplete(
2541 int handle, int number_of_navigations, IPC::Message* reply_message) {
2542 if (tab_tracker_->ContainsHandle(handle)) {
2543 NavigationController* tab = tab_tracker_->GetResource(handle);
2544 Browser* browser = FindAndActivateTab(tab);
2545 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]7dad3d5f2010-03-04 00:27:012546 AddNavigationStatusListener(tab, reply_message, number_of_navigations,
2547 false);
[email protected]1126a1d32009-08-26 15:39:262548 browser->GoForward(CURRENT_TAB);
2549 return;
2550 }
2551 }
2552
2553 AutomationMsg_GoForwardBlockUntilNavigationsComplete::WriteReplyParams(
2554 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
2555 Send(reply_message);
2556}
2557
[email protected]f7a68432009-07-29 23:18:192558RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) {
2559 if (tab_tracker_->ContainsHandle(tab_handle)) {
2560 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2561 if (!tab) {
2562 NOTREACHED();
2563 return NULL;
2564 }
2565
2566 TabContents* tab_contents = tab->tab_contents();
2567 if (!tab_contents) {
2568 NOTREACHED();
2569 return NULL;
2570 }
2571
2572 RenderViewHost* view_host = tab_contents->render_view_host();
2573 return view_host;
2574 }
2575
2576 return NULL;
2577}
[email protected]675595f2009-08-26 22:32:042578
2579void AutomationProvider::GetBrowserForWindow(int window_handle,
2580 bool* success,
2581 int* browser_handle) {
2582 *success = false;
2583 *browser_handle = 0;
2584
2585 gfx::NativeWindow window = window_tracker_->GetResource(window_handle);
2586 if (!window)
2587 return;
2588
2589 BrowserList::const_iterator iter = BrowserList::begin();
2590 for (;iter != BrowserList::end(); ++iter) {
2591 gfx::NativeWindow this_window = (*iter)->window()->GetNativeHandle();
2592 if (window == this_window) {
2593 // Add() returns the existing handle for the resource if any.
2594 *browser_handle = browser_tracker_->Add(*iter);
2595 *success = true;
2596 return;
2597 }
2598 }
2599}
[email protected]d11c8e92009-10-20 23:26:402600
2601void AutomationProvider::InstallExtension(const FilePath& crx_path,
2602 IPC::Message* reply_message) {
2603 ExtensionsService* service = profile_->GetExtensionsService();
2604 if (service) {
2605 // The observer will delete itself when done.
2606 new ExtensionNotificationObserver(this,
2607 AutomationMsg_InstallExtension::ID,
2608 reply_message);
2609
2610 const FilePath& install_dir = service->install_directory();
[email protected]6dfbbf82010-03-12 23:09:162611 scoped_refptr<CrxInstaller> installer(
2612 new CrxInstaller(install_dir,
2613 service,
2614 NULL)); // silent install, no UI
2615 installer->set_allow_privilege_increase(true);
2616 installer->InstallCrx(crx_path);
[email protected]d11c8e92009-10-20 23:26:402617 } else {
2618 AutomationMsg_InstallExtension::WriteReplyParams(
2619 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2620 Send(reply_message);
2621 }
2622}
2623
2624void AutomationProvider::LoadExpandedExtension(
2625 const FilePath& extension_dir,
2626 IPC::Message* reply_message) {
[email protected]a4378252010-02-09 08:14:382627 if (profile_->GetExtensionsService()) {
[email protected]d11c8e92009-10-20 23:26:402628 // The observer will delete itself when done.
2629 new ExtensionNotificationObserver(this,
2630 AutomationMsg_LoadExpandedExtension::ID,
2631 reply_message);
2632
2633 profile_->GetExtensionsService()->LoadExtension(extension_dir);
[email protected]d11c8e92009-10-20 23:26:402634 } else {
2635 AutomationMsg_LoadExpandedExtension::WriteReplyParams(
2636 reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
2637 Send(reply_message);
2638 }
2639}
[email protected]673fd2c02010-02-04 23:10:002640
[email protected]a1e62d12010-03-16 02:18:432641void AutomationProvider::GetEnabledExtensions(
2642 std::vector<FilePath>* result) {
2643 ExtensionsService* service = profile_->GetExtensionsService();
2644 DCHECK(service);
2645 if (service->extensions_enabled()) {
2646 const ExtensionList* extensions = service->extensions();
2647 DCHECK(extensions);
2648 for (size_t i = 0; i < extensions->size(); ++i) {
2649 Extension* extension = (*extensions)[i];
2650 DCHECK(extension);
[email protected]237f281672010-03-20 12:37:072651 if (extension->location() == Extension::INTERNAL) {
2652 result->push_back(extension->path());
2653 }
[email protected]a1e62d12010-03-16 02:18:432654 }
2655 }
2656}
2657
[email protected]673fd2c02010-02-04 23:10:002658void AutomationProvider::SaveAsAsync(int tab_handle) {
2659 NavigationController* tab = NULL;
2660 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2661 if (tab_contents)
2662 tab_contents->OnSavePage();
2663}
[email protected]7dad3d5f2010-03-04 00:27:012664
2665void AutomationProvider::SetContentSetting(
2666 int handle,
2667 const std::string& host,
2668 ContentSettingsType content_type,
2669 ContentSetting setting,
2670 bool* success) {
2671 *success = false;
2672 if (browser_tracker_->ContainsHandle(handle)) {
2673 Browser* browser = browser_tracker_->GetResource(handle);
2674 HostContentSettingsMap* map =
2675 browser->profile()->GetHostContentSettingsMap();
2676 if (host.empty()) {
2677 map->SetDefaultContentSetting(content_type, setting);
2678 } else {
2679 map->SetContentSetting(host, content_type, setting);
2680 }
2681 *success = true;
2682 }
2683}
[email protected]cc824372010-03-31 15:33:012684
2685#if !defined(TOOLKIT_VIEWS)
2686void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
2687 NOTIMPLEMENTED();
2688};
2689
2690void AutomationProvider::WaitForFocusedViewIDToChange(
2691 int handle, int previous_view_id, IPC::Message* reply_message) {
2692 NOTIMPLEMENTED();
2693}
2694
2695void AutomationProvider::StartTrackingPopupMenus(
2696 int browser_handle, bool* success) {
2697 NOTIMPLEMENTED();
2698}
2699
2700void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
2701 NOTIMPLEMENTED();
2702}
2703#endif // !defined(TOOLKIT_VIEWS)