Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Updates in flutter/fml for the shell refactor (Patch 5). #4833

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@

source_set("fml") {
sources = [
"file.cc",
"file.h",
"icu_util.cc",
"icu_util.h",
"mapping.cc",
"mapping.h",
"memory/thread_checker.h",
"memory/weak_ptr.h",
"memory/weak_ptr_internal.cc",
"memory/weak_ptr_internal.h",
"message_loop.cc",
"message_loop.h",
"message_loop_impl.cc",
"message_loop_impl.h",
"native_library.cc",
"native_library.h",
"paths.cc",
"paths.h",
"task_observer.h",
"task_runner.cc",
"task_runner.h",
"thread.cc",
Expand Down Expand Up @@ -95,6 +101,10 @@ source_set("fml") {
]
}

if (is_fuchsia) {
sources += [ "platform/fuchsia/paths_fuchsia.cc" ]
}

if (is_win) {
sources += [
"platform/win/mapping_win.cc",
Expand All @@ -103,9 +113,7 @@ source_set("fml") {
"platform/win/paths_win.cc",
]
} else {
sources += [
"platform/posix/mapping_posix.cc",
]
sources += [ "platform/posix/mapping_posix.cc" ]
}
}

Expand Down
67 changes: 67 additions & 0 deletions fml/file.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2018 The Flutter Authors. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and elswhere: keep as The Chromium Authors for now for consistency.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saying that would be factually incorrect though. We don't modify existing headers but use the latest message for new files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @Hixie for clarification on all things licence-y since I'm not 100% sure what we're meant to do in these cases.

// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/fml/file.h"

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include "lib/fxl/files/eintr_wrapper.h"

namespace fml {

fxl::UniqueFD OpenFile(const char* path,
OpenPermission permission,
bool is_directory) {
return OpenFile(fxl::UniqueFD{AT_FDCWD}, path, permission, is_directory);
}

fxl::UniqueFD OpenFile(const fxl::UniqueFD& base_directory,
const char* path,
OpenPermission permission,
bool is_directory) {
if (path == nullptr) {
return fxl::UniqueFD{};
}

int flags = 0;
switch (permission) {
case OpenPermission::kRead:
flags = O_RDONLY;
break;
case OpenPermission::kWrite:
flags = O_WRONLY;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a break here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

case OpenPermission::kReadWrite:
flags = O_RDWR;
break;
}

if (is_directory) {
flags |= O_DIRECTORY;
}

return fxl::UniqueFD{
HANDLE_EINTR(::openat(base_directory.get(), path, flags))};
}

fxl::UniqueFD Duplicate(int descriptor) {
return fxl::UniqueFD{HANDLE_EINTR(::dup(descriptor))};
}

bool IsDirectory(const fxl::UniqueFD& directory) {
if (!directory.is_valid()) {
return false;
}

struct stat stat_result = {};

if (::fstat(directory.get(), &stat_result) != 0) {
return false;
}

return S_ISDIR(stat_result.st_mode);
}

} // namespace fml
34 changes: 34 additions & 0 deletions fml/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018 The Flutter 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 FLUTTER_FML_FILE_H_
#define FLUTTER_FML_FILE_H_

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

namespace fml {

enum class OpenPermission {
kRead = 1,
kWrite = 1 << 1,
kReadWrite = kRead | kWrite,
};

fxl::UniqueFD OpenFile(const char* path,
OpenPermission permission,
bool is_directory = false);

fxl::UniqueFD OpenFile(const fxl::UniqueFD& base_directory,
const char* path,
OpenPermission permission,
bool is_directory = false);

fxl::UniqueFD Duplicate(int descriptor);

bool IsDirectory(const fxl::UniqueFD& directory);

} // namespace fml

#endif // FLUTTER_FML_FILE_H_
17 changes: 7 additions & 10 deletions fml/icu_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ static constexpr char kPathSeparator = '\\';
static constexpr char kPathSeparator = '/';
#endif

static constexpr char kIcuDataFileName[] = "icudtl.dat";

class ICUContext {
public:
ICUContext(const std::string& icu_data_path) : valid_(false) {
Expand All @@ -34,15 +32,15 @@ class ICUContext {

bool SetupMapping(const std::string& icu_data_path) {
// Check if the explicit path specified exists.
auto overriden_path_mapping = std::make_unique<FileMapping>(icu_data_path);
if (overriden_path_mapping->GetSize() != 0) {
mapping_ = std::move(overriden_path_mapping);
auto path_mapping = std::make_unique<FileMapping>(icu_data_path, false);
if (path_mapping->GetSize() != 0) {
mapping_ = std::move(path_mapping);
return true;
}

// Check to see if the mapping is in the resources bundle.
if (PlatformHasResourcesBundle()) {
auto resource = GetResourceMapping(kIcuDataFileName);
auto resource = GetResourceMapping(icu_data_path);
if (resource != nullptr && resource->GetSize() != 0) {
mapping_ = std::move(resource);
return true;
Expand All @@ -57,10 +55,8 @@ class ICUContext {
return false;
}

// 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 + kPathSeparator + kIcuDataFileName);
directory.second + kPathSeparator + icu_data_path, false);
if (file->GetSize() != 0) {
mapping_ = std::move(file);
return true;
Expand Down Expand Up @@ -96,7 +92,8 @@ class ICUContext {

void InitializeICUOnce(const std::string& icu_data_path) {
static ICUContext* context = new ICUContext(icu_data_path);
FXL_CHECK(context->IsValid()) << "Must be able to initialize the ICU context";
FXL_CHECK(context->IsValid())
<< "Must be able to initialize the ICU context. Tried: " << icu_data_path;
}

std::once_flag g_icu_init_flag;
Expand Down
20 changes: 20 additions & 0 deletions fml/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 The Flutter 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 FLUTTER_FML_MACROS_H_
#define FLUTTER_FML_MACROS_H_

#include "lib/fxl/macros.h"

#ifndef FML_USED_ON_EMBEDDER

#define FML_EMBEDDER_ONLY [[deprecated]]

#else // FML_USED_ON_EMBEDDER

#define FML_EMBEDDER_ONLY

#endif // FML_USED_ON_EMBEDDER

#endif // FLUTTER_FML_MACROS_H_
20 changes: 20 additions & 0 deletions fml/mapping.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 The Flutter 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"

namespace fml {

DataMapping::DataMapping(std::vector<uint8_t> data) : data_(std::move(data)) {}

DataMapping::~DataMapping() = default;

size_t DataMapping::GetSize() const {
return data_.size();
}

const uint8_t* DataMapping::GetMapping() const {
return data_.data();
}
} // namespace fml
21 changes: 19 additions & 2 deletions fml/mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <memory>
#include <string>
#include <vector>

#include "lib/fxl/build_config.h"

Expand Down Expand Up @@ -39,11 +40,11 @@ std::unique_ptr<Mapping> GetResourceMapping(const std::string& resource_name);

class FileMapping : public Mapping {
public:
FileMapping(const std::string& path);
FileMapping(const std::string& path, bool executable = false);

// fxl::UniqueFD isn't supported for Windows handles.
#if !OS_WIN
FileMapping(const fxl::UniqueFD& fd);
FileMapping(const fxl::UniqueFD& fd, bool executable = false);
#endif

~FileMapping() override;
Expand All @@ -63,6 +64,22 @@ class FileMapping : public Mapping {
FXL_DISALLOW_COPY_AND_ASSIGN(FileMapping);
};

class DataMapping : public Mapping {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working with asset bundles and such on Fuchsia.

public:
DataMapping(std::vector<uint8_t> data);

~DataMapping() override;

size_t GetSize() const override;

const uint8_t* GetMapping() const override;

private:
std::vector<uint8_t> data_;

FXL_DISALLOW_COPY_AND_ASSIGN(DataMapping);
};

} // namespace fml

#endif // FLUTTER_FML_MAPPING_H_
69 changes: 69 additions & 0 deletions fml/memory/thread_checker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2016 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// A class for checking that the current thread is/isn't the same as an initial
// thread.

#ifndef LIB_FXL_SYNCHRONIZATION_THREAD_CHECKER_H_
#define LIB_FXL_SYNCHRONIZATION_THREAD_CHECKER_H_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we worried that someone will pull in both this and the original? (re: the header guard name)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


#include "lib/fxl/build_config.h"

#if defined(OS_WIN)
#include <windows.h>
#else
#include <pthread.h>
#endif

#include "lib/fxl/logging.h"
#include "lib/fxl/macros.h"

namespace fml {

// A simple class that records the identity of the thread that it was created
// on, and at later points can tell if the current thread is the same as its
// creation thread. This class is thread-safe.
//
// Note: Unlike Chromium's |base::ThreadChecker|, this is *not* Debug-only (so
// #ifdef it out if you want something Debug-only). (Rationale: Having a
// |CalledOnValidThread()| that lies in Release builds seems bad. Moreover,
// there's a small space cost to having even an empty class. )
class ThreadChecker final {
public:
#if defined(OS_WIN)
ThreadChecker() : self_(GetCurrentThreadId()) {}
~ThreadChecker() {}

bool IsCreationThreadCurrent() const { return GetCurrentThreadId() == self_; }

private:
DWORD self_;

#else
ThreadChecker() : self_(pthread_self()) {}
~ThreadChecker() {}

// Returns true if the current thread is the thread this object was created
// on and false otherwise.
bool IsCreationThreadCurrent() const {
return !!pthread_equal(pthread_self(), self_);
}

private:
pthread_t self_;
#endif
};

#ifndef NDEBUG
#define FML_DECLARE_THREAD_CHECKER(c) fml::ThreadChecker c
#define FML_DCHECK_CREATION_THREAD_IS_CURRENT(c) \
FXL_DCHECK((c).IsCreationThreadCurrent())
#else
#define FML_DECLARE_THREAD_CHECKER(c)
#define FML_DCHECK_CREATION_THREAD_IS_CURRENT(c) ((void)0)
#endif

} // namespace fml

#endif // LIB_FXL_SYNCHRONIZATION_THREAD_CHECKER_H_
Loading