Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 1 | // Copyright (C) 2016 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | (function() { |
| 15 | 'use strict'; |
| 16 | |
| 17 | // Date cutoff is one day: |
Logan Hanks | d2497fb | 2016-06-15 11:57:36 -0700 | [diff] [blame] | 18 | var DRAFT_MAX_AGE = 24 * 60 * 60 * 1000; |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 19 | |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 20 | // Clean up old entries no more frequently than one day. |
Logan Hanks | d2497fb | 2016-06-15 11:57:36 -0700 | [diff] [blame] | 21 | var CLEANUP_THROTTLE_INTERVAL = 24 * 60 * 60 * 1000; |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 22 | |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 23 | Polymer({ |
| 24 | is: 'gr-storage', |
| 25 | |
| 26 | properties: { |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 27 | _lastCleanup: Number, |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 28 | _storage: { |
| 29 | type: Object, |
| 30 | value: function() { |
| 31 | return window.localStorage; |
| 32 | }, |
| 33 | }, |
| 34 | }, |
| 35 | |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 36 | getDraftComment: function(location) { |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 37 | this._cleanupDrafts(); |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 38 | return this._getObject(this._getDraftKey(location)); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 39 | }, |
| 40 | |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 41 | setDraftComment: function(location, message) { |
| 42 | var key = this._getDraftKey(location); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 43 | this._setObject(key, {message: message, updated: Date.now()}); |
| 44 | }, |
| 45 | |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 46 | eraseDraftComment: function(location) { |
| 47 | var key = this._getDraftKey(location); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 48 | this._storage.removeItem(key); |
| 49 | }, |
| 50 | |
Viktar Donich | 5008b44 | 2016-06-08 12:37:31 -0700 | [diff] [blame] | 51 | getPreferences: function() { |
| 52 | return this._getObject('localPrefs'); |
| 53 | }, |
| 54 | |
| 55 | savePreferences: function(localPrefs) { |
| 56 | this._setObject('localPrefs', localPrefs || null); |
| 57 | }, |
| 58 | |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 59 | _getDraftKey: function(location) { |
| 60 | return ['draft', location.changeNum, location.patchNum, location.path, |
Wyatt Allen | 4f4b3a7 | 2016-07-28 12:05:53 -0700 | [diff] [blame^] | 61 | location.line || ''].join(':'); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 62 | }, |
| 63 | |
| 64 | _cleanupDrafts: function() { |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 65 | // Throttle cleanup to the throttle interval. |
| 66 | if (this._lastCleanup && |
| 67 | Date.now() - this._lastCleanup < CLEANUP_THROTTLE_INTERVAL) { |
| 68 | return; |
| 69 | } |
| 70 | this._lastCleanup = Date.now(); |
| 71 | |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 72 | var draft; |
| 73 | for (var key in this._storage) { |
| 74 | if (key.indexOf('draft:') === 0) { |
| 75 | draft = this._getObject(key); |
| 76 | if (Date.now() - draft.updated > DRAFT_MAX_AGE) { |
| 77 | this._storage.removeItem(key); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | }, |
| 82 | |
| 83 | _getObject: function(key) { |
| 84 | var serial = this._storage.getItem(key); |
| 85 | if (!serial) { return null; } |
| 86 | return JSON.parse(serial); |
| 87 | }, |
| 88 | |
| 89 | _setObject: function(key, obj) { |
| 90 | this._storage.setItem(key, JSON.stringify(obj)); |
| 91 | }, |
| 92 | }); |
| 93 | })(); |