application_controller_impl.cc 2.6 KB
Newer Older
A
Adam Barth 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
// Copyright 2016 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 "flutter/content_handler/application_controller_impl.h"

#include <utility>

#include "apps/modular/lib/app/connect.h"
#include "flutter/content_handler/app.h"
#include "flutter/content_handler/runtime_holder.h"
#include "lib/ftl/logging.h"
#include "lib/mtl/vmo/vector.h"

namespace flutter_runner {

ApplicationControllerImpl::ApplicationControllerImpl(
    App* app,
    modular::ApplicationPackagePtr application,
    modular::ApplicationStartupInfoPtr startup_info,
    fidl::InterfaceRequest<modular::ApplicationController> controller)
    : app_(app), binding_(this) {
  if (controller.is_pending()) {
    binding_.Bind(std::move(controller));
    binding_.set_connection_error_handler([this] {
      app_->Destroy(this);
      // |this| has been deleted at this point.
    });
  }

  std::vector<char> bundle;
  if (!mtl::VectorFromVmo(std::move(application->data), &bundle)) {
    FTL_LOG(ERROR) << "Failed to receive bundle.";
    return;
  }

37 38 39 40
  // TODO(jeffbrown): Decide what to do with command-line arguments and
  // startup handles.

  if (startup_info->launch_info->services) {
A
Adam Barth 已提交
41
    service_provider_bindings_.AddBinding(
42
        this, std::move(startup_info->launch_info->services));
A
Adam Barth 已提交
43 44
  }

45
  url_ = startup_info->launch_info->url;
A
Adam Barth 已提交
46
  runtime_holder_.reset(new RuntimeHolder());
47 48 49
  runtime_holder_->Init(std::move(startup_info->environment),
                        fidl::GetProxy(&dart_service_provider_),
                        std::move(bundle));
A
Adam Barth 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

ApplicationControllerImpl::~ApplicationControllerImpl() = default;

void ApplicationControllerImpl::Kill(const KillCallback& callback) {
  runtime_holder_.reset();
  app_->Destroy(this);
  // |this| has been deleted at this point.
}

void ApplicationControllerImpl::Detach() {
  binding_.set_connection_error_handler(ftl::Closure());
}

void ApplicationControllerImpl::ConnectToService(
    const fidl::String& service_name,
66
    mx::channel channel) {
A
Adam Barth 已提交
67 68
  if (service_name == mozart::ViewProvider::Name_) {
    view_provider_bindings_.AddBinding(
69 70 71
        this, fidl::InterfaceRequest<mozart::ViewProvider>(std::move(channel)));
  } else {
    dart_service_provider_->ConnectToService(service_name, std::move(channel));
A
Adam Barth 已提交
72 73 74 75 76 77 78 79 80 81 82
  }
}

void ApplicationControllerImpl::CreateView(
    fidl::InterfaceRequest<mozart::ViewOwner> view_owner_request,
    fidl::InterfaceRequest<modular::ServiceProvider> services) {
  runtime_holder_->CreateView(url_, std::move(view_owner_request),
                              std::move(services));
}

}  // namespace flutter_runner