| 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_array.h" |     7 #include "base/android/jni_array.h" | 
|     8 #include "base/android/jni_string.h" |     8 #include "base/android/jni_string.h" | 
|     9 #include "base/containers/stack_container.h" |     9 #include "base/containers/stack_container.h" | 
|    10 #include "base/i18n/string_compare.h" |    10 #include "base/i18n/string_compare.h" | 
|    11 #include "base/prefs/pref_service.h" |    11 #include "base/prefs/pref_service.h" | 
|    12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |    12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 
|    13 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h" |  | 
|    14 #include "chrome/browser/bookmarks/enhanced_bookmarks_features.h" |    13 #include "chrome/browser/bookmarks/enhanced_bookmarks_features.h" | 
 |    14 #include "chrome/browser/bookmarks/managed_bookmark_service_factory.h" | 
|    15 #include "chrome/browser/profiles/incognito_helpers.h" |    15 #include "chrome/browser/profiles/incognito_helpers.h" | 
|    16 #include "chrome/browser/profiles/profile.h" |    16 #include "chrome/browser/profiles/profile.h" | 
|    17 #include "chrome/browser/profiles/profile_android.h" |    17 #include "chrome/browser/profiles/profile_android.h" | 
|    18 #include "chrome/browser/signin/signin_manager_factory.h" |    18 #include "chrome/browser/signin/signin_manager_factory.h" | 
|    19 #include "chrome/browser/undo/bookmark_undo_service_factory.h" |    19 #include "chrome/browser/undo/bookmark_undo_service_factory.h" | 
|    20 #include "chrome/common/pref_names.h" |    20 #include "chrome/common/pref_names.h" | 
|    21 #include "components/bookmarks/browser/bookmark_match.h" |    21 #include "components/bookmarks/browser/bookmark_match.h" | 
|    22 #include "components/bookmarks/browser/bookmark_model.h" |    22 #include "components/bookmarks/browser/bookmark_model.h" | 
|    23 #include "components/bookmarks/browser/bookmark_utils.h" |    23 #include "components/bookmarks/browser/bookmark_utils.h" | 
|    24 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" |    24 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" | 
|    25 #include "components/bookmarks/common/android/bookmark_type.h" |    25 #include "components/bookmarks/common/android/bookmark_type.h" | 
 |    26 #include "components/bookmarks/managed/managed_bookmark_service.h" | 
|    26 #include "components/query_parser/query_parser.h" |    27 #include "components/query_parser/query_parser.h" | 
|    27 #include "components/signin/core/browser/signin_manager.h" |    28 #include "components/signin/core/browser/signin_manager.h" | 
|    28 #include "components/undo/bookmark_undo_service.h" |    29 #include "components/undo/bookmark_undo_service.h" | 
|    29 #include "components/undo/undo_manager.h" |    30 #include "components/undo/undo_manager.h" | 
|    30 #include "content/public/browser/browser_thread.h" |    31 #include "content/public/browser/browser_thread.h" | 
|    31 #include "grit/components_strings.h" |    32 #include "grit/components_strings.h" | 
|    32 #include "jni/BookmarksBridge_jni.h" |    33 #include "jni/BookmarksBridge_jni.h" | 
|    33 #include "ui/base/l10n/l10n_util.h" |    34 #include "ui/base/l10n/l10n_util.h" | 
|    34  |    35  | 
|    35 using base::android::AttachCurrentThread; |    36 using base::android::AttachCurrentThread; | 
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|    93 BookmarksBridge::BookmarksBridge(JNIEnv* env, |    94 BookmarksBridge::BookmarksBridge(JNIEnv* env, | 
|    94                                  jobject obj, |    95                                  jobject obj, | 
|    95                                  jobject j_profile) |    96                                  jobject j_profile) | 
|    96     : weak_java_ref_(env, obj), |    97     : weak_java_ref_(env, obj), | 
|    97       bookmark_model_(NULL), |    98       bookmark_model_(NULL), | 
|    98       client_(NULL), |    99       client_(NULL), | 
|    99       partner_bookmarks_shim_(NULL) { |   100       partner_bookmarks_shim_(NULL) { | 
|   100   DCHECK_CURRENTLY_ON(BrowserThread::UI); |   101   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
|   101   profile_ = ProfileAndroid::FromProfileAndroid(j_profile); |   102   profile_ = ProfileAndroid::FromProfileAndroid(j_profile); | 
|   102   bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_); |   103   bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_); | 
|   103   client_ = ChromeBookmarkClientFactory::GetForProfile(profile_); |   104   managed_bookmark_service_ = | 
 |   105       ManagedBookmarkServiceFactory::GetForProfile(profile_); | 
