1 // Copyright 2017 The Chromium 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 #ifndef CHROME_BROWSER_VR_ASSETS_LOADER_H_ 6 #define CHROME_BROWSER_VR_ASSETS_LOADER_H_ 7 8 #include <stdint.h> 9 #include <memory> 10 11 #include "base/callback.h" 12 #include "base/files/file_path.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/version.h" 15 #include "chrome/browser/vr/assets_load_status.h" 16 #include "chrome/browser/vr/vr_base_export.h" 17 18 namespace base { 19 class DictionaryValue; 20 class SingleThreadTaskRunner; 21 class Version; 22 } // namespace base 23 24 namespace vr { 25 26 // Major component version we need to support all features. 27 constexpr uint32_t kTargetMajorVrAssetsComponentVersion = 2; 28 // Minimum major component version we are able to use with potentially reduced 29 // set of features. 30 constexpr uint32_t kMinMajorVrAssetsComponentVersion = 1; 31 32 struct AssetsLoaderSingletonTrait; 33 struct Assets; 34 35 // Manages VR assets such as the environment. Gets updated by the VR assets 36 // component. 37 // 38 // If not noted otherwise the functions are thread-safe. The reason is that the 39 // component will be made available on a different thread than the asset load 40 // request. Internally, the function calls will be posted on the main thread. 41 // The asset load may be performed on a worker thread. 42 class VR_BASE_EXPORT AssetsLoader { 43 public: 44 typedef base::OnceCallback<void(AssetsLoadStatus status, 45 std::unique_ptr<Assets> assets, 46 const base::Version& component_version)> 47 OnAssetsLoadedCallback; 48 typedef base::RepeatingCallback<void()> OnComponentReadyCallback; 49 50 // Returns the single assets instance and creates it on first call. 51 static AssetsLoader* GetInstance(); 52 53 static base::Version MinVersionWithGradients(); 54 static bool AssetsSupported(); 55 56 // Tells VR assets that a new VR assets component version is ready for use. 57 void OnComponentReady(const base::Version& version, 58 const base::FilePath& install_dir, 59 std::unique_ptr<base::DictionaryValue> manifest); 60 61 // Loads asset files and calls |on_loaded| passing the loaded asset files. 62 // |on_loaded| will be called on the caller's thread. Component must be ready 63 // when calling this function. 64 void Load(OnAssetsLoadedCallback on_loaded); 65 66 // Returns true if the component is ready. 67 // Must be called on the main thread. 68 bool ComponentReady(); 69 70 // |on_component_ready| is called on main thread when assets component becomes 71 // ready to use or got updated. Must be called on the main thread. 72 void SetOnComponentReadyCallback( 73 const OnComponentReadyCallback& on_component_ready); 74 75 private: 76 static void LoadAssetsTask( 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 78 const base::Version& component_version, 79 const base::FilePath& component_install_dir, 80 OnAssetsLoadedCallback on_loaded); 81 82 AssetsLoader(); 83 ~AssetsLoader(); 84 void OnComponentReadyInternal(const base::Version& version, 85 const base::FilePath& install_dir); 86 void LoadInternal(scoped_refptr<base::SingleThreadTaskRunner> task_runner, 87 OnAssetsLoadedCallback on_loaded); 88 89 bool component_ready_ = false; 90 base::Version component_version_; 91 base::FilePath component_install_dir_; 92 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; 93 OnComponentReadyCallback on_component_ready_callback_; 94 95 base::WeakPtrFactory<AssetsLoader> weak_ptr_factory_{this}; 96 97 friend struct AssetsLoaderSingletonTrait; 98 }; 99 100 } // namespace vr 101 102 #endif // CHROME_BROWSER_VR_ASSETS_LOADER_H_ 103