Commit 02fa99b1 authored by Viviane Yang's avatar Viviane Yang Committed by Commit Bot

[Push] Seperate methods from PushMessagingServiceImpl to util class

This change adds a new push_messaging namespace with general utils
methods needed in chrome/browser/push_messaging.

Change-Id: Iea9b5f54fe01b9a1b3eb027432731b23b4838bc5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2374745Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Commit-Queue: Viviane Yang <viviy@google.com>
Cr-Commit-Position: refs/heads/master@{#801338}
parent bc80b1d4
...@@ -1415,6 +1415,8 @@ static_library("browser") { ...@@ -1415,6 +1415,8 @@ static_library("browser") {
"push_messaging/push_messaging_service_factory.h", "push_messaging/push_messaging_service_factory.h",
"push_messaging/push_messaging_service_impl.cc", "push_messaging/push_messaging_service_impl.cc",
"push_messaging/push_messaging_service_impl.h", "push_messaging/push_messaging_service_impl.h",
"push_messaging/push_messaging_utils.cc",
"push_messaging/push_messaging_utils.h",
"query_tiles/tile_background_task.cc", "query_tiles/tile_background_task.cc",
"query_tiles/tile_background_task.h", "query_tiles/tile_background_task.h",
"query_tiles/tile_service_factory.cc", "query_tiles/tile_service_factory.cc",
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "chrome/browser/push_messaging/push_messaging_constants.h" #include "chrome/browser/push_messaging/push_messaging_constants.h"
#include "chrome/browser/push_messaging/push_messaging_features.h" #include "chrome/browser/push_messaging/push_messaging_features.h"
#include "chrome/browser/push_messaging/push_messaging_service_factory.h" #include "chrome/browser/push_messaging/push_messaging_service_factory.h"
#include "chrome/browser/push_messaging/push_messaging_utils.h"
#include "chrome/browser/ui/chrome_pages.h" #include "chrome/browser/ui/chrome_pages.h"
#include "chrome/common/buildflags.h" #include "chrome/common/buildflags.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
...@@ -181,13 +182,6 @@ content::RenderFrameHost* GetMainFrameForRenderFrameHost( ...@@ -181,13 +182,6 @@ content::RenderFrameHost* GetMainFrameForRenderFrameHost(
return web_contents ? web_contents->GetMainFrame() : nullptr; return web_contents ? web_contents->GetMainFrame() : nullptr;
} }
bool IsVapidKey(const std::string& application_server_key) {
// VAPID keys are NIST P-256 public keys in uncompressed format (64 bytes),
// verified through its length and the 0x04 prefix.
return application_server_key.size() == 65 &&
application_server_key[0] == 0x04;
}
} // namespace } // namespace
// static // static
...@@ -730,7 +724,7 @@ void PushMessagingServiceImpl::DoSubscribe( ...@@ -730,7 +724,7 @@ void PushMessagingServiceImpl::DoSubscribe(
// TODO(peter): Move this check to the renderer process & Mojo message // TODO(peter): Move this check to the renderer process & Mojo message
// validation once the flag is always enabled, and remove the // validation once the flag is always enabled, and remove the
// |render_process_id| and |render_frame_id| parameters from this method. // |render_process_id| and |render_frame_id| parameters from this method.
if (!IsVapidKey(application_server_key_string)) { if (!push_messaging::IsVapidKey(application_server_key_string)) {
content::RenderFrameHost* render_frame_host = content::RenderFrameHost* render_frame_host =
content::RenderFrameHost::FromID(render_process_id, render_frame_id); content::RenderFrameHost::FromID(render_process_id, render_frame_id);
content::RenderFrameHost* main_frame = content::RenderFrameHost* main_frame =
...@@ -769,13 +763,14 @@ void PushMessagingServiceImpl::DoSubscribe( ...@@ -769,13 +763,14 @@ void PushMessagingServiceImpl::DoSubscribe(
GetInstanceIDDriver() GetInstanceIDDriver()
->GetInstanceID(app_identifier.app_id()) ->GetInstanceID(app_identifier.app_id())
->GetToken(NormalizeSenderInfo(application_server_key_string), kGCMScope, ->GetToken(
ttl, std::map<std::string, std::string>() /* options */, push_messaging::NormalizeSenderInfo(application_server_key_string),
{} /* flags */, kGCMScope, ttl, std::map<std::string, std::string>() /* options */,
base::BindOnce(&PushMessagingServiceImpl::DidSubscribe, {} /* flags */,
weak_factory_.GetWeakPtr(), app_identifier, base::BindOnce(&PushMessagingServiceImpl::DidSubscribe,
application_server_key_string, weak_factory_.GetWeakPtr(), app_identifier,
std::move(register_callback))); application_server_key_string,
std::move(register_callback)));
} }
void PushMessagingServiceImpl::SubscribeEnd( void PushMessagingServiceImpl::SubscribeEnd(
...@@ -813,7 +808,7 @@ void PushMessagingServiceImpl::DidSubscribe( ...@@ -813,7 +808,7 @@ void PushMessagingServiceImpl::DidSubscribe(
switch (result) { switch (result) {
case InstanceID::SUCCESS: { case InstanceID::SUCCESS: {
const GURL endpoint = CreateEndpoint(subscription_id); const GURL endpoint = push_messaging::CreateEndpoint(subscription_id);
// Make sure that this subscription has associated encryption keys prior // Make sure that this subscription has associated encryption keys prior
// to returning it to the developer - they'll need this information in // to returning it to the developer - they'll need this information in
...@@ -888,7 +883,7 @@ void PushMessagingServiceImpl::GetSubscriptionInfo( ...@@ -888,7 +883,7 @@ void PushMessagingServiceImpl::GetSubscriptionInfo(
return; return;
} }
const GURL endpoint = CreateEndpoint(subscription_id); const GURL endpoint = push_messaging::CreateEndpoint(subscription_id);
const std::string& app_id = app_identifier.app_id(); const std::string& app_id = app_identifier.app_id();
base::Optional<base::Time> expiration_time = app_identifier.expiration_time(); base::Optional<base::Time> expiration_time = app_identifier.expiration_time();
...@@ -899,12 +894,12 @@ void PushMessagingServiceImpl::GetSubscriptionInfo( ...@@ -899,12 +894,12 @@ void PushMessagingServiceImpl::GetSubscriptionInfo(
if (PushMessagingAppIdentifier::UseInstanceID(app_id)) { if (PushMessagingAppIdentifier::UseInstanceID(app_id)) {
GetInstanceIDDriver()->GetInstanceID(app_id)->ValidateToken( GetInstanceIDDriver()->GetInstanceID(app_id)->ValidateToken(
NormalizeSenderInfo(sender_id), kGCMScope, subscription_id, push_messaging::NormalizeSenderInfo(sender_id), kGCMScope,
std::move(validate_cb)); subscription_id, std::move(validate_cb));
} else { } else {
GetGCMDriver()->ValidateRegistration( GetGCMDriver()->ValidateRegistration(
app_id, {NormalizeSenderInfo(sender_id)}, subscription_id, app_id, {push_messaging::NormalizeSenderInfo(sender_id)},
std::move(validate_cb)); subscription_id, std::move(validate_cb));
} }
} }
...@@ -1041,9 +1036,9 @@ void PushMessagingServiceImpl::DidClearPushSubscriptionId( ...@@ -1041,9 +1036,9 @@ void PushMessagingServiceImpl::DidClearPushSubscriptionId(
if (sender_id.empty()) { if (sender_id.empty()) {
std::move(unregister_callback).Run(gcm::GCMClient::INVALID_PARAMETER); std::move(unregister_callback).Run(gcm::GCMClient::INVALID_PARAMETER);
} else { } else {
GetGCMDriver()->UnregisterWithSenderId(app_id, GetGCMDriver()->UnregisterWithSenderId(
NormalizeSenderInfo(sender_id), app_id, push_messaging::NormalizeSenderInfo(sender_id),
std::move(unregister_callback)); std::move(unregister_callback));
} }
#else #else
GetGCMDriver()->Unregister(app_id, std::move(unregister_callback)); GetGCMDriver()->Unregister(app_id, std::move(unregister_callback));
...@@ -1263,15 +1258,10 @@ void PushMessagingServiceImpl::GetPushSubscriptionFromAppIdentifierEnd( ...@@ -1263,15 +1258,10 @@ void PushMessagingServiceImpl::GetPushSubscriptionFromAppIdentifierEnd(
std::move(callback).Run(nullptr /* subscription */); std::move(callback).Run(nullptr /* subscription */);
return; return;
} }
// Currently |user_visible_only| is always true, once silent pushes are
// enabled, get this information from SW database.
auto options = blink::mojom::PushSubscriptionOptions::New();
options->user_visible_only = true;
options->application_server_key =
std::vector<uint8_t>(sender_id.begin(), sender_id.end());
std::move(callback).Run(blink::mojom::PushSubscription::New( std::move(callback).Run(blink::mojom::PushSubscription::New(
endpoint, expiration_time, std::move(options), p256dh, auth)); endpoint, expiration_time, push_messaging::MakeOptions(sender_id), p256dh,
auth));
} }
void PushMessagingServiceImpl::FirePushSubscriptionChange( void PushMessagingServiceImpl::FirePushSubscriptionChange(
...@@ -1353,19 +1343,6 @@ void PushMessagingServiceImpl::SetRemoveExpiredSubscriptionsCallbackForTesting( ...@@ -1353,19 +1343,6 @@ void PushMessagingServiceImpl::SetRemoveExpiredSubscriptionsCallbackForTesting(
remove_expired_subscriptions_callback_for_testing_ = std::move(closure); remove_expired_subscriptions_callback_for_testing_ = std::move(closure);
} }
std::string PushMessagingServiceImpl::NormalizeSenderInfo(
const std::string& application_server_key) const {
if (!IsVapidKey(application_server_key))
return application_server_key;
std::string encoded_application_server_key;
base::Base64UrlEncode(application_server_key,
base::Base64UrlEncodePolicy::OMIT_PADDING,
&encoded_application_server_key);
return encoded_application_server_key;
}
// Assumes user_visible always since this is just meant to check // Assumes user_visible always since this is just meant to check
// if the permission was previously granted and not revoked. // if the permission was previously granted and not revoked.
bool PushMessagingServiceImpl::IsPermissionSet(const GURL& origin) { bool PushMessagingServiceImpl::IsPermissionSet(const GURL& origin) {
...@@ -1379,19 +1356,12 @@ void PushMessagingServiceImpl::GetEncryptionInfoForAppId( ...@@ -1379,19 +1356,12 @@ void PushMessagingServiceImpl::GetEncryptionInfoForAppId(
gcm::GCMEncryptionProvider::EncryptionInfoCallback callback) { gcm::GCMEncryptionProvider::EncryptionInfoCallback callback) {
if (PushMessagingAppIdentifier::UseInstanceID(app_id)) { if (PushMessagingAppIdentifier::UseInstanceID(app_id)) {
GetInstanceIDDriver()->GetInstanceID(app_id)->GetEncryptionInfo( GetInstanceIDDriver()->GetInstanceID(app_id)->GetEncryptionInfo(
NormalizeSenderInfo(sender_id), std::move(callback)); push_messaging::NormalizeSenderInfo(sender_id), std::move(callback));
} else { } else {
GetGCMDriver()->GetEncryptionInfo(app_id, std::move(callback)); GetGCMDriver()->GetEncryptionInfo(app_id, std::move(callback));
} }
} }
GURL PushMessagingServiceImpl::CreateEndpoint(
const std::string& subscription_id) const {
const GURL endpoint(kPushMessagingGcmEndpoint + subscription_id);
DCHECK(endpoint.is_valid());
return endpoint;
}
gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const { gcm::GCMDriver* PushMessagingServiceImpl::GetGCMDriver() const {
gcm::GCMProfileService* gcm_profile_service = gcm::GCMProfileService* gcm_profile_service =
gcm::GCMProfileServiceFactory::GetForProfile(profile_); gcm::GCMProfileServiceFactory::GetForProfile(profile_);
......
...@@ -299,11 +299,6 @@ class PushMessagingServiceImpl : public content::PushMessagingService, ...@@ -299,11 +299,6 @@ class PushMessagingServiceImpl : public content::PushMessagingService,
const PushMessagingAppIdentifier& app_identifier, const PushMessagingAppIdentifier& app_identifier,
blink::mojom::PushEventStatus status); blink::mojom::PushEventStatus status);
// Normalizes the |sender_info|. In most cases the |sender_info| will be
// passed through to the GCM Driver as-is, but NIST P-256 application server
// keys have to be encoded using the URL-safe variant of the base64 encoding.
std::string NormalizeSenderInfo(const std::string& sender_info) const;
// Checks if a given origin is allowed to use Push. // Checks if a given origin is allowed to use Push.
bool IsPermissionSet(const GURL& origin); bool IsPermissionSet(const GURL& origin);
...@@ -313,10 +308,6 @@ class PushMessagingServiceImpl : public content::PushMessagingService, ...@@ -313,10 +308,6 @@ class PushMessagingServiceImpl : public content::PushMessagingService,
const std::string& sender_id, const std::string& sender_id,
gcm::GCMEncryptionProvider::EncryptionInfoCallback callback); gcm::GCMEncryptionProvider::EncryptionInfoCallback callback);
// Returns the URL used to send push messages to the subscription identified
// by |subscription_id|.
GURL CreateEndpoint(const std::string& subscription_id) const;
gcm::GCMDriver* GetGCMDriver() const; gcm::GCMDriver* GetGCMDriver() const;
instance_id::InstanceIDDriver* GetInstanceIDDriver() const; instance_id::InstanceIDDriver* GetInstanceIDDriver() const;
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "chrome/browser/push_messaging/push_messaging_features.h" #include "chrome/browser/push_messaging/push_messaging_features.h"
#include "chrome/browser/push_messaging/push_messaging_service_factory.h" #include "chrome/browser/push_messaging/push_messaging_service_factory.h"
#include "chrome/browser/push_messaging/push_messaging_service_impl.h" #include "chrome/browser/push_messaging/push_messaging_service_impl.h"
#include "chrome/browser/push_messaging/push_messaging_utils.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h" #include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h" #include "components/gcm_driver/crypto/gcm_crypto_test_helpers.h"
...@@ -301,15 +302,15 @@ TEST_F(PushMessagingServiceTest, NormalizeSenderInfo) { ...@@ -301,15 +302,15 @@ TEST_F(PushMessagingServiceTest, NormalizeSenderInfo) {
// NIST P-256 public keys in uncompressed format will be encoded using the // NIST P-256 public keys in uncompressed format will be encoded using the
// URL-safe base64 encoding by the normalization function. // URL-safe base64 encoding by the normalization function.
EXPECT_EQ(kTestEncodedP256Key, push_service->NormalizeSenderInfo(p256dh)); EXPECT_EQ(kTestEncodedP256Key, push_messaging::NormalizeSenderInfo(p256dh));
// Any other value, binary or not, will be passed through as-is. // Any other value, binary or not, will be passed through as-is.
EXPECT_EQ("1234567890", push_service->NormalizeSenderInfo("1234567890")); EXPECT_EQ("1234567890", push_messaging::NormalizeSenderInfo("1234567890"));
EXPECT_EQ("foo@bar.com", push_service->NormalizeSenderInfo("foo@bar.com")); EXPECT_EQ("foo@bar.com", push_messaging::NormalizeSenderInfo("foo@bar.com"));
p256dh[0] = 0x05; // invalidate |p256dh| as a public key. p256dh[0] = 0x05; // invalidate |p256dh| as a public key.
EXPECT_EQ(p256dh, push_service->NormalizeSenderInfo(p256dh)); EXPECT_EQ(p256dh, push_messaging::NormalizeSenderInfo(p256dh));
} }
TEST_F(PushMessagingServiceTest, RemoveExpiredSubscriptions) { TEST_F(PushMessagingServiceTest, RemoveExpiredSubscriptions) {
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/push_messaging/push_messaging_utils.h"
#include "base/base64url.h"
#include "chrome/browser/push_messaging/push_messaging_constants.h"
#include "url/gurl.h"
namespace push_messaging {
GURL CreateEndpoint(const std::string& subscription_id) {
const GURL endpoint(kPushMessagingGcmEndpoint + subscription_id);
DCHECK(endpoint.is_valid());
return endpoint;
}
blink::mojom::PushSubscriptionOptionsPtr MakeOptions(
const std::string& sender_id) {
return blink::mojom::PushSubscriptionOptions::New(
/*user_visible_only=*/true,
std::vector<uint8_t>(sender_id.begin(), sender_id.end()));
}
bool IsVapidKey(const std::string& application_server_key) {
// VAPID keys are NIST P-256 public keys in uncompressed format (64 bytes),
// verified through its length and the 0x04 prefix.
return application_server_key.size() == 65 &&
application_server_key[0] == 0x04;
}
std::string NormalizeSenderInfo(const std::string& application_server_key) {
if (!IsVapidKey(application_server_key))
return application_server_key;
std::string encoded_application_server_key;
base::Base64UrlEncode(application_server_key,
base::Base64UrlEncodePolicy::OMIT_PADDING,
&encoded_application_server_key);
return encoded_application_server_key;
}
} // namespace push_messaging
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_UTILS_H_
#define CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_UTILS_H_
#include <string>
#include "third_party/blink/public/mojom/push_messaging/push_messaging.mojom.h"
class GURL;
namespace push_messaging {
// Returns the URL used to send push messages to the subscription identified
// by |subscription_id|.
GURL CreateEndpoint(const std::string& subscription_id);
// Checks size and prefix to determine whether it is a VAPID key
bool IsVapidKey(const std::string& application_server_key);
// Normalizes the |sender_info|. In most cases the |sender_info| will be
// passed through to the GCM Driver as-is, but NIST P-256 application server
// keys have to be encoded using the URL-safe variant of the base64 encoding.
std::string NormalizeSenderInfo(const std::string& sender_info);
// Currently |user_visible_only| is always true, once silent pushes are
// enabled, get this information from SW database.
blink::mojom::PushSubscriptionOptionsPtr MakeOptions(
const std::string& sender_id);
} // namespace push_messaging
#endif // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_UTILS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment