forked from flutter-team-archive/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication_controller_impl.cc
More file actions
146 lines (122 loc) · 4.44 KB
/
Copy pathapplication_controller_impl.cc
File metadata and controls
146 lines (122 loc) · 4.44 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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 <fdio/namespace.h>
#include <zircon/status.h>
#include "flutter/content_handler/app.h"
#include "flutter/content_handler/runtime_holder.h"
#include "lib/app/cpp/connect.h"
#include "lib/fsl/vmo/vector.h"
#include "lib/fxl/logging.h"
namespace flutter_runner {
ApplicationControllerImpl::ApplicationControllerImpl(
App* app,
app::ApplicationPackagePtr application,
app::ApplicationStartupInfoPtr startup_info,
f1dl::InterfaceRequest<app::ApplicationController> controller)
: app_(app), binding_(this) {
if (controller.is_valid()) {
binding_.Bind(std::move(controller));
binding_.set_error_handler([this] {
app_->Destroy(this);
// |this| has been deleted at this point.
});
}
std::vector<char> bundle;
if (application->data) {
if (!fsl::VectorFromVmo(std::move(application->data), &bundle)) {
FXL_LOG(ERROR) << "Failed to receive bundle.";
return;
}
}
// TODO(jeffbrown): Decide what to do with command-line arguments and
// startup handles.
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](f1dl::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));
fdio_ns_t* fdio_ns = SetupNamespace(startup_info->flat_namespace);
if (fdio_ns == nullptr) {
FXL_LOG(ERROR) << "Failed to initialize namespace";
return;
}
url_ = startup_info->launch_info->url;
runtime_holder_.reset(new RuntimeHolder());
runtime_holder_->SetMainIsolateShutdownCallback([this]() { Kill(); });
runtime_holder_->Init(
fdio_ns, app::ApplicationContext::CreateFrom(std::move(startup_info)),
std::move(request), std::move(bundle));
}
ApplicationControllerImpl::~ApplicationControllerImpl() = default;
constexpr char kServiceRootPath[] = "/svc";
fdio_ns_t* ApplicationControllerImpl::SetupNamespace(
const app::FlatNamespacePtr& flat) {
fdio_ns_t* fdio_namespc;
zx_status_t status = fdio_ns_create(&fdio_namespc);
if (status != ZX_OK) {
FXL_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;
}
zx::channel dir = std::move(flat->directories[i]);
zx_handle_t dir_handle = dir.release();
const char* path = flat->paths[i].data();
status = fdio_ns_bind(fdio_namespc, path, dir_handle);
if (status != ZX_OK) {
FXL_LOG(ERROR) << "Failed to bind " << flat->paths[i] << " to namespace";
zx_handle_close(dir_handle);
fdio_ns_destroy(fdio_namespc);
return nullptr;
}
}
return fdio_namespc;
}
void ApplicationControllerImpl::Kill() {
SendReturnCode(runtime_holder_->return_code());
runtime_holder_.reset();
app_->Destroy(this);
// |this| has been deleted at this point.
}
void ApplicationControllerImpl::Detach() {
binding_.set_error_handler(fxl::Closure());
}
void ApplicationControllerImpl::Wait(const WaitCallback& callback) {
wait_callbacks_.push_back(callback);
}
void ApplicationControllerImpl::SendReturnCode(int32_t return_code) {
for (const auto& iter : wait_callbacks_) {
iter(return_code);
}
wait_callbacks_.clear();
}
void ApplicationControllerImpl::CreateView(
f1dl::InterfaceRequest<mozart::ViewOwner> view_owner_request,
f1dl::InterfaceRequest<app::ServiceProvider> services) {
runtime_holder_->CreateView(url_, std::move(view_owner_request),
std::move(services));
}
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();
}
} // namespace flutter_runner