blob: a7853bebe5699047ddaafb67c67b7600fe92a7a2 [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]5fac9622009-02-04 21:49:387#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/path_service.h"
[email protected]4c4d8d22009-03-04 05:29:279#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3810#include "base/thread.h"
[email protected]4f3dc372009-02-24 00:10:2911#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2812#include "chrome/browser/app_modal_dialog.h"
initial.commit09911bf2008-07-26 23:55:2913#include "chrome/browser/automation/automation_provider_list.h"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/browser/automation/url_request_failed_dns_job.h"
15#include "chrome/browser/automation/url_request_mock_http_job.h"
16#include "chrome/browser/automation/url_request_slow_download_job.h"
[email protected]f3e99e32008-07-30 04:48:3917#include "chrome/browser/browser_window.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/browser/dom_operation_notification_details.h"
[email protected]cdaa8652008-09-13 02:48:5919#include "chrome/browser/download/download_manager.h"
[email protected]4801ecc2009-04-05 04:52:5820#include "chrome/browser/find_bar.h"
21#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/browser/find_notification_details.h"
[email protected]6524b5f92009-01-22 17:48:2523#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0324#include "chrome/browser/ssl/ssl_manager.h"
25#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]f3ec7742009-01-15 00:59:1626#include "chrome/browser/tab_contents/web_contents.h"
27#include "chrome/browser/tab_contents/web_contents_view.h"
initial.commit09911bf2008-07-26 23:55:2928#include "chrome/common/chrome_paths.h"
[email protected]6a02963e2009-01-06 16:58:0329#include "chrome/common/notification_registrar.h"
[email protected]8a3422c92008-09-24 17:42:4230#include "chrome/common/pref_service.h"
[email protected]71f65dd2009-02-11 19:14:5631#include "chrome/test/automation/automation_messages.h"
initial.commit09911bf2008-07-26 23:55:2932#include "net/base/cookie_monster.h"
[email protected]319d9e6f2009-02-18 19:47:2133#include "net/url_request/url_request_context.h"
initial.commit09911bf2008-07-26 23:55:2934#include "net/url_request/url_request_filter.h"
35
[email protected]de246f52009-02-25 18:25:4536#if defined(OS_WIN)
37// TODO(port): Port these headers.
38#include "chrome/browser/app_modal_dialog_queue.h"
39#include "chrome/browser/automation/ui_controls.h"
40#include "chrome/browser/character_encoding.h"
41#include "chrome/browser/download/save_package.h"
42#include "chrome/browser/external_tab_container.h"
43#include "chrome/browser/login_prompt.h"
44#include "chrome/browser/printing/print_job.h"
45#include "chrome/browser/views/bookmark_bar_view.h"
46#include "chrome/browser/views/location_bar_view.h"
[email protected]0bfa713f2009-04-07 20:18:2847#include "chrome/views/window/dialog_delegate.h"
[email protected]0e8588c12009-03-17 01:44:3648#include "chrome/views/window/window.h"
[email protected]de246f52009-02-25 18:25:4549#endif // defined(OS_WIN)
50
[email protected]e1acf6f2008-10-27 20:43:3351using base::Time;
52
initial.commit09911bf2008-07-26 23:55:2953class InitialLoadObserver : public NotificationObserver {
54 public:
55 InitialLoadObserver(size_t tab_count, AutomationProvider* automation)
[email protected]66791d22009-02-24 20:11:3356 : automation_(automation),
57 outstanding_tab_count_(tab_count) {
initial.commit09911bf2008-07-26 23:55:2958 if (outstanding_tab_count_ > 0) {
[email protected]bfd04a62009-02-01 18:16:5659 registrar_.Add(this, NotificationType::LOAD_START,
[email protected]6a02963e2009-01-06 16:58:0360 NotificationService::AllSources());
[email protected]bfd04a62009-02-01 18:16:5661 registrar_.Add(this, NotificationType::LOAD_STOP,
[email protected]6a02963e2009-01-06 16:58:0362 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:2963 }
64 }
65
66 ~InitialLoadObserver() {
initial.commit09911bf2008-07-26 23:55:2967 }
68
69 void ConditionMet() {
[email protected]6a02963e2009-01-06 16:58:0370 registrar_.RemoveAll();
initial.commit09911bf2008-07-26 23:55:2971 automation_->Send(new AutomationMsg_InitialLoadsComplete(0));
72 }
73
initial.commit09911bf2008-07-26 23:55:2974 virtual void Observe(NotificationType type,
75 const NotificationSource& source,
76 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:5677 if (type == NotificationType::LOAD_START) {
initial.commit09911bf2008-07-26 23:55:2978 if (outstanding_tab_count_ > loading_tabs_.size())
79 loading_tabs_.insert(source.map_key());
[email protected]bfd04a62009-02-01 18:16:5680 } else if (type == NotificationType::LOAD_STOP) {
initial.commit09911bf2008-07-26 23:55:2981 if (outstanding_tab_count_ > finished_tabs_.size()) {
82 if (loading_tabs_.find(source.map_key()) != loading_tabs_.end())
83 finished_tabs_.insert(source.map_key());
84 if (outstanding_tab_count_ == finished_tabs_.size())
85 ConditionMet();
86 }
87 } else {
88 NOTREACHED();
89 }
90 }
91
92 private:
93 typedef std::set<uintptr_t> TabSet;
94
[email protected]6a02963e2009-01-06 16:58:0395 NotificationRegistrar registrar_;
96
initial.commit09911bf2008-07-26 23:55:2997 AutomationProvider* automation_;
98 size_t outstanding_tab_count_;
99 TabSet loading_tabs_;
100 TabSet finished_tabs_;
101};
102
103// Watches for NewTabUI page loads for performance timing purposes.
104class NewTabUILoadObserver : public NotificationObserver {
105 public:
106 explicit NewTabUILoadObserver(AutomationProvider* automation)
107 : automation_(automation) {
[email protected]bfd04a62009-02-01 18:16:56108 NotificationService::current()->AddObserver(
109 this, NotificationType::INITIAL_NEW_TAB_UI_LOAD,
110 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29111 }
112
113 ~NewTabUILoadObserver() {
114 Unregister();
115 }
116
117 void Unregister() {
[email protected]bfd04a62009-02-01 18:16:56118 NotificationService::current()->RemoveObserver(
119 this, NotificationType::INITIAL_NEW_TAB_UI_LOAD,
120 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29121 }
122
123 virtual void Observe(NotificationType type,
124 const NotificationSource& source,
125 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56126 if (type == NotificationType::INITIAL_NEW_TAB_UI_LOAD) {
initial.commit09911bf2008-07-26 23:55:29127 Details<int> load_time(details);
128 automation_->Send(
129 new AutomationMsg_InitialNewTabUILoadComplete(0, *load_time.ptr()));
130 } else {
131 NOTREACHED();
132 }
133 }
134
135 private:
136 AutomationProvider* automation_;
137};
138
139class NavigationControllerRestoredObserver : public NotificationObserver {
140 public:
141 NavigationControllerRestoredObserver(AutomationProvider* automation,
142 NavigationController* controller,
[email protected]71f65dd2009-02-11 19:14:56143 int32 routing_id,
144 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29145 : automation_(automation),
146 controller_(controller),
[email protected]71f65dd2009-02-11 19:14:56147 routing_id_(routing_id),
148 reply_message_(reply_message) {
initial.commit09911bf2008-07-26 23:55:29149 if (FinishedRestoring()) {
150 registered_ = false;
151 SendDone();
152 } else {
153 registered_ = true;
154 NotificationService* service = NotificationService::current();
[email protected]bfd04a62009-02-01 18:16:56155 service->AddObserver(this, NotificationType::LOAD_STOP,
initial.commit09911bf2008-07-26 23:55:29156 NotificationService::AllSources());
157 }
158 }
159
160 ~NavigationControllerRestoredObserver() {
161 if (registered_)
162 Unregister();
163 }
164
165 virtual void Observe(NotificationType type,
166 const NotificationSource& source,
167 const NotificationDetails& details) {
168 if (FinishedRestoring()) {
169 SendDone();
170 Unregister();
171 }
172 }
173
174 private:
175 void Unregister() {
176 NotificationService* service = NotificationService::current();
[email protected]bfd04a62009-02-01 18:16:56177 service->RemoveObserver(this, NotificationType::LOAD_STOP,
initial.commit09911bf2008-07-26 23:55:29178 NotificationService::AllSources());
179 registered_ = false;
180 }
181
182 bool FinishedRestoring() {
183 return (!controller_->needs_reload() && !controller_->GetPendingEntry() &&
[email protected]d5f942ba2008-09-26 19:30:34184 !controller_->active_contents()->is_loading());
initial.commit09911bf2008-07-26 23:55:29185 }
186
187 void SendDone() {
[email protected]71f65dd2009-02-11 19:14:56188 DCHECK(reply_message_ != NULL);
189 automation_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:29190 }
191
192 bool registered_;
193 AutomationProvider* automation_;
194 NavigationController* controller_;
195 const int routing_id_;
[email protected]71f65dd2009-02-11 19:14:56196 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29197
[email protected]5a52f162008-08-27 04:15:31198 DISALLOW_COPY_AND_ASSIGN(NavigationControllerRestoredObserver);
initial.commit09911bf2008-07-26 23:55:29199};
200
[email protected]71f65dd2009-02-11 19:14:56201template<class NavigationCodeType>
initial.commit09911bf2008-07-26 23:55:29202class NavigationNotificationObserver : public NotificationObserver {
203 public:
204 NavigationNotificationObserver(NavigationController* controller,
205 AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56206 IPC::Message* reply_message,
207 NavigationCodeType success_code,
208 NavigationCodeType auth_needed_code,
209 NavigationCodeType failed_code)
initial.commit09911bf2008-07-26 23:55:29210 : automation_(automation),
[email protected]71f65dd2009-02-11 19:14:56211 reply_message_(reply_message),
initial.commit09911bf2008-07-26 23:55:29212 controller_(controller),
[email protected]71f65dd2009-02-11 19:14:56213 navigation_started_(false),
214 success_code_(success_code),
215 auth_needed_code_(auth_needed_code),
216 failed_code_(failed_code) {
initial.commit09911bf2008-07-26 23:55:29217 NotificationService* service = NotificationService::current();
[email protected]bfd04a62009-02-01 18:16:56218 service->AddObserver(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]8a3422c92008-09-24 17:42:42219 Source<NavigationController>(controller_));
[email protected]bfd04a62009-02-01 18:16:56220 service->AddObserver(this, NotificationType::LOAD_START,
initial.commit09911bf2008-07-26 23:55:29221 Source<NavigationController>(controller_));
[email protected]bfd04a62009-02-01 18:16:56222 service->AddObserver(this, NotificationType::LOAD_STOP,
initial.commit09911bf2008-07-26 23:55:29223 Source<NavigationController>(controller_));
[email protected]bfd04a62009-02-01 18:16:56224 service->AddObserver(this, NotificationType::AUTH_NEEDED,
initial.commit09911bf2008-07-26 23:55:29225 Source<NavigationController>(controller_));
[email protected]bfd04a62009-02-01 18:16:56226 service->AddObserver(this, NotificationType::AUTH_SUPPLIED,
initial.commit09911bf2008-07-26 23:55:29227 Source<NavigationController>(controller_));
228 }
229
230 ~NavigationNotificationObserver() {
initial.commit09911bf2008-07-26 23:55:29231 Unregister();
232 }
233
[email protected]71f65dd2009-02-11 19:14:56234 void ConditionMet(NavigationCodeType navigation_result) {
235 DCHECK(reply_message_ != NULL);
236
237 IPC::ParamTraits<NavigationCodeType>::Write(reply_message_,
238 navigation_result);
239 automation_->Send(reply_message_);
240 reply_message_ = NULL;
241
[email protected]d5798082008-09-29 21:02:03242 automation_->RemoveNavigationStatusListener(this);
initial.commit09911bf2008-07-26 23:55:29243 delete this;
244 }
245
246 void Unregister() {
[email protected]71f65dd2009-02-11 19:14:56247 // This means we did not receive a notification for this navigation.
248 // Send over a failed navigation status back to the caller to ensure that
249 // the caller does not hang waiting for the response.
250 if (reply_message_) {
251 IPC::ParamTraits<NavigationCodeType>::Write(reply_message_,
252 failed_code_);
253 automation_->Send(reply_message_);
254 reply_message_ = NULL;
255 }
256
initial.commit09911bf2008-07-26 23:55:29257 NotificationService* service = NotificationService::current();
[email protected]bfd04a62009-02-01 18:16:56258 service->RemoveObserver(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]8a3422c92008-09-24 17:42:42259 Source<NavigationController>(controller_));
[email protected]bfd04a62009-02-01 18:16:56260 service->RemoveObserver(this, NotificationType::LOAD_START,
initial.commit09911bf2008-07-26 23:55:29261 Source<NavigationController>(controller_));
[email protected]bfd04a62009-02-01 18:16:56262 service->RemoveObserver(this, NotificationType::LOAD_STOP,
initial.commit09911bf2008-07-26 23:55:29263 Source<NavigationController>(controller_));
[email protected]bfd04a62009-02-01 18:16:56264 service->RemoveObserver(this, NotificationType::AUTH_NEEDED,
initial.commit09911bf2008-07-26 23:55:29265 Source<NavigationController>(controller_));
[email protected]bfd04a62009-02-01 18:16:56266 service->RemoveObserver(this, NotificationType::AUTH_SUPPLIED,
initial.commit09911bf2008-07-26 23:55:29267 Source<NavigationController>(controller_));
268 }
269
270 virtual void Observe(NotificationType type,
271 const NotificationSource& source,
272 const NotificationDetails& details) {
[email protected]8a3422c92008-09-24 17:42:42273 // We listen for 2 events to determine when the navigation started because:
274 // - when this is used by the WaitForNavigation method, we might be invoked
275 // afer the load has started (but not after the entry was committed, as
276 // WaitForNavigation compares times of the last navigation).
277 // - when this is used with a page requiring authentication, we will not get
[email protected]4f3dc372009-02-24 00:10:29278 // a NotificationType::NAV_ENTRY_COMMITTED until after we authenticate, so
279 // we need the NotificationType::LOAD_START.
[email protected]bfd04a62009-02-01 18:16:56280 if (type == NotificationType::NAV_ENTRY_COMMITTED ||
281 type == NotificationType::LOAD_START) {
initial.commit09911bf2008-07-26 23:55:29282 navigation_started_ = true;
[email protected]bfd04a62009-02-01 18:16:56283 } else if (type == NotificationType::LOAD_STOP) {
initial.commit09911bf2008-07-26 23:55:29284 if (navigation_started_) {
285 navigation_started_ = false;
[email protected]71f65dd2009-02-11 19:14:56286 ConditionMet(success_code_);
initial.commit09911bf2008-07-26 23:55:29287 }
[email protected]bfd04a62009-02-01 18:16:56288 } else if (type == NotificationType::AUTH_SUPPLIED) {
initial.commit09911bf2008-07-26 23:55:29289 // The LoginHandler for this tab is no longer valid.
290 automation_->RemoveLoginHandler(controller_);
291
292 // Treat this as if navigation started again, since load start/stop don't
293 // occur while authentication is ongoing.
294 navigation_started_ = true;
[email protected]bfd04a62009-02-01 18:16:56295 } else if (type == NotificationType::AUTH_NEEDED) {
[email protected]de246f52009-02-25 18:25:45296#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29297 if (navigation_started_) {
298 // Remember the login handler that wants authentication.
299 LoginHandler* handler =
300 Details<LoginNotificationDetails>(details)->handler();
301 automation_->AddLoginHandler(controller_, handler);
302
303 // Respond that authentication is needed.
304 navigation_started_ = false;
[email protected]71f65dd2009-02-11 19:14:56305 ConditionMet(auth_needed_code_);
initial.commit09911bf2008-07-26 23:55:29306 } else {
307 NOTREACHED();
308 }
[email protected]de246f52009-02-25 18:25:45309#else
310 // TODO(port): Enable when we have LoginNotificationDetails etc.
311 NOTIMPLEMENTED();
312#endif
initial.commit09911bf2008-07-26 23:55:29313 } else {
314 NOTREACHED();
315 }
316 }
317
318 private:
319 AutomationProvider* automation_;
[email protected]71f65dd2009-02-11 19:14:56320 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29321 NavigationController* controller_;
322 bool navigation_started_;
[email protected]71f65dd2009-02-11 19:14:56323 NavigationCodeType success_code_;
324 NavigationCodeType auth_needed_code_;
325 NavigationCodeType failed_code_;
initial.commit09911bf2008-07-26 23:55:29326};
327
328class TabStripNotificationObserver : public NotificationObserver {
329 public:
330 TabStripNotificationObserver(Browser* parent, NotificationType notification,
331 AutomationProvider* automation, int32 routing_id)
332 : automation_(automation),
initial.commit09911bf2008-07-26 23:55:29333 parent_(parent),
[email protected]66791d22009-02-24 20:11:33334 notification_(notification),
initial.commit09911bf2008-07-26 23:55:29335 routing_id_(routing_id) {
336 NotificationService::current()->
337 AddObserver(this, notification_, NotificationService::AllSources());
338 }
339
340 virtual ~TabStripNotificationObserver() {
341 Unregister();
342 }
343
344 void Unregister() {
345 NotificationService::current()->
346 RemoveObserver(this, notification_, NotificationService::AllSources());
347 }
348
349 virtual void Observe(NotificationType type,
350 const NotificationSource& source,
351 const NotificationDetails& details) {
352 if (type == notification_) {
353 ObserveTab(Source<NavigationController>(source).ptr());
354
355 // If verified, no need to observe anymore
356 automation_->RemoveTabStripObserver(this);
357 delete this;
358 } else {
359 NOTREACHED();
360 }
361 }
362
363 virtual void ObserveTab(NavigationController* controller) = 0;
364
365 protected:
366 AutomationProvider* automation_;
367 Browser* parent_;
368 NotificationType notification_;
369 int32 routing_id_;
370};
371
372class TabAppendedNotificationObserver : public TabStripNotificationObserver {
373 public:
374 TabAppendedNotificationObserver(Browser* parent,
[email protected]4f3dc372009-02-24 00:10:29375 AutomationProvider* automation, int32 routing_id,
[email protected]71f65dd2009-02-11 19:14:56376 IPC::Message* reply_message)
[email protected]bfd04a62009-02-01 18:16:56377 : TabStripNotificationObserver(parent, NotificationType::TAB_PARENTED,
[email protected]71f65dd2009-02-11 19:14:56378 automation, routing_id),
379 reply_message_(reply_message) {
initial.commit09911bf2008-07-26 23:55:29380 }
381
382 virtual void ObserveTab(NavigationController* controller) {
383 int tab_index =
384 automation_->GetIndexForNavigationController(controller, parent_);
385 if (tab_index == TabStripModel::kNoTab) {
386 // This tab notification doesn't belong to the parent_
387 return;
388 }
389
390 // Give the same response even if auth is needed, since it doesn't matter.
[email protected]71f65dd2009-02-11 19:14:56391 automation_->AddNavigationStatusListener<int>(
392 controller, reply_message_, AUTOMATION_MSG_NAVIGATION_SUCCESS,
393 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, AUTOMATION_MSG_NAVIGATION_ERROR);
initial.commit09911bf2008-07-26 23:55:29394 }
[email protected]71f65dd2009-02-11 19:14:56395
396 protected:
397 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29398};
399
400class TabClosedNotificationObserver : public TabStripNotificationObserver {
401 public:
402 TabClosedNotificationObserver(Browser* parent,
403 AutomationProvider* automation,
404 int32 routing_id,
[email protected]71f65dd2009-02-11 19:14:56405 bool wait_until_closed,
406 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29407 : TabStripNotificationObserver(parent,
[email protected]bfd04a62009-02-01 18:16:56408 wait_until_closed ? NotificationType::TAB_CLOSED :
409 NotificationType::TAB_CLOSING,
410 automation,
[email protected]71f65dd2009-02-11 19:14:56411 routing_id),
412 reply_message_(reply_message) {
initial.commit09911bf2008-07-26 23:55:29413 }
414
415 virtual void ObserveTab(NavigationController* controller) {
[email protected]71f65dd2009-02-11 19:14:56416 AutomationMsg_CloseTab::WriteReplyParams(reply_message_, true);
417 automation_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:29418 }
[email protected]71f65dd2009-02-11 19:14:56419
420 protected:
421 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29422};
423
424class BrowserClosedNotificationObserver : public NotificationObserver {
425 public:
426 BrowserClosedNotificationObserver(Browser* browser,
427 AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56428 int32 routing_id,
429 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29430 : automation_(automation),
[email protected]71f65dd2009-02-11 19:14:56431 routing_id_(routing_id),
432 reply_message_(reply_message) {
[email protected]bfd04a62009-02-01 18:16:56433 NotificationService::current()->AddObserver(this,
434 NotificationType::BROWSER_CLOSED, Source<Browser>(browser));
initial.commit09911bf2008-07-26 23:55:29435 }
436
437 virtual void Observe(NotificationType type,
438 const NotificationSource& source,
439 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56440 DCHECK(type == NotificationType::BROWSER_CLOSED);
initial.commit09911bf2008-07-26 23:55:29441 Details<bool> close_app(details);
[email protected]71f65dd2009-02-11 19:14:56442 DCHECK(reply_message_ != NULL);
443 AutomationMsg_CloseBrowser::WriteReplyParams(reply_message_, true,
444 *(close_app.ptr()));
445 automation_->Send(reply_message_);
446 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29447 delete this;
448 }
449
450 private:
451 AutomationProvider* automation_;
452 int32 routing_id_;
[email protected]71f65dd2009-02-11 19:14:56453 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29454};
455
[email protected]4e41709d2009-04-08 00:04:27456namespace {
457
458// Define mapping from command to notification
459struct CommandNotification {
460 int command;
461 NotificationType::Type notification_type;
462};
463
464const struct CommandNotification command_notifications[] = {
465 {IDC_NEW_TAB, NotificationType::TAB_PARENTED}
466};
467
468} // namespace
469
[email protected]56e71b7c2009-03-27 03:05:56470class ExecuteBrowserCommandObserver : public NotificationObserver {
471 public:
[email protected]4e41709d2009-04-08 00:04:27472 ExecuteBrowserCommandObserver(AutomationProvider* automation,
473 IPC::Message* reply_message)
[email protected]56e71b7c2009-03-27 03:05:56474 : automation_(automation),
[email protected]56e71b7c2009-03-27 03:05:56475 reply_message_(reply_message) {
[email protected]56e71b7c2009-03-27 03:05:56476 }
477
478 ~ExecuteBrowserCommandObserver() {
479 }
480
[email protected]4e41709d2009-04-08 00:04:27481 bool Register(int command) {
482 if (!GetNotificationType(command, &notification_type_))
483 return false;
484 registrar_.Add(this, notification_type_,
485 NotificationService::AllSources());
486 return true;
487 }
488
[email protected]56e71b7c2009-03-27 03:05:56489 virtual void Observe(NotificationType type,
490 const NotificationSource& source,
491 const NotificationDetails& details) {
492 if (type == notification_type_) {
[email protected]49a14a82009-03-31 04:16:44493 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message_,
494 true);
[email protected]56e71b7c2009-03-27 03:05:56495 automation_->Send(reply_message_);
496 delete this;
497 } else {
498 NOTREACHED();
499 }
500 }
501
502 private:
[email protected]4e41709d2009-04-08 00:04:27503 bool GetNotificationType(int command, NotificationType::Type* type) {
504 if (!type)
505 return false;
506 bool found = false;
507 for (unsigned int i = 0; i < arraysize(command_notifications); i++) {
508 if (command_notifications[i].command == command) {
509 *type = command_notifications[i].notification_type;
510 found = true;
511 break;
512 }
513 }
514 return found;
515 }
516
[email protected]56e71b7c2009-03-27 03:05:56517 AutomationProvider* automation_;
518 NotificationType::Type notification_type_;
519 IPC::Message* reply_message_;
520 NotificationRegistrar registrar_;
521};
522
initial.commit09911bf2008-07-26 23:55:29523class FindInPageNotificationObserver : public NotificationObserver {
524 public:
525 FindInPageNotificationObserver(AutomationProvider* automation,
526 TabContents* parent_tab,
[email protected]71f65dd2009-02-11 19:14:56527 int32 routing_id,
528 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29529 : automation_(automation),
530 parent_tab_(parent_tab),
[email protected]aedd85a2008-12-04 19:32:49531 routing_id_(routing_id),
[email protected]71f65dd2009-02-11 19:14:56532 active_match_ordinal_(-1),
533 reply_message_(reply_message) {
[email protected]bfd04a62009-02-01 18:16:56534 NotificationService::current()->AddObserver(
535 this,
536 NotificationType::FIND_RESULT_AVAILABLE,
537 Source<TabContents>(parent_tab_));
initial.commit09911bf2008-07-26 23:55:29538 }
539
540 ~FindInPageNotificationObserver() {
541 Unregister();
542 }
543
544 void Unregister() {
[email protected]71f65dd2009-02-11 19:14:56545 DCHECK(reply_message_ == NULL);
initial.commit09911bf2008-07-26 23:55:29546 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56547 RemoveObserver(this, NotificationType::FIND_RESULT_AVAILABLE,
initial.commit09911bf2008-07-26 23:55:29548 Source<TabContents>(parent_tab_));
549 }
550
551 virtual void Observe(NotificationType type, const NotificationSource& source,
552 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56553 if (type == NotificationType::FIND_RESULT_AVAILABLE) {
initial.commit09911bf2008-07-26 23:55:29554 Details<FindNotificationDetails> find_details(details);
555 if (find_details->request_id() == kFindInPageRequestId) {
[email protected]aedd85a2008-12-04 19:32:49556 // We get multiple responses and one of those will contain the ordinal.
557 // This message comes to us before the final update is sent.
558 if (find_details->active_match_ordinal() > -1)
559 active_match_ordinal_ = find_details->active_match_ordinal();
initial.commit09911bf2008-07-26 23:55:29560 if (find_details->final_update()) {
[email protected]71f65dd2009-02-11 19:14:56561 DCHECK(reply_message_ != NULL);
562
563 AutomationMsg_FindInPage::WriteReplyParams(
564 reply_message_, active_match_ordinal_,
565 find_details->number_of_matches());
566
567 automation_->Send(reply_message_);
568 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29569 } else {
570 DLOG(INFO) << "Ignoring, since we only care about the final message";
571 }
572 }
573 } else {
574 NOTREACHED();
575 }
576 }
577
578 // The Find mechanism is over asynchronous IPC, so a search is kicked off and
579 // we wait for notification to find out what the results are. As the user is
580 // typing, new search requests can be issued and the Request ID helps us make
581 // sense of whether this is the current request or an old one. The unit tests,
582 // however, which uses this constant issues only one search at a time, so we
583 // don't need a rolling id to identify each search. But, we still need to
584 // specify one, so we just use a fixed one - its value does not matter.
585 static const int kFindInPageRequestId;
586 private:
587 AutomationProvider* automation_;
588 TabContents* parent_tab_;
589 int32 routing_id_;
[email protected]aedd85a2008-12-04 19:32:49590 // We will at some point (before final update) be notified of the ordinal and
591 // we need to preserve it so we can send it later.
592 int active_match_ordinal_;
[email protected]71f65dd2009-02-11 19:14:56593 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29594};
595
596const int FindInPageNotificationObserver::kFindInPageRequestId = -1;
597
598class DomOperationNotificationObserver : public NotificationObserver {
599 public:
600 explicit DomOperationNotificationObserver(AutomationProvider* automation)
601 : automation_(automation) {
602 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56603 AddObserver(this, NotificationType::DOM_OPERATION_RESPONSE,
initial.commit09911bf2008-07-26 23:55:29604 NotificationService::AllSources());
605 }
606
607 ~DomOperationNotificationObserver() {
608 NotificationService::current()->
[email protected]bfd04a62009-02-01 18:16:56609 RemoveObserver(this, NotificationType::DOM_OPERATION_RESPONSE,
initial.commit09911bf2008-07-26 23:55:29610 NotificationService::AllSources());
611 }
612
613 virtual void Observe(NotificationType type, const NotificationSource& source,
614 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56615 if (NotificationType::DOM_OPERATION_RESPONSE == type) {
initial.commit09911bf2008-07-26 23:55:29616 Details<DomOperationNotificationDetails> dom_op_details(details);
[email protected]71f65dd2009-02-11 19:14:56617
618 IPC::Message* reply_message = automation_->reply_message_release();
619 DCHECK(reply_message != NULL);
620
621 AutomationMsg_DomOperation::WriteReplyParams(
622 reply_message, dom_op_details->json());
623 automation_->Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29624 }
625 }
626 private:
627 AutomationProvider* automation_;
628};
629
630class DomInspectorNotificationObserver : public NotificationObserver {
631 public:
632 explicit DomInspectorNotificationObserver(AutomationProvider* automation)
633 : automation_(automation) {
[email protected]bfd04a62009-02-01 18:16:56634 NotificationService::current()->AddObserver(
635 this,
636 NotificationType::DOM_INSPECT_ELEMENT_RESPONSE,
637 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29638 }
639
640 ~DomInspectorNotificationObserver() {
[email protected]bfd04a62009-02-01 18:16:56641 NotificationService::current()->RemoveObserver(
642 this,
643 NotificationType::DOM_INSPECT_ELEMENT_RESPONSE,
644 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29645 }
646
647 virtual void Observe(NotificationType type, const NotificationSource& source,
648 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56649 if (NotificationType::DOM_INSPECT_ELEMENT_RESPONSE == type) {
initial.commit09911bf2008-07-26 23:55:29650 Details<int> dom_inspect_details(details);
651 automation_->ReceivedInspectElementResponse(*(dom_inspect_details.ptr()));
652 }
653 }
654
655 private:
656 AutomationProvider* automation_;
657};
658
[email protected]de246f52009-02-25 18:25:45659#if defined(OS_WIN)
660// TODO(port): Enable when printing is ported.
initial.commit09911bf2008-07-26 23:55:29661class DocumentPrintedNotificationObserver : public NotificationObserver {
662 public:
663 DocumentPrintedNotificationObserver(AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56664 int32 routing_id,
665 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29666 : automation_(automation),
667 routing_id_(routing_id),
[email protected]71f65dd2009-02-11 19:14:56668 success_(false),
669 reply_message_(reply_message) {
[email protected]bfd04a62009-02-01 18:16:56670 NotificationService::current()->AddObserver(
671 this,
672 NotificationType::PRINT_JOB_EVENT,
673 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29674 }
675
676 ~DocumentPrintedNotificationObserver() {
[email protected]71f65dd2009-02-11 19:14:56677 DCHECK(reply_message_ != NULL);
678 AutomationMsg_PrintNow::WriteReplyParams(reply_message_, success_);
679 automation_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:29680 automation_->RemoveNavigationStatusListener(this);
[email protected]bfd04a62009-02-01 18:16:56681 NotificationService::current()->RemoveObserver(
682 this,
683 NotificationType::PRINT_JOB_EVENT,
684 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29685 }
686
687 virtual void Observe(NotificationType type, const NotificationSource& source,
688 const NotificationDetails& details) {
689 using namespace printing;
[email protected]bfd04a62009-02-01 18:16:56690 DCHECK(type == NotificationType::PRINT_JOB_EVENT);
initial.commit09911bf2008-07-26 23:55:29691 switch (Details<JobEventDetails>(details)->type()) {
692 case JobEventDetails::JOB_DONE: {
693 // Succeeded.
694 success_ = true;
695 delete this;
696 break;
697 }
698 case JobEventDetails::USER_INIT_CANCELED:
699 case JobEventDetails::FAILED: {
700 // Failed.
701 delete this;
702 break;
703 }
704 case JobEventDetails::NEW_DOC:
705 case JobEventDetails::USER_INIT_DONE:
706 case JobEventDetails::DEFAULT_INIT_DONE:
707 case JobEventDetails::NEW_PAGE:
708 case JobEventDetails::PAGE_DONE:
709 case JobEventDetails::DOC_DONE:
710 case JobEventDetails::ALL_PAGES_REQUESTED: {
711 // Don't care.
712 break;
713 }
714 default: {
715 NOTREACHED();
716 break;
717 }
718 }
719 }
720
721 private:
722 scoped_refptr<AutomationProvider> automation_;
723 int32 routing_id_;
724 bool success_;
[email protected]71f65dd2009-02-11 19:14:56725 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29726};
[email protected]de246f52009-02-25 18:25:45727#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29728
[email protected]cbab76d2008-10-13 22:42:47729class AutomationInterstitialPage : public InterstitialPage {
730 public:
[email protected]a3a1d142008-12-19 00:42:30731 AutomationInterstitialPage(WebContents* tab,
[email protected]cbab76d2008-10-13 22:42:47732 const GURL& url,
733 const std::string& contents)
734 : InterstitialPage(tab, true, url),
735 contents_(contents) {
736 }
737
738 virtual std::string GetHTMLContents() { return contents_; }
739
740 private:
741 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29742
[email protected]cbab76d2008-10-13 22:42:47743 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
744};
745
initial.commit09911bf2008-07-26 23:55:29746AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57747 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56748 profile_(profile),
749 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29750 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29751 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20752 window_tracker_.reset(new AutomationWindowTracker(this));
[email protected]de246f52009-02-25 18:25:45753#if defined(OS_WIN)
754 // TODO(port): Enable as the trackers get ported.
initial.commit09911bf2008-07-26 23:55:29755 autocomplete_edit_tracker_.reset(
756 new AutomationAutocompleteEditTracker(this));
757 cwindow_tracker_.reset(new AutomationConstrainedWindowTracker(this));
758 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
[email protected]de246f52009-02-25 18:25:45759#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29760 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
761 dom_inspector_observer_.reset(new DomInspectorNotificationObserver(this));
762}
763
764AutomationProvider::~AutomationProvider() {
[email protected]0da050b92008-08-19 19:29:47765 // Make sure that any outstanding NotificationObservers also get destroyed.
766 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31767 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47768 while ((observer = it.GetNext()) != NULL)
769 delete observer;
initial.commit09911bf2008-07-26 23:55:29770}
771
772void AutomationProvider::ConnectToChannel(const std::wstring& channel_id) {
[email protected]295039bd2008-08-15 04:32:57773 channel_.reset(
[email protected]71f65dd2009-02-11 19:14:56774 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this, NULL,
775 g_browser_process->io_thread()->message_loop(),
776 true, g_browser_process->shutdown_event()));
[email protected]295039bd2008-08-15 04:32:57777 channel_->Send(new AutomationMsg_Hello(0));
initial.commit09911bf2008-07-26 23:55:29778}
779
780void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
781 if (expected_tabs == 0) {
782 Send(new AutomationMsg_InitialLoadsComplete(0));
783 } else {
784 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
785 }
786}
787
[email protected]71f65dd2009-02-11 19:14:56788template<class NavigationCodeType>
initial.commit09911bf2008-07-26 23:55:29789NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]71f65dd2009-02-11 19:14:56790 NavigationController* tab, IPC::Message* reply_message,
791 NavigationCodeType success_code,
792 NavigationCodeType auth_needed_code,
793 NavigationCodeType failed_code) {
initial.commit09911bf2008-07-26 23:55:29794 NotificationObserver* observer =
[email protected]71f65dd2009-02-11 19:14:56795 new NavigationNotificationObserver<NavigationCodeType>(
796 tab, this, reply_message, success_code, auth_needed_code,
797 failed_code);
initial.commit09911bf2008-07-26 23:55:29798
[email protected]71f65dd2009-02-11 19:14:56799 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29800 return observer;
801}
802
803void AutomationProvider::RemoveNavigationStatusListener(
804 NotificationObserver* obs) {
805 notification_observer_list_.RemoveObserver(obs);
806}
807
808NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]71f65dd2009-02-11 19:14:56809 Browser* parent, int32 routing_id, IPC::Message* reply_message) {
810 NotificationObserver* observer =
811 new TabAppendedNotificationObserver(parent, this, routing_id,
812 reply_message);
initial.commit09911bf2008-07-26 23:55:29813 notification_observer_list_.AddObserver(observer);
814
815 return observer;
816}
817
818void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
819 notification_observer_list_.RemoveObserver(obs);
820}
821
822void AutomationProvider::AddLoginHandler(NavigationController* tab,
823 LoginHandler* handler) {
824 login_handler_map_[tab] = handler;
825}
826
827void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
828 DCHECK(login_handler_map_[tab]);
829 login_handler_map_.erase(tab);
830}
831
832int AutomationProvider::GetIndexForNavigationController(
833 const NavigationController* controller, const Browser* parent) const {
834 DCHECK(parent);
835 return parent->GetIndexOfController(controller);
836}
837
838void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
839 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]71f65dd2009-02-11 19:14:56840 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser,
841 CloseBrowser)
842 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
843 CloseBrowserAsync)
844 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
845 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
846 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
847 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
848 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
849 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
850 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL,
851 NavigateToURL)
852 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
853 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
854 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
855 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
856 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
857 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth,
858 CancelAuth)
859 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
860 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
861 GetRedirectsFrom)
862 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount,
initial.commit09911bf2008-07-26 23:55:29863 GetBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56864 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
865 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29866 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56867 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
868 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
initial.commit09911bf2008-07-26 23:55:29869 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow);
[email protected]de246f52009-02-25 18:25:45870#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56871 IPC_MESSAGE_HANDLER(AutomationMsg_WindowHWND, GetWindowHWND)
[email protected]de246f52009-02-25 18:25:45872#endif // defined(OS_WIN)
[email protected]49a14a82009-03-31 04:16:44873 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4ae62752008-08-04 23:28:47874 ExecuteBrowserCommand)
[email protected]49a14a82009-03-31 04:16:44875 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]56e71b7c2009-03-27 03:05:56876 ExecuteBrowserCommandWithNotification)
[email protected]71f65dd2009-02-11 19:14:56877 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds,
initial.commit09911bf2008-07-26 23:55:29878 WindowGetViewBounds)
[email protected]71f65dd2009-02-11 19:14:56879 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible,
880 SetWindowVisible)
[email protected]de246f52009-02-25 18:25:45881#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56882 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
883 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress,
initial.commit09911bf2008-07-26 23:55:29884 WindowSimulateKeyPress)
[email protected]71f65dd2009-02-11 19:14:56885 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
886 WindowSimulateDrag)
[email protected]d7fa7552009-03-20 21:06:37887#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56888 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
889 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37890#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56891 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45892#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56893 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
894 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
895 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
896 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility,
initial.commit09911bf2008-07-26 23:55:29897 GetShelfVisibility)
898 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]71f65dd2009-02-11 19:14:56899 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator,
900 ApplyAccelerator)
901 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
902 ExecuteJavascript)
903 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29904 GetConstrainedWindowCount)
[email protected]71f65dd2009-02-11 19:14:56905 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindow,
initial.commit09911bf2008-07-26 23:55:29906 GetConstrainedWindow)
[email protected]71f65dd2009-02-11 19:14:56907 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedTitle,
initial.commit09911bf2008-07-26 23:55:29908 GetConstrainedTitle)
[email protected]71f65dd2009-02-11 19:14:56909 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage,
initial.commit09911bf2008-07-26 23:55:29910 HandleFindInPageRequest)
[email protected]71f65dd2009-02-11 19:14:56911 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID,
912 GetFocusedViewID)
913 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
914 HandleInspectElementRequest)
initial.commit09911bf2008-07-26 23:55:29915 IPC_MESSAGE_HANDLER(AutomationMsg_SetFilteredInet,
916 SetFilteredInet);
[email protected]71f65dd2009-02-11 19:14:56917 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory,
initial.commit09911bf2008-07-26 23:55:29918 GetDownloadDirectory);
919 IPC_MESSAGE_HANDLER(AutomationMsg_OpenNewBrowserWindow,
920 OpenNewBrowserWindow);
[email protected]71f65dd2009-02-11 19:14:56921 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser,
initial.commit09911bf2008-07-26 23:55:29922 GetWindowForBrowser);
[email protected]71f65dd2009-02-11 19:14:56923 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
initial.commit09911bf2008-07-26 23:55:29924 GetAutocompleteEditForBrowser);
[email protected]71f65dd2009-02-11 19:14:56925 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow,
initial.commit09911bf2008-07-26 23:55:29926 GetBrowserForWindow);
[email protected]de246f52009-02-25 18:25:45927#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29928 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]71f65dd2009-02-11 19:14:56929 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:29930 NavigateInExternalTab)
[email protected]71f65dd2009-02-11 19:14:56931 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
932 ShowInterstitialPage);
933 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
initial.commit09911bf2008-07-26 23:55:29934 HideInterstitialPage);
935 IPC_MESSAGE_HANDLER(AutomationMsg_SetAcceleratorsForTab,
936 SetAcceleratorsForTab)
937 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
938 ProcessUnhandledAccelerator)
[email protected]71f65dd2009-02-11 19:14:56939 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
940 WaitForTabToBeRestored)
[email protected]b9d227492009-02-10 15:20:27941 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus,
942 SetInitialFocus)
[email protected]87eab222009-03-13 00:47:45943 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]de246f52009-02-25 18:25:45944#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29945 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState,
946 GetSecurityState)
947 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType,
948 GetPageType)
[email protected]71f65dd2009-02-11 19:14:56949 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
950 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:29951 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
952 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
953 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:56954 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
955 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
956 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:29957 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56958 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:29959 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:56960 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:29961 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:56962 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:29963 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:56964 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowBounds,
initial.commit09911bf2008-07-26 23:55:29965 GetConstrainedWindowBounds)
[email protected]71f65dd2009-02-11 19:14:56966 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:45967 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45968 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
969 OnMessageFromExternalHost)
[email protected]71f65dd2009-02-11 19:14:56970 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find,
971 HandleFindRequest)
972 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:57973 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:56974 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:57975 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:56976 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
977 GetBookmarkBarVisibility)
978 IPC_MESSAGE_HANDLER(AutomationMsg_GetSSLInfoBarCount,
[email protected]8a3422c92008-09-24 17:42:42979 GetSSLInfoBarCount)
[email protected]71f65dd2009-02-11 19:14:56980 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickSSLInfoBarLink,
981 ClickSSLInfoBarLink)
982 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:42983 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:56984 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
985 WaitForNavigation)
986 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference,
[email protected]8a3422c92008-09-24 17:42:42987 SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:56988 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:44989 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:56990 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:20991 ClickAppModalDialogButton)
[email protected]71f65dd2009-02-11 19:14:56992 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference,
[email protected]97fa6ce32008-12-19 01:48:16993 SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:56994 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16995 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56996 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:16997 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:56998 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:16999 GetPageCurrentEncoding)
[email protected]71f65dd2009-02-11 19:14:561000 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding,
[email protected]97fa6ce32008-12-19 01:48:161001 OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:201002 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
1003 SavePackageShouldPromptUser)
initial.commit09911bf2008-07-26 23:55:291004 IPC_END_MESSAGE_MAP()
1005}
1006
[email protected]71f65dd2009-02-11 19:14:561007void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
1008 *status = -1;
initial.commit09911bf2008-07-26 23:55:291009 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
1010 Browser* browser = browser_tracker_->GetResource(handle);
1011 if (at_index >= 0 && at_index < browser->tab_count()) {
1012 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:561013 *status = 0;
initial.commit09911bf2008-07-26 23:55:291014 }
1015 }
initial.commit09911bf2008-07-26 23:55:291016}
1017
[email protected]71f65dd2009-02-11 19:14:561018void AutomationProvider::AppendTab(int handle, const GURL& url,
1019 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291020 int append_tab_response = -1; // -1 is the error code
1021 NotificationObserver* observer = NULL;
1022
1023 if (browser_tracker_->ContainsHandle(handle)) {
1024 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561025 observer = AddTabStripObserver(browser, reply_message->routing_id(),
1026 reply_message);
[email protected]22735af62009-04-07 21:09:581027 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
1028 PageTransition::TYPED,
1029 true, -1, NULL);
initial.commit09911bf2008-07-26 23:55:291030 if (tab_contents) {
1031 append_tab_response =
1032 GetIndexForNavigationController(tab_contents->controller(), browser);
1033 }
1034 }
1035
1036 if (append_tab_response < 0) {
1037 // The append tab failed. Remove the TabStripObserver
1038 if (observer) {
1039 RemoveTabStripObserver(observer);
1040 delete observer;
1041 }
1042
[email protected]71f65dd2009-02-11 19:14:561043 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
1044 append_tab_response);
1045 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291046 }
1047}
1048
[email protected]71f65dd2009-02-11 19:14:561049void AutomationProvider::NavigateToURL(int handle, const GURL& url,
1050 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291051 if (tab_tracker_->ContainsHandle(handle)) {
1052 NavigationController* tab = tab_tracker_->GetResource(handle);
1053
1054 // Simulate what a user would do. Activate the tab and then navigate.
1055 // We could allow navigating in a background tab in future.
1056 Browser* browser = FindAndActivateTab(tab);
1057
1058 if (browser) {
[email protected]71f65dd2009-02-11 19:14:561059 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1060 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1061 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1062 AUTOMATION_MSG_NAVIGATION_ERROR);
1063
initial.commit09911bf2008-07-26 23:55:291064 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:501065 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:291066 return;
1067 }
1068 }
[email protected]71f65dd2009-02-11 19:14:561069
1070 AutomationMsg_NavigateToURL::WriteReplyParams(
1071 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1072 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291073}
1074
[email protected]71f65dd2009-02-11 19:14:561075void AutomationProvider::NavigationAsync(int handle, const GURL& url,
1076 bool* status) {
1077 *status = false;
initial.commit09911bf2008-07-26 23:55:291078
1079 if (tab_tracker_->ContainsHandle(handle)) {
1080 NavigationController* tab = tab_tracker_->GetResource(handle);
1081
1082 // Simulate what a user would do. Activate the tab and then navigate.
1083 // We could allow navigating in a background tab in future.
1084 Browser* browser = FindAndActivateTab(tab);
1085
1086 if (browser) {
1087 // Don't add any listener unless a callback mechanism is desired.
1088 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:501089 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561090 *status = true;
initial.commit09911bf2008-07-26 23:55:291091 }
1092 }
initial.commit09911bf2008-07-26 23:55:291093}
1094
[email protected]71f65dd2009-02-11 19:14:561095void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291096 if (tab_tracker_->ContainsHandle(handle)) {
1097 NavigationController* tab = tab_tracker_->GetResource(handle);
1098 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:141099 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]71f65dd2009-02-11 19:14:561100 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1101 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1102 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1103 AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]485fba42009-03-24 23:27:291104 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:291105 return;
1106 }
1107 }
[email protected]71f65dd2009-02-11 19:14:561108
1109 AutomationMsg_GoBack::WriteReplyParams(
1110 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1111 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291112}
1113
[email protected]71f65dd2009-02-11 19:14:561114void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291115 if (tab_tracker_->ContainsHandle(handle)) {
1116 NavigationController* tab = tab_tracker_->GetResource(handle);
1117 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:141118 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]71f65dd2009-02-11 19:14:561119 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1120 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1121 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1122 AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]485fba42009-03-24 23:27:291123 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:291124 return;
1125 }
1126 }
[email protected]71f65dd2009-02-11 19:14:561127
1128 AutomationMsg_GoForward::WriteReplyParams(
1129 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1130 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291131}
1132
[email protected]71f65dd2009-02-11 19:14:561133void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291134 if (tab_tracker_->ContainsHandle(handle)) {
1135 NavigationController* tab = tab_tracker_->GetResource(handle);
1136 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:141137 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]71f65dd2009-02-11 19:14:561138 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1139 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1140 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1141 AUTOMATION_MSG_NAVIGATION_ERROR);
initial.commit09911bf2008-07-26 23:55:291142 browser->Reload();
1143 return;
1144 }
1145 }
[email protected]71f65dd2009-02-11 19:14:561146
1147 AutomationMsg_Reload::WriteReplyParams(
1148 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1149 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291150}
1151
[email protected]71f65dd2009-02-11 19:14:561152void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:291153 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:561154 const std::wstring& password,
1155 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291156 int status = -1;
1157
1158 if (tab_tracker_->ContainsHandle(tab_handle)) {
1159 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1160 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1161
1162 if (iter != login_handler_map_.end()) {
1163 // If auth is needed again after this, assume login has failed. This is
1164 // not strictly correct, because a navigation can require both proxy and
1165 // server auth, but it should be OK for now.
1166 LoginHandler* handler = iter->second;
[email protected]71f65dd2009-02-11 19:14:561167 AddNavigationStatusListener<int>(tab, reply_message, 0, -1, -1);
initial.commit09911bf2008-07-26 23:55:291168 handler->SetAuth(username, password);
1169 status = 0;
1170 }
1171 }
[email protected]de246f52009-02-25 18:25:451172
initial.commit09911bf2008-07-26 23:55:291173 if (status < 0) {
[email protected]71f65dd2009-02-11 19:14:561174 AutomationMsg_SetAuth::WriteReplyParams(reply_message, status);
1175 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291176 }
1177}
1178
[email protected]71f65dd2009-02-11 19:14:561179void AutomationProvider::CancelAuth(int tab_handle,
1180 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291181 int status = -1;
1182
1183 if (tab_tracker_->ContainsHandle(tab_handle)) {
1184 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1185 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1186
1187 if (iter != login_handler_map_.end()) {
1188 // If auth is needed again after this, something is screwy.
1189 LoginHandler* handler = iter->second;
[email protected]71f65dd2009-02-11 19:14:561190 AddNavigationStatusListener<int>(tab, reply_message, 0, -1, -1);
initial.commit09911bf2008-07-26 23:55:291191 handler->CancelAuth();
1192 status = 0;
1193 }
1194 }
[email protected]de246f52009-02-25 18:25:451195
initial.commit09911bf2008-07-26 23:55:291196 if (status < 0) {
[email protected]71f65dd2009-02-11 19:14:561197 AutomationMsg_CancelAuth::WriteReplyParams(reply_message, status);
1198 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291199 }
1200}
1201
[email protected]71f65dd2009-02-11 19:14:561202void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
1203 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:291204
1205 if (tab_tracker_->ContainsHandle(tab_handle)) {
1206 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1207 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1208
1209 if (iter != login_handler_map_.end()) {
1210 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:561211 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:291212 }
1213 }
initial.commit09911bf2008-07-26 23:55:291214}
1215
[email protected]71f65dd2009-02-11 19:14:561216void AutomationProvider::GetRedirectsFrom(int tab_handle,
1217 const GURL& source_url,
1218 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291219 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
1220 if (tab_tracker_->ContainsHandle(tab_handle)) {
1221 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1222 HistoryService* history_service =
1223 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
1224
1225 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
1226 "has no history service";
1227 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:561228 DCHECK(reply_message_ == NULL);
1229 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291230 // Schedule a history query for redirects. The response will be sent
1231 // asynchronously from the callback the history system uses to notify us
1232 // that it's done: OnRedirectQueryComplete.
[email protected]71f65dd2009-02-11 19:14:561233 redirect_query_routing_id_ = reply_message->routing_id();
initial.commit09911bf2008-07-26 23:55:291234 redirect_query_ = history_service->QueryRedirectsFrom(
1235 source_url, &consumer_,
1236 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
1237 return; // Response will be sent when query completes.
1238 }
1239 }
1240
1241 // Send failure response.
[email protected]deb57402009-02-06 01:35:301242 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:561243 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
1244 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291245}
1246
[email protected]71f65dd2009-02-11 19:14:561247void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
1248 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291249 if (browser_tracker_->ContainsHandle(handle)) {
1250 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561251 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:291252 }
initial.commit09911bf2008-07-26 23:55:291253}
1254
[email protected]71f65dd2009-02-11 19:14:561255void AutomationProvider::GetBrowserWindowCount(int* window_count) {
1256 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:291257}
1258
[email protected]de246f52009-02-25 18:25:451259#if defined(OS_WIN)
1260// TODO(port): Enable when dialog delegate is ported.
[email protected]71f65dd2009-02-11 19:14:561261void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
1262 int* dialog_button) {
[email protected]0bfa713f2009-04-07 20:18:281263 AppModalDialog* dialog_delegate = AppModalDialogQueue::active_dialog();
[email protected]b3a70332009-02-25 02:40:501264 *showing_dialog = (dialog_delegate != NULL);
1265 if (*showing_dialog)
1266 *dialog_button = dialog_delegate->GetDialogButtons();
1267 else
1268 *dialog_button = views::DialogDelegate::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:201269}
1270
[email protected]71f65dd2009-02-11 19:14:561271void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
1272 *success = false;
[email protected]fad84eab2008-12-05 00:37:201273
[email protected]0bfa713f2009-04-07 20:18:281274 AppModalDialog* dialog_delegate = AppModalDialogQueue::active_dialog();
[email protected]b3a70332009-02-25 02:40:501275 if (dialog_delegate &&
1276 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]fad84eab2008-12-05 00:37:201277 if ((button & views::DialogDelegate::DIALOGBUTTON_OK) ==
1278 views::DialogDelegate::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:281279 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:561280 *success = true;
[email protected]fad84eab2008-12-05 00:37:201281 }
1282 if ((button & views::DialogDelegate::DIALOGBUTTON_CANCEL) ==
1283 views::DialogDelegate::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:561284 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:281285 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:561286 *success = true;
[email protected]fad84eab2008-12-05 00:37:201287 }
1288 }
[email protected]c274acc2008-11-11 20:13:441289}
[email protected]de246f52009-02-25 18:25:451290#endif
[email protected]c274acc2008-11-11 20:13:441291
[email protected]71f65dd2009-02-11 19:14:561292void AutomationProvider::GetBrowserWindow(int index, int* handle) {
1293 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291294 if (index >= 0) {
1295 BrowserList::const_iterator iter = BrowserList::begin();
1296
1297 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
1298 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:561299 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:291300 }
1301 }
initial.commit09911bf2008-07-26 23:55:291302}
1303
[email protected]71f65dd2009-02-11 19:14:561304void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
1305 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291306 Browser* browser = BrowserList::GetLastActive();
1307 if (browser)
[email protected]71f65dd2009-02-11 19:14:561308 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:291309}
1310
[email protected]de246f52009-02-25 18:25:451311#if defined(OS_WIN)
1312// TODO(port): Remove windowsisms.
initial.commit09911bf2008-07-26 23:55:291313BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM l_param) {
1314 if (hwnd == reinterpret_cast<HWND>(l_param)) {
1315 return FALSE;
1316 }
1317 return TRUE;
1318}
1319
[email protected]71f65dd2009-02-11 19:14:561320void AutomationProvider::GetActiveWindow(int* handle) {
initial.commit09911bf2008-07-26 23:55:291321 HWND window = GetForegroundWindow();
1322
1323 // Let's make sure this window belongs to our process.
1324 if (EnumThreadWindows(::GetCurrentThreadId(),
1325 EnumThreadWndProc,
1326 reinterpret_cast<LPARAM>(window))) {
1327 // We enumerated all the windows and did not find the foreground window,
1328 // it is not our window, ignore it.
[email protected]71f65dd2009-02-11 19:14:561329 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291330 return;
1331 }
1332
[email protected]71f65dd2009-02-11 19:14:561333 *handle = window_tracker_->Add(window);
initial.commit09911bf2008-07-26 23:55:291334}
1335
[email protected]71f65dd2009-02-11 19:14:561336void AutomationProvider::GetWindowHWND(int handle, HWND* win32_handle) {
1337 *win32_handle = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291338}
[email protected]de246f52009-02-25 18:25:451339#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291340
[email protected]71f65dd2009-02-11 19:14:561341void AutomationProvider::ExecuteBrowserCommand(int handle, int command,
1342 bool* success) {
1343 *success = false;
[email protected]4ae62752008-08-04 23:28:471344 if (browser_tracker_->ContainsHandle(handle)) {
1345 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:141346 if (browser->command_updater()->SupportsCommand(command) &&
1347 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:471348 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:561349 *success = true;
[email protected]4ae62752008-08-04 23:28:471350 }
1351 }
[email protected]4ae62752008-08-04 23:28:471352}
1353
[email protected]56e71b7c2009-03-27 03:05:561354void AutomationProvider::ExecuteBrowserCommandWithNotification(
1355 int handle, int command, IPC::Message* reply_message) {
1356 if (browser_tracker_->ContainsHandle(handle)) {
1357 Browser* browser = browser_tracker_->GetResource(handle);
1358 if (browser->command_updater()->SupportsCommand(command) &&
1359 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4e41709d2009-04-08 00:04:271360 ExecuteBrowserCommandObserver* observer =
1361 new ExecuteBrowserCommandObserver(this, reply_message);
1362 if (observer->Register(command))
1363 browser->ExecuteCommand(command);
1364 else
1365 delete observer;
[email protected]56e71b7c2009-03-27 03:05:561366 return;
1367 }
1368 }
[email protected]49a14a82009-03-31 04:16:441369 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:561370 Send(reply_message);
1371}
1372
[email protected]71f65dd2009-02-11 19:14:561373void AutomationProvider::WindowGetViewBounds(int handle, int view_id,
1374 bool screen_coordinates,
1375 bool* success,
1376 gfx::Rect* bounds) {
1377 *success = false;
initial.commit09911bf2008-07-26 23:55:291378
[email protected]de246f52009-02-25 18:25:451379#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291380 void* iter = NULL;
1381 if (window_tracker_->ContainsHandle(handle)) {
1382 HWND hwnd = window_tracker_->GetResource(handle);
[email protected]a0dde122008-11-21 20:51:201383 views::RootView* root_view = views::WidgetWin::FindRootView(hwnd);
initial.commit09911bf2008-07-26 23:55:291384 if (root_view) {
[email protected]c2dacc92008-10-16 23:51:381385 views::View* view = root_view->GetViewByID(view_id);
initial.commit09911bf2008-07-26 23:55:291386 if (view) {
[email protected]71f65dd2009-02-11 19:14:561387 *success = true;
[email protected]96b667d2008-10-14 20:58:441388 gfx::Point point;
initial.commit09911bf2008-07-26 23:55:291389 if (screen_coordinates)
[email protected]c2dacc92008-10-16 23:51:381390 views::View::ConvertPointToScreen(view, &point);
initial.commit09911bf2008-07-26 23:55:291391 else
[email protected]c2dacc92008-10-16 23:51:381392 views::View::ConvertPointToView(view, root_view, &point);
[email protected]71f65dd2009-02-11 19:14:561393 *bounds = view->GetLocalBounds(false);
1394 bounds->set_origin(point);
initial.commit09911bf2008-07-26 23:55:291395 }
1396 }
1397 }
[email protected]de246f52009-02-25 18:25:451398#else
1399 // TODO(port): Enable when window_tracker is ported.
1400 NOTIMPLEMENTED();
1401#endif
initial.commit09911bf2008-07-26 23:55:291402}
1403
[email protected]de246f52009-02-25 18:25:451404#if defined(OS_WIN)
1405// TODO(port): Use portable replacement for POINT.
1406
initial.commit09911bf2008-07-26 23:55:291407// This task enqueues a mouse event on the event loop, so that the view
1408// that it's being sent to can do the requisite post-processing.
1409class MouseEventTask : public Task {
1410 public:
[email protected]c2dacc92008-10-16 23:51:381411 MouseEventTask(views::View* view,
1412 views::Event::EventType type,
initial.commit09911bf2008-07-26 23:55:291413 POINT point,
1414 int flags)
1415 : view_(view), type_(type), point_(point), flags_(flags) {}
1416 virtual ~MouseEventTask() {}
1417
1418 virtual void Run() {
[email protected]c2dacc92008-10-16 23:51:381419 views::MouseEvent event(type_, point_.x, point_.y, flags_);
initial.commit09911bf2008-07-26 23:55:291420 // We need to set the cursor position before we process the event because
1421 // some code (tab dragging, for instance) queries the actual cursor location
1422 // rather than the location of the mouse event. Note that the reason why
1423 // the drag code moved away from using mouse event locations was because
1424 // our conversion to screen location doesn't work well with multiple
1425 // monitors, so this only works reliably in a single monitor setup.
[email protected]96b667d2008-10-14 20:58:441426 gfx::Point screen_location(point_.x, point_.y);
initial.commit09911bf2008-07-26 23:55:291427 view_->ConvertPointToScreen(view_, &screen_location);
[email protected]96b667d2008-10-14 20:58:441428 ::SetCursorPos(screen_location.x(), screen_location.y());
initial.commit09911bf2008-07-26 23:55:291429 switch (type_) {
[email protected]c2dacc92008-10-16 23:51:381430 case views::Event::ET_MOUSE_PRESSED:
initial.commit09911bf2008-07-26 23:55:291431 view_->OnMousePressed(event);
1432 break;
1433
[email protected]c2dacc92008-10-16 23:51:381434 case views::Event::ET_MOUSE_DRAGGED:
initial.commit09911bf2008-07-26 23:55:291435 view_->OnMouseDragged(event);
1436 break;
1437
[email protected]c2dacc92008-10-16 23:51:381438 case views::Event::ET_MOUSE_RELEASED:
initial.commit09911bf2008-07-26 23:55:291439 view_->OnMouseReleased(event, false);
1440 break;
1441
1442 default:
1443 NOTREACHED();
1444 }
1445 }
1446
1447 private:
[email protected]c2dacc92008-10-16 23:51:381448 views::View* view_;
1449 views::Event::EventType type_;
initial.commit09911bf2008-07-26 23:55:291450 POINT point_;
1451 int flags_;
1452
[email protected]5a52f162008-08-27 04:15:311453 DISALLOW_COPY_AND_ASSIGN(MouseEventTask);
initial.commit09911bf2008-07-26 23:55:291454};
1455
[email protected]c2dacc92008-10-16 23:51:381456void AutomationProvider::ScheduleMouseEvent(views::View* view,
1457 views::Event::EventType type,
initial.commit09911bf2008-07-26 23:55:291458 POINT point,
1459 int flags) {
1460 MessageLoop::current()->PostTask(FROM_HERE,
1461 new MouseEventTask(view, type, point, flags));
1462}
[email protected]de246f52009-02-25 18:25:451463#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291464
1465// This task just adds another task to the event queue. This is useful if
1466// you want to ensure that any tasks added to the event queue after this one
1467// have already been processed by the time |task| is run.
1468class InvokeTaskLaterTask : public Task {
1469 public:
1470 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
1471 virtual ~InvokeTaskLaterTask() {}
1472
1473 virtual void Run() {
1474 MessageLoop::current()->PostTask(FROM_HERE, task_);
1475 }
1476
1477 private:
1478 Task* task_;
1479
[email protected]5a52f162008-08-27 04:15:311480 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
initial.commit09911bf2008-07-26 23:55:291481};
1482
[email protected]de246f52009-02-25 18:25:451483#if defined(OS_WIN)
1484// TODO(port): Replace POINT and other windowsisms.
1485
initial.commit09911bf2008-07-26 23:55:291486// This task sends a WindowDragResponse message with the appropriate
1487// routing ID to the automation proxy. This is implemented as a task so that
1488// we know that the mouse events (and any tasks that they spawn on the message
1489// loop) have been processed by the time this is sent.
1490class WindowDragResponseTask : public Task {
1491 public:
[email protected]71f65dd2009-02-11 19:14:561492 WindowDragResponseTask(AutomationProvider* provider, int routing_id,
1493 IPC::Message* reply_message)
1494 : provider_(provider), routing_id_(routing_id),
1495 reply_message_(reply_message) {}
initial.commit09911bf2008-07-26 23:55:291496 virtual ~WindowDragResponseTask() {}
1497
1498 virtual void Run() {
[email protected]71f65dd2009-02-11 19:14:561499 DCHECK(reply_message_ != NULL);
1500 AutomationMsg_WindowDrag::WriteReplyParams(reply_message_, true);
1501 provider_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:291502 }
1503
1504 private:
1505 AutomationProvider* provider_;
1506 int routing_id_;
[email protected]71f65dd2009-02-11 19:14:561507 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:291508
[email protected]5a52f162008-08-27 04:15:311509 DISALLOW_COPY_AND_ASSIGN(WindowDragResponseTask);
initial.commit09911bf2008-07-26 23:55:291510};
1511
1512void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
1513 int handle,
1514 POINT click,
1515 int flags) {
1516 HWND hwnd = 0;
1517
1518 if (window_tracker_->ContainsHandle(handle)) {
1519 hwnd = window_tracker_->GetResource(handle);
1520
initial.commit09911bf2008-07-26 23:55:291521 ui_controls::SendMouseMove(click.x, click.y);
1522
1523 ui_controls::MouseButton button = ui_controls::LEFT;
[email protected]c2dacc92008-10-16 23:51:381524 if ((flags & views::Event::EF_LEFT_BUTTON_DOWN) ==
1525 views::Event::EF_LEFT_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291526 button = ui_controls::LEFT;
[email protected]c2dacc92008-10-16 23:51:381527 } else if ((flags & views::Event::EF_RIGHT_BUTTON_DOWN) ==
1528 views::Event::EF_RIGHT_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291529 button = ui_controls::RIGHT;
[email protected]c2dacc92008-10-16 23:51:381530 } else if ((flags & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
1531 views::Event::EF_MIDDLE_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291532 button = ui_controls::MIDDLE;
1533 } else {
1534 NOTREACHED();
1535 }
1536 ui_controls::SendMouseClick(button);
1537 }
1538}
1539
[email protected]71f65dd2009-02-11 19:14:561540void AutomationProvider::WindowSimulateDrag(int handle,
[email protected]fe92e4e2008-11-18 21:31:321541 std::vector<POINT> drag_path,
[email protected]5e0f30c2008-08-14 22:52:441542 int flags,
[email protected]71f65dd2009-02-11 19:14:561543 bool press_escape_en_route,
1544 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291545 bool succeeded = false;
[email protected]fe92e4e2008-11-18 21:31:321546 if (browser_tracker_->ContainsHandle(handle) && (drag_path.size() > 1)) {
1547 succeeded = true;
1548
1549 UINT down_message = 0;
1550 UINT up_message = 0;
1551 WPARAM wparam_flags = 0;
1552 if (flags & views::Event::EF_SHIFT_DOWN)
1553 wparam_flags |= MK_SHIFT;
1554 if (flags & views::Event::EF_CONTROL_DOWN)
1555 wparam_flags |= MK_CONTROL;
1556 if (flags & views::Event::EF_LEFT_BUTTON_DOWN) {
1557 wparam_flags |= MK_LBUTTON;
1558 down_message = WM_LBUTTONDOWN;
1559 up_message = WM_LBUTTONUP;
1560 }
1561 if (flags & views::Event::EF_MIDDLE_BUTTON_DOWN) {
1562 wparam_flags |= MK_MBUTTON;
1563 down_message = WM_MBUTTONDOWN;
1564 up_message = WM_MBUTTONUP;
1565 }
1566 if (flags & views::Event::EF_RIGHT_BUTTON_DOWN) {
1567 wparam_flags |= MK_RBUTTON;
1568 down_message = WM_LBUTTONDOWN;
1569 up_message = WM_LBUTTONUP;
1570 }
1571
initial.commit09911bf2008-07-26 23:55:291572 Browser* browser = browser_tracker_->GetResource(handle);
1573 DCHECK(browser);
[email protected]0a6fb3f2008-11-18 21:39:551574 HWND top_level_hwnd =
1575 reinterpret_cast<HWND>(browser->window()->GetNativeHandle());
[email protected]fe92e4e2008-11-18 21:31:321576 POINT temp = drag_path[0];
1577 MapWindowPoints(top_level_hwnd, HWND_DESKTOP, &temp, 1);
1578 SetCursorPos(temp.x, temp.y);
1579 SendMessage(top_level_hwnd, down_message, wparam_flags,
1580 MAKELPARAM(drag_path[0].x, drag_path[0].y));
1581 for (int i = 1; i < static_cast<int>(drag_path.size()); ++i) {
1582 temp = drag_path[i];
1583 MapWindowPoints(top_level_hwnd, HWND_DESKTOP, &temp, 1);
1584 SetCursorPos(temp.x, temp.y);
1585 SendMessage(top_level_hwnd, WM_MOUSEMOVE, wparam_flags,
1586 MAKELPARAM(drag_path[i].x, drag_path[i].y));
[email protected]f7a391a12008-11-10 21:29:341587 }
[email protected]fe92e4e2008-11-18 21:31:321588 POINT end = drag_path[drag_path.size() - 1];
1589 MapWindowPoints(top_level_hwnd, HWND_DESKTOP, &end, 1);
1590 SetCursorPos(end.x, end.y);
1591
1592 if (press_escape_en_route) {
1593 // Press Escape.
1594 ui_controls::SendKeyPress(VK_ESCAPE,
1595 ((flags & views::Event::EF_CONTROL_DOWN)
1596 == views::Event::EF_CONTROL_DOWN),
1597 ((flags & views::Event::EF_SHIFT_DOWN) ==
1598 views::Event::EF_SHIFT_DOWN),
1599 ((flags & views::Event::EF_ALT_DOWN) ==
1600 views::Event::EF_ALT_DOWN));
1601 }
1602 SendMessage(top_level_hwnd, up_message, wparam_flags,
1603 MAKELPARAM(end.x, end.y));
1604
initial.commit09911bf2008-07-26 23:55:291605 MessageLoop::current()->PostTask(FROM_HERE,
1606 new InvokeTaskLaterTask(
[email protected]71f65dd2009-02-11 19:14:561607 new WindowDragResponseTask(this, reply_message->routing_id(),
1608 reply_message)));
initial.commit09911bf2008-07-26 23:55:291609 } else {
[email protected]71f65dd2009-02-11 19:14:561610 AutomationMsg_WindowDrag::WriteReplyParams(reply_message, true);
1611 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291612 }
1613}
1614
1615void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
1616 int handle,
1617 wchar_t key,
1618 int flags) {
1619 if (!window_tracker_->ContainsHandle(handle))
1620 return;
1621
1622 // The key event is sent to whatever window is active.
1623 ui_controls::SendKeyPress(key,
[email protected]c2dacc92008-10-16 23:51:381624 ((flags & views::Event::EF_CONTROL_DOWN) ==
1625 views::Event::EF_CONTROL_DOWN),
1626 ((flags & views::Event::EF_SHIFT_DOWN) ==
1627 views::Event::EF_SHIFT_DOWN),
1628 ((flags & views::Event::EF_ALT_DOWN) ==
1629 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:291630}
1631
[email protected]71f65dd2009-02-11 19:14:561632void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
1633 *view_id = -1;
initial.commit09911bf2008-07-26 23:55:291634 if (window_tracker_->ContainsHandle(handle)) {
1635 HWND hwnd = window_tracker_->GetResource(handle);
[email protected]c2dacc92008-10-16 23:51:381636 views::FocusManager* focus_manager =
1637 views::FocusManager::GetFocusManager(hwnd);
initial.commit09911bf2008-07-26 23:55:291638 DCHECK(focus_manager);
[email protected]c2dacc92008-10-16 23:51:381639 views::View* focused_view = focus_manager->GetFocusedView();
initial.commit09911bf2008-07-26 23:55:291640 if (focused_view)
[email protected]71f65dd2009-02-11 19:14:561641 *view_id = focused_view->GetID();
initial.commit09911bf2008-07-26 23:55:291642 }
initial.commit09911bf2008-07-26 23:55:291643}
1644
[email protected]71f65dd2009-02-11 19:14:561645void AutomationProvider::SetWindowVisible(int handle, bool visible,
1646 bool* result) {
initial.commit09911bf2008-07-26 23:55:291647 if (window_tracker_->ContainsHandle(handle)) {
1648 HWND hwnd = window_tracker_->GetResource(handle);
1649 ::ShowWindow(hwnd, visible ? SW_SHOW : SW_HIDE);
[email protected]71f65dd2009-02-11 19:14:561650 *result = true;
initial.commit09911bf2008-07-26 23:55:291651 } else {
[email protected]71f65dd2009-02-11 19:14:561652 *result = false;
initial.commit09911bf2008-07-26 23:55:291653 }
1654}
1655
[email protected]71f65dd2009-02-11 19:14:561656void AutomationProvider::IsWindowActive(int handle, bool* success,
1657 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291658 if (window_tracker_->ContainsHandle(handle)) {
1659 HWND hwnd = window_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561660 *is_active = ::GetForegroundWindow() == hwnd;
1661 *success = true;
initial.commit09911bf2008-07-26 23:55:291662 } else {
[email protected]71f65dd2009-02-11 19:14:561663 *success = false;
1664 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291665 }
1666}
1667
[email protected]659d0e82009-02-13 22:43:281668void AutomationProvider::ActivateWindow(int handle) {
initial.commit09911bf2008-07-26 23:55:291669 if (window_tracker_->ContainsHandle(handle)) {
1670 ::SetActiveWindow(window_tracker_->GetResource(handle));
1671 }
1672}
[email protected]de246f52009-02-25 18:25:451673#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291674
[email protected]71f65dd2009-02-11 19:14:561675void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1676 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291677
1678 if (browser_tracker_->ContainsHandle(handle)) {
1679 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561680 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291681 }
initial.commit09911bf2008-07-26 23:55:291682}
1683
[email protected]71f65dd2009-02-11 19:14:561684void AutomationProvider::GetTab(int win_handle, int tab_index,
1685 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561686 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291687 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1688 Browser* browser = browser_tracker_->GetResource(win_handle);
1689 if (tab_index < browser->tab_count()) {
1690 TabContents* tab_contents =
1691 browser->GetTabContentsAt(tab_index);
[email protected]71f65dd2009-02-11 19:14:561692 *tab_handle = tab_tracker_->Add(tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291693 }
1694 }
initial.commit09911bf2008-07-26 23:55:291695}
1696
[email protected]71f65dd2009-02-11 19:14:561697void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1698 std::wstring* title) {
1699 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291700 if (tab_tracker_->ContainsHandle(handle)) {
1701 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]4c4d8d22009-03-04 05:29:271702 *title = UTF16ToWideHack(tab->GetActiveEntry()->title());
[email protected]71f65dd2009-02-11 19:14:561703 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291704 }
initial.commit09911bf2008-07-26 23:55:291705}
1706
1707void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
initial.commit09911bf2008-07-26 23:55:291708 if (window_tracker_->ContainsHandle(handle)) {
1709 window_tracker_->Remove(window_tracker_->GetResource(handle));
1710 }
initial.commit09911bf2008-07-26 23:55:291711}
1712
1713void AutomationProvider::OnChannelError() {
1714 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571715 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291716}
1717
1718// TODO(brettw) change this to accept GURLs when history supports it
1719void AutomationProvider::OnRedirectQueryComplete(
1720 HistoryService::Handle request_handle,
1721 GURL from_url,
1722 bool success,
1723 HistoryService::RedirectList* redirects) {
1724 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561725 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291726
[email protected]deb57402009-02-06 01:35:301727 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:291728 if (success) {
[email protected]71f65dd2009-02-11 19:14:561729 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:291730 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301731 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291732 } else {
[email protected]71f65dd2009-02-11 19:14:561733 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:291734 }
1735
[email protected]4f3dc372009-02-24 00:10:291736 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301737
[email protected]71f65dd2009-02-11 19:14:561738 Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:291739 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561740 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291741}
1742
1743bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571744 DCHECK(channel_.get());
1745 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291746}
1747
1748Browser* AutomationProvider::FindAndActivateTab(
1749 NavigationController* controller) {
1750 int tab_index;
1751 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1752 if (browser)
1753 browser->SelectTabContentsAt(tab_index, true);
1754
1755 return browser;
1756}
1757
[email protected]71f65dd2009-02-11 19:14:561758void AutomationProvider::GetCookies(const GURL& url, int handle,
1759 int* value_size,
1760 std::string* value) {
1761 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291762 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1763 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561764 *value =
initial.commit09911bf2008-07-26 23:55:291765 tab->profile()->GetRequestContext()->cookie_store()->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561766 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291767 }
initial.commit09911bf2008-07-26 23:55:291768}
1769
[email protected]71f65dd2009-02-11 19:14:561770void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291771 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561772 int handle,
1773 int* response_value) {
1774 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291775
1776 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1777 NavigationController* tab = tab_tracker_->GetResource(handle);
1778 URLRequestContext* context = tab->profile()->GetRequestContext();
1779 if (context->cookie_store()->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561780 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291781 }
initial.commit09911bf2008-07-26 23:55:291782}
1783
[email protected]71f65dd2009-02-11 19:14:561784void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1785 *success = false;
initial.commit09911bf2008-07-26 23:55:291786 if (tab_tracker_->ContainsHandle(handle)) {
1787 NavigationController* tab = tab_tracker_->GetResource(handle);
1788 // Return what the user would see in the location bar.
[email protected]71f65dd2009-02-11 19:14:561789 *url = tab->GetActiveEntry()->display_url();
1790 *success = true;
initial.commit09911bf2008-07-26 23:55:291791 }
initial.commit09911bf2008-07-26 23:55:291792}
1793
[email protected]de246f52009-02-25 18:25:451794#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:561795void AutomationProvider::GetTabHWND(int handle, HWND* tab_hwnd) {
1796 *tab_hwnd = NULL;
initial.commit09911bf2008-07-26 23:55:291797
1798 if (tab_tracker_->ContainsHandle(handle)) {
1799 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561800 *tab_hwnd = tab->active_contents()->GetNativeView();
initial.commit09911bf2008-07-26 23:55:291801 }
initial.commit09911bf2008-07-26 23:55:291802}
[email protected]de246f52009-02-25 18:25:451803#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291804
[email protected]71f65dd2009-02-11 19:14:561805void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1806 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291807
1808 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561809 *process_id = 0;
initial.commit09911bf2008-07-26 23:55:291810 NavigationController* tab = tab_tracker_->GetResource(handle);
1811 if (tab->active_contents()->AsWebContents()) {
1812 WebContents* web_contents = tab->active_contents()->AsWebContents();
[email protected]2f15de42008-11-11 22:35:191813 if (web_contents->process())
[email protected]71f65dd2009-02-11 19:14:561814 *process_id = web_contents->process()->process().pid();
initial.commit09911bf2008-07-26 23:55:291815 }
1816 }
initial.commit09911bf2008-07-26 23:55:291817}
1818
1819void AutomationProvider::ApplyAccelerator(int handle, int id) {
1820 if (browser_tracker_->ContainsHandle(handle)) {
1821 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:141822 browser->ExecuteCommand(id);
initial.commit09911bf2008-07-26 23:55:291823 }
1824}
1825
[email protected]71f65dd2009-02-11 19:14:561826void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291827 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561828 const std::wstring& script,
1829 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291830 bool succeeded = false;
[email protected]20e93d12008-08-28 16:31:571831 WebContents* web_contents = GetWebContentsForHandle(handle, NULL);
1832 if (web_contents) {
1833 // Set the routing id of this message with the controller.
1834 // This routing id needs to be remembered for the reverse
1835 // communication while sending back the response of
1836 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331837 std::wstring set_automation_id;
1838 SStringPrintf(&set_automation_id,
1839 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561840 reply_message->routing_id());
1841
1842 DCHECK(reply_message_ == NULL);
1843 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291844
[email protected]1f5af4442008-09-25 22:11:061845 web_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331846 frame_xpath, set_automation_id);
[email protected]1f5af4442008-09-25 22:11:061847 web_contents->render_view_host()->ExecuteJavascriptInWebFrame(
1848 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571849 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291850 }
1851
1852 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561853 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1854 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291855 }
1856}
1857
[email protected]71f65dd2009-02-11 19:14:561858void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1859 *visible = false;
[email protected]20e93d12008-08-28 16:31:571860
[email protected]de246f52009-02-25 18:25:451861#if defined(OS_WIN)
[email protected]20e93d12008-08-28 16:31:571862 WebContents* web_contents = GetWebContentsForHandle(handle, NULL);
1863 if (web_contents)
[email protected]71f65dd2009-02-11 19:14:561864 *visible = web_contents->IsDownloadShelfVisible();
[email protected]de246f52009-02-25 18:25:451865#else
1866 // TODO(port): Enable when web_contents->IsDownloadShelfVisible is ported.
1867 NOTIMPLEMENTED();
1868#endif
initial.commit09911bf2008-07-26 23:55:291869}
1870
[email protected]71f65dd2009-02-11 19:14:561871void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1872 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291873 if (tab_tracker_->ContainsHandle(handle)) {
1874 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1875 TabContents* tab_contents = nav_controller->active_contents();
1876 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561877 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291878 }
1879 }
initial.commit09911bf2008-07-26 23:55:291880}
1881
[email protected]71f65dd2009-02-11 19:14:561882void AutomationProvider::GetConstrainedWindow(int handle, int index,
1883 int* cwindow_handle) {
1884 *cwindow_handle = 0;
initial.commit09911bf2008-07-26 23:55:291885 if (tab_tracker_->ContainsHandle(handle) && index >= 0) {
1886 NavigationController* nav_controller =
1887 tab_tracker_->GetResource(handle);
1888 TabContents* tab = nav_controller->active_contents();
[email protected]d5f942ba2008-09-26 19:30:341889 if (tab && index < static_cast<int>(tab->child_windows_.size())) {
[email protected]de246f52009-02-25 18:25:451890#if defined(OS_WIN)
[email protected]d5f942ba2008-09-26 19:30:341891 ConstrainedWindow* window = tab->child_windows_[index];
[email protected]71f65dd2009-02-11 19:14:561892 *cwindow_handle = cwindow_tracker_->Add(window);
[email protected]de246f52009-02-25 18:25:451893#else
1894 // TODO(port): Enable when cwindow_tracker is ported.
1895 NOTIMPLEMENTED();
1896#endif
initial.commit09911bf2008-07-26 23:55:291897 }
1898 }
initial.commit09911bf2008-07-26 23:55:291899}
1900
[email protected]71f65dd2009-02-11 19:14:561901void AutomationProvider::GetConstrainedTitle(int handle,
1902 int* title_string_size,
1903 std::wstring* title) {
1904 *title_string_size = -1; // -1 is the error code
[email protected]de246f52009-02-25 18:25:451905#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291906 if (cwindow_tracker_->ContainsHandle(handle)) {
1907 ConstrainedWindow* window = cwindow_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561908 *title = window->GetWindowTitle();
1909 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291910 }
[email protected]de246f52009-02-25 18:25:451911#else
1912 // TODO(port): Enable when cwindow_tracker is ported.
1913 NOTIMPLEMENTED();
1914#endif
initial.commit09911bf2008-07-26 23:55:291915}
1916
[email protected]71f65dd2009-02-11 19:14:561917void AutomationProvider::GetConstrainedWindowBounds(int handle, bool* exists,
1918 gfx::Rect* rect) {
1919 *rect = gfx::Rect(0, 0, 0, 0);
[email protected]de246f52009-02-25 18:25:451920#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291921 if (cwindow_tracker_->ContainsHandle(handle)) {
1922 ConstrainedWindow* window = cwindow_tracker_->GetResource(handle);
1923 if (window) {
[email protected]71f65dd2009-02-11 19:14:561924 *exists = true;
1925 *rect = window->GetCurrentBounds();
initial.commit09911bf2008-07-26 23:55:291926 }
1927 }
[email protected]de246f52009-02-25 18:25:451928#else
1929 // TODO(port): Enable when cwindow_tracker is ported.
1930 NOTIMPLEMENTED();
1931#endif
initial.commit09911bf2008-07-26 23:55:291932}
1933
1934void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:561935 int handle, const std::wstring& find_request,
1936 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:311937 NOTREACHED() << "This function has been deprecated."
1938 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:561939 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:311940 return;
1941}
1942
[email protected]4f999132009-03-31 18:08:401943void AutomationProvider::HandleFindRequest(
1944 int handle,
1945 const AutomationMsg_Find_Params& params,
1946 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291947 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561948 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
1949 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291950 return;
1951 }
1952
1953 NavigationController* nav = tab_tracker_->GetResource(handle);
1954 TabContents* tab_contents = nav->active_contents();
1955
1956 find_in_page_observer_.reset(new
[email protected]71f65dd2009-02-11 19:14:561957 FindInPageNotificationObserver(this, tab_contents,
1958 reply_message->routing_id(),
1959 reply_message));
initial.commit09911bf2008-07-26 23:55:291960
[email protected]9e0534b2008-10-21 15:03:011961 WebContents* web_contents = tab_contents->AsWebContents();
1962 if (web_contents) {
[email protected]4f3dc372009-02-24 00:10:291963 web_contents->set_current_find_request_id(
1964 FindInPageNotificationObserver::kFindInPageRequestId);
[email protected]9e0534b2008-10-21 15:03:011965 web_contents->render_view_host()->StartFinding(
1966 FindInPageNotificationObserver::kFindInPageRequestId,
[email protected]4f999132009-03-31 18:08:401967 params.search_string, params.forward, params.match_case,
1968 params.find_next);
[email protected]9e0534b2008-10-21 15:03:011969 }
initial.commit09911bf2008-07-26 23:55:291970}
1971
[email protected]5f8af2a2008-08-06 22:49:451972void AutomationProvider::HandleOpenFindInPageRequest(
1973 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:291974 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]de246f52009-02-25 18:25:451975#if defined(OS_WIN)
[email protected]4f3dc372009-02-24 00:10:291976 Browser* browser = browser_tracker_->GetResource(handle);
1977 browser->FindInPage(false, false);
[email protected]de246f52009-02-25 18:25:451978#else
1979 // TODO(port): Enable when Browser::FindInPage is ported.
1980 NOTIMPLEMENTED();
1981#endif
[email protected]5f8af2a2008-08-06 22:49:451982 }
1983}
1984
[email protected]71f65dd2009-02-11 19:14:561985void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]9e0534b2008-10-21 15:03:011986 gfx::Point position;
[email protected]71f65dd2009-02-11 19:14:561987 *visible = false;
[email protected]4f3dc372009-02-24 00:10:291988 if (browser_tracker_->ContainsHandle(handle)) {
[email protected]4f3dc372009-02-24 00:10:291989 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:581990 FindBarTesting* find_bar =
1991 browser->find_bar()->find_bar()->GetFindBarTesting();
1992 find_bar->GetFindBarWindowInfo(&position, visible);
[email protected]4f3dc372009-02-24 00:10:291993 }
[email protected]20e93d12008-08-28 16:31:571994}
1995
[email protected]71f65dd2009-02-11 19:14:561996void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
1997 int* y) {
[email protected]9e0534b2008-10-21 15:03:011998 gfx::Point position(0, 0);
[email protected]9e0534b2008-10-21 15:03:011999 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:292000 if (browser_tracker_->ContainsHandle(handle)) {
2001 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:582002 FindBarTesting* find_bar =
2003 browser->find_bar()->find_bar()->GetFindBarTesting();
2004 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:292005 }
[email protected]20e93d12008-08-28 16:31:572006
[email protected]71f65dd2009-02-11 19:14:562007 *x = position.x();
2008 *y = position.y();
[email protected]20e93d12008-08-28 16:31:572009}
2010
[email protected]71f65dd2009-02-11 19:14:562011void AutomationProvider::GetBookmarkBarVisibility(int handle, bool* visible,
2012 bool* animating) {
2013 *visible = false;
2014 *animating = false;
[email protected]c2cbeb92008-09-05 21:36:572015
[email protected]de246f52009-02-25 18:25:452016#if defined(OS_WIN)
[email protected]c2cbeb92008-09-05 21:36:572017 if (browser_tracker_->ContainsHandle(handle)) {
2018 Browser* browser = browser_tracker_->GetResource(handle);
2019 if (browser) {
[email protected]0ba1f5302009-01-22 01:34:522020 BrowserWindowTesting* testing =
2021 browser->window()->GetBrowserWindowTesting();
2022 BookmarkBarView* bookmark_bar = testing->GetBookmarkBarView();
[email protected]c2cbeb92008-09-05 21:36:572023 if (bookmark_bar) {
[email protected]71f65dd2009-02-11 19:14:562024 *animating = bookmark_bar->IsAnimating();
2025 *visible = browser->window()->IsBookmarkBarVisible();
[email protected]c2cbeb92008-09-05 21:36:572026 }
2027 }
2028 }
[email protected]de246f52009-02-25 18:25:452029#else
2030 // TODO(port): Enable when bookmarks ui is ported.
2031 NOTIMPLEMENTED();
2032#endif
[email protected]c2cbeb92008-09-05 21:36:572033}
2034
initial.commit09911bf2008-07-26 23:55:292035void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562036 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:572037 WebContents* web_contents = GetWebContentsForHandle(handle, NULL);
2038 if (web_contents) {
[email protected]71f65dd2009-02-11 19:14:562039 DCHECK(reply_message_ == NULL);
2040 reply_message_ = reply_message;
2041
[email protected]1f5af4442008-09-25 22:11:062042 web_contents->render_view_host()->InspectElementAt(x, y);
[email protected]71f65dd2009-02-11 19:14:562043 inspect_element_routing_id_ = reply_message->routing_id();
initial.commit09911bf2008-07-26 23:55:292044 } else {
[email protected]71f65dd2009-02-11 19:14:562045 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2046 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292047 }
2048}
2049
2050void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092051 if (reply_message_) {
2052 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2053 num_resources);
2054 Send(reply_message_);
2055 reply_message_ = NULL;
2056 }
initial.commit09911bf2008-07-26 23:55:292057}
2058
2059// Helper class for making changes to the URLRequest ProtocolFactory on the
2060// IO thread.
2061class SetFilteredInetTask : public Task {
2062 public:
2063 explicit SetFilteredInetTask(bool enabled) : enabled_(enabled) { }
2064 virtual void Run() {
2065 if (enabled_) {
2066 URLRequestFilter::GetInstance()->ClearHandlers();
2067
2068 URLRequestFailedDnsJob::AddUITestUrls();
2069 URLRequestSlowDownloadJob::AddUITestUrls();
2070
2071 std::wstring root_http;
2072 PathService::Get(chrome::DIR_TEST_DATA, &root_http);
2073 URLRequestMockHTTPJob::AddUITestUrls(root_http);
2074 } else {
2075 // Revert to the default handlers.
2076 URLRequestFilter::GetInstance()->ClearHandlers();
2077 }
2078 }
2079 private:
2080 bool enabled_;
2081};
2082
2083void AutomationProvider::SetFilteredInet(const IPC::Message& message,
2084 bool enabled) {
2085 // Since this involves changing the URLRequest ProtocolFactory, we want to
2086 // run on the main thread.
2087 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
2088 new SetFilteredInetTask(enabled));
2089}
2090
[email protected]4f3dc372009-02-24 00:10:292091void AutomationProvider::GetDownloadDirectory(
2092 int handle, std::wstring* download_directory) {
initial.commit09911bf2008-07-26 23:55:292093 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292094 if (tab_tracker_->ContainsHandle(handle)) {
2095 NavigationController* tab = tab_tracker_->GetResource(handle);
2096 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2097 DCHECK(dlm);
[email protected]71f65dd2009-02-11 19:14:562098 *download_directory = dlm->download_path().ToWStringHack();
initial.commit09911bf2008-07-26 23:55:292099 }
initial.commit09911bf2008-07-26 23:55:292100}
2101
[email protected]de246f52009-02-25 18:25:452102#if defined(OS_WIN)
2103// TODO(port): Remove windowsisms.
initial.commit09911bf2008-07-26 23:55:292104void AutomationProvider::OpenNewBrowserWindow(int show_command) {
2105 // We may have no current browser windows open so don't rely on
2106 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]15952e462008-11-14 00:29:052107 Browser* browser = Browser::Create(profile_);
2108 browser->AddBlankTab(true);
2109 if (show_command != SW_HIDE)
2110 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292111}
[email protected]0e9f4ee2009-04-08 01:44:202112#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292113
[email protected]71f65dd2009-02-11 19:14:562114void AutomationProvider::GetWindowForBrowser(int browser_handle,
2115 bool* success,
2116 int* handle) {
2117 *success = false;
2118 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292119
2120 if (browser_tracker_->ContainsHandle(browser_handle)) {
2121 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202122 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292123 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202124 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562125 *success = true;
initial.commit09911bf2008-07-26 23:55:292126 }
initial.commit09911bf2008-07-26 23:55:292127}
2128
[email protected]0e9f4ee2009-04-08 01:44:202129#if defined(OS_WIN)
2130// TODO(port): Remove windowsisms.
initial.commit09911bf2008-07-26 23:55:292131void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562132 int browser_handle,
2133 bool* success,
2134 int* autocomplete_edit_handle) {
2135 *success = false;
2136 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292137
2138 if (browser_tracker_->ContainsHandle(browser_handle)) {
2139 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]7745b822009-01-27 20:15:352140 BrowserWindowTesting* testing_interface =
2141 browser->window()->GetBrowserWindowTesting();
2142 LocationBarView* loc_bar_view = testing_interface->GetLocationBarView();
[email protected]81c21222008-09-10 19:35:522143 AutocompleteEditView* edit_view = loc_bar_view->location_entry();
initial.commit09911bf2008-07-26 23:55:292144 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562145 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2146 *success = true;
initial.commit09911bf2008-07-26 23:55:292147 }
initial.commit09911bf2008-07-26 23:55:292148}
2149
[email protected]71f65dd2009-02-11 19:14:562150void AutomationProvider::GetBrowserForWindow(int window_handle,
2151 bool* success,
2152 int* browser_handle) {
2153 *success = false;
2154 *browser_handle = 0;
initial.commit09911bf2008-07-26 23:55:292155
2156 if (window_tracker_->ContainsHandle(window_handle)) {
2157 HWND window = window_tracker_->GetResource(window_handle);
2158 BrowserList::const_iterator iter = BrowserList::begin();
2159 Browser* browser = NULL;
2160 for (;iter != BrowserList::end(); ++iter) {
[email protected]2d46c842008-11-14 19:24:312161 HWND hwnd = reinterpret_cast<HWND>((*iter)->window()->GetNativeHandle());
2162 if (window == hwnd) {
initial.commit09911bf2008-07-26 23:55:292163 browser = *iter;
2164 break;
2165 }
2166 }
2167 if (browser) {
2168 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562169 *browser_handle = browser_tracker_->Add(browser);
2170 *success = true;
initial.commit09911bf2008-07-26 23:55:292171 }
2172 }
initial.commit09911bf2008-07-26 23:55:292173}
[email protected]de246f52009-02-25 18:25:452174#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292175
[email protected]71f65dd2009-02-11 19:14:562176void AutomationProvider::ShowInterstitialPage(int tab_handle,
2177 const std::string& html_text,
2178 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292179 if (tab_tracker_->ContainsHandle(tab_handle)) {
2180 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2181 TabContents* tab_contents = controller->active_contents();
[email protected]965524b2009-04-04 21:32:402182
2183 AddNavigationStatusListener<bool>(controller, reply_message, true,
2184 false, false);
2185 WebContents* web_contents = tab_contents->AsWebContents();
2186 AutomationInterstitialPage* interstitial =
2187 new AutomationInterstitialPage(web_contents,
2188 GURL("about:interstitial"),
2189 html_text);
2190 interstitial->Show();
2191 return;
initial.commit09911bf2008-07-26 23:55:292192 }
[email protected]71f65dd2009-02-11 19:14:562193
2194 AutomationMsg_ShowInterstitialPage::WriteReplyParams(reply_message, false);
2195 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292196}
2197
[email protected]71f65dd2009-02-11 19:14:562198void AutomationProvider::HideInterstitialPage(int tab_handle,
2199 bool* success) {
2200 *success = false;
[email protected]20e93d12008-08-28 16:31:572201 WebContents* web_contents = GetWebContentsForHandle(tab_handle, NULL);
[email protected]a3a1d142008-12-19 00:42:302202 if (web_contents && web_contents->interstitial_page()) {
2203 web_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562204 *success = true;
initial.commit09911bf2008-07-26 23:55:292205 }
initial.commit09911bf2008-07-26 23:55:292206}
2207
[email protected]71f65dd2009-02-11 19:14:562208void AutomationProvider::CloseTab(int tab_handle,
2209 bool wait_until_closed,
2210 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292211 if (tab_tracker_->ContainsHandle(tab_handle)) {
2212 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2213 int index;
2214 Browser* browser = Browser::GetBrowserForController(controller, &index);
2215 DCHECK(browser);
[email protected]de246f52009-02-25 18:25:452216 new TabClosedNotificationObserver(browser, this,
2217 reply_message->routing_id(),
2218 wait_until_closed, reply_message);
initial.commit09911bf2008-07-26 23:55:292219 browser->CloseContents(controller->active_contents());
[email protected]de246f52009-02-25 18:25:452220 return;
initial.commit09911bf2008-07-26 23:55:292221 }
[email protected]de246f52009-02-25 18:25:452222
2223 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
initial.commit09911bf2008-07-26 23:55:292224}
2225
[email protected]71f65dd2009-02-11 19:14:562226void AutomationProvider::CloseBrowser(int browser_handle,
2227 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292228 if (browser_tracker_->ContainsHandle(browser_handle)) {
2229 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562230 new BrowserClosedNotificationObserver(browser, this,
2231 reply_message->routing_id(),
2232 reply_message);
[email protected]f3e99e32008-07-30 04:48:392233 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292234 } else {
2235 NOTREACHED();
2236 }
2237}
2238
[email protected]71f65dd2009-02-11 19:14:562239void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2240 if (browser_tracker_->ContainsHandle(browser_handle)) {
2241 Browser* browser = browser_tracker_->GetResource(browser_handle);
2242 browser->window()->Close();
2243 } else {
2244 NOTREACHED();
2245 }
2246}
2247
[email protected]de246f52009-02-25 18:25:452248#if defined(OS_WIN)
2249// TODO(port): Remove windowsisms.
[email protected]71f65dd2009-02-11 19:14:562250void AutomationProvider::CreateExternalTab(HWND parent,
[email protected]31fb110522009-01-28 21:50:392251 const gfx::Rect& dimensions,
[email protected]71f65dd2009-02-11 19:14:562252 unsigned int style,
2253 HWND* tab_container_window,
2254 int* tab_handle) {
2255 *tab_handle = 0;
2256 *tab_container_window = NULL;
initial.commit09911bf2008-07-26 23:55:292257 ExternalTabContainer *external_tab_container =
2258 new ExternalTabContainer(this);
[email protected]31fb110522009-01-28 21:50:392259 external_tab_container->Init(profile_, parent, dimensions, style);
initial.commit09911bf2008-07-26 23:55:292260 TabContents* tab_contents = external_tab_container->tab_contents();
2261 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562262 *tab_handle = tab_tracker_->Add(tab_contents->controller());
2263 *tab_container_window = *external_tab_container;
[email protected]31fb110522009-01-28 21:50:392264 } else {
2265 delete external_tab_container;
initial.commit09911bf2008-07-26 23:55:292266 }
initial.commit09911bf2008-07-26 23:55:292267}
2268
[email protected]71f65dd2009-02-11 19:14:562269void AutomationProvider::NavigateInExternalTab(
2270 int handle, const GURL& url,
2271 AutomationMsg_NavigationResponseValues* status) {
2272 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:292273
2274 if (tab_tracker_->ContainsHandle(handle)) {
2275 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c0588052008-10-27 23:01:502276 tab->LoadURL(url, GURL(), PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:562277 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:292278 }
initial.commit09911bf2008-07-26 23:55:292279}
2280
[email protected]71f65dd2009-02-11 19:14:562281void AutomationProvider::SetAcceleratorsForTab(int handle,
initial.commit09911bf2008-07-26 23:55:292282 HACCEL accel_table,
[email protected]71f65dd2009-02-11 19:14:562283 int accel_entry_count,
2284 bool* status) {
2285 *status = false;
2286
[email protected]b9d227492009-02-10 15:20:272287 ExternalTabContainer* external_tab = GetExternalTabForHandle(handle);
2288 if (external_tab) {
2289 external_tab->SetAccelerators(accel_table, accel_entry_count);
[email protected]71f65dd2009-02-11 19:14:562290 *status = true;
initial.commit09911bf2008-07-26 23:55:292291 }
initial.commit09911bf2008-07-26 23:55:292292}
2293
2294void AutomationProvider::ProcessUnhandledAccelerator(
2295 const IPC::Message& message, int handle, const MSG& msg) {
[email protected]b9d227492009-02-10 15:20:272296 ExternalTabContainer* external_tab = GetExternalTabForHandle(handle);
2297 if (external_tab) {
2298 external_tab->ProcessUnhandledAccelerator(msg);
2299 }
2300 // This message expects no response.
2301}
2302
[email protected]71f65dd2009-02-11 19:14:562303void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2304 IPC::Message* reply_message) {
2305 if (tab_tracker_->ContainsHandle(tab_handle)) {
2306 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2307 restore_tracker_.reset(
2308 new NavigationControllerRestoredObserver(this, tab,
2309 reply_message->routing_id(),
2310 reply_message));
2311 }
2312}
2313
[email protected]b9d227492009-02-10 15:20:272314void AutomationProvider::SetInitialFocus(const IPC::Message& message,
2315 int handle, bool reverse) {
2316 ExternalTabContainer* external_tab = GetExternalTabForHandle(handle);
2317 if (external_tab) {
2318 external_tab->SetInitialFocus(reverse);
initial.commit09911bf2008-07-26 23:55:292319 }
2320 // This message expects no response.
2321}
2322
[email protected]71f65dd2009-02-11 19:14:562323void AutomationProvider::GetSecurityState(int handle, bool* success,
2324 SecurityStyle* security_style,
2325 int* ssl_cert_status,
2326 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:292327 if (tab_tracker_->ContainsHandle(handle)) {
2328 NavigationController* tab = tab_tracker_->GetResource(handle);
2329 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562330 *success = true;
2331 *security_style = entry->ssl().security_style();
2332 *ssl_cert_status = entry->ssl().cert_status();
2333 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292334 } else {
[email protected]71f65dd2009-02-11 19:14:562335 *success = false;
2336 *security_style = SECURITY_STYLE_UNKNOWN;
2337 *ssl_cert_status = 0;
2338 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292339 }
2340}
2341
[email protected]71f65dd2009-02-11 19:14:562342void AutomationProvider::GetPageType(int handle, bool* success,
2343 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:292344 if (tab_tracker_->ContainsHandle(handle)) {
2345 NavigationController* tab = tab_tracker_->GetResource(handle);
2346 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562347 *page_type = entry->page_type();
2348 *success = true;
initial.commit09911bf2008-07-26 23:55:292349 // In order to return the proper result when an interstitial is shown and
2350 // no navigation entry were created for it we need to ask the WebContents.
[email protected]71f65dd2009-02-11 19:14:562351 if (*page_type == NavigationEntry::NORMAL_PAGE &&
initial.commit09911bf2008-07-26 23:55:292352 tab->active_contents()->AsWebContents() &&
[email protected]b6e09ac2008-08-12 16:11:092353 tab->active_contents()->AsWebContents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:562354 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292355 } else {
[email protected]71f65dd2009-02-11 19:14:562356 *success = false;
2357 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292358 }
2359}
2360
[email protected]71f65dd2009-02-11 19:14:562361void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
2362 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292363 if (tab_tracker_->ContainsHandle(handle)) {
2364 NavigationController* tab = tab_tracker_->GetResource(handle);
2365 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072366 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:402367 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:472368 InterstitialPage* ssl_blocking_page =
[email protected]a3a1d142008-12-19 00:42:302369 InterstitialPage::GetInterstitialPage(tab_contents->AsWebContents());
initial.commit09911bf2008-07-26 23:55:292370 if (ssl_blocking_page) {
2371 if (proceed) {
[email protected]71f65dd2009-02-11 19:14:562372 AddNavigationStatusListener<bool>(tab, reply_message, true, true,
2373 false);
2374 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:292375 return;
2376 }
2377 ssl_blocking_page->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562378 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(reply_message,
2379 true);
2380 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292381 return;
2382 }
2383 }
2384 }
2385 // We failed.
[email protected]71f65dd2009-02-11 19:14:562386 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(reply_message,
2387 false);
2388 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292389}
[email protected]de246f52009-02-25 18:25:452390#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292391
[email protected]71f65dd2009-02-11 19:14:562392void AutomationProvider::BringBrowserToFront(int browser_handle,
2393 bool* success) {
initial.commit09911bf2008-07-26 23:55:292394 if (browser_tracker_->ContainsHandle(browser_handle)) {
2395 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:062396 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:562397 *success = true;
initial.commit09911bf2008-07-26 23:55:292398 } else {
[email protected]71f65dd2009-02-11 19:14:562399 *success = false;
initial.commit09911bf2008-07-26 23:55:292400 }
2401}
2402
[email protected]71f65dd2009-02-11 19:14:562403void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
2404 int message_num,
2405 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:292406 if (browser_tracker_->ContainsHandle(browser_handle)) {
2407 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562408 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:142409 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:292410 } else {
[email protected]71f65dd2009-02-11 19:14:562411 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:292412 }
2413}
2414
[email protected]de246f52009-02-25 18:25:452415#if defined(OS_WIN)
2416// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:562417void AutomationProvider::PrintNow(int tab_handle,
2418 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:572419 NavigationController* tab = NULL;
2420 WebContents* web_contents = GetWebContentsForHandle(tab_handle, &tab);
2421 if (web_contents) {
initial.commit09911bf2008-07-26 23:55:292422 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572423 notification_observer_list_.AddObserver(
[email protected]71f65dd2009-02-11 19:14:562424 new DocumentPrintedNotificationObserver(this,
2425 reply_message->routing_id(),
2426 reply_message));
[email protected]20e93d12008-08-28 16:31:572427 if (web_contents->PrintNow())
2428 return;
initial.commit09911bf2008-07-26 23:55:292429 }
[email protected]71f65dd2009-02-11 19:14:562430 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
2431 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292432}
2433
[email protected]71f65dd2009-02-11 19:14:562434void AutomationProvider::SavePage(int tab_handle,
initial.commit09911bf2008-07-26 23:55:292435 const std::wstring& file_name,
2436 const std::wstring& dir_path,
[email protected]71f65dd2009-02-11 19:14:562437 int type,
2438 bool* success) {
initial.commit09911bf2008-07-26 23:55:292439 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562440 *success = false;
initial.commit09911bf2008-07-26 23:55:292441 return;
2442 }
2443
2444 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2445 Browser* browser = FindAndActivateTab(nav);
2446 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142447 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562448 *success = false;
initial.commit09911bf2008-07-26 23:55:292449 return;
2450 }
2451
initial.commit09911bf2008-07-26 23:55:292452 SavePackage::SavePackageType save_type =
2453 static_cast<SavePackage::SavePackageType>(type);
2454 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2455 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]965524b2009-04-04 21:32:402456 nav->tab_contents()->AsWebContents()->SavePage(
2457 file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292458
[email protected]71f65dd2009-02-11 19:14:562459 *success = true;
initial.commit09911bf2008-07-26 23:55:292460}
2461
[email protected]71f65dd2009-02-11 19:14:562462void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2463 bool* success,
2464 std::wstring* text) {
2465 *success = false;
initial.commit09911bf2008-07-26 23:55:292466 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562467 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522468 GetText();
[email protected]71f65dd2009-02-11 19:14:562469 *success = true;
initial.commit09911bf2008-07-26 23:55:292470 }
initial.commit09911bf2008-07-26 23:55:292471}
2472
[email protected]71f65dd2009-02-11 19:14:562473void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2474 const std::wstring& text,
2475 bool* success) {
2476 *success = false;
initial.commit09911bf2008-07-26 23:55:292477 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522478 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2479 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562480 *success = true;
initial.commit09911bf2008-07-26 23:55:292481 }
initial.commit09911bf2008-07-26 23:55:292482}
2483
2484void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562485 int autocomplete_edit_handle,
2486 bool* success,
2487 std::vector<AutocompleteMatchData>* matches) {
2488 *success = false;
initial.commit09911bf2008-07-26 23:55:292489 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272490 const AutocompleteResult& result = autocomplete_edit_tracker_->
2491 GetResource(autocomplete_edit_handle)->model()->result();
2492 for (AutocompleteResult::const_iterator i = result.begin();
2493 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562494 matches->push_back(AutocompleteMatchData(*i));
2495 *success = true;
initial.commit09911bf2008-07-26 23:55:292496 }
initial.commit09911bf2008-07-26 23:55:292497}
2498
2499void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562500 int autocomplete_edit_handle,
2501 bool* success,
2502 bool* query_in_progress) {
2503 *success = false;
2504 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292505 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562506 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522507 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562508 *success = true;
initial.commit09911bf2008-07-26 23:55:292509 }
initial.commit09911bf2008-07-26 23:55:292510}
2511
[email protected]28790922009-03-09 19:48:372512void AutomationProvider::OnMessageFromExternalHost(int handle,
2513 const std::string& message,
2514 const std::string& origin,
2515 const std::string& target) {
[email protected]fa83e762008-08-15 21:41:392516 if (tab_tracker_->ContainsHandle(handle)) {
2517 NavigationController* tab = tab_tracker_->GetResource(handle);
2518 if (!tab) {
2519 NOTREACHED();
2520 return;
2521 }
[email protected]965524b2009-04-04 21:32:402522 TabContents* tab_contents = tab->tab_contents();
[email protected]fa83e762008-08-15 21:41:392523 if (!tab_contents) {
2524 NOTREACHED();
2525 return;
2526 }
2527
2528 WebContents* web_contents = tab_contents->AsWebContents();
2529 if (!web_contents) {
2530 NOTREACHED();
2531 return;
2532 }
2533
2534 RenderViewHost* view_host = web_contents->render_view_host();
2535 if (!view_host) {
2536 return;
2537 }
2538
[email protected]28790922009-03-09 19:48:372539 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:392540 }
2541}
[email protected]5cc063692009-04-07 23:21:312542#endif // defined(OS_WIN)
[email protected]fa83e762008-08-15 21:41:392543
[email protected]20e93d12008-08-28 16:31:572544WebContents* AutomationProvider::GetWebContentsForHandle(
2545 int handle, NavigationController** tab) {
2546 WebContents* web_contents = NULL;
2547 if (tab_tracker_->ContainsHandle(handle)) {
2548 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2549 TabContents* tab_contents = nav_controller->active_contents();
[email protected]965524b2009-04-04 21:32:402550 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:572551 web_contents = tab_contents->AsWebContents();
2552 if (tab)
2553 *tab = nav_controller;
2554 }
2555 }
2556 return web_contents;
2557}
2558
[email protected]5cc063692009-04-07 23:21:312559#if defined(OS_WIN)
[email protected]b9d227492009-02-10 15:20:272560ExternalTabContainer* AutomationProvider::GetExternalTabForHandle(int handle) {
2561 if (tab_tracker_->ContainsHandle(handle)) {
2562 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]965524b2009-04-04 21:32:402563 return ExternalTabContainer::GetContainerForTab(
2564 tab->tab_contents()->GetNativeView());
[email protected]b9d227492009-02-10 15:20:272565 }
2566
2567 return NULL;
2568}
[email protected]de246f52009-02-25 18:25:452569#endif // defined(OS_WIN)
[email protected]b9d227492009-02-10 15:20:272570
initial.commit09911bf2008-07-26 23:55:292571TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2572 : AutomationProvider(profile) {
2573 BrowserList::AddObserver(this);
[email protected]bfd04a62009-02-01 18:16:562574 NotificationService::current()->AddObserver(
2575 this,
2576 NotificationType::SESSION_END,
initial.commit09911bf2008-07-26 23:55:292577 NotificationService::AllSources());
2578}
2579
2580TestingAutomationProvider::~TestingAutomationProvider() {
[email protected]bfd04a62009-02-01 18:16:562581 NotificationService::current()->RemoveObserver(
2582 this,
2583 NotificationType::SESSION_END,
initial.commit09911bf2008-07-26 23:55:292584 NotificationService::AllSources());
2585 BrowserList::RemoveObserver(this);
2586}
2587
2588void TestingAutomationProvider::OnChannelError() {
2589 BrowserList::CloseAllBrowsers(true);
2590 AutomationProvider::OnChannelError();
2591}
2592
2593void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2594 // For backwards compatibility with the testing automation interface, we
2595 // want the automation provider (and hence the process) to go away when the
2596 // last browser goes away.
2597 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292598 // If you change this, update Observer for NotificationType::SESSION_END
2599 // below.
[email protected]295039bd2008-08-15 04:32:572600 MessageLoop::current()->PostTask(FROM_HERE,
2601 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292602 }
2603}
2604
2605void TestingAutomationProvider::Observe(NotificationType type,
2606 const NotificationSource& source,
2607 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562608 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292609 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2610 // before the task runs resulting in this object not being deleted. This
2611 // Release balance out the Release scheduled by OnBrowserRemoving.
2612 Release();
2613}
[email protected]295039bd2008-08-15 04:32:572614
2615void TestingAutomationProvider::OnRemoveProvider() {
2616 AutomationProviderList::GetInstance()->RemoveProvider(this);
2617}
[email protected]8a3422c92008-09-24 17:42:422618
[email protected]71f65dd2009-02-11 19:14:562619void AutomationProvider::GetSSLInfoBarCount(int handle, int* count) {
2620 *count = -1; // -1 means error.
[email protected]de246f52009-02-25 18:25:452621#if defined(OS_WIN)
[email protected]8a3422c92008-09-24 17:42:422622 if (tab_tracker_->ContainsHandle(handle)) {
2623 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342624 if (nav_controller)
[email protected]71f65dd2009-02-11 19:14:562625 *count = nav_controller->active_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422626 }
[email protected]de246f52009-02-25 18:25:452627#else
2628 // TODO(port): Enable when TabContents infobar related stuff is ported.
2629 NOTIMPLEMENTED();
2630#endif
[email protected]8a3422c92008-09-24 17:42:422631}
2632
[email protected]71f65dd2009-02-11 19:14:562633void AutomationProvider::ClickSSLInfoBarLink(int handle,
[email protected]8a3422c92008-09-24 17:42:422634 int info_bar_index,
[email protected]71f65dd2009-02-11 19:14:562635 bool wait_for_navigation,
2636 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422637 bool success = false;
[email protected]de246f52009-02-25 18:25:452638#if defined(OS_WIN)
[email protected]8a3422c92008-09-24 17:42:422639 if (tab_tracker_->ContainsHandle(handle)) {
2640 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2641 if (nav_controller) {
[email protected]eb9ba192008-12-02 02:41:342642 int count = nav_controller->active_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422643 if (info_bar_index >= 0 && info_bar_index < count) {
2644 if (wait_for_navigation) {
[email protected]71f65dd2009-02-11 19:14:562645 AddNavigationStatusListener<bool>(nav_controller, reply_message,
2646 true, true, false);
[email protected]8a3422c92008-09-24 17:42:422647 }
[email protected]eb9ba192008-12-02 02:41:342648 InfoBarDelegate* delegate =
2649 nav_controller->active_contents()->GetInfoBarDelegateAt(
2650 info_bar_index);
2651 if (delegate->AsConfirmInfoBarDelegate())
2652 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422653 success = true;
2654 }
2655 }
[email protected]4f3dc372009-02-24 00:10:292656 }
[email protected]de246f52009-02-25 18:25:452657#else
2658 // TODO(port): Enable when TabContents infobar related stuff is ported.
2659 NOTIMPLEMENTED();
2660#endif
[email protected]8a3422c92008-09-24 17:42:422661 if (!wait_for_navigation || !success)
[email protected]71f65dd2009-02-11 19:14:562662 AutomationMsg_ClickSSLInfoBarLink::WriteReplyParams(reply_message,
2663 success);
[email protected]8a3422c92008-09-24 17:42:422664}
2665
[email protected]71f65dd2009-02-11 19:14:562666void AutomationProvider::GetLastNavigationTime(int handle,
2667 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422668 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562669 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422670}
2671
[email protected]71f65dd2009-02-11 19:14:562672void AutomationProvider::WaitForNavigation(int handle,
2673 int64 last_navigation_time,
2674 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422675 NavigationController* controller = NULL;
2676 if (tab_tracker_->ContainsHandle(handle))
2677 controller = tab_tracker_->GetResource(handle);
2678
2679 Time time = tab_tracker_->GetLastNavigationTime(handle);
2680 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562681 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
2682 controller != NULL);
[email protected]4f3dc372009-02-24 00:10:292683 return;
[email protected]8a3422c92008-09-24 17:42:422684 }
2685
[email protected]71f65dd2009-02-11 19:14:562686 AddNavigationStatusListener<bool>(controller, reply_message, true, true,
2687 false);
[email protected]8a3422c92008-09-24 17:42:422688}
2689
[email protected]71f65dd2009-02-11 19:14:562690void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162691 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562692 int value,
2693 bool* success) {
2694 *success = false;
[email protected]8a3422c92008-09-24 17:42:422695 if (browser_tracker_->ContainsHandle(handle)) {
2696 Browser* browser = browser_tracker_->GetResource(handle);
2697 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562698 *success = true;
[email protected]8a3422c92008-09-24 17:42:422699 }
[email protected]8a3422c92008-09-24 17:42:422700}
[email protected]97fa6ce32008-12-19 01:48:162701
[email protected]71f65dd2009-02-11 19:14:562702void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162703 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562704 const std::wstring& value,
2705 bool* success) {
2706 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162707 if (browser_tracker_->ContainsHandle(handle)) {
2708 Browser* browser = browser_tracker_->GetResource(handle);
2709 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562710 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162711 }
[email protected]97fa6ce32008-12-19 01:48:162712}
2713
[email protected]71f65dd2009-02-11 19:14:562714void AutomationProvider::GetBooleanPreference(int handle,
2715 const std::wstring& name,
2716 bool* success,
2717 bool* value) {
2718 *success = false;
2719 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162720 if (browser_tracker_->ContainsHandle(handle)) {
2721 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562722 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2723 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162724 }
[email protected]97fa6ce32008-12-19 01:48:162725}
2726
[email protected]71f65dd2009-02-11 19:14:562727void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162728 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562729 bool value,
2730 bool* success) {
2731 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162732 if (browser_tracker_->ContainsHandle(handle)) {
2733 Browser* browser = browser_tracker_->GetResource(handle);
2734 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562735 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162736 }
[email protected]97fa6ce32008-12-19 01:48:162737}
2738
2739// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562740void AutomationProvider::GetPageCurrentEncoding(
2741 int tab_handle, std::wstring* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162742 if (tab_tracker_->ContainsHandle(tab_handle)) {
2743 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2744 Browser* browser = FindAndActivateTab(nav);
2745 DCHECK(browser);
2746
[email protected]1fc025202009-01-20 23:03:142747 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162748 TabContents* tab_contents = nav->active_contents();
[email protected]71f65dd2009-02-11 19:14:562749 *current_encoding = tab_contents->AsWebContents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162750 }
2751 }
[email protected]97fa6ce32008-12-19 01:48:162752}
2753
2754// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562755void AutomationProvider::OverrideEncoding(int tab_handle,
2756 const std::wstring& encoding_name,
2757 bool* success) {
2758 *success = false;
[email protected]de246f52009-02-25 18:25:452759#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:162760 if (tab_tracker_->ContainsHandle(tab_handle)) {
2761 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2762 Browser* browser = FindAndActivateTab(nav);
2763 DCHECK(browser);
2764
[email protected]1fc025202009-01-20 23:03:142765 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]97fa6ce32008-12-19 01:48:162766 TabContents* tab_contents = nav->active_contents();
[email protected]97fa6ce32008-12-19 01:48:162767 int selected_encoding_id =
2768 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2769 if (selected_encoding_id) {
2770 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562771 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162772 }
2773 }
2774 }
[email protected]de246f52009-02-25 18:25:452775#else
2776 // TODO(port): Enable when encoding-related parts of Browser are ported.
2777 NOTIMPLEMENTED();
2778#endif
[email protected]97fa6ce32008-12-19 01:48:162779}
[email protected]5bcdb312009-01-07 21:43:202780
[email protected]4d434a1a2009-02-11 21:06:572781void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202782 SavePackage::SetShouldPromptUser(should_prompt);
2783}
[email protected]87eab222009-03-13 00:47:452784
2785#ifdef OS_WIN
2786void AutomationProvider::OnTabReposition(
2787 int tab_handle, const IPC::Reposition_Params& params) {
2788 if (!tab_tracker_->ContainsHandle(tab_handle))
2789 return;
2790
2791 if (!IsWindow(params.window))
2792 return;
2793
2794 unsigned long process_id = 0;
2795 unsigned long thread_id = 0;
2796
2797 thread_id = GetWindowThreadProcessId(params.window, &process_id);
2798
2799 if (thread_id != GetCurrentThreadId()) {
2800 NOTREACHED();
2801 return;
2802 }
2803
[email protected]a2c5a9892009-04-07 16:13:452804 if (params.set_parent) {
2805 if (IsWindow(params.parent_window)) {
2806 if (!SetParent(params.window, params.parent_window))
2807 DLOG(WARNING) << "SetParent failed. Error 0x%x" << GetLastError();
2808 }
2809 }
2810
[email protected]87eab222009-03-13 00:47:452811 SetWindowPos(params.window, params.window_insert_after, params.left,
2812 params.top, params.width, params.height, params.flags);
2813}
[email protected]a2c5a9892009-04-07 16:13:452814
[email protected]87eab222009-03-13 00:47:452815#endif // defined(OS_WIN)