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/apps/js/application_delegate_impl.h"
6
7#include "mojo/apps/js/js_app.h"
8#include "mojo/public/cpp/application/application_impl.h"
9
10namespace mojo {
11namespace apps {
12
13ContentHandlerImpl::ContentHandlerImpl(ApplicationDelegateImpl* app)
14    : content_handler_(app) {
15}
16
17ContentHandlerImpl::~ContentHandlerImpl() {
18}
19
20void ContentHandlerImpl::OnConnect(
21    const mojo::String& url,
22    URLResponsePtr content,
23    InterfaceRequest<ServiceProvider> service_provider) {
24  content_handler_->StartJSApp(url.To<std::string>(), content.Pass());
25}
26
27ApplicationDelegateImpl::ApplicationDelegateImpl()
28    : application_impl_(NULL), content_handler_factory_(this) {
29}
30
31void ApplicationDelegateImpl::Initialize(ApplicationImpl* app) {
32  application_impl_ = app;
33}
34
35ApplicationDelegateImpl::~ApplicationDelegateImpl() {
36}
37
38bool ApplicationDelegateImpl::ConfigureIncomingConnection(
39    ApplicationConnection* connection) {
40  connection->AddService(&content_handler_factory_);
41  return true;
42}
43
44void ApplicationDelegateImpl::StartJSApp(const std::string& url,
45                                         URLResponsePtr content) {
46  JSApp* app = new JSApp(this, url, content.Pass());
47  app_vector_.push_back(app);
48  // TODO(hansmuller): deal with the Start() return value.
49  app->Start();
50}
51
52void ApplicationDelegateImpl::QuitJSApp(JSApp* app) {
53  AppVector::iterator itr =
54      std::find(app_vector_.begin(), app_vector_.end(), app);
55  if (itr != app_vector_.end())
56    app_vector_.erase(itr);
57}
58
59void ApplicationDelegateImpl::ConnectToService(
60    ScopedMessagePipeHandle pipe_handle,
61    const std::string& application_url,
62    const std::string& interface_name) {
63  CHECK(application_impl_);
64  ServiceProvider* service_provider =
65      application_impl_->ConnectToApplication(application_url)
66          ->GetServiceProvider();
67  service_provider->ConnectToService(interface_name, pipe_handle.Pass());
68}
69
70}  // namespace apps
71}  // namespace mojo
72