summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/sync/PersistedMetaGlobal.java
blob: d3467545c2a602d45bede864b41c80ebace4d959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.sync;

import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.net.AuthHeaderProvider;

import android.content.SharedPreferences;

public class PersistedMetaGlobal {
  public static final String LOG_TAG = "PersistedMetaGlobal";

  public static final String META_GLOBAL_SERVER_RESPONSE_BODY = "metaGlobalServerResponseBody";
  public static final String META_GLOBAL_LAST_MODIFIED        = "metaGlobalLastModified";

  protected SharedPreferences prefs;

  public PersistedMetaGlobal(SharedPreferences prefs) {
    this.prefs = prefs;
  }

  /**
   * Sets a <code>MetaGlobal</code> from persisted prefs.
   *
   * @param metaUrl
   *          meta/global server URL
   * @param credentials
   *          Sync credentials
   *
   * @return <MetaGlobal> set from previously fetched meta/global record from
   *         server
   */
  public MetaGlobal metaGlobal(String metaUrl, AuthHeaderProvider authHeaderProvider) {
    String json = prefs.getString(META_GLOBAL_SERVER_RESPONSE_BODY, null);
    if (json == null) {
      return null;
    }
    MetaGlobal metaGlobal = null;
    try {
      CryptoRecord cryptoRecord = CryptoRecord.fromJSONRecord(json);
      MetaGlobal mg = new MetaGlobal(metaUrl, authHeaderProvider);
      mg.setFromRecord(cryptoRecord);
      metaGlobal = mg;
    } catch (Exception e) {
      Logger.warn(LOG_TAG, "Got exception decrypting persisted meta/global.", e);
    }
    return metaGlobal;
  }

  public void persistMetaGlobal(MetaGlobal metaGlobal) {
    if (metaGlobal == null) {
      Logger.debug(LOG_TAG, "Clearing persisted meta/global.");
      prefs.edit().remove(META_GLOBAL_SERVER_RESPONSE_BODY).commit();
      return;
    }
    try {
      CryptoRecord cryptoRecord = metaGlobal.asCryptoRecord();
      String json = cryptoRecord.toJSONString();
      Logger.debug(LOG_TAG, "Persisting meta/global.");
      prefs.edit().putString(META_GLOBAL_SERVER_RESPONSE_BODY, json).commit();
    } catch (Exception e) {
      Logger.warn(LOG_TAG, "Got exception encrypting while persisting meta/global.", e);
    }
  }

  public long lastModified() {
    return prefs.getLong(META_GLOBAL_LAST_MODIFIED, -1);
  }

  public void persistLastModified(long lastModified) {
    if (lastModified <= 0) {
      Logger.debug(LOG_TAG, "Clearing persisted meta/global last modified timestamp.");
      prefs.edit().remove(META_GLOBAL_LAST_MODIFIED).commit();
      return;
    }
    Logger.debug(LOG_TAG, "Persisting meta/global last modified timestamp " + lastModified + ".");
    prefs.edit().putLong(META_GLOBAL_LAST_MODIFIED, lastModified).commit();
  }

  public void purge() {
    persistLastModified(-1);
    persistMetaGlobal(null);
  }
}