Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Unified Diff: content/public/android/java/src/org/chromium/content_public/resources/ResourceManager.java

Issue 731133002: Upstream ResourceManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add generated enum class to BUILD.gn Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/public/android/java/src/org/chromium/content_public/resources/ResourceManager.java
diff --git a/content/public/android/java/src/org/chromium/content_public/resources/ResourceManager.java b/content/public/android/java/src/org/chromium/content_public/resources/ResourceManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..b80a759c6757843be95a0b4d3153daa93490ebdb
--- /dev/null
+++ b/content/public/android/java/src/org/chromium/content_public/resources/ResourceManager.java
@@ -0,0 +1,175 @@
+// Copyright 2014 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.content_public.resources;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.Rect;
+import android.util.SparseArray;
+
+import org.chromium.base.CalledByNative;
+import org.chromium.base.JNINamespace;
+import org.chromium.content_public.resources.ResourceLoader.ResourceLoaderCallback;
+import org.chromium.content_public.resources.dynamics.DynamicResource;
+import org.chromium.content_public.resources.dynamics.DynamicResourceLoader;
+import org.chromium.content_public.resources.statics.StaticResourceLoader;
+import org.chromium.content_public.resources.statics.StaticResourcePreloads;
+import org.chromium.content_public.resources.system.SystemResourceLoader;
+
+/**
+ * The Java component of a manager for all static resources to be loaded and used by CC layers.
+ * This class does not hold any resource state, but passes it directly to native as they are loaded.
+ */
+@JNINamespace("content")
+public class ResourceManager implements ResourceLoaderCallback {
+ private final SparseArray<ResourceLoader> mResourceLoaders = new SparseArray<ResourceLoader>();
+ private final SparseArray<SparseArray<LayoutResource>> mLoadedResources =
+ new SparseArray<SparseArray<LayoutResource>>();
+
+ private final float mPxToDp;
+
+ private long mNativeResourceManagerPtr;
+ private boolean mPreloadedStaticResources;
+
+ private ResourceManager(Context context, long staticResourceManagerPtr) {
+ Resources resources = context.getResources();
+ mPxToDp = 1.f / resources.getDisplayMetrics().density;
+
+ // Register ResourceLoaders
+ registerResourceLoader(new StaticResourceLoader(
+ ResourceKind.STATIC, this, resources));
+ registerResourceLoader(new DynamicResourceLoader(
+ ResourceKind.DYNAMIC, this));
+ registerResourceLoader(new DynamicResourceLoader(
+ ResourceKind.DYNAMIC_BITMAP, this));
+ registerResourceLoader(new SystemResourceLoader(
+ ResourceKind.SYSTEM, this, context));
+
+ mNativeResourceManagerPtr = staticResourceManagerPtr;
+ }
+
+ /**
+ * Creates an instance of a {@link ResourceManager}. This will
+ * @param context A {@link Context} instance to grab {@link Resources} from.
+ * @param staticResourceManagerPtr A pointer to the native component of this class.
+ * @return A new instance of a {@link ResourceManager}.
+ */
+ @CalledByNative
+ private static ResourceManager create(Context context, long staticResourceManagerPtr) {
+ return new ResourceManager(context, staticResourceManagerPtr);
+ }
+
+ /**
+ * @return A reference to the {@link DynamicResourceLoader} that provides
+ * {@link DynamicResource} objects to this class.
+ */
+ public DynamicResourceLoader getDynamicResourceLoader() {
+ return (DynamicResourceLoader) mResourceLoaders.get(
+ ResourceKind.DYNAMIC);
+ }
+
+ /**
+ * @return A reference to the {@link DynamicResourceLoader} for bitmaps that provides
+ * {@link BitmapDynamicResource} objects to this class.
+ */
+ public DynamicResourceLoader getBitmapDynamicResourceLoader() {
+ return (DynamicResourceLoader) mResourceLoaders.get(
+ ResourceKind.DYNAMIC_BITMAP);
+ }
+
+ /**
+ * TODO(dtrainor, clholgat): Move this to dynamically preload from layouts.
+ * Automatically loads any synchronous resources specified in
+ * {@link StaticResourcePreloads#getSynchronousResources(Context)} and will start asynchronous
+ * reads for any asynchronous resources specified in
+ * {@link StaticResourcePreloads#getAsynchronousResources()}.
+ * @param context An Android Context.
+ * @param preloads An interface to track all high priority resources.
+ */
+ public void preloadStaticResources(Context context, StaticResourcePreloads preloads) {
David Trainor- moved to gerrit 2014/11/18 18:53:53 Can we change this to something like preloadResour
Jaekyun Seok (inactive) 2014/11/18 23:34:02 Sounds good. I will change codes.
+ if (mPreloadedStaticResources) return;
+
+ if (preloads != null) {
+ ResourceLoader loader = mResourceLoaders.get(ResourceKind.STATIC);
+ for (Integer resId : preloads.getAsynchronousResources()) {
+ loader.preloadResource(resId);
+ }
+
+ for (Integer resId : preloads.getSynchronousResources()) {
+ loader.loadResource(resId);
+ }
+ }
+
+ mPreloadedStaticResources = true;
+ }
+
+ /**
+ * @param resType The type of the Android resource.
+ * @param resId The id of the Android resource.
+ * @return The corresponding {@link LayoutResource}.
+ */
+ public LayoutResource getResource(int resType, int resId) {
+ SparseArray<LayoutResource> bucket = mLoadedResources.get(resType);
+ return bucket != null ? bucket.get(resId) : null;
+ }
+
+ @Override
+ public void onResourceLoaded(int resType, int resId, Resource resource) {
+ if (resource == null) return;
+
+ saveMetadataForLoadedResource(resType, resId, resource);
+
+ if (mNativeResourceManagerPtr == 0) return;
+ Rect padding = resource.getPadding();
+ Rect aperture = resource.getAperture();
+
+ nativeOnResourceReady(mNativeResourceManagerPtr, resType, resId, resource.getBitmap(),
+ padding.left, padding.top, padding.right, padding.bottom,
+ aperture.left, aperture.top, aperture.right, aperture.bottom);
+ }
+
+ private void saveMetadataForLoadedResource(int resType, int resId, Resource resource) {
+ SparseArray<LayoutResource> bucket = mLoadedResources.get(resType);
+ if (bucket == null) {
+ bucket = new SparseArray<LayoutResource>();
+ mLoadedResources.put(resType, bucket);
+ }
+ bucket.put(resId, new LayoutResource(mPxToDp, resource));
+ }
+
+ @CalledByNative
+ private void destroy() {
+ assert mNativeResourceManagerPtr != 0;
+ mNativeResourceManagerPtr = 0;
+ }
+
+ @CalledByNative
+ private void resourceRequested(int resType, int resId) {
+ ResourceLoader loader = mResourceLoaders.get(resType);
+ if (loader != null) loader.loadResource(resId);
+ }
+
+ @CalledByNative
+ private void preloadResource(int resType, int resId) {
+ ResourceLoader loader = mResourceLoaders.get(resType);
+ if (loader != null) loader.preloadResource(resId);
+ }
+
+ @CalledByNative
+ private long getNativePtr() {
+ return mNativeResourceManagerPtr;
+ }
+
+ private void registerResourceLoader(ResourceLoader loader) {
+ mResourceLoaders.put(loader.getResourceType(), loader);
+ }
+
+ private native void nativeOnResourceReady(long nativeResourceManagerImpl, int resType,
+ int resId, Bitmap bitmap, int paddingLeft, int paddingTop, int paddingRight,
+ int paddingBottom, int apertureLeft, int apertureTop, int apertureRight,
+ int apertureBottom);
+
+}

Powered by Google App Engine
This is Rietveld 408576698