Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: content/browser/background_fetch/background_fetch_registration_id.cc

Issue 2777063008: Connect BackgroundFetch to the OfflineItemCollection
Patch Set: Fixed incognito check and added tests Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
7 #include <tuple> 9 #include <tuple>
8 10
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/stringprintf.h"
9 #include "content/common/service_worker/service_worker_types.h" 14 #include "content/common/service_worker/service_worker_types.h"
10 15
11 namespace content { 16 namespace content {
12 17
18 namespace {
19
20 const int kSerializationVersion = 0;
21 const char kSeparator = '#';
22
23 } // namespace
24
13 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId() 25 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId()
14 : service_worker_registration_id_(kInvalidServiceWorkerRegistrationId) {} 26 : service_worker_registration_id_(kInvalidServiceWorkerRegistrationId) {}
15 27
16 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( 28 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId(
17 int64_t service_worker_registration_id, 29 int64_t service_worker_registration_id,
18 const url::Origin& origin, 30 const url::Origin& origin,
19 const std::string& tag) 31 const std::string& tag)
20 : service_worker_registration_id_(service_worker_registration_id), 32 : service_worker_registration_id_(service_worker_registration_id),
21 origin_(origin), 33 origin_(origin),
22 tag_(tag) {} 34 tag_(tag) {}
23 35
24 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( 36 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId(
25 const BackgroundFetchRegistrationId& other) = default; 37 const BackgroundFetchRegistrationId& other) = default;
26 38
27 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId( 39 BackgroundFetchRegistrationId::BackgroundFetchRegistrationId(
28 BackgroundFetchRegistrationId&& other) = default; 40 BackgroundFetchRegistrationId&& other) = default;
29 41
30 BackgroundFetchRegistrationId::~BackgroundFetchRegistrationId() = default; 42 BackgroundFetchRegistrationId::~BackgroundFetchRegistrationId() = default;
31 43
44 // Serialization format is:
45 // version#service_worker_registration_id#origin#tag
46 std::string BackgroundFetchRegistrationId::Serialize() const {
47 return base::StringPrintf(
48 "%d%c%s%c%s%c%s", kSerializationVersion, kSeparator,
49 base::Int64ToString(service_worker_registration_id_).c_str(), kSeparator,
50 origin_.Serialize().c_str(), kSeparator, tag_.c_str());
51 }
52
53 // static
54 bool BackgroundFetchRegistrationId::Deserialize(
55 const std::string& serialized_registration_id,
56 BackgroundFetchRegistrationId* out_registration_id) {
57 DCHECK(out_registration_id);
58 std::vector<std::string> parts =
59 base::SplitString(serialized_registration_id, std::string(1, kSeparator),
60 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
61 if (parts.size() <= 3)
62 return false;
63
64 // Currently, only a single version is supported.
65 int serialization_version;
66 if (!base::StringToInt(parts[0], &serialization_version) ||
67 kSerializationVersion != serialization_version)
68 return false;
69
70 if (!base::StringToInt64(
71 parts[1], &out_registration_id->service_worker_registration_id_)) {
72 return false;
73 }
74
75 out_registration_id->origin_ = url::Origin(GURL(parts[2]));
76 if (out_registration_id->origin_.unique())
77 return false;
78
79 std::ostringstream tag;
80 tag << parts[3];
81
82 // It's valid for a tag to contain the |kSeparator|, so concatenate further
83 // parts, if any, with the |tag|. This should be a rare case.
84 for (size_t i = 4; i < parts.size(); i++)
85 tag << kSeparator << parts[i];
86
87 out_registration_id->tag_ = tag.str();
88
89 // The tag must be a non-empty string.
90 return !out_registration_id->tag_.empty();
91 }
92
32 BackgroundFetchRegistrationId& BackgroundFetchRegistrationId::operator=( 93 BackgroundFetchRegistrationId& BackgroundFetchRegistrationId::operator=(
33 const BackgroundFetchRegistrationId& other) = default; 94 const BackgroundFetchRegistrationId& other) = default;
34 95
35 bool BackgroundFetchRegistrationId::operator==( 96 bool BackgroundFetchRegistrationId::operator==(
36 const BackgroundFetchRegistrationId& other) const { 97 const BackgroundFetchRegistrationId& other) const {
37 return other.service_worker_registration_id_ == 98 return other.service_worker_registration_id_ ==
38 service_worker_registration_id_ && 99 service_worker_registration_id_ &&
39 other.origin_ == origin_ && other.tag_ == tag_; 100 other.origin_ == origin_ && other.tag_ == tag_;
40 } 101 }
41 102
42 bool BackgroundFetchRegistrationId::operator!=( 103 bool BackgroundFetchRegistrationId::operator!=(
43 const BackgroundFetchRegistrationId& other) const { 104 const BackgroundFetchRegistrationId& other) const {
44 return !(*this == other); 105 return !(*this == other);
45 } 106 }
46 107
47 bool BackgroundFetchRegistrationId::operator<( 108 bool BackgroundFetchRegistrationId::operator<(
48 const BackgroundFetchRegistrationId& other) const { 109 const BackgroundFetchRegistrationId& other) const {
49 return std::tie(service_worker_registration_id_, origin_, tag_) < 110 return std::tie(service_worker_registration_id_, origin_, tag_) <
50 std::tie(other.service_worker_registration_id_, other.origin_, 111 std::tie(other.service_worker_registration_id_, other.origin_,
51 other.tag_); 112 other.tag_);
52 } 113 }
53 114
54 bool BackgroundFetchRegistrationId::is_null() const { 115 bool BackgroundFetchRegistrationId::is_null() const {
55 return service_worker_registration_id_ == kInvalidServiceWorkerRegistrationId; 116 return service_worker_registration_id_ == kInvalidServiceWorkerRegistrationId;
56 } 117 }
57 118
58 } // namespace content 119 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698