Permalink
Browse files

Allows CommonJS from applicationDataDirectory

  • Loading branch information...
1 parent 6e3cab6 commit d332a846701df512003fdcb1fe791a6497348515 @dbankier committed Jul 18, 2012
@@ -23,7 +23,7 @@
{
private static final String TAG = "TiAssetHelper";
private static WeakReference<AssetManager> manager;
- private static String packageName, cacheDir;
+ private static String packageName, cacheDir, dataDir;
private static AssetCrypt assetCrypt;
public interface AssetCrypt
@@ -41,6 +41,7 @@ public static void init(Context context)
KrollAssetHelper.manager = new WeakReference<AssetManager>(context.getAssets());
KrollAssetHelper.packageName = context.getPackageName();
KrollAssetHelper.cacheDir = context.getCacheDir().getAbsolutePath();
+ KrollAssetHelper.dataDir = context.getDir("appdata", 0).getAbsolutePath();
}
public static String readAsset(String path)
@@ -67,14 +68,21 @@ public static String readAsset(String path)
}
}
- try {
+ try {
AssetManager assetManager = manager.get();
if (assetManager == null) {
Log.e(TAG, "AssetManager is null, can't read asset: " + path);
return null;
}
-
- InputStream in = assetManager.open(path);
+ InputStream in;
+ //[TiShadow] for loading from private data
+ if (path.startsWith("appdata-private://")) {
+ in = new FileInputStream(dataDir + path.substring(18));
+ } else if (path.startsWith("/data/data")) {
+ in = new FileInputStream(path);
+ } else {
+ in = assetManager.open(path);
+ }
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte buffer[] = new byte[1024];
int count = 0;
@@ -217,14 +217,29 @@ Module.prototype.require = function (request, context, useCache) {
return this.loadExternalModule(request, externalBinding, context);
}
- var resolved = resolveFilename(request, this);
- var id = resolved[0];
- var filename = resolved[1];
+
+ var id, filename;
+ // [TiShadow] Cowboy loading if using an appdata url
+ // need to still support /data/data (legecy TiShadow use)
+ var isNonResource = request.indexOf("/data/data") === 0 || request.indexOf("appdata-private") === 0;
+ if (isNonResource) {
+ if (request.indexOf("appdata-private") === 0 ) {
+ id = filename = request + ".js"
+ } else { // legacy /data/data usage
+ id = filename = request;
+ }
+ } else {
+ var resolved = resolveFilename(request, this);
+ id = resolved[0];
+ filename = resolved[1];
+ }
+
if (kroll.DBG) {
kroll.log(TAG, 'Loading module: ' + request + ' -> ' + filename);
}
- if (useCache) {
+ // [TiShadow] don't cache TiShadow modules
+ if (useCache && !isNonResource) {
var cachedModule = Module.cache[filename];
if (cachedModule) {
return cachedModule.exports;
@@ -236,7 +251,8 @@ Module.prototype.require = function (request, context, useCache) {
module.load(filename);
- if (useCache) {
+ // [TiShadow] don't cache TiShadow modules
+ if (useCache && !isNonResource) {
// Cache the module for future requests.
Module.cache[filename] = module;
}
@@ -1,2 +1,2 @@
-version = '2.1.0'
+version = 'tishadow_2.1.0'
module_apiversion = '2'
@@ -769,10 +769,11 @@ -(id)require:(KrollContext*)kroll path:(NSString*)path
TiModule* module = nil;
NSData *data = nil;
NSString *filepath = nil;
-
+
// first check to see if we've already loaded the module
// and if so, return it
- if (modules!=nil)
+ // [TiShadow] don't cache TiShadow modules
+ if (modules!=nil && ![path hasPrefix:@"file:"])
{
module = [modules objectForKey:path];
if (module!=nil)
@@ -811,7 +812,13 @@ -(id)require:(KrollContext*)kroll path:(NSString*)path
if (data==nil)
{
filepath = [NSString stringWithFormat:@"%@.js",path];
- NSURL *url_ = [TiHost resourceBasedURL:filepath baseURL:NULL];
+ NSURL *url_;
+ //[TiShadow] for applicationDataDirectory loading
+ if (![filepath hasPrefix:@"file:"]) {
+ url_ = [TiHost resourceBasedURL:filepath baseURL:NULL];
+ } else {
+ url_ = [NSURL URLWithString:filepath];
+ }
data = [TiUtils loadAppResource:url_];
if (data==nil)
{
@@ -823,7 +830,13 @@ -(id)require:(KrollContext*)kroll path:(NSString*)path
if (data!=nil)
{
NSString* urlPath = (filepath != nil) ? filepath : path;
- NSURL *url_ = [TiHost resourceBasedURL:urlPath baseURL:NULL];
+ NSURL *url_;
+ //[TiShadow] for applicationDataDirectory loading
+ if (![urlPath hasPrefix:@"file:"]) {
+ url_ = [TiHost resourceBasedURL:urlPath baseURL:NULL];
+ } else {
+ url_ = [NSURL URLWithString:urlPath];
+ }
const char *urlCString = [[url_ absoluteString] UTF8String];
KrollWrapper* wrapper = nil;
@@ -847,7 +860,10 @@ -(id)require:(KrollContext*)kroll path:(NSString*)path
if (module == nil) {
module = (id)wrapper;
- [modules setObject:module forKey:path];
+ // [TiShadow] don't cache TiShadow modules
+ if (![urlPath hasPrefix:@"file:"]) {
+ [modules setObject:module forKey:path];
+ }
if (filepath!=nil && module!=nil)
{
// uri is optional but we point it to where we loaded it

0 comments on commit d332a84

Please sign in to comment.