Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / keyboard / keyboard_controller.cc
index 7326875..4fdf8f0 100644 (file)
@@ -4,38 +4,49 @@
 
 #include "ui/keyboard/keyboard_controller.h"
 
+#include <set>
+
 #include "base/bind.h"
 #include "base/command_line.h"
-#include "ui/aura/layout_manager.h"
+#include "content/public/browser/render_widget_host.h"
+#include "content/public/browser/render_widget_host_iterator.h"
+#include "content/public/browser/render_widget_host_view.h"
 #include "ui/aura/window.h"
 #include "ui/aura/window_delegate.h"
+#include "ui/aura/window_observer.h"
 #include "ui/base/cursor/cursor.h"
 #include "ui/base/hit_test.h"
 #include "ui/base/ime/input_method.h"
 #include "ui/base/ime/text_input_client.h"
-#include "ui/base/ime/text_input_type.h"
+#include "ui/compositor/layer_animation_observer.h"
+#include "ui/compositor/scoped_layer_animation_settings.h"
 #include "ui/gfx/path.h"
 #include "ui/gfx/rect.h"
 #include "ui/gfx/skia_util.h"
 #include "ui/keyboard/keyboard_controller_observer.h"
 #include "ui/keyboard/keyboard_controller_proxy.h"
-#include "ui/keyboard/keyboard_switches.h"
+#include "ui/keyboard/keyboard_layout_manager.h"
 #include "ui/keyboard/keyboard_util.h"
