Index: components/profile_service/leveldb_thread.cc |
diff --git a/components/profile_service/leveldb_thread.cc b/components/profile_service/leveldb_thread.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9b57bac3ed2cf8eb3d12665ac5ebd83e785362bc |
--- /dev/null |
+++ b/components/profile_service/leveldb_thread.cc |
@@ -0,0 +1,67 @@ |
+// 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. |
+ |
+#include "components/profile_service/leveldb_thread.h" |
+ |
+#include "base/bind.h" |
+#include "base/message_loop/message_loop.h" |
+#include "components/leveldb/leveldb_service_impl.h" |
+#include "mojo/message_pump/message_pump_mojo.h" |
+ |
+namespace profile { |
+ |
+LevelDBThread::LevelDBThread( |
+ scoped_ptr<mojo::MessageLoopRef> message_loop_ref, |
+ mojo::InterfaceRequest<leveldb::LevelDBService> request) |
+ : base::Thread("profile/LevelDBThread"), |
+ parent_thread_message_loop_ref_(std::move(message_loop_ref)), |
+ initial_leveldb_request_(std::move(request)) { |
+ base::Thread::Options options; |
+ options.message_pump_factory = |
+ base::Bind(&mojo::common::MessagePumpMojo::Create); |
Ben Goodger (Google)
2016/03/11 23:21:37
I don't believe you have to do this anymore.
Elliot Glaysher
2016/03/14 20:33:20
If I don't do this, I get:
Check failed: GetTh
Ben Goodger (Google)
2016/03/14 22:25:57
I think you should use another MessagePump type th
Elliot Glaysher
2016/03/14 23:14:53
Done. (There's a weird interaction where Threads u
|
+ StartWithOptions(options); |
+} |
+ |
+LevelDBThread::~LevelDBThread() {} |
+ |
+void LevelDBThread::OnServiceError() { |
+ if (leveldb_bindings_->empty()) { |
+ // We have released the last leveldb service holding on to our reference; |
+ // release our service and bindings. We need to do this on connection error |
+ // so that it happens on our db thread and not our parent thread. |
+ leveldb_service_.reset(); |
+ leveldb_bindings_.reset(); |
Ben Goodger (Google)
2016/03/11 23:21:37
rather than use a scoped_ptr<mojo::BindingSet<>>,
Elliot Glaysher
2016/03/14 20:33:20
I can not do that without getting CalledOnValidThr
Ben Goodger (Google)
2016/03/14 22:25:57
What thread is this called on and what thread are
Elliot Glaysher
2016/03/14 23:14:53
This base::Thread is started up, and in LevelDBThr
|
+ } |
+} |
+ |
+void LevelDBThread::BindNewRequest( |
+ mojo::InterfaceRequest<leveldb::LevelDBService> request) { |
+ DCHECK_NE(GetThreadId(), base::PlatformThread::CurrentId()); |
+ task_runner()->PostTask( |
+ FROM_HERE, base::Bind(&LevelDBThread::BindNewRequestImpl, this, |
+ base::Passed(&request))); |
+} |
+ |
+void LevelDBThread::BindNewRequestImpl( |
+ mojo::InterfaceRequest<leveldb::LevelDBService> request) { |
Ben Goodger (Google)
2016/03/11 23:21:38
nit: here, and elsewhere, the new hotness is to us
|
+ DCHECK_EQ(GetThreadId(), base::PlatformThread::CurrentId()); |
+ DCHECK(leveldb_service_); |
+ leveldb_bindings_->AddBinding(leveldb_service_.get(), std::move(request)); |
+} |
+ |
+void LevelDBThread::Init() { |
+ leveldb_service_.reset(new leveldb::LevelDBServiceImpl( |
+ thread_message_loop_ref_.CreateRef())); |
+ leveldb_bindings_.reset(new mojo::BindingSet<leveldb::LevelDBService>); |
+ leveldb_bindings_->set_connection_error_handler( |
+ base::Bind(&LevelDBThread::OnServiceError, base::Unretained(this))); |
+ leveldb_bindings_->AddBinding(leveldb_service_.get(), |
+ std::move(initial_leveldb_request_)); |
+} |
+ |
+void LevelDBThread::CleanUp() { |
+ parent_thread_message_loop_ref_.reset(); |
michaeln
2016/03/16 03:11:12
Maybe cleanup long lived objects with thread affin
|
+} |
+ |
+} // namespace profile |