Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Informs to users about system location service #19529

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,28 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_CRASH_REPORT_PERMISSION_ASK_DIALOG_FOOTNOTE_TEXT_SETTING_PART" desc="This is used for replacing SETTING_TEXT from above FOOTNOTE_TEXT.">
this setting
</message>
<!-- Geolocation accuracy helper dialog -->
<message name="IDS_GEOLOCATION_ACCURACY_HELPER_DLG_HEADER_LABEL" desc="The text for header part in the dialog">
Geolocation with Brave
</message>
<message name="IDS_GEOLOCATION_ACCURACY_HELPER_DLG_CONTENTS_LABEL" desc="The text for content part in the dialog">
The Brave browser is attempting to access your location. You may not get accurate results unless you enable your operating system's <ph name="PART_ONE">$1<ex>Location services</ex></ph> setting and also enable <ph name="PART_TWO">$2<ex>Let apps access your location</ex></ph>.
</message>
<message name="IDS_GEOLOCATION_ACCURACY_HELPER_DLG_CONTENTS_PART_ONE_LABEL" desc="The text for content part one in the dialog">
Location services
</message>
<message name="IDS_GEOLOCATION_ACCURACY_HELPER_DLG_CONTENTS_PART_TWO_LABEL" desc="The text for content part two in the dialog">
Let apps access your location
</message>
Comment on lines +551 to +559
Copy link
Collaborator

@mkarolin mkarolin Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this is not great for localization. Not sure if this is doable, but would it be possible to use two placeholders for each fragment just to get offsets? For example:

<message name="IDS_GEOLOCATION_ACCURACY_HELPER_DLG_CONTENTS_LABEL"
    desc="The text for content part in the dialog">
The Brave browser is attempting to access your location. You may not get accurate results
 unless you enable your operating system's 
<ph name="STYLE_ONE_BEGIN">$1</ph>Location services<ph name="STYLE_ONE_END">$2</ph> 
setting and also enable 
<ph name="STYLE_TWO_BEING">$3</ph>Let apps access your location<ph name="STYLE_TWO_END">$4</ph>.


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Extra line breaks for visibility in GitHub

Then use l10n_util::GetStringFUTF16 to get all the offsets and apply styling to ranges in between _BEGIN and _END offsets. I guess we can replace placeholders themselves with empty strings, if necessary. WDYT?

<message name="IDS_GEOLOCATION_ACCURACY_HELPER_DLG_CONTENTS_SECOND_LABEL" desc="The text for content part in the dialog">
With those enabled, your location results should be much more accurate. <ph name="LEARN_MORE">$1<ex>Learn more</ex></ph>
</message>
<message name="IDS_GEOLOCATION_ACCURACY_HELPER_DLG_LEARN_MORE_LABEL_PART" desc="The text for learn more link in the dialog">
Learn more
</message>
<message name="IDS_GEOLOCATION_ACCURACY_HELPER_DLG_DONT_SHOW_AGAIN_LABEL" desc="The text for don't show again checkbox in the dialog">
Don't show this again
</message>
<!-- Closing confirm dialog -->
<message name="IDS_WINDOW_CLOSING_CONFIRM_DLG_OK_BUTTON_LABEL" desc="The text for ok button in the dialog">
Close all
Expand Down
2 changes: 2 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@

#if !BUILDFLAG(IS_ANDROID)
#include "brave/browser/search_engines/search_engine_provider_util.h"
#include "brave/browser/ui/geolocation/pref_names.h"
#include "brave/browser/ui/tabs/brave_tab_prefs.h"
#include "brave/components/brave_private_new_tab_ui/common/pref_names.h"
#endif
Expand Down Expand Up @@ -439,6 +440,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
base::Value(true));
registry->RegisterBooleanPref(kEnableWindowClosingConfirm, true);
registry->RegisterBooleanPref(kEnableClosingLastTab, true);
registry->RegisterBooleanPref(kShowGeolocationAccuracyHelperDialog, true);