-#include "ui/wm/public/masked_window_targeter.h"
+#include "ui/wm/core/masked_window_targeter.h"
+
+#if defined(OS_CHROMEOS)
+#include "base/process/launch.h"
+#include "base/sys_info.h"
+#endif
 
 namespace {
 
 const int kHideKeyboardDelayMs = 100;
 
-gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) {
-  const float kKeyboardHeightRatio =
-      keyboard::IsKeyboardUsabilityExperimentEnabled() ? 1.0f : 0.3f;
-  return gfx::Rect(
-      window_bounds.x(),
-      window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio),
-      window_bounds.width(),
-      window_bounds.height() * kKeyboardHeightRatio);
-}
+// The virtual keyboard show/hide animation duration.
+const int kShowAnimationDurationMs = 350;
+const int kHideAnimationDurationMs = 100;
+
+// The opacity of virtual keyboard container when show animation starts or
+// hide animation finishes. This cannot be zero because we call Show() on the
+// keyboard window before setting the opacity back to 1.0. Since windows are not
+// allowed to be shown with zero opacity, we always animate to 0.01 instead.
+const float kAnimationStartOrAfterHideOpacity = 0.01f;
 
 // Event targeter for the keyboard container.
 class KeyboardContainerTargeter : public wm::MaskedWindowTargeter {
@@ -46,14 +57,15 @@ class KeyboardContainerTargeter : public wm::MaskedWindowTargeter {
         proxy_(proxy) {
   }
 
-  virtual ~KeyboardContainerTargeter() {}
+  ~KeyboardContainerTargeter() override {}
 
  private:
   // wm::MaskedWindowTargeter:
-  virtual bool GetHitTestMask(aura::Window* window,
-                              gfx::Path* mask) const OVERRIDE {
+  bool GetHitTestMask(aura::Window* window, gfx::Path* mask) const override {
+    if (proxy_ && !proxy_->HasKeyboardWindow())
+      return true;
     gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
-        KeyboardBoundsFromWindowBounds(window->bounds());
+        keyboard::DefaultKeyboardBoundsFromWindowBounds(window->bounds());
     mask->addRect(RectToSkRect(keyboard_bounds));
     return true;
   }
@@ -71,42 +83,44 @@ class KeyboardWindowDelegate : public aura::WindowDelegate {
  public:
   explicit KeyboardWindowDelegate(keyboard::KeyboardControllerProxy* proxy)
       : proxy_(proxy) {}
-  virtual ~KeyboardWindowDelegate() {}
+  ~KeyboardWindowDelegate() override {}
 
  private:
   // Overridden from aura::WindowDelegate:
-  virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); }
-  virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); }
-  virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
-                               const gfx::Rect& new_bounds) OVERRIDE {
+  gfx::Size GetMinimumSize() const override { return gfx::Size(); }
+  gfx::Size GetMaximumSize() const override { return gfx::Size(); }
+  void OnBoundsChanged(const gfx::Rect& old_bounds,
+                       const gfx::Rect& new_bounds) override {
     bounds_ = new_bounds;
   }
-  virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
+  gfx::NativeCursor GetCursor(const gfx::Point& point) override {
     return gfx::kNullCursor;
   }
-  virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
+  int GetNonClientComponent(const gfx::Point& point) const override {
     return HTNOWHERE;
   }
-  virtual bool ShouldDescendIntoChildForEventHandling(
+  bool ShouldDescendIntoChildForEventHandling(
       aura::Window* child,
-      const gfx::Point& location) OVERRIDE {
+      const gfx::Point& location) override {
     return true;
   }
-  virtual bool CanFocus() OVERRIDE { return false; }
-  virtual void OnCaptureLost() OVERRIDE {}
-  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {}
-  virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
-  virtual void OnWindowDestroying() OVERRIDE {}
-  virtual void OnWindowDestroyed() OVERRIDE { delete this; }
-  virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
-  virtual bool HasHitTestMask() const OVERRIDE { return true; }
-  virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {
+  bool CanFocus() override { return false; }
+  void OnCaptureLost() override {}
+  void OnPaint(gfx::Canvas* canvas) override {}
+  void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
+  void OnWindowDestroying(aura::Window* window) override {}
+  void OnWindowDestroyed(aura::Window* window) override { delete this; }
+  void OnWindowTargetVisibilityChanged(bool visible) override {}
+  bool HasHitTestMask() const override {
+    return !proxy_ || proxy_->HasKeyboardWindow();
+  }
+  void GetHitTestMask(gfx::Path* mask) const override {
+    if (proxy_ && !proxy_->HasKeyboardWindow())
+      return;
     gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
-        KeyboardBoundsFromWindowBounds(bounds_);
+        keyboard::DefaultKeyboardBoundsFromWindowBounds(bounds_);
     mask->addRect(RectToSkRect(keyboard_bounds));
   }
-  virtual void DidRecreateLayer(ui::Layer* old_layer,
-                                ui::Layer* new_layer) OVERRIDE {}
 
   gfx::Rect bounds_;
   keyboard::KeyboardControllerProxy* proxy_;
@@ -114,67 +128,136 @@ class KeyboardWindowDelegate : public aura::WindowDelegate {
   DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate);
 };
 
+void ToggleTouchEventLogging(bool enable) {
+#if defined(OS_CHROMEOS)
+  if (!base::SysInfo::IsRunningOnChromeOS())
+    return;
+  CommandLine command(
+      base::FilePath("/opt/google/touchscreen/toggle_touch_event_logging"));
+  if (enable)
+    command.AppendArg("1");
+  else
+    command.AppendArg("0");
+  VLOG(1) << "Running " << command.GetCommandLineString();
+  base::LaunchOptions options;
+  options.wait = true;
+  base::LaunchProcess(command, options, NULL);
+#endif
+}
+
+aura::Window *GetFrameWindow(aura::Window *window) {
+  // Each container window has a non-negative id.  Stop traversing at the child
+  // of a container window.
+  if (!window)
+    return NULL;
+  while (window->parent() && window->parent()->id() < 0) {
+    window = window->parent();
+  }
+  return window;
+}
+
 }  // namespace
 
 namespace keyboard {
 
-// LayoutManager for the virtual keyboard container.  Manages a single window
-// (the virtual keyboard) and keeps it positioned at the bottom of the
-// owner window.
-class KeyboardLayoutManager : public aura::LayoutManager {
+// Observer for both keyboard show and hide animations. It should be owned by
+// KeyboardController.
+class CallbackAnimationObserver : public ui::LayerAnimationObserver {
  public:
-  explicit KeyboardLayoutManager(KeyboardController* controller)
-      : controller_(controller), keyboard_(NULL) {
-  }
+  CallbackAnimationObserver(ui::LayerAnimator* animator,
+                            base::Callback<void(void)> callback);
+  ~CallbackAnimationObserver() override;
 
-  // Overridden from aura::LayoutManager
-  virtual void OnWindowResized() OVERRIDE {
-    if (keyboard_ && !controller_->proxy()->resizing_from_contents())
-      ResizeKeyboardToDefault(keyboard_);
-  }
-  virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
-    DCHECK(!keyboard_);
-    keyboard_ = child;
-    ResizeKeyboardToDefault(keyboard_);
-  }
-  virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
-  virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
-  virtual void OnChildWindowVisibilityChanged(aura::Window* child,
-                                              bool visible) OVERRIDE {}
-  virtual void SetChildBounds(aura::Window* child,
-                              const gfx::Rect& requested_bounds) OVERRIDE {
-    // SetChildBounds can be invoked by resizing from the container or by
-    // resizing from the contents (through window.resizeTo call in JS).
-    // The flag resizing_from_contents() is used to determine the keyboard is
-    // resizing from which.
-    if (controller_->proxy()->resizing_from_contents()) {
-      controller_->NotifyKeyboardBoundsChanging(requested_bounds);
-      SetChildBoundsDirect(child, requested_bounds);
-    }
-  }
+ private:
+  // Overridden from ui::LayerAnimationObserver:
+  void OnLayerAnimationEnded(ui::LayerAnimationSequence* seq) override;
+  void OnLayerAnimationAborted(ui::LayerAnimationSequence* seq) override;
+  void OnLayerAnimationScheduled(ui::LayerAnimationSequence* seq) override {}
+
+  ui::LayerAnimator* animator_;
+  base::Callback<void(void)> callback_;
+
+  DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver);
+};
+
+CallbackAnimationObserver::CallbackAnimationObserver(
+    ui::LayerAnimator* animator, base::Callback<void(void)> callback)
+    : animator_(animator), callback_(callback) {
+}
+
+CallbackAnimationObserver::~CallbackAnimationObserver() {
+  animator_->RemoveObserver(this);
+}
+
+void CallbackAnimationObserver::OnLayerAnimationEnded(
+    ui::LayerAnimationSequence* seq) {
+  if (animator_->is_animating())
+    return;
+  animator_->RemoveObserver(this);
+  callback_.Run();
+}
+
+void CallbackAnimationObserver::OnLayerAnimationAborted(
+    ui::LayerAnimationSequence* seq) {
+  animator_->RemoveObserver(this);
+}
+
+class WindowBoundsChangeObserver : public aura::WindowObserver {
+ public:
+  void OnWindowBoundsChanged(aura::Window* window,
+                             const gfx::Rect& old_bounds,
+                             const gfx::Rect& new_bounds) override;
+  void OnWindowDestroyed(aura::Window* window) override;
+
+  void AddObservedWindow(aura::Window* window);
+  void RemoveAllObservedWindows();
 
  private:
-  void ResizeKeyboardToDefault(aura::Window* child) {
-    gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(
-        controller_->GetContainerWindow()->bounds());
-    SetChildBoundsDirect(child, keyboard_bounds);
+  std::set<aura::Window*> observed_windows_;
+};
+
+void WindowBoundsChangeObserver::OnWindowBoundsChanged(aura::Window* window,
+    const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
+  KeyboardController* controller =  KeyboardController::GetInstance();
+  if (controller)
+    controller->UpdateWindowInsets(window);
+}
+
+void WindowBoundsChangeObserver::OnWindowDestroyed(aura::Window* window) {
+  if (window->HasObserver(this))
+    window->RemoveObserver(this);
+  observed_windows_.erase(window);
+}
+
+void WindowBoundsChangeObserver::AddObservedWindow(aura::Window* window) {
+  if (!window->HasObserver(this)) {
+    window->AddObserver(this);
+    observed_windows_.insert(window);
   }
+}
 
-  KeyboardController* controller_;
-  aura::Window* keyboard_;
+void WindowBoundsChangeObserver::RemoveAllObservedWindows() {
+  for (std::set<aura::Window*>::iterator it = observed_windows_.begin();
+       it != observed_windows_.end(); ++it)
+    (*it)->RemoveObserver(this);
+  observed_windows_.clear();
+}
 
-  DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
-};
+// static
+KeyboardController* KeyboardController::instance_ = NULL;
 
 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
     : proxy_(proxy),
       input_method_(NULL),
       keyboard_visible_(false),
+      show_on_resize_(false),
       lock_keyboard_(false),
+      type_(ui::TEXT_INPUT_TYPE_NONE),
       weak_factory_(this) {
   CHECK(proxy);
   input_method_ = proxy_->GetInputMethod();
   input_method_->AddObserver(this);
+  window_bounds_observer_.reset(new WindowBoundsChangeObserver());
 }
 
 KeyboardController::~KeyboardController() {
@@ -182,13 +265,26 @@ KeyboardController::~KeyboardController() {
     container_->RemoveObserver(this);
   if (input_method_)
     input_method_->RemoveObserver(this);
+  ResetWindowInsets();
+}
+
+// static
+void KeyboardController::ResetInstance(KeyboardController* controller) {
+  if (instance_ && instance_ != controller)
+    delete instance_;
+  instance_ = controller;
+}
+
+// static
+KeyboardController* KeyboardController::GetInstance() {
+  return instance_;
 }
 
 aura::Window* KeyboardController::GetContainerWindow() {
   if (!container_.get()) {
     container_.reset(new aura::Window(
         new KeyboardWindowDelegate(proxy_.get())));
-    container_->set_event_targeter(scoped_ptr<ui::EventTargeter>(
+    container_->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
         new KeyboardContainerTargeter(container_.get(), proxy_.get())));
     container_->SetName("KeyboardContainer");
     container_->set_owned_by_parent(false);
@@ -201,15 +297,54 @@ aura::Window* KeyboardController::GetContainerWindow() {
 
 void KeyboardController::NotifyKeyboardBoundsChanging(
     const gfx::Rect& new_bounds) {
-  if (proxy_->GetKeyboardWindow()->IsVisible()) {
+  current_keyboard_bounds_ = new_bounds;
+  if (proxy_->HasKeyboardWindow() && proxy_->GetKeyboardWindow()->IsVisible()) {
     FOR_EACH_OBSERVER(KeyboardControllerObserver,
                       observer_list_,
                       OnKeyboardBoundsChanging(new_bounds));
+    if (keyboard::IsKeyboardOverscrollEnabled()) {
+      // Adjust the height of the viewport for visible windows on the primary
+      // display.
+      // TODO(kevers): Add EnvObserver to properly initialize insets if a
+      // window is created while the keyboard is visible.
+      scoped_ptr<content::RenderWidgetHostIterator> widgets(
+          content::RenderWidgetHost::GetRenderWidgetHosts());
+      aura::Window *keyboard_window = proxy_->GetKeyboardWindow();
+      aura::Window *root_window = keyboard_window->GetRootWindow();
+      while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
+        content::RenderWidgetHostView* view = widget->GetView();
+        // Can be NULL, e.g. if the RenderWidget is being destroyed or
+        // the render process crashed.
+        if (view) {
+          aura::Window *window = view->GetNativeView();
+          // If virtual keyboard failed to load, a widget that displays error
+          // message will be created and adds as a child of the virtual keyboard
+          // window. We want to avoid add BoundsChangedObserver to that window.
+          if (GetFrameWindow(window) != keyboard_window &&
+              window->GetRootWindow() == root_window) {
+            gfx::Rect window_bounds = window->GetBoundsInScreen();
+            gfx::Rect intersect = gfx::IntersectRects(window_bounds,
+                                                      new_bounds);
+            int overlap = intersect.height();
+            if (overlap > 0 && overlap < window_bounds.height())
+              view->SetInsets(gfx::Insets(0, 0, overlap, 0));
+            else
+              view->SetInsets(gfx::Insets());
+            AddBoundsChangedObserver(window);
+          }
+        }
+      }
+    } else {
+      ResetWindowInsets();
+    }
+  } else {
+    current_keyboard_bounds_ = gfx::Rect();
   }
 }
 
 void KeyboardController::HideKeyboard(HideReason reason) {
   keyboard_visible_ = false;
+  ToggleTouchEventLogging(true);
 
   keyboard::LogKeyboardControlEvent(
       reason == HIDE_REASON_AUTOMATIC ?
@@ -218,7 +353,23 @@ void KeyboardController::HideKeyboard(HideReason reason) {
 
   NotifyKeyboardBoundsChanging(gfx::Rect());
 
-  proxy_->HideKeyboardContainer(container_.get());
+  set_lock_keyboard(false);
+
+  ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
+  animation_observer_.reset(new CallbackAnimationObserver(
+      container_animator,
+      base::Bind(&KeyboardController::HideAnimationFinished,
+                 base::Unretained(this))));
+  container_animator->AddObserver(animation_observer_.get());
+
+  ui::ScopedLayerAnimationSettings settings(container_animator);
+  settings.SetTweenType(gfx::Tween::FAST_OUT_LINEAR_IN);
+  settings.SetTransitionDuration(
+      base::TimeDelta::FromMilliseconds(kHideAnimationDurationMs));
+  gfx::Transform transform;
+  transform.Translate(0, kAnimationDistance);
+  container_->SetTransform(transform);
+  container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
 }
 
 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
@@ -229,14 +380,24 @@ void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
   observer_list_.RemoveObserver(observer);
 }
 
+void KeyboardController::ShowKeyboard(bool lock) {
+  set_lock_keyboard(lock);
+  ShowKeyboardInternal();
+}
+
 void KeyboardController::OnWindowHierarchyChanged(
     const HierarchyChangeParams& params) {
   if (params.new_parent && params.target == container_.get())
     OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient());
 }
 
-void KeyboardController::SetOverrideContentUrl(const GURL& url) {
-  proxy_->SetOverrideContentUrl(url);
+void KeyboardController::Reload() {
+  if (proxy_->HasKeyboardWindow()) {
+    // A reload should never try to show virtual keyboard. If keyboard is not
+    // visible before reload, it should keep invisible after reload.
+    show_on_resize_ = false;
+    proxy_->ReloadKeyboardIfNeeded();
+  }
 }
 
 void KeyboardController::OnTextInputStateChanged(
@@ -244,14 +405,9 @@ void KeyboardController::OnTextInputStateChanged(
   if (!container_.get())
     return;
 
-  if (IsKeyboardUsabilityExperimentEnabled()) {
-    OnShowImeIfNeeded();
-    return;
-  }
+  type_ = client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
 
-  ui::TextInputType type =
-      client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
-  if (type == ui::TEXT_INPUT_TYPE_NONE && !lock_keyboard_) {
+  if (type_ == ui::TEXT_INPUT_TYPE_NONE && !lock_keyboard_) {
     if (keyboard_visible_) {
       // Set the visibility state here so that any queries for visibility
       // before the timer fires returns the correct future value.
@@ -268,16 +424,13 @@ void KeyboardController::OnTextInputStateChanged(
       weak_factory_.InvalidateWeakPtrs();
       keyboard_visible_ = true;
     }
-    proxy_->SetUpdateInputType(type);
+    proxy_->SetUpdateInputType(type_);
     // Do not explicitly show the Virtual keyboard unless it is in the process
     // of hiding. Instead, the virtual keyboard is shown in response to a user
     // gesture (mouse or touch) that is received while an element has input
     // focus. Showing the keyboard requires an explicit call to
     // OnShowImeIfNeeded.
   }
-  // TODO(bryeung): whenever the TextInputClient changes we need to notify the
-  // keyboard (with the TextInputType) so that it can reset it's state (e.g.
-  // abandon compositions in progress)
 }
 
 void KeyboardController::OnInputMethodDestroyed(
@@ -287,6 +440,41 @@ void KeyboardController::OnInputMethodDestroyed(
 }
 
 void KeyboardController::OnShowImeIfNeeded() {
+  ShowKeyboardInternal();
+}
+
+bool KeyboardController::ShouldEnableInsets(aura::Window* window) {
+  aura::Window *keyboard_window = proxy_->GetKeyboardWindow();
+  return (keyboard_window->GetRootWindow() == window->GetRootWindow() &&
+          keyboard::IsKeyboardOverscrollEnabled() &&
+          proxy_->GetKeyboardWindow()->IsVisible() &&
+          keyboard_visible_);
+}
+
+void KeyboardController::UpdateWindowInsets(aura::Window* window) {
+  aura::Window *keyboard_window = proxy_->GetKeyboardWindow();
+  if (window == keyboard_window)
+    return;
+
+  scoped_ptr<content::RenderWidgetHostIterator> widgets(
+      content::RenderWidgetHost::GetRenderWidgetHosts());
+  while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
+    content::RenderWidgetHostView* view = widget->GetView();
+    if (view && window->Contains(view->GetNativeView())) {
+      gfx::Rect window_bounds = view->GetNativeView()->GetBoundsInScreen();
+      gfx::Rect intersect = gfx::IntersectRects(window_bounds,
+          proxy_->GetKeyboardWindow()->bounds());
+      int overlap = ShouldEnableInsets(window) ? intersect.height() : 0;
+      if (overlap > 0 && overlap < window_bounds.height())
+        view->SetInsets(gfx::Insets(0, 0, overlap, 0));
+      else
+        view->SetInsets(gfx::Insets());
+      return;
+    }
+  }
+}
+
+void KeyboardController::ShowKeyboardInternal() {
   if (!container_.get())
     return;
 
@@ -297,8 +485,15 @@ void KeyboardController::OnShowImeIfNeeded() {
     container_->AddChild(keyboard);
     keyboard->set_owned_by_parent(false);
   }
-  if (keyboard_visible_)
+
+  proxy_->ReloadKeyboardIfNeeded();
+
+  if (keyboard_visible_) {
+    return;
+  } else if (proxy_->GetKeyboardWindow()->bounds().height() == 0) {
+    show_on_resize_ = true;
     return;
+  }
 
   keyboard_visible_ = true;
 
@@ -314,17 +509,76 @@ void KeyboardController::OnShowImeIfNeeded() {
   // point, it may in the process of hiding. We still need to show keyboard
   // container in this case.
   if (container_->IsVisible() &&
-      !container_->layer()->GetAnimator()->is_animating()) {
+      !container_->layer()->GetAnimator()->is_animating())
     return;
+
+  ToggleTouchEventLogging(false);
+  ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
+
+  // If the container is not animating, makes sure the position and opacity
+  // are at begin states for animation.
+  if (!container_animator->is_animating()) {
+    gfx::Transform transform;
+    transform.Translate(0, kAnimationDistance);
+    container_->SetTransform(transform);
+    container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
   }
 
-  NotifyKeyboardBoundsChanging(container_->children()[0]->bounds());
+  container_animator->set_preemption_strategy(
+      ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
+  animation_observer_.reset(new CallbackAnimationObserver(
+      container_animator,
+      base::Bind(&KeyboardController::ShowAnimationFinished,
+                 base::Unretained(this))));
+  container_animator->AddObserver(animation_observer_.get());
 
   proxy_->ShowKeyboardContainer(container_.get());
+
+  {
+    // Scope the following animation settings as we don't want to animate
+    // visibility change that triggered by a call to the base class function
+    // ShowKeyboardContainer with these settings. The container should become
+    // visible immediately.
+    ui::ScopedLayerAnimationSettings settings(container_animator);
+    settings.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
+    settings.SetTransitionDuration(
+        base::TimeDelta::FromMilliseconds(kShowAnimationDurationMs));
+    container_->SetTransform(gfx::Transform());
+    container_->layer()->SetOpacity(1.0);
+  }
+}
+
+void KeyboardController::ResetWindowInsets() {
+  const gfx::Insets insets;
+  scoped_ptr<content::RenderWidgetHostIterator> widgets(
+      content::RenderWidgetHost::GetRenderWidgetHosts());
+  while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
+    content::RenderWidgetHostView* view = widget->GetView();
+    if (view)
+      view->SetInsets(insets);
+  }
+  window_bounds_observer_->RemoveAllObservedWindows();
 }
 
 bool KeyboardController::WillHideKeyboard() const {
   return weak_factory_.HasWeakPtrs();
 }
 
+void KeyboardController::ShowAnimationFinished() {
+  // Notify observers after animation finished to prevent reveal desktop
+  // background during animation.
+  NotifyKeyboardBoundsChanging(proxy_->GetKeyboardWindow()->bounds());
+  proxy_->EnsureCaretInWorkArea();
+}
+
+void KeyboardController::HideAnimationFinished() {
+  proxy_->HideKeyboardContainer(container_.get());
+}
+
+void KeyboardController::AddBoundsChangedObserver(aura::Window* window) {
+  aura::Window* target_window = GetFrameWindow(window);
+  if (target_window)
+    window_bounds_observer_->AddObservedWindow(target_window);
+}
+
 }  // namespace keyboard