Skip to content

Commit

Permalink
Merge pull request #11125 from brave/default_browser_win
Browse files Browse the repository at this point in the history
Set Brave as a default browser w/o system UI on Windows
  • Loading branch information
simonhong authored Nov 18, 2021
2 parents 47eadc5 + ed615d6 commit 965a5ab
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 2 deletions.
47 changes: 47 additions & 0 deletions browser/brave_shell_integration.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_shell_integration.h"

#if defined(OS_WIN)
#include <shlobj.h>

#include <utility>

#include "brave/browser/default_protocol_handler_utils_win.h"
#include "chrome/installer/util/shell_util.h"
#endif

namespace shell_integration {

BraveDefaultBrowserWorker::BraveDefaultBrowserWorker() = default;
BraveDefaultBrowserWorker::~BraveDefaultBrowserWorker() = default;

void BraveDefaultBrowserWorker::SetAsDefaultImpl(
base::OnceClosure on_finished_callback) {
#if defined(OS_WIN)
if (GetDefaultWebClientSetPermission() != SET_DEFAULT_NOT_ALLOWED) {
bool success = false;
const wchar_t* kAssociations[] = {L"https", L"http", L".html", L".htm"};
for (const wchar_t* association : kAssociations) {
success =
protocol_handler_utils::SetDefaultProtocolHandlerFor(association);
if (!success)
break;
}

if (success) {
std::move(on_finished_callback).Run();

// Notify shell to refresh icons
::SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
return;
}
}
#endif
DefaultBrowserWorker::SetAsDefaultImpl(std::move(on_finished_callback));
}

} // namespace shell_integration
29 changes: 29 additions & 0 deletions browser/brave_shell_integration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_BRAVE_SHELL_INTEGRATION_H_
#define BRAVE_BROWSER_BRAVE_SHELL_INTEGRATION_H_

#include "chrome/browser/shell_integration.h"

namespace shell_integration {

class BraveDefaultBrowserWorker : public DefaultBrowserWorker {
public:
BraveDefaultBrowserWorker();

BraveDefaultBrowserWorker(const BraveDefaultBrowserWorker&) = delete;
BraveDefaultBrowserWorker& operator=(const BraveDefaultBrowserWorker&) =
delete;

protected:
~BraveDefaultBrowserWorker() override;

void SetAsDefaultImpl(base::OnceClosure on_finished_callback) override;
};

} // namespace shell_integration

#endif // BRAVE_BROWSER_BRAVE_SHELL_INTEGRATION_H_
2 changes: 2 additions & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ brave_chrome_browser_sources = [
"//brave/browser/brave_local_state_prefs.h",
"//brave/browser/brave_profile_prefs.cc",
"//brave/browser/brave_profile_prefs.h",
"//brave/browser/brave_shell_integration.cc",
"//brave/browser/brave_shell_integration.h",
"//brave/browser/brave_tab_helpers.cc",
"//brave/browser/brave_tab_helpers.h",
"//brave/browser/browser_context_keyed_service_factories.cc",
Expand Down
4 changes: 2 additions & 2 deletions browser/ui/views/brave_default_browser_dialog_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

#include "base/bind.h"
#include "base/memory/scoped_refptr.h"
#include "brave/browser/brave_shell_integration.h"
#include "brave/browser/ui/browser_dialogs.h"
#include "brave/common/pref_names.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/shell_integration.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "components/constrained_window/constrained_window_views.h"
Expand Down Expand Up @@ -159,6 +159,6 @@ void BraveDefaultBrowserDialogView::OnAcceptButtonClicked() {
// The worker pointer is reference counted. While it is running, the
// message loops of the FILE and UI thread will hold references to it
// and it will be automatically freed once all its tasks have finished.
base::MakeRefCounted<shell_integration::DefaultBrowserWorker>()
base::MakeRefCounted<shell_integration::BraveDefaultBrowserWorker>()
->StartSetAsDefault(base::NullCallback());
}
15 changes: 15 additions & 0 deletions chromium_src/chrome/browser/shell_integration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_SHELL_INTEGRATION_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_SHELL_INTEGRATION_H_

#define BRAVE_DEFAULT_BROWSER_WORKER_H friend class BraveDefaultBrowserWorker;

#include "../../../../chrome/browser/shell_integration.h"

#undef BRAVE_DEFAULT_BROWSER_WORKER_H

#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_SHELL_INTEGRATION_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_shell_integration.h"

#define DefaultBrowserWorker BraveDefaultBrowserWorker
#include "../../../../../../../chrome/browser/ui/webui/settings/settings_default_browser_handler.cc"
#undef DefaultBrowserWorker
12 changes: 12 additions & 0 deletions patches/chrome-browser-shell_integration.h.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/chrome/browser/shell_integration.h b/chrome/browser/shell_integration.h
index 29e98f5b2839a44a488d6baec756b660935302dd..b761dea9fbb8f0009976997a41d9a10b2aaf3df1 100644
--- a/chrome/browser/shell_integration.h
+++ b/chrome/browser/shell_integration.h
@@ -235,6 +235,7 @@ class DefaultBrowserWorker : public DefaultWebClientWorker {
protected:
~DefaultBrowserWorker() override;

+ BRAVE_DEFAULT_BROWSER_WORKER_H
private:
// Check if Chrome is the default browser.
DefaultWebClientState CheckIsDefaultImpl() override;

0 comments on commit 965a5ab

Please sign in to comment.