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 db7653bbb1bcc0ca051d49c2c8e78cf9ff0f3ab0..ce9e5af9918a5a7e373cd87df38ac71555180f88 100644 |
| --- a/content/browser/background_fetch/background_fetch_registration_id.cc |
| +++ b/content/browser/background_fetch/background_fetch_registration_id.cc |
| @@ -4,12 +4,24 @@ |
| #include "content/browser/background_fetch/background_fetch_registration_id.h" |
| +#include <iterator> |
| +#include <sstream> |
| #include <tuple> |
| +#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) {} |
| @@ -29,6 +41,55 @@ 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%s%c%s%c%s", kSerializationVersion, kSeparator, |
|
David Trainor- moved to gerrit
2017/04/18 22:15:55
Would encoding/decoding these as url parameters be
Peter Beverloo
2017/04/18 23:12:08
Do you know if we have common code for compiling a
David Trainor- moved to gerrit
2017/04/19 22:45:12
ContentSuggestionsNotificationHelper.java uses a U
|
| + base::Int64ToString(service_worker_registration_id_).c_str(), 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)) |
|
Peter Beverloo
2017/04/18 23:12:08
nit: remove the extra () (twice), brackets since i
harkness
2017/04/19 09:03:41
Done.
|
| + 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; |
| + |
| + std::ostringstream tag; |
| + tag << parts[3]; |
| + |
| + // It's valid for a tag to contain the |kSeparator|, so concatenate further |
| + // parts, if any, with the |tag|. This should be a rare case. |
| + for (size_t i = 4; i < parts.size(); i++) |
| + tag << kSeparator << parts[i]; |
| + |
| + out_registration_id->tag_ = tag.str(); |
| + |
| + // The tag must be a non-empty string. |
| + return !out_registration_id->tag_.empty(); |
| +} |
| + |
| BackgroundFetchRegistrationId& BackgroundFetchRegistrationId::operator=( |
| const BackgroundFetchRegistrationId& other) = default; |