ChromePublic: fix crash with --disable-sync
If sync is disabled (by the --disable-sync flag or by a variation),
ProfileSyncServiceFactory returns NULL which ProfileSyncServiceAndroid
is not expecting. This CL allows ProfileSyncService.get() to return
null in this case (instead of crashing on the native side), and adds
null checks to everywhere that ProfileSyncService is used on the Java
side (except for classes that are not instantiated when sync is
disabled). SyncController is now also null if ProfileSyncService is
null, and null checks are added to callers of SyncController.
BUG=557369
Review URL: https://codereview.chromium.org/1458663002
Cr-Commit-Position: refs/heads/master@{#362570}
diff --git a/chrome/browser/sync/profile_sync_service_android.cc b/chrome/browser/sync/profile_sync_service_android.cc
index e0312c6..cda481e 100644
--- a/chrome/browser/sync/profile_sync_service_android.cc
+++ b/chrome/browser/sync/profile_sync_service_android.cc
@@ -97,15 +97,19 @@
sync_service_ =
ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
- DCHECK(sync_service_);
}
-void ProfileSyncServiceAndroid::Init() {
- sync_service_->AddObserver(this);
+bool ProfileSyncServiceAndroid::Init() {
+ if (sync_service_) {
+ sync_service_->AddObserver(this);
+ return true;
+ } else {
+ return false;
+ }
}
ProfileSyncServiceAndroid::~ProfileSyncServiceAndroid() {
- if (sync_service_->HasObserver(this)) {
+ if (sync_service_ && sync_service_->HasObserver(this)) {
sync_service_->RemoveObserver(this);
}
}
@@ -442,6 +446,10 @@
static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
ProfileSyncServiceAndroid* profile_sync_service_android =
new ProfileSyncServiceAndroid(env, obj);
- profile_sync_service_android->Init();
- return reinterpret_cast<intptr_t>(profile_sync_service_android);
+ if (profile_sync_service_android->Init()) {
+ return reinterpret_cast<intptr_t>(profile_sync_service_android);
+ } else {
+ delete profile_sync_service_android;
+ return 0;
+ }
}