Chromium Code Reviews| 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/search_engines/template_url_service_android.h" | |
| 6 | |
| 7 #include "base/android/jni_map.h" | |
| 8 #include "base/android/jni_string.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/profiles/profile_manager.h" | |
| 12 #include "chrome/browser/search_engines/template_url.h" | |
| 13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
| 14 #include "chrome/browser/search_engines/template_url_service.h" | |
| 15 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 16 #include "chrome/common/chrome_notification_types.h" | |
| 17 #include "jni/TemplateUrlService_jni.h" | |
| 18 | |
| 19 using base::android::CheckException; | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: Avoid using statements except where they save
Yaron
2013/02/21 21:39:15
Done.
| |
| 20 using base::android::ConvertMapToJavaMap; | |
| 21 using base::android::ConvertUTF16ToJavaString; | |
| 22 using base::android::GetClass; | |
| 23 using base::android::ScopedJavaLocalRef; | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const char kSearchEngineId[] = "searchEngineId"; | |
| 28 const char kSearchEngineShortName[] = "searchEngineShortName"; | |
| 29 const char kSearchEngineKeyword[] = "searchEngineKeyword"; | |
| 30 const char kSearchEngineUrl[] = "searchEngineUrl"; | |
| 31 | |
| 32 Profile* GetOriginalProfile() { | |
| 33 return g_browser_process->profile_manager()->GetDefaultProfile()-> | |
| 34 GetOriginalProfile(); | |
| 35 } | |
| 36 } // namespace | |
| 37 | |
| 38 TemplateUrlServiceAndroid::TemplateUrlServiceAndroid( | |
| 39 JNIEnv* env, jobject obj) | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: One arg per line
Yaron
2013/02/21 21:39:15
Done.
| |
| 40 : weak_java_obj_(env, obj), | |
| 41 template_url_service_( | |
| 42 TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) { | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: Indent 4, not 2
Yaron
2013/02/21 21:39:15
Done.
| |
| 43 registrar_.Add(this, chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | |
| 44 content::Source<TemplateURLService>(template_url_service_)); | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: Lines of arguments should be horizontally ali
Yaron
2013/02/21 21:39:15
Sorry, I thought that was only for function declar
| |
| 45 } | |
| 46 | |
| 47 TemplateUrlServiceAndroid::~TemplateUrlServiceAndroid() { | |
| 48 } | |
| 49 | |
| 50 void TemplateUrlServiceAndroid::Observe( | |
| 51 int type, | |
| 52 const content::NotificationSource& source, | |
| 53 const content::NotificationDetails& details) { | |
| 54 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: DCHECK_EQ
Yaron
2013/02/21 21:39:15
Done.
| |
| 55 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 56 if (weak_java_obj_.get(env).is_null()) | |
| 57 return; | |
| 58 | |
| 59 Java_TemplateUrlService_templateUrlServiceLoaded(env, | |
| 60 weak_java_obj_.get(env).obj()); | |
| 61 } | |
| 62 | |
| 63 void TemplateUrlServiceAndroid::Load(JNIEnv* env, jobject obj) { | |
| 64 if (!template_url_service_->loaded()) | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: Remove conditional; Load() will check this an
Yaron
2013/02/21 21:39:15
Done.
| |
| 65 template_url_service_->Load(); | |
| 66 } | |
| 67 | |
| 68 void TemplateUrlServiceAndroid::SetSearchEngine(JNIEnv* env, | |
| 69 jobject obj, | |
| 70 jint selected_index) { | |
| 71 std::vector<TemplateURL*> template_urls = | |
| 72 template_url_service_->GetTemplateURLs(); | |
| 73 if (selected_index < 0 || | |
| 74 selected_index >= static_cast<int>(template_urls.size())) { | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: Given that you use selected_index as a size_t
Yaron
2013/02/21 21:39:15
Done.
| |
| 75 DLOG(ERROR) << "Wrong index for search engine"; | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: In general we try to avoid logging statements
Yaron
2013/02/21 21:39:15
Converted to DCHECK.
| |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 TemplateURL* template_url = template_urls[selected_index]; | |
| 80 if (template_url->prepopulate_id() > 0) { | |
| 81 template_url_service_->SetDefaultSearchProvider(template_url); | |
| 82 } else { | |
| 83 DLOG(ERROR) << "Tried to select non-prepopulated search engine"; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 jint TemplateUrlServiceAndroid::GetSearchEngine(JNIEnv* env, jobject obj) { | |
| 88 std::vector<TemplateURL*> template_urls = | |
| 89 template_url_service_->GetTemplateURLs(); | |
| 90 for (size_t i = 0; i < template_urls.size(); ++i) { | |
| 91 if (template_url_service_->GetDefaultSearchProvider() == template_urls[i]) { | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: Move this call outside the loop, since it's n
Yaron
2013/02/21 21:39:15
Done.
| |
| 92 return i; | |
| 93 } | |
| 94 } | |
| 95 return -1; | |
| 96 } | |
| 97 | |
| 98 jboolean TemplateUrlServiceAndroid::IsLoaded(JNIEnv* env, jobject obj) { | |
| 99 return template_url_service_->loaded(); | |
| 100 } | |
| 101 | |
| 102 base::android::ScopedJavaLocalRef<jobjectArray> | |
| 103 TemplateUrlServiceAndroid::GetLocalizedSearchEngines(JNIEnv* env, jobject obj) { | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: Indent 4
Yaron
2013/02/21 21:39:15
Done.
| |
| 104 TemplateURLService::TemplateURLVector template_urls = | |
| 105 template_url_service_->GetTemplateURLs(); | |
| 106 | |
| 107 int prepopulated_urls_count = 0; | |
| 108 for (size_t i = 0; i < template_urls.size(); i++) { | |
|
bulach
2013/02/20 14:41:25
nit: ++i (and 118 too)
Yaron
2013/02/21 21:39:15
Done.
| |
| 109 if (template_urls[i]->prepopulate_id() > 0) | |
| 110 prepopulated_urls_count++; | |
| 111 } | |
| 112 | |
| 113 ScopedJavaLocalRef<jclass> hashmap_clazz = GetClass(env, "java/util/HashMap"); | |
|
Peter Kasting
2013/02/20 05:31:07
Nit: clazz?
Yaron
2013/02/21 21:39:15
Customary in java but your'e right that it's wrong
| |
| 114 jobjectArray search_engine_array = | |
| 115 env->NewObjectArray(prepopulated_urls_count, hashmap_clazz.obj(), NULL); | |
| 116 | |
| 117 int prepopulated_url_index = 0; | |
| 118 for (size_t i = 0; i < template_urls.size(); i++) { | |
| 119 TemplateURL* template_url = template_urls[i]; | |
| 120 // Only show prepopulated template URLs. | |
| 121 if (template_url->prepopulate_id() <= 0) | |
| 122 continue; | |
| 123 | |
| 124 std::stringstream ss; | |
|
Peter Kasting
2013/02/20 05:31:07
The Google style guide bans stringstreams outside
Yaron
2013/02/21 21:39:15
Done.
| |
| 125 ss << i; | |
| 126 std::map<std::string, string16> search_engine_map; | |
| 127 search_engine_map[kSearchEngineId] = UTF8ToUTF16(ss.str()); | |
| 128 search_engine_map[kSearchEngineShortName] = template_url->short_name(); | |
| 129 search_engine_map[kSearchEngineKeyword] = template_url->keyword(); | |
| 130 search_engine_map[kSearchEngineUrl] = template_url->url_ref().DisplayURL(); | |
| 131 | |
| 132 DCHECK(prepopulated_url_index < prepopulated_urls_count); | |
| 133 env->SetObjectArrayElement(search_engine_array, prepopulated_url_index, | |
| 134 ConvertMapToJavaMap(env, search_engine_map).obj()); | |
| 135 CheckException(env); | |
| 136 prepopulated_url_index++; | |
| 137 } | |
| 138 return ScopedJavaLocalRef<jobjectArray>(env, search_engine_array); | |
| 139 } | |
| 140 | |
| 141 | |
| 142 static jint Init(JNIEnv* env, jobject obj) { | |
| 143 TemplateUrlServiceAndroid* template_url_service_android = | |
| 144 new TemplateUrlServiceAndroid(env, obj); | |
| 145 return reinterpret_cast<jint>(template_url_service_android); | |
| 146 } | |
| 147 | |
| 148 // static | |
| 149 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { | |
| 150 return RegisterNativesImpl(env); | |
| 151 } | |
| OLD | NEW |