OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "blimp/client/core/settings/android/blimp_settings_android.h" | 5 #include "blimp/client/core/settings/android/blimp_settings_android.h" |
6 | 6 |
7 #include "base/android/jni_string.h" | |
8 #include "base/bind.h" | |
9 #include "blimp/client/core/session/connection_status.h" | |
10 #include "blimp/client/core/settings/blimp_settings_delegate.h" | |
7 #include "blimp/client/public/blimp_client_context.h" | 11 #include "blimp/client/public/blimp_client_context.h" |
8 #include "jni/AboutBlimpPreferences_jni.h" | 12 #include "jni/AboutBlimpPreferences_jni.h" |
9 | 13 |
14 using base::android::AttachCurrentThread; | |
15 | |
10 namespace blimp { | 16 namespace blimp { |
11 namespace client { | 17 namespace client { |
12 | 18 |
13 // static | 19 // static |
14 bool BlimpSettingsAndroid::RegisterJni(JNIEnv* env) { | 20 bool BlimpSettingsAndroid::RegisterJni(JNIEnv* env) { |
15 return RegisterNativesImpl(env); | 21 return RegisterNativesImpl(env); |
16 } | 22 } |
17 | 23 |
18 // static | 24 // static |
19 BlimpSettingsAndroid* BlimpSettingsAndroid::FromJavaObject( | 25 BlimpSettingsAndroid* BlimpSettingsAndroid::FromJavaObject( |
20 JNIEnv* env, | 26 JNIEnv* env, |
21 const base::android::JavaRef<jobject>& jobj) { | 27 const base::android::JavaRef<jobject>& jobj) { |
22 return reinterpret_cast<BlimpSettingsAndroid*>( | 28 return reinterpret_cast<BlimpSettingsAndroid*>( |
23 Java_AboutBlimpPreferences_getNativePtr(env, jobj)); | 29 Java_AboutBlimpPreferences_getNativePtr(env, jobj)); |
24 } | 30 } |
25 | 31 |
26 static jlong Init(JNIEnv* env, | 32 static jlong Init(JNIEnv* env, |
27 const base::android::JavaParamRef<jobject>& jobj) { | 33 const base::android::JavaParamRef<jobject>& jobj) { |
28 return reinterpret_cast<intptr_t>(new BlimpSettingsAndroid(env, jobj)); | 34 return reinterpret_cast<intptr_t>(new BlimpSettingsAndroid(env, jobj)); |
29 } | 35 } |
30 | 36 |
31 BlimpSettingsAndroid::BlimpSettingsAndroid( | 37 BlimpSettingsAndroid::BlimpSettingsAndroid( |
32 JNIEnv* env, | 38 JNIEnv* env, |
33 const base::android::JavaRef<jobject>& jobj) | 39 const base::android::JavaRef<jobject>& jobj) |
34 : identity_source_(nullptr) { | 40 : delegate_(nullptr) { |
35 java_obj_.Reset(jobj); | 41 java_obj_.Reset(jobj); |
36 } | 42 } |
37 | 43 |
38 BlimpSettingsAndroid::~BlimpSettingsAndroid() { | 44 BlimpSettingsAndroid::~BlimpSettingsAndroid() { |
39 Java_AboutBlimpPreferences_clearNativePtr( | 45 Java_AboutBlimpPreferences_clearNativePtr( |
40 base::android::AttachCurrentThread(), java_obj_); | 46 base::android::AttachCurrentThread(), java_obj_); |
47 if (delegate_) { | |
48 delegate_->GetIdentitySource()->RemoveObserver(this); | |
49 delegate_->GetConnectionStatus()->RemoveObserver(this); | |
50 } | |
41 } | 51 } |
42 | 52 |
43 void BlimpSettingsAndroid::Destroy( | 53 void BlimpSettingsAndroid::Destroy( |
44 JNIEnv* env, | 54 JNIEnv* env, |
45 const base::android::JavaParamRef<jobject>& jobj) { | 55 const base::android::JavaParamRef<jobject>& jobj) { |
46 DCHECK(identity_source_); | |
47 identity_source_->RemoveObserver(this); | |
48 delete this; | 56 delete this; |
49 } | 57 } |
50 | 58 |
51 void BlimpSettingsAndroid::SetIdentitySource(IdentitySource* identity_source) { | 59 void BlimpSettingsAndroid::SetDelegate(BlimpSettingsDelegate* delegate) { |
52 if (identity_source_) { | 60 // Set the Delegate, it can only be called for once. |
53 identity_source_->RemoveObserver(this); | 61 DCHECK(!delegate_ && delegate); |
54 } | 62 delegate_ = delegate; |
55 | |
56 identity_source_ = identity_source; | |
57 DCHECK(identity_source_); | |
58 | 63 |
59 // Listen to sign in state change. | 64 // Listen to sign in state change. |
60 identity_source->AddObserver(this); | 65 delegate_->GetIdentitySource()->AddObserver(this); |
66 | |
67 // Listen to connection state change. | |
68 ConnectionStatus* conn_status = delegate_->GetConnectionStatus(); | |
69 DCHECK(conn_status); | |
70 conn_status->AddObserver(this); | |
71 | |
72 // Propagate connection info if the client is connected to the engine. | |
73 if (conn_status->is_connected()) { | |
74 OnConnected(); | |
75 } | |
61 } | 76 } |
62 | 77 |
63 void BlimpSettingsAndroid::OnSignedOut() { | 78 void BlimpSettingsAndroid::OnSignedOut() const { |
64 Java_AboutBlimpPreferences_onSignedOut(base::android::AttachCurrentThread(), | 79 Java_AboutBlimpPreferences_onSignedOut(base::android::AttachCurrentThread(), |
65 java_obj_); | 80 java_obj_); |
66 } | 81 } |
67 | 82 |
68 void BlimpSettingsAndroid::OnSignedIn() { | 83 void BlimpSettingsAndroid::OnSignedIn() const { |
69 Java_AboutBlimpPreferences_onSignedIn(base::android::AttachCurrentThread(), | 84 Java_AboutBlimpPreferences_onSignedIn(base::android::AttachCurrentThread(), |
70 java_obj_); | 85 java_obj_); |
71 } | 86 } |
72 | 87 |
88 void BlimpSettingsAndroid::UpdateEngineInfo() const { | |
89 DCHECK(delegate_); | |
90 | |
91 JNIEnv* env = base::android::AttachCurrentThread(); | |
92 std::string engine_id = | |
93 delegate_->GetConnectionStatus()->engine_endpoint().address().ToString(); | |
David Trainor- moved to gerrit
2016/09/14 15:49:02
I assume pulling the error message will happen in
xingliu
2016/09/14 20:44:24
Yes.
| |
94 | |
95 base::android::ScopedJavaLocalRef<jstring> jengine_id( | |
96 base::android::ConvertUTF8ToJavaString(env, engine_id)); | |
97 | |
98 Java_AboutBlimpPreferences_setEngineInfo(env, java_obj_, jengine_id); | |
99 } | |
100 | |
73 void BlimpSettingsAndroid::OnActiveAccountLogout() { | 101 void BlimpSettingsAndroid::OnActiveAccountLogout() { |
74 OnSignedOut(); | 102 OnSignedOut(); |
75 } | 103 } |
76 | 104 |
77 void BlimpSettingsAndroid::OnActiveAccountLogin() { | 105 void BlimpSettingsAndroid::OnActiveAccountLogin() { |
78 OnSignedIn(); | 106 OnSignedIn(); |
79 } | 107 } |
80 | 108 |
109 void BlimpSettingsAndroid::OnConnected() { | |
110 UpdateEngineInfo(); | |
111 } | |
112 | |
113 void BlimpSettingsAndroid::OnDisconnected(int result) { | |
114 UpdateEngineInfo(); | |
115 } | |
116 | |
81 } // namespace client | 117 } // namespace client |
82 } // namespace blimp | 118 } // namespace blimp |
OLD | NEW |