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