Commit eb7e9b53 authored by Ramin Halavati's avatar Ramin Halavati Committed by Commit Bot

Add OTRProfileID::AllowsBrowserWindows.

Non-Primary OTR profiles are not supposed to be used to create browser
windows. "DevTools::BrowserContext" are an exception to this ban and
|AllowsBrowserWindows| is added to cover them.

Bug: 1033903
Change-Id: Ib7ee274887943106f36ebaf915d48a7843bf73d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2250181Reviewed-by: default avatarYang Guo <yangguo@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Commit-Queue: Ramin Halavati <rhalavati@chromium.org>
Cr-Commit-Position: refs/heads/master@{#781816}
parent 32253f74
......@@ -35,7 +35,7 @@ content::BrowserContext* DevToolsBrowserContextManager::CreateBrowserContext() {
ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
Profile* otr_profile = original_profile->GetOffTheRecordProfile(
Profile::OTRProfileID::CreateUnique("Devtools::BrowserContext"));
Profile::OTRProfileID::CreateUniqueForDevTools());
const std::string& context_id = otr_profile->UniqueId();
otr_profiles_[context_id] = otr_profile;
otr_profile->AddObserver(this);
......
......@@ -346,7 +346,8 @@ bool OffTheRecordProfileImpl::IsLegacySupervised() const {
}
bool OffTheRecordProfileImpl::AllowsBrowserWindows() const {
return profile_->AllowsBrowserWindows();
return profile_->AllowsBrowserWindows() &&
otr_profile_id_.AllowsBrowserWindows();
}
PrefService* OffTheRecordProfileImpl::GetPrefs() {
......
......@@ -7,6 +7,7 @@
#include <string>
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
#include "chrome/browser/chrome_notification_types.h"
......@@ -107,11 +108,20 @@ class ChromeVariationsClient : public variations::VariationsClient {
content::BrowserContext* browser_context_;
};
const char kDevToolsOTRProfileIDPrefix[] = "Devtools::BrowserContext";
} // namespace
Profile::OTRProfileID::OTRProfileID(const std::string& profile_id)
: profile_id_(profile_id) {}
bool Profile::OTRProfileID::AllowsBrowserWindows() const {
// Non-Primary OTR profiles are not supposed to create Browser windows.
// DevTools::BrowserContext is an exception to this ban.
return *this == PrimaryID() ||
base::StartsWith(profile_id_, kDevToolsOTRProfileIDPrefix,
base::CompareCase::SENSITIVE);
}
// static
const Profile::OTRProfileID Profile::OTRProfileID::PrimaryID() {
return OTRProfileID("profile::primary_otr");
......@@ -127,6 +137,11 @@ Profile::OTRProfileID Profile::OTRProfileID::CreateUnique(
first_unused_index_++));
}
// static
Profile::OTRProfileID Profile::OTRProfileID::CreateUniqueForDevTools() {
return CreateUnique(kDevToolsOTRProfileIDPrefix);
}
const std::string& Profile::OTRProfileID::ToString() const {
return profile_id_;
}
......
......@@ -116,6 +116,9 @@ class Profile : public content::BrowserContext {
// Creates a unique OTR profile id with the given profile id prefix.
static OTRProfileID CreateUnique(const std::string& profile_id_prefix);
// Creates a unique OTR profile id to be used for DevTools browser contexts.
static OTRProfileID CreateUniqueForDevTools();
bool operator==(const OTRProfileID& other) const {
return profile_id_ == other.profile_id_;
}
......@@ -128,6 +131,8 @@ class Profile : public content::BrowserContext {
return profile_id_ < other.profile_id_;
}
bool AllowsBrowserWindows() const;
#if defined(OS_ANDROID)
// Constructs a Java OTRProfileID from the provided C++ OTRProfileID
base::android::ScopedJavaLocalRef<jobject> ConvertToJavaOTRProfileID(
......
......@@ -837,3 +837,16 @@ IN_PROC_BROWSER_TEST_F(ProfileBrowserTest, TestIsSameOrParent) {
EXPECT_FALSE(incognito_profile->IsSameOrParent(otr_profile));
EXPECT_FALSE(otr_profile->IsSameOrParent(incognito_profile));
}
// Tests if browser creation using non primary OTRs is blocked.
IN_PROC_BROWSER_TEST_F(ProfileBrowserTest,
TestCreatingBrowserUsingNonPrimaryOffTheRecordProfile) {
Profile::OTRProfileID otr_profile_id("profile::otr");
Profile* otr_profile =
browser()->profile()->GetOffTheRecordProfile(otr_profile_id);
EXPECT_EQ(nullptr, Browser::Create(Browser::CreateParams(
otr_profile, /* user_gesture = */ true)));
EXPECT_EQ(nullptr, Browser::Create(Browser::CreateParams(
otr_profile, /* user_gesture = */ false)));
}
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