blob: ff41a747781e0870d326a152ccd396535c581e71 [file] [log] [blame]
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -07001// 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 Hanksd2497fb2016-06-15 11:57:36 -070018 var DRAFT_MAX_AGE = 24 * 60 * 60 * 1000;
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070019
Wyatt Allen035c74f2016-05-23 13:53:10 -070020 // Clean up old entries no more frequently than one day.
Logan Hanksd2497fb2016-06-15 11:57:36 -070021 var CLEANUP_THROTTLE_INTERVAL = 24 * 60 * 60 * 1000;
Wyatt Allen035c74f2016-05-23 13:53:10 -070022
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070023 Polymer({
24 is: 'gr-storage',
25
26 properties: {
Wyatt Allen035c74f2016-05-23 13:53:10 -070027 _lastCleanup: Number,
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070028 _storage: {
29 type: Object,
30 value: function() {
31 return window.localStorage;
32 },
33 },
34 },
35
Wyatt Allen035c74f2016-05-23 13:53:10 -070036 getDraftComment: function(location) {
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070037 this._cleanupDrafts();
Wyatt Allen035c74f2016-05-23 13:53:10 -070038 return this._getObject(this._getDraftKey(location));
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070039 },
40
Wyatt Allen035c74f2016-05-23 13:53:10 -070041 setDraftComment: function(location, message) {
42 var key = this._getDraftKey(location);
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070043 this._setObject(key, {message: message, updated: Date.now()});
44 },
45
Wyatt Allen035c74f2016-05-23 13:53:10 -070046 eraseDraftComment: function(location) {
47 var key = this._getDraftKey(location);
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070048 this._storage.removeItem(key);
49 },
50
Viktar Donich5008b442016-06-08 12:37:31 -070051 getPreferences: function() {
52 return this._getObject('localPrefs');
53 },
54
55 savePreferences: function(localPrefs) {
56 this._setObject('localPrefs', localPrefs || null);
57 },
58
Wyatt Allen035c74f2016-05-23 13:53:10 -070059 _getDraftKey: function(location) {
60 return ['draft', location.changeNum, location.patchNum, location.path,
Wyatt Allen4f4b3a72016-07-28 12:05:53 -070061 location.line || ''].join(':');
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070062 },
63
64 _cleanupDrafts: function() {
Wyatt Allen035c74f2016-05-23 13:53:10 -070065 // 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 Allen7a4aa8c2016-05-18 12:37:53 -070072 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})();