1// Copyright 2013 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
5package org.chromium.sync.notifier;
6
7/**
8 * An injectable singleton that provides an invalidation client with an appropriate unique name.
9 *
10 * This singleton will always provide a somewhat reasonable name.  With proper support from outside
11 * components, it will be able to provide a name that is consistent across restarts.
12 */
13public class InvalidationClientNameProvider {
14    private static final Object LOCK = new Object();
15
16    private static InvalidationClientNameProvider sInstance;
17
18    private final Object mLock;
19
20    private InvalidationClientNameGenerator mGenerator;
21
22    private byte[] mUniqueId;
23
24    public static InvalidationClientNameProvider get() {
25        synchronized (LOCK) {
26            if (sInstance == null) {
27                sInstance = new InvalidationClientNameProvider();
28            }
29            return sInstance;
30        }
31    }
32
33    InvalidationClientNameProvider() {
34        mLock = new Object();
35        mGenerator = new RandomizedInvalidationClientNameGenerator();
36    }
37
38    /** Returns a consistent unique string of bytes for use as an invalidator client ID. */
39    public byte[] getInvalidatorClientName() {
40        synchronized (mLock) {
41            if (mUniqueId == null) {
42                mUniqueId = mGenerator.generateInvalidatorClientName();
43            }
44            return mUniqueId;
45        }
46    }
47
48    public void setPreferredClientNameGenerator(InvalidationClientNameGenerator generator) {
49        mGenerator = generator;
50    }
51}
52