blob: eb3b73be91f3041cdab8c2f80503da028e798682 [file] [log] [blame]
// 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_delegate.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/application_connection.h"
NetworkServiceDelegate::NetworkServiceDelegate() {}
NetworkServiceDelegate::~NetworkServiceDelegate() {}
void NetworkServiceDelegate::Initialize(mojo::ApplicationImpl* app) {
tracing_.Initialize(app);
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(app->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 NetworkServiceDelegate::ConfigureIncomingConnection(
mojo::ApplicationConnection* connection) {
DCHECK(context_);
connection->AddService(this);
return true;
}
void NetworkServiceDelegate::Quit() {
// 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();
}
void NetworkServiceDelegate::Create(
mojo::ApplicationConnection* connection,
mojo::InterfaceRequest<mojo::NetworkService> request) {
new mojo::NetworkServiceImpl(request.Pass(), connection, context_.get());
}