Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / push_messaging / push_messaging_app_identifier.h
blob32bfb22b601a3f1b0466fb9a81e6b8c43911e2b2
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/gtest_prod_util.h"
14 #include "base/logging.h"
15 #include "url/gurl.h"
17 class Profile;
19 namespace user_prefs {
20 class PrefRegistrySyncable;
23 // The prefix used for all push messaging application ids.
24 extern const char kPushMessagingAppIdentifierPrefix[];
26 // Type used to identify a Service Worker registration from a Push API
27 // perspective. These can be persisted to prefs, in a 1:1 mapping between
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>.
32 class PushMessagingAppIdentifier {
33 public:
34 // Register profile-specific prefs.
35 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
37 // Generates a new app identifier, with partially random app_id.
38 static PushMessagingAppIdentifier Generate(
39 const GURL& origin,
40 int64_t service_worker_registration_id);
42 // Looks up an app identifier by app_id. If not found, is_null() will be true.
43 static PushMessagingAppIdentifier FindByAppId(Profile* profile,
44 const std::string& app_id);
46 // Looks up an app identifier by origin & service worker registration id.
47 // If not found, is_null() will be true.
48 static PushMessagingAppIdentifier FindByServiceWorker(
49 Profile* profile,
50 const GURL& origin,
51 int64_t service_worker_registration_id);
53 // Returns all the PushMessagingAppIdentifiers currently registered for the
54 // given |profile|.
55 static std::vector<PushMessagingAppIdentifier> GetAll(Profile* profile);
57 ~PushMessagingAppIdentifier();
59 // Persist this app identifier to prefs.
60 void PersistToPrefs(Profile* profile) const;
62 // Delete this app identifier from prefs.
63 void DeleteFromPrefs(Profile* profile) const;
65 // Returns true if this identifier does not represent an app (i.e. this was
66 // returned by a failed Find call).
67 bool is_null() const { return service_worker_registration_id_ < 0; }
69 // String that should be passed to push services like GCM to identify a
70 // particular Service Worker (so we can route incoming messages). Example:
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
73 const std::string& app_id() const {
74 DCHECK(!is_null());
75 return app_id_;
78 const GURL& origin() const {
79 DCHECK(!is_null());
80 return origin_;
83 int64_t service_worker_registration_id() const {
84 DCHECK(!is_null());
85 return service_worker_registration_id_;
88 private:
89 friend class PushMessagingAppIdentifierTest;
90 FRIEND_TEST_ALL_PREFIXES(PushMessagingAppIdentifierTest, FindLegacy);
92 // Constructs an invalid app identifier.
93 PushMessagingAppIdentifier();
94 // Constructs a valid app identifier.
95 PushMessagingAppIdentifier(const std::string& app_id,
96 const GURL& origin,
97 int64_t service_worker_registration_id);
99 // Validates that all the fields contain valid values.
100 void DCheckValid() const;
102 std::string app_id_;
103 GURL origin_;
104 int64_t service_worker_registration_id_;
107 #endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_APP_IDENTIFIER_H_