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