blob: 7b9214c3f0f6f1dfc0d55ceb788856bbdab2debe [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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
7#include "base/path_service.h"
[email protected]de5abb92008-10-22 21:33:278#include "base/process_util.h"
[email protected]9e0534b2008-10-21 15:03:019#include "chrome/app/chrome_dll_resource.h"
initial.commit09911bf2008-07-26 23:55:2910#include "chrome/browser/automation/automation_provider_list.h"
11#include "chrome/browser/automation/ui_controls.h"
12#include "chrome/browser/automation/url_request_failed_dns_job.h"
13#include "chrome/browser/automation/url_request_mock_http_job.h"
14#include "chrome/browser/automation/url_request_slow_download_job.h"
[email protected]f3e99e32008-07-30 04:48:3915#include "chrome/browser/browser_window.h"
initial.commit09911bf2008-07-26 23:55:2916#include "chrome/browser/dom_operation_notification_details.h"
[email protected]cdaa8652008-09-13 02:48:5917#include "chrome/browser/download/download_manager.h"
[email protected]37936ee2008-09-14 01:09:5018#include "chrome/browser/download/save_package.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/browser/external_tab_container.h"
20#include "chrome/browser/find_notification_details.h"
21#include "chrome/browser/login_prompt.h"
22#include "chrome/browser/navigation_entry.h"
23#include "chrome/browser/printing/print_job.h"
[email protected]fa83e762008-08-15 21:41:3924#include "chrome/browser/render_view_host.h"
[email protected]8a3422c92008-09-24 17:42:4225#include "chrome/browser/ssl_manager.h"
initial.commit09911bf2008-07-26 23:55:2926#include "chrome/browser/ssl_blocking_page.h"
[email protected]1eb89e82008-08-15 12:27:0327#include "chrome/browser/web_contents.h"
[email protected]9e0534b2008-10-21 15:03:0128#include "chrome/browser/web_contents_view.h"
[email protected]c2cbeb92008-09-05 21:36:5729#include "chrome/browser/views/bookmark_bar_view.h"
[email protected]195442e2008-07-31 22:41:2830#include "chrome/browser/views/location_bar_view.h"
initial.commit09911bf2008-07-26 23:55:2931#include "chrome/common/chrome_paths.h"
[email protected]8a3422c92008-09-24 17:42:4232#include "chrome/common/pref_service.h"
initial.commit09911bf2008-07-26 23:55:2933#include "chrome/test/automation/automation_messages.h"
34#include "net/base/cookie_monster.h"
35#include "net/url_request/url_request_filter.h"
36
[email protected]e1acf6f2008-10-27 20:43:3337using base::Time;
38
initial.commit09911bf2008-07-26 23:55:2939class InitialLoadObserver : public NotificationObserver {
40 public:
41 InitialLoadObserver(size_t tab_count, AutomationProvider* automation)
42 : outstanding_tab_count_(tab_count),
43 automation_(automation) {
44 if (outstanding_tab_count_ > 0) {
45 NotificationService* service = NotificationService::current();
46 service->AddObserver(this, NOTIFY_LOAD_START,
47 NotificationService::AllSources());
48 service->AddObserver(this, NOTIFY_LOAD_STOP,
49 NotificationService::AllSources());
50 }
51 }
52
53 ~InitialLoadObserver() {
54 Unregister();
55 }
56
57 void ConditionMet() {
58 Unregister();
59 automation_->Send(new AutomationMsg_InitialLoadsComplete(0));
60 }
61
62 void Unregister() {
63 NotificationService* service = NotificationService::current();
64 service->RemoveObserver(this, NOTIFY_LOAD_START,
65 NotificationService::AllSources());
66 service->RemoveObserver(this, NOTIFY_LOAD_STOP,
67 NotificationService::AllSources());
68 }
69
70 virtual void Observe(NotificationType type,
71 const NotificationSource& source,
72 const NotificationDetails& details) {
73 if (type == NOTIFY_LOAD_START) {
74 if (outstanding_tab_count_ > loading_tabs_.size())
75 loading_tabs_.insert(source.map_key());
76 } else if (type == NOTIFY_LOAD_STOP) {
77 if (outstanding_tab_count_ > finished_tabs_.size()) {
78 if (loading_tabs_.find(source.map_key()) != loading_tabs_.end())
79 finished_tabs_.insert(source.map_key());
80 if (outstanding_tab_count_ == finished_tabs_.size())
81 ConditionMet();
82 }
83 } else {
84 NOTREACHED();
85 }
86 }
87
88 private:
89 typedef std::set<uintptr_t> TabSet;
90
91 AutomationProvider* automation_;
92 size_t outstanding_tab_count_;
93 TabSet loading_tabs_;
94 TabSet finished_tabs_;
95};
96
97// Watches for NewTabUI page loads for performance timing purposes.
98class NewTabUILoadObserver : public NotificationObserver {
99 public:
100 explicit NewTabUILoadObserver(AutomationProvider* automation)
101 : automation_(automation) {
102 NotificationService::current()->
103 AddObserver(this, NOTIFY_INITIAL_NEW_TAB_UI_LOAD,
104 NotificationService::AllSources());
105 }
106
107 ~NewTabUILoadObserver() {
108 Unregister();
109 }
110
111 void Unregister() {
112 NotificationService::current()->
113 RemoveObserver(this, NOTIFY_INITIAL_NEW_TAB_UI_LOAD,
114 NotificationService::AllSources());
115 }
116
117 virtual void Observe(NotificationType type,
118 const NotificationSource& source,
119 const NotificationDetails& details) {
120 if (type == NOTIFY_INITIAL_NEW_TAB_UI_LOAD) {
121 Details<int> load_time(details);
122 automation_->Send(
123 new AutomationMsg_InitialNewTabUILoadComplete(0, *load_time.ptr()));
124 } else {
125 NOTREACHED();
126 }
127 }
128
129 private:
130 AutomationProvider* automation_;
131};
132
133class NavigationControllerRestoredObserver : public NotificationObserver {
134 public:
135 NavigationControllerRestoredObserver(AutomationProvider* automation,
136 NavigationController* controller,
137 int32 routing_id)
138 : automation_(automation),
139 controller_(controller),
140 routing_id_(routing_id) {
141 if (FinishedRestoring()) {
142 registered_ = false;
143 SendDone();
144 } else {
145 registered_ = true;
146 NotificationService* service = NotificationService::current();
147 service->AddObserver(this, NOTIFY_LOAD_STOP,
148 NotificationService::AllSources());
149 }
150 }
151
152 ~NavigationControllerRestoredObserver() {
153 if (registered_)
154 Unregister();
155 }
156
157 virtual void Observe(NotificationType type,
158 const NotificationSource& source,
159 const NotificationDetails& details) {
160 if (FinishedRestoring()) {
161 SendDone();
162 Unregister();
163 }
164 }
165
166 private:
167 void Unregister() {
168 NotificationService* service = NotificationService::current();
169 service->RemoveObserver(this, NOTIFY_LOAD_STOP,
170 NotificationService::AllSources());
171 registered_ = false;
172 }
173
174 bool FinishedRestoring() {
175 return (!controller_->needs_reload() && !controller_->GetPendingEntry() &&
[email protected]d5f942ba2008-09-26 19:30:34176 !controller_->active_contents()->is_loading());
initial.commit09911bf2008-07-26 23:55:29177 }
178
179 void SendDone() {
180 automation_->Send(new AutomationMsg_TabFinishedRestoring(routing_id_));
181 }
182
183 bool registered_;
184 AutomationProvider* automation_;
185 NavigationController* controller_;
186 const int routing_id_;
187
[email protected]5a52f162008-08-27 04:15:31188 DISALLOW_COPY_AND_ASSIGN(NavigationControllerRestoredObserver);
initial.commit09911bf2008-07-26 23:55:29189};
190
initial.commit09911bf2008-07-26 23:55:29191class NavigationNotificationObserver : public NotificationObserver {
192 public:
193 NavigationNotificationObserver(NavigationController* controller,
194 AutomationProvider* automation,
195 IPC::Message* completed_response,
196 IPC::Message* auth_needed_response)
197 : automation_(automation),
198 completed_response_(completed_response),
199 auth_needed_response_(auth_needed_response),
200 controller_(controller),
201 navigation_started_(false) {
202 NotificationService* service = NotificationService::current();
[email protected]8a3422c92008-09-24 17:42:42203 service->AddObserver(this, NOTIFY_NAV_ENTRY_COMMITTED,
204 Source<NavigationController>(controller_));
initial.commit09911bf2008-07-26 23:55:29205 service->AddObserver(this, NOTIFY_LOAD_START,
206 Source<NavigationController>(controller_));
207 service->AddObserver(this, NOTIFY_LOAD_STOP,
208 Source<NavigationController>(controller_));
209 service->AddObserver(this, NOTIFY_AUTH_NEEDED,
210 Source<NavigationController>(controller_));
211 service->AddObserver(this, NOTIFY_AUTH_SUPPLIED,
212 Source<NavigationController>(controller_));
213 }
214
215 ~NavigationNotificationObserver() {
216 if (completed_response_) delete completed_response_;
217 if (auth_needed_response_) delete auth_needed_response_;
218 Unregister();
219 }
220
221 void ConditionMet(IPC::Message** response) {
222 if (*response) {
223 automation_->Send(*response);
224 *response = NULL; // *response is deleted by Send.
225 }
[email protected]d5798082008-09-29 21:02:03226 automation_->RemoveNavigationStatusListener(this);
initial.commit09911bf2008-07-26 23:55:29227 delete this;
228 }
229
230 void Unregister() {
231 NotificationService* service = NotificationService::current();
[email protected]8a3422c92008-09-24 17:42:42232 service->RemoveObserver(this, NOTIFY_NAV_ENTRY_COMMITTED,
233 Source<NavigationController>(controller_));
initial.commit09911bf2008-07-26 23:55:29234 service->RemoveObserver(this, NOTIFY_LOAD_START,
235 Source<NavigationController>(controller_));
236 service->RemoveObserver(this, NOTIFY_LOAD_STOP,
237 Source<NavigationController>(controller_));
238 service->RemoveObserver(this, NOTIFY_AUTH_NEEDED,
239 Source<NavigationController>(controller_));
240 service->RemoveObserver(this, NOTIFY_AUTH_SUPPLIED,
241 Source<NavigationController>(controller_));
242 }
243
244 virtual void Observe(NotificationType type,
245 const NotificationSource& source,
246 const NotificationDetails& details) {
[email protected]8a3422c92008-09-24 17:42:42247 // We listen for 2 events to determine when the navigation started because:
248 // - when this is used by the WaitForNavigation method, we might be invoked
249 // afer the load has started (but not after the entry was committed, as
250 // WaitForNavigation compares times of the last navigation).
251 // - when this is used with a page requiring authentication, we will not get
252 // a NOTIFY_NAV_ENTRY_COMMITTED until after we authenticate, so we need the
253 // NOTIFY_LOAD_START.
254 if (type == NOTIFY_NAV_ENTRY_COMMITTED || type == NOTIFY_LOAD_START) {
initial.commit09911bf2008-07-26 23:55:29255 navigation_started_ = true;
256 } else if (type == NOTIFY_LOAD_STOP) {
257 if (navigation_started_) {
258 navigation_started_ = false;
259 ConditionMet(&completed_response_);
260 }
261 } else if (type == NOTIFY_AUTH_SUPPLIED) {
262 // The LoginHandler for this tab is no longer valid.
263 automation_->RemoveLoginHandler(controller_);
264
265 // Treat this as if navigation started again, since load start/stop don't
266 // occur while authentication is ongoing.
267 navigation_started_ = true;
268 } else if (type == NOTIFY_AUTH_NEEDED) {
269 if (navigation_started_) {
270 // Remember the login handler that wants authentication.
271 LoginHandler* handler =
272 Details<LoginNotificationDetails>(details)->handler();
273 automation_->AddLoginHandler(controller_, handler);
274
275 // Respond that authentication is needed.
276 navigation_started_ = false;
277 ConditionMet(&auth_needed_response_);
278 } else {
279 NOTREACHED();
280 }
281 } else {
282 NOTREACHED();
283 }
284 }
285
286 private:
287 AutomationProvider* automation_;
288 IPC::Message* completed_response_;
289 IPC::Message* auth_needed_response_;
290 NavigationController* controller_;
291 bool navigation_started_;
292};
293
294class TabStripNotificationObserver : public NotificationObserver {
295 public:
296 TabStripNotificationObserver(Browser* parent, NotificationType notification,
297 AutomationProvider* automation, int32 routing_id)
298 : automation_(automation),
299 notification_(notification),
300 parent_(parent),
301 routing_id_(routing_id) {
302 NotificationService::current()->
303 AddObserver(this, notification_, NotificationService::AllSources());
304 }
305
306 virtual ~TabStripNotificationObserver() {
307 Unregister();
308 }
309
310 void Unregister() {
311 NotificationService::current()->
312 RemoveObserver(this, notification_, NotificationService::AllSources());
313 }
314
315 virtual void Observe(NotificationType type,
316 const NotificationSource& source,
317 const NotificationDetails& details) {
318 if (type == notification_) {
319 ObserveTab(Source<NavigationController>(source).ptr());
320
321 // If verified, no need to observe anymore
322 automation_->RemoveTabStripObserver(this);
323 delete this;
324 } else {
325 NOTREACHED();
326 }
327 }
328
329 virtual void ObserveTab(NavigationController* controller) = 0;
330
331 protected:
332 AutomationProvider* automation_;
333 Browser* parent_;
334 NotificationType notification_;
335 int32 routing_id_;
336};
337
338class TabAppendedNotificationObserver : public TabStripNotificationObserver {
339 public:
340 TabAppendedNotificationObserver(Browser* parent,
341 AutomationProvider* automation, int32 routing_id)
[email protected]534e54b2008-08-13 15:40:09342 : TabStripNotificationObserver(parent, NOTIFY_TAB_PARENTED, automation,
initial.commit09911bf2008-07-26 23:55:29343 routing_id) {
344 }
345
346 virtual void ObserveTab(NavigationController* controller) {
347 int tab_index =
348 automation_->GetIndexForNavigationController(controller, parent_);
349 if (tab_index == TabStripModel::kNoTab) {
350 // This tab notification doesn't belong to the parent_
351 return;
352 }
353
354 // Give the same response even if auth is needed, since it doesn't matter.
355 automation_->AddNavigationStatusListener(controller,
356 new AutomationMsg_AppendTabResponse(routing_id_, tab_index),
357 new AutomationMsg_AppendTabResponse(routing_id_, tab_index));
358 }
359};
360
361class TabClosedNotificationObserver : public TabStripNotificationObserver {
362 public:
363 TabClosedNotificationObserver(Browser* parent,
364 AutomationProvider* automation,
365 int32 routing_id,
366 bool wait_until_closed)
367 : TabStripNotificationObserver(parent,
368 wait_until_closed ? NOTIFY_TAB_CLOSED :
369 NOTIFY_TAB_CLOSING,
370 automation,
371 routing_id) {
372 }
373
374 virtual void ObserveTab(NavigationController* controller) {
375 automation_->Send(new AutomationMsg_CloseTabResponse(routing_id_, true));
376 }
377};
378
379class BrowserClosedNotificationObserver : public NotificationObserver {
380 public:
381 BrowserClosedNotificationObserver(Browser* browser,
382 AutomationProvider* automation,
383 int32 routing_id)
384 : automation_(automation),
385 routing_id_(routing_id) {
386 NotificationService::current()->
387 AddObserver(this, NOTIFY_BROWSER_CLOSED, Source<Browser>(browser));
388 }
389
390 virtual void Observe(NotificationType type,
391 const NotificationSource& source,
392 const NotificationDetails& details) {
393 DCHECK(type == NOTIFY_BROWSER_CLOSED);
394 Details<bool> close_app(details);
395 automation_->Send(
396 new AutomationMsg_CloseBrowserResponse(routing_id_,
397 true,
398 *(close_app.ptr())));
399 delete this;
400 }
401
402 private:
403 AutomationProvider* automation_;
404 int32 routing_id_;
405};
406
407class FindInPageNotificationObserver : public NotificationObserver {
408 public:
409 FindInPageNotificationObserver(AutomationProvider* automation,
410 TabContents* parent_tab,
411 int32 routing_id)
412 : automation_(automation),
413 parent_tab_(parent_tab),
414 routing_id_(routing_id) {
415 NotificationService::current()->
416 AddObserver(this, NOTIFY_FIND_RESULT_AVAILABLE,
417 Source<TabContents>(parent_tab_));
418 }
419
420 ~FindInPageNotificationObserver() {
421 Unregister();
422 }
423
424 void Unregister() {
425 NotificationService::current()->
426 RemoveObserver(this, NOTIFY_FIND_RESULT_AVAILABLE,
427 Source<TabContents>(parent_tab_));
428 }
429
430 virtual void Observe(NotificationType type, const NotificationSource& source,
431 const NotificationDetails& details) {
432 if (type == NOTIFY_FIND_RESULT_AVAILABLE) {
433 Details<FindNotificationDetails> find_details(details);
434 if (find_details->request_id() == kFindInPageRequestId) {
435 if (find_details->final_update()) {
436 automation_->Send(new AutomationMsg_FindInPageResponse(routing_id_,
437 find_details->number_of_matches()));
438 } else {
439 DLOG(INFO) << "Ignoring, since we only care about the final message";
440 }
441 }
442 } else {
443 NOTREACHED();
444 }
445 }
446
447 // The Find mechanism is over asynchronous IPC, so a search is kicked off and
448 // we wait for notification to find out what the results are. As the user is
449 // typing, new search requests can be issued and the Request ID helps us make
450 // sense of whether this is the current request or an old one. The unit tests,
451 // however, which uses this constant issues only one search at a time, so we
452 // don't need a rolling id to identify each search. But, we still need to
453 // specify one, so we just use a fixed one - its value does not matter.
454 static const int kFindInPageRequestId;
455 private:
456 AutomationProvider* automation_;
457 TabContents* parent_tab_;
458 int32 routing_id_;
459};
460
461const int FindInPageNotificationObserver::kFindInPageRequestId = -1;
462
463class DomOperationNotificationObserver : public NotificationObserver {
464 public:
465 explicit DomOperationNotificationObserver(AutomationProvider* automation)
466 : automation_(automation) {
467 NotificationService::current()->
468 AddObserver(this, NOTIFY_DOM_OPERATION_RESPONSE,
469 NotificationService::AllSources());
470 }
471
472 ~DomOperationNotificationObserver() {
473 NotificationService::current()->
474 RemoveObserver(this, NOTIFY_DOM_OPERATION_RESPONSE,
475 NotificationService::AllSources());
476 }
477
478 virtual void Observe(NotificationType type, const NotificationSource& source,
479 const NotificationDetails& details) {
480 if (NOTIFY_DOM_OPERATION_RESPONSE == type) {
481 Details<DomOperationNotificationDetails> dom_op_details(details);
482 automation_->Send(new AutomationMsg_DomOperationResponse(
483 dom_op_details->automation_id(),
484 dom_op_details->json()));
485 }
486 }
487 private:
488 AutomationProvider* automation_;
489};
490
491class DomInspectorNotificationObserver : public NotificationObserver {
492 public:
493 explicit DomInspectorNotificationObserver(AutomationProvider* automation)
494 : automation_(automation) {
495 NotificationService::current()->
496 AddObserver(this, NOTIFY_DOM_INSPECT_ELEMENT_RESPONSE,
497 NotificationService::AllSources());
498 }
499
500 ~DomInspectorNotificationObserver() {
501 NotificationService::current()->
502 RemoveObserver(this, NOTIFY_DOM_INSPECT_ELEMENT_RESPONSE,
503 NotificationService::AllSources());
504 }
505
506 virtual void Observe(NotificationType type, const NotificationSource& source,
507 const NotificationDetails& details) {
508 if (NOTIFY_DOM_INSPECT_ELEMENT_RESPONSE == type) {
509 Details<int> dom_inspect_details(details);
510 automation_->ReceivedInspectElementResponse(*(dom_inspect_details.ptr()));
511 }
512 }
513
514 private:
515 AutomationProvider* automation_;
516};
517
518class DocumentPrintedNotificationObserver : public NotificationObserver {
519 public:
520 DocumentPrintedNotificationObserver(AutomationProvider* automation,
521 int32 routing_id)
522 : automation_(automation),
523 routing_id_(routing_id),
524 success_(false) {
525 NotificationService::current()->
526 AddObserver(this, NOTIFY_PRINT_JOB_EVENT,
527 NotificationService::AllSources());
528 }
529
530 ~DocumentPrintedNotificationObserver() {
531 automation_->Send(
532 new AutomationMsg_PrintNowResponse(routing_id_, success_));
533 automation_->RemoveNavigationStatusListener(this);
534 NotificationService::current()->
535 RemoveObserver(this, NOTIFY_PRINT_JOB_EVENT,
536 NotificationService::AllSources());
537 }
538
539 virtual void Observe(NotificationType type, const NotificationSource& source,
540 const NotificationDetails& details) {
541 using namespace printing;
542 DCHECK(type == NOTIFY_PRINT_JOB_EVENT);
543 switch (Details<JobEventDetails>(details)->type()) {
544 case JobEventDetails::JOB_DONE: {
545 // Succeeded.
546 success_ = true;
547 delete this;
548 break;
549 }
550 case JobEventDetails::USER_INIT_CANCELED:
551 case JobEventDetails::FAILED: {
552 // Failed.
553 delete this;
554 break;
555 }
556 case JobEventDetails::NEW_DOC:
557 case JobEventDetails::USER_INIT_DONE:
558 case JobEventDetails::DEFAULT_INIT_DONE:
559 case JobEventDetails::NEW_PAGE:
560 case JobEventDetails::PAGE_DONE:
561 case JobEventDetails::DOC_DONE:
562 case JobEventDetails::ALL_PAGES_REQUESTED: {
563 // Don't care.
564 break;
565 }
566 default: {
567 NOTREACHED();
568 break;
569 }
570 }
571 }
572
573 private:
574 scoped_refptr<AutomationProvider> automation_;
575 int32 routing_id_;
576 bool success_;
577};
578
[email protected]cbab76d2008-10-13 22:42:47579class AutomationInterstitialPage : public InterstitialPage {
580 public:
581 AutomationInterstitialPage(TabContents* tab,
582 const GURL& url,
583 const std::string& contents)
584 : InterstitialPage(tab, true, url),
585 contents_(contents) {
586 }
587
588 virtual std::string GetHTMLContents() { return contents_; }
589
590 private:
591 std::string contents_;
592
593 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
594};
595
initial.commit09911bf2008-07-26 23:55:29596AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57597 : redirect_query_(0),
initial.commit09911bf2008-07-26 23:55:29598 profile_(profile) {
initial.commit09911bf2008-07-26 23:55:29599 browser_tracker_.reset(new AutomationBrowserTracker(this));
600 window_tracker_.reset(new AutomationWindowTracker(this));
601 tab_tracker_.reset(new AutomationTabTracker(this));
602 autocomplete_edit_tracker_.reset(
603 new AutomationAutocompleteEditTracker(this));
604 cwindow_tracker_.reset(new AutomationConstrainedWindowTracker(this));
605 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
606 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
607 dom_inspector_observer_.reset(new DomInspectorNotificationObserver(this));
608}
609
610AutomationProvider::~AutomationProvider() {
[email protected]0da050b92008-08-19 19:29:47611 // Make sure that any outstanding NotificationObservers also get destroyed.
612 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31613 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47614 while ((observer = it.GetNext()) != NULL)
615 delete observer;
initial.commit09911bf2008-07-26 23:55:29616}
617
618void AutomationProvider::ConnectToChannel(const std::wstring& channel_id) {
[email protected]295039bd2008-08-15 04:32:57619 channel_.reset(
620 new IPC::ChannelProxy(channel_id, IPC::Channel::MODE_CLIENT, this, NULL,
621 g_browser_process->io_thread()->message_loop()));
622 channel_->Send(new AutomationMsg_Hello(0));
initial.commit09911bf2008-07-26 23:55:29623}
624
625void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
626 if (expected_tabs == 0) {
627 Send(new AutomationMsg_InitialLoadsComplete(0));
628 } else {
629 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
630 }
631}
632
633NotificationObserver* AutomationProvider::AddNavigationStatusListener(
634 NavigationController* tab, IPC::Message* completed_response,
635 IPC::Message* auth_needed_response) {
636 NotificationObserver* observer =
637 new NavigationNotificationObserver(tab, this, completed_response,
638 auth_needed_response);
639 notification_observer_list_.AddObserver(observer);
640
641 return observer;
642}
643
644void AutomationProvider::RemoveNavigationStatusListener(
645 NotificationObserver* obs) {
646 notification_observer_list_.RemoveObserver(obs);
647}
648
649NotificationObserver* AutomationProvider::AddTabStripObserver(
650 Browser* parent, int32 routing_id) {
651 NotificationObserver* observer = new
652 TabAppendedNotificationObserver(parent, this, routing_id);
653 notification_observer_list_.AddObserver(observer);
654
655 return observer;
656}
657
658void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
659 notification_observer_list_.RemoveObserver(obs);
660}
661
662void AutomationProvider::AddLoginHandler(NavigationController* tab,
663 LoginHandler* handler) {
664 login_handler_map_[tab] = handler;
665}
666
667void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
668 DCHECK(login_handler_map_[tab]);
669 login_handler_map_.erase(tab);
670}
671
672int AutomationProvider::GetIndexForNavigationController(
673 const NavigationController* controller, const Browser* parent) const {
674 DCHECK(parent);
675 return parent->GetIndexOfController(controller);
676}
677
678void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
679 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
680 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequest, CloseBrowser)
681 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTabRequest, ActivateTab)
682 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndexRequest, GetActiveTabIndex)
683 IPC_MESSAGE_HANDLER(AutomationMsg_AppendTabRequest, AppendTab)
684 IPC_MESSAGE_HANDLER(AutomationMsg_CloseTabRequest, CloseTab)
685 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookiesRequest, GetCookies)
686 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookieRequest, SetCookie)
687 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateToURLRequest, NavigateToURL)
688 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsyncRequest, NavigationAsync)
689 IPC_MESSAGE_HANDLER(AutomationMsg_GoBackRequest, GoBack)
690 IPC_MESSAGE_HANDLER(AutomationMsg_GoForwardRequest, GoForward)
691 IPC_MESSAGE_HANDLER(AutomationMsg_ReloadRequest, Reload)
692 IPC_MESSAGE_HANDLER(AutomationMsg_SetAuthRequest, SetAuth)
693 IPC_MESSAGE_HANDLER(AutomationMsg_CancelAuthRequest, CancelAuth)
694 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuthRequest, NeedsAuth)
695 IPC_MESSAGE_HANDLER(AutomationMsg_RedirectsFromRequest, GetRedirectsFrom)
696 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCountRequest,
697 GetBrowserWindowCount)
698 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowRequest, GetBrowserWindow)
699 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindowRequest,
700 GetLastActiveBrowserWindow)
701 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindowRequest, GetActiveWindow)
702 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActiveRequest, IsWindowActive)
703 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow);
704 IPC_MESSAGE_HANDLER(AutomationMsg_WindowHWNDRequest, GetWindowHWND)
[email protected]4ae62752008-08-04 23:28:47705 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandRequest,
706 ExecuteBrowserCommand)
initial.commit09911bf2008-07-26 23:55:29707 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBoundsRequest,
708 WindowGetViewBounds)
709 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisibleRequest, SetWindowVisible)
710 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClickRequest, WindowSimulateClick)
711 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPressRequest,
712 WindowSimulateKeyPress)
713 IPC_MESSAGE_HANDLER(AutomationMsg_WindowDragRequest, WindowSimulateDrag)
714 IPC_MESSAGE_HANDLER(AutomationMsg_TabCountRequest, GetTabCount)
715 IPC_MESSAGE_HANDLER(AutomationMsg_TabRequest, GetTab)
716 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWNDRequest, GetTabHWND)
717 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessIDRequest, GetTabProcessID)
718 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitleRequest, GetTabTitle)
719 IPC_MESSAGE_HANDLER(AutomationMsg_TabURLRequest, GetTabURL)
720 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibilityRequest,
721 GetShelfVisibility)
722 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
723 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAcceleratorRequest, ApplyAccelerator)
724 IPC_MESSAGE_HANDLER(AutomationMsg_DomOperationRequest, ExecuteJavascript)
725 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCountRequest,
726 GetConstrainedWindowCount)
727 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowRequest,
728 GetConstrainedWindow)
729 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedTitleRequest,
730 GetConstrainedTitle)
731 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPageRequest,
732 HandleFindInPageRequest)
733 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewIDRequest, GetFocusedViewID)
734 IPC_MESSAGE_HANDLER(AutomationMsg_InspectElementRequest,
735 HandleInspectElementRequest)
736 IPC_MESSAGE_HANDLER(AutomationMsg_SetFilteredInet,
737 SetFilteredInet);
738 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectoryRequest,
739 GetDownloadDirectory);
740 IPC_MESSAGE_HANDLER(AutomationMsg_OpenNewBrowserWindow,
741 OpenNewBrowserWindow);
742 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowserRequest,
743 GetWindowForBrowser);
744 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowserRequest,
745 GetAutocompleteEditForBrowser);
746 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindowRequest,
747 GetBrowserForWindow);
748 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
749 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTabRequest,
750 NavigateInExternalTab)
751 IPC_MESSAGE_HANDLER(AutomationMsg_ShowInterstitialPageRequest,
752 ShowInterstitialPage);
753 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPageRequest,
754 HideInterstitialPage);
755 IPC_MESSAGE_HANDLER(AutomationMsg_SetAcceleratorsForTab,
756 SetAcceleratorsForTab)
757 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
758 ProcessUnhandledAccelerator)
759 IPC_MESSAGE_HANDLER(AutomationMsg_WaitForTabToBeRestored,
760 WaitForTabToBeRestored)
761 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState,
762 GetSecurityState)
763 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType,
764 GetPageType)
765 IPC_MESSAGE_HANDLER(AutomationMsg_ActionOnSSLBlockingPage,
766 ActionOnSSLBlockingPage)
767 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
768 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
769 IsPageMenuCommandEnabled)
770 IPC_MESSAGE_HANDLER(AutomationMsg_PrintNowRequest, PrintNow)
771 IPC_MESSAGE_HANDLER(AutomationMsg_SavePageRequest, SavePage)
772 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetTextRequest,
773 GetAutocompleteEditText)
774 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetTextRequest,
775 SetAutocompleteEditText)
776 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgressRequest,
777 AutocompleteEditIsQueryInProgress)
778 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatchesRequest,
779 AutocompleteEditGetMatches)
780 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowBoundsRequest,
781 GetConstrainedWindowBounds)
[email protected]5f8af2a2008-08-06 22:49:45782 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPageRequest,
783 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:45784 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
785 OnMessageFromExternalHost)
[email protected]5a52f162008-08-27 04:15:31786 IPC_MESSAGE_HANDLER(AutomationMsg_FindRequest,
787 HandleFindRequest)
[email protected]20e93d12008-08-28 16:31:57788 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibilityRequest,
789 GetFindWindowVisibility)
790 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocationRequest,
791 HandleFindWindowLocationRequest)
[email protected]c2cbeb92008-09-05 21:36:57792 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibilityRequest,
793 GetBookmarkBarVisitility)
[email protected]8a3422c92008-09-24 17:42:42794 IPC_MESSAGE_HANDLER(AutomationMsg_GetSSLInfoBarCountRequest,
795 GetSSLInfoBarCount)
796 IPC_MESSAGE_HANDLER(AutomationMsg_ClickSSLInfoBarLinkRequest,
797 ClickSSLInfoBarLink)
798 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTimeRequest,
799 GetLastNavigationTime)
800 IPC_MESSAGE_HANDLER(AutomationMsg_WaitForNavigationRequest,
801 WaitForNavigation)
802 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreferenceRequest,
803 SetIntPreference)
initial.commit09911bf2008-07-26 23:55:29804 IPC_END_MESSAGE_MAP()
805}
806
807void AutomationProvider::ActivateTab(const IPC::Message& message,
808 int handle, int at_index) {
809 int status = -1;
810 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
811 Browser* browser = browser_tracker_->GetResource(handle);
812 if (at_index >= 0 && at_index < browser->tab_count()) {
813 browser->SelectTabContentsAt(at_index, true);
814 status = 0;
815 }
816 }
817 Send(new AutomationMsg_ActivateTabResponse(message.routing_id(), status));
818}
819
820void AutomationProvider::AppendTab(const IPC::Message& message,
821 int handle, const GURL& url) {
822 int append_tab_response = -1; // -1 is the error code
823 NotificationObserver* observer = NULL;
824
825 if (browser_tracker_->ContainsHandle(handle)) {
826 Browser* browser = browser_tracker_->GetResource(handle);
827 observer = AddTabStripObserver(browser, message.routing_id());
828 TabContents* tab_contents =
[email protected]c0588052008-10-27 23:01:50829 browser->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, NULL);
initial.commit09911bf2008-07-26 23:55:29830 if (tab_contents) {
831 append_tab_response =
832 GetIndexForNavigationController(tab_contents->controller(), browser);
833 }
834 }
835
836 if (append_tab_response < 0) {
837 // The append tab failed. Remove the TabStripObserver
838 if (observer) {
839 RemoveTabStripObserver(observer);
840 delete observer;
841 }
842
843 // This will be reached only if the tab could not be appended. In case of a
844 // successful tab append, a successful navigation notification triggers the
845 // send.
846 Send(new AutomationMsg_AppendTabResponse(message.routing_id(),
847 append_tab_response));
848 }
849}
850
851void AutomationProvider::NavigateToURL(const IPC::Message& message,
852 int handle, const GURL& url) {
853 int status = AUTOMATION_MSG_NAVIGATION_ERROR;
854
855 if (tab_tracker_->ContainsHandle(handle)) {
856 NavigationController* tab = tab_tracker_->GetResource(handle);
857
858 // Simulate what a user would do. Activate the tab and then navigate.
859 // We could allow navigating in a background tab in future.
860 Browser* browser = FindAndActivateTab(tab);
861
862 if (browser) {
863 AddNavigationStatusListener(tab,
864 new AutomationMsg_NavigateToURLResponse(
865 message.routing_id(), AUTOMATION_MSG_NAVIGATION_SUCCESS),
866 new AutomationMsg_NavigateToURLResponse(
867 message.routing_id(), AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED));
868 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:50869 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29870 return;
871 }
872 }
873 Send(new AutomationMsg_NavigateToURLResponse(
874 message.routing_id(), AUTOMATION_MSG_NAVIGATION_ERROR));
875}
876
877void AutomationProvider::NavigationAsync(const IPC::Message& message,
878 int handle, const GURL& url) {
879 bool status = false;
880
881 if (tab_tracker_->ContainsHandle(handle)) {
882 NavigationController* tab = tab_tracker_->GetResource(handle);
883
884 // Simulate what a user would do. Activate the tab and then navigate.
885 // We could allow navigating in a background tab in future.
886 Browser* browser = FindAndActivateTab(tab);
887
888 if (browser) {
889 // Don't add any listener unless a callback mechanism is desired.
890 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:50891 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:29892 status = true;
893 }
894 }
895
896 Send(new AutomationMsg_NavigationAsyncResponse(message.routing_id(), status));
897}
898
899void AutomationProvider::GoBack(const IPC::Message& message, int handle) {
900 if (tab_tracker_->ContainsHandle(handle)) {
901 NavigationController* tab = tab_tracker_->GetResource(handle);
902 Browser* browser = FindAndActivateTab(tab);
903 if (browser && browser->IsCommandEnabled(IDC_BACK)) {
904 AddNavigationStatusListener(tab,
905 new AutomationMsg_GoBackResponse(
906 message.routing_id(), AUTOMATION_MSG_NAVIGATION_SUCCESS),
907 new AutomationMsg_GoBackResponse(
908 message.routing_id(), AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED));
909 browser->GoBack();
910 return;
911 }
912 }
913 Send(new AutomationMsg_GoBackResponse(message.routing_id(),
914 AUTOMATION_MSG_NAVIGATION_ERROR));
915}
916
917void AutomationProvider::GoForward(const IPC::Message& message, int handle) {
918 if (tab_tracker_->ContainsHandle(handle)) {
919 NavigationController* tab = tab_tracker_->GetResource(handle);
920 Browser* browser = FindAndActivateTab(tab);
921 if (browser && browser->IsCommandEnabled(IDC_FORWARD)) {
922 AddNavigationStatusListener(tab,
923 new AutomationMsg_GoForwardResponse(
924 message.routing_id(), AUTOMATION_MSG_NAVIGATION_SUCCESS),
925 new AutomationMsg_GoForwardResponse(
926 message.routing_id(), AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED));
927 browser->GoForward();
928 return;
929 }
930 }
931 Send(new AutomationMsg_GoForwardResponse(message.routing_id(),
932 AUTOMATION_MSG_NAVIGATION_ERROR));
933}
934
935void AutomationProvider::Reload(const IPC::Message& message, int handle) {
936 if (tab_tracker_->ContainsHandle(handle)) {
937 NavigationController* tab = tab_tracker_->GetResource(handle);
938 Browser* browser = FindAndActivateTab(tab);
939 if (browser && browser->IsCommandEnabled(IDC_RELOAD)) {
940 AddNavigationStatusListener(tab,
941 new AutomationMsg_ReloadResponse(
942 message.routing_id(), AUTOMATION_MSG_NAVIGATION_SUCCESS),
943 new AutomationMsg_ReloadResponse(
944 message.routing_id(), AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED));
945 browser->Reload();
946 return;
947 }
948 }
949 Send(new AutomationMsg_ReloadResponse(message.routing_id(),
950 AUTOMATION_MSG_NAVIGATION_ERROR));
951}
952
953void AutomationProvider::SetAuth(const IPC::Message& message, int tab_handle,
954 const std::wstring& username,
955 const std::wstring& password) {
956 int status = -1;
957
958 if (tab_tracker_->ContainsHandle(tab_handle)) {
959 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
960 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
961
962 if (iter != login_handler_map_.end()) {
963 // If auth is needed again after this, assume login has failed. This is
964 // not strictly correct, because a navigation can require both proxy and
965 // server auth, but it should be OK for now.
966 LoginHandler* handler = iter->second;
967 AddNavigationStatusListener(tab,
968 new AutomationMsg_SetAuthResponse(message.routing_id(), 0),
969 new AutomationMsg_SetAuthResponse(message.routing_id(), -1));
970 handler->SetAuth(username, password);
971 status = 0;
972 }
973 }
974 if (status < 0) {
975 Send(new AutomationMsg_SetAuthResponse(message.routing_id(), status));
976 }
977}
978
979void AutomationProvider::CancelAuth(const IPC::Message& message,
980 int tab_handle) {
981 int status = -1;
982
983 if (tab_tracker_->ContainsHandle(tab_handle)) {
984 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
985 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
986
987 if (iter != login_handler_map_.end()) {
988 // If auth is needed again after this, something is screwy.
989 LoginHandler* handler = iter->second;
990 AddNavigationStatusListener(tab,
991 new AutomationMsg_CancelAuthResponse(message.routing_id(), 0),
992 new AutomationMsg_CancelAuthResponse(message.routing_id(), -1));
993 handler->CancelAuth();
994 status = 0;
995 }
996 }
997 if (status < 0) {
998 Send(new AutomationMsg_CancelAuthResponse(message.routing_id(), status));
999 }
1000}
1001
1002void AutomationProvider::NeedsAuth(const IPC::Message& message,
1003 int tab_handle) {
1004 bool needs_auth = false;
1005
1006 if (tab_tracker_->ContainsHandle(tab_handle)) {
1007 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1008 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1009
1010 if (iter != login_handler_map_.end()) {
1011 // The LoginHandler will be in our map IFF the tab needs auth.
1012 needs_auth = true;
1013 }
1014 }
1015
1016 Send(new AutomationMsg_NeedsAuthResponse(message.routing_id(), needs_auth));
1017}
1018
1019void AutomationProvider::GetRedirectsFrom(const IPC::Message& message,
1020 int tab_handle,
1021 const GURL& source_url) {
1022 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
1023 if (tab_tracker_->ContainsHandle(tab_handle)) {
1024 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1025 HistoryService* history_service =
1026 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
1027
1028 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
1029 "has no history service";
1030 if (history_service) {
1031 // Schedule a history query for redirects. The response will be sent
1032 // asynchronously from the callback the history system uses to notify us
1033 // that it's done: OnRedirectQueryComplete.
1034 redirect_query_routing_id_ = message.routing_id();
1035 redirect_query_ = history_service->QueryRedirectsFrom(
1036 source_url, &consumer_,
1037 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
1038 return; // Response will be sent when query completes.
1039 }
1040 }
1041
1042 // Send failure response.
1043 IPC::Message* msg = new IPC::Message(
1044 message.routing_id(), AutomationMsg_RedirectsFromResponse::ID,
1045 IPC::Message::PRIORITY_NORMAL);
1046 msg->WriteInt(-1); // Negative string count indicates an error.
1047 Send(msg);
1048}
1049
1050void AutomationProvider::GetActiveTabIndex(const IPC::Message& message,
1051 int handle) {
1052 int active_tab_index = -1; // -1 is the error code
1053 if (browser_tracker_->ContainsHandle(handle)) {
1054 Browser* browser = browser_tracker_->GetResource(handle);
1055 active_tab_index = browser->selected_index();
1056 }
1057 Send(new AutomationMsg_ActiveTabIndexResponse(message.routing_id(),
1058 active_tab_index));
1059}
1060
1061void AutomationProvider::GetBrowserWindowCount(const IPC::Message& message) {
1062 Send(new AutomationMsg_BrowserWindowCountResponse(
1063 message.routing_id(), static_cast<int>(BrowserList::size())));
1064}
1065
1066void AutomationProvider::GetBrowserWindow(const IPC::Message& message,
1067 int index) {
1068 int handle = 0;
1069 if (index >= 0) {
1070 BrowserList::const_iterator iter = BrowserList::begin();
1071
1072 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
1073 if (iter != BrowserList::end()) {
1074 handle = browser_tracker_->Add(*iter);
1075 }
1076 }
1077
1078 Send(new AutomationMsg_BrowserWindowResponse(message.routing_id(), handle));
1079}
1080
1081void AutomationProvider::GetLastActiveBrowserWindow(
1082 const IPC::Message& message) {
1083 int handle = 0;
1084 Browser* browser = BrowserList::GetLastActive();
1085 if (browser)
1086 handle = browser_tracker_->Add(browser);
1087 Send(new AutomationMsg_LastActiveBrowserWindowResponse(message.routing_id(),
1088 handle));
1089}
1090
1091BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM l_param) {
1092 if (hwnd == reinterpret_cast<HWND>(l_param)) {
1093 return FALSE;
1094 }
1095 return TRUE;
1096}
1097
1098void AutomationProvider::GetActiveWindow(const IPC::Message& message) {
1099 HWND window = GetForegroundWindow();
1100
1101 // Let's make sure this window belongs to our process.
1102 if (EnumThreadWindows(::GetCurrentThreadId(),
1103 EnumThreadWndProc,
1104 reinterpret_cast<LPARAM>(window))) {
1105 // We enumerated all the windows and did not find the foreground window,
1106 // it is not our window, ignore it.
1107 Send(new AutomationMsg_ActiveWindowResponse(message.routing_id(), 0));
1108 return;
1109 }
1110
1111 int handle = window_tracker_->Add(window);
1112 Send(new AutomationMsg_ActiveWindowResponse(message.routing_id(), handle));
1113}
1114
1115void AutomationProvider::GetWindowHWND(const IPC::Message& message,
1116 int handle) {
1117 HWND win32_handle = window_tracker_->GetResource(handle);
1118 Send(new AutomationMsg_WindowHWNDResponse(message.routing_id(),
1119 win32_handle));
1120}
1121
[email protected]4ae62752008-08-04 23:28:471122void AutomationProvider::ExecuteBrowserCommand(const IPC::Message& message,
1123 int handle,
1124 int command) {
[email protected]4ae62752008-08-04 23:28:471125 bool success = false;
1126 if (browser_tracker_->ContainsHandle(handle)) {
1127 Browser* browser = browser_tracker_->GetResource(handle);
1128 if (browser->SupportsCommand(command) &&
1129 browser->IsCommandEnabled(command)) {
1130 browser->ExecuteCommand(command);
1131 success = true;
1132 }
1133 }
1134 Send(new AutomationMsg_WindowExecuteCommandResponse(message.routing_id(),
1135 success));
1136}
1137
initial.commit09911bf2008-07-26 23:55:291138void AutomationProvider::WindowGetViewBounds(const IPC::Message& message,
1139 int handle,
1140 int view_id,
1141 bool screen_coordinates) {
1142 bool succeeded = false;
[email protected]80f8b9f2008-10-16 18:17:471143 gfx::Rect bounds;
initial.commit09911bf2008-07-26 23:55:291144
1145 void* iter = NULL;
1146 if (window_tracker_->ContainsHandle(handle)) {
1147 HWND hwnd = window_tracker_->GetResource(handle);
[email protected]c2dacc92008-10-16 23:51:381148 views::RootView* root_view = views::ContainerWin::FindRootView(hwnd);
initial.commit09911bf2008-07-26 23:55:291149 if (root_view) {
[email protected]c2dacc92008-10-16 23:51:381150 views::View* view = root_view->GetViewByID(view_id);
initial.commit09911bf2008-07-26 23:55:291151 if (view) {
1152 succeeded = true;
[email protected]96b667d2008-10-14 20:58:441153 gfx::Point point;
initial.commit09911bf2008-07-26 23:55:291154 if (screen_coordinates)
[email protected]c2dacc92008-10-16 23:51:381155 views::View::ConvertPointToScreen(view, &point);
initial.commit09911bf2008-07-26 23:55:291156 else
[email protected]c2dacc92008-10-16 23:51:381157 views::View::ConvertPointToView(view, root_view, &point);
[email protected]80f8b9f2008-10-16 18:17:471158 bounds = view->GetLocalBounds(false);
1159 bounds.set_origin(point);
initial.commit09911bf2008-07-26 23:55:291160 }
1161 }
1162 }
1163
[email protected]80f8b9f2008-10-16 18:17:471164 Send(new AutomationMsg_WindowViewBoundsResponse(message.routing_id(),
1165 succeeded, bounds));
initial.commit09911bf2008-07-26 23:55:291166}
1167
1168// This task enqueues a mouse event on the event loop, so that the view
1169// that it's being sent to can do the requisite post-processing.
1170class MouseEventTask : public Task {
1171 public:
[email protected]c2dacc92008-10-16 23:51:381172 MouseEventTask(views::View* view,
1173 views::Event::EventType type,
initial.commit09911bf2008-07-26 23:55:291174 POINT point,
1175 int flags)
1176 : view_(view), type_(type), point_(point), flags_(flags) {}
1177 virtual ~MouseEventTask() {}
1178
1179 virtual void Run() {
[email protected]c2dacc92008-10-16 23:51:381180 views::MouseEvent event(type_, point_.x, point_.y, flags_);
initial.commit09911bf2008-07-26 23:55:291181 // We need to set the cursor position before we process the event because
1182 // some code (tab dragging, for instance) queries the actual cursor location
1183 // rather than the location of the mouse event. Note that the reason why
1184 // the drag code moved away from using mouse event locations was because
1185 // our conversion to screen location doesn't work well with multiple
1186 // monitors, so this only works reliably in a single monitor setup.
[email protected]96b667d2008-10-14 20:58:441187 gfx::Point screen_location(point_.x, point_.y);
initial.commit09911bf2008-07-26 23:55:291188 view_->ConvertPointToScreen(view_, &screen_location);
[email protected]96b667d2008-10-14 20:58:441189 ::SetCursorPos(screen_location.x(), screen_location.y());
initial.commit09911bf2008-07-26 23:55:291190 switch (type_) {
[email protected]c2dacc92008-10-16 23:51:381191 case views::Event::ET_MOUSE_PRESSED:
initial.commit09911bf2008-07-26 23:55:291192 view_->OnMousePressed(event);
1193 break;
1194
[email protected]c2dacc92008-10-16 23:51:381195 case views::Event::ET_MOUSE_DRAGGED:
initial.commit09911bf2008-07-26 23:55:291196 view_->OnMouseDragged(event);
1197 break;
1198
[email protected]c2dacc92008-10-16 23:51:381199 case views::Event::ET_MOUSE_RELEASED:
initial.commit09911bf2008-07-26 23:55:291200 view_->OnMouseReleased(event, false);
1201 break;
1202
1203 default:
1204 NOTREACHED();
1205 }
1206 }
1207
1208 private:
[email protected]c2dacc92008-10-16 23:51:381209 views::View* view_;
1210 views::Event::EventType type_;
initial.commit09911bf2008-07-26 23:55:291211 POINT point_;
1212 int flags_;
1213
[email protected]5a52f162008-08-27 04:15:311214 DISALLOW_COPY_AND_ASSIGN(MouseEventTask);
initial.commit09911bf2008-07-26 23:55:291215};
1216
[email protected]c2dacc92008-10-16 23:51:381217void AutomationProvider::ScheduleMouseEvent(views::View* view,
1218 views::Event::EventType type,
initial.commit09911bf2008-07-26 23:55:291219 POINT point,
1220 int flags) {
1221 MessageLoop::current()->PostTask(FROM_HERE,
1222 new MouseEventTask(view, type, point, flags));
1223}
1224
1225// This task just adds another task to the event queue. This is useful if
1226// you want to ensure that any tasks added to the event queue after this one
1227// have already been processed by the time |task| is run.
1228class InvokeTaskLaterTask : public Task {
1229 public:
1230 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
1231 virtual ~InvokeTaskLaterTask() {}
1232
1233 virtual void Run() {
1234 MessageLoop::current()->PostTask(FROM_HERE, task_);
1235 }
1236
1237 private:
1238 Task* task_;
1239
[email protected]5a52f162008-08-27 04:15:311240 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
initial.commit09911bf2008-07-26 23:55:291241};
1242
1243// This task sends a WindowDragResponse message with the appropriate
1244// routing ID to the automation proxy. This is implemented as a task so that
1245// we know that the mouse events (and any tasks that they spawn on the message
1246// loop) have been processed by the time this is sent.
1247class WindowDragResponseTask : public Task {
1248 public:
1249 WindowDragResponseTask(AutomationProvider* provider, int routing_id)
1250 : provider_(provider), routing_id_(routing_id) {}
1251 virtual ~WindowDragResponseTask() {}
1252
1253 virtual void Run() {
1254 provider_->Send(new AutomationMsg_WindowDragResponse(routing_id_, true));
1255 }
1256
1257 private:
1258 AutomationProvider* provider_;
1259 int routing_id_;
1260
[email protected]5a52f162008-08-27 04:15:311261 DISALLOW_COPY_AND_ASSIGN(WindowDragResponseTask);
initial.commit09911bf2008-07-26 23:55:291262};
1263
1264void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
1265 int handle,
1266 POINT click,
1267 int flags) {
1268 HWND hwnd = 0;
1269
1270 if (window_tracker_->ContainsHandle(handle)) {
1271 hwnd = window_tracker_->GetResource(handle);
1272
initial.commit09911bf2008-07-26 23:55:291273 ui_controls::SendMouseMove(click.x, click.y);
1274
1275 ui_controls::MouseButton button = ui_controls::LEFT;
[email protected]c2dacc92008-10-16 23:51:381276 if ((flags & views::Event::EF_LEFT_BUTTON_DOWN) ==
1277 views::Event::EF_LEFT_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291278 button = ui_controls::LEFT;
[email protected]c2dacc92008-10-16 23:51:381279 } else if ((flags & views::Event::EF_RIGHT_BUTTON_DOWN) ==
1280 views::Event::EF_RIGHT_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291281 button = ui_controls::RIGHT;
[email protected]c2dacc92008-10-16 23:51:381282 } else if ((flags & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
1283 views::Event::EF_MIDDLE_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291284 button = ui_controls::MIDDLE;
1285 } else {
1286 NOTREACHED();
1287 }
1288 ui_controls::SendMouseClick(button);
1289 }
1290}
1291
1292void AutomationProvider::WindowSimulateDrag(const IPC::Message& message,
1293 int handle,
1294 std::vector<POINT> drag_path,
[email protected]5e0f30c2008-08-14 22:52:441295 int flags,
1296 bool press_escape_en_route) {
initial.commit09911bf2008-07-26 23:55:291297 bool succeeded = false;
1298 if (browser_tracker_->ContainsHandle(handle) && (drag_path.size() > 1)) {
1299 succeeded = true;
1300
[email protected]0307076a2008-08-01 20:42:271301 UINT down_message = 0;
1302 UINT up_message = 0;
1303 WPARAM wparam_flags = 0;
[email protected]c2dacc92008-10-16 23:51:381304 if (flags & views::Event::EF_SHIFT_DOWN)
[email protected]0307076a2008-08-01 20:42:271305 wparam_flags |= MK_SHIFT;
[email protected]c2dacc92008-10-16 23:51:381306 if (flags & views::Event::EF_CONTROL_DOWN)
[email protected]0307076a2008-08-01 20:42:271307 wparam_flags |= MK_CONTROL;
[email protected]c2dacc92008-10-16 23:51:381308 if (flags & views::Event::EF_LEFT_BUTTON_DOWN) {
[email protected]0307076a2008-08-01 20:42:271309 wparam_flags |= MK_LBUTTON;
1310 down_message = WM_LBUTTONDOWN;
1311 up_message = WM_LBUTTONUP;
1312 }
[email protected]c2dacc92008-10-16 23:51:381313 if (flags & views::Event::EF_MIDDLE_BUTTON_DOWN) {
[email protected]0307076a2008-08-01 20:42:271314 wparam_flags |= MK_MBUTTON;
1315 down_message = WM_MBUTTONDOWN;
1316 up_message = WM_MBUTTONUP;
1317 }
[email protected]c2dacc92008-10-16 23:51:381318 if (flags & views::Event::EF_RIGHT_BUTTON_DOWN) {
[email protected]0307076a2008-08-01 20:42:271319 wparam_flags |= MK_RBUTTON;
1320 down_message = WM_LBUTTONDOWN;
1321 up_message = WM_LBUTTONUP;
1322 }
1323
initial.commit09911bf2008-07-26 23:55:291324 Browser* browser = browser_tracker_->GetResource(handle);
1325 DCHECK(browser);
[email protected]0307076a2008-08-01 20:42:271326 HWND top_level_hwnd = browser->GetTopLevelHWND();
1327 SetCursorPos(drag_path[0].x, drag_path[0].y);
1328 SendMessage(top_level_hwnd, down_message, wparam_flags,
1329 MAKELPARAM(drag_path[0].x, drag_path[0].y));
initial.commit09911bf2008-07-26 23:55:291330 for (int i = 1; i < static_cast<int>(drag_path.size()); ++i) {
[email protected]0307076a2008-08-01 20:42:271331 SetCursorPos(drag_path[i].x, drag_path[i].y);
1332 SendMessage(top_level_hwnd, WM_MOUSEMOVE, wparam_flags,
1333 MAKELPARAM(drag_path[i].x, drag_path[i].y));
initial.commit09911bf2008-07-26 23:55:291334 }
1335 POINT end = drag_path[drag_path.size() - 1];
[email protected]0307076a2008-08-01 20:42:271336 SetCursorPos(end.x, end.y);
[email protected]5e0f30c2008-08-14 22:52:441337
1338 if (press_escape_en_route) {
1339 // Press Escape.
1340 ui_controls::SendKeyPress(VK_ESCAPE,
[email protected]c2dacc92008-10-16 23:51:381341 ((flags & views::Event::EF_CONTROL_DOWN)
1342 == views::Event::EF_CONTROL_DOWN),
1343 ((flags & views::Event::EF_SHIFT_DOWN) ==
1344 views::Event::EF_SHIFT_DOWN),
1345 ((flags & views::Event::EF_ALT_DOWN) ==
1346 views::Event::EF_ALT_DOWN));
[email protected]5e0f30c2008-08-14 22:52:441347 }
[email protected]0307076a2008-08-01 20:42:271348 SendMessage(top_level_hwnd, up_message, wparam_flags,
1349 MAKELPARAM(end.x, end.y));
initial.commit09911bf2008-07-26 23:55:291350
1351 MessageLoop::current()->PostTask(FROM_HERE,
1352 new InvokeTaskLaterTask(
1353 new WindowDragResponseTask(this, message.routing_id())));
1354 } else {
1355 Send(new AutomationMsg_WindowDragResponse(message.routing_id(), true));
1356 }
1357}
1358
1359void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
1360 int handle,
1361 wchar_t key,
1362 int flags) {
1363 if (!window_tracker_->ContainsHandle(handle))
1364 return;
1365
1366 // The key event is sent to whatever window is active.
1367 ui_controls::SendKeyPress(key,
[email protected]c2dacc92008-10-16 23:51:381368 ((flags & views::Event::EF_CONTROL_DOWN) ==
1369 views::Event::EF_CONTROL_DOWN),
1370 ((flags & views::Event::EF_SHIFT_DOWN) ==
1371 views::Event::EF_SHIFT_DOWN),
1372 ((flags & views::Event::EF_ALT_DOWN) ==
1373 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:291374}
1375
1376void AutomationProvider::GetFocusedViewID(const IPC::Message& message,
1377 int handle) {
1378 int view_id = -1;
1379 if (window_tracker_->ContainsHandle(handle)) {
1380 HWND hwnd = window_tracker_->GetResource(handle);
[email protected]c2dacc92008-10-16 23:51:381381 views::FocusManager* focus_manager =
1382 views::FocusManager::GetFocusManager(hwnd);
initial.commit09911bf2008-07-26 23:55:291383 DCHECK(focus_manager);
[email protected]c2dacc92008-10-16 23:51:381384 views::View* focused_view = focus_manager->GetFocusedView();
initial.commit09911bf2008-07-26 23:55:291385 if (focused_view)
1386 view_id = focused_view->GetID();
1387 }
1388 Send(new AutomationMsg_GetFocusedViewIDResponse(message.routing_id(),
1389 view_id));
1390}
1391
1392void AutomationProvider::SetWindowVisible(const IPC::Message& message,
1393 int handle, bool visible) {
1394 if (window_tracker_->ContainsHandle(handle)) {
1395 HWND hwnd = window_tracker_->GetResource(handle);
1396 ::ShowWindow(hwnd, visible ? SW_SHOW : SW_HIDE);
1397 Send(new AutomationMsg_SetWindowVisibleResponse(message.routing_id(),
1398 true));
1399 } else {
1400 Send(new AutomationMsg_SetWindowVisibleResponse(message.routing_id(),
1401 false));
1402 }
1403}
1404
1405void AutomationProvider::IsWindowActive(const IPC::Message& message,
1406 int handle) {
1407 if (window_tracker_->ContainsHandle(handle)) {
1408 HWND hwnd = window_tracker_->GetResource(handle);
1409 bool is_active = ::GetForegroundWindow() == hwnd;
1410 Send(new AutomationMsg_IsWindowActiveResponse(
1411 message.routing_id(), true, is_active));
1412 } else {
1413 Send(new AutomationMsg_IsWindowActiveResponse(message.routing_id(),
1414 false, false));
1415 }
1416}
1417
1418void AutomationProvider::ActivateWindow(const IPC::Message& message,
1419 int handle) {
1420 if (window_tracker_->ContainsHandle(handle)) {
1421 ::SetActiveWindow(window_tracker_->GetResource(handle));
1422 }
1423}
1424
1425void AutomationProvider::GetTabCount(const IPC::Message& message, int handle) {
1426 int tab_count = -1; // -1 is the error code
1427
1428 if (browser_tracker_->ContainsHandle(handle)) {
1429 Browser* browser = browser_tracker_->GetResource(handle);
1430 tab_count = browser->tab_count();
1431 }
1432
1433 Send(new AutomationMsg_TabCountResponse(message.routing_id(), tab_count));
1434}
1435
1436void AutomationProvider::GetTab(const IPC::Message& message,
1437 int win_handle, int tab_index) {
1438 void* iter = NULL;
1439 int tab_handle = 0;
1440 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1441 Browser* browser = browser_tracker_->GetResource(win_handle);
1442 if (tab_index < browser->tab_count()) {
1443 TabContents* tab_contents =
1444 browser->GetTabContentsAt(tab_index);
1445 tab_handle = tab_tracker_->Add(tab_contents->controller());
1446 }
1447 }
1448
1449 Send(new AutomationMsg_TabResponse(message.routing_id(), tab_handle));
1450}
1451
1452void AutomationProvider::GetTabTitle(const IPC::Message& message, int handle) {
1453 int title_string_size = -1; // -1 is the error code
1454 std::wstring title;
1455 if (tab_tracker_->ContainsHandle(handle)) {
1456 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]1e5645ff2008-08-27 18:09:071457 title = tab->GetActiveEntry()->title();
initial.commit09911bf2008-07-26 23:55:291458 title_string_size = static_cast<int>(title.size());
1459 }
1460
1461 Send(new AutomationMsg_TabTitleResponse(message.routing_id(),
1462 title_string_size, title));
1463}
1464
1465void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1466 if (window_tracker_->ContainsHandle(handle)) {
1467 window_tracker_->Remove(window_tracker_->GetResource(handle));
1468 }
1469}
1470
1471void AutomationProvider::OnChannelError() {
1472 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571473 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291474}
1475
1476// TODO(brettw) change this to accept GURLs when history supports it
1477void AutomationProvider::OnRedirectQueryComplete(
1478 HistoryService::Handle request_handle,
1479 GURL from_url,
1480 bool success,
1481 HistoryService::RedirectList* redirects) {
1482 DCHECK(request_handle == redirect_query_);
1483
1484 // Respond to the pending request for the redirect list.
1485 IPC::Message* msg = new IPC::Message(redirect_query_routing_id_,
1486 AutomationMsg_RedirectsFromResponse::ID,
1487 IPC::Message::PRIORITY_NORMAL);
1488 if (success) {
1489 msg->WriteInt(static_cast<int>(redirects->size()));
1490 for (size_t i = 0; i < redirects->size(); i++)
1491 IPC::ParamTraits<GURL>::Write(msg, redirects->at(i));
1492 } else {
1493 msg->WriteInt(-1); // Negative count indicates failure.
1494 }
1495
1496 Send(msg);
1497 redirect_query_ = NULL;
1498}
1499
1500bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571501 DCHECK(channel_.get());
1502 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291503}
1504
1505Browser* AutomationProvider::FindAndActivateTab(
1506 NavigationController* controller) {
1507 int tab_index;
1508 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1509 if (browser)
1510 browser->SelectTabContentsAt(tab_index, true);
1511
1512 return browser;
1513}
1514
1515void AutomationProvider::GetCookies(const IPC::Message& message,
1516 const GURL& url, int handle) {
1517 std::string value;
1518
1519 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1520 NavigationController* tab = tab_tracker_->GetResource(handle);
1521 value =
1522 tab->profile()->GetRequestContext()->cookie_store()->GetCookies(url);
1523 }
1524
1525 Send(new AutomationMsg_GetCookiesResponse(message.routing_id(),
1526 static_cast<int>(value.size()), value));
1527}
1528
1529void AutomationProvider::SetCookie(const IPC::Message& message,
1530 const GURL& url,
1531 const std::string value,
1532 int handle) {
1533 int response_value = -1;
1534
1535 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1536 NavigationController* tab = tab_tracker_->GetResource(handle);
1537 URLRequestContext* context = tab->profile()->GetRequestContext();
1538 if (context->cookie_store()->SetCookie(url, value))
1539 response_value = 1;
1540 }
1541
1542 Send(new AutomationMsg_SetCookieResponse(message.routing_id(),
1543 response_value));
1544}
1545
1546void AutomationProvider::GetTabURL(const IPC::Message& message, int handle) {
1547 bool success = false;
1548 GURL url;
1549 if (tab_tracker_->ContainsHandle(handle)) {
1550 NavigationController* tab = tab_tracker_->GetResource(handle);
1551 // Return what the user would see in the location bar.
[email protected]1e5645ff2008-08-27 18:09:071552 url = tab->GetActiveEntry()->display_url();
initial.commit09911bf2008-07-26 23:55:291553 success = true;
1554 }
1555
1556 Send(new AutomationMsg_TabURLResponse(message.routing_id(), success, url));
1557}
1558
1559void AutomationProvider::GetTabHWND(const IPC::Message& message, int handle) {
1560 HWND tab_hwnd = NULL;
1561
1562 if (tab_tracker_->ContainsHandle(handle)) {
1563 NavigationController* tab = tab_tracker_->GetResource(handle);
1564 tab_hwnd = tab->active_contents()->GetContainerHWND();
1565 }
1566
1567 Send(new AutomationMsg_TabHWNDResponse(message.routing_id(), tab_hwnd));
1568}
1569
1570void AutomationProvider::GetTabProcessID(
1571 const IPC::Message& message, int handle) {
1572 int process_id = -1;
1573
1574 if (tab_tracker_->ContainsHandle(handle)) {
1575 process_id = 0;
1576 NavigationController* tab = tab_tracker_->GetResource(handle);
1577 if (tab->active_contents()->AsWebContents()) {
1578 WebContents* web_contents = tab->active_contents()->AsWebContents();
1579 if (web_contents->process()) {
1580 process_id =
1581 process_util::GetProcId(web_contents->process()->process());
1582 }
1583 }
1584 }
1585
1586 Send(new AutomationMsg_TabProcessIDResponse(message.routing_id(),
1587 process_id));
1588}
1589
1590void AutomationProvider::ApplyAccelerator(int handle, int id) {
1591 if (browser_tracker_->ContainsHandle(handle)) {
1592 Browser* browser = browser_tracker_->GetResource(handle);
1593 browser->controller()->ExecuteCommand(id);
1594 }
1595}
1596
1597void AutomationProvider::ExecuteJavascript(const IPC::Message& message,
1598 int handle,
1599 const std::wstring& frame_xpath,
1600 const std::wstring& script) {
1601 bool succeeded = false;
[email protected]20e93d12008-08-28 16:31:571602 WebContents* web_contents = GetWebContentsForHandle(handle, NULL);
1603 if (web_contents) {
1604 // Set the routing id of this message with the controller.
1605 // This routing id needs to be remembered for the reverse
1606 // communication while sending back the response of
1607 // this javascript execution.
1608 std::wstring url;
1609 SStringPrintf(&url,
1610 L"javascript:void(window.domAutomationController.setAutomationId(%d));",
1611 message.routing_id());
initial.commit09911bf2008-07-26 23:55:291612
[email protected]1f5af4442008-09-25 22:11:061613 web_contents->render_view_host()->ExecuteJavascriptInWebFrame(
1614 frame_xpath, url);
1615 web_contents->render_view_host()->ExecuteJavascriptInWebFrame(
1616 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571617 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291618 }
1619
1620 if (!succeeded) {
1621 Send(new AutomationMsg_DomOperationResponse(message.routing_id(), ""));
1622 }
1623}
1624
1625void AutomationProvider::GetShelfVisibility(const IPC::Message& message,
1626 int handle) {
1627 bool visible = false;
[email protected]20e93d12008-08-28 16:31:571628
1629 WebContents* web_contents = GetWebContentsForHandle(handle, NULL);
1630 if (web_contents)
1631 visible = web_contents->IsDownloadShelfVisible();
initial.commit09911bf2008-07-26 23:55:291632
1633 Send(new AutomationMsg_ShelfVisibilityResponse(message.routing_id(),
1634 visible));
1635}
1636
1637void AutomationProvider::GetConstrainedWindowCount(const IPC::Message& message,
1638 int handle) {
1639 int count = -1; // -1 is the error code
1640 if (tab_tracker_->ContainsHandle(handle)) {
1641 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
1642 TabContents* tab_contents = nav_controller->active_contents();
1643 if (tab_contents) {
1644 count = static_cast<int>(tab_contents->child_windows_.size());
1645 }
1646 }
1647
1648 Send(new AutomationMsg_ConstrainedWindowCountResponse(message.routing_id(),
1649 count));
1650}
1651
1652void AutomationProvider::GetConstrainedWindow(const IPC::Message& message,
1653 int handle, int index) {
1654 int cwindow_handle = 0;
1655 if (tab_tracker_->ContainsHandle(handle) && index >= 0) {
1656 NavigationController* nav_controller =
1657 tab_tracker_->GetResource(handle);
1658 TabContents* tab = nav_controller->active_contents();
[email protected]d5f942ba2008-09-26 19:30:341659 if (tab && index < static_cast<int>(tab->child_windows_.size())) {
1660 ConstrainedWindow* window = tab->child_windows_[index];
initial.commit09911bf2008-07-26 23:55:291661 cwindow_handle = cwindow_tracker_->Add(window);
1662 }
1663 }
1664
1665 Send(new AutomationMsg_ConstrainedWindowResponse(message.routing_id(),
1666 cwindow_handle));
1667}
1668
1669void AutomationProvider::GetConstrainedTitle(const IPC::Message& message,
1670 int handle) {
1671 int title_string_size = -1; // -1 is the error code
1672 std::wstring title;
1673 if (cwindow_tracker_->ContainsHandle(handle)) {
1674 ConstrainedWindow* window = cwindow_tracker_->GetResource(handle);
1675 title = window->GetWindowTitle();
1676 title_string_size = static_cast<int>(title.size());
1677 }
1678
1679 Send(new AutomationMsg_ConstrainedTitleResponse(message.routing_id(),
1680 title_string_size, title));
1681}
1682
1683void AutomationProvider::GetConstrainedWindowBounds(const IPC::Message& message,
1684 int handle) {
1685 bool exists = false;
1686 gfx::Rect rect(0, 0, 0, 0);
1687 if (cwindow_tracker_->ContainsHandle(handle)) {
1688 ConstrainedWindow* window = cwindow_tracker_->GetResource(handle);
1689 if (window) {
1690 exists = true;
1691 rect = window->GetCurrentBounds();
1692 }
1693 }
1694
1695 Send(new AutomationMsg_ConstrainedWindowBoundsResponse(message.routing_id(),
1696 exists, rect));
1697}
1698
1699void AutomationProvider::HandleFindInPageRequest(
1700 const IPC::Message& message, int handle, const std::wstring& find_request,
1701 int forward, int match_case) {
[email protected]5a52f162008-08-27 04:15:311702 NOTREACHED() << "This function has been deprecated."
1703 << "Please use HandleFindRequest instead.";
1704 Send(new AutomationMsg_FindInPageResponse(message.routing_id(), -1));
1705 return;
1706}
1707
1708void AutomationProvider::HandleFindRequest(const IPC::Message& message,
1709 int handle, const FindInPageRequest& request) {
initial.commit09911bf2008-07-26 23:55:291710 if (!tab_tracker_->ContainsHandle(handle)) {
1711 Send(new AutomationMsg_FindInPageResponse(message.routing_id(), -1));
1712 return;
1713 }
1714
1715 NavigationController* nav = tab_tracker_->GetResource(handle);
1716 TabContents* tab_contents = nav->active_contents();
1717
1718 find_in_page_observer_.reset(new
1719 FindInPageNotificationObserver(this, tab_contents, message.routing_id()));
1720
[email protected]edc28612008-08-14 20:23:361721 // The find in page dialog must be up for us to get the notification that the
[email protected]9e0534b2008-10-21 15:03:011722 // find was complete.
1723 WebContents* web_contents = tab_contents->AsWebContents();
1724 if (web_contents) {
[email protected]edc28612008-08-14 20:23:361725 NavigationController* tab = tab_tracker_->GetResource(handle);
1726 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]9e0534b2008-10-21 15:03:011727 web_contents->view()->FindInPage(*browser, true, request.forward);
[email protected]edc28612008-08-14 20:23:361728
[email protected]9e0534b2008-10-21 15:03:011729 web_contents->render_view_host()->StartFinding(
1730 FindInPageNotificationObserver::kFindInPageRequestId,
1731 request.search_string, request.forward, request.match_case,
1732 request.find_next);
1733 }
initial.commit09911bf2008-07-26 23:55:291734}
1735
[email protected]5f8af2a2008-08-06 22:49:451736void AutomationProvider::HandleOpenFindInPageRequest(
1737 const IPC::Message& message, int handle) {
[email protected]20e93d12008-08-28 16:31:571738 NavigationController* tab = NULL;
1739 WebContents* web_contents = GetWebContentsForHandle(handle, &tab);
1740 if (web_contents) {
[email protected]5f8af2a2008-08-06 22:49:451741 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]9e0534b2008-10-21 15:03:011742 web_contents->view()->FindInPage(*browser, false, false);
[email protected]5f8af2a2008-08-06 22:49:451743 }
1744}
1745
[email protected]20e93d12008-08-28 16:31:571746void AutomationProvider::GetFindWindowVisibility(const IPC::Message& message,
1747 int handle) {
[email protected]9e0534b2008-10-21 15:03:011748 gfx::Point position;
[email protected]20e93d12008-08-28 16:31:571749 bool visible = false;
1750 WebContents* web_contents = GetWebContentsForHandle(handle, NULL);
1751 if (web_contents)
[email protected]6fd1a4e22008-10-21 18:08:091752 web_contents->view()->GetFindBarWindowInfo(&position, &visible);
[email protected]20e93d12008-08-28 16:31:571753
1754 Send(new AutomationMsg_FindWindowVisibilityResponse(message.routing_id(),
1755 visible));
1756}
1757
1758void AutomationProvider::HandleFindWindowLocationRequest(
1759 const IPC::Message& message, int handle) {
[email protected]9e0534b2008-10-21 15:03:011760 gfx::Point position(0, 0);
1761 bool visible = false;
[email protected]20e93d12008-08-28 16:31:571762 WebContents* web_contents = GetWebContentsForHandle(handle, NULL);
1763 if (web_contents)
[email protected]6fd1a4e22008-10-21 18:08:091764 web_contents->view()->GetFindBarWindowInfo(&position, &visible);
[email protected]20e93d12008-08-28 16:31:571765
1766 Send(new AutomationMsg_FindWindowLocationResponse(message.routing_id(),
[email protected]9e0534b2008-10-21 15:03:011767 position.x(),
1768 position.y()));
[email protected]20e93d12008-08-28 16:31:571769}
1770
[email protected]c2cbeb92008-09-05 21:36:571771void AutomationProvider::GetBookmarkBarVisitility(const IPC::Message& message,
1772 int handle) {
1773 bool visible = false;
1774 bool animating = false;
1775
1776 void* iter = NULL;
1777 if (browser_tracker_->ContainsHandle(handle)) {
1778 Browser* browser = browser_tracker_->GetResource(handle);
1779 if (browser) {
1780 BookmarkBarView* bookmark_bar = browser->window()->GetBookmarkBarView();
1781 if (bookmark_bar) {
1782 animating = bookmark_bar->IsAnimating();
1783 visible = browser->window()->IsBookmarkBarVisible();
1784 }
1785 }
1786 }
1787
1788 Send(new AutomationMsg_BookmarkBarVisibilityResponse(message.routing_id(),
1789 visible, animating));
1790}
1791
initial.commit09911bf2008-07-26 23:55:291792void AutomationProvider::HandleInspectElementRequest(
1793 const IPC::Message& message, int handle, int x, int y) {
[email protected]20e93d12008-08-28 16:31:571794 WebContents* web_contents = GetWebContentsForHandle(handle, NULL);
1795 if (web_contents) {
[email protected]1f5af4442008-09-25 22:11:061796 web_contents->render_view_host()->InspectElementAt(x, y);
initial.commit09911bf2008-07-26 23:55:291797 inspect_element_routing_id_ = message.routing_id();
1798 } else {
1799 Send(new AutomationMsg_InspectElementResponse(message.routing_id(), -1));
1800 }
1801}
1802
1803void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
1804 Send(new AutomationMsg_InspectElementResponse(inspect_element_routing_id_,
1805 num_resources));
1806}
1807
1808// Helper class for making changes to the URLRequest ProtocolFactory on the
1809// IO thread.
1810class SetFilteredInetTask : public Task {
1811 public:
1812 explicit SetFilteredInetTask(bool enabled) : enabled_(enabled) { }
1813 virtual void Run() {
1814 if (enabled_) {
1815 URLRequestFilter::GetInstance()->ClearHandlers();
1816
1817 URLRequestFailedDnsJob::AddUITestUrls();
1818 URLRequestSlowDownloadJob::AddUITestUrls();
1819
1820 std::wstring root_http;
1821 PathService::Get(chrome::DIR_TEST_DATA, &root_http);
1822 URLRequestMockHTTPJob::AddUITestUrls(root_http);
1823 } else {
1824 // Revert to the default handlers.
1825 URLRequestFilter::GetInstance()->ClearHandlers();
1826 }
1827 }
1828 private:
1829 bool enabled_;
1830};
1831
1832void AutomationProvider::SetFilteredInet(const IPC::Message& message,
1833 bool enabled) {
1834 // Since this involves changing the URLRequest ProtocolFactory, we want to
1835 // run on the main thread.
1836 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
1837 new SetFilteredInetTask(enabled));
1838}
1839
1840void AutomationProvider::GetDownloadDirectory(const IPC::Message& message,
1841 int handle) {
1842 DLOG(INFO) << "Handling download directory request";
1843 std::wstring download_directory;
1844 if (tab_tracker_->ContainsHandle(handle)) {
1845 NavigationController* tab = tab_tracker_->GetResource(handle);
1846 DownloadManager* dlm = tab->profile()->GetDownloadManager();
1847 DCHECK(dlm);
1848 download_directory = dlm->download_path();
1849 }
1850
1851 Send(new AutomationMsg_DownloadDirectoryResponse(message.routing_id(),
1852 download_directory));
1853}
1854
1855void AutomationProvider::OpenNewBrowserWindow(int show_command) {
1856 // We may have no current browser windows open so don't rely on
1857 // asking an existing browser to execute the IDC_NEWWINDOW command
1858 Browser::OpenNewBrowserWindow(profile_, show_command);
1859}
1860
1861void AutomationProvider::GetWindowForBrowser(const IPC::Message& message,
1862 int browser_handle) {
1863 bool success = false;
1864 int window_handle = 0;
1865
1866 if (browser_tracker_->ContainsHandle(browser_handle)) {
1867 Browser* browser = browser_tracker_->GetResource(browser_handle);
1868 HWND hwnd = browser->GetTopLevelHWND();
1869 // Add() returns the existing handle for the resource if any.
1870 window_handle = window_tracker_->Add(hwnd);
1871 success = true;
1872 }
1873 Send(new AutomationMsg_WindowForBrowserResponse(message.routing_id(),
1874 success, window_handle));
1875}
1876
1877void AutomationProvider::GetAutocompleteEditForBrowser(
1878 const IPC::Message& message,
1879 int browser_handle) {
1880 bool success = false;
1881 int autocomplete_edit_handle = 0;
1882
1883 if (browser_tracker_->ContainsHandle(browser_handle)) {
1884 Browser* browser = browser_tracker_->GetResource(browser_handle);
1885 LocationBarView* loc_bar_view = browser->GetLocationBarView();
[email protected]81c21222008-09-10 19:35:521886 AutocompleteEditView* edit_view = loc_bar_view->location_entry();
initial.commit09911bf2008-07-26 23:55:291887 // Add() returns the existing handle for the resource if any.
[email protected]81c21222008-09-10 19:35:521888 autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
initial.commit09911bf2008-07-26 23:55:291889 success = true;
1890 }
1891 Send(new AutomationMsg_AutocompleteEditForBrowserResponse(
1892 message.routing_id(), success, autocomplete_edit_handle));
1893}
1894
1895void AutomationProvider::GetBrowserForWindow(const IPC::Message& message,
1896 int window_handle) {
1897 bool success = false;
1898 int browser_handle = 0;
1899
1900 if (window_tracker_->ContainsHandle(window_handle)) {
1901 HWND window = window_tracker_->GetResource(window_handle);
1902 BrowserList::const_iterator iter = BrowserList::begin();
1903 Browser* browser = NULL;
1904 for (;iter != BrowserList::end(); ++iter) {
1905 if (window == (*iter)->GetTopLevelHWND()) {
1906 browser = *iter;
1907 break;
1908 }
1909 }
1910 if (browser) {
1911 // Add() returns the existing handle for the resource if any.
1912 browser_handle = browser_tracker_->Add(browser);
1913 success = true;
1914 }
1915 }
1916 Send(new AutomationMsg_BrowserForWindowResponse(message.routing_id(),
1917 success, browser_handle));
1918}
1919
1920void AutomationProvider::ShowInterstitialPage(const IPC::Message& message,
1921 int tab_handle,
1922 const std::string& html_text) {
1923 if (tab_tracker_->ContainsHandle(tab_handle)) {
1924 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1925 TabContents* tab_contents = controller->active_contents();
1926 if (tab_contents->type() == TAB_CONTENTS_WEB) {
1927 AddNavigationStatusListener(controller,
1928 new AutomationMsg_ShowInterstitialPageResponse(message.routing_id(),
1929 true),
1930 NULL);
1931 WebContents* web_contents = tab_contents->AsWebContents();
[email protected]cbab76d2008-10-13 22:42:471932 AutomationInterstitialPage* interstitial =
1933 new AutomationInterstitialPage(web_contents,
1934 GURL("about:interstitial"),
1935 html_text);
1936 web_contents->ShowInterstitialPage(interstitial);
initial.commit09911bf2008-07-26 23:55:291937 return;
1938 }
1939 }
1940 Send(new AutomationMsg_ShowInterstitialPageResponse(message.routing_id(),
1941 false));
1942}
1943
1944void AutomationProvider::HideInterstitialPage(const IPC::Message& message,
1945 int tab_handle) {
[email protected]20e93d12008-08-28 16:31:571946 WebContents* web_contents = GetWebContentsForHandle(tab_handle, NULL);
1947 if (web_contents) {
1948 web_contents->HideInterstitialPage(false, false);
1949 Send(new AutomationMsg_HideInterstitialPageResponse(message.routing_id(),
1950 true));
1951 return;
initial.commit09911bf2008-07-26 23:55:291952 }
1953 Send(new AutomationMsg_HideInterstitialPageResponse(message.routing_id(),
1954 false));
1955}
1956
1957void AutomationProvider::CloseTab(const IPC::Message& message,
1958 int tab_handle,
1959 bool wait_until_closed) {
1960 if (tab_tracker_->ContainsHandle(tab_handle)) {
1961 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
1962 int index;
1963 Browser* browser = Browser::GetBrowserForController(controller, &index);
1964 DCHECK(browser);
1965 TabClosedNotificationObserver* observer =
1966 new TabClosedNotificationObserver(browser, this, message.routing_id(),
1967 wait_until_closed);
1968 browser->CloseContents(controller->active_contents());
1969 } else {
1970 Send(new AutomationMsg_CloseTabResponse(message.routing_id(), false));
1971 }
1972}
1973
1974void AutomationProvider::CloseBrowser(const IPC::Message& message,
1975 int browser_handle) {
1976 if (browser_tracker_->ContainsHandle(browser_handle)) {
1977 Browser* browser = browser_tracker_->GetResource(browser_handle);
1978 new BrowserClosedNotificationObserver(browser, this, message.routing_id());
[email protected]f3e99e32008-07-30 04:48:391979 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:291980 } else {
1981 NOTREACHED();
1982 }
1983}
1984
1985void AutomationProvider::CreateExternalTab(const IPC::Message& message) {
1986 int tab_handle = 0;
1987 HWND tab_container_window = NULL;
1988 ExternalTabContainer *external_tab_container =
1989 new ExternalTabContainer(this);
1990 external_tab_container->Init(profile_);
1991 TabContents* tab_contents = external_tab_container->tab_contents();
1992 if (tab_contents) {
1993 tab_handle = tab_tracker_->Add(tab_contents->controller());
1994 tab_container_window = *external_tab_container;
1995 }
1996 Send(new AutomationMsg_CreateExternalTabResponse(message.routing_id(),
1997 tab_container_window,
1998 tab_handle));
1999}
2000
2001void AutomationProvider::NavigateInExternalTab(const IPC::Message& message,
2002 int handle, const GURL& url) {
2003 bool status = false;
2004
2005 if (tab_tracker_->ContainsHandle(handle)) {
2006 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c0588052008-10-27 23:01:502007 tab->LoadURL(url, GURL(), PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:292008 status = true;
2009 }
2010
2011 Send(new AutomationMsg_NavigateInExternalTabResponse(message.routing_id(),
2012 status));
2013}
2014
2015void AutomationProvider::SetAcceleratorsForTab(const IPC::Message& message,
2016 int handle,
2017 HACCEL accel_table,
2018 int accel_entry_count) {
2019 bool status = false;
2020 if (tab_tracker_->ContainsHandle(handle)) {
2021 NavigationController* tab = tab_tracker_->GetResource(handle);
2022 TabContents* tab_contents = tab->GetTabContents(TAB_CONTENTS_WEB);
2023 ExternalTabContainer* external_tab_container =
2024 ExternalTabContainer::GetContainerForTab(
2025 tab_contents->GetContainerHWND());
2026 // This call is only valid on an externally hosted tab
2027 if (external_tab_container) {
2028 external_tab_container->SetAccelerators(accel_table,
2029 accel_entry_count);
2030 status = true;
2031 }
2032 }
2033 Send(new AutomationMsg_SetAcceleratorsForTabResponse(message.routing_id(),
2034 status));
2035}
2036
2037void AutomationProvider::ProcessUnhandledAccelerator(
2038 const IPC::Message& message, int handle, const MSG& msg) {
2039 if (tab_tracker_->ContainsHandle(handle)) {
2040 NavigationController* tab = tab_tracker_->GetResource(handle);
2041 TabContents* tab_contents = tab->GetTabContents(TAB_CONTENTS_WEB);
2042 ExternalTabContainer* external_tab_container =
2043 ExternalTabContainer::GetContainerForTab(
2044 tab_contents->GetContainerHWND());
2045 // This call is only valid on an externally hosted tab
2046 if (external_tab_container) {
2047 external_tab_container->ProcessUnhandledAccelerator(msg);
2048 }
2049 }
2050 // This message expects no response.
2051}
2052
2053void AutomationProvider::WaitForTabToBeRestored(
2054 const IPC::Message& message,
2055 int tab_handle) {
2056 if (tab_tracker_->ContainsHandle(tab_handle)) {
2057 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2058 restore_tracker_.reset(
2059 new NavigationControllerRestoredObserver(this, tab,
2060 message.routing_id()));
2061 }
2062}
2063
2064void AutomationProvider::GetSecurityState(const IPC::Message& message,
2065 int handle) {
2066 if (tab_tracker_->ContainsHandle(handle)) {
2067 NavigationController* tab = tab_tracker_->GetResource(handle);
2068 NavigationEntry* entry = tab->GetActiveEntry();
2069 Send(new AutomationMsg_GetSecurityStateResponse(message.routing_id(), true,
[email protected]eb34392b2008-08-19 15:42:202070 entry->ssl().security_style(), entry->ssl().cert_status(),
2071 entry->ssl().content_status()));
initial.commit09911bf2008-07-26 23:55:292072 } else {
2073 Send(new AutomationMsg_GetSecurityStateResponse(message.routing_id(), false,
2074 SECURITY_STYLE_UNKNOWN,
2075 0, 0));
2076 }
2077}
2078
2079void AutomationProvider::GetPageType(const IPC::Message& message, int handle) {
2080 if (tab_tracker_->ContainsHandle(handle)) {
2081 NavigationController* tab = tab_tracker_->GetResource(handle);
2082 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072083 NavigationEntry::PageType page_type = entry->page_type();
initial.commit09911bf2008-07-26 23:55:292084 // In order to return the proper result when an interstitial is shown and
2085 // no navigation entry were created for it we need to ask the WebContents.
2086 if (page_type == NavigationEntry::NORMAL_PAGE &&
2087 tab->active_contents()->AsWebContents() &&
[email protected]b6e09ac2008-08-12 16:11:092088 tab->active_contents()->AsWebContents()->showing_interstitial_page())
initial.commit09911bf2008-07-26 23:55:292089 page_type = NavigationEntry::INTERSTITIAL_PAGE;
2090
2091 Send(new AutomationMsg_GetPageTypeResponse(message.routing_id(), true,
2092 page_type));
2093 } else {
2094 Send(new AutomationMsg_GetPageTypeResponse(message.routing_id(), false,
2095 NavigationEntry::NORMAL_PAGE));
2096 }
2097}
2098
2099void AutomationProvider::ActionOnSSLBlockingPage(const IPC::Message& message,
2100 int handle, bool proceed) {
2101 if (tab_tracker_->ContainsHandle(handle)) {
2102 NavigationController* tab = tab_tracker_->GetResource(handle);
2103 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072104 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
initial.commit09911bf2008-07-26 23:55:292105 TabContents* tab_contents = tab->GetTabContents(TAB_CONTENTS_WEB);
[email protected]cbab76d2008-10-13 22:42:472106 InterstitialPage* ssl_blocking_page =
2107 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:292108 if (ssl_blocking_page) {
2109 if (proceed) {
2110 AddNavigationStatusListener(tab,
2111 new AutomationMsg_ActionOnSSLBlockingPageResponse(
2112 message.routing_id(), true),
2113 new AutomationMsg_ActionOnSSLBlockingPageResponse(
2114 message.routing_id(), true));
2115 ssl_blocking_page->Proceed();
2116 return;
2117 }
2118 ssl_blocking_page->DontProceed();
2119 Send(new AutomationMsg_ActionOnSSLBlockingPageResponse(
2120 message.routing_id(), true));
2121 return;
2122 }
2123 }
2124 }
2125 // We failed.
2126 Send(new AutomationMsg_ActionOnSSLBlockingPageResponse(message.routing_id(),
2127 false));
2128}
2129
2130void AutomationProvider::BringBrowserToFront(const IPC::Message& message,
2131 int browser_handle) {
2132 if (browser_tracker_->ContainsHandle(browser_handle)) {
2133 Browser* browser = browser_tracker_->GetResource(browser_handle);
2134 browser->MoveToFront(true);
2135 Send(new AutomationMsg_BringBrowserToFrontResponse(message.routing_id(),
2136 true));
2137 } else {
2138 Send(new AutomationMsg_BringBrowserToFrontResponse(message.routing_id(),
2139 false));
2140 }
2141}
2142
2143void AutomationProvider::IsPageMenuCommandEnabled(const IPC::Message& message,
2144 int browser_handle,
2145 int message_num) {
2146 if (browser_tracker_->ContainsHandle(browser_handle)) {
2147 Browser* browser = browser_tracker_->GetResource(browser_handle);
2148 bool menu_item_enabled =
2149 browser->controller()->IsCommandEnabled(message_num);
2150 Send(new AutomationMsg_IsPageMenuCommandEnabledResponse(
2151 message.routing_id(), menu_item_enabled));
2152 } else {
2153 Send(new AutomationMsg_IsPageMenuCommandEnabledResponse(
2154 message.routing_id(), false));
2155 }
2156}
2157
2158void AutomationProvider::PrintNow(const IPC::Message& message, int tab_handle) {
[email protected]20e93d12008-08-28 16:31:572159 NavigationController* tab = NULL;
2160 WebContents* web_contents = GetWebContentsForHandle(tab_handle, &tab);
2161 if (web_contents) {
initial.commit09911bf2008-07-26 23:55:292162 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572163 notification_observer_list_.AddObserver(
2164 new DocumentPrintedNotificationObserver(this, message.routing_id()));
2165 if (web_contents->PrintNow())
2166 return;
initial.commit09911bf2008-07-26 23:55:292167 }
2168 Send(new AutomationMsg_PrintNowResponse(message.routing_id(), false));
2169}
2170
2171void AutomationProvider::SavePage(const IPC::Message& message,
2172 int tab_handle,
2173 const std::wstring& file_name,
2174 const std::wstring& dir_path,
2175 int type) {
2176 if (!tab_tracker_->ContainsHandle(tab_handle)) {
2177 Send(new AutomationMsg_SavePageResponse(message.routing_id(), false));
2178 return;
2179 }
2180
2181 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2182 Browser* browser = FindAndActivateTab(nav);
2183 DCHECK(browser);
2184 if (!browser->IsCommandEnabled(IDC_SAVEPAGE)) {
2185 Send(new AutomationMsg_SavePageResponse(message.routing_id(), false));
2186 return;
2187 }
2188
2189 TabContents* tab_contents = nav->active_contents();
2190 if (tab_contents->type() != TAB_CONTENTS_WEB) {
2191 Send(new AutomationMsg_SavePageResponse(message.routing_id(), false));
2192 return;
2193 }
2194
2195 SavePackage::SavePackageType save_type =
2196 static_cast<SavePackage::SavePackageType>(type);
2197 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2198 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
2199 tab_contents->AsWebContents()->SavePage(file_name, dir_path, save_type);
2200
2201 Send(new AutomationMsg_SavePageResponse(
2202 message.routing_id(), true));
2203}
2204
2205void AutomationProvider::GetAutocompleteEditText(const IPC::Message& message,
2206 int autocomplete_edit_handle) {
2207 bool success = false;
2208 std::wstring text;
2209 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522210 text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2211 GetText();
initial.commit09911bf2008-07-26 23:55:292212 success = true;
2213 }
2214 Send(new AutomationMsg_AutocompleteEditGetTextResponse(message.routing_id(),
2215 success, text));
2216}
2217
2218void AutomationProvider::SetAutocompleteEditText(const IPC::Message& message,
2219 int autocomplete_edit_handle,
2220 const std::wstring& text) {
2221 bool success = false;
2222 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522223 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2224 SetUserText(text);
initial.commit09911bf2008-07-26 23:55:292225 success = true;
2226 }
2227 Send(new AutomationMsg_AutocompleteEditSetTextResponse(
2228 message.routing_id(), success));
2229}
2230
2231void AutomationProvider::AutocompleteEditGetMatches(
2232 const IPC::Message& message,
2233 int autocomplete_edit_handle) {
2234 bool success = false;
2235 std::vector<AutocompleteMatchData> matches;
2236 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272237 const AutocompleteResult& result = autocomplete_edit_tracker_->
2238 GetResource(autocomplete_edit_handle)->model()->result();
2239 for (AutocompleteResult::const_iterator i = result.begin();
2240 i != result.end(); ++i)
initial.commit09911bf2008-07-26 23:55:292241 matches.push_back(AutocompleteMatchData(*i));
2242 success = true;
2243 }
2244 Send(new AutomationMsg_AutocompleteEditGetMatchesResponse(
2245 message.routing_id(), success, matches));
2246}
2247
2248void AutomationProvider::AutocompleteEditIsQueryInProgress(
2249 const IPC::Message& message,
2250 int autocomplete_edit_handle) {
2251 bool success = false;
2252 bool query_in_progress = false;
2253 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522254 query_in_progress = autocomplete_edit_tracker_->
2255 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
initial.commit09911bf2008-07-26 23:55:292256 success = true;
2257 }
2258 Send(new AutomationMsg_AutocompleteEditIsQueryInProgressResponse(
2259 message.routing_id(), success, query_in_progress));
2260}
2261
[email protected]18cb2572008-08-21 20:34:452262void AutomationProvider::OnMessageFromExternalHost(
2263 int handle, const std::string& target, const std::string& message) {
[email protected]fa83e762008-08-15 21:41:392264 if (tab_tracker_->ContainsHandle(handle)) {
2265 NavigationController* tab = tab_tracker_->GetResource(handle);
2266 if (!tab) {
2267 NOTREACHED();
2268 return;
2269 }
2270 TabContents* tab_contents = tab->GetTabContents(TAB_CONTENTS_WEB);
2271 if (!tab_contents) {
2272 NOTREACHED();
2273 return;
2274 }
2275
2276 WebContents* web_contents = tab_contents->AsWebContents();
2277 if (!web_contents) {
2278 NOTREACHED();
2279 return;
2280 }
2281
2282 RenderViewHost* view_host = web_contents->render_view_host();
2283 if (!view_host) {
2284 return;
2285 }
2286
[email protected]18cb2572008-08-21 20:34:452287 view_host->ForwardMessageFromExternalHost(target, message);
[email protected]fa83e762008-08-15 21:41:392288 }
2289}
2290
[email protected]20e93d12008-08-28 16:31:572291WebContents* AutomationProvider::GetWebContentsForHandle(
2292 int handle, NavigationController** tab) {
2293 WebContents* web_contents = NULL;
2294 if (tab_tracker_->ContainsHandle(handle)) {
2295 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2296 TabContents* tab_contents = nav_controller->active_contents();
2297 if (tab_contents && tab_contents->type() == TAB_CONTENTS_WEB) {
2298 web_contents = tab_contents->AsWebContents();
2299 if (tab)
2300 *tab = nav_controller;
2301 }
2302 }
2303 return web_contents;
2304}
2305
initial.commit09911bf2008-07-26 23:55:292306TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2307 : AutomationProvider(profile) {
2308 BrowserList::AddObserver(this);
2309 NotificationService::current()->AddObserver(this, NOTIFY_SESSION_END,
2310 NotificationService::AllSources());
2311}
2312
2313TestingAutomationProvider::~TestingAutomationProvider() {
2314 NotificationService::current()->RemoveObserver(this, NOTIFY_SESSION_END,
2315 NotificationService::AllSources());
2316 BrowserList::RemoveObserver(this);
2317}
2318
2319void TestingAutomationProvider::OnChannelError() {
2320 BrowserList::CloseAllBrowsers(true);
2321 AutomationProvider::OnChannelError();
2322}
2323
2324void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2325 // For backwards compatibility with the testing automation interface, we
2326 // want the automation provider (and hence the process) to go away when the
2327 // last browser goes away.
2328 if (BrowserList::size() == 1) {
2329 // If you change this, update Observer for NOTIFY_SESSION_END below.
[email protected]295039bd2008-08-15 04:32:572330 MessageLoop::current()->PostTask(FROM_HERE,
2331 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292332 }
2333}
2334
2335void TestingAutomationProvider::Observe(NotificationType type,
2336 const NotificationSource& source,
2337 const NotificationDetails& details) {
2338 DCHECK(type == NOTIFY_SESSION_END);
2339 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2340 // before the task runs resulting in this object not being deleted. This
2341 // Release balance out the Release scheduled by OnBrowserRemoving.
2342 Release();
2343}
[email protected]295039bd2008-08-15 04:32:572344
2345void TestingAutomationProvider::OnRemoveProvider() {
2346 AutomationProviderList::GetInstance()->RemoveProvider(this);
2347}
[email protected]8a3422c92008-09-24 17:42:422348
2349void AutomationProvider::GetSSLInfoBarCount(const IPC::Message& message,
2350 int handle) {
2351 int count = -1; // -1 means error.
2352 if (tab_tracker_->ContainsHandle(handle)) {
2353 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2354 if (nav_controller) {
2355 count = static_cast<int>(nav_controller->ssl_manager()->
2356 visible_info_bars_.size());
2357 }
2358 }
2359 Send(new AutomationMsg_GetSSLInfoBarCountResponse(message.routing_id(),
2360 count));
2361}
2362
2363void AutomationProvider::ClickSSLInfoBarLink(const IPC::Message& message,
2364 int handle,
2365 int info_bar_index,
2366 bool wait_for_navigation) {
2367 bool success = false;
2368 if (tab_tracker_->ContainsHandle(handle)) {
2369 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2370 if (nav_controller) {
2371 int count = static_cast<int>(nav_controller->ssl_manager()->
2372 visible_info_bars_.size());
2373 if (info_bar_index >= 0 && info_bar_index < count) {
2374 if (wait_for_navigation) {
2375 AddNavigationStatusListener(nav_controller,
2376 new AutomationMsg_ClickSSLInfoBarLinkResponse(
2377 message.routing_id(), true),
2378 new AutomationMsg_ClickSSLInfoBarLinkResponse(
2379 message.routing_id(), true));
2380 }
2381 SSLManager::SSLInfoBar* info_bar =
2382 nav_controller->ssl_manager()->visible_info_bars_.
2383 GetElementAt(info_bar_index);
2384 info_bar->LinkActivated(NULL, 0); // Parameters are not used.
2385 success = true;
2386 }
2387 }
2388 }
2389 if (!wait_for_navigation || !success)
2390 Send(new AutomationMsg_ClickSSLInfoBarLinkResponse(message.routing_id(),
2391 success));
2392}
2393
2394void AutomationProvider::GetLastNavigationTime(const IPC::Message& message,
2395 int handle) {
2396 Time time = tab_tracker_->GetLastNavigationTime(handle);
2397 Send(new AutomationMsg_GetLastNavigationTimeResponse(message.routing_id(),
2398 time.ToInternalValue()));
2399}
2400
2401void AutomationProvider::WaitForNavigation(const IPC::Message& message,
2402 int handle,
2403 int64 last_navigation_time) {
2404 NavigationController* controller = NULL;
2405 if (tab_tracker_->ContainsHandle(handle))
2406 controller = tab_tracker_->GetResource(handle);
2407
2408 Time time = tab_tracker_->GetLastNavigationTime(handle);
2409 if (time.ToInternalValue() > last_navigation_time || !controller) {
2410 Send(new AutomationMsg_WaitForNavigationResponse(message.routing_id(),
2411 controller != NULL));
2412 return;
2413 }
2414
2415 AddNavigationStatusListener(controller,
2416 new AutomationMsg_WaitForNavigationResponse(message.routing_id(),
2417 true),
2418 new AutomationMsg_WaitForNavigationResponse(message.routing_id(),
2419 true));
2420}
2421
2422void AutomationProvider::SetIntPreference(const IPC::Message& message,
2423 int handle,
2424 std::wstring name,
2425 int value) {
2426 bool success = false;
2427 if (browser_tracker_->ContainsHandle(handle)) {
2428 Browser* browser = browser_tracker_->GetResource(handle);
2429 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
2430 success = true;
2431 }
2432 Send(new AutomationMsg_SetIntPreferenceResponse(message.routing_id(),
2433 success));
2434}