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 #include "content/shell/browser/layout_test/layout_test_push_messaging_service.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/macros.h"
10 #include "content/public/browser/permission_type.h"
11 #include "content/shell/browser/layout_test/layout_test_browser_context.h"
12 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h"
13 #include "content/shell/browser/layout_test/layout_test_permission_manager.h"
19 // Curve25519 public key made available to layout tests. Must be 32 bytes.
20 const uint8_t kTestCurve25519dh
[] = {
21 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
22 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
23 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
26 static_assert(sizeof(kTestCurve25519dh
) == 32,
27 "The fake public key must have the size of a real public key.");
29 blink::WebPushPermissionStatus
ToWebPushPermissionStatus(
30 PermissionStatus status
) {
32 case PERMISSION_STATUS_GRANTED
:
33 return blink::WebPushPermissionStatusGranted
;
34 case PERMISSION_STATUS_DENIED
:
35 return blink::WebPushPermissionStatusDenied
;
36 case PERMISSION_STATUS_ASK
:
37 return blink::WebPushPermissionStatusPrompt
;
41 return blink::WebPushPermissionStatusLast
;
44 } // anonymous namespace
46 LayoutTestPushMessagingService::LayoutTestPushMessagingService() {
49 LayoutTestPushMessagingService::~LayoutTestPushMessagingService() {
52 GURL
LayoutTestPushMessagingService::GetPushEndpoint() {
53 return GURL("https://example.com/LayoutTestEndpoint");
56 void LayoutTestPushMessagingService::SubscribeFromDocument(
57 const GURL
& requesting_origin
,
58 int64 service_worker_registration_id
,
59 const std::string
& sender_id
,
63 const PushMessagingService::RegisterCallback
& callback
) {
64 SubscribeFromWorker(requesting_origin
, service_worker_registration_id
,
65 sender_id
, user_visible
, callback
);
68 void LayoutTestPushMessagingService::SubscribeFromWorker(
69 const GURL
& requesting_origin
,
70 int64 service_worker_registration_id
,
71 const std::string
& sender_id
,
73 const PushMessagingService::RegisterCallback
& callback
) {
74 if (GetPermissionStatus(requesting_origin
, requesting_origin
, user_visible
) ==
75 blink::WebPushPermissionStatusGranted
) {
76 std::vector
<uint8_t> curve25519dh(
77 kTestCurve25519dh
, kTestCurve25519dh
+ arraysize(kTestCurve25519dh
));
79 callback
.Run("layoutTestRegistrationId", curve25519dh
,
80 PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE
);
82 callback
.Run("registration_id", std::vector
<uint8_t>(),
83 PUSH_REGISTRATION_STATUS_PERMISSION_DENIED
);
87 void LayoutTestPushMessagingService::GetPublicEncryptionKey(
89 int64_t service_worker_registration_id
,
90 const PublicKeyCallback
& callback
) {
91 std::vector
<uint8_t> curve25519dh(
92 kTestCurve25519dh
, kTestCurve25519dh
+ arraysize(kTestCurve25519dh
));
94 callback
.Run(true /* success */, curve25519dh
);
97 blink::WebPushPermissionStatus
98 LayoutTestPushMessagingService::GetPermissionStatus(
99 const GURL
& requesting_origin
,
100 const GURL
& embedding_origin
,
102 return ToWebPushPermissionStatus(LayoutTestContentBrowserClient::Get()
103 ->GetLayoutTestBrowserContext()
104 ->GetLayoutTestPermissionManager()
105 ->GetPermissionStatus(PermissionType::PUSH_MESSAGING
,
110 bool LayoutTestPushMessagingService::SupportNonVisibleMessages() {
114 void LayoutTestPushMessagingService::Unsubscribe(
115 const GURL
& requesting_origin
,
116 int64 service_worker_registration_id
,
117 const std::string
& sender_id
,
118 const UnregisterCallback
& callback
) {
119 callback
.Run(PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED
);
122 } // namespace content