brave_tabs::RegisterBraveProfilePrefs(registry);
#endif
Expand Down
2 changes: 2 additions & 0 deletions browser/brave_tab_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#if !BUILDFLAG(IS_ANDROID)
#include "brave/browser/ui/brave_shields_data_controller.h"
#include "brave/browser/ui/geolocation/geolocation_accuracy_tab_helper.h"
#include "chrome/browser/ui/thumbnails/thumbnail_tab_helper.h"
#endif

Expand Down Expand Up @@ -106,6 +107,7 @@ void AttachTabHelpers(content::WebContents* web_contents) {
BraveBookmarkTabHelper::CreateForWebContents(web_contents);
brave_shields::BraveShieldsDataController::CreateForWebContents(web_contents);
ThumbnailTabHelper::CreateForWebContents(web_contents);
GeolocationAccuracyTabHelper::MaybeCreateForWebContents(web_contents);
#endif

brave_rewards::RewardsTabHelper::CreateForWebContents(web_contents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
#include "content/public/browser/web_contents.h"
#include "url/gurl.h"

#if !BUILDFLAG(IS_ANDROID)
#include "brave/browser/ui/geolocation/geolocation_accuracy_tab_helper.h"
#endif

BraveGeolocationPermissionContextDelegate::
BraveGeolocationPermissionContextDelegate(
content::BrowserContext* browser_context)
: GeolocationPermissionContextDelegate(browser_context),
profile_(Profile::FromBrowserContext(browser_context)) {}
profile_(*Profile::FromBrowserContext(browser_context)) {}

BraveGeolocationPermissionContextDelegate::
~BraveGeolocationPermissionContextDelegate() = default;
Expand All @@ -33,6 +37,23 @@ bool BraveGeolocationPermissionContextDelegate::DecidePermission(
return true;
}

return GeolocationPermissionContextDelegate::DecidePermission(
id, requesting_origin, user_gesture, callback, context);
if (GeolocationPermissionContextDelegate::DecidePermission(
id, requesting_origin, user_gesture, callback, context)) {
return true;
}

#if !BUILDFLAG(IS_ANDROID)
content::RenderFrameHost* rfh =
content::RenderFrameHost::FromID(id.global_render_frame_host_id());
DCHECK(rfh);

content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(rfh);
if (GeolocationAccuracyTabHelper* accuracy_tab_helper =
GeolocationAccuracyTabHelper::FromWebContents(web_contents)) {
accuracy_tab_helper->LaunchAccuracyHelperDialogIfNeeded();
}
#endif

return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef BRAVE_BROWSER_GEOLOCATION_BRAVE_GEOLOCATION_PERMISSION_CONTEXT_DELEGATE_H_
#define BRAVE_BROWSER_GEOLOCATION_BRAVE_GEOLOCATION_PERMISSION_CONTEXT_DELEGATE_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "chrome/browser/geolocation/geolocation_permission_context_delegate.h"

class Profile;
Expand All @@ -30,7 +30,7 @@ class BraveGeolocationPermissionContextDelegate
permissions::GeolocationPermissionContext* context) override;

private:
raw_ptr<Profile> profile_ = nullptr;
const raw_ref<const Profile> profile_;
};

