Add virtual GetJavaObject on profile_oauth2_token_service_delegate

This makes GetJavaObject from oauth2_token_service_delegate_android
virtual and defined on the base class,
profile_oauth2_token_service_delegate. It is a prerequisite to the
internalization of oauth2_token_service_delegate_android, as
IdentityManager.java will need a reference on OAuth2TokenService.java on
construction. This also avoids the use of a static_cast to
oauth2_token_service_delegate_android in Identity_manager that is not
always valid.

details here:
  https://docs.google.com/document/d/1_ks5kg3qr6TVq7NT6MFg6lh5Yiv-1lic6Rsd9nu5ubI/edit?usp=sharing


Bug: 934688
Change-Id: Ie0c6d3e004dadd36fe21147a55616d782f2939dc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1748892
Commit-Queue: Pâris Meuleman <pmeuleman@chromium.org>
Reviewed-by: Boris Sazonov <bsazonov@chromium.org>
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695970}
diff --git a/components/signin/internal/identity_manager/BUILD.gn b/components/signin/internal/identity_manager/BUILD.gn
index a6a8aa9..bfcb201 100644
--- a/components/signin/internal/identity_manager/BUILD.gn
+++ b/components/signin/internal/identity_manager/BUILD.gn
@@ -22,8 +22,6 @@
     "diagnostics_provider_impl.h",
     "gaia_cookie_manager_service.cc",
     "gaia_cookie_manager_service.h",
-    "mutable_profile_oauth2_token_service_delegate.cc",
-    "mutable_profile_oauth2_token_service_delegate.h",
     "oauth2_token_service_delegate_android.cc",
     "oauth2_token_service_delegate_android.h",
     "oauth_multilogin_helper.cc",
@@ -73,6 +71,11 @@
 
   if (is_android) {
     deps += [ "//components/signin/core/browser/android:jni_headers" ]
+  } else {
+    sources += [
+      "mutable_profile_oauth2_token_service_delegate.cc",
+      "mutable_profile_oauth2_token_service_delegate.h",
+    ]
   }
 
   if (is_chromeos) {
@@ -117,7 +120,6 @@
     "account_info_util_unittest.cc",
     "account_tracker_service_unittest.cc",
     "gaia_cookie_manager_service_unittest.cc",
-    "mutable_profile_oauth2_token_service_delegate_unittest.cc",
     "oauth2_token_service_delegate_android_unittest.cc",
     "oauth_multilogin_helper_unittest.cc",
     "oauth_multilogin_token_fetcher_unittest.cc",
@@ -167,6 +169,10 @@
 
     deps += [ "//components/signin/public/identity_manager/ios:test_support" ]
   }
+
+  if (!is_android) {
+    sources += [ "mutable_profile_oauth2_token_service_delegate_unittest.cc" ]
+  }
 }
 
 # This target contains test support that backs the test support for
diff --git a/components/signin/internal/identity_manager/fake_profile_oauth2_token_service_delegate.cc b/components/signin/internal/identity_manager/fake_profile_oauth2_token_service_delegate.cc
index bef96b8..65bbb978 100644
--- a/components/signin/internal/identity_manager/fake_profile_oauth2_token_service_delegate.cc
+++ b/components/signin/internal/identity_manager/fake_profile_oauth2_token_service_delegate.cc
@@ -168,3 +168,11 @@
   it->second->error = error;
   FireAuthErrorChanged(account_id, error);
 }
+
+#if defined(OS_ANDROID)
+base::android::ScopedJavaLocalRef<jobject>
+FakeProfileOAuth2TokenServiceDelegate::GetJavaObject() {
+  NOTREACHED();
+  return base::android::ScopedJavaLocalRef<jobject>();
+}
+#endif
diff --git a/components/signin/internal/identity_manager/fake_profile_oauth2_token_service_delegate.h b/components/signin/internal/identity_manager/fake_profile_oauth2_token_service_delegate.h
index f47aa8f..d208752 100644
--- a/components/signin/internal/identity_manager/fake_profile_oauth2_token_service_delegate.h
+++ b/components/signin/internal/identity_manager/fake_profile_oauth2_token_service_delegate.h
@@ -9,6 +9,7 @@
 
 #include "base/macros.h"
 #include "base/memory/ref_counted.h"
+#include "build/build_config.h"
 #include "components/signin/internal/identity_manager/profile_oauth2_token_service_delegate.h"
 #include "google_apis/gaia/google_service_auth_error.h"
 #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
@@ -73,6 +74,10 @@
   void IssueRefreshTokenForUser(const CoreAccountId& account_id,
                                 const std::string& token);
 
+#if defined(OS_ANDROID)
+  base::android::ScopedJavaLocalRef<jobject> GetJavaObject() override;
+#endif
+
   // Maps account ids to info.
   std::map<CoreAccountId, std::unique_ptr<AccountInfo>> refresh_tokens_;
 
diff --git a/components/signin/internal/identity_manager/oauth2_token_service_delegate_android.h b/components/signin/internal/identity_manager/oauth2_token_service_delegate_android.h
index b55902b..00eedee 100644
--- a/components/signin/internal/identity_manager/oauth2_token_service_delegate_android.h
+++ b/components/signin/internal/identity_manager/oauth2_token_service_delegate_android.h
@@ -38,7 +38,7 @@
   static OAuth2TokenServiceDelegateAndroid* Create();
 
   // Returns a reference to the corresponding Java OAuth2TokenService object.
-  base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
+  base::android::ScopedJavaLocalRef<jobject> GetJavaObject() override;
 
   // Called by the TestingProfile class to disable account validation in
   // tests.  This prevents the token service from trying to look up system
diff --git a/components/signin/internal/identity_manager/profile_oauth2_token_service_delegate.h b/components/signin/internal/identity_manager/profile_oauth2_token_service_delegate.h
index d22b7ea..67b9377 100644
--- a/components/signin/internal/identity_manager/profile_oauth2_token_service_delegate.h
+++ b/components/signin/internal/identity_manager/profile_oauth2_token_service_delegate.h
@@ -20,6 +20,10 @@
 #include "google_apis/gaia/oauth2_access_token_manager.h"
 #include "net/base/backoff_entry.h"
 
+#if defined(OS_ANDROID)
+#include "base/android/jni_android.h"
+#endif
+
 namespace network {
 class SharedURLLoaderFactory;
 }
@@ -133,6 +137,9 @@
 #endif
 
 #if defined(OS_ANDROID)
+  // Returns a reference to the corresponding Java object.
+  virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() = 0;
+
   // Triggers platform specific implementation for Android to reload accounts
   // from system.
   virtual void ReloadAccountsFromSystem(
diff --git a/components/signin/public/identity_manager/identity_manager.cc b/components/signin/public/identity_manager/identity_manager.cc
index d5a87f55..891cc1b 100644
--- a/components/signin/public/identity_manager/identity_manager.cc
+++ b/components/signin/public/identity_manager/identity_manager.cc
@@ -419,10 +419,7 @@
 
 base::android::ScopedJavaLocalRef<jobject>
 IdentityManager::LegacyGetOAuth2TokenServiceJavaObject() {
-  OAuth2TokenServiceDelegateAndroid* delegate =
-      static_cast<OAuth2TokenServiceDelegateAndroid*>(
-          token_service_->GetDelegate());
-  return delegate->GetJavaObject();
+  return token_service_->GetDelegate()->GetJavaObject();
 }
 
 base::android::ScopedJavaLocalRef<jobject> IdentityManager::GetJavaObject() {