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" | |
| 16 #include "mojo/services/network/network_service_delegate_observer.h" | |
| 13 #include "mojo/services/network/network_service_impl.h" | 17 #include "mojo/services/network/network_service_impl.h" |
| 14 #include "mojo/services/network/url_loader_factory_impl.h" | 18 #include "mojo/services/network/url_loader_factory_impl.h" |
| 19 #include "mojo/util/capture_util.h" | |
| 20 #include "sql/mojo/mojo_vfs.h" | |
| 15 | 21 |
| 16 NetworkServiceDelegate::NetworkServiceDelegate() : app_(nullptr) {} | 22 namespace { |
| 17 | 23 |
| 18 NetworkServiceDelegate::~NetworkServiceDelegate() {} | 24 const char kSQLThreadName[] = "SQL_IO_Thread"; |
| 25 const char kUserDataDir[] = "user-data-dir"; | |
| 26 | |
| 27 // SQL blocks on the filesystem service, so perform all SQL functions on a | |
| 28 // separate thread. | |
| 29 class SQLThread : public base::Thread { | |
| 30 public: | |
| 31 SQLThread(filesystem::DirectoryPtr directory) | |
| 32 : base::Thread(kSQLThreadName), | |
| 33 directory_info_(directory.PassInterface().Pass()) { | |
| 34 base::Thread::Options options; | |
| 35 options.message_pump_factory = | |
| 36 base::Bind(&mojo::common::MessagePumpMojo::Create); | |
| 37 StartWithOptions(options); | |
| 38 } | |
| 39 ~SQLThread() override { Stop(); } | |
| 40 | |
| 41 void Init() override { | |
| 42 filesystem::DirectoryPtr directory; | |
| 43 directory.Bind(directory_info_.Pass()); | |
| 44 vfs_.reset(new sql::ScopedMojoFilesystemVFS(directory.Pass())); | |
| 45 } | |
| 46 | |
| 47 void CleanUp() override { | |
| 48 vfs_.reset(); | |
| 49 } | |
| 50 | |
| 51 private: | |
| 52 // Our VFS which wraps sqlite so that we can reuse the current sqlite code. | |
| 53 scoped_ptr<sql::ScopedMojoFilesystemVFS> vfs_; | |
| 54 | |
| 55 // This member is used to safely pass data from one thread to another. It is | |
| 56 // set in the constructor and is consumed in Init(). | |
| 57 mojo::InterfacePtrInfo<filesystem::Directory> directory_info_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(SQLThread); | |
| 60 }; | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 namespace mojo { | |
| 65 | |
| 66 NetworkServiceDelegate::NetworkServiceDelegate() | |
| 67 : app_(nullptr), | |
| 68 binding_(this) { | |
| 69 } | |
| 70 | |
| 71 NetworkServiceDelegate::~NetworkServiceDelegate() { | |
| 72 } | |
| 73 | |
| 74 void NetworkServiceDelegate::AddObserver( | |
| 75 NetworkServiceDelegateObserver* observer) { | |
| 76 observers_.AddObserver(observer); | |
| 77 } | |
| 78 | |
| 79 void NetworkServiceDelegate::RemoveObserver( | |
| 80 NetworkServiceDelegateObserver* observer) { | |
| 81 observers_.RemoveObserver(observer); | |
| 82 } | |
| 19 | 83 |
| 20 void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl* app) { | 84 void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl* app) { |
| 21 app_ = app; | 85 app_ = app; |
| 86 | |
| 87 #if !defined(OS_ANDROID) | |
| 88 // TODO(erg): The following doesn't work when running the android | |
| 89 // apptests. It works in the mandoline shell (on desktop and on android), and | |
| 90 // in the apptests on desktop. However, on android, whenever we make the call | |
| 91 // to OpenFileSystem, the entire mojo system hangs to the point where writes | |
| 92 // to stderr that previously would have printed to our console aren't. The | |
| 93 // apptests are also fairly resistant to being run under gdb on android. | |
| 94 mojo::URLRequestPtr request(mojo::URLRequest::New()); | |
| 95 request->url = mojo::String::From("mojo:filesystem"); | |
| 96 app_->ConnectToService(request.Pass(), &files_); | |
| 97 | |
| 98 filesystem::FileSystemClientPtr client; | |
| 99 binding_.Bind(GetProxy(&client)); | |
| 100 | |
| 101 filesystem::FileError error = filesystem::FILE_ERROR_FAILED; | |
| 102 filesystem::DirectoryPtr directory; | |
| 103 files_->OpenFileSystem("origin", GetProxy(&directory), client.Pass(), | |
| 104 mojo::Capture(&error)); | |
| 105 files_.WaitForIncomingResponse(); | |
| 106 | |
| 107 io_worker_thread_.reset(new SQLThread(directory.Pass())); | |
| 108 #endif | |
| 109 | |
| 110 // TODO(erg): Find everything else that writes to the filesystem and | |
| 111 // transition it to proxying mojo:filesystem. We shouldn't have any path | |
| 112 // calculation code here, but sadly need it until the transition is done. In | |
| 113 // the mean time, manually handle the user-data-dir switch (which gets set in | |
| 114 // tests) so that tests are writing to a temp dir. | |
| 22 base::FilePath base_path; | 115 base::FilePath base_path; |
| 23 CHECK(PathService::Get(base::DIR_TEMP, &base_path)); | 116 const base::CommandLine* command_line = |
| 24 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); | 117 base::CommandLine::ForCurrentProcess(); |
| 25 context_.reset(new mojo::NetworkContext(base_path)); | 118 if (command_line->HasSwitch(kUserDataDir)) { |
| 119 base_path = command_line->GetSwitchValuePath(kUserDataDir); | |
| 120 } else { | |
| 121 CHECK(PathService::Get(base::DIR_TEMP, &base_path)); | |
| 122 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); | |
| 123 } | |
| 124 | |
| 125 scoped_refptr<base::SequencedTaskRunner> worker_thread; | |
| 126 #if !defined(OS_ANDROID) | |
| 127 worker_thread = io_worker_thread_->task_runner(); | |
| 128 #endif | |
| 129 context_.reset(new mojo::NetworkContext(base_path, worker_thread, this)); | |
| 26 } | 130 } |
| 27 | 131 |
| 28 bool NetworkServiceDelegate::ConfigureIncomingConnection( | 132 bool NetworkServiceDelegate::ConfigureIncomingConnection( |
| 29 mojo::ApplicationConnection* connection) { | 133 mojo::ApplicationConnection* connection) { |
| 30 DCHECK(context_); | 134 DCHECK(context_); |
| 31 connection->AddService<mojo::NetworkService>(this); | 135 connection->AddService<mojo::NetworkService>(this); |
| 32 connection->AddService<mojo::URLLoaderFactory>(this); | 136 connection->AddService<mojo::URLLoaderFactory>(this); |
| 33 return true; | 137 return true; |
| 34 } | 138 } |
| 35 | 139 |
| 140 bool NetworkServiceDelegate::OnShellConnectionError() { | |
| 141 EnsureIOThreadShutdown(); | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 36 void NetworkServiceDelegate::Quit() { | 145 void NetworkServiceDelegate::Quit() { |
| 146 EnsureIOThreadShutdown(); | |
| 147 | |
| 37 // Destroy the NetworkContext now as it requires MessageLoop::current() upon | 148 // 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 | 149 // destruction and it is the last moment we know for sure that it is |
| 39 // running. | 150 // running. |
| 40 context_.reset(); | 151 context_.reset(); |
| 41 } | 152 } |
| 42 | 153 |
| 43 void NetworkServiceDelegate::Create( | 154 void NetworkServiceDelegate::Create( |
| 44 mojo::ApplicationConnection* connection, | 155 mojo::ApplicationConnection* connection, |
| 45 mojo::InterfaceRequest<mojo::NetworkService> request) { | 156 mojo::InterfaceRequest<mojo::NetworkService> request) { |
| 46 new mojo::NetworkServiceImpl( | 157 new mojo::NetworkServiceImpl( |
| 47 connection, | 158 connection, |
| 48 context_.get(), | 159 context_.get(), |
| 49 app_->app_lifetime_helper()->CreateAppRefCount(), | 160 app_->app_lifetime_helper()->CreateAppRefCount(), |
| 50 request.Pass()); | 161 request.Pass()); |
| 51 } | 162 } |
| 52 | 163 |
| 53 void NetworkServiceDelegate::Create( | 164 void NetworkServiceDelegate::Create( |
| 54 mojo::ApplicationConnection* connection, | 165 mojo::ApplicationConnection* connection, |
| 55 mojo::InterfaceRequest<mojo::URLLoaderFactory> request) { | 166 mojo::InterfaceRequest<mojo::URLLoaderFactory> request) { |
| 56 new mojo::URLLoaderFactoryImpl( | 167 new mojo::URLLoaderFactoryImpl( |
| 57 connection, | 168 connection, |
| 58 context_.get(), | 169 context_.get(), |
| 59 app_->app_lifetime_helper()->CreateAppRefCount(), | 170 app_->app_lifetime_helper()->CreateAppRefCount(), |
| 60 request.Pass()); | 171 request.Pass()); |
| 61 } | 172 } |
| 173 | |
| 174 void NetworkServiceDelegate::OnFileSystemShutdown() { | |
| 175 EnsureIOThreadShutdown(); | |
| 176 } | |
| 177 | |
| 178 void NetworkServiceDelegate::EnsureIOThreadShutdown() { | |
| 179 if (io_worker_thread_) { | |
| 180 // Broadcast to the entire system that we have to shut down anything | |
| 181 // depending on the connection an | |
|
jam
2015/07/13 15:48:33
nit: comment cut off
| |
| 182 FOR_EACH_OBSERVER(NetworkServiceDelegateObserver, observers_, | |
| 183 OnIOWorkerThreadShutdown()); | |
| 184 | |
| 185 // Destroy the io worker thread here so that we can commit any pending | |
| 186 // cookies here. | |
| 187 io_worker_thread_.reset(); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 | |
| 192 } // namespace mojo | |
| OLD | NEW |