OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/bookmarks_bridge.h" | 5 #include "chrome/browser/android/bookmarks/bookmarks_bridge.h" |
6 | 6 |
7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
8 #include "base/containers/stack_container.h" | 8 #include "base/containers/stack_container.h" |
9 #include "base/i18n/string_compare.h" | |
9 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
11 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h" | 12 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h" |
12 #include "chrome/browser/profiles/incognito_helpers.h" | 13 #include "chrome/browser/profiles/incognito_helpers.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/profiles/profile_android.h" | 15 #include "chrome/browser/profiles/profile_android.h" |
15 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
16 #include "chrome/browser/signin/signin_manager_factory.h" | 17 #include "chrome/browser/signin/signin_manager_factory.h" |
17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
18 #include "components/bookmarks/browser/bookmark_model.h" | 19 #include "components/bookmarks/browser/bookmark_model.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
33 using content::BrowserThread; | 34 using content::BrowserThread; |
34 | 35 |
35 namespace { | 36 namespace { |
36 | 37 |
37 class BookmarkNodeCreationTimeCompareFunctor { | 38 class BookmarkNodeCreationTimeCompareFunctor { |
38 public: | 39 public: |
39 bool operator()(const BookmarkNode* lhs, const BookmarkNode* rhs) { | 40 bool operator()(const BookmarkNode* lhs, const BookmarkNode* rhs) { |
40 return lhs->date_added().ToJavaTime() > rhs->date_added().ToJavaTime(); | 41 return lhs->date_added().ToJavaTime() > rhs->date_added().ToJavaTime(); |
41 } | 42 } |
42 }; | 43 }; |
44 | |
45 class BookmarkTitleComparer { | |
46 public: | |
47 explicit BookmarkTitleComparer(const icu::Collator* collator) | |
48 : collator_(collator) {} | |
49 | |
50 bool operator()(const BookmarkNode* lhs, const BookmarkNode* rhs) { | |
51 if (collator_) { | |
52 return base::i18n::CompareString16WithCollator( | |
53 collator_, lhs->GetTitle(), rhs->GetTitle()) == UCOL_LESS; | |
54 } else { | |
55 return lhs->GetTitle() < rhs->GetTitle(); | |
56 } | |
57 } | |
58 | |
59 private: | |
60 const icu::Collator* collator_; | |
61 }; | |
62 | |
63 // Since std::sort requires the compare functor to be copy-constructible, | |
64 // we are using this factory class to manage icu::Collator and create a thin | |
65 // copy-constructible functor, BookmarkTitleComparer. | |
66 // Note: This factory must outlive the functor instance it creates because | |
67 // this factory is responsible for icu::Collator lifetime that the functor | |
68 // depends on. | |
69 class BookmarkTitleComparerFactory { | |
70 public: | |
71 BookmarkTitleComparerFactory() { | |
72 UErrorCode error = U_ZERO_ERROR; | |
73 collator_.reset(icu::Collator::createInstance(error)); | |
74 if (U_FAILURE(error)) { | |
75 collator_.reset(NULL); | |
76 } | |
77 } | |
78 | |
79 BookmarkTitleComparer GetFunctor() { | |
80 return BookmarkTitleComparer(collator_.get()); | |
81 } | |
82 | |
83 private: | |
84 scoped_ptr<icu::Collator> collator_; | |
85 }; | |
86 | |
43 } // namespace | 87 } // namespace |
44 | 88 |
45 BookmarksBridge::BookmarksBridge(JNIEnv* env, | 89 BookmarksBridge::BookmarksBridge(JNIEnv* env, |
46 jobject obj, | 90 jobject obj, |
47 jobject j_profile) | 91 jobject j_profile) |
48 : weak_java_ref_(env, obj), | 92 : weak_java_ref_(env, obj), |
49 bookmark_model_(NULL), | 93 bookmark_model_(NULL), |
50 client_(NULL), | 94 client_(NULL), |
51 partner_bookmarks_shim_(NULL) { | 95 partner_bookmarks_shim_(NULL) { |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 jobject obj, | 156 jobject obj, |
113 jlong id, | 157 jlong id, |
114 jint type) { | 158 jint type) { |
115 DCHECK(IsLoaded()); | 159 DCHECK(IsLoaded()); |
116 return CreateJavaBookmark(GetNodeByID(id, type)); | 160 return CreateJavaBookmark(GetNodeByID(id, type)); |
117 } | 161 } |
118 | 162 |
119 void BookmarksBridge::GetPermanentNodeIDs(JNIEnv* env, | 163 void BookmarksBridge::GetPermanentNodeIDs(JNIEnv* env, |
120 jobject obj, | 164 jobject obj, |
121 jobject j_result_obj) { | 165 jobject j_result_obj) { |
166 // TODO(kkimlabs): Remove this function. | |
122 DCHECK(IsLoaded()); | 167 DCHECK(IsLoaded()); |
123 | 168 |
124 base::StackVector<const BookmarkNode*, 8> permanent_nodes; | 169 base::StackVector<const BookmarkNode*, 8> permanent_nodes; |
125 | 170 |
126 // Save all the permanent nodes. | 171 // Save all the permanent nodes. |
127 const BookmarkNode* root_node = bookmark_model_->root_node(); | 172 const BookmarkNode* root_node = bookmark_model_->root_node(); |
128 permanent_nodes->push_back(root_node); | 173 permanent_nodes->push_back(root_node); |
129 for (int i = 0; i < root_node->child_count(); ++i) { | 174 for (int i = 0; i < root_node->child_count(); ++i) { |
130 permanent_nodes->push_back(root_node->GetChild(i)); | 175 permanent_nodes->push_back(root_node->GetChild(i)); |
131 } | 176 } |
132 permanent_nodes->push_back( | 177 permanent_nodes->push_back( |
133 partner_bookmarks_shim_->GetPartnerBookmarksRoot()); | 178 partner_bookmarks_shim_->GetPartnerBookmarksRoot()); |
134 | 179 |
135 // Write the permanent nodes to |j_result_obj|. | 180 // Write the permanent nodes to |j_result_obj|. |
136 for (base::StackVector<const BookmarkNode*, 8>::ContainerType::const_iterator | 181 for (base::StackVector<const BookmarkNode*, 8>::ContainerType::const_iterator |
137 it = permanent_nodes->begin(); | 182 it = permanent_nodes->begin(); |
138 it != permanent_nodes->end(); | 183 it != permanent_nodes->end(); |
139 ++it) { | 184 ++it) { |
140 if (*it != NULL) { | 185 if (*it != NULL) { |
141 Java_BookmarksBridge_addToBookmarkIdList( | 186 Java_BookmarksBridge_addToBookmarkIdList( |
142 env, j_result_obj, (*it)->id(), GetBookmarkType(*it)); | 187 env, j_result_obj, (*it)->id(), GetBookmarkType(*it)); |
143 } | 188 } |
144 } | 189 } |
145 } | 190 } |
146 | 191 |
192 void BookmarksBridge::GetTopLevelFolderParentIDs(JNIEnv* env, | |
193 jobject obj, | |
194 jobject j_result_obj) { | |
195 Java_BookmarksBridge_addToBookmarkIdList( | |
196 env, j_result_obj, bookmark_model_->root_node()->id(), | |
197 GetBookmarkType(bookmark_model_->root_node())); | |
198 Java_BookmarksBridge_addToBookmarkIdList( | |
199 env, j_result_obj, bookmark_model_->mobile_node()->id(), | |
200 GetBookmarkType(bookmark_model_->mobile_node())); | |
201 Java_BookmarksBridge_addToBookmarkIdList( | |
202 env, j_result_obj, bookmark_model_->other_node()->id(), | |
Ted C
2014/09/03 17:43:17
-2 indent for these two lines
Kibeom Kim (inactive)
2014/09/03 18:15:01
Done.
| |
203 GetBookmarkType(bookmark_model_->other_node())); | |
204 } | |
205 | |
206 void BookmarksBridge::GetTopLevelFolderIDs(JNIEnv* env, | |
207 jobject obj, | |
208 jboolean get_special, | |
209 jboolean get_normal, | |
210 jobject j_result_obj) { | |
211 DCHECK(IsLoaded()); | |
212 std::vector<const BookmarkNode*> top_level_folders; | |
213 | |
214 if (get_special) { | |
215 if (client_->managed_node() && | |
216 client_->managed_node()->child_count() > 0) { | |
217 top_level_folders.push_back(client_->managed_node()); | |
218 } | |
219 // TODO(kkimlabs): add partner bookmark root node, if available. | |
Ted C
2014/09/03 17:43:17
I think this should just be:
if (partner_bookmark
Kibeom Kim (inactive)
2014/09/03 18:15:02
Done.
| |
220 } | |
221 std::size_t special_count = top_level_folders.size(); | |
222 | |
223 if (get_normal) { | |
224 DCHECK_EQ(bookmark_model_->root_node()->child_count(), 4); | |
225 | |
226 top_level_folders.push_back(bookmark_model_->bookmark_bar_node()); | |
Ted C
2014/09/03 17:43:17
why the root node in the above function and the bo
Kibeom Kim (inactive)
2014/09/03 18:15:01
You mean the DCHECK? I just added for making sure
| |
227 | |
228 const BookmarkNode* mobile_node = bookmark_model_->mobile_node(); | |
229 for (int i = 0; i < mobile_node->child_count(); ++i) { | |
230 top_level_folders.push_back(mobile_node->GetChild(i)); | |
Ted C
2014/09/03 17:43:17
What if the child isn't a folder? same below?
I
Kibeom Kim (inactive)
2014/09/03 18:15:01
Actually... I needed a folder check here. When I w
| |
231 } | |
232 | |
233 const BookmarkNode* other_node = bookmark_model_->other_node(); | |
234 for (int i = 0; i < other_node->child_count(); ++i) { | |
235 top_level_folders.push_back(other_node->GetChild(i)); | |
236 } | |
237 | |
238 std::stable_sort(top_level_folders.begin() + special_count, | |
239 top_level_folders.end(), | |
240 BookmarkTitleComparerFactory().GetFunctor()); | |
241 } | |
242 | |
243 for (std::vector<const BookmarkNode*>::const_iterator it = | |
244 top_level_folders.begin(); it != top_level_folders.end(); ++it) { | |
245 Java_BookmarksBridge_addToBookmarkIdList(env, | |
246 j_result_obj, | |
247 (*it)->id(), | |
248 GetBookmarkType(*it)); | |
249 } | |
250 } | |
251 | |
147 void BookmarksBridge::GetChildIDs(JNIEnv* env, | 252 void BookmarksBridge::GetChildIDs(JNIEnv* env, |
148 jobject obj, | 253 jobject obj, |
149 jlong id, | 254 jlong id, |
150 jint type, | 255 jint type, |
151 jboolean get_folders, | 256 jboolean get_folders, |
152 jboolean get_bookmarks, | 257 jboolean get_bookmarks, |
153 jobject j_result_obj) { | 258 jobject j_result_obj) { |
154 DCHECK(IsLoaded()); | 259 DCHECK(IsLoaded()); |
155 | 260 |
156 const BookmarkNode* parent = GetNodeByID(id, type); | 261 const BookmarkNode* parent = GetNodeByID(id, type); |
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
652 BookmarkModelChanged(); | 757 BookmarkModelChanged(); |
653 } | 758 } |
654 | 759 |
655 void BookmarksBridge::PartnerShimLoaded(PartnerBookmarksShim* shim) { | 760 void BookmarksBridge::PartnerShimLoaded(PartnerBookmarksShim* shim) { |
656 NotifyIfDoneLoading(); | 761 NotifyIfDoneLoading(); |
657 } | 762 } |
658 | 763 |
659 void BookmarksBridge::ShimBeingDeleted(PartnerBookmarksShim* shim) { | 764 void BookmarksBridge::ShimBeingDeleted(PartnerBookmarksShim* shim) { |
660 partner_bookmarks_shim_ = NULL; | 765 partner_bookmarks_shim_ = NULL; |
661 } | 766 } |
OLD | NEW |