Replace the only use of media::MockStatisticsCB with a bound method.
[chromium-blink-merge.git] / base / android / application_status_listener.cc
blobf48707282f52c765140ad3baae784fec43780f3e
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 {
14 struct LeakyLazyObserverListTraits :
15 base::internal::LeakyLazyInstanceTraits<
16 ObserverListThreadSafe<base::android::ApplicationStatusListener> > {
17 static ObserverListThreadSafe<base::android::ApplicationStatusListener>*
18 New(void* instance) {
19 ObserverListThreadSafe<base::android::ApplicationStatusListener>* ret =
20 base::internal::LeakyLazyInstanceTraits<ObserverListThreadSafe<
21 base::android::ApplicationStatusListener> >::New(instance);
22 // Leaky.
23 ret->AddRef();
24 return ret;
28 base::LazyInstance<ObserverListThreadSafe<
29 base::android::ApplicationStatusListener>,
30 LeakyLazyObserverListTraits> g_observers = LAZY_INSTANCE_INITIALIZER;
32 } // namespace
34 namespace base {
35 namespace android {
37 ApplicationStatusListener::ApplicationStatusListener(
38 const ApplicationStatusListener::ApplicationStateChangeCallback& callback)
39 : callback_(callback) {
40 DCHECK(!callback_.is_null());
41 g_observers.Get().AddObserver(this);
43 Java_ApplicationStatus_registerThreadSafeNativeApplicationStateListener(
44 base::android::AttachCurrentThread());
47 ApplicationStatusListener::~ApplicationStatusListener() {
48 g_observers.Get().RemoveObserver(this);
51 void ApplicationStatusListener::Notify(ApplicationState state) {
52 callback_.Run(state);
55 // static
56 bool ApplicationStatusListener::RegisterBindings(JNIEnv* env) {
57 return RegisterNativesImpl(env);
60 // static
61 void ApplicationStatusListener::NotifyApplicationStateChange(
62 ApplicationState state) {
63 g_observers.Get().Notify(&ApplicationStatusListener::Notify, state);
66 static void OnApplicationStateChange(JNIEnv* env,
67 jclass clazz,
68 jint new_state) {
69 ApplicationState application_state = static_cast<ApplicationState>(new_state);
70 ApplicationStatusListener::NotifyApplicationStateChange(application_state);
73 } // namespace android
74 } // namespace base