[Android] Additional changes for InterceptNavigationDelegateImpl

Addresses code review suggestions

Chromium change:

https://chromium.googlesource.com/chromium/src/+/204335845d2b7292bb99cb880203b7d132ddfa72

commit 204335845d2b7292bb99cb880203b7d132ddfa72
Author: Colin Blundell <blundell@chromium.org>
Date:   Wed Apr 15 08:52:29 2020 +0000

    [Android] Componentize InterceptNavigationDelegateImpl

    After preceding CLs this class has no remaining //chrome dependencies
    other than resources, which this CL componentizes along with the class
    itself. As this implementation of InterceptNavigationDelegate is
    specific to the overall intent launching logic in
    //components/external_intents, this CL componentizes it into that
    component. Componentizing this class will enable it to be reused in
    WebLayer.

    Bug: 1031465
This commit is contained in:
samartnik
2020-07-01 18:12:05 -04:00
committed by mkarolin
parent bfa1e1dca8
commit 8c0eaef2d7
6 changed files with 52 additions and 108 deletions
-16
View File
@@ -43,7 +43,6 @@
#include "chrome/common/chrome_paths.h"
#include "components/component_updater/component_updater_service.h"
#include "components/component_updater/timer_update_scheduler.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
@@ -101,19 +100,6 @@ void InitSystemRequestHandlerCallback() {
->RegisterOnBeforeSystemRequestCallback(before_system_request_callback);
}
PrefService* BraveGetPrefs() {
return ProfileManager::GetActiveUserProfile()
->GetOriginalProfile()
->GetPrefs();
}
void InitGetPrefsCallback() {
prefs::BravePrefService::GetPrefsCallback get_prefs_callback =
base::Bind(BraveGetPrefs);
prefs::BravePrefService::GetInstance()->RegisterGetPrefsCallback(
get_prefs_callback);
}
} // namespace
BraveBrowserProcessImpl* g_brave_browser_process = nullptr;
@@ -175,8 +161,6 @@ void BraveBrowserProcessImpl::Init() {
#endif
InitSystemRequestHandlerCallback();
InitGetPrefsCallback();
}
brave_component_updater::BraveComponent::Delegate*
@@ -10,6 +10,8 @@
#include "components/navigation_interception/intercept_navigation_delegate.h"
#include "components/navigation_interception/navigation_params.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "net/base/escape.h"
@@ -21,30 +23,14 @@ namespace {
using navigation_interception::InterceptNavigationDelegate;
using navigation_interception::NavigationParams;
bool ShouldPlayVideoInBrowser(const GURL& url) {
PrefService* pref_service =
prefs::BravePrefService::GetInstance()->GetPrefs();
if (!pref_service) {
NOTREACHED();
return false;
}
if (!pref_service->GetBoolean(kPlayYTVideoInBrowserEnabled)) {
return false;
}
if (url.host().find("youtube.com") != std::string::npos ||
url.host().find("youtu.be") != std::string::npos) {
return true;
}
return false;
}
class BraveInterceptNavigationDelegate : public InterceptNavigationDelegate {
public:
BraveInterceptNavigationDelegate(JNIEnv* env, jobject jdelegate)
: InterceptNavigationDelegate(env, jdelegate) {}
BraveInterceptNavigationDelegate(JNIEnv* env,
jobject jdelegate,
PrefService* pref_service)
: InterceptNavigationDelegate(env, jdelegate) {
pref_service_ = pref_service;
}
bool ShouldIgnoreNavigation(
const NavigationParams& navigation_params) override {
@@ -58,6 +44,27 @@ class BraveInterceptNavigationDelegate : public InterceptNavigationDelegate {
return InterceptNavigationDelegate::ShouldIgnoreNavigation(
chrome_navigation_params);
}
private:
bool ShouldPlayVideoInBrowser(const GURL& url) {
if (!pref_service_) {
NOTREACHED();
return false;
}
if (!pref_service_->GetBoolean(kPlayYTVideoInBrowserEnabled)) {
return false;
}
if (url.host().find("youtube.com") != std::string::npos ||
url.host().find("youtu.be") != std::string::npos) {
return true;
}
return false;
}
PrefService* pref_service_;
};
} // namespace
@@ -71,7 +78,9 @@ static void JNI_InterceptNavigationDelegateImpl_AssociateWithWebContents(
content::WebContents::FromJavaWebContents(jweb_contents);
InterceptNavigationDelegate::Associate(
web_contents,
std::make_unique<BraveInterceptNavigationDelegate>(env, jdelegate));
std::make_unique<BraveInterceptNavigationDelegate>(
env, jdelegate,
user_prefs::UserPrefs::Get(web_contents->GetBrowserContext())));
}
} // namespace external_intents
@@ -1,30 +0,0 @@
/* 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 "../../../../components/prefs/pref_service.cc"
namespace prefs {
BravePrefService* BravePrefService::GetInstance() {
return base::Singleton<BravePrefService>::get();
}
void BravePrefService::RegisterGetPrefsCallback(const GetPrefsCallback& cb) {
get_prefs_callback_ = cb;
}
PrefService* BravePrefService::GetPrefs() {
if (!get_prefs_callback_) {
NOTREACHED();
return nullptr;
}
return get_prefs_callback_.Run();
}
BravePrefService::BravePrefService() {}
BravePrefService::~BravePrefService() {}
} // namespace prefs
@@ -1,39 +0,0 @@
/* 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_CHROMIUM_SRC_COMPONENTS_PREFS_PREF_SERVICE_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_PREFS_PREF_SERVICE_H_
#include "../../../../components/prefs/pref_service.h"
#include "base/callback.h"
#include "base/memory/singleton.h"
namespace prefs {
// Allows access to active user's profile PrefService in components
class COMPONENTS_PREFS_EXPORT BravePrefService {
public:
typedef base::Callback<PrefService*()> GetPrefsCallback;
static BravePrefService* GetInstance();
void RegisterGetPrefsCallback(const GetPrefsCallback& cb);
PrefService* GetPrefs();
private:
friend struct base::DefaultSingletonTraits<BravePrefService>;
BravePrefService();
~BravePrefService();
GetPrefsCallback get_prefs_callback_;
DISALLOW_COPY_AND_ASSIGN(BravePrefService);
};
} // namespace prefs
#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_PREFS_PREF_SERVICE_H_
@@ -0,0 +1,8 @@
group("android") {
deps = [
"//brave/common",
"//components/prefs",
"//components/user_prefs",
"//url",
]
}
@@ -0,0 +1,12 @@
diff --git a/components/external_intents/android/BUILD.gn b/components/external_intents/android/BUILD.gn
index 530cfa2c449b94854ace00cc710818fb496d2a7e..4dab931ff4d955f8a04bfe7bdfce7cde9f37ebd8 100644
--- a/components/external_intents/android/BUILD.gn
+++ b/components/external_intents/android/BUILD.gn
@@ -57,6 +57,7 @@ static_library("android") {
"//components/navigation_interception",
"//content/public/browser",
]
+ deps += [ "//brave/components/external_intents/android" ]
}
android_library("javatests") {