Initial patch set to implement improved touch support for screen
magnification

BUG=670526
TEST=none

Review-Url: https://codereview.chromium.org/2641733002
Cr-Commit-Position: refs/heads/master@{#450259}
diff --git a/ash/magnifier/magnification_controller.cc b/ash/magnifier/magnification_controller.cc
index c677caae5..9a32419 100644
--- a/ash/magnifier/magnification_controller.cc
+++ b/ash/magnifier/magnification_controller.cc
@@ -20,6 +20,7 @@
 #include "base/command_line.h"
 #include "base/synchronization/waitable_event.h"
 #include "base/timer/timer.h"
+#include "chromeos/chromeos_switches.h"
 #include "ui/aura/client/cursor_client.h"
 #include "ui/aura/window.h"
 #include "ui/aura/window_tree_host.h"
@@ -34,6 +35,7 @@
 #include "ui/display/screen.h"
 #include "ui/events/event.h"
 #include "ui/events/event_handler.h"
+#include "ui/events/gesture_event_details.h"
 #include "ui/gfx/geometry/point3_f.h"
 #include "ui/gfx/geometry/point_conversions.h"
 #include "ui/gfx/geometry/point_f.h"
@@ -191,6 +193,7 @@
   void OnMouseEvent(ui::MouseEvent* event) override;
   void OnScrollEvent(ui::ScrollEvent* event) override;
   void OnTouchEvent(ui::TouchEvent* event) override;
+  void OnGestureEvent(ui::GestureEvent* event) override;
 
   // Moves the view port when |point| is located within
   // |x_panning_margin| and |y_pannin_margin| to the edge of the visible
@@ -690,6 +693,33 @@
   }
 }
 
+void MagnificationControllerImpl::OnGestureEvent(ui::GestureEvent* event) {
+  if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
+          chromeos::switches::kEnableTouchSupportForScreenMagnifier)) {
+    return;
+  }
+
+  const ui::GestureEventDetails& details = event->details();
+  if (details.type() == ui::ET_GESTURE_SCROLL_UPDATE &&
+      details.touch_points() == 2) {
+    gfx::Rect viewport_rect_in_dip = GetViewportRect();
+    viewport_rect_in_dip.Offset(-details.scroll_x(), -details.scroll_y());
+    gfx::Rect viewport_rect_in_pixel =
+        ui::ConvertRectToPixel(root_window_->layer(), viewport_rect_in_dip);
+    MoveWindow(viewport_rect_in_pixel.origin(), false);
+    event->SetHandled();
+  } else if (details.type() == ui::ET_GESTURE_PINCH_UPDATE &&
+             details.touch_points() == 3) {
+    float scale = GetScale() * details.scale();
+    scale = std::max(scale, kMinMagnifiedScaleThreshold);
+    scale = std::min(scale, kMaxMagnifiedScaleThreshold);
+
+    point_of_interest_ = event->root_location();
+    SetScale(scale, false);
+    event->SetHandled();
+  }
+}
+
 void MagnificationControllerImpl::MoveMagnifierWindowFollowPoint(
     const gfx::Point& point,
     int x_panning_margin,
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd
index b374e173..ce15d8ea 100644
--- a/chrome/app/generated_resources.grd
+++ b/chrome/app/generated_resources.grd
@@ -14256,6 +14256,12 @@
       <message name="IDS_FLAGS_ENABLE_ANDROID_WALLPAPERS_APP_DESCRIPTION" desc="Description of the Android Wallpapers App flag.">
         Enables the Android Wallpapers App as the default Wallpaper App on Chrome OS.
       </message>
+      <message name="IDS_FLAGS_ENABLE_TOUCH_SUPPORT_FOR_SCREEN_MAGNIFIER_NAME" desc="Name of the touch support for screen magnifier flag.">
+        Touch support for screen magnifier
+      </message>
+      <message name="IDS_FLAGS_ENABLE_TOUCH_SUPPORT_FOR_SCREEN_MAGNIFIER_DESCRIPTION" desc="Description of the touch support for screen magnifier flag.">
+        Enables touch support for screen magnifier
+      </message>
     </if>
 
     <if expr="is_android">
diff --git a/chrome/browser/about_flags.cc b/chrome/browser/about_flags.cc
index c5d84898..855e4aeb 100644
--- a/chrome/browser/about_flags.cc
+++ b/chrome/browser/about_flags.cc
@@ -2236,6 +2236,14 @@
      FEATURE_VALUE_TYPE(device::kNewUsbBackend)},
 #endif  // defined(OS_WIN)
 
