-
Notifications
You must be signed in to change notification settings - Fork 893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android private search fix #15704
Android private search fix #15704
Conversation
390e834
to
cdf90e2
Compare
cdf90e2
to
3fbefe3
Compare
|
||
// Only need last used profile because we are in settings | ||
if (mProfile == null) { | ||
if (!mIsPrivate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use curly braces
(https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/java/java.md#Curly-braces)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
JNI_BraveTemplateUrlServiceFactory_GetTemplateUrlService(JNIEnv* env, const | ||
base::android::JavaParamRef<jobject>& j_profile) { | ||
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | ||
return GetTemplateUrlService(profile)->GetJavaObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this causes to use browser/search_engines/private_window_search_engine_provider_service.cc
then it's awesome 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually asked Simon about this, and it is a mimic of private_window_search_engine_provider_service
. That class itself is only applicable to extensions though, maybe useful in the future.
3fbefe3
to
d2f8f78
Compare
@wchen342 is it ready for review or draft yet? |
@SergeyZhukovsky It's draft, still having a problem with bytecode, but the rest is finished. |
2df0740
to
6c03cfa
Compare
} | ||
|
||
static public void setDSEPrefs(TemplateUrl templateUrl, Profile profile) { | ||
Log.d("BraveSearchEngineAdapter", "setDSEPrefs: isPrivate: " + String.valueOf(profile.isOffTheRecord()) + " ShortName: " + templateUrl.getShortName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need logs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do a clean up after fixed other problems. Same for the other logs.
static public void updateActiveDSE(Profile profile) { | ||
String shortName = getDSEShortName(profile); | ||
TemplateUrl templateUrl = getTemplateUrlByShortName(profile, shortName); | ||
Log.d("BraveSearchEngineAdapter", "updateActiveDSE: isPrivate: " + String.valueOf(profile.isOffTheRecord()) + " ShortName: " + shortName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
BraveSearchEnginePrefHelper.getInstance().setFetchSEFromNative(false); | ||
} | ||
|
||
Log.d("BraveSearchEngineAdapter", "getDSEShortName: isPrivate: " + String.valueOf(profile.isOffTheRecord()) + " defaultSearchEngineName: " + defaultSearchEngineName + " DSE_SHORTNAME: " + | ||
ContextUtils.getAppSharedPreferences().getString(profile.isOffTheRecord() ? PRIVATE_DSE_SHORTNAME : STANDARD_DSE_SHORTNAME, "")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
private void runTemplateUrlServiceWithProfile(Runnable r) { | ||
BraveTemplateUrlServiceFactory.setCurrentProfile(mProfile); | ||
r.run(); | ||
BraveTemplateUrlServiceFactory.setCurrentProfile(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure I understand what we do here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what happens is the original call in chromium doesn't have a parameter, and bytecode cannot rewrite calls to add it. So I use a workaround that I add a static field in the BraveTemplateUrlServiceFactory
class and set it before any function call to it. Otherwise we need to overwrite every function in chromium's SearchEngineAdapter.java
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to call it twice, one time before the r.run()
with a profile and another one after that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see as well how sProfile
is used, do we need it at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to call it twice, one time before the r.run() with a profile and another one after that?
It is once for setting the static field and once for clearing it so other calls to get()
doesn't get stuck with the profile. I accidentally deleted the sProfile
part when doing the other fix for getting tab models in native, thanks for pointing that out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, got it, could you please make another pass and make sure everything works as intended before the merge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'll rebase, fix the formatting etc. and check everything is fine.
@@ -31,7 +32,8 @@ public void onNewTabCreated(Tab tab, @TabCreationState int creationState) {} | |||
|
|||
@Override | |||
public void onTabModelSelected(TabModel newModel, TabModel oldModel) { | |||
BraveSearchEngineUtils.updateActiveDSE(mTabModelSelector.isIncognitoSelected()); | |||
Log.w("SearchEngineTabModelSelectorObserver", "onTabModelSelected: isIncognito: " + String.valueOf(newModel.getProfile().isOffTheRecord())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logs
cbe875e
to
ecc5d0f
Compare
ecc5d0f
to
404df99
Compare
android/java/org/chromium/chrome/browser/search_engines/BraveTemplateUrlServiceFactory.java
Outdated
Show resolved
Hide resolved
android/java/org/chromium/chrome/browser/settings/BraveSearchEngineUtils.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
|
||
import org.objectweb.asm.ClassVisitor; | ||
|
||
public class BraveTemplateUrlServiceFactory extends BraveClassVisitor { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please name it BraveTemplateUrlServiceFactoryClassAdapter
for consistency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
/** | ||
* @return The singleton instance of {@link TemplateUrlService} for each profile, creating it if necessary. | ||
*/ | ||
public static TemplateUrlService get(Profile profile) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should rename it to something like getForProfile
to clearly indicate this is not the method we are changing ownership to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, changed the method name.
|
||
Profile profile = null; | ||
|
||
// Cannot import BraveActivity here due to circular dependency, need reflection all the way |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This indicates that we are misusing it.
Aside from this, in general I don't really get it, previously we had quite simple logic to apply current search engine depending on whether we are currently on private or regular tab. What has changed? How closing all private tabs fixing it? Frankly I would rather require user to relaunch browser to apply the setting instead of these substantial changes. As it's really hard to predict all unintended issues we may get going forward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check DM for a brief explanation of the issue. Also in addition:
What has changed?
There are now two services instead of one, one tied to regular profile and one tied to OTR profile. Also the service tied to OTR profile will be destroyed time to time (e.g. when user closes all private tabs and open one again later). And these all happens in native and share codes with desktop so it is not possible to override.
How closing all private tabs fixing it?
The service tied to OTR profile got terminated, and the next time a new private tab is opened, a new service is started, copying prefs from regular profile's service.
require user to relaunch browser
That is rather a product/design decision I cannot make. But as far as the current implementation goes, I see no other way to make things work because of the desktop/native changes, so this is basically the best we can do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Profile* GetOriginalProfile() {
return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
}
} // namespace
static TemplateURLService* GetTemplateUrlService() {
return TemplateURLServiceFactory::GetForProfile(GetOriginalProfile());
}
won't that give us a current active tab profile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SergeyZhukovsky Is that the code in template_url_service_factory_android.cc
? That function is never called. The service is acquired with static TemplateURLService* GetTemplateUrlService(Profile* profile)
in brave_template_url_service_factory_android.cc
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it doesn't give us the service linked to active tab. GetOriginalProfile
only return regular profile. If I remember correctly GetActiveUserOrOffTheRecordProfile()
dpesn't work the same way on Desktop and Android, that was why Brian said we shall use Profile from Java whenever possible.
Edit: Just checked the code, GetActiveUser
basically returns initial profile for non ChromeOS builds. It is mostly used for getting the current user on ChromeOS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there is a way to get it done without the layering violation? I also don't like that part much. I just saw that there is an access to profiles from native, is there a way to get current active tab from native?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated with a way to iterate through all tab models and find the active one.
a033a5f
to
a2eee33
Compare
runTemplateUrlServiceWithProfile(() -> { super.onTemplateURLServiceChanged(); }); | ||
} | ||
|
||
// private boolean locationEnabled(TemplateUrl templateUrl) is private and unused |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what this comment indicates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a method in the original adapter but it is not called anywhere so no need to override it. Maybe I shall just delete the line since it confuses others.
} | ||
|
||
static public String getDSEShortName(boolean isPrivate) { | ||
static public String getDSEShortName(Profile profile, boolean javaOnly) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain this javaOnly
parameter? It's not obvious when it should be false
and when true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed it to readJavaPrefOnly
and added a comment. Basically when it is true
the native service will not be used and only Java side preference will be used.
@wchen342 perhaps it's good to mention in test plan about testing upgrade scenarios as well that common tab engine and private should remain on upgrades and etc. |
a2eee33
to
05e2e08
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
8c3321a
to
5d2ba06
Compare
5d2ba06
to
212ac0c
Compare
Verification PASSED on
|
Example |
Example |
---|---|
Test Case #5 - Clean profile using locale without Brave as default - Canada
Went through this case several times while running through the above cases and confirmed that it's working as expected
Resolves brave/brave-browser/issues/25821
Resolves brave/brave-browser/issues/26483
Submitter Checklist:
QA/Yes
orQA/No
;release-notes/include
orrelease-notes/exclude
;OS/...
) to the associated issuenpm run test -- brave_browser_tests
,npm run test -- brave_unit_tests
,npm run lint
,npm run gn_check
,npm run tslint
git rebase master
(if needed)Reviewer Checklist:
gn
After-merge Checklist:
changes has landed on
Test Plan:
test 1 (the issue)
test 2 (upgrading from and old version)
test 3 (private tabs initialization)
test 4 (onboarding - non-Brave default)
test 4 (onboarding - Brave default)