Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/renderer_context_menu/render_view_context_menu.h" | 5 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/threading/thread_restrictions.h" | 22 #include "base/threading/thread_restrictions.h" |
| 23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "chrome/app/chrome_command_ids.h" | 24 #include "chrome/app/chrome_command_ids.h" |
| 25 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
| 26 #include "chrome/browser/chrome_notification_types.h" | 26 #include "chrome/browser/chrome_notification_types.h" |
| 27 #include "chrome/browser/pdf/pdf_extension_test_util.h" | |
| 27 #include "chrome/browser/profiles/profile_attributes_entry.h" | 28 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 28 #include "chrome/browser/profiles/profile_attributes_storage.h" | 29 #include "chrome/browser/profiles/profile_attributes_storage.h" |
| 29 #include "chrome/browser/profiles/profile_window.h" | 30 #include "chrome/browser/profiles/profile_window.h" |
| 30 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert est_util.h" | 31 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert est_util.h" |
| 31 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h" | 32 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti l.h" |
| 32 #include "chrome/browser/search_engines/template_url_service_factory.h" | 33 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 33 #include "chrome/browser/ui/browser.h" | 34 #include "chrome/browser/ui/browser.h" |
| 34 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 35 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 35 #include "chrome/common/render_messages.h" | 36 #include "chrome/common/render_messages.h" |
| 36 #include "chrome/common/thumbnail_capturer.mojom.h" | 37 #include "chrome/common/thumbnail_capturer.mojom.h" |
| 38 #include "chrome/grit/generated_resources.h" | |
| 37 #include "chrome/test/base/in_process_browser_test.h" | 39 #include "chrome/test/base/in_process_browser_test.h" |
| 38 #include "chrome/test/base/search_test_utils.h" | 40 #include "chrome/test/base/search_test_utils.h" |
| 39 #include "chrome/test/base/ui_test_utils.h" | 41 #include "chrome/test/base/ui_test_utils.h" |
| 40 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc hes.h" | 42 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc hes.h" |
| 43 #include "components/guest_view/browser/guest_view_manager_delegate.h" | |
| 44 #include "components/guest_view/browser/test_guest_view_manager.h" | |
| 41 #include "components/search_engines/template_url_data.h" | 45 #include "components/search_engines/template_url_data.h" |
| 42 #include "components/search_engines/template_url_service.h" | 46 #include "components/search_engines/template_url_service.h" |
| 43 #include "content/public/browser/browser_message_filter.h" | 47 #include "content/public/browser/browser_message_filter.h" |
| 48 #include "content/public/browser/browser_plugin_guest_manager.h" | |
| 44 #include "content/public/browser/browser_thread.h" | 49 #include "content/public/browser/browser_thread.h" |
| 45 #include "content/public/browser/navigation_controller.h" | 50 #include "content/public/browser/navigation_controller.h" |
| 46 #include "content/public/browser/navigation_entry.h" | 51 #include "content/public/browser/navigation_entry.h" |
| 47 #include "content/public/browser/notification_service.h" | 52 #include "content/public/browser/notification_service.h" |
| 48 #include "content/public/browser/render_frame_host.h" | 53 #include "content/public/browser/render_frame_host.h" |
| 49 #include "content/public/browser/render_process_host.h" | 54 #include "content/public/browser/render_process_host.h" |
| 50 #include "content/public/browser/render_view_host.h" | 55 #include "content/public/browser/render_view_host.h" |
| 51 #include "content/public/browser/render_widget_host.h" | 56 #include "content/public/browser/render_widget_host.h" |
| 52 #include "content/public/browser/web_contents.h" | 57 #include "content/public/browser/web_contents.h" |
| 53 #include "content/public/test/browser_test_utils.h" | 58 #include "content/public/test/browser_test_utils.h" |
| 54 #include "content/public/test/test_utils.h" | 59 #include "content/public/test/test_utils.h" |
| 60 #include "extensions/browser/api/extensions_api_client.h" | |
| 61 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_gues t.h" | |
| 62 #include "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view _guest.h" | |
| 55 #include "net/base/load_flags.h" | 63 #include "net/base/load_flags.h" |
| 56 #include "net/test/embedded_test_server/embedded_test_server.h" | 64 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 57 #include "net/url_request/url_request.h" | 65 #include "net/url_request/url_request.h" |
| 58 #include "net/url_request/url_request_filter.h" | 66 #include "net/url_request/url_request_filter.h" |
| 59 #include "net/url_request/url_request_interceptor.h" | 67 #include "net/url_request/url_request_interceptor.h" |
| 60 #include "services/service_manager/public/cpp/interface_provider.h" | 68 #include "services/service_manager/public/cpp/interface_provider.h" |
| 61 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 69 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 62 #include "third_party/WebKit/public/web/WebContextMenuData.h" | 70 #include "third_party/WebKit/public/web/WebContextMenuData.h" |
| 63 #include "ui/base/models/menu_model.h" | 71 #include "ui/base/models/menu_model.h" |
| 64 | 72 |
| 65 using content::WebContents; | 73 using content::WebContents; |
| 74 using extensions::MimeHandlerViewGuest; | |
| 75 using extensions::TestMimeHandlerViewGuest; | |
| 66 | 76 |
| 67 namespace { | 77 namespace { |
| 68 | 78 |
| 69 class ContextMenuBrowserTest : public InProcessBrowserTest { | 79 class ContextMenuBrowserTest : public InProcessBrowserTest { |
| 70 public: | 80 public: |
| 71 ContextMenuBrowserTest() {} | 81 ContextMenuBrowserTest() {} |
| 72 | 82 |
| 73 protected: | 83 protected: |
| 74 std::unique_ptr<TestRenderViewContextMenu> CreateContextMenuMediaTypeNone( | 84 std::unique_ptr<TestRenderViewContextMenu> CreateContextMenuMediaTypeNone( |
| 75 const GURL& unfiltered_url, | 85 const GURL& unfiltered_url, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 Profile* CreateSecondaryProfile(int profile_num) { | 127 Profile* CreateSecondaryProfile(int profile_num) { |
| 118 base::ThreadRestrictions::ScopedAllowIO allow_io; | 128 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 119 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 129 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 120 base::FilePath profile_path = profile_manager->user_data_dir(); | 130 base::FilePath profile_path = profile_manager->user_data_dir(); |
| 121 profile_path = profile_path.AppendASCII( | 131 profile_path = profile_path.AppendASCII( |
| 122 base::StringPrintf("New Profile %d", profile_num)); | 132 base::StringPrintf("New Profile %d", profile_num)); |
| 123 return profile_manager->GetProfile(profile_path); | 133 return profile_manager->GetProfile(profile_path); |
| 124 } | 134 } |
| 125 }; | 135 }; |
| 126 | 136 |
| 137 class PdfPluginContextMenuBrowserTest : public InProcessBrowserTest { | |
| 138 public: | |
| 139 PdfPluginContextMenuBrowserTest() {} | |
| 140 | |
| 141 void SetUpOnMainThread() override { | |
| 142 guest_view::GuestViewManager::set_factory_for_testing(&factory_); | |
| 143 test_guest_view_manager_ = static_cast<guest_view::TestGuestViewManager*>( | |
| 144 guest_view::GuestViewManager::CreateWithDelegate( | |
| 145 browser()->profile(), | |
| 146 extensions::ExtensionsAPIClient::Get() | |
| 147 ->CreateGuestViewManagerDelegate(browser()->profile()))); | |
| 148 } | |
| 149 | |
| 150 protected: | |
| 151 guest_view::TestGuestViewManager* test_guest_view_manager() const { | |
| 152 return test_guest_view_manager_; | |
| 153 } | |
| 154 | |
| 155 // Helper function for testing context menu of a pdf plugin inside a web page. | |
| 156 void TestContextMenuOfPdfInsideWebPage( | |
| 157 const base::FilePath::CharType* file_name) { | |
| 158 // Load a page with pdf file inside. | |
| 159 GURL page_url = ui_test_utils::GetTestUrl( | |
| 160 base::FilePath(FILE_PATH_LITERAL("pdf")), base::FilePath(file_name)); | |
| 161 ui_test_utils::NavigateToURL(browser(), page_url); | |
| 162 | |
| 163 WebContents* web_contents = | |
| 164 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 165 // Prepare to load a pdf plugin inside. | |
| 166 test_guest_view_manager_->RegisterTestGuestViewType<MimeHandlerViewGuest>( | |
| 167 base::Bind(&TestMimeHandlerViewGuest::Create)); | |
| 168 ASSERT_TRUE(content::ExecuteScript( | |
| 169 web_contents, | |
| 170 "setTimeout(\"var l = document.getElementById('link1');" | |
|
lazyboy
2017/05/17 16:34:44
Is this setTimeout necessary? Add a comment explai
Wei Li
2017/05/18 23:25:28
Since wait() functions are added, I think this can
| |
| 171 "l.click();\", 0)")); | |
| 172 | |
| 173 // Wait until the guest contents of the PDF plugin is created, | |
| 174 // and then attached. | |
| 175 WebContents* guest_contents = | |
| 176 test_guest_view_manager_->WaitForSingleGuestCreated(); | |
| 177 TestMimeHandlerViewGuest* guest = static_cast<TestMimeHandlerViewGuest*>( | |
| 178 extensions::MimeHandlerViewGuest::FromWebContents(guest_contents)); | |
| 179 ASSERT_TRUE(guest); | |
| 180 guest->WaitForGuestAttached(); | |
| 181 ASSERT_NE(web_contents, guest_contents); | |
| 182 // Get the pdf plugin's main frame. | |
| 183 content::RenderFrameHost* frame = guest_contents->GetMainFrame(); | |
| 184 ASSERT_TRUE(frame); | |
| 185 | |
| 186 content::ContextMenuParams params; | |
| 187 params.page_url = page_url; | |
| 188 params.frame_url = frame->GetLastCommittedURL(); | |
| 189 params.frame_page_state = | |
| 190 content::PageState::CreateFromURL(params.frame_url); | |
| 191 params.media_type = blink::WebContextMenuData::kMediaTypePlugin; | |
| 192 TestRenderViewContextMenu menu(frame, params); | |
| 193 menu.Init(); | |
| 194 | |
| 195 // The full page related items such as 'reload' should not be displayed. | |
| 196 ASSERT_FALSE(menu.IsItemPresent(IDC_RELOAD)); | |
| 197 } | |
| 198 | |
| 199 private: | |
| 200 guest_view::TestGuestViewManagerFactory factory_; | |
| 201 guest_view::TestGuestViewManager* test_guest_view_manager_; | |
| 202 | |
| 203 DISALLOW_COPY_AND_ASSIGN(PdfPluginContextMenuBrowserTest); | |
| 204 }; | |
| 205 | |
| 127 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, | 206 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, |
| 128 OpenEntryPresentForNormalURLs) { | 207 OpenEntryPresentForNormalURLs) { |
| 129 std::unique_ptr<TestRenderViewContextMenu> menu = | 208 std::unique_ptr<TestRenderViewContextMenu> menu = |
| 130 CreateContextMenuMediaTypeNone(GURL("http://www.google.com/"), | 209 CreateContextMenuMediaTypeNone(GURL("http://www.google.com/"), |
| 131 GURL("http://www.google.com/")); | 210 GURL("http://www.google.com/")); |
| 132 | 211 |
| 133 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB)); | 212 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB)); |
| 134 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW)); | 213 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW)); |
| 135 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_COPYLINKLOCATION)); | 214 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_COPYLINKLOCATION)); |
| 136 ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKINPROFILE)); | 215 ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKINPROFILE)); |
| (...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 664 thumbnail_capturer->RequestThumbnailForContextNode( | 743 thumbnail_capturer->RequestThumbnailForContextNode( |
| 665 0, gfx::Size(2048, 2048), | 744 0, gfx::Size(2048, 2048), |
| 666 base::Bind(callback, &response_received, run_loop.QuitClosure())); | 745 base::Bind(callback, &response_received, run_loop.QuitClosure())); |
| 667 run_loop.Run(); | 746 run_loop.Run(); |
| 668 | 747 |
| 669 // The browser should receive a response from the renderer, because the | 748 // The browser should receive a response from the renderer, because the |
| 670 // renderer should not crash. | 749 // renderer should not crash. |
| 671 ASSERT_TRUE(response_received); | 750 ASSERT_TRUE(response_received); |
| 672 } | 751 } |
| 673 | 752 |
| 753 IN_PROC_BROWSER_TEST_F(PdfPluginContextMenuBrowserTest, | |
| 754 FullPagePdfHasPageItems) { | |
| 755 // Load a pdf page. | |
| 756 GURL page_url = | |
| 757 ui_test_utils::GetTestUrl(base::FilePath(FILE_PATH_LITERAL("pdf")), | |
| 758 base::FilePath(FILE_PATH_LITERAL("test.pdf"))); | |
| 759 ui_test_utils::NavigateToURL(browser(), page_url); | |
| 760 | |
| 761 // Get the pdf plugin's WebContents and its main frame. | |
| 762 WebContents* web_contents = | |
| 763 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 764 pdf_extension_test_util::EnsurePDFHasLoaded(web_contents); | |
| 765 content::BrowserPluginGuestManager* guest_manager = | |
| 766 web_contents->GetBrowserContext()->GetGuestManager(); | |
| 767 WebContents* guest_contents = guest_manager->GetFullPageGuest(web_contents); | |
| 768 ASSERT_TRUE(guest_contents); | |
| 769 content::RenderFrameHost* frame = guest_contents->GetMainFrame(); | |
| 770 ASSERT_TRUE(frame); | |
| 771 ASSERT_NE(frame, web_contents->GetMainFrame()); | |
| 772 | |
| 773 content::ContextMenuParams params; | |
| 774 params.page_url = page_url; | |
| 775 params.frame_url = frame->GetLastCommittedURL(); | |
| 776 params.frame_page_state = content::PageState::CreateFromURL(params.frame_url); | |
| 777 params.media_type = blink::WebContextMenuData::kMediaTypePlugin; | |
| 778 TestRenderViewContextMenu menu(frame, params); | |
| 779 menu.Init(); | |
| 780 | |
| 781 // The full page related items such as 'reload' should be there. | |
| 782 ASSERT_TRUE(menu.IsItemPresent(IDC_RELOAD)); | |
| 783 } | |
| 784 | |
| 785 IN_PROC_BROWSER_TEST_F(PdfPluginContextMenuBrowserTest, | |
| 786 EmbeddedPdfHasNoPageItems) { | |
| 787 TestContextMenuOfPdfInsideWebPage(FILE_PATH_LITERAL("test-embed-pdf.html")); | |
| 788 } | |
| 789 | |
| 790 IN_PROC_BROWSER_TEST_F(PdfPluginContextMenuBrowserTest, | |
| 791 IframedPdfHasNoPageItems) { | |
| 792 TestContextMenuOfPdfInsideWebPage(FILE_PATH_LITERAL("test-iframe-pdf.html")); | |
| 793 } | |
| 794 | |
| 674 class LoadImageRequestInterceptor : public net::URLRequestInterceptor { | 795 class LoadImageRequestInterceptor : public net::URLRequestInterceptor { |
| 675 public: | 796 public: |
| 676 LoadImageRequestInterceptor() : num_requests_(0), | 797 LoadImageRequestInterceptor() : num_requests_(0), |
| 677 requests_to_wait_for_(-1), | 798 requests_to_wait_for_(-1), |
| 678 weak_factory_(this) { | 799 weak_factory_(this) { |
| 679 } | 800 } |
| 680 | 801 |
| 681 ~LoadImageRequestInterceptor() override {} | 802 ~LoadImageRequestInterceptor() override {} |
| 682 | 803 |
| 683 // net::URLRequestInterceptor implementation | 804 // net::URLRequestInterceptor implementation |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 791 IN_PROC_BROWSER_TEST_F(LoadImageBrowserTest, LoadImage) { | 912 IN_PROC_BROWSER_TEST_F(LoadImageBrowserTest, LoadImage) { |
| 792 static const char kValidImage[] = "/load_image/image.png"; | 913 static const char kValidImage[] = "/load_image/image.png"; |
| 793 SetupAndLoadImagePage(kValidImage); | 914 SetupAndLoadImagePage(kValidImage); |
| 794 AddLoadImageInterceptor(kValidImage); | 915 AddLoadImageInterceptor(kValidImage); |
| 795 AttemptLoadImage(); | 916 AttemptLoadImage(); |
| 796 interceptor_->WaitForRequests(1); | 917 interceptor_->WaitForRequests(1); |
| 797 EXPECT_EQ(1, interceptor_->num_requests()); | 918 EXPECT_EQ(1, interceptor_->num_requests()); |
| 798 } | 919 } |
| 799 | 920 |
| 800 } // namespace | 921 } // namespace |
| OLD | NEW |