Commit 201f7aa0 authored by Azeem Arshad's avatar Azeem Arshad Committed by Commit Bot

[MultideviceSetup] PreGrant Notification permission for Android Messages

This CL adds logic to automatically grant notification permissions for
Android Messages for Web immeadiately after the PWA installation.

Bug: 875511
Change-Id: I3ff64ad1439ecaf1ea57e11609a8bcf2cf4b34bf
Reviewed-on: https://chromium-review.googlesource.com/1252903Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarJeremy Klein <jlklein@chromium.org>
Commit-Queue: Azeem Arshad <azeemarshad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595513}
parent 8cb05f56
......@@ -9,6 +9,7 @@
#include "base/bind.h"
#include "base/callback.h"
#include "chrome/browser/chromeos/android_sms/android_sms_urls.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/extensions/app_launch_params.h"
......@@ -17,6 +18,8 @@
#include "chrome/browser/web_applications/components/pending_app_manager.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chromeos/components/proximity_auth/logging/logging.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "extensions/common/constants.h"
#include "ui/base/window_open_disposition.h"
......@@ -29,11 +32,16 @@ AndroidSmsAppHelperDelegateImpl::AndroidSmsAppHelperDelegateImpl(
: pending_app_manager_(
&web_app::WebAppProvider::Get(profile)->pending_app_manager()),
profile_(profile),
host_content_settings_map_(
HostContentSettingsMapFactory::GetForProfile(profile)),
weak_ptr_factory_(this) {}
AndroidSmsAppHelperDelegateImpl::AndroidSmsAppHelperDelegateImpl(
web_app::PendingAppManager* pending_app_manager)
: pending_app_manager_(pending_app_manager), weak_ptr_factory_(this) {}
web_app::PendingAppManager* pending_app_manager,
HostContentSettingsMap* host_content_settings_map)
: pending_app_manager_(pending_app_manager),
host_content_settings_map_(host_content_settings_map),
weak_ptr_factory_(this) {}
AndroidSmsAppHelperDelegateImpl::~AndroidSmsAppHelperDelegateImpl() = default;
......@@ -88,6 +96,14 @@ void AndroidSmsAppHelperDelegateImpl::OnAppInstalled(
const GURL& app_url,
web_app::InstallResultCode code) {
if (code == web_app::InstallResultCode::kSuccess) {
// Pre-Grant notification permission for Messages.
host_content_settings_map_->SetWebsiteSettingDefaultScope(
chromeos::android_sms::GetAndroidMessagesURL(),
GURL() /* top_level_url */,
ContentSettingsType::CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
content_settings::ResourceIdentifier(),
std::make_unique<base::Value>(ContentSetting::CONTENT_SETTING_ALLOW));
PA_LOG(INFO) << "Messages app installed! URL: " << app_url;
if (launch_on_install)
LaunchAndroidSmsApp();
......
......@@ -10,6 +10,7 @@
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/services/multidevice_setup/public/cpp/android_sms_app_helper_delegate.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "url/gurl.h"
class Profile;
......@@ -34,8 +35,9 @@ class AndroidSmsAppHelperDelegateImpl : public AndroidSmsAppHelperDelegate {
// built using this constructor will segfault on profile_ if
// LaunchAndroidSmsApp is called. We'll need to fix this once tests for that
// function are added. See https://crbug.com/876972.
explicit AndroidSmsAppHelperDelegateImpl(
web_app::PendingAppManager* pending_app_manager);
AndroidSmsAppHelperDelegateImpl(
web_app::PendingAppManager* pending_app_manager,
HostContentSettingsMap* host_content_settings_map);
void OnAppInstalled(bool launch_on_install,
const GURL& app_url,
web_app::InstallResultCode code);
......@@ -49,6 +51,7 @@ class AndroidSmsAppHelperDelegateImpl : public AndroidSmsAppHelperDelegate {
static const char kMessagesWebAppUrl[];
web_app::PendingAppManager* pending_app_manager_;
Profile* profile_;
HostContentSettingsMap* host_content_settings_map_;
base::WeakPtrFactory<AndroidSmsAppHelperDelegateImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(AndroidSmsAppHelperDelegateImpl);
......
......@@ -11,7 +11,11 @@
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/chromeos/android_sms/android_sms_urls.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/web_applications/components/test_pending_app_manager.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
......@@ -20,15 +24,21 @@ namespace multidevice_setup {
class AndroidSmsAppHelperDelegateImplTest : public testing::Test {
protected:
AndroidSmsAppHelperDelegateImplTest() = default;
AndroidSmsAppHelperDelegateImplTest()
: host_content_settings_map_(
HostContentSettingsMapFactory::GetForProfile(&profile_)) {}
~AndroidSmsAppHelperDelegateImplTest() override = default;
// testing::Test:
void SetUp() override {
host_content_settings_map_->ClearSettingsForOneType(
ContentSettingsType::CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
test_pending_app_manager_ =
std::make_unique<web_app::TestPendingAppManager>();
android_sms_app_helper_delegate_ = base::WrapUnique(
new AndroidSmsAppHelperDelegateImpl(test_pending_app_manager_.get()));
android_sms_app_helper_delegate_ =
base::WrapUnique(new AndroidSmsAppHelperDelegateImpl(
test_pending_app_manager_.get(), host_content_settings_map_));
}
web_app::TestPendingAppManager* test_pending_app_manager() {
......@@ -43,7 +53,20 @@ class AndroidSmsAppHelperDelegateImplTest : public testing::Test {
android_sms_app_helper_delegate_->InstallAndLaunchAndroidSmsApp();
}
ContentSetting GetNotificationSetting() {
std::unique_ptr<base::Value> notification_settings_value =
host_content_settings_map_->GetWebsiteSetting(
chromeos::android_sms::GetAndroidMessagesURL(),
GURL() /* top_level_url */,
ContentSettingsType::CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
content_settings::ResourceIdentifier(), nullptr);
return static_cast<ContentSetting>(notification_settings_value->GetInt());
}
private:
content::TestBrowserThreadBundle thread_bundle_;
TestingProfile profile_;
HostContentSettingsMap* host_content_settings_map_;
std::unique_ptr<web_app::TestPendingAppManager> test_pending_app_manager_;
std::unique_ptr<AndroidSmsAppHelperDelegate> android_sms_app_helper_delegate_;
......@@ -51,6 +74,7 @@ class AndroidSmsAppHelperDelegateImplTest : public testing::Test {
};
TEST_F(AndroidSmsAppHelperDelegateImplTest, TestInstallMessagesApp) {
EXPECT_NE(ContentSetting::CONTENT_SETTING_ALLOW, GetNotificationSetting());
InstallApp();
std::vector<web_app::PendingAppManager::AppInfo> expected_apps_to_install;
......@@ -62,6 +86,7 @@ TEST_F(AndroidSmsAppHelperDelegateImplTest, TestInstallMessagesApp) {
true); // override_previous_user_uninstall
EXPECT_EQ(expected_apps_to_install,
test_pending_app_manager()->install_requests());
EXPECT_EQ(ContentSetting::CONTENT_SETTING_ALLOW, GetNotificationSetting());
}
TEST_F(AndroidSmsAppHelperDelegateImplTest, TestInstallAndLaunchMessagesApp) {
......
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