OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_H_ | 5 #ifndef COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_H_ |
6 #define COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_H_ | 6 #define COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_H_ |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/sequenced_task_runner.h" |
9 #include "base/sequenced_task_runner_helpers.h" | 10 #include "base/sequenced_task_runner_helpers.h" |
10 #include "base/single_thread_task_runner.h" | |
11 #include "components/keyed_service/core/keyed_service_export.h" | 11 #include "components/keyed_service/core/keyed_service_export.h" |
12 | 12 |
13 class RefcountedKeyedService; | 13 class RefcountedKeyedService; |
14 | 14 |
15 namespace impl { | 15 namespace impl { |
16 | 16 |
17 struct KEYED_SERVICE_EXPORT RefcountedKeyedServiceTraits { | 17 struct KEYED_SERVICE_EXPORT RefcountedKeyedServiceTraits { |
18 static void Destruct(const RefcountedKeyedService* obj); | 18 static void Destruct(const RefcountedKeyedService* obj); |
19 }; | 19 }; |
20 | 20 |
21 } // namespace impl | 21 } // namespace impl |
22 | 22 |
23 // Base class for refcounted objects that hang off the BrowserContext. | 23 // Base class for refcounted objects that hang off the BrowserContext. |
24 // | 24 // |
25 // The two pass shutdown described in KeyedService works a bit differently | 25 // The two pass shutdown described in KeyedService works a bit differently |
26 // because there could be outstanding references on other threads. | 26 // because there could be outstanding references on other threads. |
27 // ShutdownOnUIThread() will be called on the UI thread, and then the | 27 // ShutdownOnUIThread() will be called on the UI thread, and then the |
28 // destructor will run when the last reference is dropped, which may or may not | 28 // destructor will run when the last reference is dropped, which may or may not |
29 // be after the corresponding BrowserContext has been destroyed. | 29 // be after the corresponding BrowserContext has been destroyed. |
30 // | 30 // |
31 // Optionally, if you initialize your service with the constructor that takes a | 31 // Optionally, if you initialize your service with the constructor that takes a |
32 // single thread task runner, your service will be deleted on that thread. We | 32 // SequencedTaskRunner, your service will be deleted on that sequence. We |
33 // can't use content::DeleteOnThread<> directly because RefcountedKeyedService | 33 // can't use content::DeleteOnThread<> directly because RefcountedKeyedService |
34 // must not depend on //content. | 34 // must not depend on //content. |
35 class KEYED_SERVICE_EXPORT RefcountedKeyedService | 35 class KEYED_SERVICE_EXPORT RefcountedKeyedService |
36 : public base::RefCountedThreadSafe<RefcountedKeyedService, | 36 : public base::RefCountedThreadSafe<RefcountedKeyedService, |
37 impl::RefcountedKeyedServiceTraits> { | 37 impl::RefcountedKeyedServiceTraits> { |
38 public: | 38 public: |
39 // Unlike KeyedService, ShutdownOnUI is not optional. You must do something | 39 // Unlike KeyedService, ShutdownOnUI is not optional. You must do something |
40 // to drop references during the first pass Shutdown() because this is the | 40 // to drop references during the first pass Shutdown() because this is the |
41 // only point where you are guaranteed that something is running on the UI | 41 // only point where you are guaranteed that something is running on the UI |
42 // thread. The PKSF framework will ensure that this is only called on the UI | 42 // thread. The PKSF framework will ensure that this is only called on the UI |
43 // thread; you do not need to check for that yourself. | 43 // thread; you do not need to check for that yourself. |
44 virtual void ShutdownOnUIThread() = 0; | 44 virtual void ShutdownOnUIThread() = 0; |
45 | 45 |
46 protected: | 46 protected: |
47 // If your service does not need to be deleted on a specific thread, use the | 47 // If your service does not need to be deleted on a specific sequence, use the |
48 // default constructor. | 48 // default constructor. |
49 RefcountedKeyedService(); | 49 RefcountedKeyedService(); |
50 | 50 |
51 // If you need your service to be deleted on a specific thread (for example, | 51 // If you need your service to be deleted on a specific sequence (for example, |
52 // you're converting a service that used content::DeleteOnThread<IO>), then | 52 // you're converting a service that used content::DeleteOnThread<IO>), then |
53 // use this constructor with a reference to the SingleThreadTaskRunner (you | 53 // use this constructor with a reference to the SequencedTaskRunner (e.g., you |
54 // can get it from content::BrowserThread::GetTaskRunnerForThread). | 54 // can get it from content::BrowserThread::GetTaskRunnerForThread). |
55 explicit RefcountedKeyedService( | 55 explicit RefcountedKeyedService( |
56 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | 56 scoped_refptr<base::SequencedTaskRunner> task_runner); |
57 | 57 |
58 // The second pass destruction can happen anywhere unless you specify which | 58 // The second pass destruction can happen anywhere unless you specify which |
59 // thread this service must be destroyed on by using the second constructor. | 59 // sequence this service must be destroyed on by using the second constructor. |
60 virtual ~RefcountedKeyedService(); | 60 virtual ~RefcountedKeyedService(); |
61 | 61 |
62 private: | 62 private: |
63 friend struct impl::RefcountedKeyedServiceTraits; | 63 friend struct impl::RefcountedKeyedServiceTraits; |
64 friend class base::DeleteHelper<RefcountedKeyedService>; | 64 friend class base::DeleteHelper<RefcountedKeyedService>; |
65 friend class base::RefCountedThreadSafe<RefcountedKeyedService, | 65 friend class base::RefCountedThreadSafe<RefcountedKeyedService, |
66 impl::RefcountedKeyedServiceTraits>; | 66 impl::RefcountedKeyedServiceTraits>; |
67 | 67 |
68 // Do we have to delete this object on a specific thread? | 68 // Do we have to delete this object on a specific sequence? |
69 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 69 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
70 }; | 70 }; |
71 | 71 |
72 #endif // COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_H_ | 72 #endif // COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_H_ |
OLD | NEW |