|   104  |   106  | 
|   105   // Registers the notifications we are interested. |   107   // Registers the notifications we are interested. | 
|   106   bookmark_model_->AddObserver(this); |   108   bookmark_model_->AddObserver(this); | 
|   107  |   109  | 
|   108   // Create the partner Bookmarks shim as early as possible (but don't attach). |   110   // Create the partner Bookmarks shim as early as possible (but don't attach). | 
|   109   partner_bookmarks_shim_ = PartnerBookmarksShim::BuildForBrowserContext( |   111   partner_bookmarks_shim_ = PartnerBookmarksShim::BuildForBrowserContext( | 
|   110       chrome::GetBrowserContextRedirectedInIncognito(profile_)); |   112       chrome::GetBrowserContextRedirectedInIncognito(profile_)); | 
|   111   partner_bookmarks_shim_->AddObserver(this); |   113   partner_bookmarks_shim_->AddObserver(this); | 
|   112  |   114  | 
|   113   NotifyIfDoneLoading(); |   115   NotifyIfDoneLoading(); | 
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   217  |   219  | 
|   218 void BookmarksBridge::GetTopLevelFolderIDs(JNIEnv* env, |   220 void BookmarksBridge::GetTopLevelFolderIDs(JNIEnv* env, | 
|   219                                            jobject obj, |   221                                            jobject obj, | 
|   220                                            jboolean get_special, |   222                                            jboolean get_special, | 
|   221                                            jboolean get_normal, |   223                                            jboolean get_normal, | 
|   222                                            jobject j_result_obj) { |   224                                            jobject j_result_obj) { | 
|   223   DCHECK(IsLoaded()); |   225   DCHECK(IsLoaded()); | 
|   224   std::vector<const BookmarkNode*> top_level_folders; |   226   std::vector<const BookmarkNode*> top_level_folders; | 
|   225  |   227  | 
|   226   if (get_special) { |   228   if (get_special) { | 
|   227     if (client_->managed_node() && |   229     if (managed_bookmark_service_->managed_node() && | 
|   228         client_->managed_node()->child_count() > 0) { |   230         managed_bookmark_service_->managed_node()->child_count() > 0) { | 
|   229       top_level_folders.push_back(client_->managed_node()); |   231       top_level_folders.push_back(managed_bookmark_service_->managed_node()); | 
|   230     } |   232     } | 
|   231     if (client_->supervised_node() && |   233     if (managed_bookmark_service_->supervised_node() && | 
|   232         client_->supervised_node()->child_count() > 0) { |   234         managed_bookmark_service_->supervised_node()->child_count() > 0) { | 
|   233       top_level_folders.push_back(client_->supervised_node()); |   235       top_level_folders.push_back(managed_bookmark_service_->supervised_node()); | 
|   234     } |   236     } | 
|   235     if (partner_bookmarks_shim_->HasPartnerBookmarks() |   237     if (partner_bookmarks_shim_->HasPartnerBookmarks() | 
|   236         && IsReachable(partner_bookmarks_shim_->GetPartnerBookmarksRoot())) { |   238         && IsReachable(partner_bookmarks_shim_->GetPartnerBookmarksRoot())) { | 
|   237       top_level_folders.push_back( |   239       top_level_folders.push_back( | 
|   238           partner_bookmarks_shim_->GetPartnerBookmarksRoot()); |   240           partner_bookmarks_shim_->GetPartnerBookmarksRoot()); | 
|   239     } |   241     } | 
|   240   } |   242   } | 
|   241   std::size_t special_count = top_level_folders.size(); |   243   std::size_t special_count = top_level_folders.size(); | 
|   242  |   244  | 
|   243   if (get_normal) { |   245   if (get_normal) { | 
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   318     stk.pop(); |   320     stk.pop(); | 
|   319     Java_BookmarksBridge_addToBookmarkIdListWithDepth(env, |   321     Java_BookmarksBridge_addToBookmarkIdListWithDepth(env, | 
|   320                                                       j_folders_obj, |   322                                                       j_folders_obj, | 
|   321                                                       node->id(), |   323                                                       node->id(), | 
|   322                                                       GetBookmarkType(node), |   324                                                       GetBookmarkType(node), | 
|   323                                                       j_depths_obj, |   325                                                       j_depths_obj, | 
|   324                                                       depth); |   326                                                       depth); | 
|   325     bookmarkList.clear(); |   327     bookmarkList.clear(); | 
|   326     for (int i = 0; i < node->child_count(); ++i) { |   328     for (int i = 0; i < node->child_count(); ++i) { | 
|   327       const BookmarkNode* child = node->GetChild(i); |   329       const BookmarkNode* child = node->GetChild(i); | 
|   328       if (child->is_folder() && client_->CanBeEditedByUser(child)) |   330       if (child->is_folder() && | 
 |   331           managed_bookmark_service_->CanBeEditedByUser(child)) { | 
|   329         bookmarkList.push_back(node->GetChild(i)); |   332         bookmarkList.push_back(node->GetChild(i)); | 
 |   333       } | 
|   330     } |   334     } | 
|   331     std::stable_sort(bookmarkList.begin(), |   335     std::stable_sort(bookmarkList.begin(), | 
|   332                      bookmarkList.end(), |   336                      bookmarkList.end(), | 
|   333                      BookmarkTitleComparer(this, collator.get())); |   337                      BookmarkTitleComparer(this, collator.get())); | 
|   334     for (std::vector<const BookmarkNode*>::reverse_iterator it = |   338     for (std::vector<const BookmarkNode*>::reverse_iterator it = | 
|   335              bookmarkList.rbegin(); |   339              bookmarkList.rbegin(); | 
|   336          it != bookmarkList.rend(); |   340          it != bookmarkList.rend(); | 
|   337          ++it) { |   341          ++it) { | 
|   338       stk.push(std::make_pair(*it, depth + 1)); |   342       stk.push(std::make_pair(*it, depth + 1)); | 
|   339     } |   343     } | 
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   441       folder_iter != folders.end(); ++folder_iter) { |   445       folder_iter != folders.end(); ++folder_iter) { | 
|   442     if (*folder_iter == NULL) |   446     if (*folder_iter == NULL) | 
|   443       continue; |   447       continue; | 
|   444  |   448  | 
|   445     std::list<const BookmarkNode*>::iterator insert_iter = folder_iter; |   449     std::list<const BookmarkNode*>::iterator insert_iter = folder_iter; | 
|   446     ++insert_iter; |   450     ++insert_iter; | 
|   447  |   451  | 
|   448     for (int i = 0; i < (*folder_iter)->child_count(); ++i) { |   452     for (int i = 0; i < (*folder_iter)->child_count(); ++i) { | 
|   449       const BookmarkNode* child = (*folder_iter)->GetChild(i); |   453       const BookmarkNode* child = (*folder_iter)->GetChild(i); | 
|   450       if (!IsReachable(child) || |   454       if (!IsReachable(child) || | 
|   451           bookmarks::IsDescendantOf(child, client_->managed_node()) || |   455           bookmarks::IsDescendantOf( | 
|   452           bookmarks::IsDescendantOf(child, client_->supervised_node())) { |   456               child, managed_bookmark_service_->managed_node()) || | 
 |   457           bookmarks::IsDescendantOf( | 
 |   458               child, managed_bookmark_service_->supervised_node())) { | 
|   453         continue; |   459         continue; | 
|   454       } |   460       } | 
|   455  |   461  | 
|   456       if (child->is_folder()) { |   462       if (child->is_folder()) { | 
|   457         insert_iter = folders.insert(insert_iter, child); |   463         insert_iter = folders.insert(insert_iter, child); | 
|   458       } else { |   464       } else { | 
|   459         result.push_back(child); |   465         result.push_back(child); | 
|   460       } |   466       } | 
|   461     } |   467     } | 
|   462   } |   468   } | 
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   763                                           static_cast<int64>(node_id)); |   769                                           static_cast<int64>(node_id)); | 
|   764   } |   770   } | 
|   765   return node; |   771   return node; | 
|   766 } |   772 } | 
|   767  |   773  | 
|   768 const BookmarkNode* BookmarksBridge::GetFolderWithFallback(long folder_id, |   774 const BookmarkNode* BookmarksBridge::GetFolderWithFallback(long folder_id, | 
|   769                                                            int type) { |   775                                                            int type) { | 
|   770   const BookmarkNode* folder = GetNodeByID(folder_id, type); |   776   const BookmarkNode* folder = GetNodeByID(folder_id, type); | 
|   771   if (!folder || folder->type() == BookmarkNode::URL || |   777   if (!folder || folder->type() == BookmarkNode::URL || | 
|   772       !IsFolderAvailable(folder)) { |   778       !IsFolderAvailable(folder)) { | 
|   773     if (!client_->managed_node()->empty()) |   779     if (!managed_bookmark_service_->managed_node()->empty()) | 
|   774       folder = client_->managed_node(); |   780       folder = managed_bookmark_service_->managed_node(); | 
|   775     else |   781     else | 
|   776       folder = bookmark_model_->mobile_node(); |   782       folder = bookmark_model_->mobile_node(); | 
|   777   } |   783   } | 
|   778   return folder; |   784   return folder; | 
|   779 } |   785 } | 
|   780  |   786  | 
|   781 bool BookmarksBridge::IsEditable(const BookmarkNode* node) const { |   787 bool BookmarksBridge::IsEditable(const BookmarkNode* node) const { | 
|   782   if (!node || (node->type() != BookmarkNode::FOLDER && |   788   if (!node || (node->type() != BookmarkNode::FOLDER && | 
|   783       node->type() != BookmarkNode::URL)) { |   789       node->type() != BookmarkNode::URL)) { | 
|   784     return false; |   790     return false; | 
|   785   } |   791   } | 
|   786   if (!IsEditBookmarksEnabled(profile_)) |   792   if (!IsEditBookmarksEnabled(profile_)) | 
|   787     return false; |   793     return false; | 
|   788   if (partner_bookmarks_shim_->IsPartnerBookmark(node)) |   794   if (partner_bookmarks_shim_->IsPartnerBookmark(node)) | 
|   789     return partner_bookmarks_shim_->IsEditable(node); |   795     return partner_bookmarks_shim_->IsEditable(node); | 
|   790   return client_->CanBeEditedByUser(node); |   796   return managed_bookmark_service_->CanBeEditedByUser(node); | 
|   791 } |   797 } | 
|   792  |   798  | 
|   793 bool BookmarksBridge::IsManaged(const BookmarkNode* node) const { |   799 bool BookmarksBridge::IsManaged(const BookmarkNode* node) const { | 
|   794   return bookmarks::IsDescendantOf(node, client_->managed_node()); |   800   return bookmarks::IsDescendantOf(node, | 
 |   801                                    managed_bookmark_service_->managed_node()); | 
|   795 } |   802 } | 
|   796  |   803  | 
|   797 const BookmarkNode* BookmarksBridge::GetParentNode(const BookmarkNode* node) { |   804 const BookmarkNode* BookmarksBridge::GetParentNode(const BookmarkNode* node) { | 
|   798   DCHECK(IsLoaded()); |   805   DCHECK(IsLoaded()); | 
|   799   if (node == partner_bookmarks_shim_->GetPartnerBookmarksRoot()) { |   806   if (node == partner_bookmarks_shim_->GetPartnerBookmarksRoot()) { | 
|   800     return bookmark_model_->mobile_node(); |   807     return bookmark_model_->mobile_node(); | 
|   801   } else { |   808   } else { | 
|   802     return node->parent(); |   809     return node->parent(); | 
|   803   } |   810   } | 
|   804 } |   811 } | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
|   817 } |   824 } | 
|   818  |   825  | 
|   819 bool BookmarksBridge::IsLoaded() const { |   826 bool BookmarksBridge::IsLoaded() const { | 
|   820   return (bookmark_model_->loaded() && partner_bookmarks_shim_->IsLoaded()); |   827   return (bookmark_model_->loaded() && partner_bookmarks_shim_->IsLoaded()); | 
|   821 } |   828 } | 
|   822  |   829  | 
|   823 bool BookmarksBridge::IsFolderAvailable( |   830 bool BookmarksBridge::IsFolderAvailable( | 
|   824     const BookmarkNode* folder) const { |   831     const BookmarkNode* folder) const { | 
|   825   // The managed bookmarks folder is not shown if there are no bookmarks |   832   // The managed bookmarks folder is not shown if there are no bookmarks | 
|   826   // configured via policy. |   833   // configured via policy. | 
|   827   if (folder == client_->managed_node() && folder->empty()) |   834   if (folder == managed_bookmark_service_->managed_node() && folder->empty()) | 
|   828     return false; |   835     return false; | 
|   829   // Similarly, the supervised bookmarks folder is not shown if there are no |   836   // Similarly, the supervised bookmarks folder is not shown if there are no | 
|   830   // bookmarks configured by the custodian. |   837   // bookmarks configured by the custodian. | 
|   831   if (folder == client_->supervised_node() && folder->empty()) |   838   if (folder == managed_bookmark_service_->supervised_node() && folder->empty()) | 
|   832     return false; |   839     return false; | 
|   833  |   840  | 
|   834   SigninManager* signin = SigninManagerFactory::GetForProfile( |   841   SigninManager* signin = SigninManagerFactory::GetForProfile( | 
|   835       profile_->GetOriginalProfile()); |   842       profile_->GetOriginalProfile()); | 
|   836   return (folder->type() != BookmarkNode::BOOKMARK_BAR && |   843   return (folder->type() != BookmarkNode::BOOKMARK_BAR && | 
|   837       folder->type() != BookmarkNode::OTHER_NODE) || |   844       folder->type() != BookmarkNode::OTHER_NODE) || | 
|   838       (signin && signin->IsAuthenticated()); |   845       (signin && signin->IsAuthenticated()); | 
|   839 } |   846 } | 
|   840  |   847  | 
|   841 void BookmarksBridge::NotifyIfDoneLoading() { |   848 void BookmarksBridge::NotifyIfDoneLoading() { | 
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1009   BookmarkModelChanged(); |  1016   BookmarkModelChanged(); | 
|  1010 } |  1017 } | 
|  1011  |  1018  | 
|  1012 void BookmarksBridge::PartnerShimLoaded(PartnerBookmarksShim* shim) { |  1019 void BookmarksBridge::PartnerShimLoaded(PartnerBookmarksShim* shim) { | 
|  1013   NotifyIfDoneLoading(); |  1020   NotifyIfDoneLoading(); | 
|  1014 } |  1021 } | 
|  1015  |  1022  | 
|  1016 void BookmarksBridge::ShimBeingDeleted(PartnerBookmarksShim* shim) { |  1023 void BookmarksBridge::ShimBeingDeleted(PartnerBookmarksShim* shim) { | 
|  1017   partner_bookmarks_shim_ = NULL; |  1024   partner_bookmarks_shim_ = NULL; | 
|  1018 } |  1025 } | 
| OLD | NEW |