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 BookmarkNodeTitleCompareFunctor { | |
newt (away)
2014/09/03 01:49:51
simplify this name? maybe BookmarkTitleComparer
Kibeom Kim (inactive)
2014/09/03 06:29:44
Done.
| |
46 public: | |
47 explicit BookmarkNodeTitleCompareFunctor(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 // Note: If you use this class to get BookmarkNodeTitleCompareFunctor, then | |
64 // this factory must outlive the functor instance because this factory is | |
65 // responsible for icu::Collator lifetime that the functor depends on. | |
66 class BookmarkNodeTitleCompareFunctorFactory { | |
newt (away)
2014/09/03 00:51:50
What's the point of making this a separate class?
Kibeom Kim (inactive)
2014/09/03 00:56:00
I thought so.. and tried that first, it turned out
newt (away)
2014/09/03 01:49:51
I'd add a comment explaining this, since it's far
Kibeom Kim (inactive)
2014/09/03 06:29:44
Done.
| |
67 public: | |
68 BookmarkNodeTitleCompareFunctorFactory() { | |
69 UErrorCode error = U_ZERO_ERROR; | |
70 collator_.reset(icu::Collator::createInstance(error)); | |
71 if (U_FAILURE(error)) { | |
72 collator_.reset(NULL); | |
73 } | |
74 } | |
75 | |
76 BookmarkNodeTitleCompareFunctor GetFunctor() { | |
77 return BookmarkNodeTitleCompareFunctor(collator_.get()); | |
78 } | |
79 | |
80 private: | |
81 scoped_ptr<icu::Collator> collator_; | |
82 }; | |
83 | |
43 } // namespace | 84 } // namespace |
44 | 85 |
45 BookmarksBridge::BookmarksBridge(JNIEnv* env, | 86 BookmarksBridge::BookmarksBridge(JNIEnv* env, |
46 jobject obj, | 87 jobject obj, |
47 jobject j_profile) | 88 jobject j_profile) |
48 : weak_java_ref_(env, obj), | 89 : weak_java_ref_(env, obj), |
49 bookmark_model_(NULL), | 90 bookmark_model_(NULL), |
50 client_(NULL), | 91 client_(NULL), |
51 partner_bookmarks_shim_(NULL) { | 92 partner_bookmarks_shim_(NULL) { |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 jobject obj, | 153 jobject obj, |
113 jlong id, | 154 jlong id, |
114 jint type) { | 155 jint type) { |
115 DCHECK(IsLoaded()); | 156 DCHECK(IsLoaded()); |
116 return CreateJavaBookmark(GetNodeByID(id, type)); | 157 return CreateJavaBookmark(GetNodeByID(id, type)); |
117 } | 158 } |
118 | 159 |
119 void BookmarksBridge::GetPermanentNodeIDs(JNIEnv* env, | 160 void BookmarksBridge::GetPermanentNodeIDs(JNIEnv* env, |
120 jobject obj, | 161 jobject obj, |
121 jobject j_result_obj) { | 162 jobject j_result_obj) { |
163 // TODO(kkimlabs): Remove this function. | |
122 DCHECK(IsLoaded()); | 164 DCHECK(IsLoaded()); |
123 | 165 |
124 base::StackVector<const BookmarkNode*, 8> permanent_nodes; | 166 base::StackVector<const BookmarkNode*, 8> permanent_nodes; |
125 | 167 |
126 // Save all the permanent nodes. | 168 // Save all the permanent nodes. |
127 const BookmarkNode* root_node = bookmark_model_->root_node(); | 169 const BookmarkNode* root_node = bookmark_model_->root_node(); |
128 permanent_nodes->push_back(root_node); | 170 permanent_nodes->push_back(root_node); |
129 for (int i = 0; i < root_node->child_count(); ++i) { | 171 for (int i = 0; i < root_node->child_count(); ++i) { |
130 permanent_nodes->push_back(root_node->GetChild(i)); | 172 permanent_nodes->push_back(root_node->GetChild(i)); |
131 } | 173 } |
132 permanent_nodes->push_back( | 174 permanent_nodes->push_back( |
133 partner_bookmarks_shim_->GetPartnerBookmarksRoot()); | 175 partner_bookmarks_shim_->GetPartnerBookmarksRoot()); |
134 | 176 |
135 // Write the permanent nodes to |j_result_obj|. | 177 // Write the permanent nodes to |j_result_obj|. |
136 for (base::StackVector<const BookmarkNode*, 8>::ContainerType::const_iterator | 178 for (base::StackVector<const BookmarkNode*, 8>::ContainerType::const_iterator |
137 it = permanent_nodes->begin(); | 179 it = permanent_nodes->begin(); |
138 it != permanent_nodes->end(); | 180 it != permanent_nodes->end(); |
139 ++it) { | 181 ++it) { |
140 if (*it != NULL) { | 182 if (*it != NULL) { |
141 Java_BookmarksBridge_addToBookmarkIdList( | 183 Java_BookmarksBridge_addToBookmarkIdList( |
142 env, j_result_obj, (*it)->id(), GetBookmarkType(*it)); | 184 env, j_result_obj, (*it)->id(), GetBookmarkType(*it)); |
143 } | 185 } |
144 } | 186 } |
145 } | 187 } |
146 | 188 |
189 void BookmarksBridge::GetTopLevelFolderParentIDs(JNIEnv* env, | |
190 jobject obj, | |
191 jobject j_result_obj) { | |
192 Java_BookmarksBridge_addToBookmarkIdList( | |
193 env, j_result_obj, bookmark_model_->root_node()->id(), | |
194 GetBookmarkType(bookmark_model_->root_node())); | |
195 Java_BookmarksBridge_addToBookmarkIdList( | |
196 env, j_result_obj, bookmark_model_->mobile_node()->id(), | |
197 GetBookmarkType(bookmark_model_->mobile_node())); | |
198 Java_BookmarksBridge_addToBookmarkIdList( | |
199 env, j_result_obj, bookmark_model_->other_node()->id(), | |
200 GetBookmarkType(bookmark_model_->other_node())); | |
201 } | |
202 | |
203 void BookmarksBridge::GetTopLevelFolderIDs(JNIEnv* env, | |
204 jobject obj, | |
205 jboolean get_special, | |
206 jboolean get_normal, | |
207 jobject j_result_obj) { | |
208 DCHECK(IsLoaded()); | |
209 std::vector<const BookmarkNode*> top_level_folders; | |
210 | |
211 if (get_special) { | |
212 if (client_->managed_node() && | |
213 client_->managed_node()->child_count() > 0) { | |
214 top_level_folders.push_back(client_->managed_node()); | |
215 } | |
216 // TODO(kkimlabs): add partner bookmark root node, if available. | |
217 } | |
218 std::size_t special_count = top_level_folders.size(); | |
219 | |
220 if (get_normal) { | |
221 DCHECK_EQ(bookmark_model_->root_node()->child_count(), 4); | |
222 | |
223 top_level_folders.push_back(bookmark_model_->bookmark_bar_node()); | |
224 | |
225 const BookmarkNode* mobile_node = bookmark_model_->mobile_node(); | |
226 for (int i = 0; i < mobile_node->child_count(); ++i) { | |
227 top_level_folders.push_back(mobile_node->GetChild(i)); | |
228 } | |
229 | |
230 const BookmarkNode* other_node = bookmark_model_->other_node(); | |
231 for (int i = 0; i < other_node->child_count(); ++i) { | |
232 top_level_folders.push_back(other_node->GetChild(i)); | |
233 } | |
234 | |
235 std::stable_sort(top_level_folders.begin() + special_count, | |
236 top_level_folders.end(), | |
237 BookmarkNodeTitleCompareFunctorFactory().GetFunctor()); | |
newt (away)
2014/09/03 01:49:51
It seems you're not obeying the lifetime rules you
Kibeom Kim (inactive)
2014/09/03 06:29:44
I think it's safe because BookmarkNodeTitleCompare
| |
238 } | |
239 | |
240 for (std::vector<const BookmarkNode*>::const_iterator it = | |
241 top_level_folders.begin(); it != top_level_folders.end(); ++it) { | |
242 Java_BookmarksBridge_addToBookmarkIdList(env, | |
243 j_result_obj, | |
244 (*it)->id(), | |
245 GetBookmarkType(*it)); | |
246 } | |
247 } | |
248 | |
147 void BookmarksBridge::GetChildIDs(JNIEnv* env, | 249 void BookmarksBridge::GetChildIDs(JNIEnv* env, |
148 jobject obj, | 250 jobject obj, |
149 jlong id, | 251 jlong id, |
150 jint type, | 252 jint type, |
151 jboolean get_folders, | 253 jboolean get_folders, |
152 jboolean get_bookmarks, | 254 jboolean get_bookmarks, |
153 jobject j_result_obj) { | 255 jobject j_result_obj) { |
154 DCHECK(IsLoaded()); | 256 DCHECK(IsLoaded()); |
155 | 257 |
156 const BookmarkNode* parent = GetNodeByID(id, type); | 258 const BookmarkNode* parent = GetNodeByID(id, type); |
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
652 BookmarkModelChanged(); | 754 BookmarkModelChanged(); |
653 } | 755 } |
654 | 756 |
655 void BookmarksBridge::PartnerShimLoaded(PartnerBookmarksShim* shim) { | 757 void BookmarksBridge::PartnerShimLoaded(PartnerBookmarksShim* shim) { |
656 NotifyIfDoneLoading(); | 758 NotifyIfDoneLoading(); |
657 } | 759 } |
658 | 760 |
659 void BookmarksBridge::ShimBeingDeleted(PartnerBookmarksShim* shim) { | 761 void BookmarksBridge::ShimBeingDeleted(PartnerBookmarksShim* shim) { |
660 partner_bookmarks_shim_ = NULL; | 762 partner_bookmarks_shim_ = NULL; |
661 } | 763 } |
OLD | NEW |