Skip to content

Commit bcd988c

Browse files
committed
Redirect users to extension store for inline install requests
Fix brave/brave-browser#614
1 parent a3104cd commit bcd988c

6 files changed

+163
-0
lines changed

browser/extensions/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ source_set("extensions") {
1818
"brave_extension_provider.h",
1919
"brave_tor_client_updater.cc",
2020
"brave_tor_client_updater.h",
21+
"brave_webstore_inline_installer.cc",
22+
"brave_webstore_inline_installer.h",
2123
]
2224

2325
deps = [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#include "brave/browser/extensions/brave_webstore_inline_installer.h"
6+
7+
#include "base/strings/stringprintf.h"
8+
#include "base/values.h"
9+
#include "content/public/browser/web_contents.h"
10+
11+
namespace extensions {
12+
13+
// The URL to the webstore page for a specific app.
14+
const char kWebstoreUrlFormat[] =
15+
"https://chrome.google.com/webstore/detail/%s";
16+
17+
BraveWebstoreInlineInstaller::BraveWebstoreInlineInstaller(
18+
content::WebContents* web_contents,
19+
content::RenderFrameHost* host,
20+
const std::string& webstore_item_id,
21+
const GURL& requestor_url,
22+
const Callback& callback)
23+
: WebstoreInlineInstaller(web_contents, host,
24+
webstore_item_id, requestor_url, callback) {
25+
}
26+
27+
BraveWebstoreInlineInstaller::~BraveWebstoreInlineInstaller() {}
28+
29+
bool BraveWebstoreInlineInstaller::CheckInlineInstallPermitted(
30+
const base::DictionaryValue& webstore_data,
31+
std::string* error) const {
32+
// Open a URL corresponding to the extension ID.
33+
GURL url(base::StringPrintf(kWebstoreUrlFormat, id().c_str()));
34+
web_contents()->OpenURL(content::OpenURLParams(
35+
url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB,
36+
ui::PAGE_TRANSITION_LINK, false));
37+
// Return an error so nothing else is processed
38+
*error = kInlineInstallNotSupportedKey;
39+
return false;
40+
}
41+
42+
} // namespace extensions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#ifndef BRAVE_BROWSER_EXTENSIONS_BRAVE_WEBSTORE_INLINE_INSTALLER_H_
6+
#define BRAVE_BROWSER_EXTENSIONS_BRAVE_WEBSTORE_INLINE_INSTALLER_H_
7+
8+
class BraveWebstoreBrowserTest;
9+
10+
#include "chrome/browser/extensions/webstore_inline_installer.h"
11+
12+
namespace extensions {
13+
14+
extern const char kWebstoreUrlFormat[];
15+
16+
class BraveWebstoreInlineInstaller : public WebstoreInlineInstaller {
17+
public:
18+
BraveWebstoreInlineInstaller(content::WebContents* web_contents,
19+
content::RenderFrameHost* host,
20+
const std::string& webstore_item_id,
21+
const GURL& requestor_url,
22+
const Callback& callback);
23+
24+
protected:
25+
~BraveWebstoreInlineInstaller() override;
26+
friend class ::BraveWebstoreBrowserTest;
27+
bool CheckInlineInstallPermitted(const base::DictionaryValue& webstore_data,
28+
std::string* error) const override;
29+
DISALLOW_IMPLICIT_CONSTRUCTORS(BraveWebstoreInlineInstaller);
30+
};
31+
32+
} // namespace extensions
33+
34+
#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_WEBSTORE_INLINE_INSTALLER_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#include "brave/browser/extensions/brave_webstore_inline_installer.h"
6+
#include "chrome/browser/ui/browser.h"
7+
#include "chrome/test/base/in_process_browser_test.h"
8+
#include "chrome/test/base/ui_test_utils.h"
9+
#include "content/public/test/browser_test_utils.h"
10+
11+
void OnInstalled(bool success, const std::string& error,
12+
extensions::webstore_install::Result result) {
13+
}
14+
15+
class BraveWebstoreBrowserTest : public InProcessBrowserTest {
16+
public:
17+
void SetUpOnMainThread() override {
18+
InProcessBrowserTest::SetUpOnMainThread();
19+
extension_id_ = "apdfllckaahabafndbhieahigkjlhalf";
20+
}
21+
22+
content::WebContents* contents() {
23+
return browser()->tab_strip_model()->GetActiveWebContents();
24+
}
25+
26+
bool NavigateToURLUntilLoadStop(const GURL& url) {
27+
ui_test_utils::NavigateToURL(browser(), url);
28+
return WaitForLoadStop(contents());
29+
}
30+
31+
int GetTabCount() const {
32+
return browser()->tab_strip_model()->count();
33+
}
34+
35+
bool CheckInlineInstallPermitted() {
36+
base::DictionaryValue webstore_data;
37+
std::string err;
38+
auto* installer = new extensions::BraveWebstoreInlineInstaller(contents(),
39+
contents()->GetMainFrame(), extension_id(), GURL(),
40+
base::Bind(&OnInstalled));
41+
return installer->CheckInlineInstallPermitted(webstore_data, &err);
42+
}
43+
44+
std::string extension_id() const {
45+
return extension_id_;
46+
}
47+
48+
protected:
49+
std::string extension_id_;
50+
};
51+
52+
IN_PROC_BROWSER_TEST_F(BraveWebstoreBrowserTest,
53+
RedirectsUserToChromeWebStore) {
54+
// Inline install should not be permitted.
55+
ASSERT_FALSE(CheckInlineInstallPermitted());
56+
// Inline install should create a second tab for the web-contents.
57+
GURL url(base::StringPrintf(extensions::kWebstoreUrlFormat,
58+
extension_id().c_str()));
59+
ASSERT_EQ(GetTabCount(), 2);
60+
ASSERT_STREQ(browser()->tab_strip_model()->GetWebContentsAt(1)->GetVisibleURL().spec().c_str(), url.spec().c_str());
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
diff --git a/chrome/browser/extensions/webstore_inline_installer_factory.cc b/chrome/browser/extensions/webstore_inline_installer_factory.cc
2+
index 39e0b83a7aa1b2b75fa412a762134a163e3ae0d7..b61d998422a012fb7bdbe623108cc16b211eac1e 100644
3+
--- a/chrome/browser/extensions/webstore_inline_installer_factory.cc
4+
+++ b/chrome/browser/extensions/webstore_inline_installer_factory.cc
5+
@@ -6,6 +6,7 @@
6+
7+
#include <memory>
8+
9+
+#include "brave/browser/extensions/brave_webstore_inline_installer.h"
10+
#include "chrome/browser/extensions/webstore_inline_installer.h"
11+
#include "content/public/browser/web_contents.h"
12+
13+
@@ -17,8 +18,8 @@ WebstoreInlineInstaller* WebstoreInlineInstallerFactory::CreateInstaller(
14+
const std::string& webstore_item_id,
15+
const GURL& requestor_url,
16+
const WebstoreStandaloneInstaller::Callback& callback) {
17+
- return new WebstoreInlineInstaller(contents, host, webstore_item_id,
18+
- requestor_url, callback);
19+
+ return new BraveWebstoreInlineInstaller(contents, host, webstore_item_id,
20+
+ requestor_url, callback);
21+
}
22+
23+
} // namespace extensions

test/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ test("brave_browser_tests") {
158158
"//brave/browser/brave_resources_browsertest.cc",
159159
"//brave/browser/extensions/brave_tor_client_updater_browsertest.cc",
160160
"//brave/browser/extensions/api/brave_shields_api_browsertest.cc",
161+
"//brave/browser/extensions/brave_webstore_inline_installer_browsertest.cc",
161162
"//brave/browser/ui/content_settings/brave_autoplay_blocked_image_model_browsertest.cc",
162163
"//brave/browser/ui/content_settings/brave_widevine_blocked_image_model_browsertest.cc",
163164
"//brave/browser/ui/webui/brave_new_tab_ui_browsertest.cc",

0 commit comments

Comments
 (0)