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 #ifndef BASE_ANDROID_ACTIVITY_STATUS_H_ | |
6 #define BASE_ANDROID_ACTIVITY_STATUS_H_ | |
7 | |
8 #include <jni.h> | |
9 | |
10 #include "base/android/jni_android.h" | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/singleton.h" | |
14 #include "base/observer_list_threadsafe.h" | |
15 | |
16 template <typename T> struct LeakySingletonTraits; | |
17 | |
18 namespace base { | |
19 namespace android { | |
20 | |
21 // Define activity state values like ACTIVITY_STATE_CREATED in a | |
22 // way that ensures they're always the same than their Java counterpart. | |
23 enum ActivityState { | |
24 #define DEFINE_ACTIVITY_STATE(x,y) ACTIVITY_STATE_ ## x = y, | |
25 #include "base/android/activity_state_list.h" | |
26 #undef DEFINE_ACTIVITY_STATE | |
27 }; | |
28 | |
29 // A native helper class to listen to activity state changes from | |
30 // any thread. | |
31 // | |
32 // To start listening, create a new instance, passing a callback to a | |
33 // function that takes an ActivityState parameter. To stop listening, | |
34 // simply delete the listener object. The implementation guarantees | |
35 // that the callback will always be called on the same thread than | |
36 // the one that created the listener. | |
37 // | |
38 // Example: | |
39 // | |
40 // void OnActivityStateChange(ActivityState state) { | |
41 // ... | |
42 // } | |
43 // | |
44 // // Start listening. | |
45 // ActivityStatus::Listener* my_listener = | |
46 // new ActivityStatus::Listener(base::Bind(&OnActivityStateChange)); | |
47 // | |
48 // ... | |
49 // | |
50 // // Stop listening. | |
51 // delete my_listener | |
52 // | |
53 BASE_EXPORT class ActivityStatus { | |
54 public: | |
55 typedef base::Callback<void(ActivityState)> StateChangeCallback; | |
56 | |
57 class Listener { | |
58 public: | |
59 Listener(const StateChangeCallback& callback); | |
bulach
2013/04/26 12:57:06
nit: explicit
digit1
2013/04/26 13:25:55
Done.
| |
60 ~Listener(); | |
bulach
2013/04/26 12:57:06
nit: add a \n
digit1
2013/04/26 13:25:55
Done.
| |
61 private: | |
62 friend class ActivityStatus; | |
63 | |
64 void Notify(ActivityState state) { | |
65 callback_.Run(state); | |
bulach
2013/04/26 12:57:06
nit: I think the preference is to just have decl i
digit1
2013/04/26 13:25:55
Done.
| |
66 } | |
67 | |
68 StateChangeCallback callback_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(Listener); | |
71 }; | |
72 | |
73 // NOTE: The Java ActivityStatus is a singleton too. | |
74 static ActivityStatus* GetInstance(); | |
75 | |
76 // Internal use: must be public to be called from base_jni_registrar.cc | |
77 static bool RegisterBindings(JNIEnv* env); | |
78 | |
79 // Internal use only: must be public to be called from JNI. | |
80 void OnActivityStateChange(ActivityState new_state); | |
81 | |
82 private: | |
83 friend struct DefaultSingletonTraits<ActivityStatus>; | |
84 | |
85 ActivityStatus(); | |
86 ~ActivityStatus(); | |
87 | |
88 void RegisterListener(Listener* listener); | |
89 void UnregisterListener(Listener* listener); | |
90 | |
91 scoped_refptr<ObserverListThreadSafe<Listener> > observers_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(ActivityStatus); | |
94 }; | |
95 | |
96 } // namespace android | |
97 } // namespace base | |
98 | |
99 #endif // BASE_ANDROID_ACTIVITY_STATUS_H_ | |
OLD | NEW |