Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: content/common/origin_trials/trial_token.h

Issue 1909633003: Collect UMA data for Origin Trials (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix/update unit tests Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ 5 #ifndef CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_
6 #define CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ 6 #define CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 10
11 #include "base/strings/string_piece.h" 11 #include "base/strings/string_piece.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "content/common/content_export.h" 13 #include "content/common/content_export.h"
14 #include "url/origin.h" 14 #include "url/origin.h"
15 15
16 namespace blink {
17 enum class WebOriginTrialTokenStatus;
18 }
19
16 namespace content { 20 namespace content {
17 21
18 // The Experimental Framework (EF) provides limited access to experimental 22 // The Experimental Framework (EF) provides limited access to experimental
19 // features, on a per-origin basis (origin trials). This class defines the trial 23 // features, on a per-origin basis (origin trials). This class defines the trial
20 // token data structure, used to securely provide access to an experimental 24 // token data structure, used to securely provide access to an experimental
21 // feature. 25 // feature.
22 // 26 //
23 // Features are defined by string names, provided by the implementers. The EF 27 // Features are defined by string names, provided by the implementers. The EF
24 // code does not maintain an enum or constant list for feature names. Instead, 28 // code does not maintain an enum or constant list for feature names. Instead,
25 // the EF validates the name provided by the feature implementation against any 29 // the EF validates the name provided by the feature implementation against any
26 // provided tokens. 30 // provided tokens.
27 // 31 //
28 // More documentation on the token format can be found at 32 // More documentation on the token format can be found at
29 // https://docs.google.com/document/d/1v5fi0EUV_QHckVHVF2K4P72iNywnrJtNhNZ6i2NPt 0M 33 // https://docs.google.com/document/d/1v5fi0EUV_QHckVHVF2K4P72iNywnrJtNhNZ6i2NPt 0M
30 34
31 class CONTENT_EXPORT TrialToken { 35 class CONTENT_EXPORT TrialToken {
32 public: 36 public:
33 ~TrialToken(); 37 ~TrialToken();
34 38
35 // Returns a token object if the string represents a signed well-formed token, 39 // If the string represents a signed well-formed token, a token object is
36 // or nullptr otherwise. (This does not mean that the token is currently 40 // returned, and success is returned in the |out_status| parameter. Otherwise,
37 // valid, or appropriate for a given origin / feature, just that it is 41 // the |out_status| parameter indicates what was wrong with the string, and
42 // nullptr is returned.
43 // Note that success does not mean that the token is currently valid, or
44 // appropriate for a given origin / feature. It only means that it is
38 // correctly formatted and signed by the supplied public key, and can be 45 // correctly formatted and signed by the supplied public key, and can be
39 // parsed.) 46 // parsed.
40 static std::unique_ptr<TrialToken> From(const std::string& token_text, 47 static std::unique_ptr<TrialToken> From(
41 base::StringPiece public_key); 48 const std::string& token_text,
49 base::StringPiece public_key,
50 blink::WebOriginTrialTokenStatus* out_status);
42 51
43 // Returns true if this token is appropriate for use by the given origin, 52 // Returns success if this token is appropriate for use by the given origin
44 // for the given feature name, and has not yet expired. 53 // and feature name, and has not yet expired. Otherwise, the return value
45 bool IsValidForFeature(const url::Origin& origin, 54 // indicates why the token is not valid.
46 base::StringPiece feature_name, 55 blink::WebOriginTrialTokenStatus IsValidForFeature(
47 const base::Time& now) const; 56 const url::Origin& origin,
57 base::StringPiece feature_name,
58 const base::Time& now) const;
48 59
49 url::Origin origin() { return origin_; } 60 url::Origin origin() { return origin_; }
50 std::string feature_name() { return feature_name_; } 61 std::string feature_name() { return feature_name_; }
51 base::Time expiry_time() { return expiry_time_; } 62 base::Time expiry_time() { return expiry_time_; }
52 63
53 protected: 64 protected:
54 friend class TrialTokenTest; 65 friend class TrialTokenTest;
55 66
56 // Returns the payload of a signed token, or nullptr if the token is not 67 // If the string represents a properly signed and well-formed token, the JSON
iclelland 2016/04/28 17:00:58 Extract explicitly doesn't care whether the payloa
chasej 2016/05/02 15:53:11 To me, this comment doesn't guarantee that valid J
57 // properly signed, or is not well-formed. 68 // payload is returned in the |out_token_json| parameter and success is
58 static std::unique_ptr<std::string> Extract(const std::string& token_text, 69 // returned. Otherwise,the return code indicates what was wrong with the
59 base::StringPiece public_key); 70 // string, and |out_token_json| is unchanged.
71 static blink::WebOriginTrialTokenStatus Extract(const std::string& token_text,
72 base::StringPiece public_key,
73 std::string* out_token_json);
60 74
61 // Returns a token object if the string represents a well-formed JSON token 75 // Returns a token object if the string represents a well-formed JSON token
62 // payload, or nullptr otherwise. 76 // payload, or nullptr otherwise.
63 static std::unique_ptr<TrialToken> Parse(const std::string& token_json); 77 static std::unique_ptr<TrialToken> Parse(const std::string& token_json);
iclelland 2016/04/28 17:00:58 There's an opportunity to change this to return a
chasej 2016/05/02 15:53:11 Yes, I decided not to return a status code because
iclelland 2016/05/02 16:17:55 Acknowledged.
64 78
65 bool ValidateOrigin(const url::Origin& origin) const; 79 bool ValidateOrigin(const url::Origin& origin) const;
66 bool ValidateFeatureName(base::StringPiece feature_name) const; 80 bool ValidateFeatureName(base::StringPiece feature_name) const;
67 bool ValidateDate(const base::Time& now) const; 81 bool ValidateDate(const base::Time& now) const;
68 82
69 static bool ValidateSignature(base::StringPiece signature_text, 83 static bool ValidateSignature(base::StringPiece signature_text,
70 const std::string& data, 84 const std::string& data,
71 base::StringPiece public_key); 85 base::StringPiece public_key);
72 86
73 private: 87 private:
74 TrialToken(const url::Origin& origin, 88 TrialToken(const url::Origin& origin,
75 const std::string& feature_name, 89 const std::string& feature_name,
76 uint64_t expiry_timestamp); 90 uint64_t expiry_timestamp);
77 91
78 // The origin for which this token is valid. Must be a secure origin. 92 // The origin for which this token is valid. Must be a secure origin.
79 url::Origin origin_; 93 url::Origin origin_;
80 94
81 // The name of the experimental feature which this token enables. 95 // The name of the experimental feature which this token enables.
82 std::string feature_name_; 96 std::string feature_name_;
83 97
84 // The time until which this token should be considered valid. 98 // The time until which this token should be considered valid.
85 base::Time expiry_time_; 99 base::Time expiry_time_;
86 }; 100 };
87 101
88 } // namespace content 102 } // namespace content
89 103
90 #endif // CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ 104 #endif // CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698