mojo leveldb: Get profile and leveldb connected to DOMStorageContext.

Building on the last patch which built mojo:profile and built one
for each BrowserContext, we move the LevelDBService in the profile
application to a dedicated leveldb thread to avoid deadlocks.
(Since applications all run on one thread by default, leveldb would
deadlock while waiting for profile to return data.)

This patch connects to the correct applications in
DOMStorageContextWrapper and then vends a LevelDBService instance to
each LevelDBWrapperImpl, for future use.

BUG=586194
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:linux_site_isolation

Review URL: https://codereview.chromium.org/1737933002

Cr-Commit-Position: refs/heads/master@{#382343}
diff --git a/components/filesystem/directory_impl.cc b/components/filesystem/directory_impl.cc
index e8fab2a..c80731d 100644
--- a/components/filesystem/directory_impl.cc
+++ b/components/filesystem/directory_impl.cc
@@ -14,6 +14,7 @@
 #include "base/memory/scoped_ptr.h"
 #include "build/build_config.h"
 #include "components/filesystem/file_impl.h"
+#include "components/filesystem/lock_table.h"
 #include "components/filesystem/util.h"
 #include "mojo/common/common_type_converters.h"
 #include "mojo/platform_handle/platform_handle_functions.h"
@@ -25,11 +26,11 @@
 DirectoryImpl::DirectoryImpl(mojo::InterfaceRequest<Directory> request,
                              base::FilePath directory_path,
                              scoped_ptr<base::ScopedTempDir> temp_dir,
-                             LockTable* lock_table)
+                             scoped_refptr<LockTable> lock_table)
     : binding_(this, std::move(request)),
       directory_path_(directory_path),
       temp_dir_(std::move(temp_dir)),
-      lock_table_(lock_table) {}
+      lock_table_(std::move(lock_table)) {}
 
 DirectoryImpl::~DirectoryImpl() {
 }
diff --git a/components/filesystem/directory_impl.h b/components/filesystem/directory_impl.h
index 347c514..03bba30 100644
--- a/components/filesystem/directory_impl.h
+++ b/components/filesystem/directory_impl.h
@@ -30,7 +30,7 @@
   DirectoryImpl(mojo::InterfaceRequest<Directory> request,
                 base::FilePath directory_path,
                 scoped_ptr<base::ScopedTempDir> temp_dir,
-                LockTable* lock_table);
+                scoped_refptr<LockTable> lock_table);
   ~DirectoryImpl() override;
 
   void set_connection_error_handler(const mojo::Closure& error_handler) {
@@ -73,7 +73,7 @@
   mojo::StrongBinding<Directory> binding_;
   base::FilePath directory_path_;
   scoped_ptr<base::ScopedTempDir> temp_dir_;
-  LockTable* lock_table_;
+  scoped_refptr<LockTable> lock_table_;
 
   DISALLOW_COPY_AND_ASSIGN(DirectoryImpl);
 };
diff --git a/components/filesystem/file_impl.cc b/components/filesystem/file_impl.cc
index 21b38d7..13ebced6 100644
--- a/components/filesystem/file_impl.cc
+++ b/components/filesystem/file_impl.cc
@@ -34,22 +34,22 @@
 FileImpl::FileImpl(mojo::InterfaceRequest<File> request,
                    const base::FilePath& path,
                    uint32_t flags,
-                   LockTable* lock_table)
+                   scoped_refptr<LockTable> lock_table)
     : binding_(this, std::move(request)),
       file_(path, flags),
       path_(path),
-      lock_table_(lock_table) {
+      lock_table_(std::move(lock_table)) {
   DCHECK(file_.IsValid());
 }
 
 FileImpl::FileImpl(mojo::InterfaceRequest<File> request,
                    const base::FilePath& path,
                    base::File file,
-                   LockTable* lock_table)
+                   scoped_refptr<LockTable> lock_table)
     : binding_(this, std::move(request)),
       file_(std::move(file)),
       path_(path),
-      lock_table_(lock_table) {
+      lock_table_(std::move(lock_table)) {
   DCHECK(file_.IsValid());
 }
 
diff --git a/components/filesystem/file_impl.h b/components/filesystem/file_impl.h
index 9314776c..bdae38e0 100644
--- a/components/filesystem/file_impl.h
+++ b/components/filesystem/file_impl.h
@@ -27,11 +27,11 @@
   FileImpl(mojo::InterfaceRequest<File> request,
            const base::FilePath& path,
            uint32_t flags,
-           LockTable* lock_table);
+           scoped_refptr<LockTable> lock_table);
   FileImpl(mojo::InterfaceRequest<File> request,
            const base::FilePath& path,
            base::File file,
-           LockTable* lock_table);
+           scoped_refptr<LockTable> lock_table);
   ~FileImpl() override;
 
   // Returns whether the underlying file handle is valid.
