diff --git a/fml/BUILD.gn b/fml/BUILD.gn index e81ed4f1683a3..6dabcb446c912 100644 --- a/fml/BUILD.gn +++ b/fml/BUILD.gn @@ -6,7 +6,6 @@ source_set("fml") { sources = [ "icu_util.cc", "icu_util.h", - "mapping.cc", "mapping.h", "memory/weak_ptr.h", "memory/weak_ptr_internal.cc", @@ -98,10 +97,15 @@ source_set("fml") { if (is_win) { sources += [ + "platform/win/mapping_win.cc", "platform/win/message_loop_win.cc", "platform/win/message_loop_win.h", "platform/win/paths_win.cc", ] + } else { + sources += [ + "platform/posix/mapping_posix.cc", + ] } } diff --git a/fml/icu_util.cc b/fml/icu_util.cc index a214c4f2593aa..093dc3d02b1bc 100644 --- a/fml/icu_util.cc +++ b/fml/icu_util.cc @@ -16,6 +16,12 @@ namespace fml { namespace icu { +#if OS_WIN +static constexpr char kPathSeparator = '\\'; +#else +static constexpr char kPathSeparator = '/'; +#endif + static constexpr char kIcuDataFileName[] = "icudtl.dat"; class ICUContext { @@ -53,8 +59,8 @@ class ICUContext { // FIXME(chinmaygarde): There is no Path::Join in FXL. So a non-portable // version is used here. Patch FXL and update. - auto file = std::make_unique(directory.second + "/" + - kIcuDataFileName); + auto file = std::make_unique( + directory.second + kPathSeparator + kIcuDataFileName); if (file->GetSize() != 0) { mapping_ = std::move(file); return true; diff --git a/fml/mapping.h b/fml/mapping.h index eba438b4f6ff0..8963b22a9c1f6 100644 --- a/fml/mapping.h +++ b/fml/mapping.h @@ -5,8 +5,15 @@ #ifndef FLUTTER_FML_MAPPING_H_ #define FLUTTER_FML_MAPPING_H_ +#include #include +#include "lib/fxl/build_config.h" + +#if OS_WIN +#include +#endif + #include "lib/fxl/files/unique_fd.h" #include "lib/fxl/macros.h" @@ -34,7 +41,10 @@ class FileMapping : public Mapping { public: FileMapping(const std::string& path); +// fxl::UniqueFD isn't supported for Windows handles. +#if !OS_WIN FileMapping(const fxl::UniqueFD& fd); +#endif ~FileMapping() override; @@ -46,6 +56,10 @@ class FileMapping : public Mapping { size_t size_; uint8_t* mapping_; +#if OS_WIN + HANDLE mapping_handle_; +#endif + FXL_DISALLOW_COPY_AND_ASSIGN(FileMapping); }; diff --git a/fml/message_loop_unittests.cc b/fml/message_loop_unittests.cc index f2a74a5f4a11c..f298f839fbe33 100644 --- a/fml/message_loop_unittests.cc +++ b/fml/message_loop_unittests.cc @@ -75,7 +75,7 @@ TEST(MessageLoop, NonDelayedTasksAreRunInOrder) { auto& loop = fml::MessageLoop::GetCurrent(); size_t current = 0; for (size_t i = 0; i < count; i++) { - loop.GetTaskRunner()->PostTask([&terminated, i, ¤t]() { + loop.GetTaskRunner()->PostTask([&terminated, i, ¤t, count]() { ASSERT_EQ(current, i); current++; if (count == i + 1) { @@ -105,7 +105,7 @@ TEST(MessageLoop, DelayedTasksAtSameTimeAreRunInOrder) { fxl::TimePoint::Now() + fxl::TimeDelta::FromMilliseconds(2); for (size_t i = 0; i < count; i++) { loop.GetTaskRunner()->PostTaskForTime( - [&terminated, i, ¤t]() { + [&terminated, i, ¤t, count]() { ASSERT_EQ(current, i); current++; if (count == i + 1) { @@ -187,13 +187,13 @@ TEST(MessageLoop, TIME_SENSITIVE(SingleDelayedTaskForTime)) { TEST(MessageLoop, TIME_SENSITIVE(MultipleDelayedTasksWithIncreasingDeltas)) { const auto count = 10; int checked = false; - std::thread thread([&checked]() { + std::thread thread([&checked, count]() { fml::MessageLoop::EnsureInitializedForCurrentThread(); auto& loop = fml::MessageLoop::GetCurrent(); for (int target_ms = 0 + 2; target_ms < count + 2; target_ms++) { auto begin = fxl::TimePoint::Now(); loop.GetTaskRunner()->PostDelayedTask( - [begin, target_ms, &checked]() { + [begin, target_ms, &checked, count]() { auto delta = fxl::TimePoint::Now() - begin; auto ms = delta.ToMillisecondsF(); ASSERT_GE(ms, target_ms - 2); @@ -214,13 +214,13 @@ TEST(MessageLoop, TIME_SENSITIVE(MultipleDelayedTasksWithIncreasingDeltas)) { TEST(MessageLoop, TIME_SENSITIVE(MultipleDelayedTasksWithDecreasingDeltas)) { const auto count = 10; int checked = false; - std::thread thread([&checked]() { + std::thread thread([&checked, count]() { fml::MessageLoop::EnsureInitializedForCurrentThread(); auto& loop = fml::MessageLoop::GetCurrent(); for (int target_ms = count + 2; target_ms > 0 + 2; target_ms--) { auto begin = fxl::TimePoint::Now(); loop.GetTaskRunner()->PostDelayedTask( - [begin, target_ms, &checked]() { + [begin, target_ms, &checked, count]() { auto delta = fxl::TimePoint::Now() - begin; auto ms = delta.ToMillisecondsF(); ASSERT_GE(ms, target_ms - 2); @@ -263,9 +263,9 @@ TEST(MessageLoop, TaskObserverFire) { auto& loop = fml::MessageLoop::GetCurrent(); size_t task_count = 0; size_t obs_count = 0; - CustomTaskObserver obs([&obs_count]() { obs_count++; }); + CustomTaskObserver obs([&obs_count, count]() { obs_count++; }); for (size_t i = 0; i < count; i++) { - loop.GetTaskRunner()->PostTask([&terminated, i, &task_count]() { + loop.GetTaskRunner()->PostTask([&terminated, i, &task_count, count]() { ASSERT_EQ(task_count, i); task_count++; if (count == i + 1) { diff --git a/fml/mapping.cc b/fml/platform/posix/mapping_posix.cc similarity index 100% rename from fml/mapping.cc rename to fml/platform/posix/mapping_posix.cc diff --git a/fml/platform/win/mapping_win.cc b/fml/platform/win/mapping_win.cc new file mode 100644 index 0000000000000..fd404a14f37dc --- /dev/null +++ b/fml/platform/win/mapping_win.cc @@ -0,0 +1,84 @@ +// Copyright 2017 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/fml/mapping.h" + +#include + +#include + +#include "lib/fxl/build_config.h" + +#include +#include + +using PlatformResourceMapping = fml::FileMapping; + +namespace fml { + +Mapping::Mapping() = default; + +Mapping::~Mapping() = default; + +bool PlatformHasResourcesBundle() { + return !std::is_same::value; +} + +std::unique_ptr GetResourceMapping(const std::string& resource_name) { + return std::make_unique(resource_name); +} + +FileMapping::FileMapping(const std::string& path) + : size_(0), mapping_(nullptr) { + HANDLE file_handle_ = + CreateFileA(reinterpret_cast(path.c_str()), GENERIC_READ, + FILE_SHARE_READ, nullptr, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, nullptr); + + if (file_handle_ == INVALID_HANDLE_VALUE) { + return; + } + + size_ = GetFileSize(file_handle_, nullptr); + if (size_ == INVALID_FILE_SIZE) { + size_ = 0; + return; + } + + mapping_handle_ = CreateFileMapping(file_handle_, nullptr, PAGE_READONLY, 0, + size_, nullptr); + + CloseHandle(file_handle_); + + if (mapping_handle_ == INVALID_HANDLE_VALUE) { + return; + } + + auto mapping = MapViewOfFile(mapping_handle_, FILE_MAP_READ, 0, 0, size_); + + if (mapping == INVALID_HANDLE_VALUE) { + CloseHandle(mapping_handle_); + mapping_handle_ = INVALID_HANDLE_VALUE; + return; + } + + mapping_ = static_cast(mapping); +} + +FileMapping::~FileMapping() { + if (mapping_ != nullptr) { + UnmapViewOfFile(mapping_); + CloseHandle(mapping_handle_); + } +} + +size_t FileMapping::GetSize() const { + return size_; +} + +const uint8_t* FileMapping::GetMapping() const { + return mapping_; +} + +} // namespace fml diff --git a/fml/trace_event.h b/fml/trace_event.h index a8a56c02a82d5..832d099ce38b0 100644 --- a/fml/trace_event.h +++ b/fml/trace_event.h @@ -45,13 +45,13 @@ #define TRACE_EVENT_INSTANT0(category_group, name) \ ::fml::tracing::TraceEventInstant0(category_group, name); -#define TRACE_FLOW_BEGIN(category, name, id, args...) \ +#define TRACE_FLOW_BEGIN(category, name, id) \ ::fml::tracing::TraceEventFlowBegin0(category, name, id); -#define TRACE_FLOW_STEP(category, name, id, args...) \ +#define TRACE_FLOW_STEP(category, name, id) \ ::fml::tracing::TraceEventFlowStep0(category, name, id); -#define TRACE_FLOW_END(category, name, id, args...) \ +#define TRACE_FLOW_END(category, name, id) \ ::fml::tracing::TraceEventFlowEnd0(category, name, id); #endif // TRACE_EVENT_HIDE_MACROS diff --git a/shell/common/shell.cc b/shell/common/shell.cc index 506861c430067..d839820594df8 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -265,27 +265,33 @@ void Shell::RunInPlatformViewUIThread(uintptr_t view_id, *view_existed = false; IteratePlatformViews( - [ - view_id, // argument - assets_directory = std::move(assets_directory), // argument - main = std::move(main), // argument - packages = std::move(packages), // argument - &view_existed, // out - &dart_isolate_id, // out - &isolate_name // out - ](PlatformView * view) - ->bool { - if (reinterpret_cast(view) != view_id) { - // Keep looking. - return true; - } - *view_existed = true; - view->RunFromSource(assets_directory, main, packages); - *dart_isolate_id = view->engine().GetUIIsolateMainPort(); - *isolate_name = view->engine().GetUIIsolateName(); - // We found the requested view. Stop iterating over platform views. - return false; - }); + [view_id, // argument +#if !defined(OS_WIN) + // Using std::move on const references inside lambda capture is + // not supported on Windows for some reason. + assets_directory = std::move(assets_directory), // argument + main = std::move(main), // argument + packages = std::move(packages), // argument +#else + assets_directory, // argument + main, // argument + packages, // argument +#endif + &view_existed, // out + &dart_isolate_id, // out + &isolate_name // out + ](PlatformView* view) -> bool { + if (reinterpret_cast(view) != view_id) { + // Keep looking. + return true; + } + *view_existed = true; + view->RunFromSource(assets_directory, main, packages); + *dart_isolate_id = view->engine().GetUIIsolateMainPort(); + *isolate_name = view->engine().GetUIIsolateName(); + // We found the requested view. Stop iterating over platform views. + return false; + }); latch->Signal(); } diff --git a/travis/licenses_golden/licenses_flutter b/travis/licenses_golden/licenses_flutter index 4ce918d74b61d..7574ded2c1023 100644 --- a/travis/licenses_golden/licenses_flutter +++ b/travis/licenses_golden/licenses_flutter @@ -1088,7 +1088,6 @@ FILE: ../../../flutter/flow/texture.cc FILE: ../../../flutter/flow/texture.h FILE: ../../../flutter/fml/icu_util.cc FILE: ../../../flutter/fml/icu_util.h -FILE: ../../../flutter/fml/mapping.cc FILE: ../../../flutter/fml/mapping.h FILE: ../../../flutter/fml/message_loop.cc FILE: ../../../flutter/fml/message_loop.h @@ -1122,6 +1121,8 @@ FILE: ../../../flutter/fml/platform/linux/message_loop_linux.h FILE: ../../../flutter/fml/platform/linux/paths_linux.cc FILE: ../../../flutter/fml/platform/linux/timerfd.cc FILE: ../../../flutter/fml/platform/linux/timerfd.h +FILE: ../../../flutter/fml/platform/posix/mapping_posix.cc +FILE: ../../../flutter/fml/platform/win/mapping_win.cc FILE: ../../../flutter/fml/platform/win/message_loop_win.cc FILE: ../../../flutter/fml/platform/win/message_loop_win.h FILE: ../../../flutter/fml/platform/win/paths_win.cc