| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_BROWSER_CONTEXT_KEYED_SERVICE_REFCOUNTED_BROWSER_CONTEXT_KEYE
D_SERVICE_H_ | 5 #ifndef COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_REFCOUNTED_BROWSER_CONTEXT_KEYE
D_SERVICE_H_ |
| 6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_REFCOUNTED_BROWSER_CONTEXT_KEYE
D_SERVICE_H_ | 6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_REFCOUNTED_BROWSER_CONTEXT_KEYE
D_SERVICE_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/sequenced_task_runner_helpers.h" | 9 #include "base/sequenced_task_runner_helpers.h" |
| 10 #include "components/browser_context_keyed_service/browser_context_keyed_service
_export.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 | 12 |
| 12 class RefcountedBrowserContextKeyedService; | 13 class RefcountedBrowserContextKeyedService; |
| 13 | 14 |
| 14 namespace impl { | 15 namespace impl { |
| 15 | 16 |
| 16 struct RefcountedBrowserContextKeyedServiceTraits { | 17 struct BROWSER_CONTEXT_KEYED_SERVICE_EXPORT |
| 18 RefcountedBrowserContextKeyedServiceTraits { |
| 17 static void Destruct(const RefcountedBrowserContextKeyedService* obj); | 19 static void Destruct(const RefcountedBrowserContextKeyedService* obj); |
| 18 }; | 20 }; |
| 19 | 21 |
| 20 } // namespace impl | 22 } // namespace impl |
| 21 | 23 |
| 22 // Base class for refcounted objects that hang off the BrowserContext. | 24 // Base class for refcounted objects that hang off the BrowserContext. |
| 23 // | 25 // |
| 24 // The two pass shutdown described in BrowserContextKeyedService works a bit | 26 // The two pass shutdown described in BrowserContextKeyedService works a bit |
| 25 // differently because there could be outstanding references on other | 27 // differently because there could be outstanding references on other |
| 26 // threads. ShutdownOnUIThread() will be called on the UI thread, and then the | 28 // threads. ShutdownOnUIThread() will be called on the UI thread, and then the |
| 27 // destructor will run when the last reference is dropped, which may or may not | 29 // destructor will run when the last reference is dropped, which may or may not |
| 28 // be after the corresponding BrowserContext has been destroyed. | 30 // be after the corresponding BrowserContext has been destroyed. |
| 29 // | 31 // |
| 30 // Optionally, if you initialize your service with the constructor that takes a | 32 // Optionally, if you initialize your service with the constructor that takes a |
| 31 // thread ID, your service will be deleted on that thread. We can't use | 33 // thread ID, your service will be deleted on that thread. We can't use |
| 32 // content::DeleteOnThread<> directly because | 34 // content::DeleteOnThread<> directly because |
| 33 // RefcountedBrowserContextKeyedService must be one type that | 35 // RefcountedBrowserContextKeyedService must be one type that |
| 34 // RefcountedBrowserContextKeyedServiceFactory can use. | 36 // RefcountedBrowserContextKeyedServiceFactory can use. |
| 35 class RefcountedBrowserContextKeyedService | 37 class BROWSER_CONTEXT_KEYED_SERVICE_EXPORT RefcountedBrowserContextKeyedService |
| 36 : public base::RefCountedThreadSafe< | 38 : public base::RefCountedThreadSafe< |
| 37 RefcountedBrowserContextKeyedService, | 39 RefcountedBrowserContextKeyedService, |
| 38 impl::RefcountedBrowserContextKeyedServiceTraits> { | 40 impl::RefcountedBrowserContextKeyedServiceTraits> { |
| 39 public: | 41 public: |
| 40 // Unlike BrowserContextKeyedService, ShutdownOnUI is not optional. You must | 42 // Unlike BrowserContextKeyedService, ShutdownOnUI is not optional. You must |
| 41 // do something to drop references during the first pass Shutdown() because | 43 // do something to drop references during the first pass Shutdown() because |
| 42 // this is the only point where you are guaranteed that something is running | 44 // this is the only point where you are guaranteed that something is running |
| 43 // on the UI thread. The PKSF framework will ensure that this is only called | 45 // on the UI thread. The PKSF framework will ensure that this is only called |
| 44 // on the UI thread; you do not need to check for that yourself. | 46 // on the UI thread; you do not need to check for that yourself. |
| 45 virtual void ShutdownOnUIThread() = 0; | 47 virtual void ShutdownOnUIThread() = 0; |
| 46 | 48 |
| 47 protected: | 49 protected: |
| 48 // If your service does not need to be deleted on a specific thread, use the | 50 // If your service does not need to be deleted on a specific thread, use the |
| 49 // default constructor. | 51 // default constructor. |
| 50 RefcountedBrowserContextKeyedService(); | 52 RefcountedBrowserContextKeyedService(); |
| 51 | 53 |
| 52 // If you need your service to be deleted on a specific thread (for example, | 54 // If you need your service to be deleted on a specific thread (for example, |
| 53 // you're converting a service that used content::DeleteOnThread<IO>), then | 55 // you're converting a service that used content::DeleteOnThread<IO>), then |
| 54 // use this constructor with the ID of the thread. | 56 // use this constructor with the ID of the thread. |
| 55 explicit RefcountedBrowserContextKeyedService( | 57 explicit RefcountedBrowserContextKeyedService( |
| 56 const content::BrowserThread::ID thread_id); | 58 const content::BrowserThread::ID thread_id); |
| 57 | 59 |
| 58 // The second pass destruction can happen anywhere unless you specify which | 60 // The second pass destruction can happen anywhere unless you specify which |
| 59 // thread this service must be destroyed on by using the second constructor. | 61 // thread this service must be destroyed on by using the second constructor. |
| 60 virtual ~RefcountedBrowserContextKeyedService(); | 62 virtual ~RefcountedBrowserContextKeyedService(); |
| 61 | 63 |
| 62 private: | 64 private: |
| 63 friend struct impl::RefcountedBrowserContextKeyedServiceTraits; | 65 friend struct impl::RefcountedBrowserContextKeyedServiceTraits; |
| 64 friend class base::DeleteHelper<RefcountedBrowserContextKeyedService>; | 66 friend class base::DeleteHelper<RefcountedBrowserContextKeyedService>; |
| 67 friend class base::RefCountedThreadSafe<RefcountedBrowserContextKeyedService, |
| 68 impl::RefcountedBrowserContextKeyedServiceTraits>; |
| 65 | 69 |
| 66 // Do we have to delete this object on a specific thread? | 70 // Do we have to delete this object on a specific thread? |
| 67 bool requires_destruction_on_thread_; | 71 bool requires_destruction_on_thread_; |
| 68 content::BrowserThread::ID thread_id_; | 72 content::BrowserThread::ID thread_id_; |
| 69 }; | 73 }; |
| 70 | 74 |
| 71 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_REFCOUNTED_BROWSER_CONTEXT_K
EYED_SERVICE_H_ | 75 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_REFCOUNTED_BROWSER_CONTEXT_K
EYED_SERVICE_H_ |
| OLD | NEW |