Index: components/profile_service/leveldb_thread.h |
diff --git a/components/profile_service/leveldb_thread.h b/components/profile_service/leveldb_thread.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8db464c76815d16ce9d9b76c7d77e607c14c6766 |
--- /dev/null |
+++ b/components/profile_service/leveldb_thread.h |
@@ -0,0 +1,72 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_PROFILE_SERVICE_LEVELDB_THREAD_H_ |
+#define COMPONENTS_PROFILE_SERVICE_LEVELDB_THREAD_H_ |
+ |
+#include "base/macros.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/threading/thread.h" |
+#include "components/leveldb/public/interfaces/leveldb.mojom.h" |
+#include "mojo/public/cpp/bindings/binding_set.h" |
+#include "mojo/shell/public/cpp/message_loop_ref.h" |
+ |
+namespace profile { |
+ |
+// A separate thread for leveldb. |
+// |
+// In the profile application, we need to put leveldb service on its own thread |
+// because otherwise it can deadlock with the profile service. (The leveldb |
+// service asks the profile service for a directory and blocks, but if we |
+// didn't put leveldb on its own thread, it would block its own thread.) |
+class LevelDBThread : public base::Thread, |
+ public base::RefCountedThreadSafe<LevelDBThread> { |
+ public: |
+ LevelDBThread(scoped_ptr<mojo::MessageLoopRef> message_loop_ref, |
+ leveldb::LevelDBServiceRequest request); |
+ |
+ // Passes |request| from the caller thread to our LevelDBThread and |
+ // binds |request| to the leveldb service thread. |
+ void BindNewRequest(leveldb::LevelDBServiceRequest request); |
+ |
+ private: |
+ friend class base::RefCountedThreadSafe<LevelDBThread>; |
+ ~LevelDBThread() override; |
+ |
+ // Check on each connection error whether we have no more connections. |
+ void OnServiceError(); |
+ |
+ // Implementation of BindNewRequest run on the LevelDBThread. |
+ void BindNewRequestImpl(leveldb::LevelDBServiceRequest request); |
+ |
+ // Overridden from base::SimpleThread: |
+ void Init() override; |
+ void CleanUp() override; |
+ |
+ // Hold a reference to the profile service's message loop to keep it alive as |
+ // long as there are LevelDB service bindings. Set on the parent thread; |
+ // destroyed on the child thread. |
+ scoped_ptr<mojo::MessageLoopRef> profile_service_message_loop_ref_; |
+ |
+ // The lifetime of the leveldb thread's message loop is tied to the lifetime |
+ // of the level db service it vends. |
+ mojo::MessageLoopRefFactory thread_message_loop_ref_; |
+ |
+ // The service shared between all requestors. |
+ scoped_ptr<leveldb::LevelDBService> leveldb_service_; |
+ |
+ // All outstanding references to the LevelDBService. |
+ scoped_ptr<mojo::BindingSet<leveldb::LevelDBService>> leveldb_bindings_; |
+ |
+ // Used to pass the initial request from the parent thread to the child |
+ // thread. |
+ leveldb::LevelDBServiceRequest initial_leveldb_request_; |
michaeln
2016/03/16 03:11:13
Is the second path to establish a binding needed?
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(LevelDBThread); |
+}; |
+ |
+} // namespace profile |
+ |
+#endif // COMPONENTS_PROFILE_SERVICE_LEVELDB_THREAD_H_ |