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
3 // found in the LICENSE file.
5 #include "mojo/services/network/network_service_delegate.h"
7 #include "base/at_exit.h"
8 #include "base/base_paths.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h"
14 #include "mojo/application/public/cpp/application_connection.h"
15 #include "mojo/message_pump/message_pump_mojo.h"
16 #include "mojo/services/network/network_service_delegate_observer.h"
17 #include "mojo/services/network/network_service_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"
24 const char kSQLThreadName
[] = "SQL_IO_Thread";
25 const char kUserDataDir
[] = "user-data-dir";
27 // SQL blocks on the filesystem service, so perform all SQL functions on a
29 class SQLThread
: public base::Thread
{
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
);
39 ~SQLThread() override
{ Stop(); }
41 void Init() override
{
42 filesystem::DirectoryPtr directory
;
43 directory
.Bind(directory_info_
.Pass());
44 vfs_
.reset(new sql::ScopedMojoFilesystemVFS(directory
.Pass()));
47 void CleanUp() override
{
52 // Our VFS which wraps sqlite so that we can reuse the current sqlite code.
53 scoped_ptr
<sql::ScopedMojoFilesystemVFS
> vfs_
;
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_
;
59 DISALLOW_COPY_AND_ASSIGN(SQLThread
);
66 NetworkServiceDelegate::NetworkServiceDelegate()
71 NetworkServiceDelegate::~NetworkServiceDelegate() {
74 void NetworkServiceDelegate::AddObserver(
75 NetworkServiceDelegateObserver
* observer
) {
76 observers_
.AddObserver(observer
);
79 void NetworkServiceDelegate::RemoveObserver(
80 NetworkServiceDelegateObserver
* observer
) {
81 observers_
.RemoveObserver(observer
);
84 void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl
* app
) {
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_
);
98 filesystem::FileSystemClientPtr client
;
99 binding_
.Bind(GetProxy(&client
));
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();
107 io_worker_thread_
.reset(new SQLThread(directory
.Pass()));
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.
115 base::FilePath base_path
;
116 const base::CommandLine
* command_line
=
117 base::CommandLine::ForCurrentProcess();
118 if (command_line
->HasSwitch(kUserDataDir
)) {
119 base_path
= command_line
->GetSwitchValuePath(kUserDataDir
);
121 CHECK(PathService::Get(base::DIR_TEMP
, &base_path
));
122 base_path
= base_path
.Append(FILE_PATH_LITERAL("network_service"));
125 scoped_refptr
<base::SequencedTaskRunner
> worker_thread
;
126 #if !defined(OS_ANDROID)
127 worker_thread
= io_worker_thread_
->task_runner();
129 context_
.reset(new mojo::NetworkContext(base_path
, worker_thread
, this));
132 bool NetworkServiceDelegate::ConfigureIncomingConnection(
133 mojo::ApplicationConnection
* connection
) {
135 connection
->AddService
<mojo::NetworkService
>(this);
136 connection
->AddService
<mojo::URLLoaderFactory
>(this);
140 bool NetworkServiceDelegate::OnShellConnectionError() {
141 EnsureIOThreadShutdown();
145 void NetworkServiceDelegate::Quit() {
146 EnsureIOThreadShutdown();
148 // Destroy the NetworkContext now as it requires MessageLoop::current() upon
149 // destruction and it is the last moment we know for sure that it is
154 void NetworkServiceDelegate::Create(
155 mojo::ApplicationConnection
* connection
,
156 mojo::InterfaceRequest
<mojo::NetworkService
> request
) {
157 new mojo::NetworkServiceImpl(
160 app_
->app_lifetime_helper()->CreateAppRefCount(),
164 void NetworkServiceDelegate::Create(
165 mojo::ApplicationConnection
* connection
,
166 mojo::InterfaceRequest
<mojo::URLLoaderFactory
> request
) {
167 new mojo::URLLoaderFactoryImpl(
170 app_
->app_lifetime_helper()->CreateAppRefCount(),
174 void NetworkServiceDelegate::OnFileSystemShutdown() {
175 EnsureIOThreadShutdown();
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 worker thread. Either we're shutting down or the
182 // filesystem service is shutting down.
183 FOR_EACH_OBSERVER(NetworkServiceDelegateObserver
, observers_
,
184 OnIOWorkerThreadShutdown());
186 // Destroy the io worker thread here so that we can commit any pending
188 io_worker_thread_
.reset();