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_
12 #include "base/basictypes.h"
13 #include "base/logging.h"
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
{
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(
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
,
46 int64_t service_worker_registration_id
);
48 // Returns all the PushMessagingAppIdentifiers currently registered for the
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 {
72 const GURL
& origin() const {
77 int64_t service_worker_registration_id() const {
79 return service_worker_registration_id_
;
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
,
90 int64_t service_worker_registration_id
);
92 // Validates that all the fields contain valid values.
93 void DCheckValid() const;
97 int64_t service_worker_registration_id_
;
100 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_