| 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 "chrome/browser/android/physical_web/physical_web_data_source_android.h
" | 5 #include "chrome/browser/android/physical_web/physical_web_data_source_android.h
" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" |
| 9 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 10 #include "base/values.h" | 12 #include "base/values.h" |
| 11 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 12 #include "jni/UrlManager_jni.h" | 14 #include "jni/UrlManager_jni.h" |
| 13 | 15 |
| 14 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
| 17 using base::android::ConvertJavaStringToUTF8; |
| 15 using base::android::JavaParamRef; | 18 using base::android::JavaParamRef; |
| 16 using base::android::ScopedJavaLocalRef; | 19 using base::android::ScopedJavaLocalRef; |
| 17 | 20 |
| 21 PhysicalWebCollection::PhysicalWebCollection() |
| 22 : metadata_list_(base::MakeUnique<base::ListValue>()), |
| 23 accessed_once_(false) {} |
| 24 |
| 25 PhysicalWebCollection::~PhysicalWebCollection() {} |
| 26 |
| 27 void PhysicalWebCollection::AppendMetadataItem( |
| 28 JNIEnv* env, |
| 29 const JavaParamRef<jobject>& obj, |
| 30 const JavaParamRef<jstring>& j_request_url, |
| 31 jdouble distance_estimate, |
| 32 jint scan_timestamp, |
| 33 const JavaParamRef<jstring>& j_site_url, |
| 34 const JavaParamRef<jstring>& j_icon_url, |
| 35 const JavaParamRef<jstring>& j_title, |
| 36 const JavaParamRef<jstring>& j_description, |
| 37 const JavaParamRef<jstring>& j_group_id) { |
| 38 auto metadata_item = new base::DictionaryValue(); |
| 39 metadata_item->SetString(kPhysicalWebScannedUrlKey, |
| 40 ConvertJavaStringToUTF8(j_request_url)); |
| 41 metadata_item->SetDouble(kPhysicalWebDistanceEstimateKey, distance_estimate); |
| 42 metadata_item->SetInteger(kPhysicalWebScanTimestampKey, scan_timestamp); |
| 43 metadata_item->SetString(kPhysicalWebResolvedUrlKey, |
| 44 ConvertJavaStringToUTF8(j_site_url)); |
| 45 metadata_item->SetString(kPhysicalWebIconUrlKey, |
| 46 ConvertJavaStringToUTF8(j_icon_url)); |
| 47 metadata_item->SetString(kPhysicalWebTitleKey, |
| 48 ConvertJavaStringToUTF8(j_title)); |
| 49 metadata_item->SetString(kPhysicalWebDescriptionKey, |
| 50 ConvertJavaStringToUTF8(j_description)); |
| 51 metadata_item->SetString(kPhysicalWebGroupIdKey, |
| 52 ConvertJavaStringToUTF8(j_group_id)); |
| 53 metadata_list_->Append(std::move(metadata_item)); |
| 54 } |
| 55 |
| 56 std::unique_ptr<base::ListValue> PhysicalWebCollection::GetMetadataList() { |
| 57 DCHECK(!accessed_once_); |
| 58 accessed_once_ = true; |
| 59 return std::move(metadata_list_); |
| 60 } |
| 61 |
| 18 PhysicalWebDataSourceAndroid::PhysicalWebDataSourceAndroid() { | 62 PhysicalWebDataSourceAndroid::PhysicalWebDataSourceAndroid() { |
| 19 Initialize(); | 63 Initialize(); |
| 20 } | 64 } |
| 21 | 65 |
| 22 PhysicalWebDataSourceAndroid::~PhysicalWebDataSourceAndroid() {} | 66 PhysicalWebDataSourceAndroid::~PhysicalWebDataSourceAndroid() {} |
| 23 | 67 |
| 24 void PhysicalWebDataSourceAndroid::Initialize() { | 68 void PhysicalWebDataSourceAndroid::Initialize() { |
| 25 JNIEnv* env = AttachCurrentThread(); | 69 JNIEnv* env = AttachCurrentThread(); |
| 26 | 70 |
| 27 // Cache a reference to the singleton instance of UrlManager. | 71 // Cache a reference to the singleton instance of UrlManager. |
| 28 url_manager_.Reset(Java_UrlManager_getInstance(env)); | 72 url_manager_.Reset(Java_UrlManager_getInstance(env)); |
| 29 DCHECK(url_manager_.obj()); | 73 DCHECK(url_manager_.obj()); |
| 30 } | 74 } |
| 31 | 75 |
| 32 void PhysicalWebDataSourceAndroid::StartDiscovery( | 76 void PhysicalWebDataSourceAndroid::StartDiscovery( |
| 33 bool network_request_enabled) { | 77 bool network_request_enabled) { |
| 34 // On Android, scanning is started and stopped through the Java layer. | 78 // On Android, scanning is started and stopped through the Java layer. |
| 35 NOTREACHED(); | 79 NOTREACHED(); |
| 36 } | 80 } |
| 37 | 81 |
| 38 void PhysicalWebDataSourceAndroid::StopDiscovery() { | 82 void PhysicalWebDataSourceAndroid::StopDiscovery() { |
| 39 // On Android, scanning is started and stopped through the Java layer. | 83 // On Android, scanning is started and stopped through the Java layer. |
| 40 NOTREACHED(); | 84 NOTREACHED(); |
| 41 } | 85 } |
| 42 | 86 |
| 43 std::unique_ptr<base::ListValue> PhysicalWebDataSourceAndroid::GetMetadata() { | 87 std::unique_ptr<base::ListValue> PhysicalWebDataSourceAndroid::GetMetadata() { |
| 44 // TODO(mattreynolds): get the metadata from the Java layer | 88 JNIEnv* env = AttachCurrentThread(); |
| 45 return base::MakeUnique<base::ListValue>(); | 89 |
| 90 auto pw_collection = base::MakeUnique<PhysicalWebCollection>(); |
| 91 Java_UrlManager_getPwCollection(env, url_manager_.obj(), |
| 92 (long)pw_collection.get()); |
| 93 |
| 94 return pw_collection->GetMetadataList(); |
| 46 } | 95 } |
| 47 | 96 |
| 48 bool PhysicalWebDataSourceAndroid::HasUnresolvedDiscoveries() { | 97 bool PhysicalWebDataSourceAndroid::HasUnresolvedDiscoveries() { |
| 49 NOTIMPLEMENTED(); | 98 NOTIMPLEMENTED(); |
| 50 return false; | 99 return false; |
| 51 } | 100 } |
| 52 | 101 |
| 53 // static | 102 // static |
| 54 bool PhysicalWebDataSourceAndroid::RegisterPhysicalWebDataSource(JNIEnv* env) { | 103 bool PhysicalWebDataSourceAndroid::RegisterPhysicalWebDataSource(JNIEnv* env) { |
| 55 return RegisterNativesImpl(env); | 104 return RegisterNativesImpl(env); |
| 56 } | 105 } |
| 57 | 106 |
| 58 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 107 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 59 PhysicalWebDataSource* data_source = | 108 PhysicalWebDataSource* data_source = |
| 60 g_browser_process->GetPhysicalWebDataSource(); | 109 g_browser_process->GetPhysicalWebDataSource(); |
| 61 return reinterpret_cast<intptr_t>(data_source); | 110 return reinterpret_cast<intptr_t>(data_source); |
| 62 } | 111 } |
| OLD | NEW |