OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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_PROFILE_SERVICE_LEVELDB_THREAD_H_ | |
6 #define COMPONENTS_PROFILE_SERVICE_LEVELDB_THREAD_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/threading/thread.h" | |
12 #include "components/leveldb/public/interfaces/leveldb.mojom.h" | |
13 #include "mojo/public/cpp/bindings/binding_set.h" | |
14 #include "mojo/shell/public/cpp/message_loop_ref.h" | |
15 | |
16 namespace profile { | |
17 | |
18 // A separate thread for leveldb. | |
19 // | |
20 // In the profile application, we need to put leveldb service on its own thread | |
21 // because otherwise it can deadlock with the profile service. (The leveldb | |
22 // service asks the profile service for a directory and blocks, but if we | |
23 // didn't put leveldb on its own thread, it would block its own thread.) | |
24 class LevelDBThread : public base::Thread, | |
25 public base::RefCountedThreadSafe<LevelDBThread> { | |
26 public: | |
27 LevelDBThread(scoped_ptr<mojo::MessageLoopRef> message_loop_ref, | |
28 leveldb::LevelDBServiceRequest request); | |
29 | |
30 // Passes |request| from the caller thread to our LevelDBThread and | |
31 // binds |request| to the leveldb service thread. | |
32 void BindNewRequest(leveldb::LevelDBServiceRequest request); | |
33 | |
34 private: | |
35 friend class base::RefCountedThreadSafe<LevelDBThread>; | |
36 ~LevelDBThread() override; | |
37 | |
38 // Check on each connection error whether we have no more connections. | |
39 void OnServiceError(); | |
40 | |
41 // Implementation of BindNewRequest run on the LevelDBThread. | |
42 void BindNewRequestImpl(leveldb::LevelDBServiceRequest request); | |
43 | |
44 // Overridden from base::SimpleThread: | |
45 void Init() override; | |
46 void CleanUp() override; | |
47 | |
48 // Hold a reference to the profile service's message loop to keep it alive as | |
49 // long as there are LevelDB service bindings. Set on the parent thread; | |
50 // destroyed on the child thread. | |
51 scoped_ptr<mojo::MessageLoopRef> profile_service_message_loop_ref_; | |
52 | |
53 // The lifetime of the leveldb thread's message loop is tied to the lifetime | |
54 // of the level db service it vends. | |
55 mojo::MessageLoopRefFactory thread_message_loop_ref_; | |
56 | |
57 // The service shared between all requestors. | |
58 scoped_ptr<leveldb::LevelDBService> leveldb_service_; | |
59 | |
60 // All outstanding references to the LevelDBService. | |
61 scoped_ptr<mojo::BindingSet<leveldb::LevelDBService>> leveldb_bindings_; | |
62 | |
63 // Used to pass the initial request from the parent thread to the child | |
64 // thread. | |
65 leveldb::LevelDBServiceRequest initial_leveldb_request_; | |
michaeln
2016/03/16 03:11:13
Is the second path to establish a binding needed?
| |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(LevelDBThread); | |
68 }; | |
69 | |
70 } // namespace profile | |
71 | |
72 #endif // COMPONENTS_PROFILE_SERVICE_LEVELDB_THREAD_H_ | |
OLD | NEW |