| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chrome_bookmark_client.h" | 5 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/favicon/favicon_service.h" | 11 #include "chrome/browser/favicon/favicon_service.h" |
| 12 #include "chrome/browser/favicon/favicon_service_factory.h" | 12 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 13 #include "chrome/browser/history/history_service.h" | 13 #include "chrome/browser/history/history_service.h" |
| 14 #include "chrome/browser/policy/profile_policy_connector.h" | 14 #include "chrome/browser/policy/profile_policy_connector.h" |
| 15 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 15 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "components/bookmarks/browser/bookmark_model.h" | 17 #include "components/bookmarks/browser/bookmark_model.h" |
| 18 #include "components/bookmarks/browser/bookmark_node.h" | 18 #include "components/bookmarks/browser/bookmark_node.h" |
| 19 #include "components/bookmarks/browser/bookmark_utils.h" |
| 19 #include "components/history/core/browser/url_database.h" | 20 #include "components/history/core/browser/url_database.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 22 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
| 23 #include "content/public/browser/user_metrics.h" | 24 #include "content/public/browser/user_metrics.h" |
| 24 #include "grit/components_strings.h" | 25 #include "grit/components_strings.h" |
| 25 #include "policy/policy_constants.h" | 26 #include "policy/policy_constants.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 27 | 28 |
| 28 using bookmarks::BookmarkModel; | 29 using bookmarks::BookmarkModel; |
| 29 using bookmarks::BookmarkNode; | 30 using bookmarks::BookmarkNode; |
| 30 using bookmarks::BookmarkPermanentNode; | 31 using bookmarks::BookmarkPermanentNode; |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 void RunCallbackWithImage( | 35 void RunCallbackWithImage( |
| 35 const favicon_base::FaviconImageCallback& callback, | 36 const favicon_base::FaviconImageCallback& callback, |
| 36 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 37 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 37 favicon_base::FaviconImageResult result; | 38 favicon_base::FaviconImageResult result; |
| 38 if (bitmap_result.is_valid()) { | 39 if (bitmap_result.is_valid()) { |
| 39 result.image = gfx::Image::CreateFrom1xPNGBytes( | 40 result.image = gfx::Image::CreateFrom1xPNGBytes( |
| 40 bitmap_result.bitmap_data->front(), bitmap_result.bitmap_data->size()); | 41 bitmap_result.bitmap_data->front(), bitmap_result.bitmap_data->size()); |
| 41 result.icon_url = bitmap_result.icon_url; | 42 result.icon_url = bitmap_result.icon_url; |
| 42 callback.Run(result); | 43 callback.Run(result); |
| 43 return; | 44 return; |
| 44 } | 45 } |
| 45 callback.Run(result); | 46 callback.Run(result); |
| 46 } | 47 } |
| 47 | 48 |
| 49 void LoadInitialContents(BookmarkPermanentNode* node, |
| 50 base::ListValue* initial_bookmarks, |
| 51 int64* next_node_id) { |
| 52 // Load the initial contents of the |node| now, and assign it an unused ID. |
| 53 int64 id = *next_node_id; |
| 54 node->set_id(id); |
| 55 *next_node_id = policy::ManagedBookmarksTracker::LoadInitial( |
| 56 node, initial_bookmarks, id + 1); |
| 57 node->set_visible(!node->empty()); |
| 58 } |
| 59 |
| 48 } // namespace | 60 } // namespace |
| 49 | 61 |
| 50 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile) | 62 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile) |
| 51 : profile_(profile), | 63 : profile_(profile), |
| 52 history_service_(NULL), | 64 history_service_(NULL), |
| 53 model_(NULL), | 65 model_(NULL), |
| 54 managed_node_(NULL) { | 66 managed_node_(NULL), |
| 67 supervised_node_(NULL) { |
| 55 } | 68 } |
| 56 | 69 |
| 57 ChromeBookmarkClient::~ChromeBookmarkClient() { | 70 ChromeBookmarkClient::~ChromeBookmarkClient() { |
| 58 } | 71 } |
| 59 | 72 |
| 60 void ChromeBookmarkClient::Init(BookmarkModel* model) { | 73 void ChromeBookmarkClient::Init(BookmarkModel* model) { |
| 61 DCHECK(model); | 74 DCHECK(model); |
| 62 DCHECK(!model_); | 75 DCHECK(!model_); |
| 63 model_ = model; | 76 model_ = model; |
| 64 model_->AddObserver(this); | 77 model_->AddObserver(this); |
| 65 | 78 |
| 66 managed_bookmarks_tracker_.reset(new policy::ManagedBookmarksTracker( | 79 managed_bookmarks_tracker_.reset(new policy::ManagedBookmarksTracker( |
| 67 model_, | 80 model_, |
| 68 profile_->GetPrefs(), | 81 profile_->GetPrefs(), |
| 82 false, |
| 69 base::Bind(&ChromeBookmarkClient::GetManagedBookmarksDomain, | 83 base::Bind(&ChromeBookmarkClient::GetManagedBookmarksDomain, |
| 70 base::Unretained(this)))); | 84 base::Unretained(this)))); |
| 85 supervised_bookmarks_tracker_.reset(new policy::ManagedBookmarksTracker( |
| 86 model_, |
| 87 profile_->GetPrefs(), |
| 88 true, |
| 89 base::Callback<std::string()>())); |
| 71 } | 90 } |
| 72 | 91 |
| 73 void ChromeBookmarkClient::Shutdown() { | 92 void ChromeBookmarkClient::Shutdown() { |
| 74 favicon_changed_subscription_.reset(); | 93 favicon_changed_subscription_.reset(); |
| 75 if (model_) { | 94 if (model_) { |
| 76 model_->RemoveObserver(this); | 95 model_->RemoveObserver(this); |
| 77 model_ = NULL; | 96 model_ = NULL; |
| 78 } | 97 } |
| 79 BookmarkClient::Shutdown(); | 98 BookmarkClient::Shutdown(); |
| 80 } | 99 } |
| 81 | 100 |
| 82 bool ChromeBookmarkClient::IsDescendantOfManagedNode(const BookmarkNode* node) { | |
| 83 return node && node->HasAncestor(managed_node_); | |
| 84 } | |
| 85 | |
| 86 bool ChromeBookmarkClient::HasDescendantsOfManagedNode( | |
| 87 const std::vector<const BookmarkNode*>& list) { | |
| 88 for (size_t i = 0; i < list.size(); ++i) { | |
| 89 if (IsDescendantOfManagedNode(list[i])) | |
| 90 return true; | |
| 91 } | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 bool ChromeBookmarkClient::PreferTouchIcon() { | 101 bool ChromeBookmarkClient::PreferTouchIcon() { |
| 96 #if !defined(OS_IOS) | 102 #if !defined(OS_IOS) |
| 97 return false; | 103 return false; |
| 98 #else | 104 #else |
| 99 return true; | 105 return true; |
| 100 #endif | 106 #endif |
| 101 } | 107 } |
| 102 | 108 |
| 103 base::CancelableTaskTracker::TaskId | 109 base::CancelableTaskTracker::TaskId |
| 104 ChromeBookmarkClient::GetFaviconImageForPageURL( | 110 ChromeBookmarkClient::GetFaviconImageForPageURL( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 NodeTypedCountPair pair(*i, typed_count); | 151 NodeTypedCountPair pair(*i, typed_count); |
| 146 node_typed_count_pairs->push_back(pair); | 152 node_typed_count_pairs->push_back(pair); |
| 147 } | 153 } |
| 148 } | 154 } |
| 149 | 155 |
| 150 bool ChromeBookmarkClient::IsPermanentNodeVisible( | 156 bool ChromeBookmarkClient::IsPermanentNodeVisible( |
| 151 const BookmarkPermanentNode* node) { | 157 const BookmarkPermanentNode* node) { |
| 152 DCHECK(node->type() == BookmarkNode::BOOKMARK_BAR || | 158 DCHECK(node->type() == BookmarkNode::BOOKMARK_BAR || |
| 153 node->type() == BookmarkNode::OTHER_NODE || | 159 node->type() == BookmarkNode::OTHER_NODE || |
| 154 node->type() == BookmarkNode::MOBILE || | 160 node->type() == BookmarkNode::MOBILE || |
| 155 node == managed_node_); | 161 node == managed_node_ || |
| 156 if (node == managed_node_) | 162 node == supervised_node_); |
| 163 if (node == managed_node_ || node == supervised_node_) |
| 157 return false; | 164 return false; |
| 158 #if !defined(OS_IOS) | 165 #if !defined(OS_IOS) |
| 159 return node->type() != BookmarkNode::MOBILE; | 166 return node->type() != BookmarkNode::MOBILE; |
| 160 #else | 167 #else |
| 161 return node->type() == BookmarkNode::MOBILE; | 168 return node->type() == BookmarkNode::MOBILE; |
| 162 #endif | 169 #endif |
| 163 } | 170 } |
| 164 | 171 |
| 165 void ChromeBookmarkClient::RecordAction(const base::UserMetricsAction& action) { | 172 void ChromeBookmarkClient::RecordAction(const base::UserMetricsAction& action) { |
| 166 content::RecordAction(action); | 173 content::RecordAction(action); |
| 167 } | 174 } |
| 168 | 175 |
| 169 bookmarks::LoadExtraCallback ChromeBookmarkClient::GetLoadExtraNodesCallback() { | 176 bookmarks::LoadExtraCallback ChromeBookmarkClient::GetLoadExtraNodesCallback() { |
| 170 // Create the managed_node now; it will be populated in the LoadExtraNodes | 177 // Create the managed_node_ and supervised_node_ with a temporary ID of 0 now. |
| 178 // They will be populated (and assigned proper IDs) in the LoadExtraNodes |
| 171 // callback. | 179 // callback. |
| 172 // The ownership of managed_node_ is in limbo until LoadExtraNodes runs, | 180 // The ownership of managed_node_ and supervised_node_ is in limbo until |
| 173 // so we leave it in the care of the closure meanwhile. | 181 // LoadExtraNodes runs, so we leave them in the care of the closure meanwhile. |
| 174 scoped_ptr<BookmarkPermanentNode> managed(new BookmarkPermanentNode(0)); | 182 scoped_ptr<BookmarkPermanentNode> managed(new BookmarkPermanentNode(0)); |
| 175 managed_node_ = managed.get(); | 183 managed_node_ = managed.get(); |
| 184 scoped_ptr<BookmarkPermanentNode> supervised(new BookmarkPermanentNode(0)); |
| 185 supervised_node_ = supervised.get(); |
| 176 | 186 |
| 177 return base::Bind( | 187 return base::Bind( |
| 178 &ChromeBookmarkClient::LoadExtraNodes, | 188 &ChromeBookmarkClient::LoadExtraNodes, |
| 179 base::Passed(&managed), | 189 base::Passed(&managed), |
| 180 base::Passed(managed_bookmarks_tracker_->GetInitialManagedBookmarks())); | 190 base::Passed(managed_bookmarks_tracker_->GetInitialManagedBookmarks()), |
| 191 base::Passed(&supervised), |
| 192 base::Passed( |
| 193 supervised_bookmarks_tracker_->GetInitialManagedBookmarks())); |
| 181 } | 194 } |
| 182 | 195 |
| 183 bool ChromeBookmarkClient::CanSetPermanentNodeTitle( | 196 bool ChromeBookmarkClient::CanSetPermanentNodeTitle( |
| 184 const BookmarkNode* permanent_node) { | 197 const BookmarkNode* permanent_node) { |
| 185 // The |managed_node_| can have its title updated if the user signs in or | 198 // The |managed_node_| can have its title updated if the user signs in or |
| 186 // out. | 199 // out, since the name of the managed domain can appear in it. The |
| 187 return !IsDescendantOfManagedNode(permanent_node) || | 200 // |supervised_node_| has a fixed title which can never be updated. |
| 201 return (!bookmarks::IsDescendantOf(permanent_node, managed_node_) && |
| 202 !bookmarks::IsDescendantOf(permanent_node, supervised_node_)) || |
| 188 permanent_node == managed_node_; | 203 permanent_node == managed_node_; |
| 189 } | 204 } |
| 190 | 205 |
| 191 bool ChromeBookmarkClient::CanSyncNode(const BookmarkNode* node) { | 206 bool ChromeBookmarkClient::CanSyncNode(const BookmarkNode* node) { |
| 192 return !IsDescendantOfManagedNode(node); | 207 return !bookmarks::IsDescendantOf(node, managed_node_) && |
| 208 !bookmarks::IsDescendantOf(node, supervised_node_); |
| 193 } | 209 } |
| 194 | 210 |
| 195 bool ChromeBookmarkClient::CanBeEditedByUser(const BookmarkNode* node) { | 211 bool ChromeBookmarkClient::CanBeEditedByUser(const BookmarkNode* node) { |
| 196 return !IsDescendantOfManagedNode(node); | 212 return !bookmarks::IsDescendantOf(node, managed_node_) && |
| 213 !bookmarks::IsDescendantOf(node, supervised_node_); |
| 197 } | 214 } |
| 198 | 215 |
| 199 void ChromeBookmarkClient::SetHistoryService(HistoryService* history_service) { | 216 void ChromeBookmarkClient::SetHistoryService(HistoryService* history_service) { |
| 200 DCHECK(history_service); | 217 DCHECK(history_service); |
| 201 history_service_ = history_service; | 218 history_service_ = history_service; |
| 202 favicon_changed_subscription_ = history_service_->AddFaviconChangedCallback( | 219 favicon_changed_subscription_ = history_service_->AddFaviconChangedCallback( |
| 203 base::Bind(&BookmarkModel::OnFaviconChanged, base::Unretained(model_))); | 220 base::Bind(&BookmarkModel::OnFaviconChanged, base::Unretained(model_))); |
| 204 } | 221 } |
| 205 | 222 |
| 206 void ChromeBookmarkClient::BookmarkModelChanged() { | 223 void ChromeBookmarkClient::BookmarkModelChanged() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 218 | 235 |
| 219 void ChromeBookmarkClient::BookmarkAllUserNodesRemoved( | 236 void ChromeBookmarkClient::BookmarkAllUserNodesRemoved( |
| 220 BookmarkModel* model, | 237 BookmarkModel* model, |
| 221 const std::set<GURL>& removed_urls) { | 238 const std::set<GURL>& removed_urls) { |
| 222 if (history_service_) | 239 if (history_service_) |
| 223 history_service_->URLsNoLongerBookmarked(removed_urls); | 240 history_service_->URLsNoLongerBookmarked(removed_urls); |
| 224 } | 241 } |
| 225 | 242 |
| 226 void ChromeBookmarkClient::BookmarkModelLoaded(BookmarkModel* model, | 243 void ChromeBookmarkClient::BookmarkModelLoaded(BookmarkModel* model, |
| 227 bool ids_reassigned) { | 244 bool ids_reassigned) { |
| 228 // Start tracking the managed bookmarks. This will detect any changes that | 245 // Start tracking the managed and supervised bookmarks. This will detect any |
| 229 // may have occurred while the initial managed bookmarks were being loaded | 246 // changes that may have occurred while the initial managed and supervised |
| 230 // on the background. | 247 // bookmarks were being loaded on the background. |
| 231 managed_bookmarks_tracker_->Init(managed_node_); | 248 managed_bookmarks_tracker_->Init(managed_node_); |
| 249 supervised_bookmarks_tracker_->Init(supervised_node_); |
| 232 } | 250 } |
| 233 | 251 |
| 234 // static | 252 // static |
| 235 bookmarks::BookmarkPermanentNodeList ChromeBookmarkClient::LoadExtraNodes( | 253 bookmarks::BookmarkPermanentNodeList ChromeBookmarkClient::LoadExtraNodes( |
| 236 scoped_ptr<BookmarkPermanentNode> managed_node, | 254 scoped_ptr<BookmarkPermanentNode> managed_node, |
| 237 scoped_ptr<base::ListValue> initial_managed_bookmarks, | 255 scoped_ptr<base::ListValue> initial_managed_bookmarks, |
| 256 scoped_ptr<BookmarkPermanentNode> supervised_node, |
| 257 scoped_ptr<base::ListValue> initial_supervised_bookmarks, |
| 238 int64* next_node_id) { | 258 int64* next_node_id) { |
| 239 // Load the initial contents of the |managed_node| now, and assign it an | 259 LoadInitialContents( |
| 240 // unused ID. | 260 managed_node.get(), initial_managed_bookmarks.get(), next_node_id); |
| 241 int64 managed_id = *next_node_id; | 261 managed_node->SetTitle(l10n_util::GetStringUTF16( |
| 242 managed_node->set_id(managed_id); | 262 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME)); |
| 243 *next_node_id = policy::ManagedBookmarksTracker::LoadInitial( | 263 |
| 244 managed_node.get(), initial_managed_bookmarks.get(), managed_id + 1); | 264 LoadInitialContents( |
| 245 managed_node->set_visible(!managed_node->empty()); | 265 supervised_node.get(), initial_supervised_bookmarks.get(), next_node_id); |
| 246 managed_node->SetTitle( | 266 supervised_node->SetTitle(l10n_util::GetStringUTF16( |
| 247 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME)); | 267 IDS_BOOKMARK_BAR_SUPERVISED_FOLDER_DEFAULT_NAME)); |
| 248 | 268 |
| 249 bookmarks::BookmarkPermanentNodeList extra_nodes; | 269 bookmarks::BookmarkPermanentNodeList extra_nodes; |
| 250 // Ownership of the managed node passed to the caller. | 270 // Ownership of the managed and supervised nodes passed to the caller. |
| 251 extra_nodes.push_back(managed_node.release()); | 271 extra_nodes.push_back(managed_node.release()); |
| 272 extra_nodes.push_back(supervised_node.release()); |
| 252 | 273 |
| 253 return extra_nodes.Pass(); | 274 return extra_nodes.Pass(); |
| 254 } | 275 } |
| 255 | 276 |
| 256 std::string ChromeBookmarkClient::GetManagedBookmarksDomain() { | 277 std::string ChromeBookmarkClient::GetManagedBookmarksDomain() { |
| 257 policy::ProfilePolicyConnector* connector = | 278 policy::ProfilePolicyConnector* connector = |
| 258 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 279 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
| 259 if (connector->IsPolicyFromCloudPolicy(policy::key::kManagedBookmarks)) | 280 if (connector->IsPolicyFromCloudPolicy(policy::key::kManagedBookmarks)) |
| 260 return connector->GetManagementDomain(); | 281 return connector->GetManagementDomain(); |
| 261 return std::string(); | 282 return std::string(); |
| 262 } | 283 } |
| OLD | NEW |