Chromium Code Reviews| 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..03c76324d2ac13af1a529fd0ef9f8cedccbcd37c |
| --- /dev/null |
| +++ b/components/profile_service/leveldb_thread.h |
| @@ -0,0 +1,75 @@ |
| +// 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, |
| + mojo::InterfaceRequest<leveldb::LevelDBService> request); |
| + |
| + // Passes |request| from the caller thread to our LevelDBThread and |
| + // binds |request| to the leveldb service thread. |
| + void BindNewRequest(mojo::InterfaceRequest<leveldb::LevelDBService> 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( |
| + mojo::InterfaceRequest<leveldb::LevelDBService> request); |
| + |
| + // Overridden from base::SimpleThread: |
| + void Init() override; |
| + void CleanUp() override; |
| + |
| + // A reference to the parent thread's message loop, which needs to stay alive |
| + // while this thread is running. Set on the parent thread; destroyed on the |
| + // child thread. |
| + scoped_ptr<mojo::MessageLoopRef> parent_thread_message_loop_ref_; |
|
Ben Goodger (Google)
2016/03/11 23:21:38
profile_service_message_loop_ref_?
// Hold a refe
|
| + |
| + // When we create our underlying LevelDBService, we want to give it a |
| + // reference to this object's thread's message loop, so when the service goes |
| + // away, this thread gets cleaned up which releases our reference to our |
| + // parent thread. |
| + mojo::MessageLoopRefFactory thread_message_loop_ref_; |
|
Ben Goodger (Google)
2016/03/11 23:21:38
Shorter:
// The lifetime of the leveldb thread's
|
| + |
| + // 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. |
| + mojo::InterfaceRequest<leveldb::LevelDBService> initial_leveldb_request_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(LevelDBThread); |
| +}; |
| + |
| +} // namespace profile |
| + |
| +#endif // COMPONENTS_PROFILE_SERVICE_LEVELDB_THREAD_H_ |