Remove use of JSONReader::DeprecatedRead from activity_log
[chromium-blink-merge.git] / base / android / application_status_listener.cc
blob3e6fbf833ceb98e8bca020750ce078b34eba34b8
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.
5 #include "base/android/application_status_listener.h"
7 #include <jni.h>
9 #include "base/lazy_instance.h"
10 #include "base/observer_list_threadsafe.h"
11 #include "jni/ApplicationStatus_jni.h"
13 namespace base {
14 namespace android {
16 namespace {
18 struct LeakyLazyObserverListTraits :
19 base::internal::LeakyLazyInstanceTraits<
20 ObserverListThreadSafe<ApplicationStatusListener> > {
21 static ObserverListThreadSafe<ApplicationStatusListener>*
22 New(void* instance) {
23 ObserverListThreadSafe<ApplicationStatusListener>* ret =
24 base::internal::LeakyLazyInstanceTraits<ObserverListThreadSafe<
25 ApplicationStatusListener>>::New(instance);
26 // Leaky.
27 ret->AddRef();
28 return ret;
32 LazyInstance<ObserverListThreadSafe<ApplicationStatusListener>,
33 LeakyLazyObserverListTraits> g_observers =
34 LAZY_INSTANCE_INITIALIZER;
36 } // namespace
38 ApplicationStatusListener::ApplicationStatusListener(
39 const ApplicationStatusListener::ApplicationStateChangeCallback& callback)
40 : callback_(callback) {
41 DCHECK(!callback_.is_null());
42 g_observers.Get().AddObserver(this);
44 Java_ApplicationStatus_registerThreadSafeNativeApplicationStateListener(
45 AttachCurrentThread());
48 ApplicationStatusListener::~ApplicationStatusListener() {
49 g_observers.Get().RemoveObserver(this);
52 void ApplicationStatusListener::Notify(ApplicationState state) {
53 callback_.Run(state);
56 // static
57 bool ApplicationStatusListener::RegisterBindings(JNIEnv* env) {
58 return RegisterNativesImpl(env);
61 // static
62 void ApplicationStatusListener::NotifyApplicationStateChange(
63 ApplicationState state) {
64 g_observers.Get().Notify(FROM_HERE, &ApplicationStatusListener::Notify,
65 state);
68 static void OnApplicationStateChange(JNIEnv* env,
69 jclass clazz,
70 jint new_state) {
71 ApplicationState application_state = static_cast<ApplicationState>(new_state);
72 ApplicationStatusListener::NotifyApplicationStateChange(application_state);
75 } // namespace android
76 } // namespace base