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/shell/common/run_configuration.h" |
6 | |
7 | #include <sstream> |
8 | |
9 | #include "flutter/assets/directory_asset_bundle.h" |
10 | #include "flutter/fml/file.h" |
11 | #include "flutter/fml/unique_fd.h" |
12 | #include "flutter/runtime/dart_vm.h" |
13 | #include "flutter/shell/common/persistent_cache.h" |
14 | |
15 | namespace flutter { |
16 | |
17 | RunConfiguration RunConfiguration::InferFromSettings( |
18 | const Settings& settings, |
19 | fml::RefPtr<fml::TaskRunner> io_worker) { |
20 | auto asset_manager = std::make_shared<AssetManager>(); |
21 | |
22 | if (fml::UniqueFD::traits_type::IsValid(settings.assets_dir)) { |
23 | asset_manager->PushBack(std::make_unique<DirectoryAssetBundle>( |
24 | fml::Duplicate(settings.assets_dir))); |
25 | } |
26 | |
27 | asset_manager->PushBack( |
28 | std::make_unique<DirectoryAssetBundle>(fml::OpenDirectory( |
29 | settings.assets_path.c_str(), false, fml::FilePermission::kRead))); |
30 | |
31 | return {IsolateConfiguration::InferFromSettings(settings, asset_manager, |
32 | io_worker), |
33 | asset_manager}; |
34 | } |
35 | |
36 | RunConfiguration::RunConfiguration( |
37 | std::unique_ptr<IsolateConfiguration> configuration) |
38 | : RunConfiguration(std::move(configuration), |
39 | std::make_shared<AssetManager>()) { |
40 | PersistentCache::SetAssetManager(asset_manager_); |
41 | } |
42 | |
43 | RunConfiguration::RunConfiguration( |
44 | std::unique_ptr<IsolateConfiguration> configuration, |
45 | std::shared_ptr<AssetManager> asset_manager) |
46 | : isolate_configuration_(std::move(configuration)), |
47 | asset_manager_(std::move(asset_manager)) { |
48 | PersistentCache::SetAssetManager(asset_manager_); |
49 | } |
50 | |
51 | RunConfiguration::RunConfiguration(RunConfiguration&&) = default; |
52 | |
53 | RunConfiguration::~RunConfiguration() = default; |
54 | |
55 | bool RunConfiguration::IsValid() const { |
56 | return asset_manager_ && isolate_configuration_; |
57 | } |
58 | |
59 | bool RunConfiguration::AddAssetResolver( |
60 | std::unique_ptr<AssetResolver> resolver) { |
61 | if (!resolver || !resolver->IsValid()) { |
62 | return false; |
63 | } |
64 | |
65 | asset_manager_->PushBack(std::move(resolver)); |
66 | return true; |
67 | } |
68 | |
69 | void RunConfiguration::SetEntrypoint(std::string entrypoint) { |
70 | entrypoint_ = std::move(entrypoint); |
71 | } |
72 | |
73 | void RunConfiguration::SetEntrypointAndLibrary(std::string entrypoint, |
74 | std::string library) { |
75 | SetEntrypoint(entrypoint); |
76 | entrypoint_library_ = std::move(library); |
77 | } |
78 | |
79 | std::shared_ptr<AssetManager> RunConfiguration::GetAssetManager() const { |
80 | return asset_manager_; |
81 | } |
82 | |
83 | const std::string& RunConfiguration::GetEntrypoint() const { |
84 | return entrypoint_; |
85 | } |
86 | |
87 | const std::string& RunConfiguration::GetEntrypointLibrary() const { |
88 | return entrypoint_library_; |
89 | } |
90 | |
91 | std::unique_ptr<IsolateConfiguration> |
92 | RunConfiguration::TakeIsolateConfiguration() { |
93 | return std::move(isolate_configuration_); |
94 | } |
95 | |
96 | } // namespace flutter |
97 | |