Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/history/android/sqlite_cursor.h" | 5 #include "chrome/browser/history/android/sqlite_cursor.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | |
| 8 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "chrome/browser/history/android/android_history_types.h" | 12 #include "chrome/browser/history/android/android_history_types.h" |
| 12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 13 #include "jni/SQLiteCursor_jni.h" | 14 #include "jni/SQLiteCursor_jni.h" |
| 14 #include "sql/statement.h" | 15 #include "sql/statement.h" |
| 15 | 16 |
| 16 using base::android::ConvertUTF8ToJavaString; | 17 using base::android::ConvertUTF8ToJavaString; |
| 17 using base::android::GetClass; | 18 using base::android::GetClass; |
|
Yaron
2013/03/13 22:17:51
Please remove these 3
qinmin
2013/03/14 18:53:03
Done.
| |
| 18 using base::android::HasClass; | 19 using base::android::HasClass; |
| 19 using base::android::MethodID; | 20 using base::android::MethodID; |
| 20 using base::android::ScopedJavaLocalRef; | 21 using base::android::ScopedJavaLocalRef; |
| 21 using content::BrowserThread; | 22 using content::BrowserThread; |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 SQLiteCursor::JavaColumnType ToJavaColumnType(sql::ColType type) { | 26 SQLiteCursor::JavaColumnType ToJavaColumnType(sql::ColType type) { |
| 26 switch (type) { | 27 switch (type) { |
| 27 case sql::COLUMN_TYPE_INTEGER: | 28 case sql::COLUMN_TYPE_INTEGER: |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 48 | 49 |
| 49 SQLiteCursor::TestObserver::~TestObserver() { | 50 SQLiteCursor::TestObserver::~TestObserver() { |
| 50 } | 51 } |
| 51 | 52 |
| 52 ScopedJavaLocalRef<jobject> SQLiteCursor::NewJavaSqliteCursor( | 53 ScopedJavaLocalRef<jobject> SQLiteCursor::NewJavaSqliteCursor( |
| 53 JNIEnv* env, | 54 JNIEnv* env, |
| 54 const std::vector<std::string>& column_names, | 55 const std::vector<std::string>& column_names, |
| 55 history::AndroidStatement* statement, | 56 history::AndroidStatement* statement, |
| 56 AndroidHistoryProviderService* service, | 57 AndroidHistoryProviderService* service, |
| 57 FaviconService* favicon_service) { | 58 FaviconService* favicon_service) { |
| 58 if (!HasClass(env, kSQLiteCursorClassPath)) { | |
| 59 LOG(ERROR) << "Can not find " << kSQLiteCursorClassPath; | |
| 60 return ScopedJavaLocalRef<jobject>(); | |
| 61 } | |
| 62 | |
| 63 ScopedJavaLocalRef<jclass> sclass = GetClass(env, kSQLiteCursorClassPath); | |
| 64 jmethodID method_id = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
| 65 env, sclass.obj(), "<init>", "(I)V"); | |
| 66 | |
| 67 SQLiteCursor* cursor = new SQLiteCursor(column_names, statement, service, | 59 SQLiteCursor* cursor = new SQLiteCursor(column_names, statement, service, |
| 68 favicon_service); | 60 favicon_service); |
| 69 ScopedJavaLocalRef<jobject> obj(env, | 61 return Java_SQLiteCursor_create(env, reinterpret_cast<jint>(cursor)); |
| 70 env->NewObject(sclass.obj(), method_id, reinterpret_cast<jint>(cursor))); | |
| 71 if (obj.is_null()) { | |
| 72 delete cursor; | |
| 73 return ScopedJavaLocalRef<jobject>(); | |
| 74 } | |
| 75 return obj; | |
| 76 } | 62 } |
| 77 | 63 |
| 78 bool SQLiteCursor::RegisterSqliteCursor(JNIEnv* env) { | 64 bool SQLiteCursor::RegisterSqliteCursor(JNIEnv* env) { |
| 79 return RegisterNativesImpl(env); | 65 return RegisterNativesImpl(env); |
| 80 } | 66 } |
| 81 | 67 |
| 82 jint SQLiteCursor::GetCount(JNIEnv* env, jobject obj) { | 68 jint SQLiteCursor::GetCount(JNIEnv* env, jobject obj) { |
| 83 // Moves to maxium possible position so we will reach the last row, then finds | 69 // Moves to maxium possible position so we will reach the last row, then finds |
| 84 // out the total number of rows. | 70 // out the total number of rows. |
| 85 int current_position = position_; | 71 int current_position = position_; |
| 86 int count = MoveTo(env, obj, std::numeric_limits<int>::max() - 1) + 1; | 72 int count = MoveTo(env, obj, std::numeric_limits<int>::max() - 1) + 1; |
| 87 // Moves back to the previous position. | 73 // Moves back to the previous position. |
| 88 MoveTo(env, obj, current_position); | 74 MoveTo(env, obj, current_position); |
| 89 return count; | 75 return count; |
| 90 } | 76 } |
| 91 | 77 |
| 92 ScopedJavaLocalRef<jobjectArray> SQLiteCursor::GetColumnNames(JNIEnv* env, | 78 ScopedJavaLocalRef<jobjectArray> SQLiteCursor::GetColumnNames(JNIEnv* env, |
| 93 jobject obj) { | 79 jobject obj) { |
| 94 size_t count = column_names_.size(); | 80 return base::android::ToJavaArrayOfStrings(env, column_names_); |
| 95 ScopedJavaLocalRef<jclass> sclass = GetClass(env, "java/lang/String"); | |
| 96 ScopedJavaLocalRef<jobjectArray> arr(env, | |
| 97 env->NewObjectArray(count, sclass.obj(), NULL)); | |
| 98 for (size_t i = 0; i < count; i++) { | |
| 99 ScopedJavaLocalRef<jstring> str = | |
| 100 ConvertUTF8ToJavaString(env, column_names_[i].c_str()); | |
| 101 env->SetObjectArrayElement(arr.obj(), i, str.obj()); | |
| 102 } | |
| 103 return arr; | |
| 104 } | 81 } |
| 105 | 82 |
| 106 ScopedJavaLocalRef<jstring> SQLiteCursor::GetString(JNIEnv* env, | 83 ScopedJavaLocalRef<jstring> SQLiteCursor::GetString(JNIEnv* env, |
| 107 jobject obj, | 84 jobject obj, |
| 108 jint column) { | 85 jint column) { |
| 109 string16 value = statement_->statement()->ColumnString16(column); | 86 string16 value = statement_->statement()->ColumnString16(column); |
| 110 return ScopedJavaLocalRef<jstring>(env, | 87 return ScopedJavaLocalRef<jstring>(env, |
| 111 env->NewString(value.data(), value.size())); | 88 env->NewString(value.data(), value.size())); |
| 112 } | 89 } |
| 113 | 90 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 128 jint column) { | 105 jint column) { |
| 129 std::vector<unsigned char> blob; | 106 std::vector<unsigned char> blob; |
| 130 | 107 |
| 131 // Assume the client will only get favicon using GetBlob. | 108 // Assume the client will only get favicon using GetBlob. |
| 132 if (statement_->favicon_index() == column) { | 109 if (statement_->favicon_index() == column) { |
| 133 if (!GetFavicon(statement_->statement()->ColumnInt(column), &blob)) | 110 if (!GetFavicon(statement_->statement()->ColumnInt(column), &blob)) |
| 134 return ScopedJavaLocalRef<jbyteArray>(); | 111 return ScopedJavaLocalRef<jbyteArray>(); |
| 135 } else { | 112 } else { |
| 136 statement_->statement()->ColumnBlobAsVector(column, &blob); | 113 statement_->statement()->ColumnBlobAsVector(column, &blob); |
| 137 } | 114 } |
| 138 ScopedJavaLocalRef<jbyteArray> jb(env, env->NewByteArray(blob.size())); | 115 return base::android::ToJavaByteArray(env, &blob[0], blob.size()); |
| 139 int count = 0; | |
| 140 for (std::vector<unsigned char>::const_iterator i = blob.begin(); | |
| 141 i != blob.end(); ++i) { | |
| 142 env->SetByteArrayRegion(jb.obj(), count++, 1, (jbyte *)i); | |
| 143 } | |
| 144 return jb; | |
| 145 } | 116 } |
| 146 | 117 |
| 147 jboolean SQLiteCursor::IsNull(JNIEnv* env, jobject obj, jint column) { | 118 jboolean SQLiteCursor::IsNull(JNIEnv* env, jobject obj, jint column) { |
| 148 return NULL_TYPE == GetColumnTypeInternal(column) ? JNI_TRUE : JNI_FALSE; | 119 return NULL_TYPE == GetColumnTypeInternal(column) ? JNI_TRUE : JNI_FALSE; |
| 149 } | 120 } |
| 150 | 121 |
| 151 jint SQLiteCursor::MoveTo(JNIEnv* env, jobject obj, jint pos) { | 122 jint SQLiteCursor::MoveTo(JNIEnv* env, jobject obj, jint pos) { |
| 152 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 123 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 153 base::Bind(&SQLiteCursor::RunMoveStatementOnUIThread, | 124 base::Bind(&SQLiteCursor::RunMoveStatementOnUIThread, |
| 154 base::Unretained(this), pos)); | 125 base::Unretained(this), pos)); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 } | 248 } |
| 278 | 249 |
| 279 void SQLiteCursor::RunMoveStatementOnUIThread(int pos) { | 250 void SQLiteCursor::RunMoveStatementOnUIThread(int pos) { |
| 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 281 if (!consumer_.get()) | 252 if (!consumer_.get()) |
| 282 consumer_.reset(new CancelableRequestConsumer()); | 253 consumer_.reset(new CancelableRequestConsumer()); |
| 283 service_->MoveStatement( | 254 service_->MoveStatement( |
| 284 statement_, position_, pos, consumer_.get(), | 255 statement_, position_, pos, consumer_.get(), |
| 285 base::Bind(&SQLiteCursor::OnMoved, base::Unretained(this))); | 256 base::Bind(&SQLiteCursor::OnMoved, base::Unretained(this))); |
| 286 } | 257 } |
| OLD | NEW |