Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / invalidation / InvalidationServiceFactory.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.chrome.browser.invalidation;
6
7 import android.content.Context;
8
9 import org.chromium.base.JNINamespace;
10 import org.chromium.base.ThreadUtils;
11 import org.chromium.chrome.browser.profiles.Profile;
12 import org.chromium.components.invalidation.InvalidationService;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 /**
18  * InvalidationServiceFactory maps Profiles to instances of
19  * {@link InvalidationService} instances. Each {@link Profile} will at most
20  * have one instance of this service. If the service does not already exist,
21  * it will be created on the first access.
22  */
23 @JNINamespace("invalidation")
24 public final class InvalidationServiceFactory {
25
26     private static final Map<Profile, InvalidationService> sServiceMap =
27             new HashMap<Profile, InvalidationService>();
28
29     private InvalidationServiceFactory() {}
30
31     /**
32      * Returns Java InvalidationService for the given Profile.
33      */
34     public static InvalidationService getForProfile(Profile profile) {
35         ThreadUtils.assertOnUiThread();
36         InvalidationService service = sServiceMap.get(profile);
37         if (service == null) {
38             service = nativeGetForProfile(profile);
39             sServiceMap.put(profile, service);
40         }
41         return service;
42     }
43
44     public static InvalidationService getForTest(Context context) {
45         return nativeGetForTest(context);
46     }
47
48     private static native InvalidationService nativeGetForProfile(Profile profile);
49     private static native InvalidationService nativeGetForTest(Context context);
50 }