Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / keyed_service / core / refcounted_keyed_service.h
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.
4
5 #ifndef COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_H_
6 #define COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_H_
7
8 #include "base/memory/ref_counted.h"
9 #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"
12
13 class RefcountedKeyedService;
14
15 namespace impl {
16
17 struct KEYED_SERVICE_EXPORT RefcountedKeyedServiceTraits {
18   static void Destruct(const RefcountedKeyedService* obj);
19 };
20
21 }  // namespace impl
22
23 // Base class for refcounted objects that hang off the BrowserContext.
24 //
25 // The two pass shutdown described in KeyedService works a bit differently
26 // because there could be outstanding references on other threads.
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
29 // be after the corresponding BrowserContext has been destroyed.
30 //
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
33 // can't use content::DeleteOnThread<> directly because RefcountedKeyedService
34 // must not depend on //content.
35 class KEYED_SERVICE_EXPORT RefcountedKeyedService
36     : public base::RefCountedThreadSafe<RefcountedKeyedService,
37                                         impl::RefcountedKeyedServiceTraits> {
38  public:
39   // Unlike KeyedService, ShutdownOnUI is not optional. You must do something
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
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.
44   virtual void ShutdownOnUIThread() = 0;
45
46  protected:
47   // If your service does not need to be deleted on a specific thread, use the
48   // default constructor.
49   RefcountedKeyedService();
50
51   // If you need your service to be deleted on a specific thread (for example,
52   // you're converting a service that used content::DeleteOnThread<IO>), then
53   // use this constructor with a reference to the SingleThreadTaskRunner (you
54   // can get it from content::BrowserThread::GetMessageLoopProxyForThread).
55   explicit RefcountedKeyedService(
56       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
57
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.
60   virtual ~RefcountedKeyedService();
61
62  private:
63   friend struct impl::RefcountedKeyedServiceTraits;
64   friend class base::DeleteHelper<RefcountedKeyedService>;
65   friend class base::RefCountedThreadSafe<RefcountedKeyedService,
66                                           impl::RefcountedKeyedServiceTraits>;
67
68   // Do we have to delete this object on a specific thread?
69   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
70 };
71
72 #endif  // COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_H_