#endif // BRAVE_BROWSER_GEOLOCATION_BRAVE_GEOLOCATION_PERMISSION_CONTEXT_DELEGATE_H_
13 changes: 13 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ source_set("ui") {
"content_settings/brave_autoplay_content_setting_bubble_model.h",
"content_settings/brave_content_setting_image_models.cc",
"content_settings/brave_content_setting_image_models.h",
"geolocation/geolocation_accuracy_tab_helper.cc",
"geolocation/geolocation_accuracy_tab_helper.h",
"geolocation/pref_names.h",
"omnibox/brave_omnibox_client_impl.cc",
"omnibox/brave_omnibox_client_impl.h",
"page_action/brave_page_action_icon_type.h",
Expand Down Expand Up @@ -306,6 +309,8 @@ source_set("ui") {
"views/frame/brave_opaque_browser_frame_view.h",
"views/frame/brave_window_frame_graphic.cc",
"views/frame/brave_window_frame_graphic.h",
"views/geolocation_accuracy_helper_dialog_view.cc",
"views/geolocation_accuracy_helper_dialog_view.h",
"views/omnibox/brave_omnibox_popup_view_views.cc",
"views/omnibox/brave_omnibox_popup_view_views.h",
"views/omnibox/brave_omnibox_result_view.cc",
Expand Down Expand Up @@ -427,6 +432,13 @@ source_set("ui") {
]
}

if (is_win) {
sources += [
"geolocation/geolocation_accuracy_utils_win.cc",
"geolocation/geolocation_accuracy_utils_win.h",
]
}

# brave's obsolete infobar is only used on Windows and Mac now.
if (is_win || is_mac) {
sources += [
Expand Down Expand Up @@ -552,6 +564,7 @@ source_set("ui") {
"//components/strings:components_strings",
"//components/tab_groups",
"//components/update_client",
"//components/user_prefs",
"//content/public/browser",
"//content/public/common",
"//mojo/public/cpp/bindings",
Expand Down
4 changes: 4 additions & 0 deletions browser/ui/browser_dialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ void ShowCrashReportPermissionAskDialog(Browser* browser);
// Run |callback| when dialog closed.
void ShowObsoleteSystemConfirmDialog(base::OnceCallback<void(bool)> callback);

// Launch dialog to inform that system location service is not enabled.
void ShowGeolocationAccuracyHelperDialog(content::WebContents* web_contents,
base::OnceClosure closing_callback);

#if BUILDFLAG(ENABLE_TEXT_RECOGNITION)
// Show web modal dialog for showing text that recognized from |image|.
void ShowTextRecognitionDialog(content::WebContents* web_contents,
Expand Down
24 changes: 24 additions & 0 deletions browser/ui/geolocation/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2023 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

source_set("browser_tests") {
testonly = true
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]

sources = [ "geolocation_accuracy_browsertest.cc" ]

deps = [
"//base",
"//brave/browser",
"//chrome/browser",
"//chrome/browser/ui",
"//chrome/test:test_support",
"//chrome/test:test_support_ui",
"//components/permissions",
"//components/permissions:test_support",
"//components/prefs",
"//content/test:test_support",
]
}
166 changes: 166 additions & 0 deletions browser/ui/geolocation/geolocation_accuracy_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include <memory>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "brave/browser/ui/geolocation/geolocation_accuracy_tab_helper.h"
#include "brave/browser/ui/geolocation/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/test/mock_permission_prompt_factory.h"
#include "components/prefs/pref_service.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

class GeolocationAccuracyBrowserTest : public InProcessBrowserTest {
public:
GeolocationAccuracyBrowserTest()
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}

GeolocationAccuracyBrowserTest(const GeolocationAccuracyBrowserTest&) =
delete;
GeolocationAccuracyBrowserTest& operator=(
const GeolocationAccuracyBrowserTest&) = delete;
~GeolocationAccuracyBrowserTest() override = default;

void SetUpCommandLine(base::CommandLine* command_line) override {
InProcessBrowserTest::SetUpCommandLine(command_line);
mock_cert_verifier_.SetUpCommandLine(command_line);
}

void SetUpInProcessBrowserTestFixture() override {
InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
mock_cert_verifier_.SetUpInProcessBrowserTestFixture();
}

void TearDownInProcessBrowserTestFixture() override {
InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
mock_cert_verifier_.TearDownInProcessBrowserTestFixture();
}

void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
permissions::PermissionRequestManager* manager =
permissions::PermissionRequestManager::FromWebContents(
GetActiveWebContents());
mock_permission_prompt_factory_ =
std::make_unique<permissions::MockPermissionPromptFactory>(manager);

host_resolver()->AddRule("*", "127.0.0.1");
https_server()->ServeFilesFromSourceDirectory(GetChromeTestDataDir());
ASSERT_TRUE(https_server()->Start());
}

void TearDownOnMainThread() override {
mock_permission_prompt_factory_.reset();
}

permissions::MockPermissionPromptFactory* prompt_factory() {
return mock_permission_prompt_factory_.get();
}

net::EmbeddedTestServer* https_server() { return &https_server_; }
base::RunLoop* run_loop() const { return run_loop_.get(); }

void WaitUntil(base::RepeatingCallback<bool()> condition) {
if (condition.Run()) {
return;
}

base::RepeatingTimer scheduler;
scheduler.Start(FROM_HERE, base::Milliseconds(100),
base::BindLambdaForTesting([this, &condition]() {
if (condition.Run()) {
run_loop_->Quit();
}
}));
Run();
}

void Run() {
run_loop_ = std::make_unique<base::RunLoop>();
run_loop()->Run();
}

content::WebContents* GetActiveWebContents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}

content::RenderFrameHost* GetActiveMainFrame() {
return GetActiveWebContents()->GetPrimaryMainFrame();
}

void AcceptDialogForTesting() {
web_modal::WebContentsModalDialogManager* manager =
web_modal::WebContentsModalDialogManager::FromWebContents(
GetActiveWebContents());
DCHECK(manager);
manager->CloseAllDialogs();
}

content::ContentMockCertVerifier mock_cert_verifier_;
net::test_server::EmbeddedTestServer https_server_;
std::unique_ptr<permissions::MockPermissionPromptFactory>
mock_permission_prompt_factory_;
std::unique_ptr<base::RunLoop> run_loop_;
};

IN_PROC_BROWSER_TEST_F(GeolocationAccuracyBrowserTest, DialogLaunchTest) {
const GURL& url = https_server()->GetURL("/empty.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

mock_permission_prompt_factory_->set_response_type(
permissions::PermissionRequestManager::AutoResponseType::DISMISS);

// Check bubble is shown and dialog is also launched.
content::ExecuteScriptAsync(
GetActiveMainFrame(),
"navigator.geolocation.getCurrentPosition(function(){});");
mock_permission_prompt_factory_->WaitForPermissionBubble();

GeolocationAccuracyTabHelper* accuracy_tab_helper =
GeolocationAccuracyTabHelper::FromWebContents(GetActiveWebContents());
if (!accuracy_tab_helper) {
return;
}

EXPECT_TRUE(accuracy_tab_helper->is_dialog_running_);
EXPECT_TRUE(accuracy_tab_helper->dialog_asked_in_current_navigation_);

// Wait to dialog closing.
AcceptDialogForTesting();
WaitUntil(base::BindLambdaForTesting(
[&]() { return !accuracy_tab_helper->is_dialog_running_; }));
EXPECT_TRUE(accuracy_tab_helper->dialog_asked_in_current_navigation_);

// Navigate again and check flags are cleared.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
EXPECT_FALSE(accuracy_tab_helper->is_dialog_running_);
EXPECT_FALSE(accuracy_tab_helper->dialog_asked_in_current_navigation_);

// Disable dialog launching.
browser()->profile()->GetPrefs()->SetBoolean(
kShowGeolocationAccuracyHelperDialog, false);

// Check bubble is shown again but dialog is not launched.
content::ExecuteScriptAsync(
GetActiveMainFrame(),
"navigator.geolocation.getCurrentPosition(function(){});");
mock_permission_prompt_factory_->WaitForPermissionBubble();
EXPECT_FALSE(accuracy_tab_helper->is_dialog_running_);
}
Loading
Loading