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 <stdint.h> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 class Profile; | 16 class Profile; |
17 | 17 |
18 namespace user_prefs { | 18 namespace user_prefs { |
19 class PrefRegistrySyncable; | 19 class PrefRegistrySyncable; |
20 } | 20 } |
21 | 21 |
22 // The prefix used for all push messaging application ids. | 22 // The prefix used for all push messaging application ids. |
23 extern const char kPushMessagingAppIdentifierPrefix[]; | 23 extern const char kPushMessagingAppIdentifierPrefix[]; |
24 | 24 |
25 // Type used to identify a Service Worker registration from a Push API | 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 | 26 // perspective. These can be persisted to prefs, in a 1:1 mapping between |
27 // app_id and pair<origin, service_worker_registration_id>. | 27 // app_id (which includes origin) and service_worker_registration_id. |
| 28 // Legacy mapped values saved by old versions of Chrome are also supported; |
| 29 // these don't contain the origin in the app_id, so instead they map from |
| 30 // app_id to pair<origin, service_worker_registration_id>. |
28 class PushMessagingAppIdentifier { | 31 class PushMessagingAppIdentifier { |
29 public: | 32 public: |
30 // Register profile-specific prefs. | 33 // Register profile-specific prefs. |
31 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 34 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
32 | 35 |
33 // Generates a new app identifier with random app_id. | 36 // Generates a new app identifier, with partially random app_id. |
34 static PushMessagingAppIdentifier Generate( | 37 static PushMessagingAppIdentifier Generate( |
35 const GURL& origin, | 38 const GURL& origin, |
36 int64_t service_worker_registration_id); | 39 int64_t service_worker_registration_id); |
37 | 40 |
38 // Looks up an app identifier by app_id. If not found, is_null() will be true. | 41 // Looks up an app identifier by app_id. If not found, is_null() will be true. |
39 static PushMessagingAppIdentifier Get(Profile* profile, | 42 static PushMessagingAppIdentifier FindByAppId(Profile* profile, |
40 const std::string& app_id); | 43 const std::string& app_id); |
41 | 44 |
42 // Looks up an app identifier by origin & service worker registration id. | 45 // Looks up an app identifier by origin & service worker registration id. |
43 // If not found, is_null() will be true. | 46 // If not found, is_null() will be true. |
44 static PushMessagingAppIdentifier Get(Profile* profile, | 47 static PushMessagingAppIdentifier FindByServiceWorker( |
45 const GURL& origin, | 48 Profile* profile, |
46 int64_t service_worker_registration_id); | 49 const GURL& origin, |
| 50 int64_t service_worker_registration_id); |
47 | 51 |
48 // Returns all the PushMessagingAppIdentifiers currently registered for the | 52 // Returns all the PushMessagingAppIdentifiers currently registered for the |
49 // given |profile|. | 53 // given |profile|. |
50 static std::vector<PushMessagingAppIdentifier> GetAll(Profile* profile); | 54 static std::vector<PushMessagingAppIdentifier> GetAll(Profile* profile); |
51 | 55 |
52 ~PushMessagingAppIdentifier(); | 56 ~PushMessagingAppIdentifier(); |
53 | 57 |
54 // Persist this app identifier to prefs. | 58 // Persist this app identifier to prefs. |
55 void PersistToPrefs(Profile* profile) const; | 59 void PersistToPrefs(Profile* profile) const; |
56 | 60 |
57 // Delete this app identifier from prefs. | 61 // Delete this app identifier from prefs. |
58 void DeleteFromPrefs(Profile* profile) const; | 62 void DeleteFromPrefs(Profile* profile) const; |
59 | 63 |
60 // Returns true if this identifier does not represent an app (i.e. this was | 64 // Returns true if this identifier does not represent an app (i.e. this was |
61 // returned by a failed call to Get). | 65 // returned by a failed Find call). |
62 bool is_null() const { return service_worker_registration_id_ < 0; } | 66 bool is_null() const { return service_worker_registration_id_ < 0; } |
63 | 67 |
64 // String that should be passed to push services like GCM to identify a | 68 // String that should be passed to push services like GCM to identify a |
65 // particular Service Worker (so we can route incoming messages). Example: | 69 // particular Service Worker (so we can route incoming messages). Example: |
66 // wp:9CC55CCE-B8F9-4092-A364-3B0F73A3AB5F | 70 // wp:https://foo.example.com:8443/#9CC55CCE-B8F9-4092-A364-3B0F73A3AB5F |
| 71 // Legacy app_ids have no origin, e.g. wp:9CC55CCE-B8F9-4092-A364-3B0F73A3AB5F |
67 const std::string& app_id() const { | 72 const std::string& app_id() const { |
68 DCHECK(!is_null()); | 73 DCHECK(!is_null()); |
69 return app_id_; | 74 return app_id_; |
70 } | 75 } |
71 | 76 |
72 const GURL& origin() const { | 77 const GURL& origin() const { |
73 DCHECK(!is_null()); | 78 DCHECK(!is_null()); |
74 return origin_; | 79 return origin_; |
75 } | 80 } |
76 | 81 |
(...skipping 14 matching lines...) Expand all Loading... |
91 | 96 |
92 // Validates that all the fields contain valid values. | 97 // Validates that all the fields contain valid values. |
93 void DCheckValid() const; | 98 void DCheckValid() const; |
94 | 99 |
95 std::string app_id_; | 100 std::string app_id_; |
96 GURL origin_; | 101 GURL origin_; |
97 int64_t service_worker_registration_id_; | 102 int64_t service_worker_registration_id_; |
98 }; | 103 }; |
99 | 104 |
100 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_ | 105 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_ |
OLD | NEW |