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" | |
11 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
12 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
13 #include "base/path_service.h" | 11 #include "base/path_service.h" |
14 #include "mojo/application/public/cpp/application_connection.h" | 12 #include "mojo/application/public/cpp/application_connection.h" |
15 #include "mojo/common/message_pump_mojo.h" | |
16 #include "mojo/services/network/network_service_impl.h" | 13 #include "mojo/services/network/network_service_impl.h" |
17 #include "mojo/services/network/url_loader_factory_impl.h" | 14 #include "mojo/services/network/url_loader_factory_impl.h" |
18 #include "mojo/util/capture_util.h" | |
19 #include "sql/mojo/mojo_vfs.h" | |
20 | 15 |
21 namespace { | 16 NetworkServiceDelegate::NetworkServiceDelegate() : app_(nullptr) {} |
22 | 17 |
23 const char kSQLThreadName[] = "SQL_IO_Thread"; | 18 NetworkServiceDelegate::~NetworkServiceDelegate() {} |
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 } | |
69 | 19 |
70 void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl* app) { | 20 void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl* app) { |
71 app_ = app; | 21 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. | |
97 base::FilePath base_path; | 22 base::FilePath base_path; |
98 const base::CommandLine* command_line = | 23 CHECK(PathService::Get(base::DIR_TEMP, &base_path)); |
99 base::CommandLine::ForCurrentProcess(); | 24 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); |
100 if (command_line->HasSwitch(kUserDataDir)) { | 25 context_.reset(new mojo::NetworkContext(base_path)); |
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)); | |
112 } | 26 } |
113 | 27 |
114 bool NetworkServiceDelegate::ConfigureIncomingConnection( | 28 bool NetworkServiceDelegate::ConfigureIncomingConnection( |
115 mojo::ApplicationConnection* connection) { | 29 mojo::ApplicationConnection* connection) { |
116 DCHECK(context_); | 30 DCHECK(context_); |
117 connection->AddService<mojo::NetworkService>(this); | 31 connection->AddService<mojo::NetworkService>(this); |
118 connection->AddService<mojo::URLLoaderFactory>(this); | 32 connection->AddService<mojo::URLLoaderFactory>(this); |
119 return true; | 33 return true; |
120 } | 34 } |
121 | 35 |
122 void NetworkServiceDelegate::Quit() { | 36 void NetworkServiceDelegate::Quit() { |
123 // Destroy the NetworkContext now as it requires MessageLoop::current() upon | 37 // Destroy the NetworkContext now as it requires MessageLoop::current() upon |
124 // destruction and it is the last moment we know for sure that it is | 38 // destruction and it is the last moment we know for sure that it is |
125 // running. | 39 // running. |
126 context_.reset(); | 40 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(); | |
131 } | 41 } |
132 | 42 |
133 void NetworkServiceDelegate::Create( | 43 void NetworkServiceDelegate::Create( |
134 mojo::ApplicationConnection* connection, | 44 mojo::ApplicationConnection* connection, |
135 mojo::InterfaceRequest<mojo::NetworkService> request) { | 45 mojo::InterfaceRequest<mojo::NetworkService> request) { |
136 new mojo::NetworkServiceImpl( | 46 new mojo::NetworkServiceImpl( |
137 connection, | 47 connection, |
138 context_.get(), | 48 context_.get(), |
139 app_->app_lifetime_helper()->CreateAppRefCount(), | 49 app_->app_lifetime_helper()->CreateAppRefCount(), |
140 request.Pass()); | 50 request.Pass()); |
141 } | 51 } |
142 | 52 |
143 void NetworkServiceDelegate::Create( | 53 void NetworkServiceDelegate::Create( |
144 mojo::ApplicationConnection* connection, | 54 mojo::ApplicationConnection* connection, |
145 mojo::InterfaceRequest<mojo::URLLoaderFactory> request) { | 55 mojo::InterfaceRequest<mojo::URLLoaderFactory> request) { |
146 new mojo::URLLoaderFactoryImpl( | 56 new mojo::URLLoaderFactoryImpl( |
147 connection, | 57 connection, |
148 context_.get(), | 58 context_.get(), |
149 app_->app_lifetime_helper()->CreateAppRefCount(), | 59 app_->app_lifetime_helper()->CreateAppRefCount(), |
150 request.Pass()); | 60 request.Pass()); |
151 } | 61 } |
OLD | NEW |