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::HasClass; | |
19 using base::android::MethodID; | |
20 using base::android::ScopedJavaLocalRef; | 18 using base::android::ScopedJavaLocalRef; |
21 using content::BrowserThread; | 19 using content::BrowserThread; |
22 | 20 |
23 namespace { | 21 namespace { |
24 | 22 |
25 SQLiteCursor::JavaColumnType ToJavaColumnType(sql::ColType type) { | 23 SQLiteCursor::JavaColumnType ToJavaColumnType(sql::ColType type) { |
26 switch (type) { | 24 switch (type) { |
27 case sql::COLUMN_TYPE_INTEGER: | 25 case sql::COLUMN_TYPE_INTEGER: |
28 return SQLiteCursor::NUMERIC; | 26 return SQLiteCursor::NUMERIC; |
29 case sql::COLUMN_TYPE_FLOAT: | 27 case sql::COLUMN_TYPE_FLOAT: |
(...skipping 18 matching lines...) Expand all Loading... |
48 | 46 |
49 SQLiteCursor::TestObserver::~TestObserver() { | 47 SQLiteCursor::TestObserver::~TestObserver() { |
50 } | 48 } |
51 | 49 |
52 ScopedJavaLocalRef<jobject> SQLiteCursor::NewJavaSqliteCursor( | 50 ScopedJavaLocalRef<jobject> SQLiteCursor::NewJavaSqliteCursor( |
53 JNIEnv* env, | 51 JNIEnv* env, |
54 const std::vector<std::string>& column_names, | 52 const std::vector<std::string>& column_names, |
55 history::AndroidStatement* statement, | 53 history::AndroidStatement* statement, |
56 AndroidHistoryProviderService* service, | 54 AndroidHistoryProviderService* service, |
57 FaviconService* favicon_service) { | 55 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, | 56 SQLiteCursor* cursor = new SQLiteCursor(column_names, statement, service, |
68 favicon_service); | 57 favicon_service); |
69 ScopedJavaLocalRef<jobject> obj(env, | 58 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 } | 59 } |
77 | 60 |
78 bool SQLiteCursor::RegisterSqliteCursor(JNIEnv* env) { | 61 bool SQLiteCursor::RegisterSqliteCursor(JNIEnv* env) { |
79 return RegisterNativesImpl(env); | 62 return RegisterNativesImpl(env); |
80 } | 63 } |
81 | 64 |
82 jint SQLiteCursor::GetCount(JNIEnv* env, jobject obj) { | 65 jint SQLiteCursor::GetCount(JNIEnv* env, jobject obj) { |
83 // Moves to maxium possible position so we will reach the last row, then finds | 66 // Moves to maxium possible position so we will reach the last row, then finds |
84 // out the total number of rows. | 67 // out the total number of rows. |
85 int current_position = position_; | 68 int current_position = position_; |
86 int count = MoveTo(env, obj, std::numeric_limits<int>::max() - 1) + 1; | 69 int count = MoveTo(env, obj, std::numeric_limits<int>::max() - 1) + 1; |
87 // Moves back to the previous position. | 70 // Moves back to the previous position. |
88 MoveTo(env, obj, current_position); | 71 MoveTo(env, obj, current_position); |
89 return count; | 72 return count; |
90 } | 73 } |
91 | 74 |
92 ScopedJavaLocalRef<jobjectArray> SQLiteCursor::GetColumnNames(JNIEnv* env, | 75 ScopedJavaLocalRef<jobjectArray> SQLiteCursor::GetColumnNames(JNIEnv* env, |
93 jobject obj) { | 76 jobject obj) { |
94 size_t count = column_names_.size(); | 77 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 } | 78 } |
105 | 79 |
106 ScopedJavaLocalRef<jstring> SQLiteCursor::GetString(JNIEnv* env, | 80 ScopedJavaLocalRef<jstring> SQLiteCursor::GetString(JNIEnv* env, |
107 jobject obj, | 81 jobject obj, |
108 jint column) { | 82 jint column) { |
109 string16 value = statement_->statement()->ColumnString16(column); | 83 string16 value = statement_->statement()->ColumnString16(column); |
110 return ScopedJavaLocalRef<jstring>(env, | 84 return ScopedJavaLocalRef<jstring>(env, |
111 env->NewString(value.data(), value.size())); | 85 env->NewString(value.data(), value.size())); |
112 } | 86 } |
113 | 87 |
(...skipping 14 matching lines...) Expand all Loading... |
128 jint column) { | 102 jint column) { |
129 std::vector<unsigned char> blob; | 103 std::vector<unsigned char> blob; |
130 | 104 |
131 // Assume the client will only get favicon using GetBlob. | 105 // Assume the client will only get favicon using GetBlob. |
132 if (statement_->favicon_index() == column) { | 106 if (statement_->favicon_index() == column) { |
133 if (!GetFavicon(statement_->statement()->ColumnInt(column), &blob)) | 107 if (!GetFavicon(statement_->statement()->ColumnInt(column), &blob)) |
134 return ScopedJavaLocalRef<jbyteArray>(); | 108 return ScopedJavaLocalRef<jbyteArray>(); |
135 } else { | 109 } else { |
136 statement_->statement()->ColumnBlobAsVector(column, &blob); | 110 statement_->statement()->ColumnBlobAsVector(column, &blob); |
137 } | 111 } |
138 ScopedJavaLocalRef<jbyteArray> jb(env, env->NewByteArray(blob.size())); | 112 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 } | 113 } |
146 | 114 |
147 jboolean SQLiteCursor::IsNull(JNIEnv* env, jobject obj, jint column) { | 115 jboolean SQLiteCursor::IsNull(JNIEnv* env, jobject obj, jint column) { |
148 return NULL_TYPE == GetColumnTypeInternal(column) ? JNI_TRUE : JNI_FALSE; | 116 return NULL_TYPE == GetColumnTypeInternal(column) ? JNI_TRUE : JNI_FALSE; |
149 } | 117 } |
150 | 118 |
151 jint SQLiteCursor::MoveTo(JNIEnv* env, jobject obj, jint pos) { | 119 jint SQLiteCursor::MoveTo(JNIEnv* env, jobject obj, jint pos) { |
152 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 120 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
153 base::Bind(&SQLiteCursor::RunMoveStatementOnUIThread, | 121 base::Bind(&SQLiteCursor::RunMoveStatementOnUIThread, |
154 base::Unretained(this), pos)); | 122 base::Unretained(this), pos)); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 } | 245 } |
278 | 246 |
279 void SQLiteCursor::RunMoveStatementOnUIThread(int pos) { | 247 void SQLiteCursor::RunMoveStatementOnUIThread(int pos) { |
280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
281 if (!consumer_.get()) | 249 if (!consumer_.get()) |
282 consumer_.reset(new CancelableRequestConsumer()); | 250 consumer_.reset(new CancelableRequestConsumer()); |
283 service_->MoveStatement( | 251 service_->MoveStatement( |
284 statement_, position_, pos, consumer_.get(), | 252 statement_, position_, pos, consumer_.get(), |
285 base::Bind(&SQLiteCursor::OnMoved, base::Unretained(this))); | 253 base::Bind(&SQLiteCursor::OnMoved, base::Unretained(this))); |
286 } | 254 } |
OLD | NEW |