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..401a1479b52db74e2bd6bcab765e559205ed1558 100644 |
--- a/content/browser/background_fetch/background_fetch_registration_id.cc |
+++ b/content/browser/background_fetch/background_fetch_registration_id.cc |
@@ -4,10 +4,21 @@ |
#include "content/browser/background_fetch/background_fetch_registration_id.h" |
+#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) {} |
@@ -53,4 +64,54 @@ bool BackgroundFetchRegistrationId::is_null() const { |
return service_worker_registration_id_ == kInvalidServiceWorkerRegistrationId; |
} |
+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) { |
+ 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)) |
+ return false; |
+ if (kSerializationVersion != serialization_version) |
+ 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.
|
+ |
+ if (!base::StringToInt64( |
+ parts[1], &out_registration_id.service_worker_registration_id_)) |
+ return false; |
Peter Beverloo
2017/03/31 01:32:23
{}
harkness
2017/03/31 10:11:44
Done.
|
+ |
+ 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. This is inefficient, but also very rare. |
+ if (parts.size() == 4) { |
+ if (parts[3] == "") |
+ return false; |
+ out_registration_id.tag_ = parts[3]; |
+ } else { |
+ std::string tag = parts[3]; |
+ for (size_t i = 4; i < parts.size(); i++) { |
+ tag = tag + kSeparator + parts[i]; |
+ } |
+ out_registration_id.tag_ = tag; |
+ } |
+ |
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
|
+ return true; |
+} |
+ |
} // namespace content |