OriginTrials.h (1820B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_OriginTrials_h 8 #define mozilla_OriginTrials_h 9 10 #include "mozilla/EnumSet.h" 11 #include "mozilla/origin_trials_ffi_generated.h" 12 #include "nsStringFwd.h" 13 14 class nsIPrincipal; 15 class nsGlobalWindowInner; 16 struct JSContext; 17 class JSObject; 18 19 namespace mozilla { 20 21 using OriginTrial = origin_trials_ffi::OriginTrial; 22 23 // A class that keeps a set of enabled trials / features for a particular 24 // origin. 25 // 26 // These allow sites to opt-in and provide feedback into experimental features 27 // before we ship it to the general public. 28 class OriginTrials final { 29 public: 30 using RawType = EnumSet<OriginTrial>; 31 32 OriginTrials() = default; 33 34 static OriginTrials FromRaw(RawType aRaw) { return OriginTrials(aRaw); } 35 const RawType& Raw() const { return mEnabledTrials; } 36 37 // Parses and verifies a base64-encoded token from either a header or a meta 38 // tag. If the token is valid and not expired, this will enable the relevant 39 // feature. 40 void UpdateFromToken(const nsAString& aBase64EncodedToken, 41 nsIPrincipal* aPrincipal); 42 43 bool IsEnabled(OriginTrial aTrial) const; 44 45 // Checks whether a given origin trial is enabled for a given call. 46 static bool IsEnabled(JSContext*, JSObject*, OriginTrial); 47 48 // Computes the currently-applying trials for our global. 49 static OriginTrials FromWindow(const nsGlobalWindowInner*); 50 51 private: 52 explicit OriginTrials(RawType aRaw) : mEnabledTrials(aRaw) {} 53 54 RawType mEnabledTrials; 55 }; 56 57 } // namespace mozilla 58 59 #endif