Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/services/network/network_service_delegate.h" | 5 #include "mojo/services/network/network_service_delegate.h" |
| 6 | 6 |
| 7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/base_paths.h" | 8 #include "base/base_paths.h" |
| 9 #include "base/bind.h" | |
| 10 #include "base/command_line.h" | |
| 9 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 10 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 11 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 12 #include "mojo/application/public/cpp/application_connection.h" | 14 #include "mojo/application/public/cpp/application_connection.h" |
| 15 #include "mojo/common/message_pump_mojo.h" | |
| 13 #include "mojo/services/network/network_service_impl.h" | 16 #include "mojo/services/network/network_service_impl.h" |
| 14 #include "mojo/services/network/url_loader_factory_impl.h" | 17 #include "mojo/services/network/url_loader_factory_impl.h" |
| 18 #include "mojo/util/capture_util.h" | |
| 19 #include "sql/mojo/mojo_vfs.h" | |
| 15 | 20 |
| 16 NetworkServiceDelegate::NetworkServiceDelegate() : app_(nullptr) {} | 21 namespace { |
| 17 | 22 |
| 18 NetworkServiceDelegate::~NetworkServiceDelegate() {} | 23 const char kSQLThreadName[] = "SQL_IO_Thread"; |
| 24 | |
| 25 // SQL blocks on the filesystem service, so perform all SQL functions on a | |
| 26 // separate thread. | |
| 27 class SQLThread : public base::Thread { | |
| 28 public: | |
| 29 SQLThread(filesystem::DirectoryPtr directory) | |
| 30 : base::Thread(kSQLThreadName), | |
| 31 directory_info_(directory.PassInterface().Pass()) { | |
| 32 base::Thread::Options options; | |
| 33 options.message_pump_factory = | |
| 34 base::Bind(&mojo::common::MessagePumpMojo::Create); | |
| 35 StartWithOptions(options); | |
| 36 } | |
| 37 ~SQLThread() override { Stop(); } | |
| 38 | |
| 39 void Init() override { | |
| 40 filesystem::DirectoryPtr directory; | |
| 41 directory.Bind(directory_info_.Pass()); | |
| 42 vfs_.reset(new sql::ScopedMojoFilesystemVFS(directory.Pass())); | |
| 43 } | |
| 44 | |
| 45 void CleanUp() override { | |
| 46 vfs_.reset(); | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 // Our VFS which wraps sqlite so that we can reuse the current sqlite code. | |
| 51 scoped_ptr<sql::ScopedMojoFilesystemVFS> vfs_; | |
| 52 | |
| 53 // This member is used to safely pass data from one thread to another. It is | |
| 54 // set in the constructor and is consumed in Init(). | |
| 55 mojo::InterfacePtrInfo<filesystem::Directory> directory_info_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(SQLThread); | |
| 58 }; | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 NetworkServiceDelegate::NetworkServiceDelegate() | |
| 63 : app_(nullptr) { | |
| 64 } | |
| 65 | |
| 66 NetworkServiceDelegate::~NetworkServiceDelegate() { | |
| 67 } | |
| 19 | 68 |
| 20 void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl* app) { | 69 void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl* app) { |
| 21 app_ = app; | 70 app_ = app; |
| 71 | |
| 72 mojo::URLRequestPtr request(mojo::URLRequest::New()); | |
| 73 request->url = mojo::String::From("mojo:filesystem"); | |
| 74 app_->ConnectToService(request.Pass(), &files_); | |
| 75 | |
| 76 filesystem::FileError error = filesystem::FILE_ERROR_FAILED; | |
| 77 filesystem::DirectoryPtr directory; | |
| 78 files_->OpenFileSystem("origin", GetProxy(&directory), mojo::Capture(&error)); | |
| 79 files_.WaitForIncomingResponse(); | |
| 80 | |
| 81 io_worker_thread_.reset(new SQLThread(directory.Pass())); | |
| 82 | |
| 83 // TODO(erg): Find everything else that writes to the filesystem and | |
| 84 // transition it to proxying mojo:filesystem. We shouldn't have any path | |
| 85 // calculation code here, but sadly need it until the transition is done. In | |
| 86 // the mean time, manually handle the user-data-dir switch (which gets set in | |
| 87 // tests) so that tests are writing to a temp dir. | |
| 22 base::FilePath base_path; | 88 base::FilePath base_path; |
| 23 CHECK(PathService::Get(base::DIR_TEMP, &base_path)); | 89 const base::CommandLine* command_line = |
| 24 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); | 90 base::CommandLine::ForCurrentProcess(); |
| 25 context_.reset(new mojo::NetworkContext(base_path)); | 91 if (command_line->HasSwitch("user-data-dir")) { |
|
jam
2015/06/25 15:28:24
seems this should use a constant
| |
| 92 base_path = command_line->GetSwitchValuePath("user-data-dir"); | |
| 93 } else { | |
| 94 CHECK(PathService::Get(base::DIR_TEMP, &base_path)); | |
|
jam
2015/06/25 15:28:24
why have all the services that need to write to di
Elliot Glaysher
2015/06/25 22:46:48
We don't want these services to actually have any
| |
| 95 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); | |
| 96 } | |
| 97 | |
| 98 context_.reset(new mojo::NetworkContext( | |
| 99 base_path, io_worker_thread_->task_runner())); | |
| 26 } | 100 } |
| 27 | 101 |
| 28 bool NetworkServiceDelegate::ConfigureIncomingConnection( | 102 bool NetworkServiceDelegate::ConfigureIncomingConnection( |
| 29 mojo::ApplicationConnection* connection) { | 103 mojo::ApplicationConnection* connection) { |
| 30 DCHECK(context_); | 104 DCHECK(context_); |
| 31 connection->AddService<mojo::NetworkService>(this); | 105 connection->AddService<mojo::NetworkService>(this); |
| 32 connection->AddService<mojo::URLLoaderFactory>(this); | 106 connection->AddService<mojo::URLLoaderFactory>(this); |
| 33 return true; | 107 return true; |
| 34 } | 108 } |
| 35 | 109 |
| 36 void NetworkServiceDelegate::Quit() { | 110 void NetworkServiceDelegate::Quit() { |
| 37 // Destroy the NetworkContext now as it requires MessageLoop::current() upon | 111 // Destroy the NetworkContext now as it requires MessageLoop::current() upon |
| 38 // destruction and it is the last moment we know for sure that it is | 112 // destruction and it is the last moment we know for sure that it is |
| 39 // running. | 113 // running. |
| 40 context_.reset(); | 114 context_.reset(); |
| 115 | |
| 116 // Destroy the io worker thread here so that we can commit any pending | |
| 117 // cookies here. | |
| 118 io_worker_thread_.reset(); | |
| 41 } | 119 } |
| 42 | 120 |
| 43 void NetworkServiceDelegate::Create( | 121 void NetworkServiceDelegate::Create( |
| 44 mojo::ApplicationConnection* connection, | 122 mojo::ApplicationConnection* connection, |
| 45 mojo::InterfaceRequest<mojo::NetworkService> request) { | 123 mojo::InterfaceRequest<mojo::NetworkService> request) { |
| 46 new mojo::NetworkServiceImpl( | 124 new mojo::NetworkServiceImpl( |
| 47 connection, | 125 connection, |
| 48 context_.get(), | 126 context_.get(), |
| 49 app_->app_lifetime_helper()->CreateAppRefCount(), | 127 app_->app_lifetime_helper()->CreateAppRefCount(), |
| 50 request.Pass()); | 128 request.Pass()); |
| 51 } | 129 } |
| 52 | 130 |
| 53 void NetworkServiceDelegate::Create( | 131 void NetworkServiceDelegate::Create( |
| 54 mojo::ApplicationConnection* connection, | 132 mojo::ApplicationConnection* connection, |
| 55 mojo::InterfaceRequest<mojo::URLLoaderFactory> request) { | 133 mojo::InterfaceRequest<mojo::URLLoaderFactory> request) { |
| 56 new mojo::URLLoaderFactoryImpl( | 134 new mojo::URLLoaderFactoryImpl( |
| 57 connection, | 135 connection, |
| 58 context_.get(), | 136 context_.get(), |
| 59 app_->app_lifetime_helper()->CreateAppRefCount(), | 137 app_->app_lifetime_helper()->CreateAppRefCount(), |
| 60 request.Pass()); | 138 request.Pass()); |
| 61 } | 139 } |
| OLD | NEW |