From 02ba481921dcdc96bb543b386c8d12374a939d91 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Thu, 18 Jul 2019 08:57:50 -0700 Subject: [PATCH] Made the persistent cache's directory a const pointer. (flutter/engine#9815) --- .../flutter/shell/common/persistent_cache.cc | 19 +++++++++++++++---- .../flutter/shell/common/persistent_cache.h | 5 +++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/shell/common/persistent_cache.cc b/engine/src/flutter/shell/common/persistent_cache.cc index 04ceb5266e..bc19976b38 100644 --- a/engine/src/flutter/shell/common/persistent_cache.cc +++ b/engine/src/flutter/shell/common/persistent_cache.cc @@ -51,22 +51,33 @@ void PersistentCache::SetCacheDirectoryPath(std::string path) { cache_base_path_ = path; } -PersistentCache::PersistentCache(bool read_only) : is_read_only_(read_only) { +namespace { +std::shared_ptr MakeCacheDirectory( + const std::string& global_cache_base_path, + bool read_only) { fml::UniqueFD cache_base_dir; - if (cache_base_path_.length()) { - cache_base_dir = fml::OpenDirectory(cache_base_path_.c_str(), false, + if (global_cache_base_path.length()) { + cache_base_dir = fml::OpenDirectory(global_cache_base_path.c_str(), false, fml::FilePermission::kRead); } else { cache_base_dir = fml::paths::GetCachesDirectory(); } if (cache_base_dir.is_valid()) { - cache_directory_ = std::make_shared(CreateDirectory( + return std::make_shared(CreateDirectory( cache_base_dir, {"flutter_engine", GetFlutterEngineVersion(), "skia", GetSkiaVersion()}, read_only ? fml::FilePermission::kRead : fml::FilePermission::kReadWrite)); + } else { + return std::make_shared(); } +} +} // namespace + +PersistentCache::PersistentCache(bool read_only) + : is_read_only_(read_only), + cache_directory_(MakeCacheDirectory(cache_base_path_, read_only)) { if (!IsValid()) { FML_LOG(WARNING) << "Could not acquire the persistent cache directory. " "Caching of GPU resources on disk is disabled."; diff --git a/engine/src/flutter/shell/common/persistent_cache.h b/engine/src/flutter/shell/common/persistent_cache.h index 4c7d65d95b..595593eba7 100644 --- a/engine/src/flutter/shell/common/persistent_cache.h +++ b/engine/src/flutter/shell/common/persistent_cache.h @@ -19,7 +19,8 @@ namespace flutter { /// A cache of SkData that gets stored to disk. /// -/// This is mainly used for Shaders but is also written to by Dart. +/// This is mainly used for Shaders but is also written to by Dart. It is +/// thread-safe for reading and writing from multiple threads. class PersistentCache : public GrContextOptions::PersistentCache { public: // Mutable static switch that can be set before GetCacheForProcess. If true, @@ -52,7 +53,7 @@ class PersistentCache : public GrContextOptions::PersistentCache { static std::string cache_base_path_; const bool is_read_only_; - std::shared_ptr cache_directory_; + const std::shared_ptr cache_directory_; mutable std::mutex worker_task_runners_mutex_; std::multiset> worker_task_runners_ FML_GUARDED_BY(worker_task_runners_mutex_);