summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/android/java/src/org/chromium/base/ContextTypes.java84
-rw-r--r--base/android/java/src/org/chromium/base/PathUtils.java25
-rw-r--r--base/android/javatests/src/org/chromium/base/ContextTypesTest.java43
3 files changed, 151 insertions, 1 deletions
diff --git a/base/android/java/src/org/chromium/base/ContextTypes.java b/base/android/java/src/org/chromium/base/ContextTypes.java
new file mode 100644
index 0000000..18926aa
--- /dev/null
+++ b/base/android/java/src/org/chromium/base/ContextTypes.java
@@ -0,0 +1,84 @@
+// Copyright (c) 2013 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.base;
+
+import android.content.Context;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.Map;
+
+/**
+ * Maintains the {@link Context}-to-"context type" mapping. The context type
+ * {@code MODE_APP} is chosen for the application context associated with
+ * the activity running in application mode, while {@code MODE_NORMAL} for main
+ * Chromium activity.
+ *
+ * <p>Used as singleton instance.
+ */
+public class ContextTypes {
+
+ // Available context types
+ public static final int CONTEXT_TYPE_NORMAL = 1;
+ public static final int CONTEXT_TYPE_WEBAPP = 2;
+
+ private final Map<Context, Integer> mContextMap;
+
+ private ContextTypes() {
+ mContextMap = new ConcurrentHashMap<Context, Integer>();
+ }
+
+ private static class ContextTypesHolder {
+ private static final ContextTypes INSTANCE = new ContextTypes();
+ }
+
+ public static ContextTypes getInstance() {
+ return ContextTypesHolder.INSTANCE;
+ }
+
+ /**
+ * Adds the mapping for the given {@link Context}.
+ *
+ * @param context {@link Context} in interest
+ * @param type the type associated with the context
+ * @throws IllegalArgumentException if type is not a valid one.
+ */
+ public void put(Context context, int type) throws IllegalArgumentException {
+ if (type != CONTEXT_TYPE_NORMAL && type != CONTEXT_TYPE_WEBAPP) {
+ throw new IllegalArgumentException("Wrong context type");
+ }
+ mContextMap.put(context, type);
+ }
+
+ /**
+ * Removes the mapping for the given context.
+ *
+ * @param context {@link Context} in interest
+ */
+ public void remove(Context context) {
+ mContextMap.remove(context);
+ }
+
+ /**
+ * Returns type of the given context.
+ *
+ * @param context {@link Context} in interest
+ * @return type associated with the context. Returns {@code MODE_NORMAL} by
+ * default if the mapping for the queried context is not present.
+ */
+ public int getType(Context context) {
+ Integer contextType = mContextMap.get(context);
+ return contextType == null ? CONTEXT_TYPE_NORMAL : contextType;
+ }
+
+ /**
+ * Checks if the mapping exists for the given context.
+ *
+ * @param context {@link Context} in interest
+ * @return {@code true} if the mapping exists; otherwise {@code false}
+ */
+ public boolean contains(Context context) {
+ return mContextMap.containsKey(context);
+ }
+}
diff --git a/base/android/java/src/org/chromium/base/PathUtils.java b/base/android/java/src/org/chromium/base/PathUtils.java
index c3503a4..1d9af7e 100644
--- a/base/android/java/src/org/chromium/base/PathUtils.java
+++ b/base/android/java/src/org/chromium/base/PathUtils.java
@@ -8,12 +8,16 @@ import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Environment;
+import java.io.File;
+
/**
* This class provides the path related methods for the native library.
*/
public abstract class PathUtils {
private static String sDataDirectorySuffix;
+ private static String sWebappDirectorySuffix;
+ private static String sWebappCacheDirectory;
// Prevent instantiation.
private PathUtils() {}
@@ -29,6 +33,17 @@ public abstract class PathUtils {
}
/**
+ * Sets the directory info used for chrome process running in application mode.
+ *
+ * @param webappSuffix The suffix of the directory used for storing webapp-specific profile
+ * @param cacheDir Cache directory name for web apps.
+ */
+ public static void setWebappDirectoryInfo(String webappSuffix, String cacheDir) {
+ sWebappDirectorySuffix = webappSuffix;
+ sWebappCacheDirectory = cacheDir;
+ }
+
+ /**
* @return the private directory that is used to store application data.
*/
@CalledByNative
@@ -46,7 +61,15 @@ public abstract class PathUtils {
@SuppressWarnings("unused")
@CalledByNative
private static String getCacheDirectory(Context appContext) {
- return appContext.getCacheDir().getPath();
+ if (ContextTypes.getInstance().getType(appContext) == ContextTypes.CONTEXT_TYPE_NORMAL) {
+ return appContext.getCacheDir().getPath();
+ }
+ if (sWebappDirectorySuffix == null || sWebappCacheDirectory == null) {
+ throw new IllegalStateException(
+ "setWebappDirectoryInfo must be called before getCacheDirectory");
+ }
+ return new File(appContext.getDir(sWebappDirectorySuffix, appContext.MODE_PRIVATE),
+ sWebappCacheDirectory).getPath();
}
/**
diff --git a/base/android/javatests/src/org/chromium/base/ContextTypesTest.java b/base/android/javatests/src/org/chromium/base/ContextTypesTest.java
new file mode 100644
index 0000000..a26b317
--- /dev/null
+++ b/base/android/javatests/src/org/chromium/base/ContextTypesTest.java
@@ -0,0 +1,43 @@
+// Copyright 2013 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.base;
+
+import android.content.Context;
+import android.test.AndroidTestCase;
+import android.test.mock.MockContext;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import static org.chromium.base.ContextTypes.CONTEXT_TYPE_NORMAL;
+import static org.chromium.base.ContextTypes.CONTEXT_TYPE_WEBAPP;
+
+public class ContextTypesTest extends AndroidTestCase {
+
+ @SmallTest
+ public void testReturnsExpectedType() {
+ ContextTypes contextTypes = ContextTypes.getInstance();
+ Context normal = new MockContext();
+ Context webapp = new MockContext();
+ contextTypes.put(normal, CONTEXT_TYPE_NORMAL);
+ contextTypes.put(webapp, CONTEXT_TYPE_WEBAPP);
+ assertEquals(CONTEXT_TYPE_NORMAL, contextTypes.getType(normal));
+ assertEquals(CONTEXT_TYPE_WEBAPP, contextTypes.getType(webapp));
+ }
+
+ @SmallTest
+ public void testAbsentContextReturnsNormalType() {
+ assertEquals(CONTEXT_TYPE_NORMAL, ContextTypes.getInstance().getType(new MockContext()));
+ }
+
+ @SmallTest
+ public void testPutInvalidTypeThrowsException() {
+ boolean exceptionThrown = false;
+ try {
+ ContextTypes.getInstance().put(new MockContext(), -1);
+ } catch (IllegalArgumentException e) {
+ exceptionThrown = true;
+ }
+ assertTrue(exceptionThrown);
+ }
+}