| // Copyright 2015 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "mojo/services/network/network_service_app.h" |
| |
| #include "base/at_exit.h" |
| #include "base/base_paths.h" |
| #include "base/command_line.h" |
| #include "base/bind.h" |
| #include "base/threading/thread.h" |
| #include "base/files/file_path.h" |
| #include "base/message_loop/message_loop.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/path_service.h" |
| #include "third_party/mojo/src/mojo/public/cpp/application/service_provider_impl.h" |
| |
| NetworkServiceApp::NetworkServiceApp() {} |
| |
| NetworkServiceApp::~NetworkServiceApp() {} |
| |
| void NetworkServiceApp::OnInitialize() { |
| tracing_.Initialize(shell(), &args()); |
| base::FilePath base_path; |
| CHECK(PathService::Get(base::DIR_TEMP, &base_path)); |
| base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); |
| base::CommandLine command_line(args()); |
| if (command_line.HasSwitch("clear")) { |
| scoped_ptr<base::Thread> cache_delete_thread( |
| new base::Thread("network cache delete thread")); |
| cache_delete_thread->Start(); |
| mojo::NetworkContext::ClearCache( |
| base_path, cache_delete_thread->task_runner(), |
| base::Bind(&base::Thread::Stop, |
| base::Owned(cache_delete_thread.release()))); |
| } |
| context_.reset(new mojo::NetworkContext(base_path)); |
| } |
| |
| bool NetworkServiceApp::OnAcceptConnection( |
| mojo::ServiceProviderImpl* service_provider_impl) { |
| DCHECK(context_); |
| service_provider_impl->AddService<mojo::NetworkService>( |
| [this](const mojo::ConnectionContext& connection_context, |
| mojo::InterfaceRequest<mojo::NetworkService> |
| network_service_request) { |
| new mojo::NetworkServiceImpl(network_service_request.Pass(), |
| connection_context, context_.get()); |
| }); |
| return true; |
| } |
| |
| void NetworkServiceApp::OnQuit() { |
| // Destroy the NetworkContext now as it requires MessageLoop::current() upon |
| // destruction and it is the last moment we know for sure that it is |
| // running. |
| context_.reset(); |
| } |