| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/android/infobars/infobar_container_android.h" | 5 #include "chrome/browser/ui/android/infobars/infobar_container_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/infobars/infobar.h" | 10 #include "chrome/browser/infobars/infobar.h" |
| 11 #include "chrome/browser/infobars/infobar_delegate.h" | 11 #include "chrome/browser/infobars/infobar_delegate.h" |
| 12 #include "chrome/browser/infobars/infobar_service.h" | 12 #include "chrome/browser/infobars/infobar_service.h" |
| 13 #include "chrome/browser/ui/android/infobars/auto_login_infobar_delegate_android
.h" | 13 #include "chrome/browser/ui/android/infobars/auto_login_infobar_delegate_android
.h" |
| 14 #include "chrome/browser/ui/android/infobars/infobar_android.h" | 14 #include "chrome/browser/ui/android/infobars/infobar_android.h" |
| 15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 16 #include "jni/InfoBarContainer_jni.h" | 16 #include "jni/InfoBarContainer_jni.h" |
| 17 | 17 |
| 18 using base::android::AttachCurrentThread; | |
| 19 using base::android::JavaRef; | |
| 20 using base::android::ScopedJavaLocalRef; | |
| 21 using content::WebContents; | |
| 22 | 18 |
| 23 // InfoBarContainerAndroid | 19 // InfoBarContainerAndroid ---------------------------------------------------- |
| 20 |
| 24 InfoBarContainerAndroid::InfoBarContainerAndroid(JNIEnv* env, | 21 InfoBarContainerAndroid::InfoBarContainerAndroid(JNIEnv* env, |
| 25 jobject obj, | 22 jobject obj, |
| 26 jobject auto_login_delegate) | 23 jobject auto_login_delegate) |
| 27 : InfoBarContainer(NULL), | 24 : InfoBarContainer(NULL), |
| 28 weak_java_infobar_container_(env, obj), | 25 weak_java_infobar_container_(env, obj), |
| 29 weak_java_auto_login_delegate_(env, auto_login_delegate) {} | 26 weak_java_auto_login_delegate_(env, auto_login_delegate) { |
| 27 } |
| 30 | 28 |
| 31 InfoBarContainerAndroid::~InfoBarContainerAndroid() { | 29 InfoBarContainerAndroid::~InfoBarContainerAndroid() { |
| 32 RemoveAllInfoBarsForDestruction(); | 30 RemoveAllInfoBarsForDestruction(); |
| 33 } | 31 } |
| 34 | 32 |
| 35 void InfoBarContainerAndroid::Destroy(JNIEnv* env, jobject obj) { | 33 void InfoBarContainerAndroid::Destroy(JNIEnv* env, jobject obj) { |
| 36 delete this; | 34 delete this; |
| 37 } | 35 } |
| 38 | 36 |
| 39 // TODO(miguelg) Move this out of infobar container. | |
| 40 void InfoBarContainerAndroid::OnWebContentsReplaced( | 37 void InfoBarContainerAndroid::OnWebContentsReplaced( |
| 41 WebContents* old_web_contents, | 38 content::WebContents* old_web_contents, |
| 42 WebContents* new_web_contents) { | 39 content::WebContents* new_web_contents) { |
| 43 | 40 InfoBarService* new_infobar_service = new_web_contents ? |
| 44 InfoBarService* new_infobar_service = NULL; | 41 InfoBarService::FromWebContents(new_web_contents) : NULL; |
| 45 if (new_web_contents) | 42 if (new_infobar_service) |
| 46 new_infobar_service = InfoBarService::FromWebContents(new_web_contents); | 43 ChangeInfoBarService(new_infobar_service); |
| 47 if (!new_infobar_service) | |
| 48 return; | |
| 49 | |
| 50 ChangeInfoBarService(new_infobar_service); | |
| 51 } | 44 } |
| 52 | 45 |
| 53 // InfobarContainer | |
| 54 void InfoBarContainerAndroid::PlatformSpecificAddInfoBar(InfoBar* infobar, | 46 void InfoBarContainerAndroid::PlatformSpecificAddInfoBar(InfoBar* infobar, |
| 55 size_t position) { | 47 size_t position) { |
| 56 DCHECK(infobar); | 48 DCHECK(infobar); |
| 57 InfoBarAndroid* android_bar = static_cast<InfoBarAndroid*>(infobar); | 49 InfoBarAndroid* android_bar = static_cast<InfoBarAndroid*>(infobar); |
| 58 if (!android_bar) { | 50 if (!android_bar) { |
| 59 // TODO(bulach): CLANK: implement other types of InfoBars. | 51 // TODO(bulach): CLANK: implement other types of InfoBars. |
| 60 // TODO(jrg): this will always print out WARNING_TYPE as an int. | 52 // TODO(jrg): this will always print out WARNING_TYPE as an int. |
| 61 // Try and be more helpful. | 53 // Try and be more helpful. |
| 62 NOTIMPLEMENTED() << "CLANK: infobar type " | 54 NOTIMPLEMENTED() << "CLANK: infobar type " |
| 63 << infobar->delegate()->GetInfoBarType(); | 55 << infobar->delegate()->GetInfoBarType(); |
| 64 return; | 56 return; |
| 65 } | 57 } |
| 66 | 58 |
| 67 if (infobar->delegate()->AsAutoLoginInfoBarDelegate()) { | 59 if (infobar->delegate()->AsAutoLoginInfoBarDelegate()) { |
| 68 AutoLoginInfoBarDelegateAndroid* auto_login_delegate = | 60 AutoLoginInfoBarDelegateAndroid* auto_login_delegate = |
| 69 static_cast<AutoLoginInfoBarDelegateAndroid*>( | 61 static_cast<AutoLoginInfoBarDelegateAndroid*>( |
| 70 infobar->delegate()->AsAutoLoginInfoBarDelegate()); | 62 infobar->delegate()->AsAutoLoginInfoBarDelegate()); |
| 71 if (!auto_login_delegate->AttachAccount(weak_java_auto_login_delegate_)) { | 63 if (!auto_login_delegate->AttachAccount(weak_java_auto_login_delegate_)) |
| 72 return; | 64 return; |
| 73 } | |
| 74 } | 65 } |
| 75 | 66 |
| 76 AttachJavaInfoBar(android_bar); | 67 AttachJavaInfoBar(android_bar); |
| 77 } | 68 } |
| 78 | 69 |
| 79 void InfoBarContainerAndroid::AttachJavaInfoBar(InfoBarAndroid* android_bar) { | 70 void InfoBarContainerAndroid::AttachJavaInfoBar(InfoBarAndroid* android_bar) { |
| 80 // Java infobar already set on the new bar, nothing to do. | |
| 81 if (android_bar->HasSetJavaInfoBar()) | 71 if (android_bar->HasSetJavaInfoBar()) |
| 82 return; | 72 return; |
| 83 JNIEnv* env = AttachCurrentThread(); | 73 JNIEnv* env = base::android::AttachCurrentThread(); |
| 84 ScopedJavaLocalRef<jobject> java_infobar = | 74 base::android::ScopedJavaLocalRef<jobject> java_infobar = |
| 85 android_bar->CreateRenderInfoBar(env); | 75 android_bar->CreateRenderInfoBar(env); |
| 86 Java_InfoBarContainer_addInfoBar( | 76 Java_InfoBarContainer_addInfoBar( |
| 87 env, weak_java_infobar_container_.get(env).obj(), java_infobar.obj()); | 77 env, weak_java_infobar_container_.get(env).obj(), java_infobar.obj()); |
| 88 android_bar->set_java_infobar(java_infobar); | 78 android_bar->set_java_infobar(java_infobar); |
| 89 } | 79 } |
| 90 | 80 |
| 91 void InfoBarContainerAndroid::PlatformSpecificReplaceInfoBar( | 81 void InfoBarContainerAndroid::PlatformSpecificReplaceInfoBar( |
| 92 InfoBar* old_infobar, InfoBar* new_infobar) { | 82 InfoBar* old_infobar, |
| 83 InfoBar* new_infobar) { |
| 93 InfoBarAndroid* new_android_bar = static_cast<InfoBarAndroid*>(new_infobar); | 84 InfoBarAndroid* new_android_bar = static_cast<InfoBarAndroid*>(new_infobar); |
| 94 InfoBarAndroid* old_android_bar = | 85 InfoBarAndroid* old_android_bar = (old_infobar == NULL) ? |
| 95 old_infobar != NULL ? static_cast<InfoBarAndroid*>(old_infobar) : NULL; | 86 NULL : static_cast<InfoBarAndroid*>(old_infobar); |
| 96 new_android_bar->PassJavaInfoBar(old_android_bar); | 87 new_android_bar->PassJavaInfoBar(old_android_bar); |
| 97 } | 88 } |
| 98 | 89 |
| 99 void InfoBarContainerAndroid::PlatformSpecificRemoveInfoBar(InfoBar* infobar) { | 90 void InfoBarContainerAndroid::PlatformSpecificRemoveInfoBar(InfoBar* infobar) { |
| 100 InfoBarAndroid* android_infobar = static_cast<InfoBarAndroid*>(infobar); | 91 InfoBarAndroid* android_infobar = static_cast<InfoBarAndroid*>(infobar); |
| 101 android_infobar->CloseJavaInfoBar(); | 92 android_infobar->CloseJavaInfoBar(); |
| 102 base::MessageLoop::current()->DeleteSoon(FROM_HERE, infobar); | 93 base::MessageLoop::current()->DeleteSoon(FROM_HERE, infobar); |
| 103 } | 94 } |
| 104 | 95 |
| 105 // ----------------------------------------------------------------------------- | 96 |
| 106 // Native JNI methods | 97 // Native JNI methods --------------------------------------------------------- |
| 107 // ----------------------------------------------------------------------------- | 98 |
| 108 static int Init(JNIEnv* env, | 99 static int Init(JNIEnv* env, |
| 109 jobject obj, | 100 jobject obj, |
| 110 jint native_web_contents, | 101 jint native_web_contents, |
| 111 jobject auto_login_delegate) { | 102 jobject auto_login_delegate) { |
| 112 InfoBarService* infobar_service = InfoBarService::FromWebContents( | |
| 113 reinterpret_cast<content::WebContents*>(native_web_contents)); | |
| 114 | |
| 115 InfoBarContainerAndroid* infobar_container = | 103 InfoBarContainerAndroid* infobar_container = |
| 116 new InfoBarContainerAndroid(env, obj, auto_login_delegate); | 104 new InfoBarContainerAndroid(env, obj, auto_login_delegate); |
| 117 infobar_container->ChangeInfoBarService(infobar_service); | 105 infobar_container->ChangeInfoBarService(InfoBarService::FromWebContents( |
| 106 reinterpret_cast<content::WebContents*>(native_web_contents))); |
| 118 return reinterpret_cast<int>(infobar_container); | 107 return reinterpret_cast<int>(infobar_container); |
| 119 } | 108 } |
| 120 | 109 |
| 121 // Register native methods | |
| 122 bool RegisterInfoBarContainer(JNIEnv* env) { | 110 bool RegisterInfoBarContainer(JNIEnv* env) { |
| 123 return RegisterNativesImpl(env); | 111 return RegisterNativesImpl(env); |
| 124 } | 112 } |
| OLD | NEW |