Chromium Code Reviews| 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 "base/strings/string_number_conversions.h" | |
| 8 #include "base/strings/string_split.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 | |
| 7 #include "content/common/service_worker/service_worker_types.h" | 11 #include "content/common/service_worker/service_worker_types.h" |
| 8 | 12 |
| 9 namespace content { | 13 namespace content { |
| 10 | 14 |
| 15 namespace { | |
| 16 | |
| 17 const int kSerializationVersion = 0; | |
| 18 const char kSeparator = '#'; | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 11 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId() | 22 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId() |
| 12 : service_worker_registration_id_(kInvalidServiceWorkerRegistrationId) {} | 23 : service_worker_registration_id_(kInvalidServiceWorkerRegistrationId) {} |
| 13 | 24 |
| 14 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( | 25 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( |
| 15 int64_t service_worker_registration_id, | 26 int64_t service_worker_registration_id, |
| 16 const url::Origin& origin, | 27 const url::Origin& origin, |
| 17 const std::string& tag) | 28 const std::string& tag) |
| 18 : service_worker_registration_id_(service_worker_registration_id), | 29 : service_worker_registration_id_(service_worker_registration_id), |
| 19 origin_(origin), | 30 origin_(origin), |
| 20 tag_(tag) {} | 31 tag_(tag) {} |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 46 const BackgroundFetchRegistrationId& other) const { | 57 const BackgroundFetchRegistrationId& other) const { |
| 47 return service_worker_registration_id_ < | 58 return service_worker_registration_id_ < |
| 48 other.service_worker_registration_id_ || | 59 other.service_worker_registration_id_ || |
| 49 origin_ < other.origin_ || tag_ < other.tag_; | 60 origin_ < other.origin_ || tag_ < other.tag_; |
| 50 } | 61 } |
| 51 | 62 |
| 52 bool BackgroundFetchRegistrationId::is_null() const { | 63 bool BackgroundFetchRegistrationId::is_null() const { |
| 53 return service_worker_registration_id_ == kInvalidServiceWorkerRegistrationId; | 64 return service_worker_registration_id_ == kInvalidServiceWorkerRegistrationId; |
| 54 } | 65 } |
| 55 | 66 |
| 67 std::string BackgroundFetchRegistrationId::Serialize() const { | |
| 68 return base::StringPrintf("%d%c%ld%c%s%c%s", kSerializationVersion, | |
| 69 kSeparator, service_worker_registration_id_, | |
| 70 kSeparator, origin_.Serialize().c_str(), kSeparator, | |
| 71 tag_.c_str()); | |
| 72 } | |
| 73 | |
| 74 // static | |
| 75 bool BackgroundFetchRegistrationId::Deserialize( | |
| 76 const std::string& serialized_registration_id, | |
| 77 BackgroundFetchRegistrationId& out_registration_id) { | |
| 78 std::vector<std::string> parts = | |
| 79 base::SplitString(serialized_registration_id, std::string(1, kSeparator), | |
| 80 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 81 if (parts.size() <= 3) | |
| 82 return false; | |
| 83 | |
| 84 // Currently, only a single version is supported. | |
| 85 int serialization_version; | |
| 86 if (!base::StringToInt(parts[0], &serialization_version)) | |
| 87 return false; | |
| 88 if (kSerializationVersion != serialization_version) | |
| 89 return false; | |
|
Peter Beverloo
2017/03/31 01:32:23
nit: can combine these calls:
int serialization
harkness
2017/03/31 10:11:44
Done.
| |
| 90 | |
| 91 if (!base::StringToInt64( | |
| 92 parts[1], &out_registration_id.service_worker_registration_id_)) | |
| 93 return false; | |
|
Peter Beverloo
2017/03/31 01:32:23
{}
harkness
2017/03/31 10:11:44
Done.
| |
| 94 | |
| 95 out_registration_id.origin_ = url::Origin(GURL(parts[2])); | |
| 96 if (out_registration_id.origin_.unique()) | |
| 97 return false; | |
| 98 | |
| 99 // The tag could have sparators in it, so it is possible for the SplitString | |
| 100 // to end up with more than 4 pieces. If so, concatenate all the extra pieces | |
| 101 // into a single tag. This is inefficient, but also very rare. | |
| 102 if (parts.size() == 4) { | |
| 103 if (parts[3] == "") | |
| 104 return false; | |
| 105 out_registration_id.tag_ = parts[3]; | |
| 106 } else { | |
| 107 std::string tag = parts[3]; | |
| 108 for (size_t i = 4; i < parts.size(); i++) { | |
| 109 tag = tag + kSeparator + parts[i]; | |
| 110 } | |
| 111 out_registration_id.tag_ = tag; | |
| 112 } | |
| 113 | |
|
Peter Beverloo
2017/03/31 01:32:23
I wonder if you could replace this with something
harkness
2017/03/31 10:11:44
I had to tweak this a bit to make it work (not add
| |
| 114 return true; | |
| 115 } | |
| 116 | |
| 56 } // namespace content | 117 } // namespace content |
| OLD | NEW |