application_controller_impl.cc 4.2 KB
Newer Older
A
Adam Barth 已提交
1 2 3 4 5 6 7 8
// 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>

9 10 11
#include <magenta/status.h>
#include <mxio/namespace.h>

12
#include "application/lib/app/connect.h"
A
Adam Barth 已提交
13 14 15 16 17 18 19 20 21
#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,
22 23 24
    app::ApplicationPackagePtr application,
    app::ApplicationStartupInfoPtr startup_info,
    fidl::InterfaceRequest<app::ApplicationController> controller)
A
Adam Barth 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    : 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;
  }

40 41 42 43
  // TODO(jeffbrown): Decide what to do with command-line arguments and
  // startup handles.

  if (startup_info->launch_info->services) {
44 45
    service_provider_bridge_.AddBinding(
        std::move(startup_info->launch_info->services));
A
Adam Barth 已提交
46 47
  }

48 49 50 51 52 53 54 55 56 57 58 59 60 61
  if (startup_info->launch_info->service_request.is_valid()) {
    service_provider_bridge_.ServeDirectory(
        std::move(startup_info->launch_info->service_request));
  }

  service_provider_bridge_.AddService<mozart::ViewProvider>(
      [this](fidl::InterfaceRequest<mozart::ViewProvider> request) {
    view_provider_bindings_.AddBinding(this, std::move(request));
  });

  app::ServiceProviderPtr service_provider;
  auto request = service_provider.NewRequest();
  service_provider_bridge_.set_backend(std::move(service_provider));

62 63 64 65 66
  mxio_ns_t* mxio_ns = SetupNamespace(startup_info->flat_namespace);
  if (mxio_ns == nullptr) {
    FTL_LOG(ERROR) << "Failed to initialize namespace";
  }

67
  url_ = startup_info->launch_info->url;
A
Adam Barth 已提交
68
  runtime_holder_.reset(new RuntimeHolder());
69
  runtime_holder_->Init(
70
      mxio_ns,
71
      app::ApplicationContext::CreateFrom(std::move(startup_info)),
72
      std::move(request), std::move(bundle));
A
Adam Barth 已提交
73 74 75 76
}

ApplicationControllerImpl::~ApplicationControllerImpl() = default;

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
constexpr char kServiceRootPath[] = "/svc";

mxio_ns_t* ApplicationControllerImpl::SetupNamespace(
    const app::FlatNamespacePtr& flat) {
  mxio_ns_t* mxio_namespc;
  mx_status_t status = mxio_ns_create(&mxio_namespc);
  if (status != MX_OK) {
    FTL_LOG(ERROR) << "Failed to create namespace";
    return nullptr;
  }
  for (size_t i = 0; i < flat->paths.size(); ++i) {
    if (flat->paths[i] == kServiceRootPath) {
      // Ownership of /svc goes to the ApplicationContext created above.
      continue;
    }
    mx::channel dir = std::move(flat->directories[i]);
    mx_handle_t dir_handle = dir.release();
    const char* path = flat->paths[i].data();
    status = mxio_ns_bind(mxio_namespc, path, dir_handle);
    if (status != MX_OK) {
      FTL_LOG(ERROR) << "Failed to bind " << flat->paths[i] << " to namespace";
      mx_handle_close(dir_handle);
      mxio_ns_destroy(mxio_namespc);
      return nullptr;
    }
  }
  return mxio_namespc;
}

106
void ApplicationControllerImpl::Kill() {
A
Adam Barth 已提交
107 108 109 110 111 112 113 114 115 116 117
  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::CreateView(
    fidl::InterfaceRequest<mozart::ViewOwner> view_owner_request,
118
    fidl::InterfaceRequest<app::ServiceProvider> services) {
A
Adam Barth 已提交
119 120 121 122
  runtime_holder_->CreateView(url_, std::move(view_owner_request),
                              std::move(services));
}

123 124 125 126 127 128 129 130 131 132 133 134 135
Dart_Port ApplicationControllerImpl::GetUIIsolateMainPort() {
  if (!runtime_holder_)
    return ILLEGAL_PORT;
  return runtime_holder_->GetUIIsolateMainPort();
}

std::string ApplicationControllerImpl::GetUIIsolateName() {
  if (!runtime_holder_) {
    return "";
  }
  return runtime_holder_->GetUIIsolateName();
}

A
Adam Barth 已提交
136
}  // namespace flutter_runner