Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / devtools_bridge / android / java / src / org / chromium / components / devtools_bridge / gcd / InstanceCredential.java
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.components.devtools_bridge.gcd;
6
7 import android.content.SharedPreferences;
8
9 /**
10  * Information provided by GCD when instance has registered. Instance id
11  * can be used for:
12  * 1. Making sure incoming messages are addressed to the instance.
13  * 2. It needed when sending command results to GCD.
14  * 3. For device unregistration.
15  *
16  * The Secret supposed to be used to authenticate the instance with OAuthClient
17  * (it has no user credentials).
18  */
19 public final class InstanceCredential {
20     public static final String PREF_ID = "gcd.ID";
21     public static final String PREF_SECRET = "gcd.SECRET";
22
23     public final String id;
24     public final String secret;
25
26     public InstanceCredential(String id, String secret) {
27         assert id != null;
28         assert secret != null;
29
30         this.id = id;
31         this.secret = secret;
32     }
33
34     public static InstanceCredential get(SharedPreferences preferences) {
35         String id = preferences.getString(PREF_ID, null);
36         String secret = preferences.getString(PREF_SECRET, null);
37         return id != null && secret != null ? new InstanceCredential(id, secret) : null;
38     }
39
40     public void put(SharedPreferences.Editor editor) {
41         editor.putString(PREF_ID, id);
42         editor.putString(PREF_SECRET, secret);
43     }
44
45     public static void remove(SharedPreferences.Editor editor) {
46         editor.remove(PREF_ID);
47         editor.remove(PREF_SECRET);
48     }
49 }