Skip to content

Commit

Permalink
Updated fml to build on Windows. (flutter#4415)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkonyi authored Dec 8, 2017
1 parent 5309ac6 commit 4d71856
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 36 deletions.
6 changes: 5 additions & 1 deletion fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
]
}
}

Expand Down
10 changes: 8 additions & 2 deletions fml/icu_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<FileMapping>(directory.second + "/" +
kIcuDataFileName);
auto file = std::make_unique<FileMapping>(
directory.second + kPathSeparator + kIcuDataFileName);
if (file->GetSize() != 0) {
mapping_ = std::move(file);
return true;
Expand Down
14 changes: 14 additions & 0 deletions fml/mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
#ifndef FLUTTER_FML_MAPPING_H_
#define FLUTTER_FML_MAPPING_H_

#include <memory>
#include <string>

#include "lib/fxl/build_config.h"

#if OS_WIN
#include <windows.h>
#endif

#include "lib/fxl/files/unique_fd.h"
#include "lib/fxl/macros.h"

Expand Down Expand Up @@ -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;

Expand All @@ -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);
};

Expand Down
16 changes: 8 additions & 8 deletions fml/message_loop_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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, &current]() {
loop.GetTaskRunner()->PostTask([&terminated, i, &current, count]() {
ASSERT_EQ(current, i);
current++;
if (count == i + 1) {
Expand Down Expand Up @@ -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, &current]() {
[&terminated, i, &current, count]() {
ASSERT_EQ(current, i);
current++;
if (count == i + 1) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
File renamed without changes.
84 changes: 84 additions & 0 deletions fml/platform/win/mapping_win.cc
Original file line number Diff line number Diff line change
@@ -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 <fcntl.h>

#include <type_traits>

#include "lib/fxl/build_config.h"

#include <io.h>
#include <windows.h>

using PlatformResourceMapping = fml::FileMapping;

namespace fml {

Mapping::Mapping() = default;

Mapping::~Mapping() = default;

bool PlatformHasResourcesBundle() {
return !std::is_same<PlatformResourceMapping, FileMapping>::value;
}

std::unique_ptr<Mapping> GetResourceMapping(const std::string& resource_name) {
return std::make_unique<PlatformResourceMapping>(resource_name);
}

FileMapping::FileMapping(const std::string& path)
: size_(0), mapping_(nullptr) {
HANDLE file_handle_ =
CreateFileA(reinterpret_cast<LPCSTR>(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<uint8_t*>(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
6 changes: 3 additions & 3 deletions fml/trace_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 27 additions & 21 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uintptr_t>(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<uintptr_t>(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();
}
Expand Down
3 changes: 2 additions & 1 deletion travis/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4d71856

Please sign in to comment.