blob: 641e8837c1ce1bc2a16e2e1d9316ff54ac4e893a [file] [log] [blame]
[email protected]4801ecc2009-04-05 04:52:581// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/automation/automation_provider.h"
6
[email protected]37126212009-05-06 02:23:317#include "app/message_box_flags.h"
[email protected]c6cb1992009-04-13 16:45:298#include "base/file_version_info.h"
[email protected]5fac9622009-02-04 21:49:389#include "base/message_loop.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/path_service.h"
[email protected]f44265b2009-05-19 18:52:5011#include "base/stl_util-inl.h"
[email protected]4c4d8d22009-03-04 05:29:2712#include "base/string_util.h"
[email protected]5fac9622009-02-04 21:49:3813#include "base/thread.h"
[email protected]4f3dc372009-02-24 00:10:2914#include "chrome/app/chrome_dll_resource.h"
[email protected]0bfa713f2009-04-07 20:18:2815#include "chrome/browser/app_modal_dialog.h"
[email protected]464146e2009-04-09 18:17:0916#include "chrome/browser/app_modal_dialog_queue.h"
[email protected]b83e4602009-05-15 22:58:3317#include "chrome/browser/automation/automation_extension_function.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/browser/automation/automation_provider_list.h"
[email protected]f44265b2009-05-19 18:52:5019#include "chrome/browser/automation/extension_port_container.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/browser/automation/url_request_failed_dns_job.h"
21#include "chrome/browser/automation/url_request_mock_http_job.h"
22#include "chrome/browser/automation/url_request_slow_download_job.h"
[email protected]f3e99e32008-07-30 04:48:3923#include "chrome/browser/browser_window.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/browser/dom_operation_notification_details.h"
[email protected]cdaa8652008-09-13 02:48:5925#include "chrome/browser/download/download_manager.h"
[email protected]4801ecc2009-04-05 04:52:5826#include "chrome/browser/find_bar.h"
27#include "chrome/browser/find_bar_controller.h"
initial.commit09911bf2008-07-26 23:55:2928#include "chrome/browser/find_notification_details.h"
[email protected]13869dd2009-05-05 00:40:0629#include "chrome/browser/location_bar.h"
[email protected]6524b5f92009-01-22 17:48:2530#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]3b073b22009-01-16 03:29:0331#include "chrome/browser/ssl/ssl_manager.h"
32#include "chrome/browser/ssl/ssl_blocking_page.h"
[email protected]57c6a652009-05-04 07:58:3433#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]81af9392009-04-21 02:37:4534#include "chrome/browser/tab_contents/tab_contents_view.h"
initial.commit09911bf2008-07-26 23:55:2935#include "chrome/common/chrome_paths.h"
[email protected]1c58a5c2009-05-21 18:47:1436#include "chrome/common/notification_service.h"
[email protected]3753f522009-04-14 23:15:4737#include "chrome/common/platform_util.h"
[email protected]8a3422c92008-09-24 17:42:4238#include "chrome/common/pref_service.h"
[email protected]71f65dd2009-02-11 19:14:5639#include "chrome/test/automation/automation_messages.h"
initial.commit09911bf2008-07-26 23:55:2940#include "net/base/cookie_monster.h"
[email protected]319d9e6f2009-02-18 19:47:2141#include "net/url_request/url_request_context.h"
initial.commit09911bf2008-07-26 23:55:2942#include "net/url_request/url_request_filter.h"
43
[email protected]de246f52009-02-25 18:25:4544#if defined(OS_WIN)
45// TODO(port): Port these headers.
[email protected]de246f52009-02-25 18:25:4546#include "chrome/browser/automation/ui_controls.h"
47#include "chrome/browser/character_encoding.h"
48#include "chrome/browser/download/save_package.h"
49#include "chrome/browser/external_tab_container.h"
50#include "chrome/browser/login_prompt.h"
51#include "chrome/browser/printing/print_job.h"
[email protected]de246f52009-02-25 18:25:4552#endif // defined(OS_WIN)
53
[email protected]13869dd2009-05-05 00:40:0654#if defined(OS_WIN)
55#include "chrome/browser/views/bookmark_bar_view.h"
[email protected]2362e4f2009-05-08 00:34:0556#include "views/widget/widget_win.h"
57#include "views/window/window.h"
[email protected]13869dd2009-05-05 00:40:0658#endif
59
[email protected]e1acf6f2008-10-27 20:43:3360using base::Time;
61
initial.commit09911bf2008-07-26 23:55:2962class InitialLoadObserver : public NotificationObserver {
63 public:
64 InitialLoadObserver(size_t tab_count, AutomationProvider* automation)
[email protected]66791d22009-02-24 20:11:3365 : automation_(automation),
66 outstanding_tab_count_(tab_count) {
initial.commit09911bf2008-07-26 23:55:2967 if (outstanding_tab_count_ > 0) {
[email protected]bfd04a62009-02-01 18:16:5668 registrar_.Add(this, NotificationType::LOAD_START,
[email protected]6a02963e2009-01-06 16:58:0369 NotificationService::AllSources());
[email protected]bfd04a62009-02-01 18:16:5670 registrar_.Add(this, NotificationType::LOAD_STOP,
[email protected]6a02963e2009-01-06 16:58:0371 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:2972 }
73 }
74
75 ~InitialLoadObserver() {
initial.commit09911bf2008-07-26 23:55:2976 }
77
78 void ConditionMet() {
[email protected]6a02963e2009-01-06 16:58:0379 registrar_.RemoveAll();
initial.commit09911bf2008-07-26 23:55:2980 automation_->Send(new AutomationMsg_InitialLoadsComplete(0));
81 }
82
initial.commit09911bf2008-07-26 23:55:2983 virtual void Observe(NotificationType type,
84 const NotificationSource& source,
85 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:5686 if (type == NotificationType::LOAD_START) {
initial.commit09911bf2008-07-26 23:55:2987 if (outstanding_tab_count_ > loading_tabs_.size())
88 loading_tabs_.insert(source.map_key());
[email protected]bfd04a62009-02-01 18:16:5689 } else if (type == NotificationType::LOAD_STOP) {
initial.commit09911bf2008-07-26 23:55:2990 if (outstanding_tab_count_ > finished_tabs_.size()) {
91 if (loading_tabs_.find(source.map_key()) != loading_tabs_.end())
92 finished_tabs_.insert(source.map_key());
93 if (outstanding_tab_count_ == finished_tabs_.size())
94 ConditionMet();
95 }
96 } else {
97 NOTREACHED();
98 }
99 }
100
101 private:
102 typedef std::set<uintptr_t> TabSet;
103
[email protected]6a02963e2009-01-06 16:58:03104 NotificationRegistrar registrar_;
105
initial.commit09911bf2008-07-26 23:55:29106 AutomationProvider* automation_;
107 size_t outstanding_tab_count_;
108 TabSet loading_tabs_;
109 TabSet finished_tabs_;
110};
111
112// Watches for NewTabUI page loads for performance timing purposes.
113class NewTabUILoadObserver : public NotificationObserver {
114 public:
115 explicit NewTabUILoadObserver(AutomationProvider* automation)
116 : automation_(automation) {
[email protected]1c58a5c2009-05-21 18:47:14117 registrar_.Add(this, NotificationType::INITIAL_NEW_TAB_UI_LOAD,
118 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29119 }
120
121 ~NewTabUILoadObserver() {
initial.commit09911bf2008-07-26 23:55:29122 }
123
124 virtual void Observe(NotificationType type,
125 const NotificationSource& source,
126 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56127 if (type == NotificationType::INITIAL_NEW_TAB_UI_LOAD) {
initial.commit09911bf2008-07-26 23:55:29128 Details<int> load_time(details);
129 automation_->Send(
130 new AutomationMsg_InitialNewTabUILoadComplete(0, *load_time.ptr()));
131 } else {
132 NOTREACHED();
133 }
134 }
135
136 private:
[email protected]1c58a5c2009-05-21 18:47:14137 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29138 AutomationProvider* automation_;
139};
140
141class NavigationControllerRestoredObserver : public NotificationObserver {
142 public:
143 NavigationControllerRestoredObserver(AutomationProvider* automation,
144 NavigationController* controller,
[email protected]71f65dd2009-02-11 19:14:56145 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29146 : automation_(automation),
147 controller_(controller),
[email protected]71f65dd2009-02-11 19:14:56148 reply_message_(reply_message) {
initial.commit09911bf2008-07-26 23:55:29149 if (FinishedRestoring()) {
initial.commit09911bf2008-07-26 23:55:29150 SendDone();
151 } else {
[email protected]1c58a5c2009-05-21 18:47:14152 registrar_.Add(this, NotificationType::LOAD_STOP,
153 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29154 }
155 }
156
157 ~NavigationControllerRestoredObserver() {
initial.commit09911bf2008-07-26 23:55:29158 }
159
160 virtual void Observe(NotificationType type,
161 const NotificationSource& source,
162 const NotificationDetails& details) {
163 if (FinishedRestoring()) {
164 SendDone();
[email protected]1c58a5c2009-05-21 18:47:14165 registrar_.RemoveAll();
initial.commit09911bf2008-07-26 23:55:29166 }
167 }
168
169 private:
initial.commit09911bf2008-07-26 23:55:29170 bool FinishedRestoring() {
[email protected]7f0005a2009-04-15 03:25:11171 return (!controller_->needs_reload() && !controller_->pending_entry() &&
172 !controller_->tab_contents()->is_loading());
initial.commit09911bf2008-07-26 23:55:29173 }
174
175 void SendDone() {
[email protected]71f65dd2009-02-11 19:14:56176 DCHECK(reply_message_ != NULL);
177 automation_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:29178 }
179
[email protected]1c58a5c2009-05-21 18:47:14180 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29181 AutomationProvider* automation_;
182 NavigationController* controller_;
[email protected]71f65dd2009-02-11 19:14:56183 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29184
[email protected]5a52f162008-08-27 04:15:31185 DISALLOW_COPY_AND_ASSIGN(NavigationControllerRestoredObserver);
initial.commit09911bf2008-07-26 23:55:29186};
187
[email protected]71f65dd2009-02-11 19:14:56188template<class NavigationCodeType>
initial.commit09911bf2008-07-26 23:55:29189class NavigationNotificationObserver : public NotificationObserver {
190 public:
191 NavigationNotificationObserver(NavigationController* controller,
192 AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56193 IPC::Message* reply_message,
194 NavigationCodeType success_code,
195 NavigationCodeType auth_needed_code,
196 NavigationCodeType failed_code)
initial.commit09911bf2008-07-26 23:55:29197 : automation_(automation),
[email protected]71f65dd2009-02-11 19:14:56198 reply_message_(reply_message),
initial.commit09911bf2008-07-26 23:55:29199 controller_(controller),
[email protected]71f65dd2009-02-11 19:14:56200 navigation_started_(false),
201 success_code_(success_code),
202 auth_needed_code_(auth_needed_code),
203 failed_code_(failed_code) {
[email protected]1c58a5c2009-05-21 18:47:14204 Source<NavigationController> source(controller_);
205 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, source);
206 registrar_.Add(this, NotificationType::LOAD_START, source);
207 registrar_.Add(this, NotificationType::LOAD_STOP, source);
208 registrar_.Add(this, NotificationType::AUTH_NEEDED, source);
209 registrar_.Add(this, NotificationType::AUTH_SUPPLIED, source);
initial.commit09911bf2008-07-26 23:55:29210 }
211
212 ~NavigationNotificationObserver() {
[email protected]1c58a5c2009-05-21 18:47:14213 if (reply_message_) {
214 // This means we did not receive a notification for this navigation.
215 // Send over a failed navigation status back to the caller to ensure that
216 // the caller does not hang waiting for the response.
217 IPC::ParamTraits<NavigationCodeType>::Write(reply_message_,
218 failed_code_);
219 automation_->Send(reply_message_);
220 reply_message_ = NULL;
221 }
222
223 automation_->RemoveNavigationStatusListener(this);
initial.commit09911bf2008-07-26 23:55:29224 }
225
[email protected]71f65dd2009-02-11 19:14:56226 void ConditionMet(NavigationCodeType navigation_result) {
227 DCHECK(reply_message_ != NULL);
228
229 IPC::ParamTraits<NavigationCodeType>::Write(reply_message_,
230 navigation_result);
231 automation_->Send(reply_message_);
232 reply_message_ = NULL;
233
initial.commit09911bf2008-07-26 23:55:29234 delete this;
235 }
236
initial.commit09911bf2008-07-26 23:55:29237 virtual void Observe(NotificationType type,
238 const NotificationSource& source,
239 const NotificationDetails& details) {
[email protected]8a3422c92008-09-24 17:42:42240 // We listen for 2 events to determine when the navigation started because:
241 // - when this is used by the WaitForNavigation method, we might be invoked
242 // afer the load has started (but not after the entry was committed, as
243 // WaitForNavigation compares times of the last navigation).
244 // - when this is used with a page requiring authentication, we will not get
[email protected]4f3dc372009-02-24 00:10:29245 // a NotificationType::NAV_ENTRY_COMMITTED until after we authenticate, so
246 // we need the NotificationType::LOAD_START.
[email protected]bfd04a62009-02-01 18:16:56247 if (type == NotificationType::NAV_ENTRY_COMMITTED ||
248 type == NotificationType::LOAD_START) {
initial.commit09911bf2008-07-26 23:55:29249 navigation_started_ = true;
[email protected]bfd04a62009-02-01 18:16:56250 } else if (type == NotificationType::LOAD_STOP) {
initial.commit09911bf2008-07-26 23:55:29251 if (navigation_started_) {
252 navigation_started_ = false;
[email protected]71f65dd2009-02-11 19:14:56253 ConditionMet(success_code_);
initial.commit09911bf2008-07-26 23:55:29254 }
[email protected]bfd04a62009-02-01 18:16:56255 } else if (type == NotificationType::AUTH_SUPPLIED) {
initial.commit09911bf2008-07-26 23:55:29256 // The LoginHandler for this tab is no longer valid.
257 automation_->RemoveLoginHandler(controller_);
258
259 // Treat this as if navigation started again, since load start/stop don't
260 // occur while authentication is ongoing.
261 navigation_started_ = true;
[email protected]bfd04a62009-02-01 18:16:56262 } else if (type == NotificationType::AUTH_NEEDED) {
[email protected]de246f52009-02-25 18:25:45263#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29264 if (navigation_started_) {
265 // Remember the login handler that wants authentication.
266 LoginHandler* handler =
267 Details<LoginNotificationDetails>(details)->handler();
268 automation_->AddLoginHandler(controller_, handler);
269
270 // Respond that authentication is needed.
271 navigation_started_ = false;
[email protected]71f65dd2009-02-11 19:14:56272 ConditionMet(auth_needed_code_);
initial.commit09911bf2008-07-26 23:55:29273 } else {
274 NOTREACHED();
275 }
[email protected]de246f52009-02-25 18:25:45276#else
277 // TODO(port): Enable when we have LoginNotificationDetails etc.
278 NOTIMPLEMENTED();
279#endif
initial.commit09911bf2008-07-26 23:55:29280 } else {
281 NOTREACHED();
282 }
283 }
284
285 private:
[email protected]1c58a5c2009-05-21 18:47:14286 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29287 AutomationProvider* automation_;
[email protected]71f65dd2009-02-11 19:14:56288 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29289 NavigationController* controller_;
290 bool navigation_started_;
[email protected]71f65dd2009-02-11 19:14:56291 NavigationCodeType success_code_;
292 NavigationCodeType auth_needed_code_;
293 NavigationCodeType failed_code_;
initial.commit09911bf2008-07-26 23:55:29294};
295
296class TabStripNotificationObserver : public NotificationObserver {
297 public:
[email protected]1c58a5c2009-05-21 18:47:14298 TabStripNotificationObserver(NotificationType notification,
299 AutomationProvider* automation)
300 : automation_(automation),
301 notification_(notification) {
302 registrar_.Add(this, notification_, NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29303 }
304
305 virtual ~TabStripNotificationObserver() {
initial.commit09911bf2008-07-26 23:55:29306 }
307
308 virtual void Observe(NotificationType type,
309 const NotificationSource& source,
310 const NotificationDetails& details) {
311 if (type == notification_) {
312 ObserveTab(Source<NavigationController>(source).ptr());
313
314 // If verified, no need to observe anymore
315 automation_->RemoveTabStripObserver(this);
316 delete this;
317 } else {
318 NOTREACHED();
319 }
320 }
321
322 virtual void ObserveTab(NavigationController* controller) = 0;
323
324 protected:
[email protected]1c58a5c2009-05-21 18:47:14325 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29326 AutomationProvider* automation_;
initial.commit09911bf2008-07-26 23:55:29327 NotificationType notification_;
initial.commit09911bf2008-07-26 23:55:29328};
329
330class TabAppendedNotificationObserver : public TabStripNotificationObserver {
331 public:
332 TabAppendedNotificationObserver(Browser* parent,
[email protected]1c58a5c2009-05-21 18:47:14333 AutomationProvider* automation,
334 IPC::Message* reply_message)
335 : TabStripNotificationObserver(NotificationType::TAB_PARENTED,
336 automation),
337 parent_(parent),
[email protected]71f65dd2009-02-11 19:14:56338 reply_message_(reply_message) {
initial.commit09911bf2008-07-26 23:55:29339 }
340
341 virtual void ObserveTab(NavigationController* controller) {
[email protected]1c58a5c2009-05-21 18:47:14342 if (automation_->GetIndexForNavigationController(controller, parent_) ==
343 TabStripModel::kNoTab) {
344 // This tab notification doesn't belong to the parent_.
initial.commit09911bf2008-07-26 23:55:29345 return;
346 }
347
348 // Give the same response even if auth is needed, since it doesn't matter.
[email protected]71f65dd2009-02-11 19:14:56349 automation_->AddNavigationStatusListener<int>(
350 controller, reply_message_, AUTOMATION_MSG_NAVIGATION_SUCCESS,
351 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED, AUTOMATION_MSG_NAVIGATION_ERROR);
initial.commit09911bf2008-07-26 23:55:29352 }
[email protected]71f65dd2009-02-11 19:14:56353
354 protected:
[email protected]1c58a5c2009-05-21 18:47:14355 Browser* parent_;
[email protected]71f65dd2009-02-11 19:14:56356 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29357};
358
359class TabClosedNotificationObserver : public TabStripNotificationObserver {
360 public:
[email protected]1c58a5c2009-05-21 18:47:14361 TabClosedNotificationObserver(AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56362 bool wait_until_closed,
363 IPC::Message* reply_message)
[email protected]1c58a5c2009-05-21 18:47:14364 : TabStripNotificationObserver(wait_until_closed ?
365 NotificationType::TAB_CLOSED : NotificationType::TAB_CLOSING,
366 automation),
367 reply_message_(reply_message),
368 for_browser_command_(false) {
initial.commit09911bf2008-07-26 23:55:29369 }
370
371 virtual void ObserveTab(NavigationController* controller) {
[email protected]1c58a5c2009-05-21 18:47:14372 if (for_browser_command_) {
[email protected]d79ffea2009-05-07 20:51:42373 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message_,
374 true);
[email protected]1c58a5c2009-05-21 18:47:14375 } else {
[email protected]d79ffea2009-05-07 20:51:42376 AutomationMsg_CloseTab::WriteReplyParams(reply_message_, true);
[email protected]1c58a5c2009-05-21 18:47:14377 }
[email protected]71f65dd2009-02-11 19:14:56378 automation_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:29379 }
[email protected]71f65dd2009-02-11 19:14:56380
[email protected]d79ffea2009-05-07 20:51:42381 void set_for_browser_command(bool for_browser_command) {
382 for_browser_command_ = for_browser_command;
383 }
[email protected]1c58a5c2009-05-21 18:47:14384
[email protected]71f65dd2009-02-11 19:14:56385 protected:
386 IPC::Message* reply_message_;
[email protected]d79ffea2009-05-07 20:51:42387 bool for_browser_command_;
initial.commit09911bf2008-07-26 23:55:29388};
389
[email protected]14c0a032009-04-13 18:15:14390class BrowserOpenedNotificationObserver : public NotificationObserver {
391 public:
392 BrowserOpenedNotificationObserver(AutomationProvider* automation,
393 IPC::Message* reply_message)
394 : automation_(automation),
[email protected]d79ffea2009-05-07 20:51:42395 reply_message_(reply_message),
396 for_browser_command_(false) {
[email protected]14c0a032009-04-13 18:15:14397 registrar_.Add(this, NotificationType::BROWSER_OPENED,
398 NotificationService::AllSources());
399 }
400
401 ~BrowserOpenedNotificationObserver() {
402 }
403
404 virtual void Observe(NotificationType type,
405 const NotificationSource& source,
406 const NotificationDetails& details) {
407 if (type == NotificationType::BROWSER_OPENED) {
[email protected]1c58a5c2009-05-21 18:47:14408 if (for_browser_command_) {
[email protected]d79ffea2009-05-07 20:51:42409 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message_,
410 true);
[email protected]1c58a5c2009-05-21 18:47:14411 }
[email protected]14c0a032009-04-13 18:15:14412 automation_->Send(reply_message_);
413 delete this;
414 } else {
415 NOTREACHED();
416 }
417 }
418
[email protected]d79ffea2009-05-07 20:51:42419 void set_for_browser_command(bool for_browser_command) {
420 for_browser_command_ = for_browser_command;
421 }
[email protected]1c58a5c2009-05-21 18:47:14422
[email protected]14c0a032009-04-13 18:15:14423 private:
[email protected]1c58a5c2009-05-21 18:47:14424 NotificationRegistrar registrar_;
[email protected]14c0a032009-04-13 18:15:14425 AutomationProvider* automation_;
426 IPC::Message* reply_message_;
[email protected]d79ffea2009-05-07 20:51:42427 bool for_browser_command_;
[email protected]14c0a032009-04-13 18:15:14428};
429
initial.commit09911bf2008-07-26 23:55:29430class BrowserClosedNotificationObserver : public NotificationObserver {
431 public:
432 BrowserClosedNotificationObserver(Browser* browser,
433 AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56434 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29435 : automation_(automation),
[email protected]d79ffea2009-05-07 20:51:42436 reply_message_(reply_message),
437 for_browser_command_(false) {
[email protected]c300deb32009-05-08 21:26:07438 registrar_.Add(this, NotificationType::BROWSER_CLOSED,
439 Source<Browser>(browser));
initial.commit09911bf2008-07-26 23:55:29440 }
441
442 virtual void Observe(NotificationType type,
443 const NotificationSource& source,
444 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56445 DCHECK(type == NotificationType::BROWSER_CLOSED);
initial.commit09911bf2008-07-26 23:55:29446 Details<bool> close_app(details);
[email protected]71f65dd2009-02-11 19:14:56447 DCHECK(reply_message_ != NULL);
[email protected]1c58a5c2009-05-21 18:47:14448 if (for_browser_command_) {
[email protected]d79ffea2009-05-07 20:51:42449 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message_,
450 true);
[email protected]1c58a5c2009-05-21 18:47:14451 } else {
[email protected]d79ffea2009-05-07 20:51:42452 AutomationMsg_CloseBrowser::WriteReplyParams(reply_message_, true,
453 *(close_app.ptr()));
[email protected]1c58a5c2009-05-21 18:47:14454 }
[email protected]71f65dd2009-02-11 19:14:56455 automation_->Send(reply_message_);
456 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29457 delete this;
458 }
459
[email protected]d79ffea2009-05-07 20:51:42460 void set_for_browser_command(bool for_browser_command) {
461 for_browser_command_ = for_browser_command;
462 }
[email protected]1c58a5c2009-05-21 18:47:14463
initial.commit09911bf2008-07-26 23:55:29464 private:
[email protected]c300deb32009-05-08 21:26:07465 NotificationRegistrar registrar_;
[email protected]1c58a5c2009-05-21 18:47:14466 AutomationProvider* automation_;
467 IPC::Message* reply_message_;
[email protected]d79ffea2009-05-07 20:51:42468 bool for_browser_command_;
initial.commit09911bf2008-07-26 23:55:29469};
470
[email protected]4e41709d2009-04-08 00:04:27471namespace {
472
473// Define mapping from command to notification
474struct CommandNotification {
475 int command;
476 NotificationType::Type notification_type;
477};
478
479const struct CommandNotification command_notifications[] = {
[email protected]14c0a032009-04-13 18:15:14480 {IDC_DUPLICATE_TAB, NotificationType::TAB_PARENTED},
[email protected]f8fc5882009-05-06 04:23:43481 {IDC_NEW_TAB, NotificationType::TAB_PARENTED},
[email protected]d79ffea2009-05-07 20:51:42482 // Returns as soon as the restored tab is created. To further wait until
483 // the content page is loaded, use WaitForTabToBeRestored.
[email protected]f8fc5882009-05-06 04:23:43484 {IDC_RESTORE_TAB, NotificationType::TAB_PARENTED}
[email protected]4e41709d2009-04-08 00:04:27485};
486
487} // namespace
488
[email protected]56e71b7c2009-03-27 03:05:56489class ExecuteBrowserCommandObserver : public NotificationObserver {
490 public:
[email protected]56e71b7c2009-03-27 03:05:56491 ~ExecuteBrowserCommandObserver() {
492 }
493
[email protected]d79ffea2009-05-07 20:51:42494 static bool CreateAndRegisterObserver(AutomationProvider* automation,
495 Browser* browser,
496 int command,
497 IPC::Message* reply_message) {
498 bool result = true;
499 switch (command) {
500 case IDC_NEW_WINDOW:
501 case IDC_NEW_INCOGNITO_WINDOW: {
502 BrowserOpenedNotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14503 new BrowserOpenedNotificationObserver(automation, reply_message);
[email protected]d79ffea2009-05-07 20:51:42504 observer->set_for_browser_command(true);
505 break;
506 }
507 case IDC_CLOSE_WINDOW: {
508 BrowserClosedNotificationObserver* observer =
509 new BrowserClosedNotificationObserver(browser, automation,
[email protected]d79ffea2009-05-07 20:51:42510 reply_message);
511 observer->set_for_browser_command(true);
512 break;
513 }
514 case IDC_CLOSE_TAB: {
515 TabClosedNotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14516 new TabClosedNotificationObserver(automation, true, reply_message);
[email protected]d79ffea2009-05-07 20:51:42517 observer->set_for_browser_command(true);
518 break;
519 }
[email protected]27e1c9e02009-05-13 18:41:39520 case IDC_BACK:
521 case IDC_FORWARD:
522 case IDC_RELOAD: {
523 automation->
524 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
525 &browser->GetSelectedTabContents()->controller(),
526 reply_message,
527 AUTOMATION_MSG_NAVIGATION_SUCCESS,
528 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
529 AUTOMATION_MSG_NAVIGATION_ERROR);
530 break;
531 }
[email protected]d79ffea2009-05-07 20:51:42532 default: {
533 ExecuteBrowserCommandObserver* observer =
534 new ExecuteBrowserCommandObserver(automation, reply_message);
535 if (!observer->Register(command)) {
536 delete observer;
537 result = false;
538 }
539 break;
540 }
541 }
542 return result;
[email protected]4e41709d2009-04-08 00:04:27543 }
544
[email protected]56e71b7c2009-03-27 03:05:56545 virtual void Observe(NotificationType type,
546 const NotificationSource& source,
547 const NotificationDetails& details) {
548 if (type == notification_type_) {
[email protected]49a14a82009-03-31 04:16:44549 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message_,
550 true);
[email protected]56e71b7c2009-03-27 03:05:56551 automation_->Send(reply_message_);
552 delete this;
553 } else {
554 NOTREACHED();
555 }
556 }
557
558 private:
[email protected]d79ffea2009-05-07 20:51:42559 ExecuteBrowserCommandObserver(AutomationProvider* automation,
560 IPC::Message* reply_message)
561 : automation_(automation),
562 reply_message_(reply_message) {
563 }
564
565 bool Register(int command) {
566 if (!GetNotificationType(command, &notification_type_))
567 return false;
[email protected]1c58a5c2009-05-21 18:47:14568 registrar_.Add(this, notification_type_, NotificationService::AllSources());
[email protected]d79ffea2009-05-07 20:51:42569 return true;
570 }
571
[email protected]4e41709d2009-04-08 00:04:27572 bool GetNotificationType(int command, NotificationType::Type* type) {
573 if (!type)
574 return false;
575 bool found = false;
576 for (unsigned int i = 0; i < arraysize(command_notifications); i++) {
577 if (command_notifications[i].command == command) {
578 *type = command_notifications[i].notification_type;
579 found = true;
580 break;
581 }
582 }
583 return found;
584 }
585
[email protected]1c58a5c2009-05-21 18:47:14586 NotificationRegistrar registrar_;
[email protected]56e71b7c2009-03-27 03:05:56587 AutomationProvider* automation_;
588 NotificationType::Type notification_type_;
589 IPC::Message* reply_message_;
[email protected]56e71b7c2009-03-27 03:05:56590};
591
initial.commit09911bf2008-07-26 23:55:29592class FindInPageNotificationObserver : public NotificationObserver {
593 public:
594 FindInPageNotificationObserver(AutomationProvider* automation,
595 TabContents* parent_tab,
[email protected]71f65dd2009-02-11 19:14:56596 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29597 : automation_(automation),
[email protected]71f65dd2009-02-11 19:14:56598 active_match_ordinal_(-1),
599 reply_message_(reply_message) {
[email protected]1c58a5c2009-05-21 18:47:14600 registrar_.Add(this, NotificationType::FIND_RESULT_AVAILABLE,
601 Source<TabContents>(parent_tab));
initial.commit09911bf2008-07-26 23:55:29602 }
603
604 ~FindInPageNotificationObserver() {
initial.commit09911bf2008-07-26 23:55:29605 }
606
[email protected]1c58a5c2009-05-21 18:47:14607 virtual void Observe(NotificationType type,
608 const NotificationSource& source,
609 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56610 if (type == NotificationType::FIND_RESULT_AVAILABLE) {
initial.commit09911bf2008-07-26 23:55:29611 Details<FindNotificationDetails> find_details(details);
612 if (find_details->request_id() == kFindInPageRequestId) {
[email protected]aedd85a2008-12-04 19:32:49613 // We get multiple responses and one of those will contain the ordinal.
614 // This message comes to us before the final update is sent.
615 if (find_details->active_match_ordinal() > -1)
616 active_match_ordinal_ = find_details->active_match_ordinal();
initial.commit09911bf2008-07-26 23:55:29617 if (find_details->final_update()) {
[email protected]71f65dd2009-02-11 19:14:56618 DCHECK(reply_message_ != NULL);
619
[email protected]1c58a5c2009-05-21 18:47:14620 AutomationMsg_FindInPage::WriteReplyParams(reply_message_,
621 active_match_ordinal_, find_details->number_of_matches());
[email protected]71f65dd2009-02-11 19:14:56622
623 automation_->Send(reply_message_);
624 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:29625 } else {
626 DLOG(INFO) << "Ignoring, since we only care about the final message";
627 }
628 }
629 } else {
630 NOTREACHED();
631 }
632 }
633
634 // The Find mechanism is over asynchronous IPC, so a search is kicked off and
635 // we wait for notification to find out what the results are. As the user is
636 // typing, new search requests can be issued and the Request ID helps us make
637 // sense of whether this is the current request or an old one. The unit tests,
638 // however, which uses this constant issues only one search at a time, so we
639 // don't need a rolling id to identify each search. But, we still need to
640 // specify one, so we just use a fixed one - its value does not matter.
641 static const int kFindInPageRequestId;
[email protected]1c58a5c2009-05-21 18:47:14642
initial.commit09911bf2008-07-26 23:55:29643 private:
[email protected]1c58a5c2009-05-21 18:47:14644 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29645 AutomationProvider* automation_;
[email protected]aedd85a2008-12-04 19:32:49646 // We will at some point (before final update) be notified of the ordinal and
647 // we need to preserve it so we can send it later.
648 int active_match_ordinal_;
[email protected]71f65dd2009-02-11 19:14:56649 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29650};
651
652const int FindInPageNotificationObserver::kFindInPageRequestId = -1;
653
654class DomOperationNotificationObserver : public NotificationObserver {
655 public:
656 explicit DomOperationNotificationObserver(AutomationProvider* automation)
657 : automation_(automation) {
[email protected]1c58a5c2009-05-21 18:47:14658 registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
659 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29660 }
661
662 ~DomOperationNotificationObserver() {
initial.commit09911bf2008-07-26 23:55:29663 }
664
[email protected]1c58a5c2009-05-21 18:47:14665 virtual void Observe(NotificationType type,
666 const NotificationSource& source,
667 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56668 if (NotificationType::DOM_OPERATION_RESPONSE == type) {
initial.commit09911bf2008-07-26 23:55:29669 Details<DomOperationNotificationDetails> dom_op_details(details);
[email protected]71f65dd2009-02-11 19:14:56670
671 IPC::Message* reply_message = automation_->reply_message_release();
672 DCHECK(reply_message != NULL);
673
[email protected]1c58a5c2009-05-21 18:47:14674 AutomationMsg_DomOperation::WriteReplyParams(reply_message,
675 dom_op_details->json());
[email protected]71f65dd2009-02-11 19:14:56676 automation_->Send(reply_message);
initial.commit09911bf2008-07-26 23:55:29677 }
678 }
[email protected]1c58a5c2009-05-21 18:47:14679
initial.commit09911bf2008-07-26 23:55:29680 private:
[email protected]1c58a5c2009-05-21 18:47:14681 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29682 AutomationProvider* automation_;
683};
684
685class DomInspectorNotificationObserver : public NotificationObserver {
686 public:
687 explicit DomInspectorNotificationObserver(AutomationProvider* automation)
688 : automation_(automation) {
[email protected]1c58a5c2009-05-21 18:47:14689 registrar_.Add(this, NotificationType::DOM_INSPECT_ELEMENT_RESPONSE,
690 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29691 }
692
693 ~DomInspectorNotificationObserver() {
initial.commit09911bf2008-07-26 23:55:29694 }
695
696 virtual void Observe(NotificationType type, const NotificationSource& source,
697 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:56698 if (NotificationType::DOM_INSPECT_ELEMENT_RESPONSE == type) {
initial.commit09911bf2008-07-26 23:55:29699 Details<int> dom_inspect_details(details);
700 automation_->ReceivedInspectElementResponse(*(dom_inspect_details.ptr()));
701 }
702 }
703
704 private:
[email protected]1c58a5c2009-05-21 18:47:14705 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29706 AutomationProvider* automation_;
707};
708
[email protected]de246f52009-02-25 18:25:45709#if defined(OS_WIN)
710// TODO(port): Enable when printing is ported.
initial.commit09911bf2008-07-26 23:55:29711class DocumentPrintedNotificationObserver : public NotificationObserver {
712 public:
713 DocumentPrintedNotificationObserver(AutomationProvider* automation,
[email protected]71f65dd2009-02-11 19:14:56714 IPC::Message* reply_message)
initial.commit09911bf2008-07-26 23:55:29715 : automation_(automation),
[email protected]71f65dd2009-02-11 19:14:56716 success_(false),
717 reply_message_(reply_message) {
[email protected]1c58a5c2009-05-21 18:47:14718 registrar_.Add(this, NotificationType::PRINT_JOB_EVENT,
719 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29720 }
721
722 ~DocumentPrintedNotificationObserver() {
[email protected]71f65dd2009-02-11 19:14:56723 DCHECK(reply_message_ != NULL);
724 AutomationMsg_PrintNow::WriteReplyParams(reply_message_, success_);
725 automation_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:29726 automation_->RemoveNavigationStatusListener(this);
initial.commit09911bf2008-07-26 23:55:29727 }
728
729 virtual void Observe(NotificationType type, const NotificationSource& source,
730 const NotificationDetails& details) {
731 using namespace printing;
[email protected]bfd04a62009-02-01 18:16:56732 DCHECK(type == NotificationType::PRINT_JOB_EVENT);
initial.commit09911bf2008-07-26 23:55:29733 switch (Details<JobEventDetails>(details)->type()) {
734 case JobEventDetails::JOB_DONE: {
735 // Succeeded.
736 success_ = true;
737 delete this;
738 break;
739 }
740 case JobEventDetails::USER_INIT_CANCELED:
741 case JobEventDetails::FAILED: {
742 // Failed.
743 delete this;
744 break;
745 }
746 case JobEventDetails::NEW_DOC:
747 case JobEventDetails::USER_INIT_DONE:
748 case JobEventDetails::DEFAULT_INIT_DONE:
749 case JobEventDetails::NEW_PAGE:
750 case JobEventDetails::PAGE_DONE:
751 case JobEventDetails::DOC_DONE:
752 case JobEventDetails::ALL_PAGES_REQUESTED: {
753 // Don't care.
754 break;
755 }
756 default: {
757 NOTREACHED();
758 break;
759 }
760 }
761 }
762
763 private:
[email protected]1c58a5c2009-05-21 18:47:14764 NotificationRegistrar registrar_;
initial.commit09911bf2008-07-26 23:55:29765 scoped_refptr<AutomationProvider> automation_;
initial.commit09911bf2008-07-26 23:55:29766 bool success_;
[email protected]71f65dd2009-02-11 19:14:56767 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:29768};
[email protected]de246f52009-02-25 18:25:45769#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29770
[email protected]cbab76d2008-10-13 22:42:47771class AutomationInterstitialPage : public InterstitialPage {
772 public:
[email protected]57c6a652009-05-04 07:58:34773 AutomationInterstitialPage(TabContents* tab,
[email protected]cbab76d2008-10-13 22:42:47774 const GURL& url,
775 const std::string& contents)
776 : InterstitialPage(tab, true, url),
777 contents_(contents) {
778 }
779
780 virtual std::string GetHTMLContents() { return contents_; }
781
782 private:
783 std::string contents_;
[email protected]4f3dc372009-02-24 00:10:29784
[email protected]cbab76d2008-10-13 22:42:47785 DISALLOW_COPY_AND_ASSIGN(AutomationInterstitialPage);
786};
787
initial.commit09911bf2008-07-26 23:55:29788AutomationProvider::AutomationProvider(Profile* profile)
[email protected]295039bd2008-08-15 04:32:57789 : redirect_query_(0),
[email protected]71f65dd2009-02-11 19:14:56790 profile_(profile),
791 reply_message_(NULL) {
initial.commit09911bf2008-07-26 23:55:29792 browser_tracker_.reset(new AutomationBrowserTracker(this));
initial.commit09911bf2008-07-26 23:55:29793 tab_tracker_.reset(new AutomationTabTracker(this));
[email protected]0e9f4ee2009-04-08 01:44:20794 window_tracker_.reset(new AutomationWindowTracker(this));
initial.commit09911bf2008-07-26 23:55:29795 autocomplete_edit_tracker_.reset(
796 new AutomationAutocompleteEditTracker(this));
[email protected]13869dd2009-05-05 00:40:06797#if defined(OS_WIN)
798 // TODO(port): Enable as the trackers get ported.
initial.commit09911bf2008-07-26 23:55:29799 cwindow_tracker_.reset(new AutomationConstrainedWindowTracker(this));
[email protected]a5ca79bd2009-05-12 00:06:31800#else
801 NOTIMPLEMENTED() << "hook up constrained window tracker";
802#endif
initial.commit09911bf2008-07-26 23:55:29803 new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this));
804 dom_operation_observer_.reset(new DomOperationNotificationObserver(this));
805 dom_inspector_observer_.reset(new DomInspectorNotificationObserver(this));
806}
807
808AutomationProvider::~AutomationProvider() {
[email protected]f44265b2009-05-19 18:52:50809 STLDeleteContainerPairSecondPointers(port_containers_.begin(),
810 port_containers_.end());
811 port_containers_.clear();
812
[email protected]0da050b92008-08-19 19:29:47813 // Make sure that any outstanding NotificationObservers also get destroyed.
814 ObserverList<NotificationObserver>::Iterator it(notification_observer_list_);
[email protected]5a52f162008-08-27 04:15:31815 NotificationObserver* observer;
[email protected]0da050b92008-08-19 19:29:47816 while ((observer = it.GetNext()) != NULL)
817 delete observer;
initial.commit09911bf2008-07-26 23:55:29818}
819
820void AutomationProvider::ConnectToChannel(const std::wstring& channel_id) {
[email protected]295039bd2008-08-15 04:32:57821 channel_.reset(
[email protected]71f65dd2009-02-11 19:14:56822 new IPC::SyncChannel(channel_id, IPC::Channel::MODE_CLIENT, this, NULL,
823 g_browser_process->io_thread()->message_loop(),
824 true, g_browser_process->shutdown_event()));
[email protected]79e966832009-04-21 14:23:05825 scoped_ptr<FileVersionInfo> file_version_info(
826 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
[email protected]cf620752009-04-24 17:05:40827 std::string version_string;
828 if (file_version_info != NULL) {
829 version_string = WideToASCII(file_version_info->file_version());
830 }
[email protected]c6cb1992009-04-13 16:45:29831
832 // Send a hello message with our current automation protocol version.
833 channel_->Send(new AutomationMsg_Hello(0, version_string.c_str()));
initial.commit09911bf2008-07-26 23:55:29834}
835
836void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) {
837 if (expected_tabs == 0) {
838 Send(new AutomationMsg_InitialLoadsComplete(0));
839 } else {
840 initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this));
841 }
842}
843
[email protected]71f65dd2009-02-11 19:14:56844template<class NavigationCodeType>
initial.commit09911bf2008-07-26 23:55:29845NotificationObserver* AutomationProvider::AddNavigationStatusListener(
[email protected]71f65dd2009-02-11 19:14:56846 NavigationController* tab, IPC::Message* reply_message,
847 NavigationCodeType success_code,
848 NavigationCodeType auth_needed_code,
849 NavigationCodeType failed_code) {
initial.commit09911bf2008-07-26 23:55:29850 NotificationObserver* observer =
[email protected]71f65dd2009-02-11 19:14:56851 new NavigationNotificationObserver<NavigationCodeType>(
852 tab, this, reply_message, success_code, auth_needed_code,
853 failed_code);
initial.commit09911bf2008-07-26 23:55:29854
[email protected]71f65dd2009-02-11 19:14:56855 notification_observer_list_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29856 return observer;
857}
858
859void AutomationProvider::RemoveNavigationStatusListener(
860 NotificationObserver* obs) {
861 notification_observer_list_.RemoveObserver(obs);
862}
863
864NotificationObserver* AutomationProvider::AddTabStripObserver(
[email protected]1c58a5c2009-05-21 18:47:14865 Browser* parent,
866 IPC::Message* reply_message) {
[email protected]71f65dd2009-02-11 19:14:56867 NotificationObserver* observer =
[email protected]1c58a5c2009-05-21 18:47:14868 new TabAppendedNotificationObserver(parent, this, reply_message);
initial.commit09911bf2008-07-26 23:55:29869 notification_observer_list_.AddObserver(observer);
870
871 return observer;
872}
873
874void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) {
875 notification_observer_list_.RemoveObserver(obs);
876}
877
878void AutomationProvider::AddLoginHandler(NavigationController* tab,
879 LoginHandler* handler) {
880 login_handler_map_[tab] = handler;
881}
882
883void AutomationProvider::RemoveLoginHandler(NavigationController* tab) {
884 DCHECK(login_handler_map_[tab]);
885 login_handler_map_.erase(tab);
886}
887
[email protected]f44265b2009-05-19 18:52:50888void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) {
889 int port_id = port->port_id();
890 DCHECK_NE(-1, port_id);
891 DCHECK(port_containers_.find(port_id) == port_containers_.end());
892
893 port_containers_[port_id] = port;
894}
895
896void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) {
897 int port_id = port->port_id();
898 DCHECK_NE(-1, port_id);
899
900 PortContainerMap::iterator it = port_containers_.find(port_id);
901 DCHECK(it != port_containers_.end());
902
903 if (it != port_containers_.end()) {
904 delete it->second;
905 port_containers_.erase(it);
906 }
907}
908
909ExtensionPortContainer* AutomationProvider::GetPortContainer(
910 int port_id) const {
911 PortContainerMap::const_iterator it = port_containers_.find(port_id);
912 if (it == port_containers_.end())
913 return NULL;
914
915 return it->second;
916}
917
initial.commit09911bf2008-07-26 23:55:29918int AutomationProvider::GetIndexForNavigationController(
919 const NavigationController* controller, const Browser* parent) const {
920 DCHECK(parent);
[email protected]902cdf772009-05-06 15:08:12921 return parent->GetIndexOfController(controller);
initial.commit09911bf2008-07-26 23:55:29922}
923
924void AutomationProvider::OnMessageReceived(const IPC::Message& message) {
925 IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message)
[email protected]1c58a5c2009-05-21 18:47:14926 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseBrowser, CloseBrowser)
[email protected]71f65dd2009-02-11 19:14:56927 IPC_MESSAGE_HANDLER(AutomationMsg_CloseBrowserRequestAsync,
928 CloseBrowserAsync)
929 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateTab, ActivateTab)
930 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveTabIndex, GetActiveTabIndex)
931 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_AppendTab, AppendTab)
932 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CloseTab, CloseTab)
933 IPC_MESSAGE_HANDLER(AutomationMsg_GetCookies, GetCookies)
934 IPC_MESSAGE_HANDLER(AutomationMsg_SetCookie, SetCookie)
[email protected]1c58a5c2009-05-21 18:47:14935 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_NavigateToURL, NavigateToURL)
[email protected]71f65dd2009-02-11 19:14:56936 IPC_MESSAGE_HANDLER(AutomationMsg_NavigationAsync, NavigationAsync)
937 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoBack, GoBack)
938 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_GoForward, GoForward)
939 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Reload, Reload)
940 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_SetAuth, SetAuth)
[email protected]1c58a5c2009-05-21 18:47:14941 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_CancelAuth, CancelAuth)
[email protected]71f65dd2009-02-11 19:14:56942 IPC_MESSAGE_HANDLER(AutomationMsg_NeedsAuth, NeedsAuth)
943 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RedirectsFrom,
944 GetRedirectsFrom)
[email protected]1c58a5c2009-05-21 18:47:14945 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindowCount, GetBrowserWindowCount)
[email protected]24497032009-05-01 17:00:29946 IPC_MESSAGE_HANDLER(AutomationMsg_NormalBrowserWindowCount,
947 GetNormalBrowserWindowCount)
[email protected]71f65dd2009-02-11 19:14:56948 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserWindow, GetBrowserWindow)
949 IPC_MESSAGE_HANDLER(AutomationMsg_LastActiveBrowserWindow,
initial.commit09911bf2008-07-26 23:55:29950 GetLastActiveBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56951 IPC_MESSAGE_HANDLER(AutomationMsg_ActiveWindow, GetActiveWindow)
[email protected]24497032009-05-01 17:00:29952 IPC_MESSAGE_HANDLER(AutomationMsg_FindNormalBrowserWindow,
953 FindNormalBrowserWindow)
[email protected]71f65dd2009-02-11 19:14:56954 IPC_MESSAGE_HANDLER(AutomationMsg_IsWindowActive, IsWindowActive)
[email protected]1c58a5c2009-05-21 18:47:14955 IPC_MESSAGE_HANDLER(AutomationMsg_ActivateWindow, ActivateWindow)
[email protected]de246f52009-02-25 18:25:45956#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56957 IPC_MESSAGE_HANDLER(AutomationMsg_WindowHWND, GetWindowHWND)
[email protected]de246f52009-02-25 18:25:45958#endif // defined(OS_WIN)
[email protected]49a14a82009-03-31 04:16:44959 IPC_MESSAGE_HANDLER(AutomationMsg_WindowExecuteCommandAsync,
[email protected]4f6381ee2009-04-16 02:46:33960 ExecuteBrowserCommandAsync)
[email protected]49a14a82009-03-31 04:16:44961 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowExecuteCommand,
[email protected]4f6381ee2009-04-16 02:46:33962 ExecuteBrowserCommand)
[email protected]1c58a5c2009-05-21 18:47:14963 IPC_MESSAGE_HANDLER(AutomationMsg_WindowViewBounds, WindowGetViewBounds)
964 IPC_MESSAGE_HANDLER(AutomationMsg_SetWindowVisible, SetWindowVisible)
[email protected]de246f52009-02-25 18:25:45965#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56966 IPC_MESSAGE_HANDLER(AutomationMsg_WindowClick, WindowSimulateClick)
[email protected]1c58a5c2009-05-21 18:47:14967 IPC_MESSAGE_HANDLER(AutomationMsg_WindowKeyPress, WindowSimulateKeyPress)
[email protected]71f65dd2009-02-11 19:14:56968 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag,
969 WindowSimulateDrag)
[email protected]d7fa7552009-03-20 21:06:37970#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56971 IPC_MESSAGE_HANDLER(AutomationMsg_TabCount, GetTabCount)
972 IPC_MESSAGE_HANDLER(AutomationMsg_Tab, GetTab)
[email protected]d7fa7552009-03-20 21:06:37973#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56974 IPC_MESSAGE_HANDLER(AutomationMsg_TabHWND, GetTabHWND)
[email protected]de246f52009-02-25 18:25:45975#endif // defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:56976 IPC_MESSAGE_HANDLER(AutomationMsg_TabProcessID, GetTabProcessID)
977 IPC_MESSAGE_HANDLER(AutomationMsg_TabTitle, GetTabTitle)
[email protected]77bc6732009-04-20 22:01:03978 IPC_MESSAGE_HANDLER(AutomationMsg_TabIndex, GetTabIndex)
[email protected]71f65dd2009-02-11 19:14:56979 IPC_MESSAGE_HANDLER(AutomationMsg_TabURL, GetTabURL)
[email protected]1c58a5c2009-05-21 18:47:14980 IPC_MESSAGE_HANDLER(AutomationMsg_ShelfVisibility, GetShelfVisibility)
initial.commit09911bf2008-07-26 23:55:29981 IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused)
[email protected]1c58a5c2009-05-21 18:47:14982 IPC_MESSAGE_HANDLER(AutomationMsg_ApplyAccelerator, ApplyAccelerator)
[email protected]71f65dd2009-02-11 19:14:56983 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_DomOperation,
984 ExecuteJavascript)
985 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowCount,
initial.commit09911bf2008-07-26 23:55:29986 GetConstrainedWindowCount)
[email protected]1c58a5c2009-05-21 18:47:14987 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindow, GetConstrainedWindow)
988 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedTitle, GetConstrainedTitle)
989 IPC_MESSAGE_HANDLER(AutomationMsg_FindInPage, HandleFindInPageRequest)
990 IPC_MESSAGE_HANDLER(AutomationMsg_GetFocusedViewID, GetFocusedViewID)
[email protected]71f65dd2009-02-11 19:14:56991 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InspectElement,
992 HandleInspectElementRequest)
[email protected]1c58a5c2009-05-21 18:47:14993 IPC_MESSAGE_HANDLER(AutomationMsg_SetFilteredInet, SetFilteredInet)
994 IPC_MESSAGE_HANDLER(AutomationMsg_DownloadDirectory, GetDownloadDirectory)
[email protected]14c0a032009-04-13 18:15:14995 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_OpenNewBrowserWindow,
[email protected]1c58a5c2009-05-21 18:47:14996 OpenNewBrowserWindow)
997 IPC_MESSAGE_HANDLER(AutomationMsg_WindowForBrowser, GetWindowForBrowser)
[email protected]71f65dd2009-02-11 19:14:56998 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditForBrowser,
[email protected]1c58a5c2009-05-21 18:47:14999 GetAutocompleteEditForBrowser)
1000 IPC_MESSAGE_HANDLER(AutomationMsg_BrowserForWindow, GetBrowserForWindow)
[email protected]de246f52009-02-25 18:25:451001#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291002 IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab)
[email protected]d2cc6ed2009-04-24 00:26:171003#endif
[email protected]71f65dd2009-02-11 19:14:561004 IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab,
initial.commit09911bf2008-07-26 23:55:291005 NavigateInExternalTab)
[email protected]71f65dd2009-02-11 19:14:561006 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ShowInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:141007 ShowInterstitialPage)
[email protected]71f65dd2009-02-11 19:14:561008 IPC_MESSAGE_HANDLER(AutomationMsg_HideInterstitialPage,
[email protected]1c58a5c2009-05-21 18:47:141009 HideInterstitialPage)
[email protected]d2cc6ed2009-04-24 00:26:171010#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291011 IPC_MESSAGE_HANDLER(AutomationMsg_SetAcceleratorsForTab,
1012 SetAcceleratorsForTab)
1013 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator,
1014 ProcessUnhandledAccelerator)
[email protected]d2cc6ed2009-04-24 00:26:171015#endif
[email protected]71f65dd2009-02-11 19:14:561016 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForTabToBeRestored,
1017 WaitForTabToBeRestored)
[email protected]1c58a5c2009-05-21 18:47:141018 IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus)
[email protected]d2cc6ed2009-04-24 00:26:171019#if defined(OS_WIN)
[email protected]87eab222009-03-13 00:47:451020 IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition)
[email protected]d2cc6ed2009-04-24 00:26:171021#endif
[email protected]1c58a5c2009-05-21 18:47:141022 IPC_MESSAGE_HANDLER(AutomationMsg_GetSecurityState, GetSecurityState)
1023 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageType, GetPageType)
[email protected]71f65dd2009-02-11 19:14:561024 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ActionOnSSLBlockingPage,
1025 ActionOnSSLBlockingPage)
initial.commit09911bf2008-07-26 23:55:291026 IPC_MESSAGE_HANDLER(AutomationMsg_BringBrowserToFront, BringBrowserToFront)
1027 IPC_MESSAGE_HANDLER(AutomationMsg_IsPageMenuCommandEnabled,
1028 IsPageMenuCommandEnabled)
[email protected]71f65dd2009-02-11 19:14:561029 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_PrintNow, PrintNow)
1030 IPC_MESSAGE_HANDLER(AutomationMsg_SavePage, SavePage)
1031 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetText,
initial.commit09911bf2008-07-26 23:55:291032 GetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:561033 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditSetText,
initial.commit09911bf2008-07-26 23:55:291034 SetAutocompleteEditText)
[email protected]71f65dd2009-02-11 19:14:561035 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditIsQueryInProgress,
initial.commit09911bf2008-07-26 23:55:291036 AutocompleteEditIsQueryInProgress)
[email protected]71f65dd2009-02-11 19:14:561037 IPC_MESSAGE_HANDLER(AutomationMsg_AutocompleteEditGetMatches,
initial.commit09911bf2008-07-26 23:55:291038 AutocompleteEditGetMatches)
[email protected]71f65dd2009-02-11 19:14:561039 IPC_MESSAGE_HANDLER(AutomationMsg_ConstrainedWindowBounds,
initial.commit09911bf2008-07-26 23:55:291040 GetConstrainedWindowBounds)
[email protected]71f65dd2009-02-11 19:14:561041 IPC_MESSAGE_HANDLER(AutomationMsg_OpenFindInPage,
[email protected]5f8af2a2008-08-06 22:49:451042 HandleOpenFindInPageRequest)
[email protected]18cb2572008-08-21 20:34:451043 IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost,
1044 OnMessageFromExternalHost)
[email protected]1c58a5c2009-05-21 18:47:141045 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest)
[email protected]71f65dd2009-02-11 19:14:561046 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowVisibility,
[email protected]20e93d12008-08-28 16:31:571047 GetFindWindowVisibility)
[email protected]71f65dd2009-02-11 19:14:561048 IPC_MESSAGE_HANDLER(AutomationMsg_FindWindowLocation,
[email protected]20e93d12008-08-28 16:31:571049 HandleFindWindowLocationRequest)
[email protected]71f65dd2009-02-11 19:14:561050 IPC_MESSAGE_HANDLER(AutomationMsg_BookmarkBarVisibility,
1051 GetBookmarkBarVisibility)
[email protected]1c58a5c2009-05-21 18:47:141052 IPC_MESSAGE_HANDLER(AutomationMsg_GetSSLInfoBarCount, GetSSLInfoBarCount)
[email protected]71f65dd2009-02-11 19:14:561053 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_ClickSSLInfoBarLink,
1054 ClickSSLInfoBarLink)
1055 IPC_MESSAGE_HANDLER(AutomationMsg_GetLastNavigationTime,
[email protected]8a3422c92008-09-24 17:42:421056 GetLastNavigationTime)
[email protected]71f65dd2009-02-11 19:14:561057 IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForNavigation,
1058 WaitForNavigation)
[email protected]1c58a5c2009-05-21 18:47:141059 IPC_MESSAGE_HANDLER(AutomationMsg_SetIntPreference, SetIntPreference)
[email protected]71f65dd2009-02-11 19:14:561060 IPC_MESSAGE_HANDLER(AutomationMsg_ShowingAppModalDialog,
[email protected]c274acc2008-11-11 20:13:441061 GetShowingAppModalDialog)
[email protected]71f65dd2009-02-11 19:14:561062 IPC_MESSAGE_HANDLER(AutomationMsg_ClickAppModalDialogButton,
[email protected]fad84eab2008-12-05 00:37:201063 ClickAppModalDialogButton)
[email protected]1c58a5c2009-05-21 18:47:141064 IPC_MESSAGE_HANDLER(AutomationMsg_SetStringPreference, SetStringPreference)
[email protected]71f65dd2009-02-11 19:14:561065 IPC_MESSAGE_HANDLER(AutomationMsg_GetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:161066 GetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:561067 IPC_MESSAGE_HANDLER(AutomationMsg_SetBooleanPreference,
[email protected]97fa6ce32008-12-19 01:48:161068 SetBooleanPreference)
[email protected]71f65dd2009-02-11 19:14:561069 IPC_MESSAGE_HANDLER(AutomationMsg_GetPageCurrentEncoding,
[email protected]97fa6ce32008-12-19 01:48:161070 GetPageCurrentEncoding)
[email protected]1c58a5c2009-05-21 18:47:141071 IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding)
[email protected]5bcdb312009-01-07 21:43:201072 IPC_MESSAGE_HANDLER(AutomationMsg_SavePackageShouldPromptUser,
1073 SavePackageShouldPromptUser)
[email protected]1c58a5c2009-05-21 18:47:141074 IPC_MESSAGE_HANDLER(AutomationMsg_WindowTitle, GetWindowTitle)
[email protected]b83e4602009-05-15 22:58:331075 IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation,
1076 SetEnableExtensionAutomation)
initial.commit09911bf2008-07-26 23:55:291077 IPC_END_MESSAGE_MAP()
1078}
1079
[email protected]71f65dd2009-02-11 19:14:561080void AutomationProvider::ActivateTab(int handle, int at_index, int* status) {
1081 *status = -1;
initial.commit09911bf2008-07-26 23:55:291082 if (browser_tracker_->ContainsHandle(handle) && at_index > -1) {
1083 Browser* browser = browser_tracker_->GetResource(handle);
1084 if (at_index >= 0 && at_index < browser->tab_count()) {
1085 browser->SelectTabContentsAt(at_index, true);
[email protected]71f65dd2009-02-11 19:14:561086 *status = 0;
initial.commit09911bf2008-07-26 23:55:291087 }
1088 }
initial.commit09911bf2008-07-26 23:55:291089}
1090
[email protected]71f65dd2009-02-11 19:14:561091void AutomationProvider::AppendTab(int handle, const GURL& url,
1092 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291093 int append_tab_response = -1; // -1 is the error code
1094 NotificationObserver* observer = NULL;
1095
1096 if (browser_tracker_->ContainsHandle(handle)) {
1097 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1c58a5c2009-05-21 18:47:141098 observer = AddTabStripObserver(browser, reply_message);
[email protected]22735af62009-04-07 21:09:581099 TabContents* tab_contents = browser->AddTabWithURL(url, GURL(),
1100 PageTransition::TYPED,
[email protected]5a4940be2009-05-06 06:44:391101 true, -1, false, NULL);
initial.commit09911bf2008-07-26 23:55:291102 if (tab_contents) {
1103 append_tab_response =
[email protected]ce3fa3c2009-04-20 19:55:571104 GetIndexForNavigationController(&tab_contents->controller(), browser);
initial.commit09911bf2008-07-26 23:55:291105 }
1106 }
1107
1108 if (append_tab_response < 0) {
1109 // The append tab failed. Remove the TabStripObserver
1110 if (observer) {
1111 RemoveTabStripObserver(observer);
1112 delete observer;
1113 }
1114
[email protected]71f65dd2009-02-11 19:14:561115 AutomationMsg_AppendTab::WriteReplyParams(reply_message,
1116 append_tab_response);
1117 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291118 }
1119}
1120
[email protected]71f65dd2009-02-11 19:14:561121void AutomationProvider::NavigateToURL(int handle, const GURL& url,
1122 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291123 if (tab_tracker_->ContainsHandle(handle)) {
1124 NavigationController* tab = tab_tracker_->GetResource(handle);
1125
1126 // Simulate what a user would do. Activate the tab and then navigate.
1127 // We could allow navigating in a background tab in future.
1128 Browser* browser = FindAndActivateTab(tab);
1129
1130 if (browser) {
[email protected]71f65dd2009-02-11 19:14:561131 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1132 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1133 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1134 AUTOMATION_MSG_NAVIGATION_ERROR);
1135
initial.commit09911bf2008-07-26 23:55:291136 // TODO(darin): avoid conversion to GURL
[email protected]c0588052008-10-27 23:01:501137 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
initial.commit09911bf2008-07-26 23:55:291138 return;
1139 }
1140 }
[email protected]71f65dd2009-02-11 19:14:561141
1142 AutomationMsg_NavigateToURL::WriteReplyParams(
1143 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1144 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291145}
1146
[email protected]71f65dd2009-02-11 19:14:561147void AutomationProvider::NavigationAsync(int handle, const GURL& url,
1148 bool* status) {
1149 *status = false;
initial.commit09911bf2008-07-26 23:55:291150
1151 if (tab_tracker_->ContainsHandle(handle)) {
1152 NavigationController* tab = tab_tracker_->GetResource(handle);
1153
1154 // Simulate what a user would do. Activate the tab and then navigate.
1155 // We could allow navigating in a background tab in future.
1156 Browser* browser = FindAndActivateTab(tab);
1157
1158 if (browser) {
1159 // Don't add any listener unless a callback mechanism is desired.
1160 // TODO(vibhor): Do this if such a requirement arises in future.
[email protected]c0588052008-10-27 23:01:501161 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:561162 *status = true;
initial.commit09911bf2008-07-26 23:55:291163 }
1164 }
initial.commit09911bf2008-07-26 23:55:291165}
1166
[email protected]71f65dd2009-02-11 19:14:561167void AutomationProvider::GoBack(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291168 if (tab_tracker_->ContainsHandle(handle)) {
1169 NavigationController* tab = tab_tracker_->GetResource(handle);
1170 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:141171 if (browser && browser->command_updater()->IsCommandEnabled(IDC_BACK)) {
[email protected]71f65dd2009-02-11 19:14:561172 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1173 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1174 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1175 AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]485fba42009-03-24 23:27:291176 browser->GoBack(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:291177 return;
1178 }
1179 }
[email protected]71f65dd2009-02-11 19:14:561180
1181 AutomationMsg_GoBack::WriteReplyParams(
1182 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1183 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291184}
1185
[email protected]71f65dd2009-02-11 19:14:561186void AutomationProvider::GoForward(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291187 if (tab_tracker_->ContainsHandle(handle)) {
1188 NavigationController* tab = tab_tracker_->GetResource(handle);
1189 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:141190 if (browser && browser->command_updater()->IsCommandEnabled(IDC_FORWARD)) {
[email protected]71f65dd2009-02-11 19:14:561191 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1192 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1193 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1194 AUTOMATION_MSG_NAVIGATION_ERROR);
[email protected]485fba42009-03-24 23:27:291195 browser->GoForward(CURRENT_TAB);
initial.commit09911bf2008-07-26 23:55:291196 return;
1197 }
1198 }
[email protected]71f65dd2009-02-11 19:14:561199
1200 AutomationMsg_GoForward::WriteReplyParams(
1201 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1202 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291203}
1204
[email protected]71f65dd2009-02-11 19:14:561205void AutomationProvider::Reload(int handle, IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291206 if (tab_tracker_->ContainsHandle(handle)) {
1207 NavigationController* tab = tab_tracker_->GetResource(handle);
1208 Browser* browser = FindAndActivateTab(tab);
[email protected]1fc025202009-01-20 23:03:141209 if (browser && browser->command_updater()->IsCommandEnabled(IDC_RELOAD)) {
[email protected]71f65dd2009-02-11 19:14:561210 AddNavigationStatusListener<AutomationMsg_NavigationResponseValues>(
1211 tab, reply_message, AUTOMATION_MSG_NAVIGATION_SUCCESS,
1212 AUTOMATION_MSG_NAVIGATION_AUTH_NEEDED,
1213 AUTOMATION_MSG_NAVIGATION_ERROR);
initial.commit09911bf2008-07-26 23:55:291214 browser->Reload();
1215 return;
1216 }
1217 }
[email protected]71f65dd2009-02-11 19:14:561218
1219 AutomationMsg_Reload::WriteReplyParams(
1220 reply_message, AUTOMATION_MSG_NAVIGATION_ERROR);
1221 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291222}
1223
[email protected]71f65dd2009-02-11 19:14:561224void AutomationProvider::SetAuth(int tab_handle,
initial.commit09911bf2008-07-26 23:55:291225 const std::wstring& username,
[email protected]71f65dd2009-02-11 19:14:561226 const std::wstring& password,
1227 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291228 int status = -1;
1229
1230 if (tab_tracker_->ContainsHandle(tab_handle)) {
1231 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1232 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1233
1234 if (iter != login_handler_map_.end()) {
1235 // If auth is needed again after this, assume login has failed. This is
1236 // not strictly correct, because a navigation can require both proxy and
1237 // server auth, but it should be OK for now.
1238 LoginHandler* handler = iter->second;
[email protected]71f65dd2009-02-11 19:14:561239 AddNavigationStatusListener<int>(tab, reply_message, 0, -1, -1);
initial.commit09911bf2008-07-26 23:55:291240 handler->SetAuth(username, password);
1241 status = 0;
1242 }
1243 }
[email protected]de246f52009-02-25 18:25:451244
initial.commit09911bf2008-07-26 23:55:291245 if (status < 0) {
[email protected]71f65dd2009-02-11 19:14:561246 AutomationMsg_SetAuth::WriteReplyParams(reply_message, status);
1247 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291248 }
1249}
1250
[email protected]71f65dd2009-02-11 19:14:561251void AutomationProvider::CancelAuth(int tab_handle,
1252 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291253 int status = -1;
1254
1255 if (tab_tracker_->ContainsHandle(tab_handle)) {
1256 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1257 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1258
1259 if (iter != login_handler_map_.end()) {
1260 // If auth is needed again after this, something is screwy.
1261 LoginHandler* handler = iter->second;
[email protected]71f65dd2009-02-11 19:14:561262 AddNavigationStatusListener<int>(tab, reply_message, 0, -1, -1);
initial.commit09911bf2008-07-26 23:55:291263 handler->CancelAuth();
1264 status = 0;
1265 }
1266 }
[email protected]de246f52009-02-25 18:25:451267
initial.commit09911bf2008-07-26 23:55:291268 if (status < 0) {
[email protected]71f65dd2009-02-11 19:14:561269 AutomationMsg_CancelAuth::WriteReplyParams(reply_message, status);
1270 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291271 }
1272}
1273
[email protected]71f65dd2009-02-11 19:14:561274void AutomationProvider::NeedsAuth(int tab_handle, bool* needs_auth) {
1275 *needs_auth = false;
initial.commit09911bf2008-07-26 23:55:291276
1277 if (tab_tracker_->ContainsHandle(tab_handle)) {
1278 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1279 LoginHandlerMap::iterator iter = login_handler_map_.find(tab);
1280
1281 if (iter != login_handler_map_.end()) {
1282 // The LoginHandler will be in our map IFF the tab needs auth.
[email protected]71f65dd2009-02-11 19:14:561283 *needs_auth = true;
initial.commit09911bf2008-07-26 23:55:291284 }
1285 }
initial.commit09911bf2008-07-26 23:55:291286}
1287
[email protected]71f65dd2009-02-11 19:14:561288void AutomationProvider::GetRedirectsFrom(int tab_handle,
1289 const GURL& source_url,
1290 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291291 DCHECK(!redirect_query_) << "Can only handle one redirect query at once.";
1292 if (tab_tracker_->ContainsHandle(tab_handle)) {
1293 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
1294 HistoryService* history_service =
1295 tab->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
1296
1297 DCHECK(history_service) << "Tab " << tab_handle << "'s profile " <<
1298 "has no history service";
1299 if (history_service) {
[email protected]71f65dd2009-02-11 19:14:561300 DCHECK(reply_message_ == NULL);
1301 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291302 // Schedule a history query for redirects. The response will be sent
1303 // asynchronously from the callback the history system uses to notify us
1304 // that it's done: OnRedirectQueryComplete.
initial.commit09911bf2008-07-26 23:55:291305 redirect_query_ = history_service->QueryRedirectsFrom(
1306 source_url, &consumer_,
1307 NewCallback(this, &AutomationProvider::OnRedirectQueryComplete));
1308 return; // Response will be sent when query completes.
1309 }
1310 }
1311
1312 // Send failure response.
[email protected]deb57402009-02-06 01:35:301313 std::vector<GURL> empty;
[email protected]71f65dd2009-02-11 19:14:561314 AutomationMsg_RedirectsFrom::WriteReplyParams(reply_message, false, empty);
1315 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291316}
1317
[email protected]71f65dd2009-02-11 19:14:561318void AutomationProvider::GetActiveTabIndex(int handle, int* active_tab_index) {
1319 *active_tab_index = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291320 if (browser_tracker_->ContainsHandle(handle)) {
1321 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561322 *active_tab_index = browser->selected_index();
initial.commit09911bf2008-07-26 23:55:291323 }
initial.commit09911bf2008-07-26 23:55:291324}
1325
[email protected]71f65dd2009-02-11 19:14:561326void AutomationProvider::GetBrowserWindowCount(int* window_count) {
1327 *window_count = static_cast<int>(BrowserList::size());
initial.commit09911bf2008-07-26 23:55:291328}
1329
[email protected]24497032009-05-01 17:00:291330void AutomationProvider::GetNormalBrowserWindowCount(int* window_count) {
1331 *window_count = static_cast<int>(
1332 BrowserList::GetBrowserCountForType(profile_, Browser::TYPE_NORMAL));
1333}
1334
[email protected]71f65dd2009-02-11 19:14:561335void AutomationProvider::GetShowingAppModalDialog(bool* showing_dialog,
1336 int* dialog_button) {
[email protected]0bfa713f2009-04-07 20:18:281337 AppModalDialog* dialog_delegate = AppModalDialogQueue::active_dialog();
[email protected]b3a70332009-02-25 02:40:501338 *showing_dialog = (dialog_delegate != NULL);
1339 if (*showing_dialog)
1340 *dialog_button = dialog_delegate->GetDialogButtons();
1341 else
[email protected]478ff2ed2009-04-21 23:49:181342 *dialog_button = MessageBoxFlags::DIALOGBUTTON_NONE;
[email protected]fad84eab2008-12-05 00:37:201343}
1344
[email protected]71f65dd2009-02-11 19:14:561345void AutomationProvider::ClickAppModalDialogButton(int button, bool* success) {
1346 *success = false;
[email protected]fad84eab2008-12-05 00:37:201347
[email protected]0bfa713f2009-04-07 20:18:281348 AppModalDialog* dialog_delegate = AppModalDialogQueue::active_dialog();
[email protected]b3a70332009-02-25 02:40:501349 if (dialog_delegate &&
1350 (dialog_delegate->GetDialogButtons() & button) == button) {
[email protected]478ff2ed2009-04-21 23:49:181351 if ((button & MessageBoxFlags::DIALOGBUTTON_OK) ==
1352 MessageBoxFlags::DIALOGBUTTON_OK) {
[email protected]0bfa713f2009-04-07 20:18:281353 dialog_delegate->AcceptWindow();
[email protected]71f65dd2009-02-11 19:14:561354 *success = true;
[email protected]fad84eab2008-12-05 00:37:201355 }
[email protected]478ff2ed2009-04-21 23:49:181356 if ((button & MessageBoxFlags::DIALOGBUTTON_CANCEL) ==
1357 MessageBoxFlags::DIALOGBUTTON_CANCEL) {
[email protected]71f65dd2009-02-11 19:14:561358 DCHECK(!*success) << "invalid param, OK and CANCEL specified";
[email protected]0bfa713f2009-04-07 20:18:281359 dialog_delegate->CancelWindow();
[email protected]71f65dd2009-02-11 19:14:561360 *success = true;
[email protected]fad84eab2008-12-05 00:37:201361 }
1362 }
[email protected]c274acc2008-11-11 20:13:441363}
1364
[email protected]71f65dd2009-02-11 19:14:561365void AutomationProvider::GetBrowserWindow(int index, int* handle) {
1366 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291367 if (index >= 0) {
1368 BrowserList::const_iterator iter = BrowserList::begin();
[email protected]24497032009-05-01 17:00:291369 for (; (iter != BrowserList::end()) && (index > 0); ++iter, --index);
initial.commit09911bf2008-07-26 23:55:291370 if (iter != BrowserList::end()) {
[email protected]71f65dd2009-02-11 19:14:561371 *handle = browser_tracker_->Add(*iter);
initial.commit09911bf2008-07-26 23:55:291372 }
1373 }
initial.commit09911bf2008-07-26 23:55:291374}
1375
[email protected]24497032009-05-01 17:00:291376void AutomationProvider::FindNormalBrowserWindow(int* handle) {
1377 *handle = 0;
1378 Browser* browser = BrowserList::FindBrowserWithType(profile_,
1379 Browser::TYPE_NORMAL);
1380 if (browser)
1381 *handle = browser_tracker_->Add(browser);
1382}
1383
[email protected]71f65dd2009-02-11 19:14:561384void AutomationProvider::GetLastActiveBrowserWindow(int* handle) {
1385 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291386 Browser* browser = BrowserList::GetLastActive();
1387 if (browser)
[email protected]71f65dd2009-02-11 19:14:561388 *handle = browser_tracker_->Add(browser);
initial.commit09911bf2008-07-26 23:55:291389}
1390
[email protected]de246f52009-02-25 18:25:451391#if defined(OS_WIN)
1392// TODO(port): Remove windowsisms.
initial.commit09911bf2008-07-26 23:55:291393BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM l_param) {
1394 if (hwnd == reinterpret_cast<HWND>(l_param)) {
1395 return FALSE;
1396 }
1397 return TRUE;
1398}
1399
[email protected]71f65dd2009-02-11 19:14:561400void AutomationProvider::GetActiveWindow(int* handle) {
initial.commit09911bf2008-07-26 23:55:291401 HWND window = GetForegroundWindow();
1402
1403 // Let's make sure this window belongs to our process.
1404 if (EnumThreadWindows(::GetCurrentThreadId(),
1405 EnumThreadWndProc,
1406 reinterpret_cast<LPARAM>(window))) {
1407 // We enumerated all the windows and did not find the foreground window,
1408 // it is not our window, ignore it.
[email protected]71f65dd2009-02-11 19:14:561409 *handle = 0;
initial.commit09911bf2008-07-26 23:55:291410 return;
1411 }
1412
[email protected]71f65dd2009-02-11 19:14:561413 *handle = window_tracker_->Add(window);
initial.commit09911bf2008-07-26 23:55:291414}
1415
[email protected]71f65dd2009-02-11 19:14:561416void AutomationProvider::GetWindowHWND(int handle, HWND* win32_handle) {
1417 *win32_handle = window_tracker_->GetResource(handle);
initial.commit09911bf2008-07-26 23:55:291418}
[email protected]de246f52009-02-25 18:25:451419#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291420
[email protected]4f6381ee2009-04-16 02:46:331421void AutomationProvider::ExecuteBrowserCommandAsync(int handle, int command,
1422 bool* success) {
[email protected]71f65dd2009-02-11 19:14:561423 *success = false;
[email protected]4ae62752008-08-04 23:28:471424 if (browser_tracker_->ContainsHandle(handle)) {
1425 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]1fc025202009-01-20 23:03:141426 if (browser->command_updater()->SupportsCommand(command) &&
1427 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]4ae62752008-08-04 23:28:471428 browser->ExecuteCommand(command);
[email protected]71f65dd2009-02-11 19:14:561429 *success = true;
[email protected]4ae62752008-08-04 23:28:471430 }
1431 }
[email protected]4ae62752008-08-04 23:28:471432}
1433
[email protected]4f6381ee2009-04-16 02:46:331434void AutomationProvider::ExecuteBrowserCommand(
[email protected]56e71b7c2009-03-27 03:05:561435 int handle, int command, IPC::Message* reply_message) {
1436 if (browser_tracker_->ContainsHandle(handle)) {
1437 Browser* browser = browser_tracker_->GetResource(handle);
1438 if (browser->command_updater()->SupportsCommand(command) &&
1439 browser->command_updater()->IsCommandEnabled(command)) {
[email protected]d79ffea2009-05-07 20:51:421440 if (ExecuteBrowserCommandObserver::CreateAndRegisterObserver(
1441 this, browser, command, reply_message)) {
[email protected]4e41709d2009-04-08 00:04:271442 browser->ExecuteCommand(command);
[email protected]d79ffea2009-05-07 20:51:421443 return;
1444 }
[email protected]56e71b7c2009-03-27 03:05:561445 }
1446 }
[email protected]49a14a82009-03-31 04:16:441447 AutomationMsg_WindowExecuteCommand::WriteReplyParams(reply_message, false);
[email protected]56e71b7c2009-03-27 03:05:561448 Send(reply_message);
1449}
1450
[email protected]71f65dd2009-02-11 19:14:561451void AutomationProvider::WindowGetViewBounds(int handle, int view_id,
1452 bool screen_coordinates,
1453 bool* success,
1454 gfx::Rect* bounds) {
1455 *success = false;
initial.commit09911bf2008-07-26 23:55:291456
[email protected]de246f52009-02-25 18:25:451457#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291458 void* iter = NULL;
1459 if (window_tracker_->ContainsHandle(handle)) {
1460 HWND hwnd = window_tracker_->GetResource(handle);
[email protected]a0dde122008-11-21 20:51:201461 views::RootView* root_view = views::WidgetWin::FindRootView(hwnd);
initial.commit09911bf2008-07-26 23:55:291462 if (root_view) {
[email protected]c2dacc92008-10-16 23:51:381463 views::View* view = root_view->GetViewByID(view_id);
initial.commit09911bf2008-07-26 23:55:291464 if (view) {
[email protected]71f65dd2009-02-11 19:14:561465 *success = true;
[email protected]96b667d2008-10-14 20:58:441466 gfx::Point point;
initial.commit09911bf2008-07-26 23:55:291467 if (screen_coordinates)
[email protected]c2dacc92008-10-16 23:51:381468 views::View::ConvertPointToScreen(view, &point);
initial.commit09911bf2008-07-26 23:55:291469 else
[email protected]c2dacc92008-10-16 23:51:381470 views::View::ConvertPointToView(view, root_view, &point);
[email protected]71f65dd2009-02-11 19:14:561471 *bounds = view->GetLocalBounds(false);
1472 bounds->set_origin(point);
initial.commit09911bf2008-07-26 23:55:291473 }
1474 }
1475 }
[email protected]de246f52009-02-25 18:25:451476#else
1477 // TODO(port): Enable when window_tracker is ported.
1478 NOTIMPLEMENTED();
1479#endif
initial.commit09911bf2008-07-26 23:55:291480}
1481
[email protected]de246f52009-02-25 18:25:451482#if defined(OS_WIN)
1483// TODO(port): Use portable replacement for POINT.
1484
initial.commit09911bf2008-07-26 23:55:291485// This task enqueues a mouse event on the event loop, so that the view
1486// that it's being sent to can do the requisite post-processing.
1487class MouseEventTask : public Task {
1488 public:
[email protected]c2dacc92008-10-16 23:51:381489 MouseEventTask(views::View* view,
1490 views::Event::EventType type,
initial.commit09911bf2008-07-26 23:55:291491 POINT point,
1492 int flags)
1493 : view_(view), type_(type), point_(point), flags_(flags) {}
1494 virtual ~MouseEventTask() {}
1495
1496 virtual void Run() {
[email protected]c2dacc92008-10-16 23:51:381497 views::MouseEvent event(type_, point_.x, point_.y, flags_);
initial.commit09911bf2008-07-26 23:55:291498 // We need to set the cursor position before we process the event because
1499 // some code (tab dragging, for instance) queries the actual cursor location
1500 // rather than the location of the mouse event. Note that the reason why
1501 // the drag code moved away from using mouse event locations was because
1502 // our conversion to screen location doesn't work well with multiple
1503 // monitors, so this only works reliably in a single monitor setup.
[email protected]96b667d2008-10-14 20:58:441504 gfx::Point screen_location(point_.x, point_.y);
initial.commit09911bf2008-07-26 23:55:291505 view_->ConvertPointToScreen(view_, &screen_location);
[email protected]96b667d2008-10-14 20:58:441506 ::SetCursorPos(screen_location.x(), screen_location.y());
initial.commit09911bf2008-07-26 23:55:291507 switch (type_) {
[email protected]c2dacc92008-10-16 23:51:381508 case views::Event::ET_MOUSE_PRESSED:
initial.commit09911bf2008-07-26 23:55:291509 view_->OnMousePressed(event);
1510 break;
1511
[email protected]c2dacc92008-10-16 23:51:381512 case views::Event::ET_MOUSE_DRAGGED:
initial.commit09911bf2008-07-26 23:55:291513 view_->OnMouseDragged(event);
1514 break;
1515
[email protected]c2dacc92008-10-16 23:51:381516 case views::Event::ET_MOUSE_RELEASED:
initial.commit09911bf2008-07-26 23:55:291517 view_->OnMouseReleased(event, false);
1518 break;
1519
1520 default:
1521 NOTREACHED();
1522 }
1523 }
1524
1525 private:
[email protected]c2dacc92008-10-16 23:51:381526 views::View* view_;
1527 views::Event::EventType type_;
initial.commit09911bf2008-07-26 23:55:291528 POINT point_;
1529 int flags_;
1530
[email protected]5a52f162008-08-27 04:15:311531 DISALLOW_COPY_AND_ASSIGN(MouseEventTask);
initial.commit09911bf2008-07-26 23:55:291532};
1533
[email protected]c2dacc92008-10-16 23:51:381534void AutomationProvider::ScheduleMouseEvent(views::View* view,
1535 views::Event::EventType type,
initial.commit09911bf2008-07-26 23:55:291536 POINT point,
1537 int flags) {
1538 MessageLoop::current()->PostTask(FROM_HERE,
1539 new MouseEventTask(view, type, point, flags));
1540}
[email protected]de246f52009-02-25 18:25:451541#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291542
1543// This task just adds another task to the event queue. This is useful if
1544// you want to ensure that any tasks added to the event queue after this one
1545// have already been processed by the time |task| is run.
1546class InvokeTaskLaterTask : public Task {
1547 public:
1548 explicit InvokeTaskLaterTask(Task* task) : task_(task) {}
1549 virtual ~InvokeTaskLaterTask() {}
1550
1551 virtual void Run() {
1552 MessageLoop::current()->PostTask(FROM_HERE, task_);
1553 }
1554
1555 private:
1556 Task* task_;
1557
[email protected]5a52f162008-08-27 04:15:311558 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask);
initial.commit09911bf2008-07-26 23:55:291559};
1560
[email protected]de246f52009-02-25 18:25:451561#if defined(OS_WIN)
1562// TODO(port): Replace POINT and other windowsisms.
1563
initial.commit09911bf2008-07-26 23:55:291564// This task sends a WindowDragResponse message with the appropriate
1565// routing ID to the automation proxy. This is implemented as a task so that
1566// we know that the mouse events (and any tasks that they spawn on the message
1567// loop) have been processed by the time this is sent.
1568class WindowDragResponseTask : public Task {
1569 public:
[email protected]1c58a5c2009-05-21 18:47:141570 WindowDragResponseTask(AutomationProvider* provider,
[email protected]71f65dd2009-02-11 19:14:561571 IPC::Message* reply_message)
[email protected]1c58a5c2009-05-21 18:47:141572 : provider_(provider), reply_message_(reply_message) {}
initial.commit09911bf2008-07-26 23:55:291573 virtual ~WindowDragResponseTask() {}
1574
1575 virtual void Run() {
[email protected]71f65dd2009-02-11 19:14:561576 DCHECK(reply_message_ != NULL);
1577 AutomationMsg_WindowDrag::WriteReplyParams(reply_message_, true);
1578 provider_->Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:291579 }
1580
1581 private:
1582 AutomationProvider* provider_;
[email protected]71f65dd2009-02-11 19:14:561583 IPC::Message* reply_message_;
initial.commit09911bf2008-07-26 23:55:291584
[email protected]5a52f162008-08-27 04:15:311585 DISALLOW_COPY_AND_ASSIGN(WindowDragResponseTask);
initial.commit09911bf2008-07-26 23:55:291586};
1587
1588void AutomationProvider::WindowSimulateClick(const IPC::Message& message,
1589 int handle,
1590 POINT click,
1591 int flags) {
1592 HWND hwnd = 0;
1593
1594 if (window_tracker_->ContainsHandle(handle)) {
1595 hwnd = window_tracker_->GetResource(handle);
1596
initial.commit09911bf2008-07-26 23:55:291597 ui_controls::SendMouseMove(click.x, click.y);
1598
1599 ui_controls::MouseButton button = ui_controls::LEFT;
[email protected]c2dacc92008-10-16 23:51:381600 if ((flags & views::Event::EF_LEFT_BUTTON_DOWN) ==
1601 views::Event::EF_LEFT_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291602 button = ui_controls::LEFT;
[email protected]c2dacc92008-10-16 23:51:381603 } else if ((flags & views::Event::EF_RIGHT_BUTTON_DOWN) ==
1604 views::Event::EF_RIGHT_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291605 button = ui_controls::RIGHT;
[email protected]c2dacc92008-10-16 23:51:381606 } else if ((flags & views::Event::EF_MIDDLE_BUTTON_DOWN) ==
1607 views::Event::EF_MIDDLE_BUTTON_DOWN) {
initial.commit09911bf2008-07-26 23:55:291608 button = ui_controls::MIDDLE;
1609 } else {
1610 NOTREACHED();
1611 }
1612 ui_controls::SendMouseClick(button);
1613 }
1614}
1615
[email protected]71f65dd2009-02-11 19:14:561616void AutomationProvider::WindowSimulateDrag(int handle,
[email protected]fe92e4e2008-11-18 21:31:321617 std::vector<POINT> drag_path,
[email protected]5e0f30c2008-08-14 22:52:441618 int flags,
[email protected]71f65dd2009-02-11 19:14:561619 bool press_escape_en_route,
1620 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291621 bool succeeded = false;
[email protected]fe92e4e2008-11-18 21:31:321622 if (browser_tracker_->ContainsHandle(handle) && (drag_path.size() > 1)) {
1623 succeeded = true;
1624
1625 UINT down_message = 0;
1626 UINT up_message = 0;
1627 WPARAM wparam_flags = 0;
1628 if (flags & views::Event::EF_SHIFT_DOWN)
1629 wparam_flags |= MK_SHIFT;
1630 if (flags & views::Event::EF_CONTROL_DOWN)
1631 wparam_flags |= MK_CONTROL;
1632 if (flags & views::Event::EF_LEFT_BUTTON_DOWN) {
1633 wparam_flags |= MK_LBUTTON;
1634 down_message = WM_LBUTTONDOWN;
1635 up_message = WM_LBUTTONUP;
1636 }
1637 if (flags & views::Event::EF_MIDDLE_BUTTON_DOWN) {
1638 wparam_flags |= MK_MBUTTON;
1639 down_message = WM_MBUTTONDOWN;
1640 up_message = WM_MBUTTONUP;
1641 }
1642 if (flags & views::Event::EF_RIGHT_BUTTON_DOWN) {
1643 wparam_flags |= MK_RBUTTON;
1644 down_message = WM_LBUTTONDOWN;
1645 up_message = WM_LBUTTONUP;
1646 }
1647
initial.commit09911bf2008-07-26 23:55:291648 Browser* browser = browser_tracker_->GetResource(handle);
1649 DCHECK(browser);
[email protected]0a6fb3f2008-11-18 21:39:551650 HWND top_level_hwnd =
1651 reinterpret_cast<HWND>(browser->window()->GetNativeHandle());
[email protected]fe92e4e2008-11-18 21:31:321652 POINT temp = drag_path[0];
1653 MapWindowPoints(top_level_hwnd, HWND_DESKTOP, &temp, 1);
1654 SetCursorPos(temp.x, temp.y);
1655 SendMessage(top_level_hwnd, down_message, wparam_flags,
1656 MAKELPARAM(drag_path[0].x, drag_path[0].y));
1657 for (int i = 1; i < static_cast<int>(drag_path.size()); ++i) {
1658 temp = drag_path[i];
1659 MapWindowPoints(top_level_hwnd, HWND_DESKTOP, &temp, 1);
1660 SetCursorPos(temp.x, temp.y);
1661 SendMessage(top_level_hwnd, WM_MOUSEMOVE, wparam_flags,
1662 MAKELPARAM(drag_path[i].x, drag_path[i].y));
[email protected]f7a391a12008-11-10 21:29:341663 }
[email protected]fe92e4e2008-11-18 21:31:321664 POINT end = drag_path[drag_path.size() - 1];
1665 MapWindowPoints(top_level_hwnd, HWND_DESKTOP, &end, 1);
1666 SetCursorPos(end.x, end.y);
1667
1668 if (press_escape_en_route) {
1669 // Press Escape.
1670 ui_controls::SendKeyPress(VK_ESCAPE,
1671 ((flags & views::Event::EF_CONTROL_DOWN)
1672 == views::Event::EF_CONTROL_DOWN),
1673 ((flags & views::Event::EF_SHIFT_DOWN) ==
1674 views::Event::EF_SHIFT_DOWN),
1675 ((flags & views::Event::EF_ALT_DOWN) ==
1676 views::Event::EF_ALT_DOWN));
1677 }
1678 SendMessage(top_level_hwnd, up_message, wparam_flags,
1679 MAKELPARAM(end.x, end.y));
1680
[email protected]1c58a5c2009-05-21 18:47:141681 MessageLoop::current()->PostTask(FROM_HERE, new InvokeTaskLaterTask(
1682 new WindowDragResponseTask(this, reply_message)));
initial.commit09911bf2008-07-26 23:55:291683 } else {
[email protected]71f65dd2009-02-11 19:14:561684 AutomationMsg_WindowDrag::WriteReplyParams(reply_message, true);
1685 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291686 }
1687}
1688
1689void AutomationProvider::WindowSimulateKeyPress(const IPC::Message& message,
1690 int handle,
1691 wchar_t key,
1692 int flags) {
1693 if (!window_tracker_->ContainsHandle(handle))
1694 return;
1695
1696 // The key event is sent to whatever window is active.
1697 ui_controls::SendKeyPress(key,
[email protected]c2dacc92008-10-16 23:51:381698 ((flags & views::Event::EF_CONTROL_DOWN) ==
1699 views::Event::EF_CONTROL_DOWN),
1700 ((flags & views::Event::EF_SHIFT_DOWN) ==
1701 views::Event::EF_SHIFT_DOWN),
1702 ((flags & views::Event::EF_ALT_DOWN) ==
1703 views::Event::EF_ALT_DOWN));
initial.commit09911bf2008-07-26 23:55:291704}
1705
[email protected]71f65dd2009-02-11 19:14:561706void AutomationProvider::GetFocusedViewID(int handle, int* view_id) {
1707 *view_id = -1;
initial.commit09911bf2008-07-26 23:55:291708 if (window_tracker_->ContainsHandle(handle)) {
1709 HWND hwnd = window_tracker_->GetResource(handle);
[email protected]c2dacc92008-10-16 23:51:381710 views::FocusManager* focus_manager =
1711 views::FocusManager::GetFocusManager(hwnd);
initial.commit09911bf2008-07-26 23:55:291712 DCHECK(focus_manager);
[email protected]c2dacc92008-10-16 23:51:381713 views::View* focused_view = focus_manager->GetFocusedView();
initial.commit09911bf2008-07-26 23:55:291714 if (focused_view)
[email protected]71f65dd2009-02-11 19:14:561715 *view_id = focused_view->GetID();
initial.commit09911bf2008-07-26 23:55:291716 }
initial.commit09911bf2008-07-26 23:55:291717}
1718
[email protected]71f65dd2009-02-11 19:14:561719void AutomationProvider::SetWindowVisible(int handle, bool visible,
1720 bool* result) {
initial.commit09911bf2008-07-26 23:55:291721 if (window_tracker_->ContainsHandle(handle)) {
1722 HWND hwnd = window_tracker_->GetResource(handle);
1723 ::ShowWindow(hwnd, visible ? SW_SHOW : SW_HIDE);
[email protected]71f65dd2009-02-11 19:14:561724 *result = true;
initial.commit09911bf2008-07-26 23:55:291725 } else {
[email protected]71f65dd2009-02-11 19:14:561726 *result = false;
initial.commit09911bf2008-07-26 23:55:291727 }
1728}
[email protected]d2cc6ed2009-04-24 00:26:171729#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291730
[email protected]71f65dd2009-02-11 19:14:561731void AutomationProvider::IsWindowActive(int handle, bool* success,
1732 bool* is_active) {
initial.commit09911bf2008-07-26 23:55:291733 if (window_tracker_->ContainsHandle(handle)) {
[email protected]d2cc6ed2009-04-24 00:26:171734 *is_active =
1735 platform_util::IsWindowActive(window_tracker_->GetResource(handle));
[email protected]71f65dd2009-02-11 19:14:561736 *success = true;
initial.commit09911bf2008-07-26 23:55:291737 } else {
[email protected]71f65dd2009-02-11 19:14:561738 *success = false;
1739 *is_active = false;
initial.commit09911bf2008-07-26 23:55:291740 }
1741}
1742
[email protected]d2cc6ed2009-04-24 00:26:171743// TODO(port): port this.
1744#if defined(OS_WIN)
[email protected]659d0e82009-02-13 22:43:281745void AutomationProvider::ActivateWindow(int handle) {
initial.commit09911bf2008-07-26 23:55:291746 if (window_tracker_->ContainsHandle(handle)) {
1747 ::SetActiveWindow(window_tracker_->GetResource(handle));
1748 }
1749}
[email protected]d2cc6ed2009-04-24 00:26:171750#endif
initial.commit09911bf2008-07-26 23:55:291751
[email protected]71f65dd2009-02-11 19:14:561752void AutomationProvider::GetTabCount(int handle, int* tab_count) {
1753 *tab_count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291754
1755 if (browser_tracker_->ContainsHandle(handle)) {
1756 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561757 *tab_count = browser->tab_count();
initial.commit09911bf2008-07-26 23:55:291758 }
initial.commit09911bf2008-07-26 23:55:291759}
1760
[email protected]71f65dd2009-02-11 19:14:561761void AutomationProvider::GetTab(int win_handle, int tab_index,
1762 int* tab_handle) {
[email protected]71f65dd2009-02-11 19:14:561763 *tab_handle = 0;
initial.commit09911bf2008-07-26 23:55:291764 if (browser_tracker_->ContainsHandle(win_handle) && (tab_index >= 0)) {
1765 Browser* browser = browser_tracker_->GetResource(win_handle);
1766 if (tab_index < browser->tab_count()) {
1767 TabContents* tab_contents =
1768 browser->GetTabContentsAt(tab_index);
[email protected]ce3fa3c2009-04-20 19:55:571769 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
initial.commit09911bf2008-07-26 23:55:291770 }
1771 }
initial.commit09911bf2008-07-26 23:55:291772}
1773
[email protected]71f65dd2009-02-11 19:14:561774void AutomationProvider::GetTabTitle(int handle, int* title_string_size,
1775 std::wstring* title) {
1776 *title_string_size = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291777 if (tab_tracker_->ContainsHandle(handle)) {
1778 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c100dbd2009-04-29 23:44:361779 NavigationEntry* entry = tab->GetActiveEntry();
1780 if (entry != NULL) {
1781 *title = UTF16ToWideHack(entry->title());
1782 } else {
1783 *title = std::wstring();
1784 }
[email protected]71f65dd2009-02-11 19:14:561785 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291786 }
initial.commit09911bf2008-07-26 23:55:291787}
1788
[email protected]77bc6732009-04-20 22:01:031789void AutomationProvider::GetTabIndex(int handle, int* tabstrip_index) {
1790 *tabstrip_index = -1; // -1 is the error code
1791
1792 if (tab_tracker_->ContainsHandle(handle)) {
1793 NavigationController* tab = tab_tracker_->GetResource(handle);
1794 Browser* browser = Browser::GetBrowserForController(tab, NULL);
[email protected]902cdf772009-05-06 15:08:121795 *tabstrip_index = browser->tabstrip_model()->GetIndexOfController(tab);
[email protected]77bc6732009-04-20 22:01:031796 }
1797}
1798
initial.commit09911bf2008-07-26 23:55:291799void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) {
1800 if (window_tracker_->ContainsHandle(handle)) {
1801 window_tracker_->Remove(window_tracker_->GetResource(handle));
1802 }
1803}
1804
1805void AutomationProvider::OnChannelError() {
1806 LOG(ERROR) << "AutomationProxy went away, shutting down app.";
[email protected]295039bd2008-08-15 04:32:571807 AutomationProviderList::GetInstance()->RemoveProvider(this);
initial.commit09911bf2008-07-26 23:55:291808}
1809
1810// TODO(brettw) change this to accept GURLs when history supports it
1811void AutomationProvider::OnRedirectQueryComplete(
1812 HistoryService::Handle request_handle,
1813 GURL from_url,
1814 bool success,
1815 HistoryService::RedirectList* redirects) {
1816 DCHECK(request_handle == redirect_query_);
[email protected]71f65dd2009-02-11 19:14:561817 DCHECK(reply_message_ != NULL);
initial.commit09911bf2008-07-26 23:55:291818
[email protected]deb57402009-02-06 01:35:301819 std::vector<GURL> redirects_gurl;
initial.commit09911bf2008-07-26 23:55:291820 if (success) {
[email protected]71f65dd2009-02-11 19:14:561821 reply_message_->WriteBool(true);
initial.commit09911bf2008-07-26 23:55:291822 for (size_t i = 0; i < redirects->size(); i++)
[email protected]deb57402009-02-06 01:35:301823 redirects_gurl.push_back(redirects->at(i));
initial.commit09911bf2008-07-26 23:55:291824 } else {
[email protected]71f65dd2009-02-11 19:14:561825 reply_message_->WriteInt(-1); // Negative count indicates failure.
initial.commit09911bf2008-07-26 23:55:291826 }
1827
[email protected]4f3dc372009-02-24 00:10:291828 IPC::ParamTraits<std::vector<GURL> >::Write(reply_message_, redirects_gurl);
[email protected]deb57402009-02-06 01:35:301829
[email protected]71f65dd2009-02-11 19:14:561830 Send(reply_message_);
initial.commit09911bf2008-07-26 23:55:291831 redirect_query_ = NULL;
[email protected]71f65dd2009-02-11 19:14:561832 reply_message_ = NULL;
initial.commit09911bf2008-07-26 23:55:291833}
1834
1835bool AutomationProvider::Send(IPC::Message* msg) {
[email protected]295039bd2008-08-15 04:32:571836 DCHECK(channel_.get());
1837 return channel_->Send(msg);
initial.commit09911bf2008-07-26 23:55:291838}
1839
1840Browser* AutomationProvider::FindAndActivateTab(
1841 NavigationController* controller) {
1842 int tab_index;
1843 Browser* browser = Browser::GetBrowserForController(controller, &tab_index);
1844 if (browser)
1845 browser->SelectTabContentsAt(tab_index, true);
1846
1847 return browser;
1848}
1849
[email protected]71f65dd2009-02-11 19:14:561850void AutomationProvider::GetCookies(const GURL& url, int handle,
1851 int* value_size,
1852 std::string* value) {
1853 *value_size = -1;
initial.commit09911bf2008-07-26 23:55:291854 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1855 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561856 *value =
initial.commit09911bf2008-07-26 23:55:291857 tab->profile()->GetRequestContext()->cookie_store()->GetCookies(url);
[email protected]71f65dd2009-02-11 19:14:561858 *value_size = static_cast<int>(value->size());
initial.commit09911bf2008-07-26 23:55:291859 }
initial.commit09911bf2008-07-26 23:55:291860}
1861
[email protected]71f65dd2009-02-11 19:14:561862void AutomationProvider::SetCookie(const GURL& url,
initial.commit09911bf2008-07-26 23:55:291863 const std::string value,
[email protected]71f65dd2009-02-11 19:14:561864 int handle,
1865 int* response_value) {
1866 *response_value = -1;
initial.commit09911bf2008-07-26 23:55:291867
1868 if (url.is_valid() && tab_tracker_->ContainsHandle(handle)) {
1869 NavigationController* tab = tab_tracker_->GetResource(handle);
1870 URLRequestContext* context = tab->profile()->GetRequestContext();
1871 if (context->cookie_store()->SetCookie(url, value))
[email protected]71f65dd2009-02-11 19:14:561872 *response_value = 1;
initial.commit09911bf2008-07-26 23:55:291873 }
initial.commit09911bf2008-07-26 23:55:291874}
1875
[email protected]71f65dd2009-02-11 19:14:561876void AutomationProvider::GetTabURL(int handle, bool* success, GURL* url) {
1877 *success = false;
initial.commit09911bf2008-07-26 23:55:291878 if (tab_tracker_->ContainsHandle(handle)) {
1879 NavigationController* tab = tab_tracker_->GetResource(handle);
1880 // Return what the user would see in the location bar.
[email protected]71f65dd2009-02-11 19:14:561881 *url = tab->GetActiveEntry()->display_url();
1882 *success = true;
initial.commit09911bf2008-07-26 23:55:291883 }
initial.commit09911bf2008-07-26 23:55:291884}
1885
[email protected]de246f52009-02-25 18:25:451886#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:561887void AutomationProvider::GetTabHWND(int handle, HWND* tab_hwnd) {
1888 *tab_hwnd = NULL;
initial.commit09911bf2008-07-26 23:55:291889
1890 if (tab_tracker_->ContainsHandle(handle)) {
1891 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111892 *tab_hwnd = tab->tab_contents()->GetNativeView();
initial.commit09911bf2008-07-26 23:55:291893 }
initial.commit09911bf2008-07-26 23:55:291894}
[email protected]de246f52009-02-25 18:25:451895#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291896
[email protected]71f65dd2009-02-11 19:14:561897void AutomationProvider::GetTabProcessID(int handle, int* process_id) {
1898 *process_id = -1;
initial.commit09911bf2008-07-26 23:55:291899
1900 if (tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:561901 *process_id = 0;
[email protected]57c6a652009-05-04 07:58:341902 TabContents* tab_contents =
1903 tab_tracker_->GetResource(handle)->tab_contents();
1904 if (tab_contents->process())
1905 *process_id = tab_contents->process()->process().pid();
initial.commit09911bf2008-07-26 23:55:291906 }
initial.commit09911bf2008-07-26 23:55:291907}
1908
1909void AutomationProvider::ApplyAccelerator(int handle, int id) {
[email protected]4f6381ee2009-04-16 02:46:331910 NOTREACHED() << "This function has been deprecated. "
1911 << "Please use ExecuteBrowserCommandAsync instead.";
initial.commit09911bf2008-07-26 23:55:291912}
1913
[email protected]71f65dd2009-02-11 19:14:561914void AutomationProvider::ExecuteJavascript(int handle,
initial.commit09911bf2008-07-26 23:55:291915 const std::wstring& frame_xpath,
[email protected]71f65dd2009-02-11 19:14:561916 const std::wstring& script,
1917 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:291918 bool succeeded = false;
[email protected]57c6a652009-05-04 07:58:341919 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1920 if (tab_contents) {
[email protected]20e93d12008-08-28 16:31:571921 // Set the routing id of this message with the controller.
1922 // This routing id needs to be remembered for the reverse
1923 // communication while sending back the response of
1924 // this javascript execution.
[email protected]f29acf52008-11-03 20:08:331925 std::wstring set_automation_id;
1926 SStringPrintf(&set_automation_id,
1927 L"window.domAutomationController.setAutomationId(%d);",
[email protected]71f65dd2009-02-11 19:14:561928 reply_message->routing_id());
1929
1930 DCHECK(reply_message_ == NULL);
1931 reply_message_ = reply_message;
initial.commit09911bf2008-07-26 23:55:291932
[email protected]57c6a652009-05-04 07:58:341933 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]f29acf52008-11-03 20:08:331934 frame_xpath, set_automation_id);
[email protected]57c6a652009-05-04 07:58:341935 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
[email protected]1f5af4442008-09-25 22:11:061936 frame_xpath, script);
[email protected]20e93d12008-08-28 16:31:571937 succeeded = true;
initial.commit09911bf2008-07-26 23:55:291938 }
1939
1940 if (!succeeded) {
[email protected]71f65dd2009-02-11 19:14:561941 AutomationMsg_DomOperation::WriteReplyParams(reply_message, std::string());
1942 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:291943 }
1944}
1945
[email protected]71f65dd2009-02-11 19:14:561946void AutomationProvider::GetShelfVisibility(int handle, bool* visible) {
1947 *visible = false;
[email protected]20e93d12008-08-28 16:31:571948
[email protected]57c6a652009-05-04 07:58:341949 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
1950 if (tab_contents)
1951 *visible = tab_contents->IsDownloadShelfVisible();
initial.commit09911bf2008-07-26 23:55:291952}
1953
[email protected]71f65dd2009-02-11 19:14:561954void AutomationProvider::GetConstrainedWindowCount(int handle, int* count) {
1955 *count = -1; // -1 is the error code
initial.commit09911bf2008-07-26 23:55:291956 if (tab_tracker_->ContainsHandle(handle)) {
1957 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111958 TabContents* tab_contents = nav_controller->tab_contents();
initial.commit09911bf2008-07-26 23:55:291959 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:561960 *count = static_cast<int>(tab_contents->child_windows_.size());
initial.commit09911bf2008-07-26 23:55:291961 }
1962 }
initial.commit09911bf2008-07-26 23:55:291963}
1964
[email protected]71f65dd2009-02-11 19:14:561965void AutomationProvider::GetConstrainedWindow(int handle, int index,
1966 int* cwindow_handle) {
1967 *cwindow_handle = 0;
initial.commit09911bf2008-07-26 23:55:291968 if (tab_tracker_->ContainsHandle(handle) && index >= 0) {
1969 NavigationController* nav_controller =
1970 tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:111971 TabContents* tab = nav_controller->tab_contents();
[email protected]d5f942ba2008-09-26 19:30:341972 if (tab && index < static_cast<int>(tab->child_windows_.size())) {
[email protected]de246f52009-02-25 18:25:451973#if defined(OS_WIN)
[email protected]d5f942ba2008-09-26 19:30:341974 ConstrainedWindow* window = tab->child_windows_[index];
[email protected]71f65dd2009-02-11 19:14:561975 *cwindow_handle = cwindow_tracker_->Add(window);
[email protected]de246f52009-02-25 18:25:451976#else
1977 // TODO(port): Enable when cwindow_tracker is ported.
1978 NOTIMPLEMENTED();
1979#endif
initial.commit09911bf2008-07-26 23:55:291980 }
1981 }
initial.commit09911bf2008-07-26 23:55:291982}
1983
[email protected]71f65dd2009-02-11 19:14:561984void AutomationProvider::GetConstrainedTitle(int handle,
1985 int* title_string_size,
1986 std::wstring* title) {
1987 *title_string_size = -1; // -1 is the error code
[email protected]de246f52009-02-25 18:25:451988#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291989 if (cwindow_tracker_->ContainsHandle(handle)) {
1990 ConstrainedWindow* window = cwindow_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:561991 *title = window->GetWindowTitle();
1992 *title_string_size = static_cast<int>(title->size());
initial.commit09911bf2008-07-26 23:55:291993 }
[email protected]de246f52009-02-25 18:25:451994#else
1995 // TODO(port): Enable when cwindow_tracker is ported.
1996 NOTIMPLEMENTED();
1997#endif
initial.commit09911bf2008-07-26 23:55:291998}
1999
[email protected]71f65dd2009-02-11 19:14:562000void AutomationProvider::GetConstrainedWindowBounds(int handle, bool* exists,
2001 gfx::Rect* rect) {
2002 *rect = gfx::Rect(0, 0, 0, 0);
[email protected]de246f52009-02-25 18:25:452003#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292004 if (cwindow_tracker_->ContainsHandle(handle)) {
2005 ConstrainedWindow* window = cwindow_tracker_->GetResource(handle);
2006 if (window) {
[email protected]71f65dd2009-02-11 19:14:562007 *exists = true;
2008 *rect = window->GetCurrentBounds();
initial.commit09911bf2008-07-26 23:55:292009 }
2010 }
[email protected]de246f52009-02-25 18:25:452011#else
2012 // TODO(port): Enable when cwindow_tracker is ported.
2013 NOTIMPLEMENTED();
2014#endif
initial.commit09911bf2008-07-26 23:55:292015}
2016
2017void AutomationProvider::HandleFindInPageRequest(
[email protected]71f65dd2009-02-11 19:14:562018 int handle, const std::wstring& find_request,
2019 int forward, int match_case, int* active_ordinal, int* matches_found) {
[email protected]5a52f162008-08-27 04:15:312020 NOTREACHED() << "This function has been deprecated."
2021 << "Please use HandleFindRequest instead.";
[email protected]71f65dd2009-02-11 19:14:562022 *matches_found = -1;
[email protected]5a52f162008-08-27 04:15:312023 return;
2024}
2025
[email protected]4f999132009-03-31 18:08:402026void AutomationProvider::HandleFindRequest(
2027 int handle,
2028 const AutomationMsg_Find_Params& params,
2029 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292030 if (!tab_tracker_->ContainsHandle(handle)) {
[email protected]71f65dd2009-02-11 19:14:562031 AutomationMsg_FindInPage::WriteReplyParams(reply_message, -1, -1);
2032 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292033 return;
2034 }
2035
2036 NavigationController* nav = tab_tracker_->GetResource(handle);
[email protected]7f0005a2009-04-15 03:25:112037 TabContents* tab_contents = nav->tab_contents();
initial.commit09911bf2008-07-26 23:55:292038
2039 find_in_page_observer_.reset(new
[email protected]1c58a5c2009-05-21 18:47:142040 FindInPageNotificationObserver(this, tab_contents, reply_message));
initial.commit09911bf2008-07-26 23:55:292041
[email protected]57c6a652009-05-04 07:58:342042 tab_contents->set_current_find_request_id(
2043 FindInPageNotificationObserver::kFindInPageRequestId);
2044 tab_contents->render_view_host()->StartFinding(
2045 FindInPageNotificationObserver::kFindInPageRequestId,
2046 params.search_string, params.forward, params.match_case,
2047 params.find_next);
initial.commit09911bf2008-07-26 23:55:292048}
2049
[email protected]5f8af2a2008-08-06 22:49:452050void AutomationProvider::HandleOpenFindInPageRequest(
2051 const IPC::Message& message, int handle) {
[email protected]4f3dc372009-02-24 00:10:292052 if (browser_tracker_->ContainsHandle(handle)) {
2053 Browser* browser = browser_tracker_->GetResource(handle);
2054 browser->FindInPage(false, false);
[email protected]5f8af2a2008-08-06 22:49:452055 }
2056}
2057
[email protected]71f65dd2009-02-11 19:14:562058void AutomationProvider::GetFindWindowVisibility(int handle, bool* visible) {
[email protected]9e0534b2008-10-21 15:03:012059 gfx::Point position;
[email protected]71f65dd2009-02-11 19:14:562060 *visible = false;
[email protected]4f3dc372009-02-24 00:10:292061 if (browser_tracker_->ContainsHandle(handle)) {
2062 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:582063 FindBarTesting* find_bar =
2064 browser->find_bar()->find_bar()->GetFindBarTesting();
2065 find_bar->GetFindBarWindowInfo(&position, visible);
[email protected]4f3dc372009-02-24 00:10:292066 }
[email protected]20e93d12008-08-28 16:31:572067}
2068
[email protected]71f65dd2009-02-11 19:14:562069void AutomationProvider::HandleFindWindowLocationRequest(int handle, int* x,
2070 int* y) {
[email protected]9e0534b2008-10-21 15:03:012071 gfx::Point position(0, 0);
2072 bool visible = false;
[email protected]4f3dc372009-02-24 00:10:292073 if (browser_tracker_->ContainsHandle(handle)) {
2074 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]4801ecc2009-04-05 04:52:582075 FindBarTesting* find_bar =
2076 browser->find_bar()->find_bar()->GetFindBarTesting();
2077 find_bar->GetFindBarWindowInfo(&position, &visible);
[email protected]4f3dc372009-02-24 00:10:292078 }
[email protected]20e93d12008-08-28 16:31:572079
[email protected]71f65dd2009-02-11 19:14:562080 *x = position.x();
2081 *y = position.y();
[email protected]20e93d12008-08-28 16:31:572082}
2083
[email protected]71f65dd2009-02-11 19:14:562084void AutomationProvider::GetBookmarkBarVisibility(int handle, bool* visible,
2085 bool* animating) {
2086 *visible = false;
2087 *animating = false;
[email protected]c2cbeb92008-09-05 21:36:572088
[email protected]de246f52009-02-25 18:25:452089#if defined(OS_WIN)
[email protected]c2cbeb92008-09-05 21:36:572090 if (browser_tracker_->ContainsHandle(handle)) {
2091 Browser* browser = browser_tracker_->GetResource(handle);
2092 if (browser) {
[email protected]0ba1f5302009-01-22 01:34:522093 BrowserWindowTesting* testing =
2094 browser->window()->GetBrowserWindowTesting();
2095 BookmarkBarView* bookmark_bar = testing->GetBookmarkBarView();
[email protected]c2cbeb92008-09-05 21:36:572096 if (bookmark_bar) {
[email protected]71f65dd2009-02-11 19:14:562097 *animating = bookmark_bar->IsAnimating();
2098 *visible = browser->window()->IsBookmarkBarVisible();
[email protected]c2cbeb92008-09-05 21:36:572099 }
2100 }
2101 }
[email protected]de246f52009-02-25 18:25:452102#else
2103 // TODO(port): Enable when bookmarks ui is ported.
2104 NOTIMPLEMENTED();
2105#endif
[email protected]c2cbeb92008-09-05 21:36:572106}
2107
initial.commit09911bf2008-07-26 23:55:292108void AutomationProvider::HandleInspectElementRequest(
[email protected]71f65dd2009-02-11 19:14:562109 int handle, int x, int y, IPC::Message* reply_message) {
[email protected]57c6a652009-05-04 07:58:342110 TabContents* tab_contents = GetTabContentsForHandle(handle, NULL);
2111 if (tab_contents) {
[email protected]71f65dd2009-02-11 19:14:562112 DCHECK(reply_message_ == NULL);
2113 reply_message_ = reply_message;
2114
[email protected]57c6a652009-05-04 07:58:342115 tab_contents->render_view_host()->InspectElementAt(x, y);
initial.commit09911bf2008-07-26 23:55:292116 } else {
[email protected]71f65dd2009-02-11 19:14:562117 AutomationMsg_InspectElement::WriteReplyParams(reply_message, -1);
2118 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292119 }
2120}
2121
2122void AutomationProvider::ReceivedInspectElementResponse(int num_resources) {
[email protected]396c3b32009-03-12 22:26:092123 if (reply_message_) {
2124 AutomationMsg_InspectElement::WriteReplyParams(reply_message_,
2125 num_resources);
2126 Send(reply_message_);
2127 reply_message_ = NULL;
2128 }
initial.commit09911bf2008-07-26 23:55:292129}
2130
2131// Helper class for making changes to the URLRequest ProtocolFactory on the
2132// IO thread.
2133class SetFilteredInetTask : public Task {
2134 public:
2135 explicit SetFilteredInetTask(bool enabled) : enabled_(enabled) { }
2136 virtual void Run() {
2137 if (enabled_) {
2138 URLRequestFilter::GetInstance()->ClearHandlers();
2139
2140 URLRequestFailedDnsJob::AddUITestUrls();
2141 URLRequestSlowDownloadJob::AddUITestUrls();
2142
2143 std::wstring root_http;
2144 PathService::Get(chrome::DIR_TEST_DATA, &root_http);
2145 URLRequestMockHTTPJob::AddUITestUrls(root_http);
2146 } else {
2147 // Revert to the default handlers.
2148 URLRequestFilter::GetInstance()->ClearHandlers();
2149 }
2150 }
2151 private:
2152 bool enabled_;
2153};
2154
2155void AutomationProvider::SetFilteredInet(const IPC::Message& message,
2156 bool enabled) {
2157 // Since this involves changing the URLRequest ProtocolFactory, we want to
2158 // run on the main thread.
2159 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
2160 new SetFilteredInetTask(enabled));
2161}
2162
[email protected]4f3dc372009-02-24 00:10:292163void AutomationProvider::GetDownloadDirectory(
2164 int handle, std::wstring* download_directory) {
initial.commit09911bf2008-07-26 23:55:292165 DLOG(INFO) << "Handling download directory request";
initial.commit09911bf2008-07-26 23:55:292166 if (tab_tracker_->ContainsHandle(handle)) {
2167 NavigationController* tab = tab_tracker_->GetResource(handle);
2168 DownloadManager* dlm = tab->profile()->GetDownloadManager();
2169 DCHECK(dlm);
[email protected]71f65dd2009-02-11 19:14:562170 *download_directory = dlm->download_path().ToWStringHack();
initial.commit09911bf2008-07-26 23:55:292171 }
initial.commit09911bf2008-07-26 23:55:292172}
2173
[email protected]14c0a032009-04-13 18:15:142174void AutomationProvider::OpenNewBrowserWindow(bool show,
2175 IPC::Message* reply_message) {
2176 new BrowserOpenedNotificationObserver(this, reply_message);
initial.commit09911bf2008-07-26 23:55:292177 // We may have no current browser windows open so don't rely on
2178 // asking an existing browser to execute the IDC_NEWWINDOW command
[email protected]15952e462008-11-14 00:29:052179 Browser* browser = Browser::Create(profile_);
2180 browser->AddBlankTab(true);
[email protected]3683cbb2009-04-09 21:46:152181 if (show)
[email protected]15952e462008-11-14 00:29:052182 browser->window()->Show();
initial.commit09911bf2008-07-26 23:55:292183}
2184
[email protected]71f65dd2009-02-11 19:14:562185void AutomationProvider::GetWindowForBrowser(int browser_handle,
2186 bool* success,
2187 int* handle) {
2188 *success = false;
2189 *handle = 0;
initial.commit09911bf2008-07-26 23:55:292190
2191 if (browser_tracker_->ContainsHandle(browser_handle)) {
2192 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]0e9f4ee2009-04-08 01:44:202193 gfx::NativeWindow win = browser->window()->GetNativeHandle();
initial.commit09911bf2008-07-26 23:55:292194 // Add() returns the existing handle for the resource if any.
[email protected]0e9f4ee2009-04-08 01:44:202195 *handle = window_tracker_->Add(win);
[email protected]71f65dd2009-02-11 19:14:562196 *success = true;
initial.commit09911bf2008-07-26 23:55:292197 }
initial.commit09911bf2008-07-26 23:55:292198}
2199
[email protected]13869dd2009-05-05 00:40:062200#if defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:292201void AutomationProvider::GetAutocompleteEditForBrowser(
[email protected]71f65dd2009-02-11 19:14:562202 int browser_handle,
2203 bool* success,
2204 int* autocomplete_edit_handle) {
2205 *success = false;
2206 *autocomplete_edit_handle = 0;
initial.commit09911bf2008-07-26 23:55:292207
2208 if (browser_tracker_->ContainsHandle(browser_handle)) {
2209 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]13869dd2009-05-05 00:40:062210 LocationBar* loc_bar = browser->window()->GetLocationBar();
2211 AutocompleteEditView* edit_view = loc_bar->location_entry();
initial.commit09911bf2008-07-26 23:55:292212 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562213 *autocomplete_edit_handle = autocomplete_edit_tracker_->Add(edit_view);
2214 *success = true;
initial.commit09911bf2008-07-26 23:55:292215 }
initial.commit09911bf2008-07-26 23:55:292216}
[email protected]13869dd2009-05-05 00:40:062217#endif // defined(OS_WIN) || defined(OS_LINUX)
initial.commit09911bf2008-07-26 23:55:292218
[email protected]13869dd2009-05-05 00:40:062219#if defined(OS_WIN)
2220// TODO(port): Remove windowsisms.
[email protected]71f65dd2009-02-11 19:14:562221void AutomationProvider::GetBrowserForWindow(int window_handle,
2222 bool* success,
2223 int* browser_handle) {
2224 *success = false;
2225 *browser_handle = 0;
initial.commit09911bf2008-07-26 23:55:292226
2227 if (window_tracker_->ContainsHandle(window_handle)) {
2228 HWND window = window_tracker_->GetResource(window_handle);
2229 BrowserList::const_iterator iter = BrowserList::begin();
2230 Browser* browser = NULL;
2231 for (;iter != BrowserList::end(); ++iter) {
[email protected]2d46c842008-11-14 19:24:312232 HWND hwnd = reinterpret_cast<HWND>((*iter)->window()->GetNativeHandle());
2233 if (window == hwnd) {
initial.commit09911bf2008-07-26 23:55:292234 browser = *iter;
2235 break;
2236 }
2237 }
2238 if (browser) {
2239 // Add() returns the existing handle for the resource if any.
[email protected]71f65dd2009-02-11 19:14:562240 *browser_handle = browser_tracker_->Add(browser);
2241 *success = true;
initial.commit09911bf2008-07-26 23:55:292242 }
2243 }
initial.commit09911bf2008-07-26 23:55:292244}
[email protected]de246f52009-02-25 18:25:452245#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292246
[email protected]71f65dd2009-02-11 19:14:562247void AutomationProvider::ShowInterstitialPage(int tab_handle,
2248 const std::string& html_text,
2249 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292250 if (tab_tracker_->ContainsHandle(tab_handle)) {
2251 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
[email protected]7f0005a2009-04-15 03:25:112252 TabContents* tab_contents = controller->tab_contents();
[email protected]965524b2009-04-04 21:32:402253
2254 AddNavigationStatusListener<bool>(controller, reply_message, true,
2255 false, false);
[email protected]965524b2009-04-04 21:32:402256 AutomationInterstitialPage* interstitial =
[email protected]57c6a652009-05-04 07:58:342257 new AutomationInterstitialPage(tab_contents,
[email protected]965524b2009-04-04 21:32:402258 GURL("about:interstitial"),
2259 html_text);
2260 interstitial->Show();
2261 return;
initial.commit09911bf2008-07-26 23:55:292262 }
[email protected]71f65dd2009-02-11 19:14:562263
2264 AutomationMsg_ShowInterstitialPage::WriteReplyParams(reply_message, false);
2265 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292266}
2267
[email protected]71f65dd2009-02-11 19:14:562268void AutomationProvider::HideInterstitialPage(int tab_handle,
2269 bool* success) {
2270 *success = false;
[email protected]57c6a652009-05-04 07:58:342271 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, NULL);
2272 if (tab_contents && tab_contents->interstitial_page()) {
2273 tab_contents->interstitial_page()->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562274 *success = true;
initial.commit09911bf2008-07-26 23:55:292275 }
initial.commit09911bf2008-07-26 23:55:292276}
2277
[email protected]71f65dd2009-02-11 19:14:562278void AutomationProvider::CloseTab(int tab_handle,
2279 bool wait_until_closed,
2280 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292281 if (tab_tracker_->ContainsHandle(tab_handle)) {
2282 NavigationController* controller = tab_tracker_->GetResource(tab_handle);
2283 int index;
2284 Browser* browser = Browser::GetBrowserForController(controller, &index);
2285 DCHECK(browser);
[email protected]1c58a5c2009-05-21 18:47:142286 new TabClosedNotificationObserver(this, wait_until_closed, reply_message);
[email protected]7f0005a2009-04-15 03:25:112287 browser->CloseContents(controller->tab_contents());
[email protected]de246f52009-02-25 18:25:452288 return;
initial.commit09911bf2008-07-26 23:55:292289 }
[email protected]de246f52009-02-25 18:25:452290
2291 AutomationMsg_CloseTab::WriteReplyParams(reply_message, false);
initial.commit09911bf2008-07-26 23:55:292292}
2293
[email protected]71f65dd2009-02-11 19:14:562294void AutomationProvider::CloseBrowser(int browser_handle,
2295 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292296 if (browser_tracker_->ContainsHandle(browser_handle)) {
2297 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562298 new BrowserClosedNotificationObserver(browser, this,
[email protected]71f65dd2009-02-11 19:14:562299 reply_message);
[email protected]f3e99e32008-07-30 04:48:392300 browser->window()->Close();
initial.commit09911bf2008-07-26 23:55:292301 } else {
2302 NOTREACHED();
2303 }
2304}
2305
[email protected]71f65dd2009-02-11 19:14:562306void AutomationProvider::CloseBrowserAsync(int browser_handle) {
2307 if (browser_tracker_->ContainsHandle(browser_handle)) {
2308 Browser* browser = browser_tracker_->GetResource(browser_handle);
2309 browser->window()->Close();
2310 } else {
2311 NOTREACHED();
2312 }
2313}
2314
[email protected]de246f52009-02-25 18:25:452315#if defined(OS_WIN)
2316// TODO(port): Remove windowsisms.
[email protected]71f65dd2009-02-11 19:14:562317void AutomationProvider::CreateExternalTab(HWND parent,
[email protected]31fb110522009-01-28 21:50:392318 const gfx::Rect& dimensions,
[email protected]71f65dd2009-02-11 19:14:562319 unsigned int style,
[email protected]dd07d60b2009-04-23 18:24:322320 bool incognito,
[email protected]71f65dd2009-02-11 19:14:562321 HWND* tab_container_window,
2322 int* tab_handle) {
2323 *tab_handle = 0;
2324 *tab_container_window = NULL;
initial.commit09911bf2008-07-26 23:55:292325 ExternalTabContainer *external_tab_container =
2326 new ExternalTabContainer(this);
[email protected]dd07d60b2009-04-23 18:24:322327 Profile* profile = incognito? profile_->GetOffTheRecordProfile() : profile_;
2328 external_tab_container->Init(profile, parent, dimensions, style);
initial.commit09911bf2008-07-26 23:55:292329 TabContents* tab_contents = external_tab_container->tab_contents();
2330 if (tab_contents) {
[email protected]ce3fa3c2009-04-20 19:55:572331 *tab_handle = tab_tracker_->Add(&tab_contents->controller());
[email protected]eac83f02009-05-08 18:44:442332 external_tab_container->set_tab_handle(*tab_handle);
[email protected]71f65dd2009-02-11 19:14:562333 *tab_container_window = *external_tab_container;
[email protected]31fb110522009-01-28 21:50:392334 } else {
2335 delete external_tab_container;
initial.commit09911bf2008-07-26 23:55:292336 }
initial.commit09911bf2008-07-26 23:55:292337}
[email protected]d2cc6ed2009-04-24 00:26:172338#endif
initial.commit09911bf2008-07-26 23:55:292339
[email protected]71f65dd2009-02-11 19:14:562340void AutomationProvider::NavigateInExternalTab(
2341 int handle, const GURL& url,
2342 AutomationMsg_NavigationResponseValues* status) {
2343 *status = AUTOMATION_MSG_NAVIGATION_ERROR;
initial.commit09911bf2008-07-26 23:55:292344
2345 if (tab_tracker_->ContainsHandle(handle)) {
2346 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]c0588052008-10-27 23:01:502347 tab->LoadURL(url, GURL(), PageTransition::TYPED);
[email protected]71f65dd2009-02-11 19:14:562348 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS;
initial.commit09911bf2008-07-26 23:55:292349 }
initial.commit09911bf2008-07-26 23:55:292350}
2351
[email protected]d2cc6ed2009-04-24 00:26:172352#if defined(OS_WIN)
2353// TODO(port): remove windowisms.
[email protected]71f65dd2009-02-11 19:14:562354void AutomationProvider::SetAcceleratorsForTab(int handle,
initial.commit09911bf2008-07-26 23:55:292355 HACCEL accel_table,
[email protected]71f65dd2009-02-11 19:14:562356 int accel_entry_count,
2357 bool* status) {
2358 *status = false;
2359
[email protected]b9d227492009-02-10 15:20:272360 ExternalTabContainer* external_tab = GetExternalTabForHandle(handle);
2361 if (external_tab) {
2362 external_tab->SetAccelerators(accel_table, accel_entry_count);
[email protected]71f65dd2009-02-11 19:14:562363 *status = true;
initial.commit09911bf2008-07-26 23:55:292364 }
initial.commit09911bf2008-07-26 23:55:292365}
2366
2367void AutomationProvider::ProcessUnhandledAccelerator(
2368 const IPC::Message& message, int handle, const MSG& msg) {
[email protected]b9d227492009-02-10 15:20:272369 ExternalTabContainer* external_tab = GetExternalTabForHandle(handle);
2370 if (external_tab) {
2371 external_tab->ProcessUnhandledAccelerator(msg);
2372 }
2373 // This message expects no response.
2374}
[email protected]d2cc6ed2009-04-24 00:26:172375#endif
[email protected]b9d227492009-02-10 15:20:272376
[email protected]71f65dd2009-02-11 19:14:562377void AutomationProvider::WaitForTabToBeRestored(int tab_handle,
2378 IPC::Message* reply_message) {
2379 if (tab_tracker_->ContainsHandle(tab_handle)) {
2380 NavigationController* tab = tab_tracker_->GetResource(tab_handle);
2381 restore_tracker_.reset(
[email protected]1c58a5c2009-05-21 18:47:142382 new NavigationControllerRestoredObserver(this, tab, reply_message));
[email protected]71f65dd2009-02-11 19:14:562383 }
2384}
2385
[email protected]b9d227492009-02-10 15:20:272386void AutomationProvider::SetInitialFocus(const IPC::Message& message,
2387 int handle, bool reverse) {
[email protected]d2cc6ed2009-04-24 00:26:172388#if defined(OS_WIN)
[email protected]b9d227492009-02-10 15:20:272389 ExternalTabContainer* external_tab = GetExternalTabForHandle(handle);
2390 if (external_tab) {
2391 external_tab->SetInitialFocus(reverse);
initial.commit09911bf2008-07-26 23:55:292392 }
2393 // This message expects no response.
[email protected]d2cc6ed2009-04-24 00:26:172394#elif defined(OS_POSIX)
2395 // TODO(port) enable this function.
2396 NOTIMPLEMENTED();
2397#endif
initial.commit09911bf2008-07-26 23:55:292398}
2399
[email protected]d2cc6ed2009-04-24 00:26:172400// TODO(port): enable these functions.
2401#if defined(OS_WIN)
[email protected]71f65dd2009-02-11 19:14:562402void AutomationProvider::GetSecurityState(int handle, bool* success,
2403 SecurityStyle* security_style,
2404 int* ssl_cert_status,
2405 int* mixed_content_status) {
initial.commit09911bf2008-07-26 23:55:292406 if (tab_tracker_->ContainsHandle(handle)) {
2407 NavigationController* tab = tab_tracker_->GetResource(handle);
2408 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562409 *success = true;
2410 *security_style = entry->ssl().security_style();
2411 *ssl_cert_status = entry->ssl().cert_status();
2412 *mixed_content_status = entry->ssl().content_status();
initial.commit09911bf2008-07-26 23:55:292413 } else {
[email protected]71f65dd2009-02-11 19:14:562414 *success = false;
2415 *security_style = SECURITY_STYLE_UNKNOWN;
2416 *ssl_cert_status = 0;
2417 *mixed_content_status = 0;
initial.commit09911bf2008-07-26 23:55:292418 }
2419}
2420
[email protected]71f65dd2009-02-11 19:14:562421void AutomationProvider::GetPageType(int handle, bool* success,
2422 NavigationEntry::PageType* page_type) {
initial.commit09911bf2008-07-26 23:55:292423 if (tab_tracker_->ContainsHandle(handle)) {
2424 NavigationController* tab = tab_tracker_->GetResource(handle);
2425 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]71f65dd2009-02-11 19:14:562426 *page_type = entry->page_type();
2427 *success = true;
initial.commit09911bf2008-07-26 23:55:292428 // In order to return the proper result when an interstitial is shown and
[email protected]57c6a652009-05-04 07:58:342429 // no navigation entry were created for it we need to ask the TabContents.
[email protected]71f65dd2009-02-11 19:14:562430 if (*page_type == NavigationEntry::NORMAL_PAGE &&
[email protected]57c6a652009-05-04 07:58:342431 tab->tab_contents()->showing_interstitial_page())
[email protected]71f65dd2009-02-11 19:14:562432 *page_type = NavigationEntry::INTERSTITIAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292433 } else {
[email protected]71f65dd2009-02-11 19:14:562434 *success = false;
2435 *page_type = NavigationEntry::NORMAL_PAGE;
initial.commit09911bf2008-07-26 23:55:292436 }
2437}
2438
[email protected]71f65dd2009-02-11 19:14:562439void AutomationProvider::ActionOnSSLBlockingPage(int handle, bool proceed,
2440 IPC::Message* reply_message) {
initial.commit09911bf2008-07-26 23:55:292441 if (tab_tracker_->ContainsHandle(handle)) {
2442 NavigationController* tab = tab_tracker_->GetResource(handle);
2443 NavigationEntry* entry = tab->GetActiveEntry();
[email protected]1e5645ff2008-08-27 18:09:072444 if (entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE) {
[email protected]965524b2009-04-04 21:32:402445 TabContents* tab_contents = tab->tab_contents();
[email protected]cbab76d2008-10-13 22:42:472446 InterstitialPage* ssl_blocking_page =
[email protected]57c6a652009-05-04 07:58:342447 InterstitialPage::GetInterstitialPage(tab_contents);
initial.commit09911bf2008-07-26 23:55:292448 if (ssl_blocking_page) {
2449 if (proceed) {
[email protected]71f65dd2009-02-11 19:14:562450 AddNavigationStatusListener<bool>(tab, reply_message, true, true,
2451 false);
2452 ssl_blocking_page->Proceed();
initial.commit09911bf2008-07-26 23:55:292453 return;
2454 }
2455 ssl_blocking_page->DontProceed();
[email protected]71f65dd2009-02-11 19:14:562456 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(reply_message,
2457 true);
2458 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292459 return;
2460 }
2461 }
2462 }
2463 // We failed.
[email protected]71f65dd2009-02-11 19:14:562464 AutomationMsg_ActionOnSSLBlockingPage::WriteReplyParams(reply_message,
2465 false);
2466 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292467}
[email protected]de246f52009-02-25 18:25:452468#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:292469
[email protected]71f65dd2009-02-11 19:14:562470void AutomationProvider::BringBrowserToFront(int browser_handle,
2471 bool* success) {
initial.commit09911bf2008-07-26 23:55:292472 if (browser_tracker_->ContainsHandle(browser_handle)) {
2473 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]cd7ffc22008-11-12 00:26:062474 browser->window()->Activate();
[email protected]71f65dd2009-02-11 19:14:562475 *success = true;
initial.commit09911bf2008-07-26 23:55:292476 } else {
[email protected]71f65dd2009-02-11 19:14:562477 *success = false;
initial.commit09911bf2008-07-26 23:55:292478 }
2479}
2480
[email protected]71f65dd2009-02-11 19:14:562481void AutomationProvider::IsPageMenuCommandEnabled(int browser_handle,
2482 int message_num,
2483 bool* menu_item_enabled) {
initial.commit09911bf2008-07-26 23:55:292484 if (browser_tracker_->ContainsHandle(browser_handle)) {
2485 Browser* browser = browser_tracker_->GetResource(browser_handle);
[email protected]71f65dd2009-02-11 19:14:562486 *menu_item_enabled =
[email protected]1fc025202009-01-20 23:03:142487 browser->command_updater()->IsCommandEnabled(message_num);
initial.commit09911bf2008-07-26 23:55:292488 } else {
[email protected]71f65dd2009-02-11 19:14:562489 *menu_item_enabled = false;
initial.commit09911bf2008-07-26 23:55:292490 }
2491}
2492
[email protected]de246f52009-02-25 18:25:452493#if defined(OS_WIN)
[email protected]3753f522009-04-14 23:15:472494// TODO(port): Enable this.
[email protected]71f65dd2009-02-11 19:14:562495void AutomationProvider::PrintNow(int tab_handle,
2496 IPC::Message* reply_message) {
[email protected]20e93d12008-08-28 16:31:572497 NavigationController* tab = NULL;
[email protected]57c6a652009-05-04 07:58:342498 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab);
2499 if (tab_contents) {
initial.commit09911bf2008-07-26 23:55:292500 FindAndActivateTab(tab);
[email protected]20e93d12008-08-28 16:31:572501 notification_observer_list_.AddObserver(
[email protected]1c58a5c2009-05-21 18:47:142502 new DocumentPrintedNotificationObserver(this, reply_message));
[email protected]57c6a652009-05-04 07:58:342503 if (tab_contents->PrintNow())
[email protected]20e93d12008-08-28 16:31:572504 return;
initial.commit09911bf2008-07-26 23:55:292505 }
[email protected]71f65dd2009-02-11 19:14:562506 AutomationMsg_PrintNow::WriteReplyParams(reply_message, false);
2507 Send(reply_message);
initial.commit09911bf2008-07-26 23:55:292508}
[email protected]3753f522009-04-14 23:15:472509#endif
initial.commit09911bf2008-07-26 23:55:292510
[email protected]71f65dd2009-02-11 19:14:562511void AutomationProvider::SavePage(int tab_handle,
initial.commit09911bf2008-07-26 23:55:292512 const std::wstring& file_name,
2513 const std::wstring& dir_path,
[email protected]71f65dd2009-02-11 19:14:562514 int type,
2515 bool* success) {
initial.commit09911bf2008-07-26 23:55:292516 if (!tab_tracker_->ContainsHandle(tab_handle)) {
[email protected]71f65dd2009-02-11 19:14:562517 *success = false;
initial.commit09911bf2008-07-26 23:55:292518 return;
2519 }
2520
2521 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2522 Browser* browser = FindAndActivateTab(nav);
2523 DCHECK(browser);
[email protected]1fc025202009-01-20 23:03:142524 if (!browser->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)) {
[email protected]71f65dd2009-02-11 19:14:562525 *success = false;
initial.commit09911bf2008-07-26 23:55:292526 return;
2527 }
2528
initial.commit09911bf2008-07-26 23:55:292529 SavePackage::SavePackageType save_type =
2530 static_cast<SavePackage::SavePackageType>(type);
2531 DCHECK(save_type >= SavePackage::SAVE_AS_ONLY_HTML &&
2532 save_type <= SavePackage::SAVE_AS_COMPLETE_HTML);
[email protected]57c6a652009-05-04 07:58:342533 nav->tab_contents()->SavePage(file_name, dir_path, save_type);
initial.commit09911bf2008-07-26 23:55:292534
[email protected]71f65dd2009-02-11 19:14:562535 *success = true;
initial.commit09911bf2008-07-26 23:55:292536}
2537
[email protected]13869dd2009-05-05 00:40:062538#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]3753f522009-04-14 23:15:472539// TODO(port): Enable these.
[email protected]71f65dd2009-02-11 19:14:562540void AutomationProvider::GetAutocompleteEditText(int autocomplete_edit_handle,
2541 bool* success,
2542 std::wstring* text) {
2543 *success = false;
initial.commit09911bf2008-07-26 23:55:292544 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562545 *text = autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
[email protected]81c21222008-09-10 19:35:522546 GetText();
[email protected]71f65dd2009-02-11 19:14:562547 *success = true;
initial.commit09911bf2008-07-26 23:55:292548 }
initial.commit09911bf2008-07-26 23:55:292549}
2550
[email protected]71f65dd2009-02-11 19:14:562551void AutomationProvider::SetAutocompleteEditText(int autocomplete_edit_handle,
2552 const std::wstring& text,
2553 bool* success) {
2554 *success = false;
initial.commit09911bf2008-07-26 23:55:292555 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]81c21222008-09-10 19:35:522556 autocomplete_edit_tracker_->GetResource(autocomplete_edit_handle)->
2557 SetUserText(text);
[email protected]71f65dd2009-02-11 19:14:562558 *success = true;
initial.commit09911bf2008-07-26 23:55:292559 }
initial.commit09911bf2008-07-26 23:55:292560}
2561
2562void AutomationProvider::AutocompleteEditGetMatches(
[email protected]71f65dd2009-02-11 19:14:562563 int autocomplete_edit_handle,
2564 bool* success,
2565 std::vector<AutocompleteMatchData>* matches) {
2566 *success = false;
initial.commit09911bf2008-07-26 23:55:292567 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]8deeb952008-10-09 18:21:272568 const AutocompleteResult& result = autocomplete_edit_tracker_->
2569 GetResource(autocomplete_edit_handle)->model()->result();
2570 for (AutocompleteResult::const_iterator i = result.begin();
2571 i != result.end(); ++i)
[email protected]71f65dd2009-02-11 19:14:562572 matches->push_back(AutocompleteMatchData(*i));
2573 *success = true;
initial.commit09911bf2008-07-26 23:55:292574 }
initial.commit09911bf2008-07-26 23:55:292575}
2576
2577void AutomationProvider::AutocompleteEditIsQueryInProgress(
[email protected]71f65dd2009-02-11 19:14:562578 int autocomplete_edit_handle,
2579 bool* success,
2580 bool* query_in_progress) {
2581 *success = false;
2582 *query_in_progress = false;
initial.commit09911bf2008-07-26 23:55:292583 if (autocomplete_edit_tracker_->ContainsHandle(autocomplete_edit_handle)) {
[email protected]71f65dd2009-02-11 19:14:562584 *query_in_progress = autocomplete_edit_tracker_->
[email protected]81c21222008-09-10 19:35:522585 GetResource(autocomplete_edit_handle)->model()->query_in_progress();
[email protected]71f65dd2009-02-11 19:14:562586 *success = true;
initial.commit09911bf2008-07-26 23:55:292587 }
initial.commit09911bf2008-07-26 23:55:292588}
2589
[email protected]28790922009-03-09 19:48:372590void AutomationProvider::OnMessageFromExternalHost(int handle,
2591 const std::string& message,
2592 const std::string& origin,
2593 const std::string& target) {
[email protected]fa83e762008-08-15 21:41:392594 if (tab_tracker_->ContainsHandle(handle)) {
2595 NavigationController* tab = tab_tracker_->GetResource(handle);
2596 if (!tab) {
2597 NOTREACHED();
2598 return;
2599 }
[email protected]f44265b2009-05-19 18:52:502600
[email protected]965524b2009-04-04 21:32:402601 TabContents* tab_contents = tab->tab_contents();
[email protected]fa83e762008-08-15 21:41:392602 if (!tab_contents) {
2603 NOTREACHED();
2604 return;
2605 }
2606
[email protected]57c6a652009-05-04 07:58:342607 RenderViewHost* view_host = tab_contents->render_view_host();
[email protected]fa83e762008-08-15 21:41:392608 if (!view_host) {
2609 return;
2610 }
2611
[email protected]f44265b2009-05-19 18:52:502612 if (AutomationExtensionFunction::InterceptMessageFromExternalHost(
[email protected]b83e4602009-05-15 22:58:332613 view_host, message, origin, target)) {
[email protected]f44265b2009-05-19 18:52:502614 // Message was diverted.
2615 return;
[email protected]b83e4602009-05-15 22:58:332616 }
[email protected]f44265b2009-05-19 18:52:502617
2618 if (ExtensionPortContainer::InterceptMessageFromExternalHost(message,
2619 origin, target, this, view_host, handle)) {
2620 // Message was diverted.
2621 return;
2622 }
2623
2624 view_host->ForwardMessageFromExternalHost(message, origin, target);
[email protected]fa83e762008-08-15 21:41:392625 }
2626}
[email protected]13869dd2009-05-05 00:40:062627#endif // defined(OS_WIN) || defined(OS_LINUX)
[email protected]fa83e762008-08-15 21:41:392628
[email protected]57c6a652009-05-04 07:58:342629TabContents* AutomationProvider::GetTabContentsForHandle(
[email protected]20e93d12008-08-28 16:31:572630 int handle, NavigationController** tab) {
[email protected]20e93d12008-08-28 16:31:572631 if (tab_tracker_->ContainsHandle(handle)) {
2632 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]57c6a652009-05-04 07:58:342633 if (tab)
2634 *tab = nav_controller;
2635 return nav_controller->tab_contents();
[email protected]20e93d12008-08-28 16:31:572636 }
[email protected]57c6a652009-05-04 07:58:342637 return NULL;
[email protected]20e93d12008-08-28 16:31:572638}
2639
[email protected]5cc063692009-04-07 23:21:312640#if defined(OS_WIN)
[email protected]b9d227492009-02-10 15:20:272641ExternalTabContainer* AutomationProvider::GetExternalTabForHandle(int handle) {
2642 if (tab_tracker_->ContainsHandle(handle)) {
2643 NavigationController* tab = tab_tracker_->GetResource(handle);
[email protected]965524b2009-04-04 21:32:402644 return ExternalTabContainer::GetContainerForTab(
2645 tab->tab_contents()->GetNativeView());
[email protected]b9d227492009-02-10 15:20:272646 }
2647
2648 return NULL;
2649}
[email protected]de246f52009-02-25 18:25:452650#endif // defined(OS_WIN)
[email protected]b9d227492009-02-10 15:20:272651
initial.commit09911bf2008-07-26 23:55:292652TestingAutomationProvider::TestingAutomationProvider(Profile* profile)
2653 : AutomationProvider(profile) {
2654 BrowserList::AddObserver(this);
[email protected]1c58a5c2009-05-21 18:47:142655 registrar_.Add(this, NotificationType::SESSION_END,
2656 NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:292657}
2658
2659TestingAutomationProvider::~TestingAutomationProvider() {
initial.commit09911bf2008-07-26 23:55:292660 BrowserList::RemoveObserver(this);
2661}
2662
2663void TestingAutomationProvider::OnChannelError() {
2664 BrowserList::CloseAllBrowsers(true);
2665 AutomationProvider::OnChannelError();
2666}
2667
2668void TestingAutomationProvider::OnBrowserRemoving(const Browser* browser) {
2669 // For backwards compatibility with the testing automation interface, we
2670 // want the automation provider (and hence the process) to go away when the
2671 // last browser goes away.
2672 if (BrowserList::size() == 1) {
[email protected]4f3dc372009-02-24 00:10:292673 // If you change this, update Observer for NotificationType::SESSION_END
2674 // below.
[email protected]295039bd2008-08-15 04:32:572675 MessageLoop::current()->PostTask(FROM_HERE,
2676 NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
initial.commit09911bf2008-07-26 23:55:292677 }
2678}
2679
2680void TestingAutomationProvider::Observe(NotificationType type,
2681 const NotificationSource& source,
2682 const NotificationDetails& details) {
[email protected]bfd04a62009-02-01 18:16:562683 DCHECK(type == NotificationType::SESSION_END);
initial.commit09911bf2008-07-26 23:55:292684 // OnBrowserRemoving does a ReleaseLater. When session end is received we exit
2685 // before the task runs resulting in this object not being deleted. This
2686 // Release balance out the Release scheduled by OnBrowserRemoving.
2687 Release();
2688}
[email protected]295039bd2008-08-15 04:32:572689
2690void TestingAutomationProvider::OnRemoveProvider() {
2691 AutomationProviderList::GetInstance()->RemoveProvider(this);
2692}
[email protected]8a3422c92008-09-24 17:42:422693
[email protected]71f65dd2009-02-11 19:14:562694void AutomationProvider::GetSSLInfoBarCount(int handle, int* count) {
2695 *count = -1; // -1 means error.
[email protected]8a3422c92008-09-24 17:42:422696 if (tab_tracker_->ContainsHandle(handle)) {
2697 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
[email protected]eb9ba192008-12-02 02:41:342698 if (nav_controller)
[email protected]7f0005a2009-04-15 03:25:112699 *count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422700 }
[email protected]8a3422c92008-09-24 17:42:422701}
2702
[email protected]71f65dd2009-02-11 19:14:562703void AutomationProvider::ClickSSLInfoBarLink(int handle,
[email protected]8a3422c92008-09-24 17:42:422704 int info_bar_index,
[email protected]71f65dd2009-02-11 19:14:562705 bool wait_for_navigation,
2706 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422707 bool success = false;
2708 if (tab_tracker_->ContainsHandle(handle)) {
2709 NavigationController* nav_controller = tab_tracker_->GetResource(handle);
2710 if (nav_controller) {
[email protected]7f0005a2009-04-15 03:25:112711 int count = nav_controller->tab_contents()->infobar_delegate_count();
[email protected]8a3422c92008-09-24 17:42:422712 if (info_bar_index >= 0 && info_bar_index < count) {
2713 if (wait_for_navigation) {
[email protected]71f65dd2009-02-11 19:14:562714 AddNavigationStatusListener<bool>(nav_controller, reply_message,
2715 true, true, false);
[email protected]8a3422c92008-09-24 17:42:422716 }
[email protected]eb9ba192008-12-02 02:41:342717 InfoBarDelegate* delegate =
[email protected]7f0005a2009-04-15 03:25:112718 nav_controller->tab_contents()->GetInfoBarDelegateAt(
[email protected]eb9ba192008-12-02 02:41:342719 info_bar_index);
2720 if (delegate->AsConfirmInfoBarDelegate())
2721 delegate->AsConfirmInfoBarDelegate()->Accept();
[email protected]8a3422c92008-09-24 17:42:422722 success = true;
2723 }
2724 }
[email protected]4f3dc372009-02-24 00:10:292725 }
[email protected]8a3422c92008-09-24 17:42:422726 if (!wait_for_navigation || !success)
[email protected]71f65dd2009-02-11 19:14:562727 AutomationMsg_ClickSSLInfoBarLink::WriteReplyParams(reply_message,
2728 success);
[email protected]8a3422c92008-09-24 17:42:422729}
2730
[email protected]71f65dd2009-02-11 19:14:562731void AutomationProvider::GetLastNavigationTime(int handle,
2732 int64* last_navigation_time) {
[email protected]8a3422c92008-09-24 17:42:422733 Time time = tab_tracker_->GetLastNavigationTime(handle);
[email protected]71f65dd2009-02-11 19:14:562734 *last_navigation_time = time.ToInternalValue();
[email protected]8a3422c92008-09-24 17:42:422735}
2736
[email protected]71f65dd2009-02-11 19:14:562737void AutomationProvider::WaitForNavigation(int handle,
2738 int64 last_navigation_time,
2739 IPC::Message* reply_message) {
[email protected]8a3422c92008-09-24 17:42:422740 NavigationController* controller = NULL;
2741 if (tab_tracker_->ContainsHandle(handle))
2742 controller = tab_tracker_->GetResource(handle);
2743
2744 Time time = tab_tracker_->GetLastNavigationTime(handle);
2745 if (time.ToInternalValue() > last_navigation_time || !controller) {
[email protected]71f65dd2009-02-11 19:14:562746 AutomationMsg_WaitForNavigation::WriteReplyParams(reply_message,
2747 controller != NULL);
[email protected]4f3dc372009-02-24 00:10:292748 return;
[email protected]8a3422c92008-09-24 17:42:422749 }
2750
[email protected]71f65dd2009-02-11 19:14:562751 AddNavigationStatusListener<bool>(controller, reply_message, true, true,
2752 false);
[email protected]8a3422c92008-09-24 17:42:422753}
2754
[email protected]71f65dd2009-02-11 19:14:562755void AutomationProvider::SetIntPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162756 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562757 int value,
2758 bool* success) {
2759 *success = false;
[email protected]8a3422c92008-09-24 17:42:422760 if (browser_tracker_->ContainsHandle(handle)) {
2761 Browser* browser = browser_tracker_->GetResource(handle);
2762 browser->profile()->GetPrefs()->SetInteger(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562763 *success = true;
[email protected]8a3422c92008-09-24 17:42:422764 }
[email protected]8a3422c92008-09-24 17:42:422765}
[email protected]97fa6ce32008-12-19 01:48:162766
[email protected]71f65dd2009-02-11 19:14:562767void AutomationProvider::SetStringPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162768 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562769 const std::wstring& value,
2770 bool* success) {
2771 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162772 if (browser_tracker_->ContainsHandle(handle)) {
2773 Browser* browser = browser_tracker_->GetResource(handle);
2774 browser->profile()->GetPrefs()->SetString(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562775 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162776 }
[email protected]97fa6ce32008-12-19 01:48:162777}
2778
[email protected]71f65dd2009-02-11 19:14:562779void AutomationProvider::GetBooleanPreference(int handle,
2780 const std::wstring& name,
2781 bool* success,
2782 bool* value) {
2783 *success = false;
2784 *value = false;
[email protected]97fa6ce32008-12-19 01:48:162785 if (browser_tracker_->ContainsHandle(handle)) {
2786 Browser* browser = browser_tracker_->GetResource(handle);
[email protected]71f65dd2009-02-11 19:14:562787 *value = browser->profile()->GetPrefs()->GetBoolean(name.c_str());
2788 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162789 }
[email protected]97fa6ce32008-12-19 01:48:162790}
2791
[email protected]71f65dd2009-02-11 19:14:562792void AutomationProvider::SetBooleanPreference(int handle,
[email protected]97fa6ce32008-12-19 01:48:162793 const std::wstring& name,
[email protected]71f65dd2009-02-11 19:14:562794 bool value,
2795 bool* success) {
2796 *success = false;
[email protected]97fa6ce32008-12-19 01:48:162797 if (browser_tracker_->ContainsHandle(handle)) {
2798 Browser* browser = browser_tracker_->GetResource(handle);
2799 browser->profile()->GetPrefs()->SetBoolean(name.c_str(), value);
[email protected]71f65dd2009-02-11 19:14:562800 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162801 }
[email protected]97fa6ce32008-12-19 01:48:162802}
2803
2804// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562805void AutomationProvider::GetPageCurrentEncoding(
2806 int tab_handle, std::wstring* current_encoding) {
[email protected]97fa6ce32008-12-19 01:48:162807 if (tab_tracker_->ContainsHandle(tab_handle)) {
2808 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2809 Browser* browser = FindAndActivateTab(nav);
2810 DCHECK(browser);
2811
[email protected]57c6a652009-05-04 07:58:342812 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU))
2813 *current_encoding = nav->tab_contents()->encoding();
[email protected]97fa6ce32008-12-19 01:48:162814 }
[email protected]97fa6ce32008-12-19 01:48:162815}
2816
2817// Gets the current used encoding name of the page in the specified tab.
[email protected]71f65dd2009-02-11 19:14:562818void AutomationProvider::OverrideEncoding(int tab_handle,
2819 const std::wstring& encoding_name,
2820 bool* success) {
2821 *success = false;
[email protected]de246f52009-02-25 18:25:452822#if defined(OS_WIN)
[email protected]97fa6ce32008-12-19 01:48:162823 if (tab_tracker_->ContainsHandle(tab_handle)) {
2824 NavigationController* nav = tab_tracker_->GetResource(tab_handle);
2825 Browser* browser = FindAndActivateTab(nav);
2826 DCHECK(browser);
2827
[email protected]1fc025202009-01-20 23:03:142828 if (browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) {
[email protected]7f0005a2009-04-15 03:25:112829 TabContents* tab_contents = nav->tab_contents();
[email protected]97fa6ce32008-12-19 01:48:162830 int selected_encoding_id =
2831 CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name);
2832 if (selected_encoding_id) {
2833 browser->OverrideEncoding(selected_encoding_id);
[email protected]71f65dd2009-02-11 19:14:562834 *success = true;
[email protected]97fa6ce32008-12-19 01:48:162835 }
2836 }
2837 }
[email protected]de246f52009-02-25 18:25:452838#else
2839 // TODO(port): Enable when encoding-related parts of Browser are ported.
2840 NOTIMPLEMENTED();
2841#endif
[email protected]97fa6ce32008-12-19 01:48:162842}
[email protected]5bcdb312009-01-07 21:43:202843
[email protected]4d434a1a2009-02-11 21:06:572844void AutomationProvider::SavePackageShouldPromptUser(bool should_prompt) {
[email protected]5bcdb312009-01-07 21:43:202845 SavePackage::SetShouldPromptUser(should_prompt);
2846}
[email protected]87eab222009-03-13 00:47:452847
[email protected]b83e4602009-05-15 22:58:332848void AutomationProvider::SetEnableExtensionAutomation(bool automation_enabled) {
2849 AutomationExtensionFunction::SetEnabled(automation_enabled);
2850}
2851
[email protected]d2cc6ed2009-04-24 00:26:172852#if defined(OS_WIN)
2853// TODO(port): Reposition_Params is win-specific. We'll need to port it.
[email protected]87eab222009-03-13 00:47:452854void AutomationProvider::OnTabReposition(
2855 int tab_handle, const IPC::Reposition_Params& params) {
2856 if (!tab_tracker_->ContainsHandle(tab_handle))
2857 return;
2858
2859 if (!IsWindow(params.window))
2860 return;
2861
2862 unsigned long process_id = 0;
2863 unsigned long thread_id = 0;
2864
2865 thread_id = GetWindowThreadProcessId(params.window, &process_id);
2866
2867 if (thread_id != GetCurrentThreadId()) {
2868 NOTREACHED();
2869 return;
2870 }
2871
[email protected]a9233d0f2009-04-20 05:39:332872 SetWindowPos(params.window, params.window_insert_after, params.left,
2873 params.top, params.width, params.height, params.flags);
2874
[email protected]a2c5a9892009-04-07 16:13:452875 if (params.set_parent) {
2876 if (IsWindow(params.parent_window)) {
2877 if (!SetParent(params.window, params.parent_window))
2878 DLOG(WARNING) << "SetParent failed. Error 0x%x" << GetLastError();
2879 }
2880 }
[email protected]87eab222009-03-13 00:47:452881}
[email protected]d2cc6ed2009-04-24 00:26:172882#endif
[email protected]3753f522009-04-14 23:15:472883
2884void AutomationProvider::GetWindowTitle(int handle, string16* text) {
2885 gfx::NativeWindow window = window_tracker_->GetResource(handle);
2886 text->assign(platform_util::GetWindowTitle(window));
2887}