| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "content/public/browser/push_messaging_service.h" | 5 #include "content/public/browser/push_messaging_service.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "chrome/browser/permissions/permission_manager.h" | 16 #include "chrome/browser/permissions/permission_manager.h" |
| 17 #include "chrome/browser/permissions/permission_manager_factory.h" | 17 #include "chrome/browser/permissions/permission_manager_factory.h" |
| 18 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" | 18 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" |
| 19 #include "chrome/browser/push_messaging/push_messaging_permission_context.h" | 19 #include "chrome/browser/push_messaging/push_messaging_permission_context.h" |
| 20 #include "chrome/browser/push_messaging/push_messaging_permission_context_factor
y.h" | 20 #include "chrome/browser/push_messaging/push_messaging_permission_context_factor
y.h" |
| 21 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" | 21 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" |
| 22 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" | 22 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
| 23 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 23 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| 24 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 24 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 25 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 26 #include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h" | 26 #include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h" |
| 27 #include "components/gcm_driver/fake_gcm_client_factory.h" | 27 #include "components/gcm_driver/fake_gcm_client_factory.h" |
| 28 #include "components/gcm_driver/gcm_profile_service.h" | 28 #include "components/gcm_driver/gcm_profile_service.h" |
| 29 #include "content/public/common/content_switches.h" | 29 #include "content/public/common/content_switches.h" |
| 30 #include "content/public/common/push_event_payload.h" |
| 30 #include "content/public/test/test_browser_thread_bundle.h" | 31 #include "content/public/test/test_browser_thread_bundle.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 const char kTestOrigin[] = "https://example.com"; | 36 const char kTestOrigin[] = "https://example.com"; |
| 36 const char kTestSenderId[] = "1234567890"; | 37 const char kTestSenderId[] = "1234567890"; |
| 37 const int64_t kTestServiceWorkerId = 42; | 38 const int64_t kTestServiceWorkerId = 42; |
| 38 const char kTestPayload[] = "Hello, world!"; | 39 const char kTestPayload[] = "Hello, world!"; |
| 39 | 40 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 117 |
| 117 *subscription_id_out = registration_id; | 118 *subscription_id_out = registration_id; |
| 118 *p256dh_out = p256dh; | 119 *p256dh_out = p256dh; |
| 119 *auth_out = auth; | 120 *auth_out = auth; |
| 120 } | 121 } |
| 121 | 122 |
| 122 // Callback to use when observing messages dispatched by the push service. | 123 // Callback to use when observing messages dispatched by the push service. |
| 123 void DidDispatchMessage(std::string* app_id_out, | 124 void DidDispatchMessage(std::string* app_id_out, |
| 124 GURL* origin_out, | 125 GURL* origin_out, |
| 125 int64_t* service_worker_registration_id_out, | 126 int64_t* service_worker_registration_id_out, |
| 126 std::string* message_data_out, | 127 content::PushEventPayload* payload_out, |
| 127 const std::string& app_id, | 128 const std::string& app_id, |
| 128 const GURL& origin, | 129 const GURL& origin, |
| 129 int64_t service_worker_registration_id, | 130 int64_t service_worker_registration_id, |
| 130 const std::string& message_data) { | 131 const content::PushEventPayload& payload) { |
| 131 *app_id_out = app_id; | 132 *app_id_out = app_id; |
| 132 *origin_out = origin; | 133 *origin_out = origin; |
| 133 *service_worker_registration_id_out = service_worker_registration_id; | 134 *service_worker_registration_id_out = service_worker_registration_id; |
| 134 *message_data_out = message_data; | 135 *payload_out = payload; |
| 135 } | 136 } |
| 136 | 137 |
| 137 protected: | 138 protected: |
| 138 PushMessagingTestingProfile* profile() { return &profile_; } | 139 PushMessagingTestingProfile* profile() { return &profile_; } |
| 139 | 140 |
| 140 private: | 141 private: |
| 141 content::TestBrowserThreadBundle thread_bundle_; | 142 content::TestBrowserThreadBundle thread_bundle_; |
| 142 PushMessagingTestingProfile profile_; | 143 PushMessagingTestingProfile profile_; |
| 143 }; | 144 }; |
| 144 | 145 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 ASSERT_NE(kTestPayload, message.raw_data); | 187 ASSERT_NE(kTestPayload, message.raw_data); |
| 187 ASSERT_FALSE(message.decrypted); | 188 ASSERT_FALSE(message.decrypted); |
| 188 | 189 |
| 189 // (4) Find the app_id that has been associated with the subscription. | 190 // (4) Find the app_id that has been associated with the subscription. |
| 190 PushMessagingAppIdentifier app_identifier = | 191 PushMessagingAppIdentifier app_identifier = |
| 191 PushMessagingAppIdentifier::FindByServiceWorker(profile(), origin, | 192 PushMessagingAppIdentifier::FindByServiceWorker(profile(), origin, |
| 192 kTestServiceWorkerId); | 193 kTestServiceWorkerId); |
| 193 | 194 |
| 194 ASSERT_FALSE(app_identifier.is_null()); | 195 ASSERT_FALSE(app_identifier.is_null()); |
| 195 | 196 |
| 196 std::string app_id, message_data; | 197 std::string app_id; |
| 197 GURL dispatched_origin; | 198 GURL dispatched_origin; |
| 198 int64_t service_worker_registration_id; | 199 int64_t service_worker_registration_id; |
| 200 content::PushEventPayload payload; |
| 199 | 201 |
| 200 // (5) Observe message dispatchings from the Push Messaging service, and then | 202 // (5) Observe message dispatchings from the Push Messaging service, and then |
| 201 // dispatch the |message| on the GCM driver as if it had actually been | 203 // dispatch the |message| on the GCM driver as if it had actually been |
| 202 // received by Google Cloud Messaging. | 204 // received by Google Cloud Messaging. |
| 203 push_service->SetMessageDispatchedCallbackForTesting( | 205 push_service->SetMessageDispatchedCallbackForTesting(base::Bind( |
| 204 base::Bind(&PushMessagingServiceTest::DidDispatchMessage, | 206 &PushMessagingServiceTest::DidDispatchMessage, base::Unretained(this), |
| 205 base::Unretained(this), &app_id, &dispatched_origin, | 207 &app_id, &dispatched_origin, &service_worker_registration_id, &payload)); |
| 206 &service_worker_registration_id, &message_data)); | |
| 207 | 208 |
| 208 gcm::FakeGCMProfileService* fake_profile_service = | 209 gcm::FakeGCMProfileService* fake_profile_service = |
| 209 static_cast<gcm::FakeGCMProfileService*>( | 210 static_cast<gcm::FakeGCMProfileService*>( |
| 210 gcm::GCMProfileServiceFactory::GetForProfile(profile())); | 211 gcm::GCMProfileServiceFactory::GetForProfile(profile())); |
| 211 | 212 |
| 212 fake_profile_service->DispatchMessage(app_identifier.app_id(), message); | 213 fake_profile_service->DispatchMessage(app_identifier.app_id(), message); |
| 213 | 214 |
| 214 base::RunLoop().RunUntilIdle(); | 215 base::RunLoop().RunUntilIdle(); |
| 215 | 216 |
| 216 // (6) Verify that the message, as received by the Push Messaging Service, has | 217 // (6) Verify that the message, as received by the Push Messaging Service, has |
| 217 // indeed been decrypted by the GCM Driver, and has been forwarded to the | 218 // indeed been decrypted by the GCM Driver, and has been forwarded to the |
| 218 // Service Worker that has been associated with the subscription. | 219 // Service Worker that has been associated with the subscription. |
| 219 EXPECT_EQ(app_identifier.app_id(), app_id); | 220 EXPECT_EQ(app_identifier.app_id(), app_id); |
| 220 EXPECT_EQ(origin, dispatched_origin); | 221 EXPECT_EQ(origin, dispatched_origin); |
| 221 EXPECT_EQ(service_worker_registration_id, kTestServiceWorkerId); | 222 EXPECT_EQ(service_worker_registration_id, kTestServiceWorkerId); |
| 222 | 223 |
| 223 EXPECT_EQ(kTestPayload, message_data); | 224 EXPECT_FALSE(payload.isNull()); |
| 225 EXPECT_EQ(kTestPayload, payload.getData()); |
| 224 } | 226 } |
| OLD | NEW |