| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "flutter/assets/directory_asset_bundle.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "flutter/fml/eintr_wrapper.h" |
| 10 | #include "flutter/fml/file.h" |
| 11 | #include "flutter/fml/mapping.h" |
| 12 | |
| 13 | namespace flutter { |
| 14 | |
| 15 | DirectoryAssetBundle::DirectoryAssetBundle(fml::UniqueFD descriptor) |
| 16 | : descriptor_(std::move(descriptor)) { |
| 17 | if (!fml::IsDirectory(descriptor_)) { |
| 18 | return; |
| 19 | } |
| 20 | is_valid_ = true; |
| 21 | } |
| 22 | |
| 23 | DirectoryAssetBundle::~DirectoryAssetBundle() = default; |
| 24 | |
| 25 | // |AssetResolver| |
| 26 | bool DirectoryAssetBundle::IsValid() const { |
| 27 | return is_valid_; |
| 28 | } |
| 29 | |
| 30 | // |AssetResolver| |
| 31 | std::unique_ptr<fml::Mapping> DirectoryAssetBundle::GetAsMapping( |
| 32 | const std::string& asset_name) const { |
| 33 | if (!is_valid_) { |
| 34 | FML_DLOG(WARNING) << "Asset bundle was not valid."; |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | auto mapping = std::make_unique<fml::FileMapping>(fml::OpenFile( |
| 39 | descriptor_, asset_name.c_str(), false, fml::FilePermission::kRead)); |
| 40 | |
| 41 | if (!mapping->IsValid()) { |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
| 45 | return mapping; |
| 46 | } |
| 47 | |
| 48 | } // namespace flutter |
| 49 |