app.cc 4.1 KB
Newer Older
A
Adam Barth 已提交
1 2 3 4 5 6 7 8 9
// 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/app.h"

#include <thread>
#include <utility>

10
#include "apps/icu_data/lib/icu_data.h"
A
Adam Barth 已提交
11
#include "apps/tracing/lib/trace/provider.h"
A
Adam Barth 已提交
12 13 14 15 16 17 18 19 20 21
#include "flutter/common/settings.h"
#include "flutter/common/threads.h"
#include "flutter/sky/engine/platform/fonts/fuchsia/FontCacheFuchsia.h"
#include "lib/ftl/macros.h"
#include "lib/ftl/tasks/task_runner.h"
#include "lib/mtl/tasks/message_loop.h"

namespace flutter_runner {
namespace {

22 23
static App* g_app = nullptr;

A
Adam Barth 已提交
24 25 26 27 28 29 30
void QuitMessageLoop() {
  mtl::MessageLoop::GetCurrent()->QuitNow();
}

}  // namespace

App::App() {
31
  g_app = this;
32
  context_ = app::ApplicationContext::CreateFromStartupInfo();
A
Adam Barth 已提交
33

A
Adam Barth 已提交
34
  tracing::InitializeTracer(context_.get(), {});
A
Adam Barth 已提交
35

36 37
  gpu_thread_ = std::make_unique<Thread>();
  io_thread_ = std::make_unique<Thread>();
A
Adam Barth 已提交
38

39 40
  FTL_CHECK(gpu_thread_->IsValid()) << "Must be able to create the GPU thread";
  FTL_CHECK(io_thread_->IsValid()) << "Must be able to create the IO thread";
A
Adam Barth 已提交
41

42 43 44
  auto ui_task_runner = mtl::MessageLoop::GetCurrent()->task_runner();
  auto gpu_task_runner = gpu_thread_->TaskRunner();
  auto io_task_runner = io_thread_->TaskRunner();
A
Adam Barth 已提交
45 46

  // Notice that the Platform and UI threads are actually the same.
47 48 49 50 51
  blink::Threads::Set(blink::Threads(ui_task_runner,   // Platform
                                     gpu_task_runner,  // GPU
                                     ui_task_runner,   // UI
                                     io_task_runner    // IO
                                     ));
52

53
  if (!icu_data::Initialize(context_.get())) {
54 55 56
    FTL_LOG(ERROR) << "Could not initialize ICU data.";
  }

57 58 59
  blink::Settings settings;
  settings.enable_observatory = true;
  blink::Settings::Set(settings);
60

A
Adam Barth 已提交
61 62 63
  blink::SetFontProvider(
      context_->ConnectToEnvironmentService<fonts::FontProvider>());

64 65
  context_->outgoing_services()->AddService<app::ApplicationRunner>(
      [this](fidl::InterfaceRequest<app::ApplicationRunner> request) {
A
Adam Barth 已提交
66 67 68 69 70
        runner_bindings_.AddBinding(this, std::move(request));
      });
}

App::~App() {
71
  icu_data::Release();
72 73
  blink::Threads::Gpu()->PostTask(QuitMessageLoop);
  blink::Threads::IO()->PostTask(QuitMessageLoop);
74 75 76 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 106 107 108 109 110 111
  g_app = nullptr;
}

App& App::Shared() {
  FTL_DCHECK(g_app);
  return *g_app;
}

void App::WaitForPlatformViewIds(
    std::vector<PlatformViewInfo>* platform_view_ids) {
  ftl::AutoResetWaitableEvent latch;

  blink::Threads::UI()->PostTask([this, platform_view_ids, &latch]() {
    WaitForPlatformViewsIdsUIThread(platform_view_ids, &latch);
  });

  latch.Wait();
}

void App::WaitForPlatformViewsIdsUIThread(
    std::vector<PlatformViewInfo>* platform_view_ids,
    ftl::AutoResetWaitableEvent* latch) {
  for (auto it = controllers_.begin(); it != controllers_.end(); it++) {
    ApplicationControllerImpl* controller = it->first;

    if (!controller) {
      continue;
    }

    PlatformViewInfo info;
    // TODO(zra): We should create real IDs for these instead of relying on the
    // address of the controller. Maybe just use the UI Isolate main port?
    info.view_id = reinterpret_cast<uintptr_t>(controller);
    info.isolate_id = controller->GetUIIsolateMainPort();
    info.isolate_name = controller->GetUIIsolateName();
    platform_view_ids->push_back(info);
  }
  latch->Signal();
A
Adam Barth 已提交
112 113 114
}

void App::StartApplication(
115 116 117
    app::ApplicationPackagePtr application,
    app::ApplicationStartupInfoPtr startup_info,
    fidl::InterfaceRequest<app::ApplicationController> controller) {
A
Adam Barth 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  std::unique_ptr<ApplicationControllerImpl> impl =
      std::make_unique<ApplicationControllerImpl>(this, std::move(application),
                                                  std::move(startup_info),
                                                  std::move(controller));
  ApplicationControllerImpl* key = impl.get();
  controllers_.emplace(key, std::move(impl));
}

void App::Destroy(ApplicationControllerImpl* controller) {
  auto it = controllers_.find(controller);
  if (it == controllers_.end())
    return;
  controllers_.erase(it);
}

}  // namespace flutter_runner