OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "base/android/application_status_listener.h" |
| 6 |
| 7 #include <jni.h> |
| 8 |
| 9 #include "base/lazy_instance.h" |
| 10 #include "base/observer_list_threadsafe.h" |
| 11 #include "jni/ApplicationStatus_jni.h" |
| 12 |
| 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; |
| 25 } |
| 26 }; |
| 27 |
| 28 base::LazyInstance<ObserverListThreadSafe< |
| 29 base::android::ApplicationStatusListener>, |
| 30 LeakyLazyObserverListTraits> g_observers = LAZY_INSTANCE_INITIALIZER; |
| 31 |
| 32 } // namespace |
| 33 |
| 34 namespace base { |
| 35 namespace android { |
| 36 |
| 37 ApplicationStatusListener::ApplicationStatusListener( |
| 38 const ApplicationStatusListener::ApplicationStateChangeCallback& callback) |
| 39 : callback_(callback) { |
| 40 DCHECK(!callback_.is_null()); |
| 41 g_observers.Get().AddObserver(this); |
| 42 } |
| 43 |
| 44 ApplicationStatusListener::~ApplicationStatusListener() { |
| 45 g_observers.Get().RemoveObserver(this); |
| 46 } |
| 47 |
| 48 void ApplicationStatusListener::Notify(ApplicationState state) { |
| 49 callback_.Run(state); |
| 50 } |
| 51 |
| 52 // static |
| 53 bool ApplicationStatusListener::RegisterBindings(JNIEnv* env) { |
| 54 bool result = RegisterNativesImpl(env); |
| 55 if (result) |
| 56 Java_ApplicationStatus_registerThreadSafeNativeApplicationStateListener(env)
; |
| 57 return result; |
| 58 } |
| 59 |
| 60 // static |
| 61 void ApplicationStatusListener::NotifyApplicationStateChange( |
| 62 ApplicationState state) { |
| 63 g_observers.Get().Notify(&ApplicationStatusListener::Notify, state); |
| 64 } |
| 65 |
| 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); |
| 71 } |
| 72 |
| 73 } // namespace android |
| 74 } // namespace base |
OLD | NEW |