11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright 2014 The Chromium Authors. All rights reserved.
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Use of this source code is governed by a BSD-style license that can be
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// found in the LICENSE file.
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/apps/js/application_delegate_impl.h"
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/apps/js/js_app.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/public/cpp/application/application_impl.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace mojo {
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace apps {
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciContentHandlerImpl::ContentHandlerImpl(ApplicationDelegateImpl* app)
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : content_handler_(app) {
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciContentHandlerImpl::~ContentHandlerImpl() {
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ContentHandlerImpl::OnConnect(
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const mojo::String& url,
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    URLResponsePtr content,
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    InterfaceRequest<ServiceProvider> service_provider) {
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  content_handler_->StartJSApp(url.To<std::string>(), content.Pass());
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciApplicationDelegateImpl::ApplicationDelegateImpl()
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : application_impl_(NULL), content_handler_factory_(this) {
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ApplicationDelegateImpl::Initialize(ApplicationImpl* app) {
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  application_impl_ = app;
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciApplicationDelegateImpl::~ApplicationDelegateImpl() {
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool ApplicationDelegateImpl::ConfigureIncomingConnection(
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ApplicationConnection* connection) {
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  connection->AddService(&content_handler_factory_);
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return true;
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ApplicationDelegateImpl::StartJSApp(const std::string& url,
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         URLResponsePtr content) {
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  JSApp* app = new JSApp(this, url, content.Pass());
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  app_vector_.push_back(app);
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // TODO(hansmuller): deal with the Start() return value.
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  app->Start();
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ApplicationDelegateImpl::QuitJSApp(JSApp* app) {
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  AppVector::iterator itr =
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      std::find(app_vector_.begin(), app_vector_.end(), app);
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (itr != app_vector_.end())
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    app_vector_.erase(itr);
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ApplicationDelegateImpl::ConnectToService(
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ScopedMessagePipeHandle pipe_handle,
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::string& application_url,
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::string& interface_name) {
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CHECK(application_impl_);
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ServiceProvider* service_provider =
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      application_impl_->ConnectToApplication(application_url)
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          ->GetServiceProvider();
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  service_provider->ConnectToService(interface_name, pipe_handle.Pass());
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace apps
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace mojo
72