+#if defined(OS_CHROMEOS)
+    {"enable-touch-support-for-screen-magnifier",
+     IDS_FLAGS_ENABLE_TOUCH_SUPPORT_FOR_SCREEN_MAGNIFIER_NAME,
+     IDS_FLAGS_ENABLE_TOUCH_SUPPORT_FOR_SCREEN_MAGNIFIER_DESCRIPTION, kOsCrOS,
+     SINGLE_VALUE_TYPE(
+         chromeos::switches::kEnableTouchSupportForScreenMagnifier)},
+#endif  // OS_CHROMEOS
+
     // NOTE: Adding new command-line switches requires adding corresponding
     // entries to enum "LoginCustomFlags" in histograms.xml. See note in
     // histograms.xml and don't forget to run AboutFlagsHistogramTest unit test.
diff --git a/chromeos/chromeos_switches.cc b/chromeos/chromeos_switches.cc
index 0b264cb..c60ef0ad 100644
--- a/chromeos/chromeos_switches.cc
+++ b/chromeos/chromeos_switches.cc
@@ -288,6 +288,10 @@
 const char kEnableTouchpadThreeFingerClick[] =
     "enable-touchpad-three-finger-click";
 
+// Enables touch support for screen magnifier.
+const char kEnableTouchSupportForScreenMagnifier[] =
+    "enable-touch-support-for-screen-magnifier";
+
 // Enables the chromecast support for video player app.
 const char kEnableVideoPlayerChromecastSupport[] =
     "enable-video-player-chromecast-support";
diff --git a/chromeos/chromeos_switches.h b/chromeos/chromeos_switches.h
index 8b67205..5e7e9fac 100644
--- a/chromeos/chromeos_switches.h
+++ b/chromeos/chromeos_switches.h
@@ -93,6 +93,7 @@
 CHROMEOS_EXPORT extern const char kEnableScreenshotTestingWithMode[];
 CHROMEOS_EXPORT extern const char kEnableTouchCalibrationSetting[];
 CHROMEOS_EXPORT extern const char kEnableTouchpadThreeFingerClick[];
+CHROMEOS_EXPORT extern const char kEnableTouchSupportForScreenMagnifier[];
 CHROMEOS_EXPORT extern const char kEnableVideoPlayerChromecastSupport[];
 CHROMEOS_EXPORT extern const char kEnterpriseDisableArc[];
 CHROMEOS_EXPORT extern const char kEnterpriseEnableForcedReEnrollment[];
diff --git a/tools/metrics/histograms/histograms.xml b/tools/metrics/histograms/histograms.xml
index 8d6ded6..6f7f410 100644
--- a/tools/metrics/histograms/histograms.xml
+++ b/tools/metrics/histograms/histograms.xml
@@ -97618,6 +97618,7 @@
   <int value="1996125159" label="AutoplayMutedVideos:enabled"/>
   <int value="1997047666" label="NTPSnippetsIncreasedVisibility:disabled"/>
   <int value="2000091128" label="enable-touch-hover"/>
+  <int value="2003811018" label="enable-touch-support-for-screen-magnifier"/>
   <int value="2004483175" label="multi-instance-merge-tabs"/>
   <int value="2004829262" label="enable-webgl-draft-extensions"/>
   <int value="2005614493" label="tab-management-experiment-type-dill"/>