blob: ce26d28c373f6e5afa96a4d1001537039ae4bde1 [file] [log] [blame]
[email protected]4801ecc2009-04-05 04:52:581// Copyright (c) 2006-2009 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]202e7a72009-06-15 03:48:367#include "app/l10n_util.h"
[email protected]37126212009-05-06 02:23:318#include "app/message_box_flags.h"
[email protected]c6cb1992009-04-13 16:45:299#include "base/file_version_info.h"
[email protected]a9024892009-06-16 23:13:5510#include "base/json_reader.h"
[email protected]5fac9622009-02-04 21:49:3811#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/path_service.h"
[email protected]f44265b2009-05-19 18:52:5013#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2714#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3815#include "base/thread.h"
[email protected]a7eee32f2009-05-22 18:08:1716#include "base/values.h"
[email protected]4f3dc372009-02-24 00:10:2917#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2818#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0919#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3320#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2921#include "chrome/browser/automation/automation_provider_list.h"
[email protected]a9024892009-06-16 23:13:5522#include "chrome/browser/automation/extension_automation_constants.h"
[email protected]f44265b2009-05-19 18:52:5023#include "chrome/browser/automation/extension_port_container.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/browser/automation/url_request_failed_dns_job.h"
25#include "chrome/browser/automation/url_request_mock_http_job.h"
26#include "chrome/browser/automation/url_request_slow_download_job.h"
[email protected]66ba4932009-06-04 19:22:1327#include "chrome/browser/blocked_popup_container.h"
[email protected]5c238752009-06-13 10:29:0728#include "chrome/browser/browser_process.h"
[email protected]f3e99e32008-07-30 04:48:3929#include "chrome/browser/browser_window.h"
initial.commit09911bf2008-07-26 23:55:2930#include "chrome/browser/dom_operation_notification_details.h"
[email protected]d9f9b792009-06-24 13:17:1231#include "chrome/browser/debugger/devtools_manager.h"
[email protected]cdaa8652008-09-13 02:48:5932#include "chrome/browser/download/download_manager.h"
[email protected]59560e0b2009-06-04 03:30:2233#include "chrome/browser/download/download_shelf.h"
[email protected]a9024892009-06-16 23:13:5534#include "chrome/browser/extensions/extension_message_service.h"
[email protected]4801ecc2009-04-05 04:52:5835#include "chrome/browser/find_bar.h"
36#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2937#include "chrome/browser/find_notification_details.h"
[email protected]13869dd2009-05-05 00:40:0638#include "chrome/browser/location_bar.h"
[email protected]a7eee32f2009-05-22 18:08:1739#include "chrome/browser/profile_manager.h"
[email protected]6524b5f92009-01-22 17:48:2540#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0341#include "chrome/browser/ssl/ssl_manager.h"
42#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3443#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4544#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]a7eee32f2009-05-22 18:08:1745#include "chrome/common/automation_constants.h"
initial.commit09911bf2008-07-26 23:55:2946#include "chrome/common/chrome_paths.h"
[email protected]a7eee32f2009-05-22 18:08:1747#include "chrome/common/json_value_serializer.h"
[email protected]1c58a5c2009-05-21 18:47:1448#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4749#include "chrome/common/platform_util.h"
[email protected]8a3422c92008-09-24 17:42:4250#include "chrome/common/pref_service.h"
[email protected]71f65dd2009-02-11 19:14:5651#include "chrome/test/automation/automation_messages.h"
initial.commit09911bf2008-07-26 23:55:2952#include "net/base/cookie_monster.h"
[email protected]a7eee32f2009-05-22 18:08:1753#include "net/proxy/proxy_service.h"
54#include "net/proxy/proxy_config_service_fixed.h"
[email protected]319d9e6f2009-02-18 19:47:2155#include "net/url_request/url_request_context.h"
initial.commit09911bf2008-07-26 23:55:2956#include "net/url_request/url_request_filter.h"
57
[email protected]de246f52009-02-25 18:25:4558#if defined(OS_WIN)
59// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4560#include "chrome/browser/automation/ui_controls.h"
61#include "chrome/browser/character_encoding.h"
62#include "chrome/browser/download/save_package.h"
63#include "chrome/browser/external_tab_container.h"
[email protected]de246f52009-02-25 18:25:4564#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4565#endif // defined(OS_WIN)
66
[email protected]e8382172009-06-19 22:16:2867#if defined(OS_WIN) || defined(OS_LINUX)
68// TODO(port): Port these to the mac.
69#include "chrome/browser/login_prompt.h"
70#endif
71
[email protected]13869dd2009-05-05 00:40:0672#if defined(OS_WIN)
73#include "chrome/browser/views/bookmark_bar_view.h"
[email protected]9095e982009-05-27 17:28:2474#include "views/widget/root_view.h"
[email protected]2362e4f2009-05-08 00:34:0575#include "views/widget/widget_win.h"
76#include "views/window/window.h"
[email protected]13869dd2009-05-05 00:40:0677#endif
78
[email protected]e1acf6f2008-10-27 20:43:3379using base::Time;
80
initial.commit09911bf2008-07-26 23:55:2981class InitialLoadObserver : public NotificationObserver {
82 public:
83 InitialLoadObserver(size_t tab_count, AutomationProvider* automation)
[email protected]66791d22009-02-24 20:11:3384 : automation_(automation),
85 outstanding_tab_count_(tab_count) {
initial.commit09911bf2008-07-26 23:55:2986 if (outstanding_tab_count_ > 0) {
[email protected]bfd04a62009-02-01 18:16:5687 registrar_.Add(this, NotificationType::LOAD_START,
[email protected]6a02963e2009-01-06 16:58:0388 NotificationService::AllSources());
[email protected]bfd04a62009-02-01 18:16:5689 registrar_.Add(this, NotificationType::LOAD_STOP,
[email protected]6a02963e2009-01-06 16:58:0390 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:2991 }
92 }
93
94 ~InitialLoadObserver() {
initial.commit09911bf2008-07-26 23:55:2995 }
96
97 void ConditionMet() {
[email protected]6a02963e2009-01-06 16:58:0398 registrar_.RemoveAll();
initial.commit09911bf2008-07-26 23:55:2999 automation_->Send(new AutomationMsg_InitialLoadsComplete(0));
100 }
101
initial.commit09911bf2008-07-26 23:55:29102 virtual void Observe(NotificationType type,
103 const NotificationSource& source,
104 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56105 if (type == NotificationType::LOAD_START) {
initial.commit09911bf2008-07-26 23:55:29106 if (outstanding_tab_count_ > loading_tabs_.size())
107 loading_tabs_.insert(source.map_key());
[email protected]bfd04a62009-02-01 18:16:56108 } else if (type == NotificationType::LOAD_STOP) {
initial.commit09911bf2008-07-26 23:55:29109 if (outstanding_tab_count_ > finished_tabs_.size()) {
110 if (loading_tabs_.find(source.map_key()) != loading_tabs_.end())
111 finished_tabs_.insert(source.map_key());
112 if (outstanding_tab_count_ == finished_tabs_.size())
113 ConditionMet();
114 }
115 } else {
116 NOTREACHED();
117 }
118 }
119
120 private:
121 typedef std::set<uintptr_t> TabSet;
122
[email protected]6a02963e2009-01-06 16:58:03123 NotificationRegistrar registrar_;
124
initial.commit09911bf2008-07-26 23:55:29125 AutomationProvider* automation_;
126 size_t outstanding_tab_count_;
127 TabSet loading_tabs_;
128 TabSet finished_tabs_;
129};
130
131// Watches for NewTabUI page loads for performance timing purposes.
132class NewTabUILoadObserver : public NotificationObserver {
133 public:
134 explicit NewTabUILoadObserver(AutomationProvider* automation)
135 : automation_(automation) {
[email protected]1c58a5c2009-05-21 18:47:14136 registrar_.Add(this, NotificationType::INITIAL_NEW_TAB_UI_LOAD,
137 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29138 }
139
140 ~NewTabUILoadObserver() {
initial.commit09911bf2008-07-26 23:55:29141 }
142
143 virtual void Observe(NotificationType type,
144 const NotificationSource& source,
145 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56146 if (type == NotificationType::INITIAL_NEW_TAB_UI_LOAD) {
initial.commit09911bf2008-07-26 23:55:29147 Details<int> load_time(details);
148 automation_->Send(
149 new AutomationMsg_InitialNewTabUILoadComplete(0, *load_time.ptr()));
150 } else {
151 NOTREACHED();
152 }
153 }
154
155 private:
[email protected]1c58a5c2009-05-21 18:47:14156 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29157 AutomationProvider* automation_;
158};
159
160class NavigationControllerRestoredObserver : public NotificationObserver {
161 public:
162 NavigationControllerRestoredObserver(AutomationProvider* automation,
163 NavigationController* controller,
[email protected]71f65dd2009-02-11 19:14:56164 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29165 : automation_(automation),
166 controller_(controller),
[email protected]71f65dd2009-02-11 19:14:56167 reply_message_(reply_message) {
initial.commit09911bf2008-07-26 23:55:29168 if (FinishedRestoring()) {
initial.commit09911bf2008-07-26 23:55:29169 SendDone();
170 } else {
[email protected]1c58a5c2009-05-21 18:47:14171 registrar_.Add(this, NotificationType::LOAD_STOP,
172 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29173 }
174 }
175
176 ~NavigationControllerRestoredObserver() {
initial.commit09911bf2008-07-26 23:55:29177 }
178
179 virtual void Observe(NotificationType type,
180 const NotificationSource& source,
181 const NotificationDetails& details) {
182 if (FinishedRestoring()) {
183 SendDone();
[email protected]1c58a5c2009-05-21 18:47:14184 registrar_.RemoveAll();
initial.commit09911bf2008-07-26 23:55:29185 }
186 }
187
188 private:
initial.commit09911bf2008-07-26 23:55:29189 bool FinishedRestoring() {
[email protected]7f0005a2009-04-15 03:25:11190 return (!controller_->needs_reload() && !controller_->pending_entry() &&
191 !controller_->tab_contents()->is_loading());
initial.commit09911bf2008-07-26 23:55:29192 }
193
194 void SendDone() {
[email protected]71f65dd2009-02-11 19:14:56195 DCHECK(reply_message_ != NULL);
196 automation_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:29197 }
198
[email protected]1c58a5c2009-05-21 18:47:14199 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29200 AutomationProvider* automation_;
201 NavigationController* controller_;
[email protected]71f65dd2009-02-11 19:14:56202 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29203
[email protected]5a52f162008-08-27 04:15:31204 DISALLOW_COPY_AND_ASSIGN(NavigationControllerRestoredObserver);
initial.commit09911bf2008-07-26 23:55:29205};
206
[email protected]71f65dd2009-02-11 19:14:56207template<class NavigationCodeType>
initial.commit09911bf2008-07-26 23:55:29208class NavigationNotificationObserver : public NotificationObserver {
209 public:
210 NavigationNotificationObserver(NavigationController* controller,
211 AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56212 IPC::Message* reply_message,
213 NavigationCodeType success_code,
214 NavigationCodeType auth_needed_code,
215 NavigationCodeType failed_code)
initial.commit09911bf2008-07-26 23:55:29216 : automation_(automation),
[email protected]71f65dd2009-02-11 19:14:56217 reply_message_(reply_message),
initial.commit09911bf2008-07-26 23:55:29218 controller_(controller),
[email protected]71f65dd2009-02-11 19:14:56219 navigation_started_(false),
220 success_code_(success_code),
221 auth_needed_code_(auth_needed_code),
222 failed_code_(failed_code) {
[email protected]1c58a5c2009-05-21 18:47:14223 Source<NavigationController> source(controller_);
224 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, source);
225 registrar_.Add(this, NotificationType::LOAD_START, source);
226 registrar_.Add(this, NotificationType::LOAD_STOP, source);
227 registrar_.Add(this, NotificationType::AUTH_NEEDED, source);
228 registrar_.Add(this, NotificationType::AUTH_SUPPLIED, source);
initial.commit09911bf2008-07-26 23:55:29229 }
230
231 ~NavigationNotificationObserver() {
[email protected]1c58a5c2009-05-21 18:47:14232 if (reply_message_) {
233 // This means we did not receive a notification for this navigation.
234 // Send over a failed navigation status back to the caller to ensure that
235 // the caller does not hang waiting for the response.
236 IPC::ParamTraits<NavigationCodeType>::Write(reply_message_,
237 failed_code_);
238 automation_->Send(reply_message_);
239 reply_message_ = NULL;
240 }
241
242 automation_->RemoveNavigationStatusListener(this);
initial.commit09911bf2008-07-26 23:55:29243 }
244
[email protected]71f65dd2009-02-11 19:14:56245 void ConditionMet(NavigationCodeType navigation_result) {
246 DCHECK(reply_message_ != NULL);
247
248 IPC::ParamTraits<NavigationCodeType>::Write(reply_message_,
249 navigation_result);
250 automation_->Send(reply_message_);
251 reply_message_ = NULL;
252
initial.commit09911bf2008-07-26 23:55:29253 delete this;
254 }
255
initial.commit09911bf2008-07-26 23:55:29256 virtual void Observe(NotificationType type,
257 const NotificationSource& source,
258 const NotificationDetails& details) {
[email protected]8a3422c92008-09-24 17:42:42259 // We listen for 2 events to determine when the navigation started because:
260 // - when this is used by the WaitForNavigation method, we might be invoked
261 // afer the load has started (but not after the entry was committed, as
262 // WaitForNavigation compares times of the last navigation).
263 // - when this is used with a page requiring authentication, we will not get
[email protected]4f3dc372009-02-24 00:10:29264 // a NotificationType::NAV_ENTRY_COMMITTED until after we authenticate, so
265 // we need the NotificationType::LOAD_START.
[email protected]bfd04a62009-02-01 18:16:56266 if (type == NotificationType::NAV_ENTRY_COMMITTED ||
267 type == NotificationType::LOAD_START) {
initial.commit09911bf2008-07-26 23:55:29268 navigation_started_ = true;
[email protected]bfd04a62009-02-01 18:16:56269 } else if (type == NotificationType::LOAD_STOP) {
initial.commit09911bf2008-07-26 23:55:29270 if (navigation_started_) {
271 navigation_started_ = false;
[email protected]71f65dd2009-02-11 19:14:56272 ConditionMet(success_code_);
initial.commit09911bf2008-07-26 23:55:29273 }
[email protected]bfd04a62009-02-01 18:16:56274 } else if (type == NotificationType::AUTH_SUPPLIED) {
initial.commit09911bf2008-07-26 23:55:29275 // The LoginHandler for this tab is no longer valid.
276 automation_->RemoveLoginHandler(controller_);
277
278 // Treat this as if navigation started again, since load start/stop don't
279 // occur while authentication is ongoing.
280 navigation_started_ = true;
[email protected]bfd04a62009-02-01 18:16:56281 } else if (type == NotificationType::AUTH_NEEDED) {
[email protected]de246f52009-02-25 18:25:45282#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29283 if (navigation_started_) {
284 // Remember the login handler that wants authentication.
285 LoginHandler* handler =
286 Details<LoginNotificationDetails>(details)->handler();
287 automation_->AddLoginHandler(controller_, handler);
288
289 // Respond that authentication is needed.
290 navigation_started_ = false;
[email protected]71f65dd2009-02-11 19:14:56291 ConditionMet(auth_needed_code_);
initial.commit09911bf2008-07-26 23:55:29292 } else {
293 NOTREACHED();
294 }
[email protected]de246f52009-02-25 18:25:45295#else
296 // TODO(port): Enable when we have LoginNotificationDetails etc.
297 NOTIMPLEMENTED();
298#endif
initial.commit09911bf2008-07-26 23:55:29299 } else {
300 NOTREACHED();
301 }
302 }
303
304 private:
[email protected]1c58a5c2009-05-21 18:47:14305 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29306 AutomationProvider* automation_;
[email protected]71f65dd2009-02-11 19:14:56307 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29308 NavigationController* controller_;
309 bool navigation_started_;
[email protected]71f65dd2009-02-11 19:14:56310 NavigationCodeType success_code_;
311 NavigationCodeType auth_needed_code_;
312 NavigationCodeType failed_code_;
initial.commit09911bf2008-07-26 23:55:29313};
314
315class TabStripNotificationObserver : public NotificationObserver {
316 public:
[email protected]1c58a5c2009-05-21 18:47:14317 TabStripNotificationObserver(NotificationType notification,
318 AutomationProvider* automation)
319 : automation_(automation),
320 notification_(notification) {
321 registrar_.Add(this, notification_, NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29322 }
323
324 virtual ~TabStripNotificationObserver() {
initial.commit09911bf2008-07-26 23:55:29325 }
326
327 virtual void Observe(NotificationType type,
328 const NotificationSource& source,
329 const NotificationDetails& details) {
330 if (type == notification_) {
331 ObserveTab(Source<NavigationController>(source).ptr());
332
333 // If verified, no need to observe anymore
334 automation_->RemoveTabStripObserver(this);
335 delete this;
336 } else {
337 NOTREACHED();
338 }
339 }
340
341 virtual void ObserveTab(NavigationController* controller) = 0;
342
343 protected:
[email protected]1c58a5c2009-05-21 18:47:14344 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29345 AutomationProvider* automation_;
initial.commit09911bf2008-07-26 23:55:29346 NotificationType notification_;
initial.commit09911bf2008-07-26 23:55:29347};
348
349class TabAppendedNotificationObserver : public TabStripNotificationObserver {
350 public:
351 TabAppendedNotificationObserver(Browser* parent,
[email protected]1c58a5c2009-05-21 18:47:14352 AutomationProvider* automation,
353 IPC::Message* reply_message)
354 : TabStripNotificationObserver(NotificationType::TAB_PARENTED,
355 automation),
356 parent_(parent),
[email protected]71f65dd2009-02-11 19:14:56357 reply_message_(reply_message) {
initial.commit09911bf2008-07-26 23:55:29358 }
359
360 virtual void ObserveTab(NavigationController* controller) {
[email protected]1c58a5c2009-05-21 18:47:14361 if (automation_->GetIndexForNavigationController(controller, parent_) ==
362 TabStripModel::kNoTab) {
363 // This tab notification doesn't belong to the parent_.
initial.commit09911bf2008-07-26 23:55:29364 return;
365 }
366
367 // Give the same response even if auth is needed, since it doesn't matter.
[email protected]71f65dd2009-02-11 19:14:56368 automation_->AddNavigationStatusListener<int>(
369 controller, reply_message_, AUTOMATION_MSG_NAVIGATION_SUCCESS,
370 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, AUTOMATION_MSG_NAVIGATION_ERROR);
initial.commit09911bf2008-07-26 23:55:29371 }
[email protected]71f65dd2009-02-11 19:14:56372
373 protected:
[email protected]1c58a5c2009-05-21 18:47:14374 Browser* parent_;
[email protected]71f65dd2009-02-11 19:14:56375 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29376};
377
378class TabClosedNotificationObserver : public TabStripNotificationObserver {
379 public:
[email protected]1c58a5c2009-05-21 18:47:14380 TabClosedNotificationObserver(AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56381 bool wait_until_closed,
382 IPC::Message* reply_message)
[email protected]1c58a5c2009-05-21 18:47:14383 : TabStripNotificationObserver(wait_until_closed ?
384 NotificationType::TAB_CLOSED : NotificationType::TAB_CLOSING,
385 automation),
386 reply_message_(reply_message),
387 for_browser_command_(false) {
initial.commit09911bf2008-07-26 23:55:29388 }
389
390 virtual void ObserveTab(NavigationController* controller) {
[email protected]1c58a5c2009-05-21 18:47:14391 if (for_browser_command_) {
[email protected]d79ffea2009-05-07 20:51:42392 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message_,
393 true);
[email protected]1c58a5c2009-05-21 18:47:14394 } else {
[email protected]d79ffea2009-05-07 20:51:42395 AutomationMsg_CloseTab::WriteReplyParams(reply_message_, true);
[email protected]1c58a5c2009-05-21 18:47:14396 }
[email protected]71f65dd2009-02-11 19:14:56397 automation_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:29398 }
[email protected]71f65dd2009-02-11 19:14:56399
[email protected]d79ffea2009-05-07 20:51:42400 void set_for_browser_command(bool for_browser_command) {
401 for_browser_command_ = for_browser_command;
402 }
[email protected]1c58a5c2009-05-21 18:47:14403
[email protected]71f65dd2009-02-11 19:14:56404 protected:
405 IPC::Message* reply_message_;
[email protected]d79ffea2009-05-07 20:51:42406 bool for_browser_command_;
initial.commit09911bf2008-07-26 23:55:29407};
408
[email protected]14c0a032009-04-13 18:15:14409class BrowserOpenedNotificationObserver : public NotificationObserver {
410 public:
411 BrowserOpenedNotificationObserver(AutomationProvider* automation,
412 IPC::Message* reply_message)
413 : automation_(automation),
[email protected]d79ffea2009-05-07 20:51:42414 reply_message_(reply_message),
415 for_browser_command_(false) {
[email protected]14c0a032009-04-13 18:15:14416 registrar_.Add(this, NotificationType::BROWSER_OPENED,
417 NotificationService::AllSources());
418 }
419
420 ~BrowserOpenedNotificationObserver() {
421 }
422
423 virtual void Observe(NotificationType type,
424 const NotificationSource& source,
425 const NotificationDetails& details) {
426 if (type == NotificationType::BROWSER_OPENED) {
[email protected]1c58a5c2009-05-21 18:47:14427 if (for_browser_command_) {
[email protected]d79ffea2009-05-07 20:51:42428 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message_,
429 true);
[email protected]1c58a5c2009-05-21 18:47:14430 }
[email protected]14c0a032009-04-13 18:15:14431 automation_->Send(reply_message_);
432 delete this;
433 } else {
434 NOTREACHED();
435 }
436 }
437
[email protected]d79ffea2009-05-07 20:51:42438 void set_for_browser_command(bool for_browser_command) {
439 for_browser_command_ = for_browser_command;
440 }
[email protected]1c58a5c2009-05-21 18:47:14441
[email protected]14c0a032009-04-13 18:15:14442 private:
[email protected]1c58a5c2009-05-21 18:47:14443 NotificationRegistrar registrar_;
[email protected]14c0a032009-04-13 18:15:14444 AutomationProvider* automation_;
445 IPC::Message* reply_message_;
[email protected]d79ffea2009-05-07 20:51:42446 bool for_browser_command_;
[email protected]14c0a032009-04-13 18:15:14447};
448
initial.commit09911bf2008-07-26 23:55:29449class BrowserClosedNotificationObserver : public NotificationObserver {
450 public:
451 BrowserClosedNotificationObserver(Browser* browser,
452 AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56453 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29454 : automation_(automation),
[email protected]d79ffea2009-05-07 20:51:42455 reply_message_(reply_message),
456 for_browser_command_(false) {
[email protected]c300deb32009-05-08 21:26:07457 registrar_.Add(this, NotificationType::BROWSER_CLOSED,
458 Source<Browser>(browser));
initial.commit09911bf2008-07-26 23:55:29459 }
460
461 virtual void Observe(NotificationType type,
462 const NotificationSource& source,
463 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56464 DCHECK(type == NotificationType::BROWSER_CLOSED);
initial.commit09911bf2008-07-26 23:55:29465 Details<bool> close_app(details);
[email protected]71f65dd2009-02-11 19:14:56466 DCHECK(reply_message_ != NULL);
[email protected]1c58a5c2009-05-21 18:47:14467 if (for_browser_command_) {
[email protected]d79ffea2009-05-07 20:51:42468 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message_,
469 true);
[email protected]1c58a5c2009-05-21 18:47:14470 } else {
[email protected]d79ffea2009-05-07 20:51:42471 AutomationMsg_CloseBrowser::WriteReplyParams(reply_message_, true,
472 *(close_app.ptr()));
[email protected]1c58a5c2009-05-21 18:47:14473 }
[email protected]71f65dd2009-02-11 19:14:56474 automation_->Send(reply_message_);
475 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29476 delete this;
477 }
478
[email protected]d79ffea2009-05-07 20:51:42479 void set_for_browser_command(bool for_browser_command) {
480 for_browser_command_ = for_browser_command;
481 }
[email protected]1c58a5c2009-05-21 18:47:14482
initial.commit09911bf2008-07-26 23:55:29483 private:
[email protected]c300deb32009-05-08 21:26:07484 NotificationRegistrar registrar_;
[email protected]1c58a5c2009-05-21 18:47:14485 AutomationProvider* automation_;
486 IPC::Message* reply_message_;
[email protected]d79ffea2009-05-07 20:51:42487 bool for_browser_command_;
initial.commit09911bf2008-07-26 23:55:29488};
489
[email protected]4e41709d2009-04-08 00:04:27490namespace {
491
492// Define mapping from command to notification
493struct CommandNotification {
494 int command;
495 NotificationType::Type notification_type;
496};
497
498const struct CommandNotification command_notifications[] = {
[email protected]14c0a032009-04-13 18:15:14499 {IDC_DUPLICATE_TAB, NotificationType::TAB_PARENTED},
[email protected]f8fc5882009-05-06 04:23:43500 {IDC_NEW_TAB, NotificationType::TAB_PARENTED},
[email protected]d79ffea2009-05-07 20:51:42501 // Returns as soon as the restored tab is created. To further wait until
502 // the content page is loaded, use WaitForTabToBeRestored.
[email protected]f8fc5882009-05-06 04:23:43503 {IDC_RESTORE_TAB, NotificationType::TAB_PARENTED}
[email protected]4e41709d2009-04-08 00:04:27504};
505
506} // namespace
507
[email protected]56e71b7c2009-03-27 03:05:56508class ExecuteBrowserCommandObserver : public NotificationObserver {
509 public:
[email protected]56e71b7c2009-03-27 03:05:56510 ~ExecuteBrowserCommandObserver() {
511 }
512
[email protected]d79ffea2009-05-07 20:51:42513 static bool CreateAndRegisterObserver(AutomationProvider* automation,
514 Browser* browser,
515 int command,
516 IPC::Message* reply_message) {
517 bool result = true;
518 switch (command) {
519 case IDC_NEW_WINDOW:
520 case IDC_NEW_INCOGNITO_WINDOW: {
521 BrowserOpenedNotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14522 new BrowserOpenedNotificationObserver(automation, reply_message);
[email protected]d79ffea2009-05-07 20:51:42523 observer->set_for_browser_command(true);
524 break;
525 }
526 case IDC_CLOSE_WINDOW: {
527 BrowserClosedNotificationObserver* observer =
528 new BrowserClosedNotificationObserver(browser, automation,
[email protected]d79ffea2009-05-07 20:51:42529 reply_message);
530 observer->set_for_browser_command(true);
531 break;
532 }
533 case IDC_CLOSE_TAB: {
534 TabClosedNotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14535 new TabClosedNotificationObserver(automation, true, reply_message);
[email protected]d79ffea2009-05-07 20:51:42536 observer->set_for_browser_command(true);
537 break;
538 }
[email protected]27e1c9e02009-05-13 18:41:39539 case IDC_BACK:
540 case IDC_FORWARD:
541 case IDC_RELOAD: {
542 automation->
543 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
544 &browser->GetSelectedTabContents()->controller(),
545 reply_message,
546 AUTOMATION_MSG_NAVIGATION_SUCCESS,
547 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
548 AUTOMATION_MSG_NAVIGATION_ERROR);
549 break;
550 }
[email protected]d79ffea2009-05-07 20:51:42551 default: {
552 ExecuteBrowserCommandObserver* observer =
553 new ExecuteBrowserCommandObserver(automation, reply_message);
554 if (!observer->Register(command)) {
555 delete observer;
556 result = false;
557 }
558 break;
559 }
560 }
561 return result;
[email protected]4e41709d2009-04-08 00:04:27562 }
563
[email protected]56e71b7c2009-03-27 03:05:56564 virtual void Observe(NotificationType type,
565 const NotificationSource& source,
566 const NotificationDetails& details) {
567 if (type == notification_type_) {
[email protected]49a14a82009-03-31 04:16:44568 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message_,
569 true);
[email protected]56e71b7c2009-03-27 03:05:56570 automation_->Send(reply_message_);
571 delete this;
572 } else {
573 NOTREACHED();
574 }
575 }
576
577 private:
[email protected]d79ffea2009-05-07 20:51:42578 ExecuteBrowserCommandObserver(AutomationProvider* automation,
579 IPC::Message* reply_message)
580 : automation_(automation),
581 reply_message_(reply_message) {
582 }
583
584 bool Register(int command) {
585 if (!GetNotificationType(command, &notification_type_))
586 return false;
[email protected]1c58a5c2009-05-21 18:47:14587 registrar_.Add(this, notification_type_, NotificationService::AllSources());
[email protected]d79ffea2009-05-07 20:51:42588 return true;
589 }
590
[email protected]4e41709d2009-04-08 00:04:27591 bool GetNotificationType(int command, NotificationType::Type* type) {
592 if (!type)
593 return false;
594 bool found = false;
595 for (unsigned int i = 0; i < arraysize(command_notifications); i++) {
596 if (command_notifications[i].command == command) {
597 *type = command_notifications[i].notification_type;
598 found = true;
599 break;
600 }
601 }
602 return found;
603 }
604
[email protected]1c58a5c2009-05-21 18:47:14605 NotificationRegistrar registrar_;
[email protected]56e71b7c2009-03-27 03:05:56606 AutomationProvider* automation_;
607 NotificationType::Type notification_type_;
608 IPC::Message* reply_message_;
[email protected]56e71b7c2009-03-27 03:05:56609};
610
initial.commit09911bf2008-07-26 23:55:29611class FindInPageNotificationObserver : public NotificationObserver {
612 public:
613 FindInPageNotificationObserver(AutomationProvider* automation,
614 TabContents* parent_tab,
[email protected]71f65dd2009-02-11 19:14:56615 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29616 : automation_(automation),
[email protected]71f65dd2009-02-11 19:14:56617 active_match_ordinal_(-1),
618 reply_message_(reply_message) {
[email protected]1c58a5c2009-05-21 18:47:14619 registrar_.Add(this, NotificationType::FIND_RESULT_AVAILABLE,
620 Source<TabContents>(parent_tab));
initial.commit09911bf2008-07-26 23:55:29621 }
622
623 ~FindInPageNotificationObserver() {
initial.commit09911bf2008-07-26 23:55:29624 }
625
[email protected]1c58a5c2009-05-21 18:47:14626 virtual void Observe(NotificationType type,
627 const NotificationSource& source,
628 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56629 if (type == NotificationType::FIND_RESULT_AVAILABLE) {
initial.commit09911bf2008-07-26 23:55:29630 Details<FindNotificationDetails> find_details(details);
631 if (find_details->request_id() == kFindInPageRequestId) {
[email protected]aedd85a2008-12-04 19:32:49632 // We get multiple responses and one of those will contain the ordinal.
633 // This message comes to us before the final update is sent.
634 if (find_details->active_match_ordinal() > -1)
635 active_match_ordinal_ = find_details->active_match_ordinal();
initial.commit09911bf2008-07-26 23:55:29636 if (find_details->final_update()) {
[email protected]71f65dd2009-02-11 19:14:56637 DCHECK(reply_message_ != NULL);
638
[email protected]1c58a5c2009-05-21 18:47:14639 AutomationMsg_FindInPage::WriteReplyParams(reply_message_,
640 active_match_ordinal_, find_details->number_of_matches());
[email protected]71f65dd2009-02-11 19:14:56641
642 automation_->Send(reply_message_);
643 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29644 } else {
645 DLOG(INFO) << "Ignoring, since we only care about the final message";
646 }
647 }
648 } else {
649 NOTREACHED();
650 }
651 }
652
653 // The Find mechanism is over asynchronous IPC, so a search is kicked off and
654 // we wait for notification to find out what the results are. As the user is
655 // typing, new search requests can be issued and the Request ID helps us make
656 // sense of whether this is the current request or an old one. The unit tests,
657 // however, which uses this constant issues only one search at a time, so we
658 // don't need a rolling id to identify each search. But, we still need to
659 // specify one, so we just use a fixed one - its value does not matter.
660 static const int kFindInPageRequestId;
[email protected]1c58a5c2009-05-21 18:47:14661
initial.commit09911bf2008-07-26 23:55:29662 private:
[email protected]1c58a5c2009-05-21 18:47:14663 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29664 AutomationProvider* automation_;
[email protected]aedd85a2008-12-04 19:32:49665 // We will at some point (before final update) be notified of the ordinal and
666 // we need to preserve it so we can send it later.
667 int active_match_ordinal_;
[email protected]71f65dd2009-02-11 19:14:56668 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29669};
670
671const int FindInPageNotificationObserver::kFindInPageRequestId = -1;
672
673class DomOperationNotificationObserver : public NotificationObserver {
674 public:
675 explicit DomOperationNotificationObserver(AutomationProvider* automation)
676 : automation_(automation) {
[email protected]1c58a5c2009-05-21 18:47:14677 registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
678 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29679 }
680
681 ~DomOperationNotificationObserver() {
initial.commit09911bf2008-07-26 23:55:29682 }
683
[email protected]1c58a5c2009-05-21 18:47:14684 virtual void Observe(NotificationType type,
685 const NotificationSource& source,
686 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56687 if (NotificationType::DOM_OPERATION_RESPONSE == type) {
initial.commit09911bf2008-07-26 23:55:29688 Details<DomOperationNotificationDetails> dom_op_details(details);
[email protected]71f65dd2009-02-11 19:14:56689
690 IPC::Message* reply_message = automation_->reply_message_release();
691 DCHECK(reply_message != NULL);
692
[email protected]1c58a5c2009-05-21 18:47:14693 AutomationMsg_DomOperation::WriteReplyParams(reply_message,
694 dom_op_details->json());
[email protected]71f65dd2009-02-11 19:14:56695 automation_->Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29696 }
697 }
[email protected]1c58a5c2009-05-21 18:47:14698
initial.commit09911bf2008-07-26 23:55:29699 private:
[email protected]1c58a5c2009-05-21 18:47:14700 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29701 AutomationProvider* automation_;
702};
703
[email protected]de246f52009-02-25 18:25:45704#if defined(OS_WIN)
705// TODO(port): Enable when printing is ported.
initial.commit09911bf2008-07-26 23:55:29706class DocumentPrintedNotificationObserver : public NotificationObserver {
707 public:
708 DocumentPrintedNotificationObserver(AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56709 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29710 : automation_(automation),
[email protected]71f65dd2009-02-11 19:14:56711 success_(false),
712 reply_message_(reply_message) {
[email protected]1c58a5c2009-05-21 18:47:14713 registrar_.Add(this, NotificationType::PRINT_JOB_EVENT,
714 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29715 }
716
717 ~DocumentPrintedNotificationObserver() {
[email protected]71f65dd2009-02-11 19:14:56718 DCHECK(reply_message_ != NULL);
719 AutomationMsg_PrintNow::WriteReplyParams(reply_message_, success_);
720 automation_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:29721 automation_->RemoveNavigationStatusListener(this);
initial.commit09911bf2008-07-26 23:55:29722 }
723
724 virtual void Observe(NotificationType type, const NotificationSource& source,
725 const NotificationDetails& details) {
726 using namespace printing;
[email protected]bfd04a62009-02-01 18:16:56727 DCHECK(type == NotificationType::PRINT_JOB_EVENT);
initial.commit09911bf2008-07-26 23:55:29728 switch (Details<JobEventDetails>(details)->type()) {
729 case JobEventDetails::JOB_DONE: {
730 // Succeeded.
731 success_ = true;
732 delete this;
733 break;
734 }
735 case JobEventDetails::USER_INIT_CANCELED:
736 case JobEventDetails::FAILED: {
737 // Failed.
738 delete this;
739 break;
740 }
741 case JobEventDetails::NEW_DOC:
742 case JobEventDetails::USER_INIT_DONE:
743 case JobEventDetails::DEFAULT_INIT_DONE:
744 case JobEventDetails::NEW_PAGE:
745 case JobEventDetails::PAGE_DONE:
746 case JobEventDetails::DOC_DONE:
747 case JobEventDetails::ALL_PAGES_REQUESTED: {
748 // Don't care.
749 break;
750 }
751 default: {
752 NOTREACHED();
753 break;
754 }
755 }
756 }
757
758 private:
[email protected]1c58a5c2009-05-21 18:47:14759 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29760 scoped_refptr<AutomationProvider> automation_;
initial.commit09911bf2008-07-26 23:55:29761 bool success_;
[email protected]71f65dd2009-02-11 19:14:56762 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29763};
[email protected]de246f52009-02-25 18:25:45764#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29765
[email protected]cbab76d2008-10-13 22:42:47766class AutomationInterstitialPage : public InterstitialPage {
767 public:
[email protected]57c6a652009-05-04 07:58:34768 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47769 const GURL& url,
770 const std::string& contents)
771 : InterstitialPage(tab, true, url),
772 contents_(contents) {
773 }
774
775 virtual std::string GetHTMLContents() { return contents_; }
776
777 private:
778 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29779
[email protected]cbab76d2008-10-13 22:42:47780 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
781};
782
initial.commit09911bf2008-07-26 23:55:29783AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57784 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56785 profile_(profile),
786 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29787 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29788 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20789 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29790 autocomplete_edit_tracker_.reset(
791 new AutomationAutocompleteEditTracker(this));
initial.commit09911bf2008-07-26 23:55:29792 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
793 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
initial.commit09911bf2008-07-26 23:55:29794}
795
796AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50797 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
798 port_containers_.end());
799 port_containers_.clear();
800
[email protected]0da050b92008-08-19 19:29:47801 // Make sure that any outstanding NotificationObservers also get destroyed.
802 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31803 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47804 while ((observer = it.GetNext()) != NULL)
805 delete observer;
initial.commit09911bf2008-07-26 23:55:29806}
807
[email protected]9a3a293b2009-06-04 22:28:16808void AutomationProvider::ConnectToChannel(const std::string& channel_id) {
[email protected]295039bd2008-08-15 04:32:57809 channel_.reset(
[email protected]71f65dd2009-02-11 19:14:56810 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this, NULL,
811 g_browser_process->io_thread()->message_loop(),
812 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05813 scoped_ptr<FileVersionInfo> file_version_info(
814 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40815 std::string version_string;
816 if (file_version_info != NULL) {
817 version_string = WideToASCII(file_version_info->file_version());
818 }
[email protected]c6cb1992009-04-13 16:45:29819
820 // Send a hello message with our current automation protocol version.
821 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29822}
823
824void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
825 if (expected_tabs == 0) {
826 Send(new AutomationMsg_InitialLoadsComplete(0));
827 } else {
828 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
829 }
830}
831
[email protected]71f65dd2009-02-11 19:14:56832template<class NavigationCodeType>
initial.commit09911bf2008-07-26 23:55:29833NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]71f65dd2009-02-11 19:14:56834 NavigationController* tab, IPC::Message* reply_message,
835 NavigationCodeType success_code,
836 NavigationCodeType auth_needed_code,
837 NavigationCodeType failed_code) {
initial.commit09911bf2008-07-26 23:55:29838 NotificationObserver* observer =
[email protected]71f65dd2009-02-11 19:14:56839 new NavigationNotificationObserver<NavigationCodeType>(
840 tab, this, reply_message, success_code, auth_needed_code,
841 failed_code);
initial.commit09911bf2008-07-26 23:55:29842
[email protected]71f65dd2009-02-11 19:14:56843 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29844 return observer;
845}
846
847void AutomationProvider::RemoveNavigationStatusListener(
848 NotificationObserver* obs) {
849 notification_observer_list_.RemoveObserver(obs);
850}
851
852NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14853 Browser* parent,
854 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56855 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14856 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29857 notification_observer_list_.AddObserver(observer);
858
859 return observer;
860}
861
862void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
863 notification_observer_list_.RemoveObserver(obs);
864}
865
866void AutomationProvider::AddLoginHandler(NavigationController* tab,
867 LoginHandler* handler) {
868 login_handler_map_[tab] = handler;
869}
870
871void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
872 DCHECK(login_handler_map_[tab]);
873 login_handler_map_.erase(tab);
874}
875
[email protected]f44265b2009-05-19 18:52:50876void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
877 int port_id = port->port_id();
878 DCHECK_NE(-1, port_id);
879 DCHECK(port_containers_.find(port_id) == port_containers_.end());
880
881 port_containers_[port_id] = port;
882}
883
884void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
885 int port_id = port->port_id();
886 DCHECK_NE(-1, port_id);
887
888 PortContainerMap::iterator it = port_containers_.find(port_id);
889 DCHECK(it != port_containers_.end());
890
891 if (it != port_containers_.end()) {
892 delete it->second;
893 port_containers_.erase(it);
894 }
895}
896
897ExtensionPortContainer* AutomationProvider::GetPortContainer(
898 int port_id) const {
899 PortContainerMap::const_iterator it = port_containers_.find(port_id);
900 if (it == port_containers_.end())
901 return NULL;
902
903 return it->second;
904}
905
initial.commit09911bf2008-07-26 23:55:29906int AutomationProvider::GetIndexForNavigationController(
907 const NavigationController* controller, const Browser* parent) const {
908 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12909 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29910}
911
912void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
913 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14914 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56915 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
916 CloseBrowserAsync)
917 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
918 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
919 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
920 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
921 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
922 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14923 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]71f65dd2009-02-11 19:14:56924 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
925 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
926 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
927 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
928 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14929 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56930 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
931 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
932 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14933 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29934 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
935 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56936 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
[email protected]202e7a72009-06-15 03:48:36937 IPC_MESSAGE_HANDLER(AutomationMsg_GetBrowserLocale, GetBrowserLocale)
[email protected]71f65dd2009-02-11 19:14:56938 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29939 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56940 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29941 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
942 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56943 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14944 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]de246f52009-02-25 18:25:45945#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56946 IPC_MESSAGE_HANDLER(AutomationMsg_WindowHWND, GetWindowHWND)
[email protected]de246f52009-02-25 18:25:45947#endif // defined(OS_WIN)
[email protected]49a14a82009-03-31 04:16:44948 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33949 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44950 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33951 ExecuteBrowserCommand)
[email protected]1c58a5c2009-05-21 18:47:14952 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
953 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]de246f52009-02-25 18:25:45954#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56955 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]1c58a5c2009-05-21 18:47:14956 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]71f65dd2009-02-11 19:14:56957 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
958 WindowSimulateDrag)
[email protected]d7fa7552009-03-20 21:06:37959#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56960 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
961 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37962#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56963 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45964#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56965 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
966 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03967 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56968 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14969 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
initial.commit09911bf2008-07-26 23:55:29970 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14971 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56972 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
973 ExecuteJavascript)
974 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29975 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14976 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
977 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56978 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
979 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14980 IPC_MESSAGE_HANDLER(AutomationMsg_SetFilteredInet, SetFilteredInet)
981 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]a7eee32f2009-05-22 18:08:17982 IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig);
[email protected]14c0a032009-04-13 18:15:14983 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14984 OpenNewBrowserWindow)
985 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56986 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14987 GetAutocompleteEditForBrowser)
988 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:45989#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29990 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:17991#endif
[email protected]71f65dd2009-02-11 19:14:56992 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29993 NavigateInExternalTab)
[email protected]71f65dd2009-02-11 19:14:56994 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14995 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:56996 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:14997 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:17998#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29999 IPC_MESSAGE_HANDLER(AutomationMsg_SetAcceleratorsForTab,
1000 SetAcceleratorsForTab)
1001 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
1002 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:171003#endif
[email protected]71f65dd2009-02-11 19:14:561004 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
1005 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:141006 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:171007#if defined(OS_WIN)
[email protected]87eab222009-03-13 00:47:451008 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]e943d6662009-06-12 03:50:391009 IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome,
1010 OnForwardContextMenuCommandToChrome)
[email protected]d2cc6ed2009-04-24 00:26:171011#endif
[email protected]1c58a5c2009-05-21 18:47:141012 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
1013 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]71f65dd2009-02-11 19:14:561014 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
1015 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:291016 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
1017 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
1018 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:561019 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
1020 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
1021 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:291022 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:561023 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:291024 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:561025 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:291026 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:561027 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:291028 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:561029 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:451030 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:451031 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
1032 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:141033 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:561034 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:571035 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:561036 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:571037 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:561038 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
1039 GetBookmarkBarVisibility)
[email protected]1c58a5c2009-05-21 18:47:141040 IPC_MESSAGE_HANDLER(AutomationMsg_GetSSLInfoBarCount, GetSSLInfoBarCount)
[email protected]71f65dd2009-02-11 19:14:561041 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickSSLInfoBarLink,
1042 ClickSSLInfoBarLink)
1043 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:421044 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:561045 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
1046 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:141047 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:561048 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:441049 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:561050 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:201051 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:141052 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:561053 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:161054 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:561055 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:161056 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:561057 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:161058 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:141059 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:201060 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
1061 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:141062 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]b83e4602009-05-15 22:58:331063 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
1064 SetEnableExtensionAutomation)
[email protected]59560e0b2009-06-04 03:30:221065 IPC_MESSAGE_HANDLER(AutomationMsg_SetShelfVisibility, SetShelfVisibility)
[email protected]66ba4932009-06-04 19:22:131066 IPC_MESSAGE_HANDLER(AutomationMsg_BlockedPopupCount, GetBlockedPopupCount)
initial.commit09911bf2008-07-26 23:55:291067 IPC_END_MESSAGE_MAP()
1068}
1069
[email protected]71f65dd2009-02-11 19:14:561070void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
1071 *status = -1;
initial.commit09911bf2008-07-26 23:55:291072 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
1073 Browser* browser = browser_tracker_->GetResource(handle);
1074 if (at_index >= 0 && at_index < browser->tab_count()) {
1075 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:561076 *status = 0;
initial.commit09911bf2008-07-26 23:55:291077 }
1078 }
initial.commit09911bf2008-07-26 23:55:291079}
1080
[email protected]71f65dd2009-02-11 19:14:561081void AutomationProvider::AppendTab(int handle, const GURL& url,
1082 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291083 int append_tab_response = -1; // -1 is the error code
1084 NotificationObserver* observer = NULL;
1085
1086 if (browser_tracker_->ContainsHandle(handle)) {
1087 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:141088 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:581089 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
1090 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:391091 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:291092 if (tab_contents) {
1093 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:571094 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:291095 }
1096 }
1097
1098 if (append_tab_response < 0) {
1099 // The append tab failed. Remove the TabStripObserver
1100 if (observer) {
1101 RemoveTabStripObserver(observer);
1102 delete observer;
1103 }
1104
[email protected]71f65dd2009-02-11 19:14:561105 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
1106 append_tab_response);
1107 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291108 }
1109}
1110
[email protected]71f65dd2009-02-11 19:14:561111void AutomationProvider::NavigateToURL(int handle, const GURL& url,
1112 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291113 if (tab_tracker_->ContainsHandle(handle)) {
1114 NavigationController* tab = tab_tracker_->GetResource(handle);
1115
1116 // Simulate what a user would do. Activate the tab and then navigate.
1117 // We could allow navigating in a background tab in future.
1118 Browser* browser = FindAndActivateTab(tab);
1119
1120 if (browser) {
[email protected]71f65dd2009-02-11 19:14:561121 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1122 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1123 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1124 AUTOMATION_MSG_NAVIGATION_ERROR);
1125
initial.commit09911bf2008-07-26 23:55:291126 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:501127 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:291128 return;
1129 }
1130 }
[email protected]71f65dd2009-02-11 19:14:561131
1132 AutomationMsg_NavigateToURL::WriteReplyParams(
1133 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1134 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291135}
1136
[email protected]71f65dd2009-02-11 19:14:561137void AutomationProvider::NavigationAsync(int handle, const GURL& url,
1138 bool* status) {
1139 *status = false;
initial.commit09911bf2008-07-26 23:55:291140
1141 if (tab_tracker_->ContainsHandle(handle)) {
1142 NavigationController* tab = tab_tracker_->GetResource(handle);
1143
1144 // Simulate what a user would do. Activate the tab and then navigate.
1145 // We could allow navigating in a background tab in future.
1146 Browser* browser = FindAndActivateTab(tab);
1147
1148 if (browser) {
1149 // Don't add any listener unless a callback mechanism is desired.
1150 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:501151 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561152 *status = true;
initial.commit09911bf2008-07-26 23:55:291153 }
1154 }
initial.commit09911bf2008-07-26 23:55:291155}
1156
[email protected]71f65dd2009-02-11 19:14:561157void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291158 if (tab_tracker_->ContainsHandle(handle)) {
1159 NavigationController* tab = tab_tracker_->GetResource(handle);
1160 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:141161 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]71f65dd2009-02-11 19:14:561162 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1163 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1164 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1165 AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]485fba42009-03-24 23:27:291166 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:291167 return;
1168 }
1169 }
[email protected]71f65dd2009-02-11 19:14:561170
1171 AutomationMsg_GoBack::WriteReplyParams(
1172 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1173 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291174}
1175
[email protected]71f65dd2009-02-11 19:14:561176void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291177 if (tab_tracker_->ContainsHandle(handle)) {
1178 NavigationController* tab = tab_tracker_->GetResource(handle);
1179 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:141180 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]71f65dd2009-02-11 19:14:561181 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1182 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1183 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1184 AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]485fba42009-03-24 23:27:291185 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:291186 return;
1187 }
1188 }
[email protected]71f65dd2009-02-11 19:14:561189
1190 AutomationMsg_GoForward::WriteReplyParams(
1191 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1192 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291193}
1194
[email protected]71f65dd2009-02-11 19:14:561195void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291196 if (tab_tracker_->ContainsHandle(handle)) {
1197 NavigationController* tab = tab_tracker_->GetResource(handle);
1198 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:141199 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]71f65dd2009-02-11 19:14:561200 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1201 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1202 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1203 AUTOMATION_MSG_NAVIGATION_ERROR);
initial.commit09911bf2008-07-26 23:55:291204 browser->Reload();
1205 return;
1206 }
1207 }
[email protected]71f65dd2009-02-11 19:14:561208
1209 AutomationMsg_Reload::WriteReplyParams(
1210 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1211 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291212}
1213
[email protected]71f65dd2009-02-11 19:14:561214void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:291215 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:561216 const std::wstring& password,
1217 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291218 int status = -1;
1219
1220 if (tab_tracker_->ContainsHandle(tab_handle)) {
1221 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1222 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1223
1224 if (iter != login_handler_map_.end()) {
1225 // If auth is needed again after this, assume login has failed. This is
1226 // not strictly correct, because a navigation can require both proxy and
1227 // server auth, but it should be OK for now.
1228 LoginHandler* handler = iter->second;
[email protected]71f65dd2009-02-11 19:14:561229 AddNavigationStatusListener<int>(tab, reply_message, 0, -1, -1);
initial.commit09911bf2008-07-26 23:55:291230 handler->SetAuth(username, password);
1231 status = 0;
1232 }
1233 }
[email protected]de246f52009-02-25 18:25:451234
initial.commit09911bf2008-07-26 23:55:291235 if (status < 0) {
[email protected]71f65dd2009-02-11 19:14:561236 AutomationMsg_SetAuth::WriteReplyParams(reply_message, status);
1237 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291238 }
1239}
1240
[email protected]71f65dd2009-02-11 19:14:561241void AutomationProvider::CancelAuth(int tab_handle,
1242 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291243 int status = -1;
1244
1245 if (tab_tracker_->ContainsHandle(tab_handle)) {
1246 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1247 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1248
1249 if (iter != login_handler_map_.end()) {
1250 // If auth is needed again after this, something is screwy.
1251 LoginHandler* handler = iter->second;
[email protected]71f65dd2009-02-11 19:14:561252 AddNavigationStatusListener<int>(tab, reply_message, 0, -1, -1);
initial.commit09911bf2008-07-26 23:55:291253 handler->CancelAuth();
1254 status = 0;
1255 }
1256 }
[email protected]de246f52009-02-25 18:25:451257
initial.commit09911bf2008-07-26 23:55:291258 if (status < 0) {
[email protected]71f65dd2009-02-11 19:14:561259 AutomationMsg_CancelAuth::WriteReplyParams(reply_message, status);
1260 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291261 }
1262}
1263
[email protected]71f65dd2009-02-11 19:14:561264void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
1265 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:291266
1267 if (tab_tracker_->ContainsHandle(tab_handle)) {
1268 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1269 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1270
1271 if (iter != login_handler_map_.end()) {
1272 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:561273 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:291274 }
1275 }
initial.commit09911bf2008-07-26 23:55:291276}
1277
[email protected]71f65dd2009-02-11 19:14:561278void AutomationProvider::GetRedirectsFrom(int tab_handle,
1279 const GURL& source_url,
1280 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291281 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
1282 if (tab_tracker_->ContainsHandle(tab_handle)) {
1283 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1284 HistoryService* history_service =
1285 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
1286
1287 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
1288 "has no history service";
1289 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:561290 DCHECK(reply_message_ == NULL);
1291 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291292 // Schedule a history query for redirects. The response will be sent
1293 // asynchronously from the callback the history system uses to notify us
1294 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:291295 redirect_query_ = history_service->QueryRedirectsFrom(
1296 source_url, &consumer_,
1297 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
1298 return; // Response will be sent when query completes.
1299 }
1300 }
1301
1302 // Send failure response.
[email protected]deb57402009-02-06 01:35:301303 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:561304 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
1305 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291306}
1307
[email protected]71f65dd2009-02-11 19:14:561308void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
1309 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291310 if (browser_tracker_->ContainsHandle(handle)) {
1311 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561312 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:291313 }
initial.commit09911bf2008-07-26 23:55:291314}
1315
[email protected]202e7a72009-06-15 03:48:361316void AutomationProvider::GetBrowserLocale(string16* locale) {
1317 DCHECK(g_browser_process);
1318 *locale = WideToUTF16(g_browser_process->GetApplicationLocale());
1319}
1320
[email protected]71f65dd2009-02-11 19:14:561321void AutomationProvider::GetBrowserWindowCount(int* window_count) {
1322 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:291323}
1324
[email protected]24497032009-05-01 17:00:291325void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
1326 *window_count = static_cast<int>(
1327 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
1328}
1329
[email protected]71f65dd2009-02-11 19:14:561330void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
1331 int* dialog_button) {
[email protected]1f460072009-05-28 17:02:071332 AppModalDialog* dialog_delegate =
1333 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:501334 *showing_dialog = (dialog_delegate != NULL);
1335 if (*showing_dialog)
1336 *dialog_button = dialog_delegate->GetDialogButtons();
1337 else
[email protected]478ff2ed2009-04-21 23:49:181338 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:201339}
1340
[email protected]71f65dd2009-02-11 19:14:561341void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
1342 *success = false;
[email protected]fad84eab2008-12-05 00:37:201343
[email protected]1f460072009-05-28 17:02:071344 AppModalDialog* dialog_delegate =
1345 Singleton<AppModalDialogQueue>()->active_dialog();
[email protected]b3a70332009-02-25 02:40:501346 if (dialog_delegate &&
1347 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:181348 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
1349 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:281350 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:561351 *success = true;
[email protected]fad84eab2008-12-05 00:37:201352 }
[email protected]478ff2ed2009-04-21 23:49:181353 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
1354 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:561355 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:281356 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:561357 *success = true;
[email protected]fad84eab2008-12-05 00:37:201358 }
1359 }
[email protected]c274acc2008-11-11 20:13:441360}
1361
[email protected]71f65dd2009-02-11 19:14:561362void AutomationProvider::GetBrowserWindow(int index, int* handle) {
1363 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291364 if (index >= 0) {
1365 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:291366 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:291367 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:561368 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:291369 }
1370 }
initial.commit09911bf2008-07-26 23:55:291371}
1372
[email protected]24497032009-05-01 17:00:291373void AutomationProvider::FindNormalBrowserWindow(int* handle) {
1374 *handle = 0;
1375 Browser* browser = BrowserList::FindBrowserWithType(profile_,
1376 Browser::TYPE_NORMAL);
1377 if (browser)
1378 *handle = browser_tracker_->Add(browser);
1379}
1380
[email protected]71f65dd2009-02-11 19:14:561381void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
1382 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291383 Browser* browser = BrowserList::GetLastActive();
1384 if (browser)
[email protected]71f65dd2009-02-11 19:14:561385 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:291386}
1387
[email protected]de246f52009-02-25 18:25:451388#if defined(OS_WIN)
1389// TODO(port): Remove windowsisms.
initial.commit09911bf2008-07-26 23:55:291390BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM l_param) {
1391 if (hwnd == reinterpret_cast<HWND>(l_param)) {
1392 return FALSE;
1393 }
1394 return TRUE;
1395}
1396
[email protected]71f65dd2009-02-11 19:14:561397void AutomationProvider::GetActiveWindow(int* handle) {
initial.commit09911bf2008-07-26 23:55:291398 HWND window = GetForegroundWindow();
1399
1400 // Let's make sure this window belongs to our process.
1401 if (EnumThreadWindows(::GetCurrentThreadId(),
1402 EnumThreadWndProc,
1403 reinterpret_cast<LPARAM>(window))) {
1404 // We enumerated all the windows and did not find the foreground window,
1405 // it is not our window, ignore it.
[email protected]71f65dd2009-02-11 19:14:561406 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291407 return;
1408 }
1409
[email protected]71f65dd2009-02-11 19:14:561410 *handle = window_tracker_->Add(window);
initial.commit09911bf2008-07-26 23:55:291411}
1412
[email protected]71f65dd2009-02-11 19:14:561413void AutomationProvider::GetWindowHWND(int handle, HWND* win32_handle) {
1414 *win32_handle = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291415}
[email protected]de246f52009-02-25 18:25:451416#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291417
[email protected]4f6381ee2009-04-16 02:46:331418void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
1419 bool* success) {
[email protected]71f65dd2009-02-11 19:14:561420 *success = false;
[email protected]4ae62752008-08-04 23:28:471421 if (browser_tracker_->ContainsHandle(handle)) {
1422 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:141423 if (browser->command_updater()->SupportsCommand(command) &&
1424 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:471425 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:561426 *success = true;
[email protected]4ae62752008-08-04 23:28:471427 }
1428 }
[email protected]4ae62752008-08-04 23:28:471429}
1430
[email protected]4f6381ee2009-04-16 02:46:331431void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:561432 int handle, int command, IPC::Message* reply_message) {
1433 if (browser_tracker_->ContainsHandle(handle)) {
1434 Browser* browser = browser_tracker_->GetResource(handle);
1435 if (browser->command_updater()->SupportsCommand(command) &&
1436 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]d79ffea2009-05-07 20:51:421437 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
1438 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:271439 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:421440 return;
1441 }
[email protected]56e71b7c2009-03-27 03:05:561442 }
1443 }
[email protected]49a14a82009-03-31 04:16:441444 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:561445 Send(reply_message);
1446}
1447
[email protected]71f65dd2009-02-11 19:14:561448void AutomationProvider::WindowGetViewBounds(int handle, int view_id,
1449 bool screen_coordinates,
1450 bool* success,
1451 gfx::Rect* bounds) {
1452 *success = false;
initial.commit09911bf2008-07-26 23:55:291453
[email protected]de246f52009-02-25 18:25:451454#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291455 void* iter = NULL;
1456 if (window_tracker_->ContainsHandle(handle)) {
1457 HWND hwnd = window_tracker_->GetResource(handle);
[email protected]a0dde122008-11-21 20:51:201458 views::RootView* root_view = views::WidgetWin::FindRootView(hwnd);
initial.commit09911bf2008-07-26 23:55:291459 if (root_view) {
[email protected]c2dacc92008-10-16 23:51:381460 views::View* view = root_view->GetViewByID(view_id);
initial.commit09911bf2008-07-26 23:55:291461 if (view) {
[email protected]71f65dd2009-02-11 19:14:561462 *success = true;
[email protected]96b667d2008-10-14 20:58:441463 gfx::Point point;
initial.commit09911bf2008-07-26 23:55:291464 if (screen_coordinates)
[email protected]c2dacc92008-10-16 23:51:381465 views::View::ConvertPointToScreen(view, &point);
initial.commit09911bf2008-07-26 23:55:291466 else
[email protected]c2dacc92008-10-16 23:51:381467 views::View::ConvertPointToView(view, root_view, &point);
[email protected]71f65dd2009-02-11 19:14:561468 *bounds = view->GetLocalBounds(false);
1469 bounds->set_origin(point);
initial.commit09911bf2008-07-26 23:55:291470 }
1471 }
1472 }
[email protected]de246f52009-02-25 18:25:451473#else
1474 // TODO(port): Enable when window_tracker is ported.
1475 NOTIMPLEMENTED();
1476#endif
initial.commit09911bf2008-07-26 23:55:291477}
1478
[email protected]de246f52009-02-25 18:25:451479#if defined(OS_WIN)
1480// TODO(port): Use portable replacement for POINT.
1481
initial.commit09911bf2008-07-26 23:55:291482// This task enqueues a mouse event on the event loop, so that the view
1483// that it's being sent to can do the requisite post-processing.
1484class MouseEventTask : public Task {
1485 public:
[email protected]c2dacc92008-10-16 23:51:381486 MouseEventTask(views::View* view,
1487 views::Event::EventType type,
initial.commit09911bf2008-07-26 23:55:291488 POINT point,
1489 int flags)
1490 : view_(view), type_(type), point_(point), flags_(flags) {}
1491 virtual ~MouseEventTask() {}
1492
1493 virtual void Run() {
[email protected]c2dacc92008-10-16 23:51:381494 views::MouseEvent event(type_, point_.x, point_.y, flags_);
initial.commit09911bf2008-07-26 23:55:291495 // We need to set the cursor position before we process the event because
1496 // some code (tab dragging, for instance) queries the actual cursor location
1497 // rather than the location of the mouse event. Note that the reason why
1498 // the drag code moved away from using mouse event locations was because
1499 // our conversion to screen location doesn't work well with multiple
1500 // monitors, so this only works reliably in a single monitor setup.
[email protected]96b667d2008-10-14 20:58:441501 gfx::Point screen_location(point_.x, point_.y);
initial.commit09911bf2008-07-26 23:55:291502 view_->ConvertPointToScreen(view_, &screen_location);
[email protected]96b667d2008-10-14 20:58:441503 ::SetCursorPos(screen_location.x(), screen_location.y());
initial.commit09911bf2008-07-26 23:55:291504 switch (type_) {
[email protected]c2dacc92008-10-16 23:51:381505 case views::Event::ET_MOUSE_PRESSED:
initial.commit09911bf2008-07-26 23:55:291506 view_->OnMousePressed(event);
1507 break;
1508
[email protected]c2dacc92008-10-16 23:51:381509 case views::Event::ET_MOUSE_DRAGGED:
initial.commit09911bf2008-07-26 23:55:291510 view_->OnMouseDragged(event);
1511 break;
1512
[email protected]c2dacc92008-10-16 23:51:381513 case views::Event::ET_MOUSE_RELEASED:
initial.commit09911bf2008-07-26 23:55:291514 view_->OnMouseReleased(event, false);
1515 break;
1516
1517 default:
1518 NOTREACHED();
1519 }
1520 }
1521
1522 private:
[email protected]c2dacc92008-10-16 23:51:381523 views::View* view_;
1524 views::Event::EventType type_;
initial.commit09911bf2008-07-26 23:55:291525 POINT point_;
1526 int flags_;
1527
[email protected]5a52f162008-08-27 04:15:311528 DISALLOW_COPY_AND_ASSIGN(MouseEventTask);
initial.commit09911bf2008-07-26 23:55:291529};
1530
[email protected]c2dacc92008-10-16 23:51:381531void AutomationProvider::ScheduleMouseEvent(views::View* view,
1532 views::Event::EventType type,
initial.commit09911bf2008-07-26 23:55:291533 POINT point,
1534 int flags) {
1535 MessageLoop::current()->PostTask(FROM_HERE,
1536 new MouseEventTask(view, type, point, flags));
1537}
[email protected]de246f52009-02-25 18:25:451538#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291539
1540// This task just adds another task to the event queue. This is useful if
1541// you want to ensure that any tasks added to the event queue after this one
1542// have already been processed by the time |task| is run.
1543class InvokeTaskLaterTask : public Task {
1544 public:
1545 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
1546 virtual ~InvokeTaskLaterTask() {}
1547
1548 virtual void Run() {
1549 MessageLoop::current()->PostTask(FROM_HERE, task_);
1550 }
1551
1552 private:
1553 Task* task_;
1554
[email protected]5a52f162008-08-27 04:15:311555 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
initial.commit09911bf2008-07-26 23:55:291556};
1557
[email protected]de246f52009-02-25 18:25:451558#if defined(OS_WIN)
1559// TODO(port): Replace POINT and other windowsisms.
1560
initial.commit09911bf2008-07-26 23:55:291561// This task sends a WindowDragResponse message with the appropriate
1562// routing ID to the automation proxy. This is implemented as a task so that
1563// we know that the mouse events (and any tasks that they spawn on the message
1564// loop) have been processed by the time this is sent.
1565class WindowDragResponseTask : public Task {
1566 public:
[email protected]1c58a5c2009-05-21 18:47:141567 WindowDragResponseTask(AutomationProvider* provider,
[email protected]71f65dd2009-02-11 19:14:561568 IPC::Message* reply_message)
[email protected]1c58a5c2009-05-21 18:47:141569 : provider_(provider), reply_message_(reply_message) {}
initial.commit09911bf2008-07-26 23:55:291570 virtual ~WindowDragResponseTask() {}
1571
1572 virtual void Run() {
[email protected]71f65dd2009-02-11 19:14:561573 DCHECK(reply_message_ != NULL);
1574 AutomationMsg_WindowDrag::WriteReplyParams(reply_message_, true);
1575 provider_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:291576 }
1577
1578 private:
1579 AutomationProvider* provider_;
[email protected]71f65dd2009-02-11 19:14:561580 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:291581
[email protected]5a52f162008-08-27 04:15:311582 DISALLOW_COPY_AND_ASSIGN(WindowDragResponseTask);
initial.commit09911bf2008-07-26 23:55:291583};
1584
1585void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
1586 int handle,
1587 POINT click,
1588 int flags) {
1589 HWND hwnd = 0;
1590
1591 if (window_tracker_->ContainsHandle(handle)) {
1592 hwnd = window_tracker_->GetResource(handle);
1593
initial.commit09911bf2008-07-26 23:55:291594 ui_controls::SendMouseMove(click.x, click.y);
1595
1596 ui_controls::MouseButton button = ui_controls::LEFT;
[email protected]c2dacc92008-10-16 23:51:381597 if ((flags & views::Event::EF_LEFT_BUTTON_DOWN) ==
1598 views::Event::EF_LEFT_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291599 button = ui_controls::LEFT;
[email protected]c2dacc92008-10-16 23:51:381600 } else if ((flags & views::Event::EF_RIGHT_BUTTON_DOWN) ==
1601 views::Event::EF_RIGHT_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291602 button = ui_controls::RIGHT;
[email protected]c2dacc92008-10-16 23:51:381603 } else if ((flags & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
1604 views::Event::EF_MIDDLE_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291605 button = ui_controls::MIDDLE;
1606 } else {
1607 NOTREACHED();
1608 }
1609 ui_controls::SendMouseClick(button);
1610 }
1611}
1612
[email protected]71f65dd2009-02-11 19:14:561613void AutomationProvider::WindowSimulateDrag(int handle,
[email protected]fe92e4e2008-11-18 21:31:321614 std::vector<POINT> drag_path,
[email protected]5e0f30c2008-08-14 22:52:441615 int flags,
[email protected]71f65dd2009-02-11 19:14:561616 bool press_escape_en_route,
1617 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291618 bool succeeded = false;
[email protected]fe92e4e2008-11-18 21:31:321619 if (browser_tracker_->ContainsHandle(handle) && (drag_path.size() > 1)) {
1620 succeeded = true;
1621
1622 UINT down_message = 0;
1623 UINT up_message = 0;
1624 WPARAM wparam_flags = 0;
1625 if (flags & views::Event::EF_SHIFT_DOWN)
1626 wparam_flags |= MK_SHIFT;
1627 if (flags & views::Event::EF_CONTROL_DOWN)
1628 wparam_flags |= MK_CONTROL;
1629 if (flags & views::Event::EF_LEFT_BUTTON_DOWN) {
1630 wparam_flags |= MK_LBUTTON;
1631 down_message = WM_LBUTTONDOWN;
1632 up_message = WM_LBUTTONUP;
1633 }
1634 if (flags & views::Event::EF_MIDDLE_BUTTON_DOWN) {
1635 wparam_flags |= MK_MBUTTON;
1636 down_message = WM_MBUTTONDOWN;
1637 up_message = WM_MBUTTONUP;
1638 }
1639 if (flags & views::Event::EF_RIGHT_BUTTON_DOWN) {
1640 wparam_flags |= MK_RBUTTON;
1641 down_message = WM_LBUTTONDOWN;
1642 up_message = WM_LBUTTONUP;
1643 }
1644
initial.commit09911bf2008-07-26 23:55:291645 Browser* browser = browser_tracker_->GetResource(handle);
1646 DCHECK(browser);
[email protected]0a6fb3f2008-11-18 21:39:551647 HWND top_level_hwnd =
1648 reinterpret_cast<HWND>(browser->window()->GetNativeHandle());
[email protected]fe92e4e2008-11-18 21:31:321649 POINT temp = drag_path[0];
1650 MapWindowPoints(top_level_hwnd, HWND_DESKTOP, &temp, 1);
1651 SetCursorPos(temp.x, temp.y);
1652 SendMessage(top_level_hwnd, down_message, wparam_flags,
1653 MAKELPARAM(drag_path[0].x, drag_path[0].y));
1654 for (int i = 1; i < static_cast<int>(drag_path.size()); ++i) {
1655 temp = drag_path[i];
1656 MapWindowPoints(top_level_hwnd, HWND_DESKTOP, &temp, 1);
1657 SetCursorPos(temp.x, temp.y);
1658 SendMessage(top_level_hwnd, WM_MOUSEMOVE, wparam_flags,
1659 MAKELPARAM(drag_path[i].x, drag_path[i].y));
[email protected]f7a391a12008-11-10 21:29:341660 }
[email protected]fe92e4e2008-11-18 21:31:321661 POINT end = drag_path[drag_path.size() - 1];
1662 MapWindowPoints(top_level_hwnd, HWND_DESKTOP, &end, 1);
1663 SetCursorPos(end.x, end.y);
1664
1665 if (press_escape_en_route) {
1666 // Press Escape.
1667 ui_controls::SendKeyPress(VK_ESCAPE,
1668 ((flags & views::Event::EF_CONTROL_DOWN)
1669 == views::Event::EF_CONTROL_DOWN),
1670 ((flags & views::Event::EF_SHIFT_DOWN) ==
1671 views::Event::EF_SHIFT_DOWN),
1672 ((flags & views::Event::EF_ALT_DOWN) ==
1673 views::Event::EF_ALT_DOWN));
1674 }
1675 SendMessage(top_level_hwnd, up_message, wparam_flags,
1676 MAKELPARAM(end.x, end.y));
1677
[email protected]1c58a5c2009-05-21 18:47:141678 MessageLoop::current()->PostTask(FROM_HERE, new InvokeTaskLaterTask(
1679 new WindowDragResponseTask(this, reply_message)));
initial.commit09911bf2008-07-26 23:55:291680 } else {
[email protected]71f65dd2009-02-11 19:14:561681 AutomationMsg_WindowDrag::WriteReplyParams(reply_message, true);
1682 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291683 }
1684}
1685
1686void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
1687 int handle,
1688 wchar_t key,
1689 int flags) {
1690 if (!window_tracker_->ContainsHandle(handle))
1691 return;
1692
1693 // The key event is sent to whatever window is active.
1694 ui_controls::SendKeyPress(key,
[email protected]c2dacc92008-10-16 23:51:381695 ((flags & views::Event::EF_CONTROL_DOWN) ==
1696 views::Event::EF_CONTROL_DOWN),
1697 ((flags & views::Event::EF_SHIFT_DOWN) ==
1698 views::Event::EF_SHIFT_DOWN),
1699 ((flags & views::Event::EF_ALT_DOWN) ==
1700 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:291701}
1702
[email protected]71f65dd2009-02-11 19:14:561703void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
1704 *view_id = -1;
initial.commit09911bf2008-07-26 23:55:291705 if (window_tracker_->ContainsHandle(handle)) {
1706 HWND hwnd = window_tracker_->GetResource(handle);
[email protected]c2dacc92008-10-16 23:51:381707 views::FocusManager* focus_manager =
[email protected]c39e7152009-06-22 15:27:451708 views::FocusManager::GetFocusManager(hwnd);
initial.commit09911bf2008-07-26 23:55:291709 DCHECK(focus_manager);
[email protected]c2dacc92008-10-16 23:51:381710 views::View* focused_view = focus_manager->GetFocusedView();
initial.commit09911bf2008-07-26 23:55:291711 if (focused_view)
[email protected]71f65dd2009-02-11 19:14:561712 *view_id = focused_view->GetID();
initial.commit09911bf2008-07-26 23:55:291713 }
initial.commit09911bf2008-07-26 23:55:291714}
1715
[email protected]71f65dd2009-02-11 19:14:561716void AutomationProvider::SetWindowVisible(int handle, bool visible,
1717 bool* result) {
initial.commit09911bf2008-07-26 23:55:291718 if (window_tracker_->ContainsHandle(handle)) {
1719 HWND hwnd = window_tracker_->GetResource(handle);
1720 ::ShowWindow(hwnd, visible ? SW_SHOW : SW_HIDE);
[email protected]71f65dd2009-02-11 19:14:561721 *result = true;
initial.commit09911bf2008-07-26 23:55:291722 } else {
[email protected]71f65dd2009-02-11 19:14:561723 *result = false;
initial.commit09911bf2008-07-26 23:55:291724 }
1725}
[email protected]d2cc6ed2009-04-24 00:26:171726#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291727
[email protected]71f65dd2009-02-11 19:14:561728void AutomationProvider::IsWindowActive(int handle, bool* success,
1729 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291730 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171731 *is_active =
1732 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561733 *success = true;
initial.commit09911bf2008-07-26 23:55:291734 } else {
[email protected]71f65dd2009-02-11 19:14:561735 *success = false;
1736 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291737 }
1738}
1739
[email protected]d2cc6ed2009-04-24 00:26:171740// TODO(port): port this.
1741#if defined(OS_WIN)
[email protected]659d0e82009-02-13 22:43:281742void AutomationProvider::ActivateWindow(int handle) {
initial.commit09911bf2008-07-26 23:55:291743 if (window_tracker_->ContainsHandle(handle)) {
1744 ::SetActiveWindow(window_tracker_->GetResource(handle));
1745 }
1746}
[email protected]d2cc6ed2009-04-24 00:26:171747#endif
initial.commit09911bf2008-07-26 23:55:291748
[email protected]71f65dd2009-02-11 19:14:561749void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1750 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291751
1752 if (browser_tracker_->ContainsHandle(handle)) {
1753 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561754 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291755 }
initial.commit09911bf2008-07-26 23:55:291756}
1757
[email protected]71f65dd2009-02-11 19:14:561758void AutomationProvider::GetTab(int win_handle, int tab_index,
1759 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561760 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291761 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1762 Browser* browser = browser_tracker_->GetResource(win_handle);
1763 if (tab_index < browser->tab_count()) {
1764 TabContents* tab_contents =
1765 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571766 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291767 }
1768 }
initial.commit09911bf2008-07-26 23:55:291769}
1770
[email protected]71f65dd2009-02-11 19:14:561771void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1772 std::wstring* title) {
1773 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291774 if (tab_tracker_->ContainsHandle(handle)) {
1775 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361776 NavigationEntry* entry = tab->GetActiveEntry();
1777 if (entry != NULL) {
1778 *title = UTF16ToWideHack(entry->title());
1779 } else {
1780 *title = std::wstring();
1781 }
[email protected]71f65dd2009-02-11 19:14:561782 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291783 }
initial.commit09911bf2008-07-26 23:55:291784}
1785
[email protected]77bc6732009-04-20 22:01:031786void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1787 *tabstrip_index = -1; // -1 is the error code
1788
1789 if (tab_tracker_->ContainsHandle(handle)) {
1790 NavigationController* tab = tab_tracker_->GetResource(handle);
1791 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121792 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031793 }
1794}
1795
initial.commit09911bf2008-07-26 23:55:291796void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1797 if (window_tracker_->ContainsHandle(handle)) {
1798 window_tracker_->Remove(window_tracker_->GetResource(handle));
1799 }
1800}
1801
1802void AutomationProvider::OnChannelError() {
1803 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571804 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291805}
1806
1807// TODO(brettw) change this to accept GURLs when history supports it
1808void AutomationProvider::OnRedirectQueryComplete(
1809 HistoryService::Handle request_handle,
1810 GURL from_url,
1811 bool success,
1812 HistoryService::RedirectList* redirects) {
1813 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561814 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291815
[email protected]deb57402009-02-06 01:35:301816 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:291817 if (success) {
[email protected]71f65dd2009-02-11 19:14:561818 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:291819 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301820 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291821 } else {
[email protected]71f65dd2009-02-11 19:14:561822 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:291823 }
1824
[email protected]4f3dc372009-02-24 00:10:291825 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301826
[email protected]71f65dd2009-02-11 19:14:561827 Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:291828 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561829 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291830}
1831
1832bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571833 DCHECK(channel_.get());
1834 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291835}
1836
1837Browser* AutomationProvider::FindAndActivateTab(
1838 NavigationController* controller) {
1839 int tab_index;
1840 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1841 if (browser)
1842 browser->SelectTabContentsAt(tab_index, true);
1843
1844 return browser;
1845}
1846
[email protected]71f65dd2009-02-11 19:14:561847void AutomationProvider::GetCookies(const GURL& url, int handle,
1848 int* value_size,
1849 std::string* value) {
1850 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291851 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1852 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561853 *value =
initial.commit09911bf2008-07-26 23:55:291854 tab->profile()->GetRequestContext()->cookie_store()->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561855 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291856 }
initial.commit09911bf2008-07-26 23:55:291857}
1858
[email protected]71f65dd2009-02-11 19:14:561859void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291860 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561861 int handle,
1862 int* response_value) {
1863 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291864
1865 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1866 NavigationController* tab = tab_tracker_->GetResource(handle);
1867 URLRequestContext* context = tab->profile()->GetRequestContext();
1868 if (context->cookie_store()->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561869 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291870 }
initial.commit09911bf2008-07-26 23:55:291871}
1872
[email protected]71f65dd2009-02-11 19:14:561873void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1874 *success = false;
initial.commit09911bf2008-07-26 23:55:291875 if (tab_tracker_->ContainsHandle(handle)) {
1876 NavigationController* tab = tab_tracker_->GetResource(handle);
1877 // Return what the user would see in the location bar.
[email protected]71f65dd2009-02-11 19:14:561878 *url = tab->GetActiveEntry()->display_url();
1879 *success = true;
initial.commit09911bf2008-07-26 23:55:291880 }
initial.commit09911bf2008-07-26 23:55:291881}
1882
[email protected]de246f52009-02-25 18:25:451883#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:561884void AutomationProvider::GetTabHWND(int handle, HWND* tab_hwnd) {
1885 *tab_hwnd = NULL;
initial.commit09911bf2008-07-26 23:55:291886
1887 if (tab_tracker_->ContainsHandle(handle)) {
1888 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111889 *tab_hwnd = tab->tab_contents()->GetNativeView();
initial.commit09911bf2008-07-26 23:55:291890 }
initial.commit09911bf2008-07-26 23:55:291891}
[email protected]de246f52009-02-25 18:25:451892#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291893
[email protected]71f65dd2009-02-11 19:14:561894void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1895 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291896
1897 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561898 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341899 TabContents* tab_contents =
1900 tab_tracker_->GetResource(handle)->tab_contents();
1901 if (tab_contents->process())
1902 *process_id = tab_contents->process()->process().pid();
initial.commit09911bf2008-07-26 23:55:291903 }
initial.commit09911bf2008-07-26 23:55:291904}
1905
1906void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331907 NOTREACHED() << "This function has been deprecated. "
1908 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291909}
1910
[email protected]71f65dd2009-02-11 19:14:561911void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291912 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561913 const std::wstring& script,
1914 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291915 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341916 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1917 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571918 // Set the routing id of this message with the controller.
1919 // This routing id needs to be remembered for the reverse
1920 // communication while sending back the response of
1921 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331922 std::wstring set_automation_id;
1923 SStringPrintf(&set_automation_id,
1924 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561925 reply_message->routing_id());
1926
1927 DCHECK(reply_message_ == NULL);
1928 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291929
[email protected]57c6a652009-05-04 07:58:341930 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331931 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341932 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061933 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571934 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291935 }
1936
1937 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561938 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1939 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291940 }
1941}
1942
[email protected]71f65dd2009-02-11 19:14:561943void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1944 *visible = false;
[email protected]20e93d12008-08-28 16:31:571945
[email protected]59560e0b2009-06-04 03:30:221946 if (browser_tracker_->ContainsHandle(handle)) {
1947 Browser* browser = browser_tracker_->GetResource(handle);
1948 if (browser) {
1949 *visible = browser->window()->IsDownloadShelfVisible();
1950 }
1951 }
initial.commit09911bf2008-07-26 23:55:291952}
1953
[email protected]59560e0b2009-06-04 03:30:221954void AutomationProvider::SetShelfVisibility(int handle, bool visible) {
1955 if (browser_tracker_->ContainsHandle(handle)) {
1956 Browser* browser = browser_tracker_->GetResource(handle);
1957 if (browser) {
1958 if (visible)
1959 browser->window()->GetDownloadShelf()->Show();
1960 else
1961 browser->window()->GetDownloadShelf()->Close();
1962 }
1963 }
1964}
1965
1966
[email protected]71f65dd2009-02-11 19:14:561967void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1968 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291969 if (tab_tracker_->ContainsHandle(handle)) {
1970 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111971 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291972 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561973 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291974 }
1975 }
initial.commit09911bf2008-07-26 23:55:291976}
1977
initial.commit09911bf2008-07-26 23:55:291978void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561979 int handle, const std::wstring& find_request,
1980 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311981 NOTREACHED() << "This function has been deprecated."
1982 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561983 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311984 return;
1985}
1986
[email protected]4f999132009-03-31 18:08:401987void AutomationProvider::HandleFindRequest(
1988 int handle,
1989 const AutomationMsg_Find_Params& params,
1990 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291991 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561992 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1993 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291994 return;
1995 }
1996
1997 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111998 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:291999
2000 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:142001 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:292002
[email protected]57c6a652009-05-04 07:58:342003 tab_contents->set_current_find_request_id(
2004 FindInPageNotificationObserver::kFindInPageRequestId);
2005 tab_contents->render_view_host()->StartFinding(
2006 FindInPageNotificationObserver::kFindInPageRequestId,
2007 params.search_string, params.forward, params.match_case,
2008 params.find_next);
initial.commit09911bf2008-07-26 23:55:292009}
2010
[email protected]5f8af2a2008-08-06 22:49:452011void AutomationProvider::HandleOpenFindInPageRequest(
2012 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:292013 if (browser_tracker_->ContainsHandle(handle)) {
2014 Browser* browser = browser_tracker_->GetResource(handle);
2015 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:452016 }
2017}
2018
[email protected]71f65dd2009-02-11 19:14:562019void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]9e0534b2008-10-21 15:03:012020 gfx::Point position;
[email protected]71f65dd2009-02-11 19:14:562021 *visible = false;
[email protected]4f3dc372009-02-24 00:10:292022 if (browser_tracker_->ContainsHandle(handle)) {
2023 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:582024 FindBarTesting* find_bar =
2025 browser->find_bar()->find_bar()->GetFindBarTesting();
2026 find_bar->GetFindBarWindowInfo(&position, visible);
[email protected]4f3dc372009-02-24 00:10:292027 }
[email protected]20e93d12008-08-28 16:31:572028}
2029
[email protected]71f65dd2009-02-11 19:14:562030void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
2031 int* y) {
[email protected]9e0534b2008-10-21 15:03:012032 gfx::Point position(0, 0);
2033 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:292034 if (browser_tracker_->ContainsHandle(handle)) {
2035 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:582036 FindBarTesting* find_bar =
2037 browser->find_bar()->find_bar()->GetFindBarTesting();
2038 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:292039 }
[email protected]20e93d12008-08-28 16:31:572040
[email protected]71f65dd2009-02-11 19:14:562041 *x = position.x();
2042 *y = position.y();
[email protected]20e93d12008-08-28 16:31:572043}
2044
[email protected]71f65dd2009-02-11 19:14:562045void AutomationProvider::GetBookmarkBarVisibility(int handle, bool* visible,
2046 bool* animating) {
2047 *visible = false;
2048 *animating = false;
[email protected]c2cbeb92008-09-05 21:36:572049
[email protected]de246f52009-02-25 18:25:452050#if defined(OS_WIN)
[email protected]c2cbeb92008-09-05 21:36:572051 if (browser_tracker_->ContainsHandle(handle)) {
2052 Browser* browser = browser_tracker_->GetResource(handle);
2053 if (browser) {
[email protected]0ba1f5302009-01-22 01:34:522054 BrowserWindowTesting* testing =
2055 browser->window()->GetBrowserWindowTesting();
2056 BookmarkBarView* bookmark_bar = testing->GetBookmarkBarView();
[email protected]c2cbeb92008-09-05 21:36:572057 if (bookmark_bar) {
[email protected]71f65dd2009-02-11 19:14:562058 *animating = bookmark_bar->IsAnimating();
2059 *visible = browser->window()->IsBookmarkBarVisible();
[email protected]c2cbeb92008-09-05 21:36:572060 }
2061 }
2062 }
[email protected]de246f52009-02-25 18:25:452063#else
2064 // TODO(port): Enable when bookmarks ui is ported.
2065 NOTIMPLEMENTED();
2066#endif
[email protected]c2cbeb92008-09-05 21:36:572067}
2068
initial.commit09911bf2008-07-26 23:55:292069void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562070 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342071 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2072 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562073 DCHECK(reply_message_ == NULL);
2074 reply_message_ = reply_message;
2075
[email protected]d9f9b792009-06-24 13:17:122076 DevToolsManager::GetInstance()->InspectElement(
2077 tab_contents->render_view_host(), x, y);
initial.commit09911bf2008-07-26 23:55:292078 } else {
[email protected]71f65dd2009-02-11 19:14:562079 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2080 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292081 }
2082}
2083
2084void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092085 if (reply_message_) {
2086 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2087 num_resources);
2088 Send(reply_message_);
2089 reply_message_ = NULL;
2090 }
initial.commit09911bf2008-07-26 23:55:292091}
2092
2093// Helper class for making changes to the URLRequest ProtocolFactory on the
2094// IO thread.
2095class SetFilteredInetTask : public Task {
2096 public:
2097 explicit SetFilteredInetTask(bool enabled) : enabled_(enabled) { }
2098 virtual void Run() {
2099 if (enabled_) {
2100 URLRequestFilter::GetInstance()->ClearHandlers();
2101
2102 URLRequestFailedDnsJob::AddUITestUrls();
2103 URLRequestSlowDownloadJob::AddUITestUrls();
2104
2105 std::wstring root_http;
2106 PathService::Get(chrome::DIR_TEST_DATA, &root_http);
2107 URLRequestMockHTTPJob::AddUITestUrls(root_http);
2108 } else {
2109 // Revert to the default handlers.
2110 URLRequestFilter::GetInstance()->ClearHandlers();
2111 }
2112 }
2113 private:
2114 bool enabled_;
2115};
2116
2117void AutomationProvider::SetFilteredInet(const IPC::Message& message,
2118 bool enabled) {
2119 // Since this involves changing the URLRequest ProtocolFactory, we want to
2120 // run on the main thread.
2121 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
2122 new SetFilteredInetTask(enabled));
2123}
2124
[email protected]a7eee32f2009-05-22 18:08:172125class SetProxyConfigTask : public Task {
2126 public:
2127 explicit SetProxyConfigTask(net::ProxyService* proxy_service,
2128 const std::string& new_proxy_config)
2129 : proxy_service_(proxy_service), proxy_config_(new_proxy_config) {}
2130 virtual void Run() {
2131 // First, deserialize the JSON string. If this fails, log and bail.
2132 JSONStringValueSerializer deserializer(proxy_config_);
2133 std::string error_message;
2134 scoped_ptr<Value> root(deserializer.Deserialize(&error_message));
2135 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) {
2136 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: "
2137 << error_message;
2138 return;
2139 }
2140
2141 scoped_ptr<DictionaryValue> dict(
2142 static_cast<DictionaryValue*>(root.release()));
2143 // Now put together a proxy configuration from the deserialized string.
2144 net::ProxyConfig pc;
2145 PopulateProxyConfig(*dict.get(), &pc);
2146
2147 DCHECK(proxy_service_);
2148 scoped_ptr<net::ProxyConfigService> proxy_config_service(
2149 new net::ProxyConfigServiceFixed(pc));
2150 proxy_service_->ResetConfigService(proxy_config_service.release());
2151 }
2152
2153 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) {
2154 DCHECK(pc);
2155 bool no_proxy = false;
2156 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) {
2157 // Make no changes to the ProxyConfig.
2158 return;
2159 }
2160 bool auto_config;
2161 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) {
2162 pc->auto_detect = true;
2163 }
2164 std::string pac_url;
2165 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) {
2166 pc->pac_url = GURL(pac_url);
2167 }
2168 std::string proxy_bypass_list;
2169 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
2170 pc->ParseNoProxyList(proxy_bypass_list);
2171 }
2172 std::string proxy_server;
2173 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
2174 pc->proxy_rules.ParseFromString(proxy_server);
2175 }
2176 }
2177
2178 private:
2179 net::ProxyService* proxy_service_;
2180 std::string proxy_config_;
2181};
2182
2183
2184void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) {
2185 URLRequestContext* context = Profile::GetDefaultRequestContext();
2186 // If we don't have a default request context yet then we have to create
2187 // one.
2188 bool run_on_ui_thread = false;
2189 if (!context) {
2190 FilePath user_data_dir;
2191 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
2192 ProfileManager* profile_manager = g_browser_process->profile_manager();
2193 DCHECK(profile_manager);
2194 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
2195 DCHECK(profile);
2196 context = profile->GetRequestContext();
2197 run_on_ui_thread = true;
2198 }
2199 DCHECK(context);
2200 // Every URLRequestContext should have a proxy service.
2201 net::ProxyService* proxy_service = context->proxy_service();
2202 DCHECK(proxy_service);
2203
2204 // If we just now created the URLRequestContext then we can immediately
2205 // set the proxy settings on this (the UI) thread. If there was already
2206 // a URLRequestContext, then run the reset on the IO thread.
2207 if (run_on_ui_thread) {
2208 SetProxyConfigTask set_proxy_config_task(proxy_service, new_proxy_config);
2209 set_proxy_config_task.Run();
2210 } else {
2211 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
2212 new SetProxyConfigTask(proxy_service, new_proxy_config));
2213 }
2214}
2215
[email protected]4f3dc372009-02-24 00:10:292216void AutomationProvider::GetDownloadDirectory(
2217 int handle, std::wstring* download_directory) {
initial.commit09911bf2008-07-26 23:55:292218 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292219 if (tab_tracker_->ContainsHandle(handle)) {
2220 NavigationController* tab = tab_tracker_->GetResource(handle);
2221 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2222 DCHECK(dlm);
[email protected]71f65dd2009-02-11 19:14:562223 *download_directory = dlm->download_path().ToWStringHack();
initial.commit09911bf2008-07-26 23:55:292224 }
initial.commit09911bf2008-07-26 23:55:292225}
2226
[email protected]14c0a032009-04-13 18:15:142227void AutomationProvider::OpenNewBrowserWindow(bool show,
2228 IPC::Message* reply_message) {
2229 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292230 // We may have no current browser windows open so don't rely on
2231 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]15952e462008-11-14 00:29:052232 Browser* browser = Browser::Create(profile_);
2233 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152234 if (show)
[email protected]15952e462008-11-14 00:29:052235 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292236}
2237
[email protected]71f65dd2009-02-11 19:14:562238void AutomationProvider::GetWindowForBrowser(int browser_handle,
2239 bool* success,
2240 int* handle) {
2241 *success = false;
2242 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292243
2244 if (browser_tracker_->ContainsHandle(browser_handle)) {
2245 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202246 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292247 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202248 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562249 *success = true;
initial.commit09911bf2008-07-26 23:55:292250 }
initial.commit09911bf2008-07-26 23:55:292251}
2252
[email protected]13869dd2009-05-05 00:40:062253#if defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:292254void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562255 int browser_handle,
2256 bool* success,
2257 int* autocomplete_edit_handle) {
2258 *success = false;
2259 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292260
2261 if (browser_tracker_->ContainsHandle(browser_handle)) {
2262 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062263 LocationBar* loc_bar = browser->window()->GetLocationBar();
2264 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292265 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562266 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2267 *success = true;
initial.commit09911bf2008-07-26 23:55:292268 }
initial.commit09911bf2008-07-26 23:55:292269}
[email protected]13869dd2009-05-05 00:40:062270#endif // defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:292271
[email protected]13869dd2009-05-05 00:40:062272#if defined(OS_WIN)
2273// TODO(port): Remove windowsisms.
[email protected]71f65dd2009-02-11 19:14:562274void AutomationProvider::GetBrowserForWindow(int window_handle,
2275 bool* success,
2276 int* browser_handle) {
2277 *success = false;
2278 *browser_handle = 0;
initial.commit09911bf2008-07-26 23:55:292279
2280 if (window_tracker_->ContainsHandle(window_handle)) {
2281 HWND window = window_tracker_->GetResource(window_handle);
2282 BrowserList::const_iterator iter = BrowserList::begin();
2283 Browser* browser = NULL;
2284 for (;iter != BrowserList::end(); ++iter) {
[email protected]2d46c842008-11-14 19:24:312285 HWND hwnd = reinterpret_cast<HWND>((*iter)->window()->GetNativeHandle());
2286 if (window == hwnd) {
initial.commit09911bf2008-07-26 23:55:292287 browser = *iter;
2288 break;
2289 }
2290 }
2291 if (browser) {
2292 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562293 *browser_handle = browser_tracker_->Add(browser);
2294 *success = true;
initial.commit09911bf2008-07-26 23:55:292295 }
2296 }
initial.commit09911bf2008-07-26 23:55:292297}
[email protected]de246f52009-02-25 18:25:452298#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292299
[email protected]71f65dd2009-02-11 19:14:562300void AutomationProvider::ShowInterstitialPage(int tab_handle,
2301 const std::string& html_text,
2302 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292303 if (tab_tracker_->ContainsHandle(tab_handle)) {
2304 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112305 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402306
2307 AddNavigationStatusListener<bool>(controller, reply_message, true,
2308 false, false);
[email protected]965524b2009-04-04 21:32:402309 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342310 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402311 GURL("about:interstitial"),
2312 html_text);
2313 interstitial->Show();
2314 return;
initial.commit09911bf2008-07-26 23:55:292315 }
[email protected]71f65dd2009-02-11 19:14:562316
2317 AutomationMsg_ShowInterstitialPage::WriteReplyParams(reply_message, false);
2318 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292319}
2320
[email protected]71f65dd2009-02-11 19:14:562321void AutomationProvider::HideInterstitialPage(int tab_handle,
2322 bool* success) {
2323 *success = false;
[email protected]57c6a652009-05-04 07:58:342324 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2325 if (tab_contents && tab_contents->interstitial_page()) {
2326 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562327 *success = true;
initial.commit09911bf2008-07-26 23:55:292328 }
initial.commit09911bf2008-07-26 23:55:292329}
2330
[email protected]71f65dd2009-02-11 19:14:562331void AutomationProvider::CloseTab(int tab_handle,
2332 bool wait_until_closed,
2333 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292334 if (tab_tracker_->ContainsHandle(tab_handle)) {
2335 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2336 int index;
2337 Browser* browser = Browser::GetBrowserForController(controller, &index);
2338 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142339 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112340 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452341 return;
initial.commit09911bf2008-07-26 23:55:292342 }
[email protected]de246f52009-02-25 18:25:452343
2344 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
initial.commit09911bf2008-07-26 23:55:292345}
2346
[email protected]71f65dd2009-02-11 19:14:562347void AutomationProvider::CloseBrowser(int browser_handle,
2348 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292349 if (browser_tracker_->ContainsHandle(browser_handle)) {
2350 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562351 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562352 reply_message);
[email protected]f3e99e32008-07-30 04:48:392353 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292354 } else {
2355 NOTREACHED();
2356 }
2357}
2358
[email protected]71f65dd2009-02-11 19:14:562359void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2360 if (browser_tracker_->ContainsHandle(browser_handle)) {
2361 Browser* browser = browser_tracker_->GetResource(browser_handle);
2362 browser->window()->Close();
2363 } else {
2364 NOTREACHED();
2365 }
2366}
2367
[email protected]de246f52009-02-25 18:25:452368#if defined(OS_WIN)
2369// TODO(port): Remove windowsisms.
[email protected]71f65dd2009-02-11 19:14:562370void AutomationProvider::CreateExternalTab(HWND parent,
[email protected]31fb110522009-01-28 21:50:392371 const gfx::Rect& dimensions,
[email protected]71f65dd2009-02-11 19:14:562372 unsigned int style,
[email protected]dd07d60b2009-04-23 18:24:322373 bool incognito,
[email protected]71f65dd2009-02-11 19:14:562374 HWND* tab_container_window,
[email protected]c4e944f2009-06-11 18:22:192375 HWND* tab_window,
[email protected]71f65dd2009-02-11 19:14:562376 int* tab_handle) {
2377 *tab_handle = 0;
2378 *tab_container_window = NULL;
[email protected]c4e944f2009-06-11 18:22:192379 *tab_window = NULL;
initial.commit09911bf2008-07-26 23:55:292380 ExternalTabContainer *external_tab_container =
2381 new ExternalTabContainer(this);
[email protected]dd07d60b2009-04-23 18:24:322382 Profile* profile = incognito? profile_->GetOffTheRecordProfile() : profile_;
2383 external_tab_container->Init(profile, parent, dimensions, style);
initial.commit09911bf2008-07-26 23:55:292384 TabContents* tab_contents = external_tab_container->tab_contents();
2385 if (tab_contents) {
[email protected]ce3fa3c2009-04-20 19:55:572386 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
[email protected]eac83f02009-05-08 18:44:442387 external_tab_container->set_tab_handle(*tab_handle);
[email protected]9095e982009-05-27 17:28:242388 *tab_container_window = external_tab_container->GetNativeView();
[email protected]c4e944f2009-06-11 18:22:192389 *tab_window = tab_contents->GetNativeView();
[email protected]31fb110522009-01-28 21:50:392390 } else {
2391 delete external_tab_container;
initial.commit09911bf2008-07-26 23:55:292392 }
initial.commit09911bf2008-07-26 23:55:292393}
[email protected]d2cc6ed2009-04-24 00:26:172394#endif
initial.commit09911bf2008-07-26 23:55:292395
[email protected]71f65dd2009-02-11 19:14:562396void AutomationProvider::NavigateInExternalTab(
2397 int handle, const GURL& url,
2398 AutomationMsg_NavigationResponseValues* status) {
2399 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:292400
2401 if (tab_tracker_->ContainsHandle(handle)) {
2402 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c0588052008-10-27 23:01:502403 tab->LoadURL(url, GURL(), PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:562404 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:292405 }
initial.commit09911bf2008-07-26 23:55:292406}
2407
[email protected]d2cc6ed2009-04-24 00:26:172408#if defined(OS_WIN)
2409// TODO(port): remove windowisms.
[email protected]71f65dd2009-02-11 19:14:562410void AutomationProvider::SetAcceleratorsForTab(int handle,
initial.commit09911bf2008-07-26 23:55:292411 HACCEL accel_table,
[email protected]71f65dd2009-02-11 19:14:562412 int accel_entry_count,
2413 bool* status) {
2414 *status = false;
2415
[email protected]b9d227492009-02-10 15:20:272416 ExternalTabContainer* external_tab = GetExternalTabForHandle(handle);
2417 if (external_tab) {
2418 external_tab->SetAccelerators(accel_table, accel_entry_count);
[email protected]71f65dd2009-02-11 19:14:562419 *status = true;
initial.commit09911bf2008-07-26 23:55:292420 }
initial.commit09911bf2008-07-26 23:55:292421}
2422
2423void AutomationProvider::ProcessUnhandledAccelerator(
2424 const IPC::Message& message, int handle, const MSG& msg) {
[email protected]b9d227492009-02-10 15:20:272425 ExternalTabContainer* external_tab = GetExternalTabForHandle(handle);
2426 if (external_tab) {
2427 external_tab->ProcessUnhandledAccelerator(msg);
2428 }
2429 // This message expects no response.
2430}
[email protected]d2cc6ed2009-04-24 00:26:172431#endif
[email protected]b9d227492009-02-10 15:20:272432
[email protected]71f65dd2009-02-11 19:14:562433void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2434 IPC::Message* reply_message) {
2435 if (tab_tracker_->ContainsHandle(tab_handle)) {
2436 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2437 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:142438 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:562439 }
2440}
2441
[email protected]b9d227492009-02-10 15:20:272442void AutomationProvider::SetInitialFocus(const IPC::Message& message,
2443 int handle, bool reverse) {
[email protected]d2cc6ed2009-04-24 00:26:172444#if defined(OS_WIN)
[email protected]b9d227492009-02-10 15:20:272445 ExternalTabContainer* external_tab = GetExternalTabForHandle(handle);
2446 if (external_tab) {
[email protected]90daadb42009-06-08 21:27:282447 external_tab->FocusThroughTabTraversal(reverse);
initial.commit09911bf2008-07-26 23:55:292448 }
2449 // This message expects no response.
[email protected]d2cc6ed2009-04-24 00:26:172450#elif defined(OS_POSIX)
2451 // TODO(port) enable this function.
2452 NOTIMPLEMENTED();
2453#endif
initial.commit09911bf2008-07-26 23:55:292454}
2455
[email protected]d2cc6ed2009-04-24 00:26:172456// TODO(port): enable these functions.
2457#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:562458void AutomationProvider::GetSecurityState(int handle, bool* success,
2459 SecurityStyle* security_style,
2460 int* ssl_cert_status,
2461 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:292462 if (tab_tracker_->ContainsHandle(handle)) {
2463 NavigationController* tab = tab_tracker_->GetResource(handle);
2464 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562465 *success = true;
2466 *security_style = entry->ssl().security_style();
2467 *ssl_cert_status = entry->ssl().cert_status();
2468 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292469 } else {
[email protected]71f65dd2009-02-11 19:14:562470 *success = false;
2471 *security_style = SECURITY_STYLE_UNKNOWN;
2472 *ssl_cert_status = 0;
2473 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292474 }
2475}
2476
[email protected]71f65dd2009-02-11 19:14:562477void AutomationProvider::GetPageType(int handle, bool* success,
2478 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:292479 if (tab_tracker_->ContainsHandle(handle)) {
2480 NavigationController* tab = tab_tracker_->GetResource(handle);
2481 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562482 *page_type = entry->page_type();
2483 *success = true;
initial.commit09911bf2008-07-26 23:55:292484 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:342485 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:562486 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:342487 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:562488 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292489 } else {
[email protected]71f65dd2009-02-11 19:14:562490 *success = false;
2491 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292492 }
2493}
2494
[email protected]71f65dd2009-02-11 19:14:562495void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
2496 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292497 if (tab_tracker_->ContainsHandle(handle)) {
2498 NavigationController* tab = tab_tracker_->GetResource(handle);
2499 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072500 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:402501 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:472502 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:342503 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:292504 if (ssl_blocking_page) {
2505 if (proceed) {
[email protected]71f65dd2009-02-11 19:14:562506 AddNavigationStatusListener<bool>(tab, reply_message, true, true,
2507 false);
2508 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:292509 return;
2510 }
2511 ssl_blocking_page->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562512 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(reply_message,
2513 true);
2514 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292515 return;
2516 }
2517 }
2518 }
2519 // We failed.
[email protected]71f65dd2009-02-11 19:14:562520 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(reply_message,
2521 false);
2522 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292523}
[email protected]de246f52009-02-25 18:25:452524#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292525
[email protected]71f65dd2009-02-11 19:14:562526void AutomationProvider::BringBrowserToFront(int browser_handle,
2527 bool* success) {
initial.commit09911bf2008-07-26 23:55:292528 if (browser_tracker_->ContainsHandle(browser_handle)) {
2529 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:062530 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:562531 *success = true;
initial.commit09911bf2008-07-26 23:55:292532 } else {
[email protected]71f65dd2009-02-11 19:14:562533 *success = false;
initial.commit09911bf2008-07-26 23:55:292534 }
2535}
2536
[email protected]71f65dd2009-02-11 19:14:562537void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
2538 int message_num,
2539 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:292540 if (browser_tracker_->ContainsHandle(browser_handle)) {
2541 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562542 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:142543 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:292544 } else {
[email protected]71f65dd2009-02-11 19:14:562545 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:292546 }
2547}
2548
[email protected]de246f52009-02-25 18:25:452549#if defined(OS_WIN)
[email protected]3753f522009-04-14 23:15:472550// TODO(port): Enable this.
[email protected]71f65dd2009-02-11 19:14:562551void AutomationProvider::PrintNow(int tab_handle,
2552 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:572553 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:342554 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2555 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:292556 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572557 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:142558 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:342559 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:572560 return;
initial.commit09911bf2008-07-26 23:55:292561 }
[email protected]71f65dd2009-02-11 19:14:562562 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
2563 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292564}
[email protected]3753f522009-04-14 23:15:472565#endif
initial.commit09911bf2008-07-26 23:55:292566
[email protected]71f65dd2009-02-11 19:14:562567void AutomationProvider::SavePage(int tab_handle,
initial.commit09911bf2008-07-26 23:55:292568 const std::wstring& file_name,
2569 const std::wstring& dir_path,
[email protected]71f65dd2009-02-11 19:14:562570 int type,
2571 bool* success) {
initial.commit09911bf2008-07-26 23:55:292572 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562573 *success = false;
initial.commit09911bf2008-07-26 23:55:292574 return;
2575 }
2576
2577 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2578 Browser* browser = FindAndActivateTab(nav);
2579 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142580 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562581 *success = false;
initial.commit09911bf2008-07-26 23:55:292582 return;
2583 }
2584
initial.commit09911bf2008-07-26 23:55:292585 SavePackage::SavePackageType save_type =
2586 static_cast<SavePackage::SavePackageType>(type);
2587 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2588 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:342589 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292590
[email protected]71f65dd2009-02-11 19:14:562591 *success = true;
initial.commit09911bf2008-07-26 23:55:292592}
2593
[email protected]13869dd2009-05-05 00:40:062594#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]3753f522009-04-14 23:15:472595// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:562596void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2597 bool* success,
2598 std::wstring* text) {
2599 *success = false;
initial.commit09911bf2008-07-26 23:55:292600 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562601 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522602 GetText();
[email protected]71f65dd2009-02-11 19:14:562603 *success = true;
initial.commit09911bf2008-07-26 23:55:292604 }
initial.commit09911bf2008-07-26 23:55:292605}
2606
[email protected]71f65dd2009-02-11 19:14:562607void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2608 const std::wstring& text,
2609 bool* success) {
2610 *success = false;
initial.commit09911bf2008-07-26 23:55:292611 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522612 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2613 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562614 *success = true;
initial.commit09911bf2008-07-26 23:55:292615 }
initial.commit09911bf2008-07-26 23:55:292616}
2617
2618void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562619 int autocomplete_edit_handle,
2620 bool* success,
2621 std::vector<AutocompleteMatchData>* matches) {
2622 *success = false;
initial.commit09911bf2008-07-26 23:55:292623 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272624 const AutocompleteResult& result = autocomplete_edit_tracker_->
2625 GetResource(autocomplete_edit_handle)->model()->result();
2626 for (AutocompleteResult::const_iterator i = result.begin();
2627 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562628 matches->push_back(AutocompleteMatchData(*i));
2629 *success = true;
initial.commit09911bf2008-07-26 23:55:292630 }
initial.commit09911bf2008-07-26 23:55:292631}
2632
2633void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562634 int autocomplete_edit_handle,
2635 bool* success,
2636 bool* query_in_progress) {
2637 *success = false;
2638 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292639 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562640 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522641 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562642 *success = true;
initial.commit09911bf2008-07-26 23:55:292643 }
initial.commit09911bf2008-07-26 23:55:292644}
2645
[email protected]28790922009-03-09 19:48:372646void AutomationProvider::OnMessageFromExternalHost(int handle,
2647 const std::string& message,
2648 const std::string& origin,
2649 const std::string& target) {
[email protected]fa83e762008-08-15 21:41:392650 if (tab_tracker_->ContainsHandle(handle)) {
2651 NavigationController* tab = tab_tracker_->GetResource(handle);
2652 if (!tab) {
2653 NOTREACHED();
2654 return;
2655 }
[email protected]f44265b2009-05-19 18:52:502656
[email protected]965524b2009-04-04 21:32:402657 TabContents* tab_contents = tab->tab_contents();
[email protected]fa83e762008-08-15 21:41:392658 if (!tab_contents) {
2659 NOTREACHED();
2660 return;
2661 }
2662
[email protected]57c6a652009-05-04 07:58:342663 RenderViewHost* view_host = tab_contents->render_view_host();
[email protected]fa83e762008-08-15 21:41:392664 if (!view_host) {
2665 return;
2666 }
2667
[email protected]f44265b2009-05-19 18:52:502668 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
[email protected]a9024892009-06-16 23:13:552669 view_host, message, origin, target)) {
[email protected]f44265b2009-05-19 18:52:502670 // Message was diverted.
2671 return;
[email protected]b83e4602009-05-15 22:58:332672 }
[email protected]f44265b2009-05-19 18:52:502673
2674 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
[email protected]a9024892009-06-16 23:13:552675 origin, target, this, view_host, handle)) {
2676 // Message was diverted.
2677 return;
2678 }
2679
2680 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) {
[email protected]f44265b2009-05-19 18:52:502681 // Message was diverted.
2682 return;
2683 }
2684
2685 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:392686 }
2687}
[email protected]a9024892009-06-16 23:13:552688
2689bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost(
2690 const std::string& message, const std::string& origin,
2691 const std::string& target) {
2692 if (target !=
2693 extension_automation_constants::kAutomationBrowserEventRequestTarget)
2694 return false;
2695
2696 if (origin != extension_automation_constants::kAutomationOrigin) {
2697 LOG(WARNING) << "Wrong origin on automation browser event " << origin;
2698 return false;
2699 }
2700
2701 // The message is a JSON-encoded array with two elements, both strings. The
2702 // first is the name of the event to dispatch. The second is a JSON-encoding
2703 // of the arguments specific to that event.
2704 scoped_ptr<Value> message_value(JSONReader::Read(message, false));
2705 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) {
2706 LOG(WARNING) << "Invalid browser event specified through automation";
2707 return false;
2708 }
2709
2710 const ListValue* args = static_cast<const ListValue*>(message_value.get());
2711
2712 std::string event_name;
2713 if (!args->GetString(0, &event_name)) {
2714 LOG(WARNING) << "No browser event name specified through automation";
2715 return false;
2716 }
2717
2718 std::string json_args;
2719 if (!args->GetString(1, &json_args)) {
2720 LOG(WARNING) << "No browser event args specified through automation";
2721 return false;
2722 }
2723
2724 ExtensionMessageService::GetInstance(profile()->GetRequestContext())->
2725 DispatchEventToRenderers(event_name.c_str(), json_args);
2726
2727 return true;
2728}
[email protected]13869dd2009-05-05 00:40:062729#endif // defined(OS_WIN) || defined(OS_LINUX)
[email protected]fa83e762008-08-15 21:41:392730
[email protected]57c6a652009-05-04 07:58:342731TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572732 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572733 if (tab_tracker_->ContainsHandle(handle)) {
2734 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342735 if (tab)
2736 *tab = nav_controller;
2737 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572738 }
[email protected]57c6a652009-05-04 07:58:342739 return NULL;
[email protected]20e93d12008-08-28 16:31:572740}
2741
[email protected]5cc063692009-04-07 23:21:312742#if defined(OS_WIN)
[email protected]b9d227492009-02-10 15:20:272743ExternalTabContainer* AutomationProvider::GetExternalTabForHandle(int handle) {
2744 if (tab_tracker_->ContainsHandle(handle)) {
2745 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]965524b2009-04-04 21:32:402746 return ExternalTabContainer::GetContainerForTab(
2747 tab->tab_contents()->GetNativeView());
[email protected]b9d227492009-02-10 15:20:272748 }
2749
2750 return NULL;
2751}
[email protected]de246f52009-02-25 18:25:452752#endif // defined(OS_WIN)
[email protected]b9d227492009-02-10 15:20:272753
initial.commit09911bf2008-07-26 23:55:292754TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2755 : AutomationProvider(profile) {
2756 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142757 registrar_.Add(this, NotificationType::SESSION_END,
2758 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292759}
2760
2761TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292762 BrowserList::RemoveObserver(this);
2763}
2764
2765void TestingAutomationProvider::OnChannelError() {
2766 BrowserList::CloseAllBrowsers(true);
2767 AutomationProvider::OnChannelError();
2768}
2769
2770void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2771 // For backwards compatibility with the testing automation interface, we
2772 // want the automation provider (and hence the process) to go away when the
2773 // last browser goes away.
2774 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292775 // If you change this, update Observer for NotificationType::SESSION_END
2776 // below.
[email protected]295039bd2008-08-15 04:32:572777 MessageLoop::current()->PostTask(FROM_HERE,
2778 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292779 }
2780}
2781
2782void TestingAutomationProvider::Observe(NotificationType type,
2783 const NotificationSource& source,
2784 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562785 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292786 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2787 // before the task runs resulting in this object not being deleted. This
2788 // Release balance out the Release scheduled by OnBrowserRemoving.
2789 Release();
2790}
[email protected]295039bd2008-08-15 04:32:572791
2792void TestingAutomationProvider::OnRemoveProvider() {
2793 AutomationProviderList::GetInstance()->RemoveProvider(this);
2794}
[email protected]8a3422c92008-09-24 17:42:422795
[email protected]71f65dd2009-02-11 19:14:562796void AutomationProvider::GetSSLInfoBarCount(int handle, int* count) {
2797 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422798 if (tab_tracker_->ContainsHandle(handle)) {
2799 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342800 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112801 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422802 }
[email protected]8a3422c92008-09-24 17:42:422803}
2804
[email protected]71f65dd2009-02-11 19:14:562805void AutomationProvider::ClickSSLInfoBarLink(int handle,
[email protected]8a3422c92008-09-24 17:42:422806 int info_bar_index,
[email protected]71f65dd2009-02-11 19:14:562807 bool wait_for_navigation,
2808 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422809 bool success = false;
2810 if (tab_tracker_->ContainsHandle(handle)) {
2811 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2812 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112813 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422814 if (info_bar_index >= 0 && info_bar_index < count) {
2815 if (wait_for_navigation) {
[email protected]71f65dd2009-02-11 19:14:562816 AddNavigationStatusListener<bool>(nav_controller, reply_message,
2817 true, true, false);
[email protected]8a3422c92008-09-24 17:42:422818 }
[email protected]eb9ba192008-12-02 02:41:342819 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112820 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342821 info_bar_index);
2822 if (delegate->AsConfirmInfoBarDelegate())
2823 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422824 success = true;
2825 }
2826 }
[email protected]4f3dc372009-02-24 00:10:292827 }
[email protected]8a3422c92008-09-24 17:42:422828 if (!wait_for_navigation || !success)
[email protected]71f65dd2009-02-11 19:14:562829 AutomationMsg_ClickSSLInfoBarLink::WriteReplyParams(reply_message,
2830 success);
[email protected]8a3422c92008-09-24 17:42:422831}
2832
[email protected]71f65dd2009-02-11 19:14:562833void AutomationProvider::GetLastNavigationTime(int handle,
2834 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422835 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562836 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422837}
2838
[email protected]71f65dd2009-02-11 19:14:562839void AutomationProvider::WaitForNavigation(int handle,
2840 int64 last_navigation_time,
2841 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422842 NavigationController* controller = NULL;
2843 if (tab_tracker_->ContainsHandle(handle))
2844 controller = tab_tracker_->GetResource(handle);
2845
2846 Time time = tab_tracker_->GetLastNavigationTime(handle);
2847 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562848 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
2849 controller != NULL);
[email protected]4f3dc372009-02-24 00:10:292850 return;
[email protected]8a3422c92008-09-24 17:42:422851 }
2852
[email protected]71f65dd2009-02-11 19:14:562853 AddNavigationStatusListener<bool>(controller, reply_message, true, true,
2854 false);
[email protected]8a3422c92008-09-24 17:42:422855}
2856
[email protected]71f65dd2009-02-11 19:14:562857void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162858 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562859 int value,
2860 bool* success) {
2861 *success = false;
[email protected]8a3422c92008-09-24 17:42:422862 if (browser_tracker_->ContainsHandle(handle)) {
2863 Browser* browser = browser_tracker_->GetResource(handle);
2864 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562865 *success = true;
[email protected]8a3422c92008-09-24 17:42:422866 }
[email protected]8a3422c92008-09-24 17:42:422867}
[email protected]97fa6ce32008-12-19 01:48:162868
[email protected]71f65dd2009-02-11 19:14:562869void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162870 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562871 const std::wstring& value,
2872 bool* success) {
2873 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162874 if (browser_tracker_->ContainsHandle(handle)) {
2875 Browser* browser = browser_tracker_->GetResource(handle);
2876 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562877 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162878 }
[email protected]97fa6ce32008-12-19 01:48:162879}
2880
[email protected]71f65dd2009-02-11 19:14:562881void AutomationProvider::GetBooleanPreference(int handle,
2882 const std::wstring& name,
2883 bool* success,
2884 bool* value) {
2885 *success = false;
2886 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162887 if (browser_tracker_->ContainsHandle(handle)) {
2888 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562889 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2890 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162891 }
[email protected]97fa6ce32008-12-19 01:48:162892}
2893
[email protected]71f65dd2009-02-11 19:14:562894void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162895 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562896 bool value,
2897 bool* success) {
2898 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162899 if (browser_tracker_->ContainsHandle(handle)) {
2900 Browser* browser = browser_tracker_->GetResource(handle);
2901 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562902 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162903 }
[email protected]97fa6ce32008-12-19 01:48:162904}
2905
2906// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562907void AutomationProvider::GetPageCurrentEncoding(
2908 int tab_handle, std::wstring* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162909 if (tab_tracker_->ContainsHandle(tab_handle)) {
2910 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2911 Browser* browser = FindAndActivateTab(nav);
2912 DCHECK(browser);
2913
[email protected]57c6a652009-05-04 07:58:342914 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2915 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162916 }
[email protected]97fa6ce32008-12-19 01:48:162917}
2918
2919// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562920void AutomationProvider::OverrideEncoding(int tab_handle,
2921 const std::wstring& encoding_name,
2922 bool* success) {
2923 *success = false;
[email protected]de246f52009-02-25 18:25:452924#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:162925 if (tab_tracker_->ContainsHandle(tab_handle)) {
2926 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2927 Browser* browser = FindAndActivateTab(nav);
2928 DCHECK(browser);
2929
[email protected]1fc025202009-01-20 23:03:142930 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]7f0005a2009-04-15 03:25:112931 TabContents* tab_contents = nav->tab_contents();
[email protected]97fa6ce32008-12-19 01:48:162932 int selected_encoding_id =
2933 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2934 if (selected_encoding_id) {
2935 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562936 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162937 }
2938 }
2939 }
[email protected]de246f52009-02-25 18:25:452940#else
2941 // TODO(port): Enable when encoding-related parts of Browser are ported.
2942 NOTIMPLEMENTED();
2943#endif
[email protected]97fa6ce32008-12-19 01:48:162944}
[email protected]5bcdb312009-01-07 21:43:202945
[email protected]4d434a1a2009-02-11 21:06:572946void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202947 SavePackage::SetShouldPromptUser(should_prompt);
2948}
[email protected]87eab222009-03-13 00:47:452949
[email protected]b83e4602009-05-15 22:58:332950void AutomationProvider::SetEnableExtensionAutomation(bool automation_enabled) {
2951 AutomationExtensionFunction::SetEnabled(automation_enabled);
2952}
2953
[email protected]d2cc6ed2009-04-24 00:26:172954#if defined(OS_WIN)
2955// TODO(port): Reposition_Params is win-specific. We'll need to port it.
[email protected]87eab222009-03-13 00:47:452956void AutomationProvider::OnTabReposition(
2957 int tab_handle, const IPC::Reposition_Params& params) {
2958 if (!tab_tracker_->ContainsHandle(tab_handle))
2959 return;
2960
2961 if (!IsWindow(params.window))
2962 return;
2963
2964 unsigned long process_id = 0;
2965 unsigned long thread_id = 0;
2966
2967 thread_id = GetWindowThreadProcessId(params.window, &process_id);
2968
2969 if (thread_id != GetCurrentThreadId()) {
2970 NOTREACHED();
2971 return;
2972 }
2973
[email protected]a9233d0f2009-04-20 05:39:332974 SetWindowPos(params.window, params.window_insert_after, params.left,
2975 params.top, params.width, params.height, params.flags);
2976
[email protected]a2c5a9892009-04-07 16:13:452977 if (params.set_parent) {
2978 if (IsWindow(params.parent_window)) {
2979 if (!SetParent(params.window, params.parent_window))
2980 DLOG(WARNING) << "SetParent failed. Error 0x%x" << GetLastError();
2981 }
2982 }
[email protected]87eab222009-03-13 00:47:452983}
[email protected]e943d6662009-06-12 03:50:392984
2985void AutomationProvider::OnForwardContextMenuCommandToChrome(int tab_handle,
2986 int command) {
2987 if (tab_tracker_->ContainsHandle(tab_handle)) {
2988 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2989 if (!tab) {
2990 NOTREACHED();
2991 return;
2992 }
2993
2994 TabContents* tab_contents = tab->tab_contents();
2995 if (!tab_contents || !tab_contents->delegate()) {
2996 NOTREACHED();
2997 return;
2998 }
2999
3000 tab_contents->delegate()->ExecuteContextMenuCommand(command);
3001 }
3002}
3003
3004#endif // defined(OS_WIN)
[email protected]3753f522009-04-14 23:15:473005
3006void AutomationProvider::GetWindowTitle(int handle, string16* text) {
3007 gfx::NativeWindow window = window_tracker_->GetResource(handle);
3008 text->assign(platform_util::GetWindowTitle(window));
3009}
[email protected]66ba4932009-06-04 19:22:133010
3011void AutomationProvider::GetBlockedPopupCount(int handle, int* count) {
3012 *count = -1; // -1 is the error code
3013 if (tab_tracker_->ContainsHandle(handle)) {
3014 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
3015 TabContents* tab_contents = nav_controller->tab_contents();
3016 if (tab_contents) {
3017 BlockedPopupContainer* container =
3018 tab_contents->blocked_popup_container();
3019 if (container) {
3020 *count = static_cast<int>(container->GetBlockedPopupCount());
3021 } else {
3022 // If we don't have a container, we don't have any blocked popups to
3023 // contain!
3024 *count = 0;
3025 }
3026 }
3027 }
3028}