| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/bookmarks/partner_bookmarks_reader.h" | 5 #include "chrome/browser/android/bookmarks/partner_bookmarks_reader.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "chrome/browser/android/bookmarks/partner_bookmarks_shim.h" | 11 #include "chrome/browser/android/bookmarks/partner_bookmarks_shim.h" |
| 11 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/favicon/favicon_service_factory.h" | 13 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
| 16 #include "components/favicon/core/favicon_service.h" | 17 #include "components/favicon/core/favicon_service.h" |
| 17 #include "components/favicon_base/favicon_types.h" | 18 #include "components/favicon_base/favicon_types.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "jni/PartnerBookmarksReader_jni.h" | 20 #include "jni/PartnerBookmarksReader_jni.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 wip_next_available_id_(0) { | 115 wip_next_available_id_(0) { |
| 115 } | 116 } |
| 116 | 117 |
| 117 PartnerBookmarksReader::~PartnerBookmarksReader() {} | 118 PartnerBookmarksReader::~PartnerBookmarksReader() {} |
| 118 | 119 |
| 119 void PartnerBookmarksReader::PartnerBookmarksCreationComplete( | 120 void PartnerBookmarksReader::PartnerBookmarksCreationComplete( |
| 120 JNIEnv*, | 121 JNIEnv*, |
| 121 const JavaParamRef<jobject>&) { | 122 const JavaParamRef<jobject>&) { |
| 122 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 123 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 123 partner_bookmarks_shim_->SetPartnerBookmarksRoot( | 124 partner_bookmarks_shim_->SetPartnerBookmarksRoot( |
| 124 wip_partner_bookmarks_root_.release()); | 125 std::move(wip_partner_bookmarks_root_)); |
| 125 wip_next_available_id_ = 0; | 126 wip_next_available_id_ = 0; |
| 126 } | 127 } |
| 127 | 128 |
| 128 void PartnerBookmarksReader::Destroy(JNIEnv* env, | 129 void PartnerBookmarksReader::Destroy(JNIEnv* env, |
| 129 const JavaParamRef<jobject>& obj) { | 130 const JavaParamRef<jobject>& obj) { |
| 130 delete this; | 131 delete this; |
| 131 } | 132 } |
| 132 | 133 |
| 133 void PartnerBookmarksReader::Reset(JNIEnv* env, | 134 void PartnerBookmarksReader::Reset(JNIEnv* env, |
| 134 const JavaParamRef<jobject>& obj) { | 135 const JavaParamRef<jobject>& obj) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 146 jlong parent_id, | 147 jlong parent_id, |
| 147 const JavaParamRef<jbyteArray>& favicon, | 148 const JavaParamRef<jbyteArray>& favicon, |
| 148 const JavaParamRef<jbyteArray>& touchicon) { | 149 const JavaParamRef<jbyteArray>& touchicon) { |
| 149 base::string16 url; | 150 base::string16 url; |
| 150 base::string16 title; | 151 base::string16 title; |
| 151 if (jurl) | 152 if (jurl) |
| 152 url = ConvertJavaStringToUTF16(env, jurl); | 153 url = ConvertJavaStringToUTF16(env, jurl); |
| 153 if (jtitle) | 154 if (jtitle) |
| 154 title = ConvertJavaStringToUTF16(env, jtitle); | 155 title = ConvertJavaStringToUTF16(env, jtitle); |
| 155 | 156 |
| 156 BookmarkNode* node = NULL; | 157 jlong node_id = 0; |
| 157 if (wip_partner_bookmarks_root_.get()) { | 158 if (wip_partner_bookmarks_root_.get()) { |
| 158 node = new BookmarkNode(wip_next_available_id_++, GURL(url)); | 159 std::unique_ptr<BookmarkNode> node = |
| 160 base::MakeUnique<BookmarkNode>(wip_next_available_id_++, GURL(url)); |
| 159 node->set_type(is_folder ? BookmarkNode::FOLDER : BookmarkNode::URL); | 161 node->set_type(is_folder ? BookmarkNode::FOLDER : BookmarkNode::URL); |
| 160 node->SetTitle(title); | 162 node->SetTitle(title); |
| 161 | 163 |
| 162 // Handle favicon and touchicon | 164 // Handle favicon and touchicon |
| 163 if (profile_ != NULL && (favicon != NULL || touchicon != NULL)) { | 165 if (profile_ != nullptr && (favicon != nullptr || touchicon != nullptr)) { |
| 164 jbyteArray icon = (touchicon != NULL) ? touchicon : favicon; | 166 jbyteArray icon = (touchicon != nullptr) ? touchicon : favicon; |
| 165 const favicon_base::IconType icon_type = | 167 const favicon_base::IconType icon_type = |
| 166 touchicon ? favicon_base::TOUCH_ICON : favicon_base::FAVICON; | 168 touchicon ? favicon_base::TOUCH_ICON : favicon_base::FAVICON; |
| 167 const int icon_len = env->GetArrayLength(icon); | 169 const int icon_len = env->GetArrayLength(icon); |
| 168 jbyte* icon_bytes = env->GetByteArrayElements(icon, NULL); | 170 jbyte* icon_bytes = env->GetByteArrayElements(icon, nullptr); |
| 169 if (icon_bytes) | 171 if (icon_bytes) |
| 170 PrepareAndSetFavicon(env, icon_bytes, icon_len, | 172 PrepareAndSetFavicon(env, icon_bytes, icon_len, node.get(), profile_, |
| 171 node, profile_, icon_type); | 173 icon_type); |
| 172 env->ReleaseByteArrayElements(icon, icon_bytes, JNI_ABORT); | 174 env->ReleaseByteArrayElements(icon, icon_bytes, JNI_ABORT); |
| 173 } | 175 } |
| 174 | 176 |
| 175 const BookmarkNode* parent = | 177 const BookmarkNode* parent = |
| 176 GetNodeByID(wip_partner_bookmarks_root_.get(), parent_id); | 178 GetNodeByID(wip_partner_bookmarks_root_.get(), parent_id); |
| 177 if (!parent) { | 179 if (!parent) { |
| 178 LOG(WARNING) << "partner_bookmarks_shim: invalid/unknown parent_id=" | 180 LOG(WARNING) << "partner_bookmarks_shim: invalid/unknown parent_id=" |
| 179 << parent_id << ": adding to the root"; | 181 << parent_id << ": adding to the root"; |
| 180 parent = wip_partner_bookmarks_root_.get(); | 182 parent = wip_partner_bookmarks_root_.get(); |
| 181 } | 183 } |
| 182 const_cast<BookmarkNode*>(parent)->Add(node, parent->child_count()); | 184 node_id = node->id(); |
| 185 const_cast<BookmarkNode*>(parent)->Add(std::move(node), |
| 186 parent->child_count()); |
| 183 } else { | 187 } else { |
| 184 node = new BookmarkPermanentNode(wip_next_available_id_++); | 188 std::unique_ptr<BookmarkPermanentNode> node = |
| 189 base::MakeUnique<BookmarkPermanentNode>(wip_next_available_id_++); |
| 190 node_id = node->id(); |
| 185 node->SetTitle(title); | 191 node->SetTitle(title); |
| 186 wip_partner_bookmarks_root_.reset(node); | 192 wip_partner_bookmarks_root_ = std::move(node); |
| 187 } | 193 } |
| 188 return node->id(); | 194 return node_id; |
| 189 } | 195 } |
| 190 | 196 |
| 191 // static | 197 // static |
| 192 static void DisablePartnerBookmarksEditing(JNIEnv* env, | 198 static void DisablePartnerBookmarksEditing(JNIEnv* env, |
| 193 const JavaParamRef<jclass>& clazz) { | 199 const JavaParamRef<jclass>& clazz) { |
| 194 PartnerBookmarksShim::DisablePartnerBookmarksEditing(); | 200 PartnerBookmarksShim::DisablePartnerBookmarksEditing(); |
| 195 } | 201 } |
| 196 | 202 |
| 197 // static | 203 // static |
| 198 bool PartnerBookmarksReader::RegisterPartnerBookmarksReader(JNIEnv* env) { | 204 bool PartnerBookmarksReader::RegisterPartnerBookmarksReader(JNIEnv* env) { |
| 199 return RegisterNativesImpl(env); | 205 return RegisterNativesImpl(env); |
| 200 } | 206 } |
| 201 | 207 |
| 202 // ---------------------------------------------------------------- | 208 // ---------------------------------------------------------------- |
| 203 | 209 |
| 204 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 210 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 205 Profile* profile = ProfileManager::GetActiveUserProfile(); | 211 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 206 PartnerBookmarksShim* partner_bookmarks_shim = | 212 PartnerBookmarksShim* partner_bookmarks_shim = |
| 207 PartnerBookmarksShim::BuildForBrowserContext(profile); | 213 PartnerBookmarksShim::BuildForBrowserContext(profile); |
| 208 PartnerBookmarksReader* reader = new PartnerBookmarksReader( | 214 PartnerBookmarksReader* reader = new PartnerBookmarksReader( |
| 209 partner_bookmarks_shim, profile); | 215 partner_bookmarks_shim, profile); |
| 210 return reinterpret_cast<intptr_t>(reader); | 216 return reinterpret_cast<intptr_t>(reader); |
| 211 } | 217 } |
| OLD | NEW |