OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016 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 NET_TRAFFIC_ANNOTATION_NETWORK_TRAFFIC_ANNOTATION_H_ | |
6 #define NET_TRAFFIC_ANNOTATION_NETWORK_TRAFFIC_ANNOTATION_H_ | |
7 | |
8 namespace { | |
9 | |
10 // Recuresively compute hash code of the given string as a constant expression. | |
11 template <int N> | |
12 static constexpr unsigned int recursive_hash(const char* str) { | |
13 return (unsigned int)(recursive_hash<N - 1>(str) * 31 + str[N]) % 138003713; | |
14 } | |
15 | |
16 // Recursion stopper for the above function. | |
17 template <> | |
18 constexpr unsigned int recursive_hash<-1>(const char* str) { | |
19 return 7; | |
20 } | |
21 | |
22 // Entry point to function that computes hash as constant expression. | |
23 #define COMPUTE_STRING_HASH(S) recursive_hash<sizeof(S)>(S) | |
24 | |
25 } // namespace | |
26 | |
27 namespace net { | |
28 | |
29 // Defined type for network traffic annotation tags. | |
30 typedef const unsigned int NetworkTrafficAnnotationTag; | |
asanka
2017/01/26 20:59:28
using NetworkTrafficAnnotationTag = ...
Also, use
Ramin Halavati
2017/01/27 07:48:58
Done.
| |
31 | |
32 // Function to convert a network traffic annotation's unique id and protobuf | |
33 // text into a NetworkTrafficAnnotationTag. | |
34 // | |
35 // This function serves as a tag that can be discovered and extracted via | |
36 // clang tools. This allows reviewing all network traffic that is generated | |
37 // and annotated by Chrome. | |
38 // | |
39 // |unique_id| should be a string that uniquely identifies this annotation | |
40 // across all of Chromium source code. | |
41 // |proto| is a text-encoded NetworkTrafficAnnotation protobuf (see | |
42 // tools/traffic_annotaiton/traffic_annotation.proto) | |
43 // | |
44 // This function should be called with inlined parameters like | |
45 // NetworkTrafficAnnotationTag tag = DefineNetworkTrafficAnnotation( | |
46 // "unique_tag_id", | |
47 // R"(semantics: {...} | |
48 // policy: {...} | |
49 // )"); | |
50 // This allows the compiler to calculate and extract the hashed |unique_id| | |
51 // at compile time without copying the entire protobuf into the text segment | |
52 // of the binary or creating any runtime performance impact. | |
53 // | |
54 // Please do NOT use the following syntax: | |
55 // const char* proto = R("..."); | |
56 // NetworkTrafficAnnotationTag tag = DefineNetworkTrafficAnnotation( | |
57 // "unique_tag_id", proto); | |
58 constexpr NetworkTrafficAnnotationTag DefineNetworkTrafficAnnotation( | |
59 const char* unique_id, | |
asanka
2017/01/26 20:59:28
Actually, if the intention is to introduce a param
Ramin Halavati
2017/01/27 07:48:58
|unique_id|s have two purposes here:
1- They stay
asanka
2017/01/27 16:58:00
Using typed ints (a.la enum NetworkTrafficAnnotati
Ramin Halavati
2017/01/30 08:43:25
I am not sure if I got your concern clearly. Now t
| |
60 const char* proto) { | |
61 return (NetworkTrafficAnnotationTag)COMPUTE_STRING_HASH(unique_id); | |
asanka
2017/01/26 20:59:28
sizeof(unique_id) == sizeof(const char*) here.
Ramin Halavati
2017/01/27 07:48:58
Yes, but we want the |unique_id| to be kept in the
asanka
2017/01/27 16:58:00
Sure, but what I was pointing out was that you are
Ramin Halavati
2017/01/30 08:43:25
Acknowledged.
| |
62 } | |
63 | |
64 // Placeholder for unannotated usages. | |
65 #define NO_TRAFFIC_ANNOTATION_YET \ | |
66 net::DefineNetworkTrafficAnnotation("Undefined", "Nothing here yet.") | |
67 | |
68 // Macro for unit tests traffic annotations. | |
69 #define TRAFFIC_ANNOTATION_FOR_TESTS \ | |
70 net::DefineNetworkTrafficAnnotation( \ | |
71 "UnitTest", "Traffic annotation for unit, browser and other tests") | |
72 | |
73 } // namespace net | |
74 | |
75 #endif // NET_TRAFFIC_ANNOTATION_NETWORK_TRAFFIC_ANNOTATION_H_ | |
OLD | NEW |