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" |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "mojo/application/public/cpp/application_connection.h" | 13 #include "mojo/application/public/cpp/application_connection.h" |
| 14 #include "mojo/common/message_pump_mojo.h" |
13 #include "mojo/services/network/network_service_impl.h" | 15 #include "mojo/services/network/network_service_impl.h" |
14 #include "mojo/services/network/url_loader_factory_impl.h" | 16 #include "mojo/services/network/url_loader_factory_impl.h" |
| 17 #include "mojo/util/capture_util.h" |
| 18 #include "sql/mojo/mojo_vfs.h" |
15 | 19 |
16 NetworkServiceDelegate::NetworkServiceDelegate() : app_(nullptr) {} | 20 namespace { |
17 | 21 |
18 NetworkServiceDelegate::~NetworkServiceDelegate() {} | 22 const char kSQLThreadName[] = "SQL_IO_Thread"; |
| 23 |
| 24 // SQL blocks on the filesystem service, so perform all SQL functions on a |
| 25 // separate thread. |
| 26 class SQLThread : public base::Thread { |
| 27 public: |
| 28 SQLThread(filesystem::DirectoryPtr directory) |
| 29 : base::Thread(kSQLThreadName), |
| 30 directory_info_(directory.PassInterface().Pass()) { |
| 31 base::Thread::Options options; |
| 32 options.message_pump_factory = |
| 33 base::Bind(&mojo::common::MessagePumpMojo::Create); |
| 34 StartWithOptions(options); |
| 35 } |
| 36 ~SQLThread() override { Stop(); } |
| 37 |
| 38 void Init() override { |
| 39 filesystem::DirectoryPtr directory; |
| 40 directory.Bind(directory_info_.Pass()); |
| 41 vfs_.reset(new sql::ScopedMojoFilesystemVFS(directory.Pass())); |
| 42 } |
| 43 |
| 44 void CleanUp() override { |
| 45 vfs_.reset(); |
| 46 } |
| 47 |
| 48 private: |
| 49 // Our VFS which wraps sqlite so that we can reuse the current sqlite code. |
| 50 scoped_ptr<sql::ScopedMojoFilesystemVFS> vfs_; |
| 51 |
| 52 // This member is used to safely pass data from one thread to another. It is |
| 53 // set in the constructor and is consumed in Init(). |
| 54 mojo::InterfacePtrInfo<filesystem::Directory> directory_info_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(SQLThread); |
| 57 }; |
| 58 |
| 59 } // namespace |
| 60 |
| 61 NetworkServiceDelegate::NetworkServiceDelegate() |
| 62 : app_(nullptr) { |
| 63 } |
| 64 |
| 65 NetworkServiceDelegate::~NetworkServiceDelegate() { |
| 66 } |
19 | 67 |
20 void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl* app) { | 68 void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl* app) { |
21 app_ = app; | 69 app_ = app; |
| 70 |
| 71 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 72 request->url = mojo::String::From("mojo:filesystem"); |
| 73 app_->ConnectToService(request.Pass(), &files_); |
| 74 |
| 75 filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| 76 filesystem::DirectoryPtr directory; |
| 77 files_->OpenFileSystem("origin", GetProxy(&directory), mojo::Capture(&error)); |
| 78 files_.WaitForIncomingResponse(); |
| 79 |
| 80 io_worker_thread_.reset(new SQLThread(directory.Pass())); |
| 81 |
| 82 // TODO(erg): Find everything else that writes to the filesystem and |
| 83 // transition it to proxying mojo:filesystem |
22 base::FilePath base_path; | 84 base::FilePath base_path; |
23 CHECK(PathService::Get(base::DIR_TEMP, &base_path)); | 85 CHECK(PathService::Get(base::DIR_TEMP, &base_path)); |
24 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); | 86 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); |
25 context_.reset(new mojo::NetworkContext(base_path)); | 87 |
| 88 context_.reset(new mojo::NetworkContext( |
| 89 base_path, io_worker_thread_->task_runner())); |
26 } | 90 } |
27 | 91 |
28 bool NetworkServiceDelegate::ConfigureIncomingConnection( | 92 bool NetworkServiceDelegate::ConfigureIncomingConnection( |
29 mojo::ApplicationConnection* connection) { | 93 mojo::ApplicationConnection* connection) { |
30 DCHECK(context_); | 94 DCHECK(context_); |
31 connection->AddService<mojo::NetworkService>(this); | 95 connection->AddService<mojo::NetworkService>(this); |
32 connection->AddService<mojo::URLLoaderFactory>(this); | 96 connection->AddService<mojo::URLLoaderFactory>(this); |
33 return true; | 97 return true; |
34 } | 98 } |
35 | 99 |
36 void NetworkServiceDelegate::Quit() { | 100 void NetworkServiceDelegate::Quit() { |
37 // Destroy the NetworkContext now as it requires MessageLoop::current() upon | 101 // 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 | 102 // destruction and it is the last moment we know for sure that it is |
39 // running. | 103 // running. |
40 context_.reset(); | 104 context_.reset(); |
| 105 |
| 106 // Destroy the io worker thread here so that we can commit any pending |
| 107 // cookies here. |
| 108 io_worker_thread_.reset(); |
41 } | 109 } |
42 | 110 |
43 void NetworkServiceDelegate::Create( | 111 void NetworkServiceDelegate::Create( |
44 mojo::ApplicationConnection* connection, | 112 mojo::ApplicationConnection* connection, |
45 mojo::InterfaceRequest<mojo::NetworkService> request) { | 113 mojo::InterfaceRequest<mojo::NetworkService> request) { |
46 new mojo::NetworkServiceImpl( | 114 new mojo::NetworkServiceImpl( |
47 connection, | 115 connection, |
48 context_.get(), | 116 context_.get(), |
49 app_->app_lifetime_helper()->CreateAppRefCount(), | 117 app_->app_lifetime_helper()->CreateAppRefCount(), |
50 request.Pass()); | 118 request.Pass()); |
51 } | 119 } |
52 | 120 |
53 void NetworkServiceDelegate::Create( | 121 void NetworkServiceDelegate::Create( |
54 mojo::ApplicationConnection* connection, | 122 mojo::ApplicationConnection* connection, |
55 mojo::InterfaceRequest<mojo::URLLoaderFactory> request) { | 123 mojo::InterfaceRequest<mojo::URLLoaderFactory> request) { |
56 new mojo::URLLoaderFactoryImpl( | 124 new mojo::URLLoaderFactoryImpl( |
57 connection, | 125 connection, |
58 context_.get(), | 126 context_.get(), |
59 app_->app_lifetime_helper()->CreateAppRefCount(), | 127 app_->app_lifetime_helper()->CreateAppRefCount(), |
60 request.Pass()); | 128 request.Pass()); |
61 } | 129 } |
OLD | NEW |