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