OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 5 #include "content/browser/background_fetch/background_fetch_registration_id.h" |
6 | 6 |
7 #include <iterator> | |
8 #include <sstream> | |
9 | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_split.h" | |
12 #include "base/strings/stringprintf.h" | |
7 #include "content/common/service_worker/service_worker_types.h" | 13 #include "content/common/service_worker/service_worker_types.h" |
8 | 14 |
9 namespace content { | 15 namespace content { |
10 | 16 |
17 namespace { | |
18 | |
19 const int kSerializationVersion = 0; | |
20 const char kSeparator = '#'; | |
21 | |
22 } // namespace | |
23 | |
11 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId() | 24 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId() |
12 : service_worker_registration_id_(kInvalidServiceWorkerRegistrationId) {} | 25 : service_worker_registration_id_(kInvalidServiceWorkerRegistrationId) {} |
13 | 26 |
14 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( | 27 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( |
15 int64_t service_worker_registration_id, | 28 int64_t service_worker_registration_id, |
16 const url::Origin& origin, | 29 const url::Origin& origin, |
17 const std::string& tag) | 30 const std::string& tag) |
18 : service_worker_registration_id_(service_worker_registration_id), | 31 : service_worker_registration_id_(service_worker_registration_id), |
19 origin_(origin), | 32 origin_(origin), |
20 tag_(tag) {} | 33 tag_(tag) {} |
21 | 34 |
22 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( | 35 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( |
23 const BackgroundFetchRegistrationId& other) = default; | 36 const BackgroundFetchRegistrationId& other) = default; |
24 | 37 |
25 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( | 38 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( |
26 BackgroundFetchRegistrationId&& other) = default; | 39 BackgroundFetchRegistrationId&& other) = default; |
27 | 40 |
28 BackgroundFetchRegistrationId::~BackgroundFetchRegistrationId() = default; | 41 BackgroundFetchRegistrationId::~BackgroundFetchRegistrationId() = default; |
29 | 42 |
43 // Serialization format is: | |
44 // version#service_worker_registration_id#origin#tag | |
45 std::string BackgroundFetchRegistrationId::Serialize() const { | |
46 return base::StringPrintf("%d%c%ld%c%s%c%s", kSerializationVersion, | |
47 kSeparator, service_worker_registration_id_, | |
48 kSeparator, origin_.Serialize().c_str(), kSeparator, | |
49 tag_.c_str()); | |
50 } | |
51 | |
52 // static | |
53 bool BackgroundFetchRegistrationId::Deserialize( | |
54 const std::string& serialized_registration_id, | |
55 BackgroundFetchRegistrationId* out_registration_id) { | |
56 DCHECK(out_registration_id); | |
57 std::vector<std::string> parts = | |
58 base::SplitString(serialized_registration_id, std::string(1, kSeparator), | |
59 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
60 if (parts.size() <= 3) | |
61 return false; | |
62 | |
63 // Currently, only a single version is supported. | |
64 int serialization_version; | |
65 if ((!base::StringToInt(parts[0], &serialization_version)) || | |
66 (kSerializationVersion != serialization_version)) | |
67 return false; | |
68 | |
69 if (!base::StringToInt64( | |
70 parts[1], &out_registration_id->service_worker_registration_id_)) { | |
71 return false; | |
72 } | |
73 | |
74 out_registration_id->origin_ = url::Origin(GURL(parts[2])); | |
75 if (out_registration_id->origin_.unique()) | |
76 return false; | |
77 | |
78 // The tag could have sparators in it, so it is possible for the SplitString | |
79 // to end up with more than 4 pieces. If so, concatenate all the extra pieces | |
80 // into a single tag. | |
81 if (parts.size() == 4) { | |
82 if (parts[3] == "") | |
83 return false; | |
84 out_registration_id->tag_ = parts[3]; | |
85 } else { | |
86 std::ostringstream tag; | |
87 std::copy(parts.begin() + 3, parts.end() - 1, | |
88 std::ostream_iterator<std::string>(tag, &kSeparator)); | |
89 tag << *(parts.end() - 1); | |
90 out_registration_id->tag_ = tag.str(); | |
91 } | |
Peter Beverloo
2017/03/31 11:54:06
What I'd love is for this branch to go away. Can w
harkness
2017/04/03 12:47:22
Done.
| |
92 | |
93 return true; | |
94 } | |
95 | |
30 BackgroundFetchRegistrationId& BackgroundFetchRegistrationId::operator=( | 96 BackgroundFetchRegistrationId& BackgroundFetchRegistrationId::operator=( |
31 const BackgroundFetchRegistrationId& other) = default; | 97 const BackgroundFetchRegistrationId& other) = default; |
32 | 98 |
33 bool BackgroundFetchRegistrationId::operator==( | 99 bool BackgroundFetchRegistrationId::operator==( |
34 const BackgroundFetchRegistrationId& other) const { | 100 const BackgroundFetchRegistrationId& other) const { |
35 return other.service_worker_registration_id_ == | 101 return other.service_worker_registration_id_ == |
36 service_worker_registration_id_ && | 102 service_worker_registration_id_ && |
37 other.origin_ == origin_ && other.tag_ == tag_; | 103 other.origin_ == origin_ && other.tag_ == tag_; |
38 } | 104 } |
39 | 105 |
40 bool BackgroundFetchRegistrationId::operator!=( | 106 bool BackgroundFetchRegistrationId::operator!=( |
41 const BackgroundFetchRegistrationId& other) const { | 107 const BackgroundFetchRegistrationId& other) const { |
42 return !(*this == other); | 108 return !(*this == other); |
43 } | 109 } |
44 | 110 |
45 bool BackgroundFetchRegistrationId::operator<( | 111 bool BackgroundFetchRegistrationId::operator<( |
46 const BackgroundFetchRegistrationId& other) const { | 112 const BackgroundFetchRegistrationId& other) const { |
47 return service_worker_registration_id_ < | 113 return service_worker_registration_id_ < |
48 other.service_worker_registration_id_ || | 114 other.service_worker_registration_id_ || |
49 origin_ < other.origin_ || tag_ < other.tag_; | 115 origin_ < other.origin_ || tag_ < other.tag_; |
50 } | 116 } |
51 | 117 |
52 bool BackgroundFetchRegistrationId::is_null() const { | 118 bool BackgroundFetchRegistrationId::is_null() const { |
53 return service_worker_registration_id_ == kInvalidServiceWorkerRegistrationId; | 119 return service_worker_registration_id_ == kInvalidServiceWorkerRegistrationId; |
54 } | 120 } |
55 | 121 |
56 } // namespace content | 122 } // namespace content |
OLD | NEW |