Cleanup PushMessagingAppIdentifier
[chromium-blink-merge.git] / chrome / browser / push_messaging / push_messaging_app_identifier.h
blob6d64fc2be5f4066ed6b5a1ca7b493c9dc5a564db
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 #ifndef CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_
6 #define CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/logging.h"
14 #include "url/gurl.h"
16 class Profile;
18 namespace user_prefs {
19 class PrefRegistrySyncable;
22 // The prefix used for all push messaging application ids.
23 extern const char kPushMessagingAppIdentifierPrefix[];
25 // Type used to identify a Service Worker registration from a Push API
26 // perspective. These can be persisted to prefs, in a 1:1 mapping between
27 // app_id and pair<origin, service_worker_registration_id>.
28 class PushMessagingAppIdentifier {
29 public:
30 // Register profile-specific prefs.
31 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
33 // Generates a new app identifier with random app_id.
34 static PushMessagingAppIdentifier Generate(
35 const GURL& origin,
36 int64_t service_worker_registration_id);
38 // Looks up an app identifier by app_id. If not found, is_null() will be true.
39 static PushMessagingAppIdentifier Get(Profile* profile,
40 const std::string& app_id);
42 // Looks up an app identifier by origin & service worker registration id.
43 // If not found, is_null() will be true.
44 static PushMessagingAppIdentifier Get(Profile* profile,
45 const GURL& origin,
46 int64_t service_worker_registration_id);
48 // Returns all the PushMessagingAppIdentifiers currently registered for the
49 // given |profile|.
50 static std::vector<PushMessagingAppIdentifier> GetAll(Profile* profile);
52 ~PushMessagingAppIdentifier();
54 // Persist this app identifier to prefs.
55 void PersistToPrefs(Profile* profile) const;
57 // Delete this app identifier from prefs.
58 void DeleteFromPrefs(Profile* profile) const;
60 // Returns true if this identifier does not represent an app (i.e. this was
61 // returned by a failed call to Get).
62 bool is_null() const { return service_worker_registration_id_ < 0; }
64 // String that should be passed to push services like GCM to identify a
65 // particular Service Worker (so we can route incoming messages). Example:
66 // wp:9CC55CCE-B8F9-4092-A364-3B0F73A3AB5F
67 const std::string& app_id() const {
68 DCHECK(!is_null());
69 return app_id_;
72 const GURL& origin() const {
73 DCHECK(!is_null());
74 return origin_;
77 int64_t service_worker_registration_id() const {
78 DCHECK(!is_null());
79 return service_worker_registration_id_;
82 private:
83 friend class PushMessagingAppIdentifierTest;
85 // Constructs an invalid app identifier.
86 PushMessagingAppIdentifier();
87 // Constructs a valid app identifier.
88 PushMessagingAppIdentifier(const std::string& app_id,
89 const GURL& origin,
90 int64_t service_worker_registration_id);
92 // Validates that all the fields contain valid values.
93 void DCheckValid() const;
95 std::string app_id_;
96 GURL origin_;
97 int64_t service_worker_registration_id_;
100 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_