chrome/android: Update toolbar drawing in native.

Add a ToolbarResource to push relevant info for drawing the toolbar to
native. This replaces the previous use of NinePatchResource for this.
And some cleanup in ToolbarLayer.

BUG=700454

Review-Url: https://codereview.chromium.org/2752693003
Cr-Commit-Position: refs/heads/master@{#458831}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/compositor/resources/ResourceFactory.java b/chrome/android/java/src/org/chromium/chrome/browser/compositor/resources/ResourceFactory.java
new file mode 100644
index 0000000..06237d7
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/compositor/resources/ResourceFactory.java
@@ -0,0 +1,27 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.compositor.resources;
+
+import android.graphics.Rect;
+
+import org.chromium.base.annotations.JNINamespace;
+
+/**
+ * Utility class for creating native resources.
+ */
+@JNINamespace("android")
+public class ResourceFactory {
+    public static long createToolbarContainerResource(
+            Rect toolbarPosition, Rect locationBarPosition, int shadowHeight) {
+        return nativeCreateToolbarContainerResource(toolbarPosition.left, toolbarPosition.top,
+                toolbarPosition.right, toolbarPosition.bottom, locationBarPosition.left,
+                locationBarPosition.top, locationBarPosition.right, locationBarPosition.bottom,
+                shadowHeight);
+    }
+
+    private static native long nativeCreateToolbarContainerResource(int toolbarLeft, int toolbarTop,
+            int toolbarRight, int toolbarBottom, int locationBarLeft, int locationBarTop,
+            int locationBarRight, int locationBarBottom, int shadowHeight);
+}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/toolbar/ToolbarControlContainer.java b/chrome/android/java/src/org/chromium/chrome/browser/toolbar/ToolbarControlContainer.java
index fa95604..8d61ca79 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/toolbar/ToolbarControlContainer.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/toolbar/ToolbarControlContainer.java
@@ -17,6 +17,7 @@
 
 import org.chromium.chrome.R;
 import org.chromium.chrome.browser.compositor.layouts.eventfilter.EdgeSwipeHandler;
+import org.chromium.chrome.browser.compositor.resources.ResourceFactory;
 import org.chromium.chrome.browser.contextualsearch.SwipeRecognizer;
 import org.chromium.chrome.browser.util.ViewUtils;
 import org.chromium.chrome.browser.widget.ClipDrawableProgressBar.DrawingInfo;
@@ -160,6 +161,8 @@
 
     private static class ToolbarViewResourceAdapter extends ViewResourceAdapter {
         private final int[] mTempPosition = new int[2];
+        private final Rect mLocationBarRect = new Rect();
+        private final Rect mToolbarRect = new Rect();
         private final View mToolbarContainer;
 
         private Toolbar mToolbar;
@@ -220,16 +223,18 @@
         }
 
         @Override
-        protected void computeContentPadding(Rect outContentPadding) {
-            outContentPadding.set(
-                    0, mTabStripHeightPx, mToolbarContainer.getWidth(), mToolbar.getHeight());
-        }
-
-        @Override
-        protected void computeContentAperture(Rect outContentAperture) {
-            mToolbar.getLocationBarContentRect(outContentAperture);
+        public long createNativeResource() {
             mToolbar.getPositionRelativeToContainer(mToolbarContainer, mTempPosition);
-            outContentAperture.offset(mTempPosition[0], mTempPosition[1]);
+            mToolbarRect.set(mTempPosition[0], mTempPosition[1], mToolbarContainer.getWidth(),
+                    mTempPosition[1] + mToolbar.getHeight());
+
+            mToolbar.getLocationBarContentRect(mLocationBarRect);
+            mLocationBarRect.offset(mTempPosition[0], mTempPosition[1]);
+
+            int shadowHeight =
+                    mToolbarContainer.getHeight() - mToolbar.getHeight() - mTabStripHeightPx;
+            return ResourceFactory.createToolbarContainerResource(
+                    mToolbarRect, mLocationBarRect, shadowHeight);
         }
     }
 
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappControlContainer.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappControlContainer.java
index abbe8098..f794a04 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappControlContainer.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappControlContainer.java
@@ -6,10 +6,12 @@
 
 import android.content.Context;
 import android.graphics.Color;
+import android.graphics.Rect;
 import android.util.AttributeSet;
 import android.view.View;
 
 import org.chromium.chrome.browser.compositor.layouts.eventfilter.EdgeSwipeHandler;
+import org.chromium.chrome.browser.compositor.resources.ResourceFactory;
 import org.chromium.chrome.browser.widget.ClipDrawableProgressBar.DrawingInfo;
 import org.chromium.chrome.browser.widget.ControlContainer;
 import org.chromium.chrome.browser.widget.ViewResourceFrameLayout;
@@ -20,6 +22,22 @@
  */
 public class WebappControlContainer extends ViewResourceFrameLayout
         implements ControlContainer {
+    private class WebAppViewResourceAdapter extends ViewResourceAdapter {
+        private final Rect mToolbarRect = new Rect();
+        private final Rect mLocationBarContentRect = new Rect();
+
+        public WebAppViewResourceAdapter(View view) {
+            super(view);
+        }
+
+        @Override
+        public long createNativeResource() {
+            mToolbarRect.set(0, 0, getWidth(), getHeight());
+            mLocationBarContentRect.set(0, 0, getWidth(), getHeight());
+            return ResourceFactory.createToolbarContainerResource(
+                    mToolbarRect, mLocationBarContentRect, 0);
+        }
+    }
 
     /** Constructor for inflating from XML. */
     public WebappControlContainer(Context context, AttributeSet attrs) {
@@ -31,6 +49,11 @@
     }
 
     @Override
+    protected final ViewResourceAdapter createResourceAdapter() {
+        return new WebAppViewResourceAdapter(this);
+    }
+
+    @Override
     public ViewResourceAdapter getToolbarResourceAdapter() {
         return getResourceAdapter();
     }
diff --git a/chrome/android/java_sources.gni b/chrome/android/java_sources.gni
index 53ea8bba..1f04a5a6 100644
--- a/chrome/android/java_sources.gni
+++ b/chrome/android/java_sources.gni
@@ -210,6 +210,7 @@
   "java/src/org/chromium/chrome/browser/compositor/overlays/strip/StripLayoutTab.java",
   "java/src/org/chromium/chrome/browser/compositor/overlays/strip/StripStacker.java",
   "java/src/org/chromium/chrome/browser/compositor/overlays/strip/TabLoadTracker.java",
+  "java/src/org/chromium/chrome/browser/compositor/resources/ResourceFactory.java",
   "java/src/org/chromium/chrome/browser/compositor/resources/StaticResourcePreloads.java",
   "java/src/org/chromium/chrome/browser/compositor/scene_layer/ContextualSearchSceneLayer.java",
   "java/src/org/chromium/chrome/browser/compositor/scene_layer/ReaderModeSceneLayer.java",
diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn
index 637c004..a22a313 100644
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -2625,6 +2625,10 @@
       "android/compositor/layer/toolbar_layer.h",
       "android/compositor/layer_title_cache.cc",
       "android/compositor/layer_title_cache.h",
+      "android/compositor/resources/resource_factory.cc",
+      "android/compositor/resources/resource_factory.h",
+      "android/compositor/resources/toolbar_resource.cc",
+      "android/compositor/resources/toolbar_resource.h",
       "android/compositor/scene_layer/contextual_search_scene_layer.cc",
       "android/compositor/scene_layer/contextual_search_scene_layer.h",
       "android/compositor/scene_layer/reader_mode_scene_layer.cc",
@@ -3988,6 +3992,7 @@
       "../android/java/src/org/chromium/chrome/browser/compositor/LayerTitleCache.java",
       "../android/java/src/org/chromium/chrome/browser/compositor/bottombar/OverlayPanelContent.java",
       "../android/java/src/org/chromium/chrome/browser/compositor/layouts/content/TabContentManager.java",
+      "../android/java/src/org/chromium/chrome/browser/compositor/resources/ResourceFactory.java",
       "../android/java/src/org/chromium/chrome/browser/compositor/scene_layer/ContextualSearchSceneLayer.java",
       "../android/java/src/org/chromium/chrome/browser/compositor/scene_layer/ReaderModeSceneLayer.java",
       "../android/java/src/org/chromium/chrome/browser/compositor/scene_layer/SceneLayer.java",
diff --git a/chrome/browser/android/chrome_jni_registrar.cc b/chrome/browser/android/chrome_jni_registrar.cc
index 9bc939b6..3da04be1 100644
--- a/chrome/browser/android/chrome_jni_registrar.cc
+++ b/chrome/browser/android/chrome_jni_registrar.cc
@@ -23,6 +23,7 @@
 #include "chrome/browser/android/chrome_feature_list.h"
 #include "chrome/browser/android/compositor/compositor_view.h"
 #include "chrome/browser/android/compositor/layer_title_cache.h"
+#include "chrome/browser/android/compositor/resources/resource_factory.h"
 #include "chrome/browser/android/compositor/scene_layer/contextual_search_scene_layer.h"
 #include "chrome/browser/android/compositor/scene_layer/reader_mode_scene_layer.h"
 #include "chrome/browser/android/compositor/scene_layer/scene_layer.h"
@@ -379,6 +380,7 @@
     {"ReaderModeSceneLayer", RegisterReaderModeSceneLayer},
     {"RemoteMediaPlayerBridge",
      remote_media::RemoteMediaPlayerBridge::RegisterRemoteMediaPlayerBridge},
+    {"ResourceFactory", RegisterResourceFactory},
     {"ResourcePrefetchPredictor",
      predictors::RegisterResourcePrefetchPredictor},
     {"RevenueStats", chrome::android::RegisterRevenueStats},
diff --git a/chrome/browser/android/compositor/layer/toolbar_layer.cc b/chrome/browser/android/compositor/layer/toolbar_layer.cc
index 4015f17..e2389b8b 100644
--- a/chrome/browser/android/compositor/layer/toolbar_layer.cc
+++ b/chrome/browser/android/compositor/layer/toolbar_layer.cc
@@ -8,6 +8,7 @@
 #include "cc/layers/solid_color_layer.h"
 #include "cc/layers/ui_resource_layer.h"
 #include "cc/resources/scoped_ui_resource.h"
+#include "chrome/browser/android/compositor/resources/toolbar_resource.h"
 #include "content/public/browser/android/compositor.h"
 #include "third_party/skia/include/core/SkColor.h"
 #include "ui/android/resources/nine_patch_resource.h"
@@ -37,9 +38,8 @@
     bool show_debug,
     bool clip_shadow,
     bool browser_controls_at_bottom) {
-  // TODO(khushalsagar): This should not be a nine-patch resource.
-  ui::NinePatchResource* resource =
-      ui::NinePatchResource::From(resource_manager_->GetResource(
+  ToolbarResource* resource =
+      ToolbarResource::From(resource_manager_->GetResource(
           ui::ANDROID_RESOURCE_TYPE_DYNAMIC, toolbar_resource_id));
 
   // Ensure the toolbar resource is available before making the layer visible.
@@ -47,63 +47,50 @@
   if (!resource)
     return;
 
-  // This layer effectively draws over the space it takes for shadows.  Set the
-  // bounds to the non-shadow size so that other things can properly line up.
-  // Padding height does not include the height of the tabstrip, so we add
-  // it explicitly by adding y offset.
-  gfx::Size size =
-      gfx::Size(resource->padding().width(),
-                resource->padding().height() + resource->padding().y());
-  layer_->SetBounds(size);
+  // This layer effectively draws over the space the resource takes for shadows.
+  // Set the bounds to the non-shadow size so that other things can properly
+  // line up.
+  gfx::Size toolbar_bounds =
+      gfx::Size(resource->size().width(),
+                resource->size().height() - resource->shadow_height());
+  layer_->SetBounds(toolbar_bounds);
 
   // The toolbar_root_ contains all of the layers that make up the toolbar. The
   // toolbar_root_ is moved around inside of layer_ to allow appropriate
   // clipping of the shadow.
-  toolbar_root_->SetBounds(resource->padding().size());
-
-  gfx::PointF root_layer_position(0, y_offset);
-  gfx::PointF background_position(resource->padding().origin());
+  toolbar_root_->SetBounds(toolbar_bounds);
   if (browser_controls_at_bottom) {
-    // The toolbar's position as if it were completely shown.
-    float base_toolbar_y = window_height - resource->padding().size().height();
-    float layer_offset =
-        resource->size().height() - resource->padding().size().height();
-
-    root_layer_position.set_y(base_toolbar_y + y_offset);
-    toolbar_root_->SetPosition(gfx::PointF(0, -layer_offset));
-    background_position.set_y(layer_offset);
+    // If the browser controld are at bottom, this means that the shadow is at
+    // top of the container, i.e., at the top of the resource bitmap, move the
+    // toolbar up by the amount of the shadow to allow clipping if necessary.
+    toolbar_root_->SetPosition(gfx::PointF(0, -resource->shadow_height()));
   }
-  layer_->SetPosition(root_layer_position);
 
-  toolbar_background_layer_->SetBounds(resource->padding().size());
-  toolbar_background_layer_->SetPosition(background_position);
+  toolbar_background_layer_->SetBounds(resource->toolbar_rect().size());
+  toolbar_background_layer_->SetPosition(
+      gfx::PointF(resource->toolbar_rect().origin()));
   toolbar_background_layer_->SetBackgroundColor(toolbar_background_color);
 
-  bool url_bar_visible = (resource->aperture().width() != 0);
+  bool url_bar_visible = (resource->location_bar_content_rect().width() != 0);
   url_bar_background_layer_->SetHideLayerAndSubtree(!url_bar_visible);
   if (url_bar_visible) {
     ui::NinePatchResource* url_bar_background_resource =
         ui::NinePatchResource::From(resource_manager_->GetResource(
             ui::ANDROID_RESOURCE_TYPE_STATIC, url_bar_background_resource_id));
-    gfx::Size url_bar_size(resource->aperture().width() +
-                               url_bar_background_resource->size().width() -
-                               url_bar_background_resource->padding().width(),
-                           resource->aperture().height() +
-                               url_bar_background_resource->size().height() -
-                               url_bar_background_resource->padding().height());
-    gfx::Rect url_bar_border(
-        url_bar_background_resource->Border(url_bar_size));
-    gfx::PointF url_bar_position = gfx::PointF(
-        resource->aperture().x() - url_bar_background_resource->padding().x(),
-        resource->aperture().y() - url_bar_background_resource->padding().y());
 
-    url_bar_background_layer_->SetUIResourceId(
-        url_bar_background_resource->ui_resource()->id());
-    url_bar_background_layer_->SetBorder(url_bar_border);
+    gfx::Size draw_size(url_bar_background_resource->DrawSize(
+        resource->location_bar_content_rect().size()));
+    gfx::Rect border(url_bar_background_resource->Border(draw_size));
+    gfx::PointF position(url_bar_background_resource->DrawPosition(
+        resource->location_bar_content_rect().origin()));
+
+    url_bar_background_layer_->SetBounds(draw_size);
+    url_bar_background_layer_->SetPosition(position);
+    url_bar_background_layer_->SetBorder(border);
     url_bar_background_layer_->SetAperture(
         url_bar_background_resource->aperture());
-    url_bar_background_layer_->SetBounds(url_bar_size);
-    url_bar_background_layer_->SetPosition(url_bar_position);
+    url_bar_background_layer_->SetUIResourceId(
+        url_bar_background_resource->ui_resource()->id());
     url_bar_background_layer_->SetOpacity(url_bar_alpha);
   }
 
@@ -114,8 +101,9 @@
 
   anonymize_layer_->SetHideLayerAndSubtree(!anonymize);
   if (anonymize) {
-    anonymize_layer_->SetPosition(gfx::PointF(resource->aperture().origin()));
-    anonymize_layer_->SetBounds(resource->aperture().size());
+    anonymize_layer_->SetPosition(
+        gfx::PointF(resource->location_bar_content_rect().origin()));
+    anonymize_layer_->SetBounds(resource->location_bar_content_rect().size());
     anonymize_layer_->SetBackgroundColor(toolbar_textbox_background_color);
   }
 
@@ -124,6 +112,14 @@
     layer_->AddChild(debug_layer_);
   else if (!show_debug && debug_layer_->parent())
     debug_layer_->RemoveFromParent();
+
+  gfx::PointF root_layer_position(0, y_offset);
+  if (browser_controls_at_bottom) {
+    // The toolbar's position as if it were completely shown.
+    float base_toolbar_y = window_height - toolbar_bounds.height();
+    root_layer_position.set_y(base_toolbar_y + y_offset);
+  }
+  layer_->SetPosition(root_layer_position);
 }
 
 void ToolbarLayer::UpdateProgressBar(int progress_bar_x,
diff --git a/chrome/browser/android/compositor/resources/resource_factory.cc b/chrome/browser/android/compositor/resources/resource_factory.cc
new file mode 100644
index 0000000..f721430
--- /dev/null
+++ b/chrome/browser/android/compositor/resources/resource_factory.cc
@@ -0,0 +1,39 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/android/compositor/resources/resource_factory.h"
+
+#include "chrome/browser/android/compositor/resources/toolbar_resource.h"
+#include "jni/ResourceFactory_jni.h"
+
+using base::android::JavaParamRef;
+
+namespace android {
+
+bool RegisterResourceFactory(JNIEnv* env) {
+  return RegisterNativesImpl(env);
+}
+
+jlong CreateToolbarContainerResource(JNIEnv* env,
+                                     const JavaParamRef<jclass>& clazz,
+                                     jint toolbar_left,
+                                     jint toolbar_top,
+                                     jint toolbar_right,
+                                     jint toolbar_bottom,
+                                     jint location_bar_left,
+                                     jint location_bar_top,
+                                     jint location_bar_right,
+                                     jint location_bar_bottom,
+                                     jint shadow_height) {
+  gfx::Rect toolbar_rect(toolbar_left, toolbar_top,
+                         toolbar_right - toolbar_left,
+                         toolbar_bottom - toolbar_top);
+  gfx::Rect location_bar_content_rect(location_bar_left, location_bar_top,
+                                      location_bar_right - location_bar_left,
+                                      location_bar_bottom - location_bar_top);
+  return reinterpret_cast<intptr_t>(new ToolbarResource(
+      toolbar_rect, location_bar_content_rect, shadow_height));
+}
+
+}  // namespace android
diff --git a/chrome/browser/android/compositor/resources/resource_factory.h b/chrome/browser/android/compositor/resources/resource_factory.h
new file mode 100644
index 0000000..37f5c40
--- /dev/null
+++ b/chrome/browser/android/compositor/resources/resource_factory.h
@@ -0,0 +1,16 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_ANDROID_COMPOSITOR_RESOURCES_RESOURCE_FACTORY_H_
+#define CHROME_BROWSER_ANDROID_COMPOSITOR_RESOURCES_RESOURCE_FACTORY_H_
+
+#include "base/android/jni_android.h"
+
+namespace android {
+
+bool RegisterResourceFactory(JNIEnv* env);
+
+}  // namespace android
+
+#endif  // CHROME_BROWSER_ANDROID_COMPOSITOR_RESOURCES_RESOURCE_FACTORY_H_
diff --git a/chrome/browser/android/compositor/resources/toolbar_resource.cc b/chrome/browser/android/compositor/resources/toolbar_resource.cc
new file mode 100644
index 0000000..c3173a3
--- /dev/null
+++ b/chrome/browser/android/compositor/resources/toolbar_resource.cc
@@ -0,0 +1,32 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/android/compositor/resources/toolbar_resource.h"
+
+#include "base/memory/ptr_util.h"
+
+namespace android {
+
+// static
+ToolbarResource* ToolbarResource::From(ui::Resource* resource) {
+  DCHECK_EQ(Type::TOOLBAR, resource->type());
+  return static_cast<ToolbarResource*>(resource);
+}
+
+ToolbarResource::ToolbarResource(gfx::Rect toolbar_rect,
+                                 gfx::Rect location_bar_content_rect,
+                                 int shadow_height)
+    : Resource(Type::TOOLBAR),
+      toolbar_rect_(toolbar_rect),
+      location_bar_content_rect_(location_bar_content_rect),
+      shadow_height_(shadow_height) {}
+
+ToolbarResource::~ToolbarResource() = default;
+
+std::unique_ptr<ui::Resource> ToolbarResource::CreateForCopy() {
+  return base::MakeUnique<ToolbarResource>(
+      toolbar_rect_, location_bar_content_rect_, shadow_height_);
+}
+
+}  // namespace android
diff --git a/chrome/browser/android/compositor/resources/toolbar_resource.h b/chrome/browser/android/compositor/resources/toolbar_resource.h
new file mode 100644
index 0000000..94f7411
--- /dev/null
+++ b/chrome/browser/android/compositor/resources/toolbar_resource.h
@@ -0,0 +1,38 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_ANDROID_COMPOSITOR_RESOURCES_TOOLBAR_RESOURCE_H_
+#define CHROME_BROWSER_ANDROID_COMPOSITOR_RESOURCES_TOOLBAR_RESOURCE_H_
+
+#include "ui/android/resources/resource.h"
+
+namespace android {
+
+class ToolbarResource final : public ui::Resource {
+ public:
+  static ToolbarResource* From(ui::Resource* resource);
+
+  ToolbarResource(gfx::Rect toolbar_rect,
+                  gfx::Rect location_bar_content_rect,
+                  int shadow_height);
+  ~ToolbarResource() override;
+
+  std::unique_ptr<ui::Resource> CreateForCopy() override;
+
+  gfx::Rect toolbar_rect() const { return toolbar_rect_; }
+  gfx::Rect location_bar_content_rect() const {
+    return location_bar_content_rect_;
+  }
+  int shadow_height() const { return shadow_height_; }
+
+ private:
+  // All rects are in the Toolbar container's space.
+  gfx::Rect toolbar_rect_;
+  gfx::Rect location_bar_content_rect_;
+  int shadow_height_;
+};
+
+}  // namespace android
+
+#endif  // CHROME_BROWSER_ANDROID_COMPOSITOR_RESOURCES_TOOLBAR_RESOURCE_H_
diff --git a/ui/android/java/src/org/chromium/ui/resources/ResourceFactory.java b/ui/android/java/src/org/chromium/ui/resources/ResourceFactory.java
index eb5f1be1..a4032d2 100644
--- a/ui/android/java/src/org/chromium/ui/resources/ResourceFactory.java
+++ b/ui/android/java/src/org/chromium/ui/resources/ResourceFactory.java
@@ -20,8 +20,7 @@
                                                ninePatchData.getAperture());
     }
 
-    // Make this private in https://codereview.chromium.org/2752693003/
-    public static long createNinePatchBitmapResource(Rect padding, Rect aperture) {
+    private static long createNinePatchBitmapResource(Rect padding, Rect aperture) {
         return nativeCreateNinePatchBitmapResource(padding.left, padding.top, padding.right,
                 padding.bottom, aperture.left, aperture.top, aperture.right, aperture.bottom);
     }
diff --git a/ui/android/java/src/org/chromium/ui/resources/dynamics/ViewResourceAdapter.java b/ui/android/java/src/org/chromium/ui/resources/dynamics/ViewResourceAdapter.java
index c9daab1..540e5f84 100644
--- a/ui/android/java/src/org/chromium/ui/resources/dynamics/ViewResourceAdapter.java
+++ b/ui/android/java/src/org/chromium/ui/resources/dynamics/ViewResourceAdapter.java
@@ -25,8 +25,6 @@
 public class ViewResourceAdapter implements DynamicResource, OnLayoutChangeListener {
     private final View mView;
     private final Rect mDirtyRect = new Rect();
-    private final Rect mContentPadding = new Rect();
-    private final Rect mContentAperture = new Rect();
 
     private Bitmap mBitmap;
     private Rect mBitmapSize = new Rect();
@@ -75,17 +73,16 @@
         return mBitmapSize;
     }
 
+    /**
+     * Override this method to create the native resource type for the generated bitmap.
+     */
     @Override
     public long createNativeResource() {
-        // TODO(khushalsagar): Fix this to create the correct native resource type.
-        // See crbug.com/700454.
-        computeContentPadding(mContentPadding);
-        computeContentAperture(mContentAperture);
-        return ResourceFactory.createNinePatchBitmapResource(mContentPadding, mContentAperture);
+        return ResourceFactory.createBitmapResource(null);
     }
 
     @Override
-    public NinePatchData getNinePatchData() {
+    public final NinePatchData getNinePatchData() {
         return null;
     }
 
@@ -150,22 +147,6 @@
     }
 
     /**
-     * Gives overriding classes the chance to specify a different content padding.
-     * @param outContentPadding The resulting content padding.
-     */
-    protected void computeContentPadding(Rect outContentPadding) {
-        outContentPadding.set(0, 0, mView.getWidth(), mView.getHeight());
-    }
-
-    /**
-     * Gives overriding classes the chance to specify a different content aperture.
-     * @param outContentAperture The resulting content aperture.
-     */
-    protected void computeContentAperture(Rect outContentAperture) {
-        outContentAperture.set(0, 0, mView.getWidth(), mView.getHeight());
-    }
-
-    /**
      * @return Whether |mBitmap| is corresponding to |mView| or not.
      */
     private boolean validateBitmap() {
diff --git a/ui/android/resources/nine_patch_resource.cc b/ui/android/resources/nine_patch_resource.cc
index cf8055dd..5a8ff78e 100644
--- a/ui/android/resources/nine_patch_resource.cc
+++ b/ui/android/resources/nine_patch_resource.cc
@@ -5,6 +5,8 @@
 #include "ui/android/resources/nine_patch_resource.h"
 
 #include "base/memory/ptr_util.h"
+#include "cc/layers/nine_patch_layer.h"
+#include "ui/gfx/geometry/point_f.h"
 
 namespace ui {
 
@@ -21,6 +23,22 @@
 
 NinePatchResource::~NinePatchResource() = default;
 
+gfx::Size NinePatchResource::DrawSize(const gfx::Size& content_size) const {
+  // The effective drawing size of the resource includes the size of the content
+  // (fit inside the expanded padding area) and the size of the margins on each
+  // side.
+  return gfx::Size(content_size.width() + size().width() - padding_.width(),
+                   content_size.height() + size().height() - padding_.height());
+}
+
+gfx::PointF NinePatchResource::DrawPosition(
+    const gfx::Point& content_position) const {
+  // Offset the location of the layer by the amount taken by the left and top
+  // margin.
+  return gfx::PointF(content_position.x() - padding_.x(),
+                     content_position.y() - padding_.y());
+}
+
 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds) const {
   return Border(bounds, gfx::InsetsF(1.f, 1.f, 1.f, 1.f));
 }
diff --git a/ui/android/resources/nine_patch_resource.h b/ui/android/resources/nine_patch_resource.h
index 11b6502..9fe3815 100644
--- a/ui/android/resources/nine_patch_resource.h
+++ b/ui/android/resources/nine_patch_resource.h
@@ -8,6 +8,11 @@
 #include "ui/android/resources/resource.h"
 #include "ui/android/ui_android_export.h"
 #include "ui/gfx/geometry/insets_f.h"
+#include "ui/gfx/geometry/point_f.h"
+
+namespace cc {
+class NinePatchLayer;
+}
 
 namespace ui {
 
@@ -20,6 +25,20 @@
 
   std::unique_ptr<Resource> CreateForCopy() override;
 
+  // Returns the drawing size that the resource will take for padding content
+  // of size |content_size|.
+  gfx::Size DrawSize(const gfx::Size& content_size) const;
+
+  // Returns the position where the resource should be drawn to account for
+  // margins, given the |content_position| in the parent's coordinate space.
+  gfx::PointF DrawPosition(const gfx::Point& content_position) const;
+
+  // Updates draw properties on |layer| used to draw this resource. The
+  // |content_location| is the rect of the content to be fit inside the resource
+  // in the parent's coordinate space.
+  void UpdateNinePatchLayer(cc::NinePatchLayer* layer,
+                            const gfx::Rect& content_location) const;
+
   gfx::Rect Border(const gfx::Size& bounds) const;
   gfx::Rect Border(const gfx::Size& bounds, const gfx::InsetsF& scale) const;
 
diff --git a/ui/android/resources/resource.h b/ui/android/resources/resource.h
index a30f128..ac75b31 100644
--- a/ui/android/resources/resource.h
+++ b/ui/android/resources/resource.h
@@ -14,7 +14,7 @@
 
 class UI_ANDROID_EXPORT Resource {
  public:
-  enum class Type { BITMAP, NINE_PATCH_BITMAP };
+  enum class Type { BITMAP, NINE_PATCH_BITMAP, TOOLBAR };
 
   Resource();
   virtual ~Resource();