OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/automation/automation_provider.h" | 5 #include "chrome/browser/automation/automation_provider.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/automation/automation_browser_tracker.h" | 10 #include "chrome/browser/automation/automation_browser_tracker.h" |
11 #include "chrome/browser/automation/automation_extension_function.h" | |
12 #include "chrome/browser/automation/automation_tab_tracker.h" | 11 #include "chrome/browser/automation/automation_tab_tracker.h" |
13 #include "chrome/browser/automation/automation_window_tracker.h" | 12 #include "chrome/browser/automation/automation_window_tracker.h" |
14 #include "chrome/browser/automation/extension_automation_constants.h" | |
15 #include "chrome/browser/automation/extension_port_container.h" | |
16 #include "chrome/browser/automation/ui_controls.h" | 13 #include "chrome/browser/automation/ui_controls.h" |
17 #include "chrome/browser/browser_window.h" | 14 #include "chrome/browser/browser_window.h" |
18 #include "chrome/browser/extensions/extension_event_router.h" | |
19 #include "chrome/browser/external_tab_container_win.h" | 15 #include "chrome/browser/external_tab_container_win.h" |
20 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
22 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" | 18 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" |
23 #include "chrome/common/automation_messages.h" | 19 #include "chrome/common/automation_messages.h" |
24 #include "content/browser/renderer_host/render_view_host.h" | 20 #include "content/browser/renderer_host/render_view_host.h" |
25 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/tab_contents/tab_contents.h" |
26 #include "content/common/page_zoom.h" | 22 #include "content/common/page_zoom.h" |
27 #include "ui/base/keycodes/keyboard_codes.h" | 23 #include "ui/base/keycodes/keyboard_codes.h" |
28 #include "views/focus/accelerator_handler.h" | 24 #include "views/focus/accelerator_handler.h" |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 *tab_container_window = external_tab_container->GetNativeView(); | 394 *tab_container_window = external_tab_container->GetNativeView(); |
399 *tab_window = tab_contents->GetNativeView(); | 395 *tab_window = tab_contents->GetNativeView(); |
400 *session_id = tab_contents->controller().session_id().id(); | 396 *session_id = tab_contents->controller().session_id().id(); |
401 } else { | 397 } else { |
402 external_tab_container->Uninitialize(); | 398 external_tab_container->Uninitialize(); |
403 } | 399 } |
404 | 400 |
405 TRACE_EVENT_END("AutomationProvider::ConnectExternalTab", 0, ""); | 401 TRACE_EVENT_END("AutomationProvider::ConnectExternalTab", 0, ""); |
406 } | 402 } |
407 | 403 |
408 void AutomationProvider::SetEnableExtensionAutomation( | |
409 int tab_handle, | |
410 const std::vector<std::string>& functions_enabled) { | |
411 ExternalTabContainer* external_tab = GetExternalTabForHandle(tab_handle); | |
412 if (external_tab) { | |
413 external_tab->SetEnableExtensionAutomation(functions_enabled); | |
414 } else { | |
415 // Tab must exist, and must be an external tab so that its | |
416 // delegate has an on-empty | |
417 // implementation of ForwardMessageToExternalHost. | |
418 DLOG(WARNING) << | |
419 "SetEnableExtensionAutomation called with invalid tab handle."; | |
420 } | |
421 } | |
422 | |
423 void AutomationProvider::OnBrowserMoved(int tab_handle) { | 404 void AutomationProvider::OnBrowserMoved(int tab_handle) { |
424 ExternalTabContainer* external_tab = GetExternalTabForHandle(tab_handle); | 405 ExternalTabContainer* external_tab = GetExternalTabForHandle(tab_handle); |
425 if (external_tab) { | 406 if (external_tab) { |
426 external_tab->WindowMoved(); | 407 external_tab->WindowMoved(); |
427 } else { | 408 } else { |
428 DLOG(WARNING) << | 409 DLOG(WARNING) << |
429 "AutomationProvider::OnBrowserMoved called with invalid tab handle."; | 410 "AutomationProvider::OnBrowserMoved called with invalid tab handle."; |
430 } | 411 } |
431 } | 412 } |
432 | 413 |
433 void AutomationProvider::OnMessageFromExternalHost(int handle, | 414 void AutomationProvider::OnMessageFromExternalHost(int handle, |
434 const std::string& message, | 415 const std::string& message, |
435 const std::string& origin, | 416 const std::string& origin, |
436 const std::string& target) { | 417 const std::string& target) { |
437 RenderViewHost* view_host = GetViewForTab(handle); | 418 RenderViewHost* view_host = GetViewForTab(handle); |
438 if (!view_host) | 419 if (!view_host) |
439 return; | 420 return; |
440 | 421 |
441 if (AutomationExtensionFunction::InterceptMessageFromExternalHost( | |
442 view_host, message, origin, target)) { | |
443 // Message was diverted. | |
444 return; | |
445 } | |
446 | |
447 if (ExtensionPortContainer::InterceptMessageFromExternalHost( | |
448 message, origin, target, this, view_host, handle)) { | |
449 // Message was diverted. | |
450 return; | |
451 } | |
452 | |
453 if (InterceptBrowserEventMessageFromExternalHost(message, origin, target)) { | |
454 // Message was diverted. | |
455 return; | |
456 } | |
457 | |
458 view_host->ForwardMessageFromExternalHost(message, origin, target); | 422 view_host->ForwardMessageFromExternalHost(message, origin, target); |
459 } | 423 } |
460 | 424 |
461 bool AutomationProvider::InterceptBrowserEventMessageFromExternalHost( | |
462 const std::string& message, const std::string& origin, | |
463 const std::string& target) { | |
464 if (target != | |
465 extension_automation_constants::kAutomationBrowserEventRequestTarget) | |
466 return false; | |
467 | |
468 if (origin != extension_automation_constants::kAutomationOrigin) { | |
469 LOG(WARNING) << "Wrong origin on automation browser event " << origin; | |
470 return false; | |
471 } | |
472 | |
473 // The message is a JSON-encoded array with two elements, both strings. The | |
474 // first is the name of the event to dispatch. The second is a JSON-encoding | |
475 // of the arguments specific to that event. | |
476 scoped_ptr<Value> message_value(base::JSONReader::Read(message, false)); | |
477 if (!message_value.get() || !message_value->IsType(Value::TYPE_LIST)) { | |
478 LOG(WARNING) << "Invalid browser event specified through automation"; | |
479 return false; | |
480 } | |
481 | |
482 const ListValue* args = static_cast<const ListValue*>(message_value.get()); | |
483 | |
484 std::string event_name; | |
485 if (!args->GetString(0, &event_name)) { | |
486 LOG(WARNING) << "No browser event name specified through automation"; | |
487 return false; | |
488 } | |
489 | |
490 std::string json_args; | |
491 if (!args->GetString(1, &json_args)) { | |
492 LOG(WARNING) << "No browser event args specified through automation"; | |
493 return false; | |
494 } | |
495 | |
496 if (profile()->GetExtensionEventRouter()) { | |
497 profile()->GetExtensionEventRouter()->DispatchEventToRenderers( | |
498 event_name, json_args, profile(), GURL()); | |
499 } | |
500 | |
501 return true; | |
502 } | |
503 | |
504 void AutomationProvider::NavigateInExternalTab( | 425 void AutomationProvider::NavigateInExternalTab( |
505 int handle, const GURL& url, const GURL& referrer, | 426 int handle, const GURL& url, const GURL& referrer, |
506 AutomationMsg_NavigationResponseValues* status) { | 427 AutomationMsg_NavigationResponseValues* status) { |
507 *status = AUTOMATION_MSG_NAVIGATION_ERROR; | 428 *status = AUTOMATION_MSG_NAVIGATION_ERROR; |
508 | 429 |
509 if (tab_tracker_->ContainsHandle(handle)) { | 430 if (tab_tracker_->ContainsHandle(handle)) { |
510 NavigationController* tab = tab_tracker_->GetResource(handle); | 431 NavigationController* tab = tab_tracker_->GetResource(handle); |
511 tab->LoadURL(url, referrer, PageTransition::TYPED); | 432 tab->LoadURL(url, referrer, PageTransition::TYPED); |
512 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS; | 433 *status = AUTOMATION_MSG_NAVIGATION_SUCCESS; |
513 } | 434 } |
(...skipping 21 matching lines...) Expand all Loading... |
535 | 456 |
536 void AutomationProvider::OnSetZoomLevel(int handle, int zoom_level) { | 457 void AutomationProvider::OnSetZoomLevel(int handle, int zoom_level) { |
537 if (tab_tracker_->ContainsHandle(handle)) { | 458 if (tab_tracker_->ContainsHandle(handle)) { |
538 NavigationController* tab = tab_tracker_->GetResource(handle); | 459 NavigationController* tab = tab_tracker_->GetResource(handle); |
539 if (tab->tab_contents() && tab->tab_contents()->render_view_host()) { | 460 if (tab->tab_contents() && tab->tab_contents()->render_view_host()) { |
540 tab->tab_contents()->render_view_host()->Zoom( | 461 tab->tab_contents()->render_view_host()->Zoom( |
541 static_cast<PageZoom::Function>(zoom_level)); | 462 static_cast<PageZoom::Function>(zoom_level)); |
542 } | 463 } |
543 } | 464 } |
544 } | 465 } |
OLD | NEW |