environment_factory.cc (4550B)
1 /* 2 * Copyright 2023 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "api/environment/environment_factory.h" 12 13 #include <memory> 14 #include <utility> 15 16 #include "absl/base/nullability.h" 17 #include "api/environment/deprecated_global_field_trials.h" 18 #include "api/environment/environment.h" 19 #include "api/field_trials_view.h" 20 #include "api/make_ref_counted.h" 21 #include "api/ref_counted_base.h" 22 #include "api/rtc_event_log/rtc_event_log.h" 23 #include "api/scoped_refptr.h" 24 #include "api/task_queue/default_task_queue_factory.h" 25 #include "api/task_queue/task_queue_factory.h" 26 #include "rtc_base/checks.h" 27 #include "system_wrappers/include/clock.h" 28 29 namespace webrtc { 30 namespace { 31 32 template <typename T> 33 void Store(absl_nonnull std::unique_ptr<T> value, 34 scoped_refptr<const RefCountedBase>& leaf) { 35 class StorageNode : public RefCountedBase { 36 public: 37 StorageNode(scoped_refptr<const RefCountedBase> parent, 38 absl_nonnull std::unique_ptr<T> value) 39 : parent_(std::move(parent)), value_(std::move(value)) {} 40 41 StorageNode(const StorageNode&) = delete; 42 StorageNode& operator=(const StorageNode&) = delete; 43 44 ~StorageNode() override = default; 45 46 private: 47 scoped_refptr<const RefCountedBase> parent_; 48 absl_nonnull std::unique_ptr<T> value_; 49 }; 50 51 // Utilities provided with ownership form a tree: 52 // Root is nullptr, each node keeps an ownership of one utility. 53 // Each child node has a link to the parent, but parent is unaware of its 54 // children. Each `EnvironmentFactory` and `Environment` keep a reference to a 55 // 'leaf_' - node with the last provided utility. This way `Environment` keeps 56 // ownership of a single branch of the storage tree with each used utiltity 57 // owned by one of the nodes on that branch. 58 leaf = make_ref_counted<StorageNode>(std::move(leaf), std::move(value)); 59 } 60 61 } // namespace 62 63 EnvironmentFactory::EnvironmentFactory(const Environment& env) 64 : leaf_(env.storage_), 65 field_trials_(env.field_trials_), 66 clock_(env.clock_), 67 task_queue_factory_(env.task_queue_factory_), 68 event_log_(env.event_log_) {} 69 70 void EnvironmentFactory::Set( 71 absl_nullable std::unique_ptr<const FieldTrialsView> utility) { 72 if (utility != nullptr) { 73 field_trials_ = utility.get(); 74 Store(std::move(utility), leaf_); 75 } 76 } 77 78 void EnvironmentFactory::Set(absl_nullable std::unique_ptr<Clock> utility) { 79 if (utility != nullptr) { 80 clock_ = utility.get(); 81 Store(std::move(utility), leaf_); 82 } 83 } 84 85 void EnvironmentFactory::Set( 86 absl_nullable std::unique_ptr<TaskQueueFactory> utility) { 87 if (utility != nullptr) { 88 task_queue_factory_ = utility.get(); 89 Store(std::move(utility), leaf_); 90 } 91 } 92 93 void EnvironmentFactory::Set( 94 absl_nullable std::unique_ptr<RtcEventLog> utility) { 95 if (utility != nullptr) { 96 event_log_ = utility.get(); 97 Store(std::move(utility), leaf_); 98 } 99 } 100 101 Environment EnvironmentFactory::CreateWithDefaults() && { 102 if (field_trials_ == nullptr) { 103 Set(std::make_unique<DeprecatedGlobalFieldTrials>()); 104 } 105 #if defined(WEBRTC_MOZILLA_BUILD) 106 // We want to use our clock, not GetRealTimeClockRaw, and we avoid 107 // building the code under third_party/libwebrtc/task_queue. To 108 // ensure we're setting up things correctly, namely providing an 109 // Environment object with a preset task_queue_factory and clock, 110 // we'll do a release assert here. 111 RTC_CHECK(clock_); 112 RTC_CHECK(task_queue_factory_); 113 #else 114 if (clock_ == nullptr) { 115 Set(Clock::GetRealTimeClock()); 116 } 117 if (task_queue_factory_ == nullptr) { 118 Set(CreateDefaultTaskQueueFactory(field_trials_)); 119 } 120 #endif 121 if (event_log_ == nullptr) { 122 Set(std::make_unique<RtcEventLogNull>()); 123 } 124 125 RTC_DCHECK(field_trials_ != nullptr); 126 RTC_DCHECK(clock_ != nullptr); 127 RTC_DCHECK(task_queue_factory_ != nullptr); 128 RTC_DCHECK(event_log_ != nullptr); 129 return Environment(std::move(leaf_), // 130 field_trials_, clock_, task_queue_factory_, event_log_); 131 } 132 133 Environment EnvironmentFactory::Create() const { 134 // Create a temporary copy to avoid mutating `this` with default utilities. 135 return EnvironmentFactory(*this).CreateWithDefaults(); 136 } 137 138 } // namespace webrtc