| OLD | NEW |
| 1 // Copyright (c) 2012 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 |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 for (i = sorted_ids.begin(); | 518 for (i = sorted_ids.begin(); |
| 519 i != sorted_ids.end(); ++i) { | 519 i != sorted_ids.end(); ++i) { |
| 520 AppendExtensionItems(i->second, &index); | 520 AppendExtensionItems(i->second, &index); |
| 521 } | 521 } |
| 522 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime", | 522 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime", |
| 523 base::TimeTicks::Now() - begin); | 523 base::TimeTicks::Now() - begin); |
| 524 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index); | 524 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index); |
| 525 } | 525 } |
| 526 | 526 |
| 527 void RenderViewContextMenu::InitMenu() { | 527 void RenderViewContextMenu::InitMenu() { |
| 528 if (GetPlatformApp()) { | 528 const Extension* extension = GetExtension(); |
| 529 AppendPlatformAppItems(); | 529 if (extension) { |
| 530 if (extension->is_platform_app()) |
| 531 AppendPlatformAppItems(extension); |
| 532 else |
| 533 AppendPopupExtensionItems(); |
| 530 return; | 534 return; |
| 531 } | 535 } |
| 532 | 536 |
| 533 bool has_link = !params_.unfiltered_link_url.is_empty(); | 537 bool has_link = !params_.unfiltered_link_url.is_empty(); |
| 534 bool has_selection = !params_.selection_text.empty(); | 538 bool has_selection = !params_.selection_text.empty(); |
| 535 | 539 |
| 536 if (AppendCustomItems()) { | 540 if (AppendCustomItems()) { |
| 537 // If there's a selection, don't early return when there are custom items, | 541 // If there's a selection, don't early return when there are custom items, |
| 538 // but fall through to adding the normal ones after the custom ones. | 542 // but fall through to adding the normal ones after the custom ones. |
| 539 if (has_selection) { | 543 if (has_selection) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 break; | 594 break; |
| 591 case WebContextMenuData::MediaTypePlugin: | 595 case WebContextMenuData::MediaTypePlugin: |
| 592 AppendPluginItems(); | 596 AppendPluginItems(); |
| 593 break; | 597 break; |
| 594 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED | 598 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED |
| 595 case WebContextMenuData::MediaTypeFile: | 599 case WebContextMenuData::MediaTypeFile: |
| 596 break; | 600 break; |
| 597 #endif | 601 #endif |
| 598 } | 602 } |
| 599 | 603 |
| 600 if (params_.is_editable) { | |
| 601 // Add a menu item that shows suggestions. | |
| 602 if (!spelling_menu_observer_.get()) { | |
| 603 spelling_menu_observer_.reset(new SpellingMenuObserver(this)); | |
| 604 } | |
| 605 if (spelling_menu_observer_.get()) { | |
| 606 observers_.AddObserver(spelling_menu_observer_.get()); | |
| 607 spelling_menu_observer_->InitMenu(params_); | |
| 608 } | |
| 609 } | |
| 610 | |
| 611 if (params_.is_editable) | 604 if (params_.is_editable) |
| 612 AppendEditableItems(); | 605 AppendEditableItems(); |
| 613 else if (has_selection) | 606 else if (has_selection) |
| 614 AppendCopyItem(); | 607 AppendCopyItem(); |
| 615 | 608 |
| 616 if (has_selection) | 609 if (has_selection) |
| 617 AppendSearchProvider(); | 610 AppendSearchProvider(); |
| 618 | 611 |
| 619 if (!IsDevToolsURL(params_.page_url)) | 612 if (!IsDevToolsURL(params_.page_url)) |
| 620 AppendAllExtensionItems(); | 613 AppendAllExtensionItems(); |
| 621 | 614 |
| 622 AppendDeveloperItems(); | 615 AppendDeveloperItems(); |
| 623 | 616 |
| 624 if (!print_preview_menu_observer_.get()) { | 617 if (!print_preview_menu_observer_.get()) { |
| 625 TabContentsWrapper* wrapper = | 618 TabContentsWrapper* wrapper = |
| 626 TabContentsWrapper::GetCurrentWrapperForContents(source_web_contents_); | 619 TabContentsWrapper::GetCurrentWrapperForContents(source_web_contents_); |
| 627 print_preview_menu_observer_.reset( | 620 print_preview_menu_observer_.reset( |
| 628 new PrintPreviewContextMenuObserver(wrapper)); | 621 new PrintPreviewContextMenuObserver(wrapper)); |
| 629 } | 622 } |
| 630 observers_.AddObserver(print_preview_menu_observer_.get()); | 623 observers_.AddObserver(print_preview_menu_observer_.get()); |
| 631 } | 624 } |
| 632 | 625 |
| 633 const Extension* RenderViewContextMenu::GetPlatformApp() const { | 626 const Extension* RenderViewContextMenu::GetExtension() const { |
| 634 ExtensionProcessManager* process_manager = | 627 ExtensionProcessManager* process_manager = |
| 635 profile_->GetExtensionProcessManager(); | 628 profile_->GetExtensionProcessManager(); |
| 636 // There is no process manager in some tests. | 629 // There is no process manager in some tests. |
| 637 if (!process_manager) { | 630 if (!process_manager) { |
| 638 return NULL; | 631 return NULL; |
| 639 } | 632 } |
| 640 | 633 |
| 641 ExtensionProcessManager::const_iterator iter; | 634 ExtensionProcessManager::const_iterator iter; |
| 642 for (iter = process_manager->begin(); iter != process_manager->end(); | 635 for (iter = process_manager->begin(); iter != process_manager->end(); |
| 643 ++iter) { | 636 ++iter) { |
| 644 ExtensionHost* host = *iter; | 637 ExtensionHost* host = *iter; |
| 645 if (host->host_contents() == source_web_contents_) { | 638 if (host->host_contents() == source_web_contents_) |
| 646 if (host->extension() && host->extension()->is_platform_app()) { | 639 return host->extension(); |
| 647 return host->extension(); | |
| 648 } | |
| 649 } | |
| 650 } | 640 } |
| 651 | 641 |
| 652 return NULL; | 642 return NULL; |
| 653 } | 643 } |
| 654 | 644 |
| 655 void RenderViewContextMenu::AppendPlatformAppItems() { | 645 void RenderViewContextMenu::AppendPlatformAppItems( |
| 656 const Extension* platform_app = GetPlatformApp(); | 646 const Extension* platform_app) { |
| 657 DCHECK(platform_app); | 647 DCHECK(platform_app); |
| 658 int index = 0; | 648 int index = 0; |
| 659 AppendExtensionItems(platform_app->id(), &index); | 649 AppendExtensionItems(platform_app->id(), &index); |
| 650 |
| 651 // Add dev tools for unpacked extensions. |
| 652 if (platform_app->location() == Extension::LOAD) |
| 653 AppendDeveloperItems(); |
| 654 } |
| 655 |
| 656 void RenderViewContextMenu::AppendPopupExtensionItems() { |
| 657 bool has_selection = !params_.selection_text.empty(); |
| 658 |
| 659 if (params_.is_editable) |
| 660 AppendEditableItems(); |
| 661 else if (has_selection) |
| 662 AppendCopyItem(); |
| 663 |
| 664 if (has_selection) |
| 665 AppendSearchProvider(); |
| 666 |
| 667 AppendAllExtensionItems(); |
| 668 AppendDeveloperItems(); |
| 660 } | 669 } |
| 661 | 670 |
| 662 void RenderViewContextMenu::LookUpInDictionary() { | 671 void RenderViewContextMenu::LookUpInDictionary() { |
| 663 // Used only in the Mac port. | 672 // Used only in the Mac port. |
| 664 NOTREACHED(); | 673 NOTREACHED(); |
| 665 } | 674 } |
| 666 | 675 |
| 667 void RenderViewContextMenu::AddMenuItem(int command_id, | 676 void RenderViewContextMenu::AddMenuItem(int command_id, |
| 668 const string16& title) { | 677 const string16& title) { |
| 669 menu_model_.AddItem(command_id, title); | 678 menu_model_.AddItem(command_id, title); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 return; | 917 return; |
| 909 menu_model_.AddItem( | 918 menu_model_.AddItem( |
| 910 IDC_CONTENT_CONTEXT_SEARCHWEBFOR, | 919 IDC_CONTENT_CONTEXT_SEARCHWEBFOR, |
| 911 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, | 920 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, |
| 912 default_provider->short_name(), | 921 default_provider->short_name(), |
| 913 printable_selection_text)); | 922 printable_selection_text)); |
| 914 } | 923 } |
| 915 } | 924 } |
| 916 | 925 |
| 917 void RenderViewContextMenu::AppendEditableItems() { | 926 void RenderViewContextMenu::AppendEditableItems() { |
| 927 AppendSpellingSuggestionsSubMenu(); |
| 928 |
| 918 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_UNDO, | 929 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_UNDO, |
| 919 IDS_CONTENT_CONTEXT_UNDO); | 930 IDS_CONTENT_CONTEXT_UNDO); |
| 920 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_REDO, | 931 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_REDO, |
| 921 IDS_CONTENT_CONTEXT_REDO); | 932 IDS_CONTENT_CONTEXT_REDO); |
| 922 menu_model_.AddSeparator(); | 933 menu_model_.AddSeparator(); |
| 923 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_CUT, | 934 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_CUT, |
| 924 IDS_CONTENT_CONTEXT_CUT); | 935 IDS_CONTENT_CONTEXT_CUT); |
| 925 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPY, | 936 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPY, |
| 926 IDS_CONTENT_CONTEXT_COPY); | 937 IDS_CONTENT_CONTEXT_COPY); |
| 927 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE, | 938 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 946 // languages. | 957 // languages. |
| 947 // This functionality is exposed as a keyboard shortcut on Windows & Linux. | 958 // This functionality is exposed as a keyboard shortcut on Windows & Linux. |
| 948 AppendBidiSubMenu(); | 959 AppendBidiSubMenu(); |
| 949 #endif // OS_MACOSX | 960 #endif // OS_MACOSX |
| 950 | 961 |
| 951 menu_model_.AddSeparator(); | 962 menu_model_.AddSeparator(); |
| 952 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_SELECTALL, | 963 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_SELECTALL, |
| 953 IDS_CONTENT_CONTEXT_SELECTALL); | 964 IDS_CONTENT_CONTEXT_SELECTALL); |
| 954 } | 965 } |
| 955 | 966 |
| 967 void RenderViewContextMenu::AppendSpellingSuggestionsSubMenu() { |
| 968 if (!spelling_menu_observer_.get()) |
| 969 spelling_menu_observer_.reset(new SpellingMenuObserver(this)); |
| 970 observers_.AddObserver(spelling_menu_observer_.get()); |
| 971 spelling_menu_observer_->InitMenu(params_); |
| 972 } |
| 973 |
| 956 void RenderViewContextMenu::AppendSpellcheckOptionsSubMenu() { | 974 void RenderViewContextMenu::AppendSpellcheckOptionsSubMenu() { |
| 957 if (!spellchecker_submenu_observer_.get()) { | 975 if (!spellchecker_submenu_observer_.get()) { |
| 958 spellchecker_submenu_observer_.reset(new SpellCheckerSubMenuObserver( | 976 spellchecker_submenu_observer_.reset(new SpellCheckerSubMenuObserver( |
| 959 this, this, kSpellcheckRadioGroup)); | 977 this, this, kSpellcheckRadioGroup)); |
| 960 } | 978 } |
| 961 if (spellchecker_submenu_observer_.get()) { | 979 spellchecker_submenu_observer_->InitMenu(params_); |
| 962 spellchecker_submenu_observer_->InitMenu(params_); | 980 observers_.AddObserver(spellchecker_submenu_observer_.get()); |
| 963 observers_.AddObserver(spellchecker_submenu_observer_.get()); | |
| 964 } | |
| 965 } | 981 } |
| 966 | 982 |
| 967 void RenderViewContextMenu::AppendSpeechInputOptionsSubMenu() { | 983 void RenderViewContextMenu::AppendSpeechInputOptionsSubMenu() { |
| 968 if (params_.speech_input_enabled) { | 984 if (params_.speech_input_enabled) { |
| 969 speech_input_submenu_model_.AddCheckItem( | 985 speech_input_submenu_model_.AddCheckItem( |
| 970 IDC_CONTENT_CONTEXT_SPEECH_INPUT_FILTER_PROFANITIES, | 986 IDC_CONTENT_CONTEXT_SPEECH_INPUT_FILTER_PROFANITIES, |
| 971 l10n_util::GetStringUTF16( | 987 l10n_util::GetStringUTF16( |
| 972 IDS_CONTENT_CONTEXT_SPEECH_INPUT_FILTER_PROFANITIES)); | 988 IDS_CONTENT_CONTEXT_SPEECH_INPUT_FILTER_PROFANITIES)); |
| 973 | 989 |
| 974 speech_input_submenu_model_.AddItemWithStringId( | 990 speech_input_submenu_model_.AddItemWithStringId( |
| (...skipping 890 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1865 if (view) | 1881 if (view) |
| 1866 view->ShowingContextMenu(false); | 1882 view->ShowingContextMenu(false); |
| 1867 RenderViewHost* rvh = source_web_contents_->GetRenderViewHost(); | 1883 RenderViewHost* rvh = source_web_contents_->GetRenderViewHost(); |
| 1868 if (rvh) { | 1884 if (rvh) { |
| 1869 rvh->NotifyContextMenuClosed(params_.custom_context); | 1885 rvh->NotifyContextMenuClosed(params_.custom_context); |
| 1870 } | 1886 } |
| 1871 } | 1887 } |
| 1872 | 1888 |
| 1873 bool RenderViewContextMenu::IsDevCommandEnabled(int id) const { | 1889 bool RenderViewContextMenu::IsDevCommandEnabled(int id) const { |
| 1874 if (id == IDC_CONTENT_CONTEXT_INSPECTELEMENT) { | 1890 if (id == IDC_CONTENT_CONTEXT_INSPECTELEMENT) { |
| 1875 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 1891 // Don't enable the web inspector if JavaScript is disabled. We don't |
| 1876 TabContentsWrapper* tab_contents_wrapper = | 1892 // check this when this is the web contents of an extension (e.g. |
| 1877 TabContentsWrapper::GetCurrentWrapperForContents( | 1893 // for a popup extension or a platform app) as they have JavaScript |
| 1878 source_web_contents_); | 1894 // always enabled. |
| 1879 if (!tab_contents_wrapper) | 1895 const Extension* extension = GetExtension(); |
| 1880 return false; | 1896 if (!extension) { |
| 1881 // Don't enable the web inspector if JavaScript is disabled. | 1897 TabContentsWrapper* contents_wrapper = |
| 1882 if (!tab_contents_wrapper->prefs_tab_helper()->per_tab_prefs()->GetBoolean( | 1898 TabContentsWrapper::GetCurrentWrapperForContents( |
| 1883 prefs::kWebKitJavascriptEnabled) || | 1899 source_web_contents_); |
| 1884 command_line.HasSwitch(switches::kDisableJavaScript)) | 1900 if (!contents_wrapper) |
| 1885 return false; | 1901 return false; |
| 1902 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1903 if (!contents_wrapper->prefs_tab_helper()->per_tab_prefs()->GetBoolean( |
| 1904 prefs::kWebKitJavascriptEnabled) || |
| 1905 command_line->HasSwitch(switches::kDisableJavaScript)) |
| 1906 return false; |
| 1907 #if !defined(TOOLKIT_VIEWS) |
| 1908 } else { |
| 1909 // Disable dev tools for popup extensions for non-views builds, as the |
| 1910 // extension popups for these builds do not support dynamically inspecting |
| 1911 // the popups. |
| 1912 // TODO(benwells): Add support for these builds and remove this #if. |
| 1913 if (!extension->is_platform_app()) |
| 1914 return false; |
| 1915 #endif |
| 1916 } |
| 1917 |
| 1886 // Don't enable the web inspector if the developer tools are disabled via | 1918 // Don't enable the web inspector if the developer tools are disabled via |
| 1887 // the preference dev-tools-disabled. | 1919 // the preference dev-tools-disabled. |
| 1888 if (profile_->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled)) | 1920 if (profile_->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled)) |
| 1889 return false; | 1921 return false; |
| 1890 } | 1922 } |
| 1891 | 1923 |
| 1892 return true; | 1924 return true; |
| 1893 } | 1925 } |
| 1894 | 1926 |
| 1895 string16 RenderViewContextMenu::PrintableSelectionText() { | 1927 string16 RenderViewContextMenu::PrintableSelectionText() { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1945 source_web_contents_->GetRenderViewHost()-> | 1977 source_web_contents_->GetRenderViewHost()-> |
| 1946 ExecuteMediaPlayerActionAtLocation(location, action); | 1978 ExecuteMediaPlayerActionAtLocation(location, action); |
| 1947 } | 1979 } |
| 1948 | 1980 |
| 1949 void RenderViewContextMenu::PluginActionAt( | 1981 void RenderViewContextMenu::PluginActionAt( |
| 1950 const gfx::Point& location, | 1982 const gfx::Point& location, |
| 1951 const WebPluginAction& action) { | 1983 const WebPluginAction& action) { |
| 1952 source_web_contents_->GetRenderViewHost()-> | 1984 source_web_contents_->GetRenderViewHost()-> |
| 1953 ExecutePluginActionAtLocation(location, action); | 1985 ExecutePluginActionAtLocation(location, action); |
| 1954 } | 1986 } |
| OLD | NEW |