| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bookmarks/bookmark_model.h" | 5 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/i18n/string_compare.h" | 12 #include "base/i18n/string_compare.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_expanded_state_tracker.h" | 14 #include "chrome/browser/bookmarks/bookmark_expanded_state_tracker.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_index.h" | 15 #include "chrome/browser/bookmarks/bookmark_index.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_model_observer.h" | 16 #include "chrome/browser/bookmarks/bookmark_model_observer.h" |
| 17 #include "chrome/browser/bookmarks/bookmark_storage.h" | 17 #include "chrome/browser/bookmarks/bookmark_storage.h" |
| 18 #include "chrome/browser/bookmarks/bookmark_utils.h" | 18 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 19 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
| 20 #include "chrome/browser/favicon/favicon_changed_details.h" | 20 #include "chrome/browser/favicon/favicon_changed_details.h" |
| 21 #include "chrome/browser/favicon/favicon_service.h" | 21 #include "chrome/browser/favicon/favicon_service.h" |
| 22 #include "chrome/browser/favicon/favicon_service_factory.h" | 22 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 23 #include "chrome/browser/history/history_service.h" | 23 #include "chrome/browser/history/history_service.h" |
| 24 #include "chrome/browser/history/history_service_factory.h" | 24 #include "chrome/browser/history/history_service_factory.h" |
| 25 #include "chrome/browser/history/url_database.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 26 #include "components/bookmarks/core/browser/bookmark_title_match.h" | 27 #include "components/bookmarks/core/browser/bookmark_title_match.h" |
| 27 #include "components/favicon_base/favicon_types.h" | 28 #include "components/favicon_base/favicon_types.h" |
| 29 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/notification_details.h" | 30 #include "content/public/browser/notification_details.h" |
| 29 #include "content/public/browser/notification_source.h" | 31 #include "content/public/browser/notification_source.h" |
| 32 #include "content/public/browser/user_metrics.h" |
| 30 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "ui/gfx/favicon_size.h" | 35 #include "ui/gfx/favicon_size.h" |
| 33 #include "ui/gfx/image/image_util.h" | 36 #include "ui/gfx/image/image_util.h" |
| 34 | 37 |
| 35 using base::Time; | 38 using base::Time; |
| 36 | 39 |
| 37 namespace { | 40 namespace { |
| 38 | 41 |
| 39 // Helper to get a mutable bookmark node. | 42 // Helper to get a mutable bookmark node. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 121 |
| 119 expanded_state_tracker_.reset( | 122 expanded_state_tracker_.reset( |
| 120 new BookmarkExpandedStateTracker(this, profile_->GetPrefs())); | 123 new BookmarkExpandedStateTracker(this, profile_->GetPrefs())); |
| 121 | 124 |
| 122 // Listen for changes to favicons so that we can update the favicon of the | 125 // Listen for changes to favicons so that we can update the favicon of the |
| 123 // node appropriately. | 126 // node appropriately. |
| 124 registrar_.Add(this, chrome::NOTIFICATION_FAVICON_CHANGED, | 127 registrar_.Add(this, chrome::NOTIFICATION_FAVICON_CHANGED, |
| 125 content::Source<Profile>(profile_)); | 128 content::Source<Profile>(profile_)); |
| 126 | 129 |
| 127 // Load the bookmarks. BookmarkStorage notifies us when done. | 130 // Load the bookmarks. BookmarkStorage notifies us when done. |
| 128 store_ = new BookmarkStorage(profile_, this, task_runner.get()); | 131 store_ = new BookmarkStorage(this, profile_->GetPath(), task_runner.get()); |
| 129 store_->LoadBookmarks(CreateLoadDetails()); | 132 store_->LoadBookmarks(this, CreateLoadDetails()); |
| 130 } | 133 } |
| 131 | 134 |
| 132 const BookmarkNode* BookmarkModel::GetParentForNewNodes() { | 135 const BookmarkNode* BookmarkModel::GetParentForNewNodes() { |
| 133 std::vector<const BookmarkNode*> nodes = | 136 std::vector<const BookmarkNode*> nodes = |
| 134 bookmark_utils::GetMostRecentlyModifiedFolders(this, 1); | 137 bookmark_utils::GetMostRecentlyModifiedFolders(this, 1); |
| 135 DCHECK(!nodes.empty()); // This list is always padded with default folders. | 138 DCHECK(!nodes.empty()); // This list is always padded with default folders. |
| 136 return nodes[0]; | 139 return nodes[0]; |
| 137 } | 140 } |
| 138 | 141 |
| 139 void BookmarkModel::AddObserver(BookmarkModelObserver* observer) { | 142 void BookmarkModel::AddObserver(BookmarkModelObserver* observer) { |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 SetDateFolderModified(node, Time()); | 617 SetDateFolderModified(node, Time()); |
| 615 } | 618 } |
| 616 | 619 |
| 617 void BookmarkModel::GetBookmarksWithTitlesMatching( | 620 void BookmarkModel::GetBookmarksWithTitlesMatching( |
| 618 const base::string16& text, | 621 const base::string16& text, |
| 619 size_t max_count, | 622 size_t max_count, |
| 620 std::vector<BookmarkTitleMatch>* matches) { | 623 std::vector<BookmarkTitleMatch>* matches) { |
| 621 if (!loaded_) | 624 if (!loaded_) |
| 622 return; | 625 return; |
| 623 | 626 |
| 624 index_->GetBookmarksWithTitlesMatching(text, max_count, matches); | 627 index_->GetBookmarksWithTitlesMatching(this, text, max_count, matches); |
| 625 } | 628 } |
| 626 | 629 |
| 627 void BookmarkModel::ClearStore() { | 630 void BookmarkModel::ClearStore() { |
| 628 registrar_.RemoveAll(); | 631 registrar_.RemoveAll(); |
| 629 store_ = NULL; | 632 store_ = NULL; |
| 630 } | 633 } |
| 631 | 634 |
| 632 void BookmarkModel::SetPermanentNodeVisible(BookmarkNode::Type type, | 635 void BookmarkModel::SetPermanentNodeVisible(BookmarkNode::Type type, |
| 633 bool value) { | 636 bool value) { |
| 634 DCHECK(loaded_); | 637 DCHECK(loaded_); |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 880 node->set_icon_url(image_result.icon_url); | 883 node->set_icon_url(image_result.icon_url); |
| 881 FaviconLoaded(node); | 884 FaviconLoaded(node); |
| 882 } | 885 } |
| 883 } | 886 } |
| 884 | 887 |
| 885 void BookmarkModel::LoadFavicon(BookmarkNode* node) { | 888 void BookmarkModel::LoadFavicon(BookmarkNode* node) { |
| 886 if (node->is_folder()) | 889 if (node->is_folder()) |
| 887 return; | 890 return; |
| 888 | 891 |
| 889 DCHECK(node->url().is_valid()); | 892 DCHECK(node->url().is_valid()); |
| 890 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( | 893 base::CancelableTaskTracker::TaskId taskId = GetFaviconImageForURL( |
| 891 profile_, Profile::EXPLICIT_ACCESS); | 894 node->url(), |
| 892 if (!favicon_service) | 895 favicon_base::FAVICON, |
| 893 return; | 896 gfx::kFaviconSize, |
| 894 base::CancelableTaskTracker::TaskId taskId = | 897 base::Bind( |
| 895 favicon_service->GetFaviconImageForURL( | 898 &BookmarkModel::OnFaviconDataAvailable, base::Unretained(this), node), |
| 896 FaviconService::FaviconForURLParams( | 899 &cancelable_task_tracker_); |
| 897 node->url(), favicon_base::FAVICON, gfx::kFaviconSize), | 900 if (taskId != base::CancelableTaskTracker::kBadTaskId) |
| 898 base::Bind(&BookmarkModel::OnFaviconDataAvailable, | 901 node->set_favicon_load_task_id(taskId); |
| 899 base::Unretained(this), | |
| 900 node), | |
| 901 &cancelable_task_tracker_); | |
| 902 node->set_favicon_load_task_id(taskId); | |
| 903 } | 902 } |
| 904 | 903 |
| 905 void BookmarkModel::FaviconLoaded(const BookmarkNode* node) { | 904 void BookmarkModel::FaviconLoaded(const BookmarkNode* node) { |
| 906 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, | 905 FOR_EACH_OBSERVER(BookmarkModelObserver, observers_, |
| 907 BookmarkNodeFaviconChanged(this, node)); | 906 BookmarkNodeFaviconChanged(this, node)); |
| 908 } | 907 } |
| 909 | 908 |
| 910 void BookmarkModel::CancelPendingFaviconLoadRequests(BookmarkNode* node) { | 909 void BookmarkModel::CancelPendingFaviconLoadRequests(BookmarkNode* node) { |
| 911 if (node->favicon_load_task_id() != base::CancelableTaskTracker::kBadTaskId) { | 910 if (node->favicon_load_task_id() != base::CancelableTaskTracker::kBadTaskId) { |
| 912 cancelable_task_tracker_.TryCancel(node->favicon_load_task_id()); | 911 cancelable_task_tracker_.TryCancel(node->favicon_load_task_id()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 return next_node_id_++; | 955 return next_node_id_++; |
| 957 } | 956 } |
| 958 | 957 |
| 959 BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { | 958 BookmarkLoadDetails* BookmarkModel::CreateLoadDetails() { |
| 960 BookmarkPermanentNode* bb_node = | 959 BookmarkPermanentNode* bb_node = |
| 961 CreatePermanentNode(BookmarkNode::BOOKMARK_BAR); | 960 CreatePermanentNode(BookmarkNode::BOOKMARK_BAR); |
| 962 BookmarkPermanentNode* other_node = | 961 BookmarkPermanentNode* other_node = |
| 963 CreatePermanentNode(BookmarkNode::OTHER_NODE); | 962 CreatePermanentNode(BookmarkNode::OTHER_NODE); |
| 964 BookmarkPermanentNode* mobile_node = | 963 BookmarkPermanentNode* mobile_node = |
| 965 CreatePermanentNode(BookmarkNode::MOBILE); | 964 CreatePermanentNode(BookmarkNode::MOBILE); |
| 966 return new BookmarkLoadDetails(bb_node, other_node, mobile_node, | 965 return new BookmarkLoadDetails( |
| 967 new BookmarkIndex(profile_), | 966 bb_node, other_node, mobile_node, new BookmarkIndex(), next_node_id_); |
| 968 next_node_id_); | |
| 969 } | 967 } |
| 968 |
| 969 base::CancelableTaskTracker::TaskId BookmarkModel::GetFaviconImageForURL( |
| 970 const GURL& page_url, |
| 971 int icon_types, |
| 972 int desired_size_in_dip, |
| 973 const FaviconImageCallback& callback, |
| 974 base::CancelableTaskTracker* tracker) { |
| 975 FaviconService* favicon_service = |
| 976 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 977 if (!favicon_service) |
| 978 return base::CancelableTaskTracker::kBadTaskId; |
| 979 return favicon_service->GetFaviconImageForURL( |
| 980 FaviconService::FaviconForURLParams( |
| 981 page_url, icon_types, desired_size_in_dip), |
| 982 callback, |
| 983 tracker); |
| 984 } |
| 985 |
| 986 void BookmarkModel::ExtractBookmarkNodePairs( |
| 987 const NodeSet& nodes, |
| 988 NodeTypedCountPairs* node_typed_count_pairs) { |
| 989 HistoryService* history_service = |
| 990 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 991 history::URLDatabase* url_db = |
| 992 history_service ? history_service->InMemoryDatabase() : NULL; |
| 993 for (NodeSet::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { |
| 994 int typed_count = 0; |
| 995 |
| 996 // If |url_db| is the InMemoryDatabase, it might not cache all URLRows, but |
| 997 // it guarantees to contain those with |typed_count| > 0. Thus, if we cannot |
| 998 // fetch the URLRow, it is safe to assume that its |typed_count| is 0. |
| 999 history::URLRow url; |
| 1000 if (url_db && url_db->GetRowForURL((*i)->url(), &url)) |
| 1001 typed_count = url.typed_count(); |
| 1002 |
| 1003 NodeTypedCountPair pair(*i, typed_count); |
| 1004 node_typed_count_pairs->push_back(pair); |
| 1005 } |
| 1006 } |
| 1007 |
| 1008 void BookmarkModel::RecordAction(const base::UserMetricsAction& action) { |
| 1009 content::RecordAction(action); |
| 1010 } |
| 1011 |
| 1012 bool BookmarkModel::PostTask(const tracked_objects::Location& from_here, |
| 1013 const base::Closure& task) { |
| 1014 return content::BrowserThread::PostTask( |
| 1015 content::BrowserThread::UI, from_here, task); |
| 1016 } |
| OLD | NEW |