OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.Intent; | 8 import android.content.Intent; |
9 import android.content.SharedPreferences; | 9 import android.content.SharedPreferences; |
10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 // updates less frequently. crbug.com/680128. | 77 // updates less frequently. crbug.com/680128. |
78 public static final long RELAXED_UPDATE_INTERVAL = TimeUnit.DAYS.toMillis(30
L); | 78 public static final long RELAXED_UPDATE_INTERVAL = TimeUnit.DAYS.toMillis(30
L); |
79 | 79 |
80 // Number of milliseconds to wait before re-requesting an updated WebAPK fro
m the WebAPK | 80 // Number of milliseconds to wait before re-requesting an updated WebAPK fro
m the WebAPK |
81 // server if the previous update attempt failed. | 81 // server if the previous update attempt failed. |
82 public static final long RETRY_UPDATE_DURATION = TimeUnit.HOURS.toMillis(12L
); | 82 public static final long RETRY_UPDATE_DURATION = TimeUnit.HOURS.toMillis(12L
); |
83 | 83 |
84 // The default shell Apk version of WebAPKs. | 84 // The default shell Apk version of WebAPKs. |
85 static final int DEFAULT_SHELL_APK_VERSION = 1; | 85 static final int DEFAULT_SHELL_APK_VERSION = 1; |
86 | 86 |
87 // Invalid constants for timestamps and URLs. '0' is used as the invalid tim
estamp as | 87 // Unset/invalid constants for last used times and URLs. 0 is used as the nu
ll last used time as |
88 // WebappRegistry and WebApkUpdateManager assume that timestamps are always
valid. | 88 // WebappRegistry assumes that this is always a valid timestamp. |
89 static final long TIMESTAMP_INVALID = 0; | 89 static final long LAST_USED_UNSET = 0; |
| 90 static final long LAST_USED_INVALID = -1; |
90 static final String URL_INVALID = ""; | 91 static final String URL_INVALID = ""; |
91 static final int VERSION_INVALID = 0; | 92 static final int VERSION_INVALID = 0; |
92 | 93 |
93 // We use a heuristic to determine whether a web app is still installed on t
he home screen, as | 94 // We use a heuristic to determine whether a web app is still installed on t
he home screen, as |
94 // there is no way to do so directly. Any web app which has been opened in t
he last ten days | 95 // there is no way to do so directly. Any web app which has been opened in t
he last ten days |
95 // is considered to be still on the home screen. | 96 // is considered to be still on the home screen. |
96 static final long WEBAPP_LAST_OPEN_MAX_TIME = TimeUnit.DAYS.toMillis(10L); | 97 static final long WEBAPP_LAST_OPEN_MAX_TIME = TimeUnit.DAYS.toMillis(10L); |
97 | 98 |
98 private static Clock sClock = new Clock(); | 99 private static Clock sClock = new Clock(); |
99 private static Factory sFactory = new Factory(); | 100 private static Factory sFactory = new Factory(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 */ | 132 */ |
132 public long currentTimeMillis() { | 133 public long currentTimeMillis() { |
133 return System.currentTimeMillis(); | 134 return System.currentTimeMillis(); |
134 } | 135 } |
135 } | 136 } |
136 | 137 |
137 /** | 138 /** |
138 * Opens an instance of WebappDataStorage for the web app specified. | 139 * Opens an instance of WebappDataStorage for the web app specified. |
139 * @param webappId The ID of the web app. | 140 * @param webappId The ID of the web app. |
140 */ | 141 */ |
141 static WebappDataStorage open(String webappId) { | 142 static WebappDataStorage open(final String webappId) { |
142 return sFactory.create(webappId); | 143 final WebappDataStorage storage = sFactory.create(webappId); |
| 144 if (storage.getLastUsedTime() == LAST_USED_INVALID) { |
| 145 // If the last used time is invalid then ensure that there is no dat
a in the |
| 146 // WebappDataStorage which needs to be cleaned up. |
| 147 assert storage.isEmpty(); |
| 148 } |
| 149 return storage; |
143 } | 150 } |
144 | 151 |
145 /** | 152 /** |
146 * Sets the clock used to get the current time. | 153 * Sets the clock used to get the current time. |
147 */ | 154 */ |
148 @VisibleForTesting | 155 @VisibleForTesting |
149 public static void setClockForTests(Clock clock) { | 156 public static void setClockForTests(Clock clock) { |
150 sClock = clock; | 157 sClock = clock; |
151 } | 158 } |
152 | 159 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 mPreferences.edit().clear().apply(); | 314 mPreferences.edit().clear().apply(); |
308 } | 315 } |
309 | 316 |
310 /** | 317 /** |
311 * Deletes the URL and scope, and sets all timestamps to 0 in SharedPreferen
ces. | 318 * Deletes the URL and scope, and sets all timestamps to 0 in SharedPreferen
ces. |
312 * This does not remove the stored splash screen image (if any) for the app. | 319 * This does not remove the stored splash screen image (if any) for the app. |
313 */ | 320 */ |
314 void clearHistory() { | 321 void clearHistory() { |
315 SharedPreferences.Editor editor = mPreferences.edit(); | 322 SharedPreferences.Editor editor = mPreferences.edit(); |
316 | 323 |
317 editor.remove(KEY_LAST_USED); | 324 // The last used time is set to 0 to ensure that a valid value is always
present. |
| 325 // If the web app is not launched prior to the next cleanup, then its re
maining data will be |
| 326 // removed. Otherwise, the next launch from home screen will update the
last used time. |
| 327 editor.putLong(KEY_LAST_USED, LAST_USED_UNSET); |
318 editor.remove(KEY_URL); | 328 editor.remove(KEY_URL); |
319 editor.remove(KEY_SCOPE); | 329 editor.remove(KEY_SCOPE); |
320 editor.remove(KEY_LAST_CHECK_WEB_MANIFEST_UPDATE_TIME); | 330 editor.remove(KEY_LAST_CHECK_WEB_MANIFEST_UPDATE_TIME); |
321 editor.remove(KEY_LAST_UPDATE_REQUEST_COMPLETE_TIME); | 331 editor.remove(KEY_LAST_UPDATE_REQUEST_COMPLETE_TIME); |
322 editor.remove(KEY_DID_LAST_UPDATE_REQUEST_SUCCEED); | 332 editor.remove(KEY_DID_LAST_UPDATE_REQUEST_SUCCEED); |
323 editor.remove(KEY_UPDATE_REQUESTED); | 333 editor.remove(KEY_UPDATE_REQUESTED); |
324 editor.remove(KEY_RELAX_UPDATES); | 334 editor.remove(KEY_RELAX_UPDATES); |
325 editor.remove(KEY_DISMISSED_DISCLOSURE); | 335 editor.remove(KEY_DISMISSED_DISCLOSURE); |
326 editor.apply(); | 336 editor.apply(); |
327 } | 337 } |
(...skipping 19 matching lines...) Expand all Loading... |
347 | 357 |
348 /** Updates the source. */ | 358 /** Updates the source. */ |
349 public void updateSource(int source) { | 359 public void updateSource(int source) { |
350 mPreferences.edit().putInt(KEY_SOURCE, source).apply(); | 360 mPreferences.edit().putInt(KEY_SOURCE, source).apply(); |
351 } | 361 } |
352 | 362 |
353 /** | 363 /** |
354 * Returns the last used time of this object, or -1 if it is not stored. | 364 * Returns the last used time of this object, or -1 if it is not stored. |
355 */ | 365 */ |
356 public long getLastUsedTime() { | 366 public long getLastUsedTime() { |
357 return mPreferences.getLong(KEY_LAST_USED, TIMESTAMP_INVALID); | 367 return mPreferences.getLong(KEY_LAST_USED, LAST_USED_INVALID); |
358 } | 368 } |
359 | 369 |
360 /** | 370 /** |
361 * Update the information associated with the web app with the specified dat
a. Used for testing. | 371 * Update the information associated with the web app with the specified dat
a. Used for testing. |
362 * @param splashScreenImage The image encoded as a string which should be sh
own on the splash | 372 * @param splashScreenImage The image encoded as a string which should be sh
own on the splash |
363 * screen of the web app. | 373 * screen of the web app. |
364 */ | 374 */ |
365 @VisibleForTesting | 375 @VisibleForTesting |
366 void updateSplashScreenImageForTests(String splashScreenImage) { | 376 void updateSplashScreenImageForTests(String splashScreenImage) { |
367 mPreferences.edit().putString(KEY_SPLASH_ICON, splashScreenImage).apply(
); | 377 mPreferences.edit().putString(KEY_SPLASH_ICON, splashScreenImage).apply(
); |
(...skipping 21 matching lines...) Expand all Loading... |
389 mPreferences.edit() | 399 mPreferences.edit() |
390 .putLong(KEY_LAST_CHECK_WEB_MANIFEST_UPDATE_TIME, sClock.current
TimeMillis()) | 400 .putLong(KEY_LAST_CHECK_WEB_MANIFEST_UPDATE_TIME, sClock.current
TimeMillis()) |
391 .apply(); | 401 .apply(); |
392 } | 402 } |
393 | 403 |
394 /** | 404 /** |
395 * Returns the completion time of the last check for whether the WebAPK's We
b Manifest was | 405 * Returns the completion time of the last check for whether the WebAPK's We
b Manifest was |
396 * updated. This time needs to be set when the WebAPK is registered. | 406 * updated. This time needs to be set when the WebAPK is registered. |
397 */ | 407 */ |
398 private long getLastCheckForWebManifestUpdateTime() { | 408 private long getLastCheckForWebManifestUpdateTime() { |
399 return mPreferences.getLong(KEY_LAST_CHECK_WEB_MANIFEST_UPDATE_TIME, TIM
ESTAMP_INVALID); | 409 return mPreferences.getLong(KEY_LAST_CHECK_WEB_MANIFEST_UPDATE_TIME, LAS
T_USED_INVALID); |
400 } | 410 } |
401 | 411 |
402 /** | 412 /** |
403 * Updates when the last WebAPK update request finished (successfully or uns
uccessfully). | 413 * Updates when the last WebAPK update request finished (successfully or uns
uccessfully). |
404 */ | 414 */ |
405 void updateTimeOfLastWebApkUpdateRequestCompletion() { | 415 void updateTimeOfLastWebApkUpdateRequestCompletion() { |
406 mPreferences.edit() | 416 mPreferences.edit() |
407 .putLong(KEY_LAST_UPDATE_REQUEST_COMPLETE_TIME, sClock.currentTi
meMillis()) | 417 .putLong(KEY_LAST_UPDATE_REQUEST_COMPLETE_TIME, sClock.currentTi
meMillis()) |
408 .apply(); | 418 .apply(); |
409 } | 419 } |
410 | 420 |
411 /** | 421 /** |
412 * Returns when the last WebAPK update request completed (successfully or un
successfully). | 422 * Returns when the last WebAPK update request completed (successfully or un
successfully). |
413 * This time needs to be set when the WebAPK is registered. | 423 * This time needs to be set when the WebAPK is registered. |
414 */ | 424 */ |
415 long getLastWebApkUpdateRequestCompletionTime() { | 425 long getLastWebApkUpdateRequestCompletionTime() { |
416 return mPreferences.getLong(KEY_LAST_UPDATE_REQUEST_COMPLETE_TIME, TIMES
TAMP_INVALID); | 426 return mPreferences.getLong(KEY_LAST_UPDATE_REQUEST_COMPLETE_TIME, LAST_
USED_INVALID); |
417 } | 427 } |
418 | 428 |
419 /** | 429 /** |
420 * Updates whether the last update request to WebAPK Server succeeded. | 430 * Updates whether the last update request to WebAPK Server succeeded. |
421 */ | 431 */ |
422 void updateDidLastWebApkUpdateRequestSucceed(boolean success) { | 432 void updateDidLastWebApkUpdateRequestSucceed(boolean success) { |
423 mPreferences.edit().putBoolean(KEY_DID_LAST_UPDATE_REQUEST_SUCCEED, succ
ess).apply(); | 433 mPreferences.edit().putBoolean(KEY_DID_LAST_UPDATE_REQUEST_SUCCEED, succ
ess).apply(); |
424 } | 434 } |
425 | 435 |
426 /** | 436 /** |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 int getLastRequestedShellApkVersion() { | 478 int getLastRequestedShellApkVersion() { |
469 return mPreferences.getInt(KEY_LAST_REQUESTED_SHELL_APK_VERSION, DEFAULT
_SHELL_APK_VERSION); | 479 return mPreferences.getInt(KEY_LAST_REQUESTED_SHELL_APK_VERSION, DEFAULT
_SHELL_APK_VERSION); |
470 } | 480 } |
471 | 481 |
472 /** | 482 /** |
473 * Returns whether the previous WebAPK update attempt succeeded. Returns tru
e if there has not | 483 * Returns whether the previous WebAPK update attempt succeeded. Returns tru
e if there has not |
474 * been any update attempts. | 484 * been any update attempts. |
475 */ | 485 */ |
476 boolean didPreviousUpdateSucceed() { | 486 boolean didPreviousUpdateSucceed() { |
477 long lastUpdateCompletionTime = getLastWebApkUpdateRequestCompletionTime
(); | 487 long lastUpdateCompletionTime = getLastWebApkUpdateRequestCompletionTime
(); |
478 if (lastUpdateCompletionTime == TIMESTAMP_INVALID) { | 488 if (lastUpdateCompletionTime == WebappDataStorage.LAST_USED_INVALID) { |
479 return true; | 489 return true; |
480 } | 490 } |
481 return getDidLastWebApkUpdateRequestSucceed(); | 491 return getDidLastWebApkUpdateRequestSucceed(); |
482 } | 492 } |
483 | 493 |
484 /** Sets whether we should check for updates less frequently. */ | 494 /** Sets whether we should check for updates less frequently. */ |
485 void setRelaxedUpdates(boolean relaxUpdates) { | 495 void setRelaxedUpdates(boolean relaxUpdates) { |
486 mPreferences.edit().putBoolean(KEY_RELAX_UPDATES, relaxUpdates).apply(); | 496 mPreferences.edit().putBoolean(KEY_RELAX_UPDATES, relaxUpdates).apply(); |
487 } | 497 } |
488 | 498 |
(...skipping 18 matching lines...) Expand all Loading... |
507 protected WebappDataStorage(String webappId) { | 517 protected WebappDataStorage(String webappId) { |
508 mId = webappId; | 518 mId = webappId; |
509 mPreferences = ContextUtils.getApplicationContext().getSharedPreferences
( | 519 mPreferences = ContextUtils.getApplicationContext().getSharedPreferences
( |
510 SHARED_PREFS_FILE_PREFIX + webappId, Context.MODE_PRIVATE); | 520 SHARED_PREFS_FILE_PREFIX + webappId, Context.MODE_PRIVATE); |
511 } | 521 } |
512 | 522 |
513 private boolean isEmpty() { | 523 private boolean isEmpty() { |
514 return mPreferences.getAll().isEmpty(); | 524 return mPreferences.getAll().isEmpty(); |
515 } | 525 } |
516 } | 526 } |
OLD | NEW |