Chromium Code Reviews| Index: content/browser/background_fetch/background_fetch_registration_id.cc |
| diff --git a/content/browser/background_fetch/background_fetch_registration_id.cc b/content/browser/background_fetch/background_fetch_registration_id.cc |
| index 18156b0d4ea03193ead71dcfbeca3efedf17b477..26749641f250cebc2dbf29e655390f4cef68f2e9 100644 |
| --- a/content/browser/background_fetch/background_fetch_registration_id.cc |
| +++ b/content/browser/background_fetch/background_fetch_registration_id.cc |
| @@ -4,10 +4,23 @@ |
| #include "content/browser/background_fetch/background_fetch_registration_id.h" |
| +#include <iterator> |
| +#include <sstream> |
| + |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/stringprintf.h" |
| #include "content/common/service_worker/service_worker_types.h" |
| namespace content { |
| +namespace { |
| + |
| +const int kSerializationVersion = 0; |
| +const char kSeparator = '#'; |
| + |
| +} // namespace |
| + |
| BackgroundFetchRegistrationId::BackgroundFetchRegistrationId() |
| : service_worker_registration_id_(kInvalidServiceWorkerRegistrationId) {} |
| @@ -27,6 +40,59 @@ BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( |
| BackgroundFetchRegistrationId::~BackgroundFetchRegistrationId() = default; |
| +// Serialization format is: |
| +// version#service_worker_registration_id#origin#tag |
| +std::string BackgroundFetchRegistrationId::Serialize() const { |
| + return base::StringPrintf("%d%c%ld%c%s%c%s", kSerializationVersion, |
| + kSeparator, service_worker_registration_id_, |
| + kSeparator, origin_.Serialize().c_str(), kSeparator, |
| + tag_.c_str()); |
| +} |
| + |
| +// static |
| +bool BackgroundFetchRegistrationId::Deserialize( |
| + const std::string& serialized_registration_id, |
| + BackgroundFetchRegistrationId* out_registration_id) { |
| + DCHECK(out_registration_id); |
| + std::vector<std::string> parts = |
| + base::SplitString(serialized_registration_id, std::string(1, kSeparator), |
| + base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| + if (parts.size() <= 3) |
| + return false; |
| + |
| + // Currently, only a single version is supported. |
| + int serialization_version; |
| + if ((!base::StringToInt(parts[0], &serialization_version)) || |
| + (kSerializationVersion != serialization_version)) |
| + return false; |
| + |
| + if (!base::StringToInt64( |
| + parts[1], &out_registration_id->service_worker_registration_id_)) { |
| + return false; |
| + } |
| + |
| + out_registration_id->origin_ = url::Origin(GURL(parts[2])); |
| + if (out_registration_id->origin_.unique()) |
| + return false; |
| + |
| + // The tag could have sparators in it, so it is possible for the SplitString |
| + // to end up with more than 4 pieces. If so, concatenate all the extra pieces |
| + // into a single tag. |
| + if (parts.size() == 4) { |
| + if (parts[3] == "") |
| + return false; |
| + out_registration_id->tag_ = parts[3]; |
| + } else { |
| + std::ostringstream tag; |
| + std::copy(parts.begin() + 3, parts.end() - 1, |
| + std::ostream_iterator<std::string>(tag, &kSeparator)); |
| + tag << *(parts.end() - 1); |
| + out_registration_id->tag_ = tag.str(); |
| + } |
|
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.
|
| + |
| + return true; |
| +} |
| + |
| BackgroundFetchRegistrationId& BackgroundFetchRegistrationId::operator=( |
| const BackgroundFetchRegistrationId& other) = default; |