OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_ | 5 #ifndef CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_ |
6 #define CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_ | 6 #define CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_ |
7 | 7 |
| 8 #include <stdint.h> |
8 #include <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/logging.h" |
12 #include "url/gurl.h" | 14 #include "url/gurl.h" |
13 | 15 |
14 class Profile; | 16 class Profile; |
15 | 17 |
16 namespace user_prefs { | 18 namespace user_prefs { |
17 class PrefRegistrySyncable; | 19 class PrefRegistrySyncable; |
18 } | 20 } |
19 | 21 |
20 // The prefix used for all push messaging application ids. | 22 // The prefix used for all push messaging application ids. |
21 extern const char kPushMessagingAppIdentifierPrefix[]; | 23 extern const char kPushMessagingAppIdentifierPrefix[]; |
22 | 24 |
23 // Type used to identify a web app from a Push API perspective. | 25 // Type used to identify a Service Worker registration from a Push API |
24 // These can be persisted to disk, in a 1:1 mapping between app_id and | 26 // perspective. These can be persisted to prefs, in a 1:1 mapping between |
25 // pair<origin, service_worker_registration_id>. | 27 // app_id and pair<origin, service_worker_registration_id>. |
26 class PushMessagingAppIdentifier { | 28 class PushMessagingAppIdentifier { |
27 public: | 29 public: |
28 // Register profile-specific prefs. | 30 // Register profile-specific prefs. |
29 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 31 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
30 | 32 |
31 // Generates a new app identifier with random app_id. | 33 // Generates a new app identifier with random app_id. |
32 static PushMessagingAppIdentifier Generate( | 34 static PushMessagingAppIdentifier Generate( |
33 const GURL& origin, | 35 const GURL& origin, |
34 int64 service_worker_registration_id); | 36 int64_t service_worker_registration_id); |
35 | 37 |
36 // Looks up an app identifier by app_id. Will be invalid if not found. | 38 // Looks up an app identifier by app_id. If not found, is_null() will be true. |
37 static PushMessagingAppIdentifier Get(Profile* profile, | 39 static PushMessagingAppIdentifier Get(Profile* profile, |
38 const std::string& app_id); | 40 const std::string& app_id); |
39 | 41 |
40 // Looks up an app identifier by origin & service worker registration id. | 42 // Looks up an app identifier by origin & service worker registration id. |
41 // Will be invalid if not found. | 43 // If not found, is_null() will be true. |
42 static PushMessagingAppIdentifier Get(Profile* profile, | 44 static PushMessagingAppIdentifier Get(Profile* profile, |
43 const GURL& origin, | 45 const GURL& origin, |
44 int64 service_worker_registration_id); | 46 int64_t service_worker_registration_id); |
45 | 47 |
46 // Returns all the PushMessagingAppIdentifiers currently registered for the | 48 // Returns all the PushMessagingAppIdentifiers currently registered for the |
47 // given |profile|. | 49 // given |profile|. |
48 static std::vector<PushMessagingAppIdentifier> GetAll(Profile* profile); | 50 static std::vector<PushMessagingAppIdentifier> GetAll(Profile* profile); |
49 | 51 |
50 ~PushMessagingAppIdentifier(); | 52 ~PushMessagingAppIdentifier(); |
51 | 53 |
52 // Persist this app identifier to disk. | 54 // Persist this app identifier to prefs. |
53 void PersistToDisk(Profile* profile) const; | 55 void PersistToPrefs(Profile* profile) const; |
54 | 56 |
55 // Delete this app identifier from disk. | 57 // Delete this app identifier from prefs. |
56 void DeleteFromDisk(Profile* profile) const; // TODO: Does const make sense? | 58 void DeleteFromPrefs(Profile* profile) const; |
57 | 59 |
58 bool IsValid() 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; } |
59 | 63 |
60 const std::string& app_id() const { return app_id_; } | 64 // String that should be passed to push services like GCM to identify a |
61 const GURL& origin() const { return origin_; } | 65 // particular Service Worker (so we can route incoming messages). Example: |
62 int64 service_worker_registration_id() const { | 66 // wp:9CC55CCE-B8F9-4092-A364-3B0F73A3AB5F |
| 67 const std::string& app_id() const { |
| 68 DCHECK(!is_null()); |
| 69 return app_id_; |
| 70 } |
| 71 |
| 72 const GURL& origin() const { |
| 73 DCHECK(!is_null()); |
| 74 return origin_; |
| 75 } |
| 76 |
| 77 int64_t service_worker_registration_id() const { |
| 78 DCHECK(!is_null()); |
63 return service_worker_registration_id_; | 79 return service_worker_registration_id_; |
64 } | 80 } |
65 | 81 |
66 private: | 82 private: |
67 friend class PushMessagingAppIdentifierTest; | 83 friend class PushMessagingAppIdentifierTest; |
68 | 84 |
69 // Constructs an invalid app identifier. | 85 // Constructs an invalid app identifier. |
70 PushMessagingAppIdentifier(); | 86 PushMessagingAppIdentifier(); |
71 // Constructs a valid app identifier. | 87 // Constructs a valid app identifier. |
72 PushMessagingAppIdentifier(const std::string& app_id, | 88 PushMessagingAppIdentifier(const std::string& app_id, |
73 const GURL& origin, | 89 const GURL& origin, |
74 int64 service_worker_registration_id); | 90 int64_t service_worker_registration_id); |
| 91 |
| 92 // Validates that all the fields contain valid values. |
| 93 void DCheckValid() const; |
75 | 94 |
76 std::string app_id_; | 95 std::string app_id_; |
77 GURL origin_; | 96 GURL origin_; |
78 int64 service_worker_registration_id_; | 97 int64_t service_worker_registration_id_; |
79 }; | 98 }; |
80 | 99 |
81 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_ | 100 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_ |
OLD | NEW |