-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed brave scheme url is changed to chrome scheme url when copying f…
…rom omnibox fix brave/brave-browser#1973
- Loading branch information
Showing
6 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Copyright (c) 2020 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/ui/views/omnibox/brave_omnibox_view_views.h" | ||
|
||
#include "base/feature_list.h" | ||
#include "brave/browser/ui/toolbar/brave_location_bar_model_delegate.h" | ||
#include "brave/common/url_constants.h" | ||
#include "components/omnibox/browser/omnibox_edit_model.h" | ||
#include "content/public/common/url_constants.h" | ||
#include "ui/base/clipboard/scoped_clipboard_writer.h" | ||
#include "url/gurl.h" | ||
|
||
namespace { | ||
// Copied from omnibox_view_views.cc | ||
constexpr base::Feature kOmniboxCanCopyHyperlinksToClipboard{ | ||
"OmniboxCanCopyHyperlinksToClipboard", base::FEATURE_ENABLED_BY_DEFAULT}; | ||
|
||
bool ShouldAdjust(const base::string16& original, | ||
const base::string16& adjusted) { | ||
// Only adjust scheme is changed from brave to chrome. | ||
if (GURL(original).scheme() == kBraveUIScheme && | ||
GURL(adjusted).scheme() == content::kChromeUIScheme) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void ReplaceChromeSchemeToBrave(GURL* url) { | ||
DCHECK_EQ(content::kChromeUIScheme, url->scheme()); | ||
GURL::Replacements replacements; | ||
replacements.SetSchemeStr(kBraveUIScheme); | ||
*url = url->ReplaceComponents(replacements); | ||
} | ||
|
||
} // namespace | ||
|
||
void BraveOmniboxViewViews::OnAfterCutOrCopy( | ||
ui::ClipboardBuffer clipboard_buffer) { | ||
ui::Clipboard* cb = ui::Clipboard::GetForCurrentThread(); | ||
base::string16 selected_text; | ||
cb->ReadText(clipboard_buffer, &selected_text); | ||
const base::string16 original_selected_text = selected_text; | ||
|
||
GURL url; | ||
bool write_url = false; | ||
model()->AdjustTextForCopy(GetSelectedRange().GetMin(), &selected_text, &url, | ||
&write_url); | ||
|
||
if (ShouldAdjust(original_selected_text, selected_text)) { | ||
BraveLocationBarModelDelegate::FormattedStringFromURL(url, &selected_text); | ||
ReplaceChromeSchemeToBrave(&url); | ||
} | ||
|
||
ui::ScopedClipboardWriter scoped_clipboard_writer(clipboard_buffer); | ||
scoped_clipboard_writer.WriteText(selected_text); | ||
|
||
if (write_url && | ||
base::FeatureList::IsEnabled(kOmniboxCanCopyHyperlinksToClipboard)) { | ||
scoped_clipboard_writer.WriteHyperlink(selected_text, url.spec()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* Copyright (c) 2020 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_UI_VIEWS_OMNIBOX_BRAVE_OMNIBOX_VIEW_VIEWS_H_ | ||
#define BRAVE_BROWSER_UI_VIEWS_OMNIBOX_BRAVE_OMNIBOX_VIEW_VIEWS_H_ | ||
|
||
#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" | ||
|
||
class BraveOmniboxViewViews : public OmniboxViewViews { | ||
public: | ||
using OmniboxViewViews::OmniboxViewViews; | ||
~BraveOmniboxViewViews() override = default; | ||
|
||
BraveOmniboxViewViews(const BraveOmniboxViewViews&) = delete; | ||
BraveOmniboxViewViews& operator=(const BraveOmniboxViewViews&) = delete; | ||
|
||
private: | ||
// OmniboxViewViews overrides: | ||
void OnAfterCutOrCopy(ui::ClipboardBuffer clipboard_buffer) override; | ||
}; | ||
|
||
#endif // BRAVE_BROWSER_UI_VIEWS_OMNIBOX_BRAVE_OMNIBOX_VIEW_VIEWS_H_ |
43 changes: 43 additions & 0 deletions
43
browser/ui/views/omnibox/brave_omnibox_view_views_browsertest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* Copyright (c) 2020 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 "chrome/browser/ui/views/frame/browser_view.h" | ||
#include "chrome/browser/ui/views/location_bar/location_bar_view.h" | ||
#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" | ||
#include "chrome/browser/ui/views/toolbar/toolbar_view.h" | ||
#include "chrome/test/base/in_process_browser_test.h" | ||
#include "chrome/test/base/ui_test_utils.h" | ||
#include "url/gurl.h" | ||
#include "ui/base/clipboard/clipboard.h" | ||
#include "ui/strings/grit/ui_strings.h" | ||
|
||
class BraveOmniboxViewViewsTest : public InProcessBrowserTest { | ||
public: | ||
LocationBarView* location_bar() { | ||
auto* browser_view = BrowserView::GetBrowserViewForBrowser(browser()); | ||
return browser_view->toolbar()->location_bar(); | ||
} | ||
OmniboxViewViews* omnibox_view() { return location_bar()->omnibox_view(); } | ||
}; | ||
|
||
// Load brave url and check copied url also has brave scheme. | ||
IN_PROC_BROWSER_TEST_F(BraveOmniboxViewViewsTest, CopyURLToClipboardTest) { | ||
const std::string test_url("brave://version/"); | ||
ui_test_utils::NavigateToURL(browser(), GURL(test_url)); | ||
|
||
omnibox_view()->SelectAll(true); | ||
omnibox_view()->ExecuteCommand(IDS_APP_COPY, 0); | ||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); | ||
std::string text_from_clipboard; | ||
clipboard->ReadAsciiText(ui::ClipboardBuffer::kCopyPaste, | ||
&text_from_clipboard); | ||
EXPECT_EQ(test_url, text_from_clipboard); | ||
|
||
#if defined(OS_LINUX) | ||
clipboard->ReadAsciiText(ui::ClipboardBuffer::kSelection, | ||
&text_from_clipboard); | ||
EXPECT_EQ(test_url, text_from_clipboard); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters