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