提交 4d09067a 编写于 作者: J John McCutchan

Unify timer implementation between Flutter and Mojo embedders

上级 545f9000
......@@ -24,7 +24,6 @@
#include "sky/engine/tonic/dart_library_natives.h"
#include "sky/engine/tonic/dart_microtask_queue.h"
#include "sky/engine/tonic/dart_state.h"
#include "sky/engine/tonic/dart_timer_heap.h"
#include "sky/engine/wtf/text/WTFString.h"
#if defined(OS_ANDROID)
......@@ -50,9 +49,7 @@ namespace blink {
#define BUILTIN_NATIVE_LIST(V) \
V(Logger_PrintString, 1) \
V(ScheduleMicrotask, 1) \
V(GetBaseURLString, 0) \
V(Timer_create, 3) \
V(Timer_cancel, 1)
V(GetBaseURLString, 0)
BUILTIN_NATIVE_LIST(DECLARE_FUNCTION);
......@@ -72,7 +69,6 @@ static Dart_Handle GetClosure(Dart_Handle builtin_library, const char* name) {
static void InitDartInternal(Dart_Handle builtin_library,
DartRuntimeHooks::IsolateType isolate_type) {
Dart_Handle print = GetClosure(builtin_library, "_getPrintClosure");
Dart_Handle timer = GetClosure(builtin_library, "_getCreateTimerClosure");
Dart_Handle internal_library = Dart_LookupLibrary(ToDart("dart:_internal"));
......@@ -80,11 +76,19 @@ static void InitDartInternal(Dart_Handle builtin_library,
internal_library, ToDart("_printClosure"), print));
if (isolate_type == DartRuntimeHooks::MainIsolate) {
Dart_Handle vm_hooks_name = ToDart("VMLibraryHooks");
Dart_Handle vm_hooks = Dart_GetClass(internal_library, vm_hooks_name);
DART_CHECK_VALID(vm_hooks);
Dart_Handle timer_name = ToDart("timerFactory");
DART_CHECK_VALID(Dart_SetField(vm_hooks, timer_name, timer));
// Import internal_library into builtin_library.
DART_CHECK_VALID(Dart_LibraryImportLibrary(builtin_library,
internal_library,
Dart_Null()));
// Call |_setupHooks| to configure |VMLibraryHooks|.
Dart_Handle method_name =
Dart_NewStringFromCString("_setupHooks");
DART_CHECK_VALID(Dart_Invoke(builtin_library, method_name, 0, NULL))
// Call |_setupHooks| to configure |VMLibraryHooks|.
Dart_Handle isolate_lib = Dart_LookupLibrary(ToDart("dart:isolate"));
DART_CHECK_VALID(isolate_lib);
DART_CHECK_VALID(Dart_Invoke(isolate_lib, method_name, 0, NULL));
} else {
CHECK(isolate_type == DartRuntimeHooks::SecondaryIsolate);
Dart_Handle io_lib = Dart_LookupLibrary(ToDart("dart:io"));
......@@ -186,35 +190,4 @@ void GetBaseURLString(Dart_NativeArguments args) {
Dart_SetReturnValue(args, ToDart(UIDartState::Current()->url()));
}
void Timer_create(Dart_NativeArguments args) {
int64_t milliseconds = 0;
DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &milliseconds));
Dart_Handle closure = Dart_GetNativeArgument(args, 1);
DART_CHECK_VALID(closure);
CHECK(Dart_IsClosure(closure));
bool repeating = false;
DART_CHECK_VALID(Dart_GetNativeBooleanArgument(args, 2, &repeating));
DartState* state = DartState::Current();
CHECK(state);
std::unique_ptr<DartTimerHeap::Task> task =
std::unique_ptr<DartTimerHeap::Task>(new DartTimerHeap::Task);
task->closure.Set(state, closure);
task->delay = base::TimeDelta::FromMilliseconds(milliseconds);
task->repeating = repeating;
int timer_id = state->timer_heap().Add(std::move(task));
Dart_SetIntegerReturnValue(args, timer_id);
}
void Timer_cancel(Dart_NativeArguments args) {
int64_t timer_id = 0;
DART_CHECK_VALID(Dart_GetNativeIntegerArgument(args, 0, &timer_id));
DartState* state = DartState::Current();
CHECK(state);
state->timer_heap().Remove(timer_id);
}
} // namespace blink
......@@ -11,10 +11,10 @@
/// pointer input system, and functions for image decoding.
library dart_ui;
import 'dart:async';
import 'dart:math' as math;
import 'dart:mojo.internal';
import 'dart:nativewrappers';
import 'dart:typed_data';
import 'dart:math' as math;
part 'Color.dart';
part 'FilterQuality.dart';
......
......@@ -12,6 +12,7 @@ dart:ui,::,_getScheduleMicrotaskClosure
dart:ui,::,_onAppLifecycleStateChanged
dart:ui,::,_popRoute
dart:ui,::,_pushRoute
dart:ui,::,_setupHooks
dart:ui,::,_updateLocale
dart:ui,::,_updateWindowMetrics
dart:ui,::,takeRootBundleHandle
......
......@@ -13,46 +13,19 @@ class _Logger {
static void _printString(String s) native "Logger_PrintString";
}
class _Timer implements Timer {
_Timer(int milliseconds,
void callback(Timer timer),
bool repeating) {
_id = _create(milliseconds, () {
if (!repeating)
_id = 0;
callback(this);
}, repeating);
}
void cancel() {
_cancel(_id);
_id = 0;
}
bool get isActive => _id != 0;
static int _create(int milliseconds,
void callback(),
bool repeating) native "Timer_create";
static void _cancel(int id) native "Timer_cancel";
int _id;
_setupHooks() {
// Wire up timer implementation that is driven by MojoHandleWatcher.
VMLibraryHooks.eventHandlerSendData = MojoHandleWatcher.timer;
VMLibraryHooks.timerMillisecondClock = MojoCoreNatives.timerMillisecondClock;
}
void _scheduleMicrotask(void callback()) native "ScheduleMicrotask";
Timer _createTimer(int milliseconds,
void callback(Timer timer),
bool repeating) {
return new _Timer(milliseconds, callback, repeating);
}
String _getBaseURLString() native "GetBaseURLString";
Uri _getBaseURL() => Uri.parse(_getBaseURLString());
_getPrintClosure() => _print;
_getScheduleMicrotaskClosure() => _scheduleMicrotask;
_getGetBaseURLClosure() =>_getBaseURL;
_getCreateTimerClosure() => _createTimer;
_getGetBaseURLClosure() => _getBaseURL;
// Though the "main" symbol is not included in any of the libraries imported
// above, the builtin library will be included manually during VM setup. This
......
......@@ -43,8 +43,6 @@ source_set("tonic") {
"dart_snapshot_loader.h",
"dart_state.cc",
"dart_state.h",
"dart_timer_heap.cc",
"dart_timer_heap.h",
"dart_wrappable.cc",
"dart_wrappable.h",
"dart_wrapper_info.h",
......
......@@ -9,7 +9,6 @@
#include "sky/engine/tonic/dart_exception_factory.h"
#include "sky/engine/tonic/dart_library_loader.h"
#include "sky/engine/tonic/dart_message_handler.h"
#include "sky/engine/tonic/dart_timer_heap.h"
namespace blink {
......@@ -24,7 +23,6 @@ DartState::DartState()
class_library_(std::unique_ptr<DartClassLibrary>(new DartClassLibrary)),
exception_factory_(new DartExceptionFactory(this)),
library_loader_(new DartLibraryLoader(this)),
timer_heap_(new DartTimerHeap()),
message_handler_(std::unique_ptr<DartMessageHandler>(
new DartMessageHandler())),
weak_factory_(this) {
......
......@@ -50,7 +50,6 @@ class DartState {
DartClassLibrary& class_library() { return *class_library_; }
DartExceptionFactory& exception_factory() { return *exception_factory_; }
DartLibraryLoader& library_loader() { return *library_loader_; }
DartTimerHeap& timer_heap() { return *timer_heap_; }
DartMessageHandler& message_handler() { return *message_handler_; }
Dart_Handle index_handle() { return index_handle_.value(); }
......@@ -62,7 +61,6 @@ class DartState {
std::unique_ptr<DartClassLibrary> class_library_;
std::unique_ptr<DartExceptionFactory> exception_factory_;
std::unique_ptr<DartLibraryLoader> library_loader_;
std::unique_ptr<DartTimerHeap> timer_heap_;
std::unique_ptr<DartMessageHandler> message_handler_;
DartPersistentValue index_handle_;
......
// 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.
#include "sky/engine/tonic/dart_timer_heap.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/time/time.h"
#include "sky/engine/tonic/dart_api_scope.h"
#include "sky/engine/tonic/dart_invoke.h"
#include "sky/engine/tonic/dart_isolate_scope.h"
#include "sky/engine/tonic/dart_state.h"
namespace blink {
DartTimerHeap::DartTimerHeap() : next_timer_id_(1), weak_factory_(this) {
}
DartTimerHeap::~DartTimerHeap() {
}
int DartTimerHeap::Add(std::unique_ptr<Task> task) {
int id = next_timer_id_++;
Schedule(id, std::move(task));
return id;
}
void DartTimerHeap::Remove(int id) {
heap_.erase(id);
}
void DartTimerHeap::Schedule(int id, std::unique_ptr<Task> task) {
base::TimeDelta delay = task->delay;
heap_[id] = std::move(task);
base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
base::Bind(&DartTimerHeap::Run, weak_factory_.GetWeakPtr(), id), delay);
}
void DartTimerHeap::Run(int id) {
auto it = heap_.find(id);
if (it == heap_.end())
return;
std::unique_ptr<Task> task = std::move(it->second);
heap_.erase(it);
if (!task->closure.dart_state())
return;
DartState::Scope dart_scope(task->closure.dart_state().get());
DartInvokeVoid(task->closure.value());
if (task->repeating)
Schedule(id, std::move(task));
}
}
// 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.
#ifndef SKY_ENGINE_TONIC_DART_TIMER_HEAP_H_
#define SKY_ENGINE_TONIC_DART_TIMER_HEAP_H_
#include <unordered_map>
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/tonic/dart_persistent_value.h"
namespace blink {
class DartTimerHeap {
public:
DartTimerHeap();
~DartTimerHeap();
struct Task {
DartPersistentValue closure;
base::TimeDelta delay;
bool repeating = false;
};
int Add(std::unique_ptr<Task> task);
void Remove(int id);
private:
void Schedule(int id, std::unique_ptr<Task> task);
void Run(int id);
int next_timer_id_;
std::unordered_map<int, std::unique_ptr<Task>> heap_;
base::WeakPtrFactory<DartTimerHeap> weak_factory_;
};
}
#endif // SKY_ENGINE_TONIC_DART_TIMER_HEAP_H_
echo "Analyzing dart:ui library..."
RESULTS=`dartanalyzer --ignore-unrecognized-flags --supermixin --enable-strict-call-checks --enable_type_checks --strong --package-warnings --fatal-warnings --strong-hints --fatal-hints --lints out/Debug/gen/sky/bindings/dart_ui.dart 2>&1 | grep -v "\[error\] Native functions can only be declared in the SDK and code that is loaded through native extensions" | grep -Ev "\[(hint|error)\] The function '.+' is not used" | grep -v "\[warning\] Undefined name 'main'" | grep -v "\[info\] TODO" | grep -Ev "[0-9]+ errors.*found." | grep -v "Analyzing \[out/Debug/gen/sky/bindings/dart_ui.dart\]\.\.\."`
RESULTS=`dartanalyzer --ignore-unrecognized-flags --supermixin --enable-strict-call-checks --enable_type_checks --strong --package-warnings --fatal-warnings --strong-hints --fatal-hints --lints out/Debug/gen/sky/bindings/dart_ui.dart 2>&1 |grep -v "\[error\] Target of URI does not exist: 'dart:mojo.internal'" | grep -v "\[error\] Native functions can only be declared in the SDK and code that is loaded through native extensions" | grep -Ev "\[(hint|error)\] The function '.+' is not used" | grep -v "\[warning\] Undefined name 'main'" |grep -v "\[warning\] Undefined name 'VMLibraryHooks" | grep -v "\[warning\] Undefined name 'MojoHandleWatcher'" | grep -v "\[warning\] Undefined name 'MojoCoreNatives'" | grep -v "\[info\] TODO" | grep -Ev "[0-9]+ errors.*found." | grep -v "Analyzing \[out/Debug/gen/sky/bindings/dart_ui.dart\]\.\.\."`
echo "$RESULTS"
if [ -n "$RESULTS" ]; then exit 1; fi
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册