OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
6 #include <set> | 6 #include <set> |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 9 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
10 | 10 |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/time.h" | 16 #include "base/time.h" |
17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
18 #include "chrome/app/chrome_command_ids.h" | 18 #include "chrome/app/chrome_command_ids.h" |
19 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 19 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
20 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 20 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
21 #include "chrome/browser/autocomplete/autocomplete_match.h" | 21 #include "chrome/browser/autocomplete/autocomplete_match.h" |
22 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
23 #include "chrome/browser/debugger/devtools_window.h" | 23 #include "chrome/browser/debugger/devtools_window.h" |
24 #include "chrome/browser/download/download_service.h" | 24 #include "chrome/browser/download/download_service.h" |
25 #include "chrome/browser/download/download_service_factory.h" | 25 #include "chrome/browser/download/download_service_factory.h" |
26 #include "chrome/browser/extensions/extension_event_router.h" | 26 #include "chrome/browser/extensions/extension_event_router.h" |
| 27 #include "chrome/browser/extensions/extension_host.h" |
27 #include "chrome/browser/extensions/extension_service.h" | 28 #include "chrome/browser/extensions/extension_service.h" |
28 #include "chrome/browser/google/google_util.h" | 29 #include "chrome/browser/google/google_util.h" |
29 #include "chrome/browser/net/browser_url_util.h" | 30 #include "chrome/browser/net/browser_url_util.h" |
30 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 31 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
31 #include "chrome/browser/prefs/pref_member.h" | 32 #include "chrome/browser/prefs/pref_member.h" |
32 #include "chrome/browser/prefs/pref_service.h" | 33 #include "chrome/browser/prefs/pref_service.h" |
33 #include "chrome/browser/printing/print_preview_context_menu_observer.h" | 34 #include "chrome/browser/printing/print_preview_context_menu_observer.h" |
34 #include "chrome/browser/printing/print_preview_tab_controller.h" | 35 #include "chrome/browser/printing/print_preview_tab_controller.h" |
35 #include "chrome/browser/printing/print_view_manager.h" | 36 #include "chrome/browser/printing/print_view_manager.h" |
36 #include "chrome/browser/profiles/profile.h" | 37 #include "chrome/browser/profiles/profile.h" |
37 #include "chrome/browser/profiles/profile_io_data.h" | 38 #include "chrome/browser/profiles/profile_io_data.h" |
38 #include "chrome/browser/search_engines/template_url.h" | 39 #include "chrome/browser/search_engines/template_url.h" |
39 #include "chrome/browser/search_engines/template_url_service.h" | 40 #include "chrome/browser/search_engines/template_url_service.h" |
40 #include "chrome/browser/search_engines/template_url_service_factory.h" | 41 #include "chrome/browser/search_engines/template_url_service_factory.h" |
41 #include "chrome/browser/spellchecker/spellcheck_host.h" | 42 #include "chrome/browser/spellchecker/spellcheck_host.h" |
42 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 43 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
43 #include "chrome/browser/tab_contents/retargeting_details.h" | 44 #include "chrome/browser/tab_contents/retargeting_details.h" |
44 #include "chrome/browser/tab_contents/spellchecker_submenu_observer.h" | 45 #include "chrome/browser/tab_contents/spellchecker_submenu_observer.h" |
45 #include "chrome/browser/tab_contents/spelling_menu_observer.h" | 46 #include "chrome/browser/tab_contents/spelling_menu_observer.h" |
46 #include "chrome/browser/translate/translate_manager.h" | 47 #include "chrome/browser/translate/translate_manager.h" |
47 #include "chrome/browser/translate/translate_prefs.h" | 48 #include "chrome/browser/translate/translate_prefs.h" |
48 #include "chrome/browser/translate/translate_tab_helper.h" | 49 #include "chrome/browser/translate/translate_tab_helper.h" |
49 #include "chrome/browser/ui/browser.h" | 50 #include "chrome/browser/ui/browser.h" |
50 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" | 51 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" |
51 #include "chrome/browser/ui/browser_list.h" | |
52 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" | 52 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" |
53 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 53 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
54 #include "chrome/browser/web_applications/web_app.h" | |
55 #include "chrome/common/chrome_constants.h" | 54 #include "chrome/common/chrome_constants.h" |
56 #include "chrome/common/chrome_notification_types.h" | 55 #include "chrome/common/chrome_notification_types.h" |
57 #include "chrome/common/chrome_switches.h" | 56 #include "chrome/common/chrome_switches.h" |
| 57 #include "chrome/common/extensions/extension.h" |
58 #include "chrome/common/pref_names.h" | 58 #include "chrome/common/pref_names.h" |
59 #include "chrome/common/print_messages.h" | 59 #include "chrome/common/print_messages.h" |
60 #include "chrome/common/spellcheck_messages.h" | 60 #include "chrome/common/spellcheck_messages.h" |
61 #include "chrome/common/url_constants.h" | 61 #include "chrome/common/url_constants.h" |
62 #include "content/browser/child_process_security_policy.h" | 62 #include "content/browser/child_process_security_policy.h" |
63 #include "content/browser/download/download_stats.h" | 63 #include "content/browser/download/download_stats.h" |
64 #include "content/browser/download/save_package.h" | 64 #include "content/browser/download/save_package.h" |
65 #include "content/browser/renderer_host/render_view_host.h" | 65 #include "content/browser/renderer_host/render_view_host.h" |
66 #include "content/browser/renderer_host/render_widget_host_view.h" | 66 #include "content/browser/renderer_host/render_widget_host_view.h" |
67 #include "content/browser/speech/speech_input_preferences.h" | 67 #include "content/browser/speech/speech_input_preferences.h" |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 if (!service) | 491 if (!service) |
492 return; // In unit-tests, we may not have an ExtensionService. | 492 return; // In unit-tests, we may not have an ExtensionService. |
493 ExtensionMenuManager* menu_manager = service->menu_manager(); | 493 ExtensionMenuManager* menu_manager = service->menu_manager(); |
494 | 494 |
495 // Get a list of extension id's that have context menu items, and sort it by | 495 // Get a list of extension id's that have context menu items, and sort it by |
496 // the extension's name. | 496 // the extension's name. |
497 std::set<std::string> ids = menu_manager->ExtensionIds(); | 497 std::set<std::string> ids = menu_manager->ExtensionIds(); |
498 std::vector<std::pair<std::string, std::string> > sorted_ids; | 498 std::vector<std::pair<std::string, std::string> > sorted_ids; |
499 for (std::set<std::string>::iterator i = ids.begin(); i != ids.end(); ++i) { | 499 for (std::set<std::string>::iterator i = ids.begin(); i != ids.end(); ++i) { |
500 const Extension* extension = service->GetExtensionById(*i, false); | 500 const Extension* extension = service->GetExtensionById(*i, false); |
501 // Platform apps have their context menus created directly in InitMenu. | 501 // Platform apps have their context menus created directly in |
| 502 // AppendPlatformAppItems. |
502 if (extension && !extension->is_platform_app()) | 503 if (extension && !extension->is_platform_app()) |
503 sorted_ids.push_back( | 504 sorted_ids.push_back( |
504 std::pair<std::string, std::string>(extension->name(), *i)); | 505 std::pair<std::string, std::string>(extension->name(), *i)); |
505 } | 506 } |
506 // TODO(asargent) - See if this works properly for i18n names (bug 32363). | 507 // TODO(asargent) - See if this works properly for i18n names (bug 32363). |
507 std::sort(sorted_ids.begin(), sorted_ids.end()); | 508 std::sort(sorted_ids.begin(), sorted_ids.end()); |
508 | 509 |
509 if (sorted_ids.empty()) | 510 if (sorted_ids.empty()) |
510 return; | 511 return; |
511 | 512 |
512 int index = 0; | 513 int index = 0; |
513 base::TimeTicks begin = base::TimeTicks::Now(); | 514 base::TimeTicks begin = base::TimeTicks::Now(); |
514 std::vector<std::pair<std::string, std::string> >::const_iterator i; | 515 std::vector<std::pair<std::string, std::string> >::const_iterator i; |
515 for (i = sorted_ids.begin(); | 516 for (i = sorted_ids.begin(); |
516 i != sorted_ids.end(); ++i) { | 517 i != sorted_ids.end(); ++i) { |
517 AppendExtensionItems(i->second, &index); | 518 AppendExtensionItems(i->second, &index); |
518 } | 519 } |
519 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime", | 520 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime", |
520 base::TimeTicks::Now() - begin); | 521 base::TimeTicks::Now() - begin); |
521 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index); | 522 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index); |
522 } | 523 } |
523 | 524 |
524 void RenderViewContextMenu::InitMenu() { | 525 void RenderViewContextMenu::InitMenu() { |
| 526 if (GetPlatformApp()) { |
| 527 AppendPlatformAppItems(); |
| 528 return; |
| 529 } |
| 530 |
525 bool has_link = !params_.unfiltered_link_url.is_empty(); | 531 bool has_link = !params_.unfiltered_link_url.is_empty(); |
526 bool has_selection = !params_.selection_text.empty(); | 532 bool has_selection = !params_.selection_text.empty(); |
527 | 533 |
528 Browser* active_browser = BrowserList::FindBrowserWithWebContents( | |
529 source_web_contents_); | |
530 if (active_browser && active_browser->is_app()) { | |
531 const std::string ext_id = web_app::GetExtensionIdFromApplicationName( | |
532 active_browser->app_name()); | |
533 const Extension* app = | |
534 profile_->GetExtensionService()->GetInstalledExtension(ext_id); | |
535 if (app && app->is_platform_app()) { | |
536 int index = 0; | |
537 AppendExtensionItems(app->id(), &index); | |
538 return; | |
539 } | |
540 } | |
541 | |
542 if (AppendCustomItems()) { | 534 if (AppendCustomItems()) { |
543 // If there's a selection, don't early return when there are custom items, | 535 // If there's a selection, don't early return when there are custom items, |
544 // but fall through to adding the normal ones after the custom ones. | 536 // but fall through to adding the normal ones after the custom ones. |
545 if (has_selection) { | 537 if (has_selection) { |
546 menu_model_.AddSeparator(); | 538 menu_model_.AddSeparator(); |
547 } else { | 539 } else { |
548 // Don't add items for Pepper menu. | 540 // Don't add items for Pepper menu. |
549 if (!params_.custom_context.is_pepper_menu) | 541 if (!params_.custom_context.is_pepper_menu) |
550 AppendDeveloperItems(); | 542 AppendDeveloperItems(); |
551 return; | 543 return; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 | 621 |
630 if (!print_preview_menu_observer_.get()) { | 622 if (!print_preview_menu_observer_.get()) { |
631 TabContentsWrapper* wrapper = | 623 TabContentsWrapper* wrapper = |
632 TabContentsWrapper::GetCurrentWrapperForContents(source_web_contents_); | 624 TabContentsWrapper::GetCurrentWrapperForContents(source_web_contents_); |
633 print_preview_menu_observer_.reset( | 625 print_preview_menu_observer_.reset( |
634 new PrintPreviewContextMenuObserver(wrapper)); | 626 new PrintPreviewContextMenuObserver(wrapper)); |
635 } | 627 } |
636 observers_.AddObserver(print_preview_menu_observer_.get()); | 628 observers_.AddObserver(print_preview_menu_observer_.get()); |
637 } | 629 } |
638 | 630 |
| 631 const Extension* RenderViewContextMenu::GetPlatformApp() const { |
| 632 ExtensionProcessManager* process_manager = |
| 633 profile_->GetExtensionProcessManager(); |
| 634 // There is no process manager in some tests. |
| 635 if (!process_manager) { |
| 636 return NULL; |
| 637 } |
| 638 |
| 639 ExtensionProcessManager::const_iterator iter; |
| 640 for (iter = process_manager->begin(); iter != process_manager->end(); |
| 641 ++iter) { |
| 642 ExtensionHost* host = *iter; |
| 643 if (host->host_contents() == source_web_contents_) { |
| 644 if (host->extension() && host->extension()->is_platform_app()) { |
| 645 return host->extension(); |
| 646 } |
| 647 } |
| 648 } |
| 649 |
| 650 return NULL; |
| 651 } |
| 652 |
| 653 void RenderViewContextMenu::AppendPlatformAppItems() { |
| 654 const Extension* platform_app = GetPlatformApp(); |
| 655 DCHECK(platform_app); |
| 656 int index = 0; |
| 657 AppendExtensionItems(platform_app->id(), &index); |
| 658 } |
| 659 |
639 void RenderViewContextMenu::LookUpInDictionary() { | 660 void RenderViewContextMenu::LookUpInDictionary() { |
640 // Used only in the Mac port. | 661 // Used only in the Mac port. |
641 NOTREACHED(); | 662 NOTREACHED(); |
642 } | 663 } |
643 | 664 |
644 void RenderViewContextMenu::AddMenuItem(int command_id, | 665 void RenderViewContextMenu::AddMenuItem(int command_id, |
645 const string16& title) { | 666 const string16& title) { |
646 menu_model_.AddItem(command_id, title); | 667 menu_model_.AddItem(command_id, title); |
647 } | 668 } |
648 | 669 |
(...skipping 1220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1869 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), | 1890 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), |
1870 g_browser_process->clipboard()); | 1891 g_browser_process->clipboard()); |
1871 } | 1892 } |
1872 | 1893 |
1873 void RenderViewContextMenu::MediaPlayerActionAt( | 1894 void RenderViewContextMenu::MediaPlayerActionAt( |
1874 const gfx::Point& location, | 1895 const gfx::Point& location, |
1875 const WebMediaPlayerAction& action) { | 1896 const WebMediaPlayerAction& action) { |
1876 source_web_contents_->GetRenderViewHost()-> | 1897 source_web_contents_->GetRenderViewHost()-> |
1877 ExecuteMediaPlayerActionAtLocation(location, action); | 1898 ExecuteMediaPlayerActionAtLocation(location, action); |
1878 } | 1899 } |
OLD | NEW |