Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / services / gcm / push_messaging_application_id.cc
blobe1afca64d90cb662ae9fc713d74089efa3a113bd
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/services/gcm/push_messaging_application_id.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_split.h"
11 namespace {
12 const char kSeparator = '#'; // Ok as only the origin of the url is used.
15 namespace gcm {
17 const char kPushMessagingApplicationIdPrefix[] = "push";
19 // static
20 PushMessagingApplicationId PushMessagingApplicationId::Parse(
21 const std::string& id) {
22 std::vector<std::string> parts;
23 base::SplitString(id, kSeparator, &parts);
25 if (parts.size() != 3 || parts[0] != kPushMessagingApplicationIdPrefix)
26 return PushMessagingApplicationId();
28 GURL origin = GURL(parts[1]);
29 if (!origin.is_valid() || origin.GetOrigin() != origin)
30 return PushMessagingApplicationId();
32 int64 service_worker_registration_id;
33 if (!base::StringToInt64(parts[2], &service_worker_registration_id))
34 return PushMessagingApplicationId();
36 return PushMessagingApplicationId(origin, service_worker_registration_id);
39 bool PushMessagingApplicationId::IsValid() {
40 return origin.is_valid() && origin.GetOrigin() == origin &&
41 service_worker_registration_id >= 0;
44 std::string PushMessagingApplicationId::ToString() const {
45 return (std::string(kPushMessagingApplicationIdPrefix) + kSeparator +
46 origin.spec() + kSeparator +
47 base::Int64ToString(service_worker_registration_id));
50 } // namespace gcm