Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
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
12 changes: 6 additions & 6 deletions assets/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

source_set("assets") {
sources = [
"asset_provider.h",
"asset_manager.cc",
"asset_manager.h",
"asset_resolver.h",
"directory_asset_bundle.cc",
"directory_asset_bundle.h",
"unzipper_provider.cc",
"unzipper_provider.h",
"zip_asset_store.cc",
"zip_asset_store.h",
]

deps = [
"$flutter_root/common",
"$flutter_root/fml",
"$flutter_root/glue",
"//garnet/public/lib/fxl",
"//garnet/public/lib/zip",
Expand All @@ -23,7 +25,5 @@ source_set("assets") {
"//third_party/zlib:minizip",
]

public_configs = [
"$flutter_root:config",
]
public_configs = [ "$flutter_root:config" ]
}
55 changes: 55 additions & 0 deletions assets/asset_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2017 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/assets/asset_manager.h"

#include "flutter/assets/directory_asset_bundle.h"
#include "flutter/assets/zip_asset_store.h"
#include "flutter/glue/trace_event.h"
#include "lib/fxl/files/path.h"

namespace blink {

AssetManager::AssetManager() = default;

AssetManager::~AssetManager() = default;

void AssetManager::PushFront(std::unique_ptr<AssetResolver> resolver) {
if (resolver == nullptr || !resolver->IsValid()) {
return;
}

resolvers_.push_front(std::move(resolver));
}

void AssetManager::PushBack(std::unique_ptr<AssetResolver> resolver) {
if (resolver == nullptr || !resolver->IsValid()) {
return;
}

resolvers_.push_back(std::move(resolver));
}

// |blink::AssetResolver|
bool AssetManager::GetAsBuffer(const std::string& asset_name,
std::vector<uint8_t>* data) const {
if (asset_name.size() == 0) {
return false;
}
TRACE_EVENT0("flutter", "AssetManager::GetAsBuffer");
for (const auto& resolver : resolvers_) {
if (resolver->GetAsBuffer(asset_name, data)) {
return true;
}
}
FXL_DLOG(ERROR) << "Could not find asset: " << asset_name;
return false;
}

// |blink::AssetResolver|
bool AssetManager::IsValid() const {
return resolvers_.size() > 0;
}

} // namespace blink
47 changes: 47 additions & 0 deletions assets/asset_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2017 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_ASSETS_ASSET_MANAGER_H_
#define FLUTTER_ASSETS_ASSET_MANAGER_H_

#include <deque>
#include <memory>
#include <string>

#include "flutter/assets/asset_resolver.h"
#include "lib/fxl/files/unique_fd.h"
#include "lib/fxl/macros.h"
#include "lib/fxl/memory/ref_counted.h"

namespace blink {

class AssetManager final : public AssetResolver,
public fxl::RefCountedThreadSafe<AssetManager> {
public:
void PushFront(std::unique_ptr<AssetResolver> resolver);

void PushBack(std::unique_ptr<AssetResolver> resolver);

// |blink::AssetResolver|
bool IsValid() const override;

// |blink::AssetResolver|
bool GetAsBuffer(const std::string& asset_name,
std::vector<uint8_t>* data) const override;

private:
std::deque<std::unique_ptr<AssetResolver>> resolvers_;

AssetManager();

~AssetManager();

FXL_DISALLOW_COPY_AND_ASSIGN(AssetManager);
FRIEND_MAKE_REF_COUNTED(AssetManager);
FRIEND_REF_COUNTED_THREAD_SAFE(AssetManager);
};

} // namespace blink

#endif // FLUTTER_ASSETS_ASSET_MANAGER_H_
25 changes: 0 additions & 25 deletions assets/asset_provider.h

This file was deleted.

32 changes: 32 additions & 0 deletions assets/asset_resolver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2017 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_ASSETS_ASSET_RESOLVER_H_
#define FLUTTER_ASSETS_ASSET_RESOLVER_H_

#include <string>
#include <vector>

#include "lib/fxl/macros.h"

namespace blink {

class AssetResolver {
public:
AssetResolver() = default;

virtual ~AssetResolver() = default;

virtual bool IsValid() const = 0;

virtual bool GetAsBuffer(const std::string& asset_name,
std::vector<uint8_t>* data) const = 0;

private:
FXL_DISALLOW_COPY_AND_ASSIGN(AssetResolver);
};

} // namespace blink

#endif // FLUTTER_ASSETS_ASSET_RESOLVER_H_
85 changes: 33 additions & 52 deletions assets/directory_asset_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,54 @@
// found in the LICENSE file.

#include "flutter/assets/directory_asset_bundle.h"
#include "lib/fxl/build_config.h"

#include <fcntl.h>

#include <utility>

#include "flutter/fml/file.h"
#include "flutter/fml/mapping.h"
#include "lib/fxl/files/eintr_wrapper.h"
#include "lib/fxl/files/file.h"
#include "lib/fxl/files/path.h"
#include "lib/fxl/files/unique_fd.h"
#include "lib/fxl/portable_unistd.h"

namespace blink {

bool DirectoryAssetBundle::GetAsBuffer(const std::string& asset_name,
std::vector<uint8_t>* data) {
if (fd_.is_valid()) {
#if defined(OS_WIN)
// This code path is not valid in a Windows environment.
return false;
#else
fxl::UniqueFD asset_file(openat(fd_.get(), asset_name.c_str(), O_RDONLY));
if (!asset_file.is_valid())
return false;

constexpr size_t kBufferSize = 1 << 16;
size_t offset = 0;
ssize_t bytes_read = 0;
do {
offset += bytes_read;
data->resize(offset + kBufferSize);
bytes_read = read(asset_file.get(), &(*data)[offset], kBufferSize);
} while (bytes_read > 0);
DirectoryAssetBundle::DirectoryAssetBundle(fxl::UniqueFD descriptor)
: descriptor_(std::move(descriptor)) {
if (!fml::IsDirectory(descriptor_)) {
return;
}
is_valid_ = true;
}

if (bytes_read < 0) {
FXL_LOG(ERROR) << "Reading " << asset_name << " failed";
data->clear();
return false;
}
DirectoryAssetBundle::~DirectoryAssetBundle() = default;

data->resize(offset + bytes_read);
return true;
#endif
}
std::string asset_path = GetPathForAsset(asset_name);
if (asset_path.empty())
return false;
return files::ReadFileToVector(asset_path, data);
// |blink::AssetResolver|
bool DirectoryAssetBundle::IsValid() const {
return is_valid_;
}

DirectoryAssetBundle::~DirectoryAssetBundle() {}
// |blink::AssetResolver|
bool DirectoryAssetBundle::GetAsBuffer(const std::string& asset_name,
std::vector<uint8_t>* data) const {
if (data == nullptr) {
return false;
}

DirectoryAssetBundle::DirectoryAssetBundle(std::string directory)
: directory_(std::move(directory)), fd_() {}
if (!is_valid_) {
FXL_DLOG(WARNING) << "Asset bundle was not valid.";
return false;
}

DirectoryAssetBundle::DirectoryAssetBundle(fxl::UniqueFD fd)
: fd_(std::move(fd)) {}
fml::FileMapping mapping(
fml::OpenFile(descriptor_, asset_name.c_str(), fml::OpenPermission::kRead,
false /* directory */),
false /* executable */);

std::string DirectoryAssetBundle::GetPathForAsset(
const std::string& asset_name) {
std::string asset_path = files::SimplifyPath(directory_ + "/" + asset_name);
if (asset_path.find(directory_) != 0u) {
FXL_LOG(ERROR) << "Asset name '" << asset_name
<< "' attempted to traverse outside asset bundle.";
return std::string();
if (mapping.GetMapping() == nullptr) {
return false;
}
return asset_path;

data->resize(mapping.GetSize());
memmove(data->data(), mapping.GetMapping(), mapping.GetSize());
return true;
}

} // namespace blink
30 changes: 15 additions & 15 deletions assets/directory_asset_bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
#ifndef FLUTTER_ASSETS_DIRECTORY_ASSET_BUNDLE_H_
#define FLUTTER_ASSETS_DIRECTORY_ASSET_BUNDLE_H_

#include <string>
#include <vector>

#include "flutter/assets/asset_provider.h"
#include "flutter/assets/asset_resolver.h"
#include "lib/fxl/files/unique_fd.h"
#include "lib/fxl/macros.h"
#include "lib/fxl/memory/ref_counted.h"

namespace blink {

class DirectoryAssetBundle
: public AssetProvider {
class DirectoryAssetBundle : public AssetResolver {
public:
explicit DirectoryAssetBundle(std::string directory);
// Expects fd to be valid, otherwise the file descriptor is ignored.
explicit DirectoryAssetBundle(fxl::UniqueFD fd);
virtual ~DirectoryAssetBundle();

virtual bool GetAsBuffer(const std::string& asset_name, std::vector<uint8_t>* data);
explicit DirectoryAssetBundle(fxl::UniqueFD descriptor);

std::string GetPathForAsset(const std::string& asset_name);
~DirectoryAssetBundle() override;

private:
const std::string directory_;
fxl::UniqueFD fd_;
const fxl::UniqueFD descriptor_;
bool is_valid_ = false;

std::string GetPathForAsset(const std::string& asset_name) const;

// |blink::AssetResolver|
bool IsValid() const override;

// |blink::AssetResolver|
bool GetAsBuffer(const std::string& asset_name,
std::vector<uint8_t>* data) const override;

FXL_DISALLOW_COPY_AND_ASSIGN(DirectoryAssetBundle);
};
Expand Down
27 changes: 27 additions & 0 deletions assets/file_asset_bundle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 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/assets/file_asset_bundle.h"

#include "lib/fxl/files/file.h"
#include "lib/fxl/files/path.h"

namespace blink {

FileAssetBundle::FileAssetBundle() = default;

FileAssetBundle::~FileAssetBundle() = default;

// |blink::AssetResolver|
bool FileAssetBundle::IsValid() const {
return true;
}

// |blink::AssetResolver|
bool FileAssetBundle::GetAsBuffer(const std::string& asset_name,
std::vector<uint8_t>* data) const {
return files::ReadFileToVector(files::AbsolutePath(asset_name), data);
}

} // namespace blink
Loading