1// Copyright 2014 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.
4
5#include "mojo/shell/network_application_loader.h"
6
7#include "base/base_paths.h"
8#include "base/files/file_path.h"
9#include "base/path_service.h"
10#include "mojo/public/cpp/application/application_connection.h"
11#include "mojo/public/cpp/application/application_delegate.h"
12#include "mojo/public/cpp/application/application_impl.h"
13#include "mojo/services/network/network_service_impl.h"
14
15namespace {
16base::FilePath GetBasePath() {
17  base::FilePath path;
18  CHECK(PathService::Get(base::DIR_TEMP, &path));
19  return path.Append(FILE_PATH_LITERAL("network_service"));
20}
21}
22
23namespace mojo {
24namespace shell {
25
26NetworkApplicationLoader::NetworkApplicationLoader() {
27}
28
29NetworkApplicationLoader::~NetworkApplicationLoader() {
30}
31
32void NetworkApplicationLoader::Load(ApplicationManager* manager,
33                                    const GURL& url,
34                                    scoped_refptr<LoadCallbacks> callbacks) {
35  ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication();
36  if (!shell_handle.is_valid())
37    return;
38
39  uintptr_t key = reinterpret_cast<uintptr_t>(manager);
40  if (apps_.find(key) == apps_.end()) {
41    scoped_ptr<ApplicationImpl> app(
42        new ApplicationImpl(this, shell_handle.Pass()));
43    apps_.add(key, app.Pass());
44  }
45}
46
47void NetworkApplicationLoader::OnApplicationError(ApplicationManager* manager,
48                                                  const GURL& url) {
49  apps_.erase(reinterpret_cast<uintptr_t>(manager));
50}
51
52void NetworkApplicationLoader::Initialize(ApplicationImpl* app) {
53  // The context must be created on the same thread as the network service.
54  context_.reset(new NetworkContext(GetBasePath()));
55}
56
57bool NetworkApplicationLoader::ConfigureIncomingConnection(
58    ApplicationConnection* connection) {
59  connection->AddService(this);
60  return true;
61}
62
63void NetworkApplicationLoader::Create(
64    ApplicationConnection* connection,
65    InterfaceRequest<NetworkService> request) {
66  BindToRequest(new NetworkServiceImpl(connection, context_.get()), &request);
67}
68
69}  // namespace shell
70}  // namespace mojo
71