@@ -74,7 +74,7 @@
   mojo::StrongBinding<File> binding_;
   base::File file_;
   base::FilePath path_;
-  LockTable* lock_table_;
+  scoped_refptr<LockTable> lock_table_;
 
   DISALLOW_COPY_AND_ASSIGN(FileImpl);
 };
diff --git a/components/filesystem/file_system_app.cc b/components/filesystem/file_system_app.cc
index 9ec38069..2163351 100644
--- a/components/filesystem/file_system_app.cc
+++ b/components/filesystem/file_system_app.cc
@@ -52,7 +52,7 @@
 void FileSystemApp::Create(mojo::Connection* connection,
                            mojo::InterfaceRequest<FileSystem> request) {
   new FileSystemImpl(connection, std::move(request), GetUserDataDir(),
-                     lock_table_.get());
+                     lock_table_);
 }
 
 void FileSystemApp::ShellConnectionLost() {
diff --git a/components/filesystem/file_system_app.h b/components/filesystem/file_system_app.h
index 1eb65c3..5e297ad8 100644
--- a/components/filesystem/file_system_app.h
+++ b/components/filesystem/file_system_app.h
@@ -42,7 +42,7 @@
 
   mojo::TracingImpl tracing_;
 
-  scoped_ptr<LockTable> lock_table_;
+  scoped_refptr<LockTable> lock_table_;
 
   DISALLOW_COPY_AND_ASSIGN(FileSystemApp);
 };
diff --git a/components/filesystem/file_system_impl.cc b/components/filesystem/file_system_impl.cc
index f8b9594d..b1eacf16 100644
--- a/components/filesystem/file_system_impl.cc
+++ b/components/filesystem/file_system_impl.cc
@@ -15,6 +15,7 @@
 #include "base/memory/scoped_ptr.h"
 #include "build/build_config.h"
 #include "components/filesystem/directory_impl.h"
+#include "components/filesystem/lock_table.h"
 #include "mojo/shell/public/cpp/connection.h"
 #include "url/gurl.h"
 
@@ -23,10 +24,10 @@
 FileSystemImpl::FileSystemImpl(mojo::Connection* connection,
                                mojo::InterfaceRequest<FileSystem> request,
                                base::FilePath persistent_dir,
-                               LockTable* lock_table)
+                               scoped_refptr<LockTable> lock_table)
     : remote_application_name_(connection->GetRemoteIdentity().name()),
       binding_(this, std::move(request)),
-      lock_table_(lock_table),
+      lock_table_(std::move(lock_table)),
       persistent_dir_(persistent_dir) {}
 
 FileSystemImpl::~FileSystemImpl() {
diff --git a/components/filesystem/file_system_impl.h b/components/filesystem/file_system_impl.h
index 7bbecd2..ff5d9bc4 100644
--- a/components/filesystem/file_system_impl.h
+++ b/components/filesystem/file_system_impl.h
@@ -32,7 +32,7 @@
   FileSystemImpl(mojo::Connection* connection,
                  mojo::InterfaceRequest<FileSystem> request,
                  base::FilePath persistent_dir,
-                 LockTable* lock_table);
+                 scoped_refptr<LockTable> lock_table);
   ~FileSystemImpl() override;
 
   // |Files| implementation:
@@ -46,7 +46,7 @@
  private:
   const std::string remote_application_name_;
   mojo::StrongBinding<FileSystem> binding_;
-  LockTable* lock_table_;
+  scoped_refptr<LockTable> lock_table_;
 
   base::FilePath persistent_dir_;
 
diff --git a/components/filesystem/lock_table.h b/components/filesystem/lock_table.h
index 9be4ca6..73674ac2 100644
--- a/components/filesystem/lock_table.h
+++ b/components/filesystem/lock_table.h
@@ -8,6 +8,7 @@
 #include <set>
 
 #include "base/files/file.h"
+#include "base/memory/ref_counted.h"
 
 namespace filesystem {
 
@@ -16,10 +17,9 @@
 // A table of all locks held by this process. We have one global table owned by
 // the app, but accessible by everything just in case two connections from the
 // same origin try to lock the same file.
-class LockTable {
+class LockTable : public base::RefCounted<LockTable> {
  public:
   LockTable();
-  ~LockTable();
 
   // Locks a file.
   base::File::Error LockFile(FileImpl* file);
@@ -33,6 +33,9 @@
   void RemoveFromLockTable(const base::FilePath& path);
 
  private:
+  friend class base::RefCounted<LockTable>;
+  ~LockTable();
+
   // Open, locked files. We keep track of this so we quickly error when we try
   // to double lock a file.
   std::set<base::FilePath> locked_files_;