Commit 12c77448 authored by Archana Simha's avatar Archana Simha Committed by Commit Bot

Alarm will be triggered in the same browser context it was created in.

Previously if the extension was in split mode both the incognito and
the non incognito processes would receive the onAlarm event even if
the alarm was created in a different browser context.

Now alarms.onAlarm is restricted to the browser context the alarm
was created.

Bug: 1011374
Change-Id: I1c5ed9273da1ec7d4e8b211b04591c8ae839ad1a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2067542Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: Archana Simha <archanasimha@chromium.org>
Cr-Commit-Position: refs/heads/master@{#748480}
parent 3d660fd2
// 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/extensions/extension_apitest.h"
#include "extensions/browser/event_router.h"
#include "extensions/common/api/test.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
using extensions::ResultCatcher;
namespace extensions {
class AlarmsApiTest : public ExtensionApiTest {
public:
void SetUpOnMainThread() override {
ExtensionApiTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(StartEmbeddedTestServer());
}
static std::unique_ptr<base::ListValue> BuildEventArguments(
const bool last_message) {
api::test::OnMessage::Info info;
info.data = "";
info.last_message = last_message;
return api::test::OnMessage::Create(info);
}
};
// Tests that an alarm created by an extension with incognito split mode is
// only triggered in the browser context it was created in.
IN_PROC_BROWSER_TEST_F(AlarmsApiTest, IncognitoSplit) {
// We need 2 ResultCatchers because we'll be running the same test in both
// regular and incognito mode.
Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
ResultCatcher catcher_incognito;
catcher_incognito.RestrictToBrowserContext(incognito_profile);
ResultCatcher catcher;
catcher.RestrictToBrowserContext(browser()->profile());
EventRouter* event_router = EventRouter::Get(incognito_profile);
ExtensionTestMessageListener listener("ready: false", false);
ExtensionTestMessageListener listener_incognito("ready: true", false);
ASSERT_TRUE(LoadExtensionIncognito(
test_data_dir_.AppendASCII("alarms").AppendASCII("split")));
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
// Open incognito window.
OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
event_router->BroadcastEvent(std::make_unique<Event>(
events::FOR_TEST, api::test::OnMessage::kEventName,
BuildEventArguments(true)));
EXPECT_TRUE(catcher.GetNextResult());
EXPECT_TRUE(catcher_incognito.GetNextResult());
}
// Tests that the behavior for an alarm created in incognito context should be
// the same if incognito is in spanning mode.
IN_PROC_BROWSER_TEST_F(AlarmsApiTest, IncognitoSpanning) {
ResultCatcher catcher;
catcher.RestrictToBrowserContext(browser()->profile());
ASSERT_TRUE(LoadExtensionIncognito(
test_data_dir_.AppendASCII("alarms").AppendASCII("spanning")));
// Open incognito window.
OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
} // namespace extensions
......@@ -1633,6 +1633,7 @@ if (!is_android) {
"../browser/extensions/alert_apitest.cc",
"../browser/extensions/all_urls_apitest.cc",
"../browser/extensions/api/activity_log_private/activity_log_private_apitest.cc",
"../browser/extensions/api/alarms/alarms_apitest.cc",
"../browser/extensions/api/autofill_private/autofill_private_apitest.cc",
"../browser/extensions/api/automation/automation_apitest.cc",
"../browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_apitest_chromeos.cc",
......
// 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.
let inIncognito = chrome.extension.inIncognitoContext;
chrome.alarms.onAlarm.addListener(function(alarm) {
chrome.test.assertEq(inIncognito ? 'incognito' : 'normal', alarm.name);
chrome.test.succeed();
});
chrome.test.runTests([
// Creates an alarm with the name of the context it was created in.
function createAlarms() {
const alarmName = inIncognito ? 'incognito' : 'normal';
chrome.alarms.create(alarmName, {delayInMinutes: 0.001});
}
]);
{
"permissions": ["alarms"],
"background": {
"scripts": ["background.js"]
},
"description": "An extension which triggers an alarm to ensure that when incognito is in split mode onAlarm is only called in the same context the alarm was created in.",
"manifest_version": 2,
"incognito": "spanning",
"name": "Alarm Incognito Split Test Extension",
"version": "0.1.1"
}
// 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.
let inIncognito = chrome.extension.inIncognitoContext;
let alarmTriggered = false;
let testEventFired = false;
function checkAndCompleteTest() {
if (alarmTriggered && testEventFired)
chrome.test.succeed();
}
chrome.alarms.onAlarm.addListener(function(alarm) {
chrome.test.assertEq(inIncognito ? 'incognito' : 'normal', alarm.name);
alarmTriggered = true;
checkAndCompleteTest();
});
chrome.test.onMessage.addListener(function() {
testEventFired = true;
checkAndCompleteTest();
});
chrome.test.runTests([
// Creates an alarm with the name of the context it was created in.
function createAlarms() {
const alarmName = inIncognito ? 'incognito' : 'normal';
chrome.alarms.create(alarmName, {delayInMinutes: 0.001});
}
]);
// Send a message to C++ to let it know that JS has registered event
// listeners for onAlarm and onMessage.
chrome.test.sendMessage('ready: ' + inIncognito);
{
"permissions": ["alarms"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"description": "An extension which triggers an alarm to ensure that when incognito is in split mode onAlarm is only called in the same context the alarm was created in.",
"manifest_version": 2,
"incognito": "split",
"name": "Alarm Incognito Split Test Extension",
"version": "0.1.1"
}
......@@ -50,8 +50,9 @@ class DefaultAlarmDelegate : public AlarmManager::Delegate {
void OnAlarm(const std::string& extension_id, const Alarm& alarm) override {
std::unique_ptr<base::ListValue> args(new base::ListValue());
args->Append(alarm.js_alarm->ToValue());
std::unique_ptr<Event> event(new Event(
events::ALARMS_ON_ALARM, alarms::OnAlarm::kEventName, std::move(args)));
std::unique_ptr<Event> event(new Event(events::ALARMS_ON_ALARM,
alarms::OnAlarm::kEventName,
std::move(args), browser_context_));
EventRouter::Get(browser_context_)
->DispatchEventToExtension(extension_id, std::move(event));
}
......
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