window.cc 10.1 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
#include "flutter/lib/ui/window/window.h"
6

7
#include "flutter/lib/ui/compositing/scene.h"
8
#include "flutter/lib/ui/ui_dart_state.h"
A
Adam Barth 已提交
9
#include "flutter/lib/ui/window/platform_message_response_dart.h"
10
#include "lib/tonic/converter/dart_converter.h"
11
#include "lib/tonic/dart_args.h"
12
#include "lib/tonic/dart_library_natives.h"
13
#include "lib/tonic/dart_microtask_queue.h"
14
#include "lib/tonic/logging/dart_invoke.h"
15
#include "lib/tonic/typed_data/dart_byte_data.h"
16

17
using tonic::DartInvokeField;
18
using tonic::DartState;
19 20 21
using tonic::StdStringToDart;
using tonic::ToDart;

22 23 24
namespace blink {
namespace {

25
void DefaultRouteName(Dart_NativeArguments args) {
26 27
  std::string routeName =
      UIDartState::Current()->window()->client()->DefaultRouteName();
28 29 30
  Dart_SetReturnValue(args, StdStringToDart(routeName));
}

31
void ScheduleFrame(Dart_NativeArguments args) {
32
  UIDartState::Current()->window()->client()->ScheduleFrame();
33 34 35 36
}

void Render(Dart_NativeArguments args) {
  Dart_Handle exception = nullptr;
37 38
  Scene* scene =
      tonic::DartConverter<Scene*>::FromArguments(args, 1, exception);
39 40 41 42
  if (exception) {
    Dart_ThrowException(exception);
    return;
  }
43
  UIDartState::Current()->window()->client()->Render(scene);
44 45
}

46 47 48 49 50 51 52 53 54 55 56
void UpdateSemantics(Dart_NativeArguments args) {
  Dart_Handle exception = nullptr;
  SemanticsUpdate* update =
      tonic::DartConverter<SemanticsUpdate*>::FromArguments(args, 1, exception);
  if (exception) {
    Dart_ThrowException(exception);
    return;
  }
  UIDartState::Current()->window()->client()->UpdateSemantics(update);
}

57 58 59 60
Dart_Handle SendPlatformMessage(Dart_Handle window,
                                const std::string& name,
                                Dart_Handle callback,
                                const tonic::DartByteData& data) {
61
  UIDartState* dart_state = UIDartState::Current();
A
Adam Barth 已提交
62

63 64 65 66 67 68
  if (!dart_state->window()) {
    // Must release the TypedData buffer before allocating other Dart objects.
    data.Release();
    return ToDart("Platform messages can only be sent from the main isolate");
  }

69
  fxl::RefPtr<PlatformMessageResponse> response;
A
Adam Barth 已提交
70
  if (!Dart_IsNull(callback)) {
71
    response = fxl::MakeRefCounted<PlatformMessageResponseDart>(
72 73
        tonic::DartPersistentValue(dart_state, callback),
        dart_state->GetTaskRunners().GetUITaskRunner());
A
Adam Barth 已提交
74
  }
75
  if (Dart_IsNull(data.dart_handle())) {
76
    dart_state->window()->client()->HandlePlatformMessage(
77
        fxl::MakeRefCounted<PlatformMessage>(name, response));
78 79 80
  } else {
    const uint8_t* buffer = static_cast<const uint8_t*>(data.data());

81
    dart_state->window()->client()->HandlePlatformMessage(
82
        fxl::MakeRefCounted<PlatformMessage>(
83 84 85
            name, std::vector<uint8_t>(buffer, buffer + data.length_in_bytes()),
            response));
  }
86 87

  return Dart_Null();
88 89 90 91 92 93
}

void _SendPlatformMessage(Dart_NativeArguments args) {
  tonic::DartCallStatic(&SendPlatformMessage, args);
}

A
Adam Barth 已提交
94 95 96
void RespondToPlatformMessage(Dart_Handle window,
                              int response_id,
                              const tonic::DartByteData& data) {
97 98 99 100
  if (Dart_IsNull(data.dart_handle())) {
    UIDartState::Current()->window()->CompletePlatformMessageEmptyResponse(
        response_id);
  } else {
101
    // TODO(engine): Avoid this copy.
102 103 104 105 106
    const uint8_t* buffer = static_cast<const uint8_t*>(data.data());
    UIDartState::Current()->window()->CompletePlatformMessageResponse(
        response_id,
        std::vector<uint8_t>(buffer, buffer + data.length_in_bytes()));
  }
A
Adam Barth 已提交
107 108 109 110 111 112
}

void _RespondToPlatformMessage(Dart_NativeArguments args) {
  tonic::DartCallStatic(&RespondToPlatformMessage, args);
}

113 114
}  // namespace

115
Dart_Handle ToByteData(const std::vector<uint8_t>& buffer) {
116 117
  Dart_Handle data_handle =
      Dart_NewTypedData(Dart_TypedData_kByteData, buffer.size());
118 119 120 121 122 123 124 125 126 127 128 129 130 131
  if (Dart_IsError(data_handle))
    return data_handle;

  Dart_TypedData_Type type;
  void* data = nullptr;
  intptr_t num_bytes = 0;
  FXL_CHECK(!Dart_IsError(
      Dart_TypedDataAcquireData(data_handle, &type, &data, &num_bytes)));

  memcpy(data, buffer.data(), num_bytes);
  Dart_TypedDataReleaseData(data_handle);
  return data_handle;
}

A
Adam Barth 已提交
132
WindowClient::~WindowClient() {}
133

A
Adam Barth 已提交
134
Window::Window(WindowClient* client) : client_(client) {}
135

A
Adam Barth 已提交
136
Window::~Window() {}
137 138 139 140 141

void Window::DidCreateIsolate() {
  library_.Set(DartState::Current(), Dart_LookupLibrary(ToDart("dart:ui")));
}

142
void Window::UpdateWindowMetrics(const ViewportMetrics& metrics) {
143 144
  viewport_metrics_ = metrics;

145
  tonic::DartState* dart_state = library_.dart_state().get();
146 147
  if (!dart_state)
    return;
148
  tonic::DartState::Scope scope(dart_state);
149 150 151 152 153 154 155 156 157 158 159 160 161 162
  DartInvokeField(library_.value(), "_updateWindowMetrics",
                  {
                      ToDart(metrics.device_pixel_ratio),
                      ToDart(metrics.physical_width),
                      ToDart(metrics.physical_height),
                      ToDart(metrics.physical_padding_top),
                      ToDart(metrics.physical_padding_right),
                      ToDart(metrics.physical_padding_bottom),
                      ToDart(metrics.physical_padding_left),
                      ToDart(metrics.physical_view_inset_top),
                      ToDart(metrics.physical_view_inset_right),
                      ToDart(metrics.physical_view_inset_bottom),
                      ToDart(metrics.physical_view_inset_left),
                  });
163 164
}

165 166
void Window::UpdateLocale(const std::string& language_code,
                          const std::string& country_code) {
167
  tonic::DartState* dart_state = library_.dart_state().get();
168 169
  if (!dart_state)
    return;
170
  tonic::DartState::Scope scope(dart_state);
171

172 173 174 175 176
  DartInvokeField(library_.value(), "_updateLocale",
                  {
                      StdStringToDart(language_code),
                      StdStringToDart(country_code),
                  });
177 178
}

179
void Window::UpdateUserSettingsData(const std::string& data) {
180 181 182 183 184
  tonic::DartState* dart_state = library_.dart_state().get();
  if (!dart_state)
    return;
  tonic::DartState::Scope scope(dart_state);

185
  DartInvokeField(library_.value(), "_updateUserSettingsData",
186
                  {
187
                      StdStringToDart(data),
188 189 190
                  });
}

191 192 193 194 195 196 197 198 199 200
void Window::UpdateSemanticsEnabled(bool enabled) {
  tonic::DartState* dart_state = library_.dart_state().get();
  if (!dart_state)
    return;
  tonic::DartState::Scope scope(dart_state);

  DartInvokeField(library_.value(), "_updateSemanticsEnabled",
                  {ToDart(enabled)});
}

201 202 203 204 205 206 207 208 209 210
void Window::UpdateAssistiveTechnologyEnabled(bool enabled) {
  tonic::DartState* dart_state = library_.dart_state().get();
  if (!dart_state)
    return;
  tonic::DartState::Scope scope(dart_state);

  DartInvokeField(library_.value(), "_updateAssistiveTechnologyEnabled",
                  {ToDart(enabled)});
}

211
void Window::DispatchPlatformMessage(fxl::RefPtr<PlatformMessage> message) {
212 213 214 215
  tonic::DartState* dart_state = library_.dart_state().get();
  if (!dart_state)
    return;
  tonic::DartState::Scope scope(dart_state);
216 217
  Dart_Handle data_handle =
      (message->hasData()) ? ToByteData(message->data()) : Dart_Null();
218 219 220
  if (Dart_IsError(data_handle))
    return;

A
Adam Barth 已提交
221 222 223 224 225
  int response_id = 0;
  if (auto response = message->response()) {
    response_id = next_response_id_++;
    pending_responses_[response_id] = response;
  }
226

227 228 229
  DartInvokeField(
      library_.value(), "_dispatchPlatformMessage",
      {ToDart(message->channel()), data_handle, ToDart(response_id)});
A
Adam Barth 已提交
230
}
231

A
Adam Barth 已提交
232 233 234 235 236 237 238 239 240
void Window::DispatchPointerDataPacket(const PointerDataPacket& packet) {
  tonic::DartState* dart_state = library_.dart_state().get();
  if (!dart_state)
    return;
  tonic::DartState::Scope scope(dart_state);

  Dart_Handle data_handle = ToByteData(packet.data());
  if (Dart_IsError(data_handle))
    return;
241 242 243 244
  DartInvokeField(library_.value(), "_dispatchPointerDataPacket",
                  {data_handle});
}

245 246 247
void Window::DispatchSemanticsAction(int32_t id,
                                     SemanticsAction action,
                                     std::vector<uint8_t> args) {
248 249 250 251 252
  tonic::DartState* dart_state = library_.dart_state().get();
  if (!dart_state)
    return;
  tonic::DartState::Scope scope(dart_state);

253 254 255 256 257 258 259 260
  Dart_Handle args_handle = (args.empty()) ? Dart_Null() : ToByteData(args);

  if (Dart_IsError(args_handle))
    return;

  DartInvokeField(
      library_.value(), "_dispatchSemanticsAction",
      {ToDart(id), ToDart(static_cast<int32_t>(action)), args_handle});
261 262
}

263
void Window::BeginFrame(fxl::TimePoint frameTime) {
264
  tonic::DartState* dart_state = library_.dart_state().get();
265 266
  if (!dart_state)
    return;
267
  tonic::DartState::Scope scope(dart_state);
268

269
  int64_t microseconds = (frameTime - fxl::TimePoint()).ToMicroseconds();
270

A
Adam Barth 已提交
271 272 273 274
  DartInvokeField(library_.value(), "_beginFrame",
                  {
                      Dart_NewInteger(microseconds),
                  });
275

276
  UIDartState::Current()->FlushMicrotasksNow();
277 278

  DartInvokeField(library_.value(), "_drawFrame", {});
279 280
}

281 282 283 284 285 286 287 288 289 290 291
void Window::CompletePlatformMessageEmptyResponse(int response_id) {
  if (!response_id)
    return;
  auto it = pending_responses_.find(response_id);
  if (it == pending_responses_.end())
    return;
  auto response = std::move(it->second);
  pending_responses_.erase(it);
  response->CompleteEmpty();
}

A
Adam Barth 已提交
292
void Window::CompletePlatformMessageResponse(int response_id,
293
                                             std::vector<uint8_t> data) {
A
Adam Barth 已提交
294 295 296 297 298 299 300
  if (!response_id)
    return;
  auto it = pending_responses_.find(response_id);
  if (it == pending_responses_.end())
    return;
  auto response = std::move(it->second);
  pending_responses_.erase(it);
301
  response->Complete(std::make_unique<fml::DataMapping>(std::move(data)));
A
Adam Barth 已提交
302 303
}

304
void Window::RegisterNatives(tonic::DartLibraryNatives* natives) {
305
  natives->Register({
306
      {"Window_defaultRouteName", DefaultRouteName, 1, true},
307 308
      {"Window_scheduleFrame", ScheduleFrame, 1, true},
      {"Window_sendPlatformMessage", _SendPlatformMessage, 4, true},
A
Adam Barth 已提交
309
      {"Window_respondToPlatformMessage", _RespondToPlatformMessage, 3, true},
310 311 312
      {"Window_render", Render, 2, true},
      {"Window_updateSemantics", UpdateSemantics, 2, true},
  });
313 314 315
}

}  // namespace blink