Optimizes an access to TemplateUrlService native implementation and fixes a crash on edit private tabs search engine
This commit is contained in:
+32
-17
@@ -28,13 +28,13 @@ public class BraveSearchEngineAdapter extends SearchEngineAdapter {
|
||||
public static final String STANDARD_DSE_SHORTNAME = "standard_dse_shortname";
|
||||
|
||||
private Profile mProfile;
|
||||
private boolean needUpdateActiveDSE;
|
||||
private boolean mNeedUpdateActiveDSE;
|
||||
|
||||
public BraveSearchEngineAdapter(Context context, Profile profile) {
|
||||
super(context, profile);
|
||||
}
|
||||
|
||||
static public void setDSEPrefs(TemplateUrl templateUrl, Profile profile) {
|
||||
public static void setDSEPrefs(TemplateUrl templateUrl, Profile profile) {
|
||||
SharedPreferences.Editor sharedPreferencesEditor =
|
||||
ContextUtils.getAppSharedPreferences().edit();
|
||||
sharedPreferencesEditor.putString(
|
||||
@@ -43,28 +43,36 @@ public class BraveSearchEngineAdapter extends SearchEngineAdapter {
|
||||
sharedPreferencesEditor.apply();
|
||||
}
|
||||
|
||||
static public void updateActiveDSE(Profile profile) {
|
||||
String shortName = getDSEShortName(profile, false);
|
||||
TemplateUrl templateUrl = getTemplateUrlByShortName(profile, shortName);
|
||||
public static void updateActiveDSE(Profile profile, TemplateUrlService templateUrlServiceArg) {
|
||||
String shortName = getDSEShortName(profile, false, templateUrlServiceArg);
|
||||
TemplateUrl templateUrl =
|
||||
getTemplateUrlByShortName(profile, shortName, templateUrlServiceArg);
|
||||
if (templateUrl == null) {
|
||||
return;
|
||||
}
|
||||
String keyword = templateUrl.getKeyword();
|
||||
TemplateUrlService templateUrlService = TemplateUrlServiceFactory.getForProfile(profile);
|
||||
if (templateUrlService != null)
|
||||
TemplateUrlService templateUrlService =
|
||||
templateUrlServiceArg != null
|
||||
? templateUrlServiceArg
|
||||
: TemplateUrlServiceFactory.getForProfile(profile);
|
||||
if (templateUrlService != null) {
|
||||
templateUrlService.setSearchEngine(keyword);
|
||||
else
|
||||
} else {
|
||||
setDSEPrefs(templateUrl, profile);
|
||||
}
|
||||
}
|
||||
|
||||
// when readJavaPrefOnly is true, only read short names from Java preference and
|
||||
// avoid calling native methods
|
||||
static public String getDSEShortName(Profile profile, boolean readJavaPrefOnly) {
|
||||
public static String getDSEShortName(
|
||||
Profile profile, boolean readJavaPrefOnly, TemplateUrlService templateUrlServiceArg) {
|
||||
String defaultSearchEngineName = null;
|
||||
|
||||
if (!readJavaPrefOnly) {
|
||||
TemplateUrlService templateUrlService =
|
||||
TemplateUrlServiceFactory.getForProfile(profile);
|
||||
final TemplateUrlService templateUrlService =
|
||||
templateUrlServiceArg != null
|
||||
? templateUrlServiceArg
|
||||
: TemplateUrlServiceFactory.getForProfile(profile);
|
||||
TemplateUrl dseTemplateUrl = null;
|
||||
if (templateUrlService != null) {
|
||||
dseTemplateUrl = templateUrlService.getDefaultSearchEngineTemplateUrl();
|
||||
@@ -85,8 +93,12 @@ public class BraveSearchEngineAdapter extends SearchEngineAdapter {
|
||||
defaultSearchEngineName);
|
||||
}
|
||||
|
||||
static public TemplateUrl getTemplateUrlByShortName(Profile profile, String name) {
|
||||
TemplateUrlService templateUrlService = TemplateUrlServiceFactory.getForProfile(profile);
|
||||
public static TemplateUrl getTemplateUrlByShortName(
|
||||
Profile profile, String name, TemplateUrlService templateUrlServiceArg) {
|
||||
TemplateUrlService templateUrlService =
|
||||
templateUrlServiceArg != null
|
||||
? templateUrlServiceArg
|
||||
: TemplateUrlServiceFactory.getForProfile(profile);
|
||||
if (templateUrlService != null) {
|
||||
List<TemplateUrl> templateUrls = templateUrlService.getTemplateUrls();
|
||||
for (int index = 0; index < templateUrls.size(); ++index) {
|
||||
@@ -109,7 +121,7 @@ public class BraveSearchEngineAdapter extends SearchEngineAdapter {
|
||||
if (templateUrlService == null || !templateUrlService.isLoaded()) {
|
||||
// updateActiveDSE needs to be delayed for private because service needs to be
|
||||
// loaded if no private tab is opened already
|
||||
needUpdateActiveDSE = true;
|
||||
mNeedUpdateActiveDSE = true;
|
||||
}
|
||||
} catch (IllegalStateException e) {
|
||||
// IllegalStateException indicates that search engine is not available anymore. We just
|
||||
@@ -120,6 +132,9 @@ public class BraveSearchEngineAdapter extends SearchEngineAdapter {
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (!mProfile.isNativeInitialized()) {
|
||||
return;
|
||||
}
|
||||
TemplateUrlService templateUrlService = TemplateUrlServiceFactory.getForProfile(mProfile);
|
||||
// For some reason there is a short period of time when native reference to the profile
|
||||
// has been destroyed but Java reference still exists. The stop() function only removes
|
||||
@@ -151,9 +166,9 @@ public class BraveSearchEngineAdapter extends SearchEngineAdapter {
|
||||
// It is necessary to ensure user's selection is updated on first entering private setting
|
||||
// but it causes updateActiveDSE() to be called twice (once here and
|
||||
// once from SearchEngineTabModelSelectorObserver)
|
||||
if (needUpdateActiveDSE) {
|
||||
needUpdateActiveDSE = false;
|
||||
updateActiveDSE(mProfile);
|
||||
if (mNeedUpdateActiveDSE) {
|
||||
mNeedUpdateActiveDSE = false;
|
||||
updateActiveDSE(mProfile, null);
|
||||
}
|
||||
super.onTemplateUrlServiceLoaded();
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import org.chromium.components.search_engines.TemplateUrl;
|
||||
import org.chromium.components.search_engines.TemplateUrlService;
|
||||
|
||||
public class BraveSearchEngineUtils {
|
||||
static public void initializeBraveSearchEngineStates(TabModelSelector tabModelSelector) {
|
||||
public static void initializeBraveSearchEngineStates(TabModelSelector tabModelSelector) {
|
||||
// There is no point in creating service for OTR profile in advance since they change
|
||||
// It will be initialized in SearchEngineTabModelSelectorObserver when called on an OTR
|
||||
// profile
|
||||
@@ -31,25 +31,26 @@ public class BraveSearchEngineUtils {
|
||||
initializeBraveSearchEngineStates(profile);
|
||||
}
|
||||
|
||||
static public void initializeBraveSearchEngineStates(Profile profile) {
|
||||
public static void initializeBraveSearchEngineStates(Profile profile) {
|
||||
final TemplateUrlService templateUrlService =
|
||||
TemplateUrlServiceFactory.getForProfile(profile);
|
||||
|
||||
if (!templateUrlService.isLoaded()) {
|
||||
templateUrlService.registerLoadListener(new TemplateUrlService.LoadListener() {
|
||||
@Override
|
||||
public void onTemplateUrlServiceLoaded() {
|
||||
templateUrlService.unregisterLoadListener(this);
|
||||
doInitializeBraveSearchEngineStates(profile);
|
||||
}
|
||||
});
|
||||
templateUrlService.registerLoadListener(
|
||||
new TemplateUrlService.LoadListener() {
|
||||
@Override
|
||||
public void onTemplateUrlServiceLoaded() {
|
||||
templateUrlService.unregisterLoadListener(this);
|
||||
doInitializeBraveSearchEngineStates(profile, null);
|
||||
}
|
||||
});
|
||||
templateUrlService.load();
|
||||
} else {
|
||||
doInitializeBraveSearchEngineStates(profile);
|
||||
doInitializeBraveSearchEngineStates(profile, templateUrlService);
|
||||
}
|
||||
}
|
||||
|
||||
static private void initializeDSEPrefs(Profile profile) {
|
||||
private static void initializeDSEPrefs(Profile profile) {
|
||||
// At first run, we should set initial default prefs to each standard/private DSE prefs.
|
||||
// Those pref values will be used until user change DES options explicitly.
|
||||
final String notInitialized = "notInitialized";
|
||||
@@ -69,17 +70,20 @@ public class BraveSearchEngineUtils {
|
||||
}
|
||||
}
|
||||
|
||||
static private void doInitializeBraveSearchEngineStates(Profile profile) {
|
||||
private static void doInitializeBraveSearchEngineStates(
|
||||
Profile profile, TemplateUrlService templateUrlServiceArg) {
|
||||
final TemplateUrlService templateUrlService =
|
||||
TemplateUrlServiceFactory.getForProfile(profile);
|
||||
templateUrlServiceArg != null
|
||||
? templateUrlServiceArg
|
||||
: TemplateUrlServiceFactory.getForProfile(profile);
|
||||
assert templateUrlService.isLoaded();
|
||||
|
||||
initializeDSEPrefs(profile);
|
||||
updateActiveDSE(profile);
|
||||
updateActiveDSE(profile, templateUrlService);
|
||||
QuickActionSearchAndBookmarkWidgetProvider.initializeDelegate();
|
||||
}
|
||||
|
||||
static public void setDSEPrefs(TemplateUrl templateUrl, Profile profile) {
|
||||
public static void setDSEPrefs(TemplateUrl templateUrl, Profile profile) {
|
||||
BraveSearchEngineAdapter.setDSEPrefs(templateUrl, profile);
|
||||
if (!profile.isOffTheRecord() && templateUrl != null) {
|
||||
QuickActionSearchAndBookmarkWidgetProvider.updateSearchEngine(
|
||||
@@ -87,15 +91,15 @@ public class BraveSearchEngineUtils {
|
||||
}
|
||||
}
|
||||
|
||||
static public void updateActiveDSE(Profile profile) {
|
||||
BraveSearchEngineAdapter.updateActiveDSE(profile);
|
||||
public static void updateActiveDSE(Profile profile, TemplateUrlService templateUrlServiceArg) {
|
||||
BraveSearchEngineAdapter.updateActiveDSE(profile, templateUrlServiceArg);
|
||||
}
|
||||
|
||||
static public String getDSEShortName(Profile profile, boolean javaOnly) {
|
||||
return BraveSearchEngineAdapter.getDSEShortName(profile, javaOnly);
|
||||
public static String getDSEShortName(Profile profile, boolean javaOnly) {
|
||||
return BraveSearchEngineAdapter.getDSEShortName(profile, javaOnly, null);
|
||||
}
|
||||
|
||||
static public TemplateUrl getTemplateUrlByShortName(Profile profile, String name) {
|
||||
return BraveSearchEngineAdapter.getTemplateUrlByShortName(profile, name);
|
||||
public static TemplateUrl getTemplateUrlByShortName(Profile profile, String name) {
|
||||
return BraveSearchEngineAdapter.getTemplateUrlByShortName(profile, name, null);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ public class SearchEngineTabModelSelectorObserver implements TabModelSelectorObs
|
||||
if (newModel.getProfile().isOffTheRecord()) {
|
||||
BraveSearchEngineUtils.initializeBraveSearchEngineStates(newModel.getProfile());
|
||||
} else {
|
||||
BraveSearchEngineUtils.updateActiveDSE(newModel.getProfile());
|
||||
BraveSearchEngineUtils.updateActiveDSE(newModel.getProfile(), null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -36,7 +36,7 @@ class BraveDropdownItemViewInfoListBuilder extends DropdownItemViewInfoListBuild
|
||||
private @Nullable BraveSearchBannerProcessor mBraveSearchBannerProcessor;
|
||||
private UrlBarEditingTextStateProvider mUrlBarEditingTextProvider;
|
||||
private @NonNull Supplier<Tab> mActivityTabSupplier;
|
||||
private static final List<String> mBraveSearchEngineDefaultRegions =
|
||||
private static final List<String> sBraveSearchEngineDefaultRegions =
|
||||
Arrays.asList("CA", "DE", "FR", "GB", "US", "AT", "ES", "MX");
|
||||
@Px
|
||||
private static final int DROPDOWN_HEIGHT_UNKNOWN = -1;
|
||||
@@ -105,15 +105,15 @@ class BraveDropdownItemViewInfoListBuilder extends DropdownItemViewInfoListBuild
|
||||
if (ChromeFeatureList.isEnabled(BraveFeatureList.BRAVE_SEARCH_OMNIBOX_BANNER)
|
||||
&& mUrlBarEditingTextProvider != null
|
||||
&& mUrlBarEditingTextProvider.getTextWithoutAutocomplete().length() > 0
|
||||
&& activeTab != null && !activeTab.isIncognito()
|
||||
&& mBraveSearchEngineDefaultRegions.contains(Locale.getDefault().getCountry())
|
||||
&& !BraveSearchEngineAdapter
|
||||
.getDSEShortName(
|
||||
Profile.fromWebContents(activeTab.getWebContents()), false)
|
||||
.equals("Brave")
|
||||
&& activeTab != null
|
||||
&& !activeTab.isIncognito()
|
||||
&& sBraveSearchEngineDefaultRegions.contains(Locale.getDefault().getCountry())
|
||||
&& !BraveSearchEngineAdapter.getDSEShortName(
|
||||
Profile.fromWebContents(activeTab.getWebContents()), false, null)
|
||||
.equals("Brave")
|
||||
&& !OmniboxPrefManager.getInstance().isBraveSearchPromoBannerDismissed()
|
||||
&& !OmniboxPrefManager.getInstance()
|
||||
.isBraveSearchPromoBannerDismissedCurrentSession()) {
|
||||
.isBraveSearchPromoBannerDismissedCurrentSession()) {
|
||||
long expiredDate =
|
||||
OmniboxPrefManager.getInstance().getBraveSearchPromoBannerExpiredDate();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user