Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / origin-trials / OriginTrials.h
blobd1de36a20267555e99d8ba159b711f8926ec4bcc
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/. */
7 #ifndef mozilla_OriginTrials_h
8 #define mozilla_OriginTrials_h
10 #include "mozilla/origin_trials_ffi_generated.h"
11 #include "mozilla/EnumSet.h"
12 #include "nsStringFwd.h"
14 class nsIPrincipal;
15 class nsGlobalWindowInner;
16 struct JSContext;
17 class JSObject;
19 namespace mozilla {
21 using OriginTrial = origin_trials_ffi::OriginTrial;
23 // A class that keeps a set of enabled trials / features for a particular
24 // origin.
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>;
32 OriginTrials() = default;
34 static OriginTrials FromRaw(RawType aRaw) { return OriginTrials(aRaw); }
35 const RawType& Raw() const { return mEnabledTrials; }
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);
43 bool IsEnabled(OriginTrial aTrial) const;
45 // Checks whether a given origin trial is enabled for a given call.
46 static bool IsEnabled(JSContext*, JSObject*, OriginTrial);
48 // Computes the currently-applying trials for our global.
49 static OriginTrials FromWindow(const nsGlobalWindowInner*);
51 private:
52 explicit OriginTrials(RawType aRaw) : mEnabledTrials(aRaw) {}
54 RawType mEnabledTrials;
57 } // namespace mozilla
59 #endif