Open hyperlinks in PDF in a new tab when middle mouse clicking.

BUG=71754
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2127383002
Cr-Commit-Position: refs/heads/master@{#405430}
diff --git a/chrome/browser/pdf/pdf_extension_test.cc b/chrome/browser/pdf/pdf_extension_test.cc
index 3e11df7..03e31b51b 100644
--- a/chrome/browser/pdf/pdf_extension_test.cc
+++ b/chrome/browser/pdf/pdf_extension_test.cc
@@ -39,6 +39,7 @@
 #include "content/public/browser/download_manager.h"
 #include "content/public/browser/notification_observer.h"
 #include "content/public/browser/notification_registrar.h"
+#include "content/public/browser/notification_service.h"
 #include "content/public/browser/plugin_service.h"
 #include "content/public/browser/render_process_host.h"
 #include "content/public/browser/render_view_host.h"
@@ -48,6 +49,7 @@
 #include "extensions/browser/extension_registry.h"
 #include "extensions/common/manifest_handlers/mime_types_handler.h"
 #include "extensions/test/result_catcher.h"
+#include "net/dns/mock_host_resolver.h"
 #include "net/test/embedded_test_server/embedded_test_server.h"
 #include "ui/base/resource/resource_bundle.h"
 #include "url/gurl.h"
@@ -585,3 +587,57 @@
       << "Expected:\n" << expected_ax_tree
       << "\n\nActual:\n" << ax_tree_dump;
 }
+
+IN_PROC_BROWSER_TEST_F(PDFExtensionTest, LinkMiddleClick) {
+  host_resolver()->AddRule("www.example.com", "127.0.0.1");
+  GURL test_pdf_url(embedded_test_server()->GetURL("/pdf/test-link.pdf"));
+  content::WebContents* guest_contents = LoadPdfGetGuestContents(test_pdf_url);
+  ASSERT_TRUE(guest_contents);
+
+  // The link position of the test-link.pdf in page coordinates is (110, 110).
+  // Convert the link position from page coordinates to screen coordinates.
+  ASSERT_TRUE(content::ExecuteScript(guest_contents,
+      "var visiblePage = viewer.viewport.getMostVisiblePage();"
+      "var visiblePageDimensions ="
+      "    viewer.viewport.getPageScreenRect(visiblePage);"
+      "var viewportPosition = viewer.viewport.position;"
+      "var screenOffsetX = visiblePageDimensions.x - viewportPosition.x;"
+      "var screenOffsetY = visiblePageDimensions.y - viewportPosition.y;"
+      "var linkScreenPositionX = Math.floor(110 + screenOffsetX);"
+      "var linkScreenPositionY = Math.floor(110 + screenOffsetY);"));
+
+  int x;
+  ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
+      guest_contents,
+      "window.domAutomationController.send(linkScreenPositionX);",
+      &x));
+
+  int y;
+  ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
+      guest_contents,
+      "window.domAutomationController.send(linkScreenPositionY);",
+      &y));
+
+  gfx::Point point(x, y);
+  content::WebContents* web_contents =
+      browser()->tab_strip_model()->GetActiveWebContents();
+
+  content::WindowedNotificationObserver observer(
+      chrome::NOTIFICATION_TAB_ADDED,
+      content::NotificationService::AllSources());
+  content::SimulateMouseClickAt(web_contents, 0,
+      blink::WebMouseEvent::ButtonMiddle, point);
+  observer.Wait();
+
+  int tab_count = browser()->tab_strip_model()->count();
+  ASSERT_EQ(2, tab_count);
+
+  // TODO(jaepark): Middle mouse clicking on a link should not change
+  // the focus of the tab. See http://crbug.com/628054.
+  content::WebContents* new_web_contents =
+      browser()->tab_strip_model()->GetActiveWebContents();
+  ASSERT_NE(web_contents, new_web_contents);
+
+  const GURL& url = new_web_contents->GetURL();
+  ASSERT_EQ(std::string("http://www.example.com/"), url.spec());
+}
diff --git a/chrome/test/data/pdf/test-link.pdf b/chrome/test/data/pdf/test-link.pdf
new file mode 100644
index 0000000..d9e8737
--- /dev/null
+++ b/chrome/test/data/pdf/test-link.pdf
Binary files differ
diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc
index 08e869de..d0796a5 100644
--- a/pdf/pdfium/pdfium_engine.cc
+++ b/pdf/pdfium/pdfium_engine.cc
@@ -1527,8 +1527,11 @@
     selection_.clear();
     return true;
   }
-  if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT)
+
+  if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT &&
+      event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) {
     return false;
+  }
 
   SelectionChangeInvalidator selection_invalidator(this);
   selection_.clear();
@@ -1546,6 +1549,10 @@
   if (area == PDFiumPage::WEBLINK_AREA)
     return true;
 
+  // Prevent middle mouse button from selecting texts.
+  if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE)
+    return false;
+
   if (area == PDFiumPage::DOCLINK_AREA) {
     client_->ScrollToPage(target.page);
     client_->FormTextFieldFocusChange(false);
@@ -1620,8 +1627,11 @@
 }
 
 bool PDFiumEngine::OnMouseUp(const pp::MouseInputEvent& event) {
-  if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT)
+
+  if (event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_LEFT &&
+      event.GetButton() != PP_INPUTEVENT_MOUSEBUTTON_MIDDLE) {
     return false;
+  }
 
   int page_index = -1;
   int char_index = -1;
@@ -1633,13 +1643,18 @@
   // Open link on mouse up for same link for which mouse down happened earlier.
   if (mouse_down_state_.Matches(area, target)) {
     if (area == PDFiumPage::WEBLINK_AREA) {
-      bool open_in_new_tab = !!(event.GetModifiers() & kDefaultKeyModifier);
+      bool open_in_new_tab = !!(event.GetModifiers() & kDefaultKeyModifier) ||
+          !!(event.GetModifiers() & PP_INPUTEVENT_MODIFIER_MIDDLEBUTTONDOWN);
       client_->NavigateTo(target.url, open_in_new_tab);
       client_->FormTextFieldFocusChange(false);
       return true;
     }
   }
 
+  // Prevent middle mouse button from selecting texts.
+  if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_MIDDLE)
+    return false;
+
   if (page_index != -1) {
     double page_x, page_y;
     pp::Point point = event.GetPosition();