New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TIMOB-25663] Android: HttpClient.cache. ImageView.cache #9719
Conversation
Generated by 🚫 dangerJS |
a1c18f6
to
9f0a469
Compare
9f0a469
to
8b37968
Compare
8b37968
to
8aaab57
Compare
8aaab57
to
b57ee3e
Compare
@drauggres Thanks for all your work on this! We will schedule this for the next release now. Can you please re-sign the Axway CLA? It was changed lately to migrate the legal part of the swaggy open source community to our legal team. |
Generated by 🚫 dangerJS |
'use strict'; | ||
var should = require('./utilities/assertions'); | ||
|
||
describe('Titanium.Android.HttpResponseCache', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just do describe.android('Titanium.Android.HttpResponseCache', function () {
and all the nested tests all be limited to android, so that you don't need to ad date filter to each it
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
tests/Resources/app.js
Outdated
require('./ti.accelerometer.test'); | ||
require('./ti.analytics.test'); | ||
require('./ti.api.test'); | ||
require('./ti.android.httpresponsecache.test'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once I merge to the full test suite, I'd modify the app.js like this, but for PRs the easier way to avoid it is to simply ensure your additional tests are named ti.whatever.addontest.js
and they'll get picked up automatically. The addontest.js
suffix is what gets it picked up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove app.js
Probably someone should update README
xhr.send(); | ||
}); | ||
|
||
it.android('HttpResponseCache xhr.cache custom', function (finish) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, you can just add an "add-on test" to add new unit tests to the suite and not have to copy/override the existing API test file. Makes it easier for you and for us to see the new tests added.
Copying and modifying is what's necessary for modifying existing tests (say fixing a test we skip on a given platform and re-enabling it).
So anyways, for next time, you could just create a ti.network.httpclient.addontest.js
and add these two new tests to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
{ | ||
HttpResponseCache cache = HttpResponseCache.getInstalled(); | ||
if (cache != null) { | ||
cache.getNetworkCount(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing return
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
// clang-format off | ||
@Kroll.method | ||
@Kroll.getProperty | ||
public long getHttpCacheSize() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not real fond of the long name here. I assume it was used to not clash with the size()
method?
Why not just mirror the underlying API's use of maxSize
for the property/method name?
Additionally, please note that we're going to be moving away from getter/setter methods that just wrap properties in favor of using typical JS property accessors. So in this case, that'd be removing the @Kroll.method
annotation and just assigning this method to be the backing impl for cache.httpCacheSize
calls in JS. (Or hopefully cache.maxSize
if the property is renamed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maxSize
it is
// clang-format off | ||
@Kroll.method | ||
@Kroll.setProperty | ||
public void setHttpCachePath(String value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, not fond of the long name. Why not just use path
as the property?
Also, the setter method would be deprecated in 8.0.0 immediately anyhow. So maybe just use this as the @Kroll.setProperty
impl backing cache.path = 'whatever';
calls. (i.e. remove @Kroll.method
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
httpCachePath
-> path
, property only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a very nice API addition that could be very helpful for app developers. Thanks, @drauggres !
My first concern is that the path set is only a relative path and doesn't offer control whether the cache gets put in the external cache or the app cache dir, which can have security/data privacy issues. I think the property should be the full path so the dev has control to explicitly choose that (though perhaps it could default to app cache directory or something). To enable that we may need to expose the external cache dir in our APIs.
I'm also concerned that this is very Android-specific, as I believe iOS could also implement something similar as well here.
@hansemannn Can you please take a look and see if we couldn't make this a more general API cross-platform? Maybe hang it off Ti.Network.Cache
or something?
// clang-format off | ||
@Kroll.method | ||
@Kroll.setProperty | ||
public void setHttpCacheSize(long value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setMaxSize(long value)
?
// clang-format off | ||
@Kroll.method | ||
@Kroll.getProperty | ||
public String getHttpCachePath() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getPath()
? Remove the @Kroll.method
annotation (and then the clang-format comments are unnecessary too)?
if (cache != null) { | ||
return cache.size(); | ||
} | ||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this return -1
? Or do we want to have differing values for "no cache installed" versus "cache installed, but unable to determine size"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or do we want to have differing values for "no cache installed" versus "cache installed, but unable to determine size"?
I think, we shouldn't mix it.
@@ -163,7 +165,14 @@ public static TiDrawableReference fromUrl(KrollProxy proxy, String url) | |||
if (url == null || url.length() == 0 || url.trim().length() == 0) { | |||
return new TiDrawableReference(proxy.getActivity(), DrawableReferenceType.NULL); | |||
} | |||
return fromUrl(proxy.getActivity(), proxy.resolveUrl(null, url)); | |||
boolean useCaches = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can shorten this to:
boolean useCaches = TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_CACHE), false);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -239,8 +248,15 @@ public static TiDrawableReference fromObject(KrollProxy proxy, Object object) | |||
object = proxy.resolveUrl(null, (String) object); | |||
} | |||
|
|||
boolean useCaches = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, you can shrink this down to a one-liner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
extends: Titanium.Proxy | ||
platforms: [android] | ||
createable: false | ||
since: "7.1.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, we clearly missed this release. This will need to be updated for whatever release we can get this in. master is currently targeting 7.4.0, but I think feature freeze may be happening for that real soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to 7.4.0
|
||
- name: httpCacheSize | ||
summary: Maximum cache size in bytes. | ||
description: | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maye want to add some details/notes that this needs to be set before #install()
and once that's called this can't be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add this note:
To apply changes of this property to existing HttpResponseCache instance, you should call [install](Titanium.Android.HttpResponseCache.install) method.
- name: httpCachePath | ||
summary: Relative path for cache | ||
description: | | ||
HTTP response cache will be stored on external storage if it exists on moment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, maybe more more explicit that this property is consulted at time #install()
is called, but changing later won't do anything. (In fact we may even want to spit out some warning logs in the impl if they get called after #install()
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add this note:
To apply changes of this property to existing HttpResponseCache instance, you should call [install](Titanium.Android.HttpResponseCache.install) method.
cache.close(); | ||
} | ||
TiApplication tiApp = TiApplication.getInstance(); | ||
File dir = tiApp.getApplicationContext().getExternalCacheDir(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not have the path
(or httpCacheDir
) property value determine the directory and relative path to store the cache?
I'm worried here because the Android docs point out that if the requests contain private data, the external cache dir is a bad place to stick the cache due to access controls not being enforced on external storage.
I'm assuming it's because we don't actually expose the external cache dir anywhere in our APIs? cc @garymathews @jquick-axway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming it's because we don't actually expose the external cache dir anywhere in our APIs?
Yes. Also this is how currently cacheDir
calculated for TiResponseCache
.
@sgtcoolguy Thanks for thorough code review.
What about additional boolean property (smth like |
I have to agree with @hansemannn comment here that the API to control the response cache (deletion, size, location, etc.) should be redesigned to so that it can work on both Android and iOS. I also don't like the idea of replacing our I do agree that we should add "cache" property support to I'd prefer to keep our |
Resume:
|
JIRA: https://jira.appcelerator.org/browse/TIMOB-25663
Add
Ti.Android.HttpResponseCache
moduleAdd
Ti.Network.HTTPClient
cache
propertyAdd
Ti.UI.ImageView
cache
property