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