Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: chrome/browser/bookmarks/chrome_bookmark_client.cc

Issue 1233673002: Fix componentization of chrome/browser/bookmarks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix "gn check" and compilation on Mac Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 IsPermanentNode(
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 }
71 68
72 ChromeBookmarkClient::~ChromeBookmarkClient() { 69 ChromeBookmarkClient::~ChromeBookmarkClient() {
73 } 70 }
74 71
75 void ChromeBookmarkClient::Init(BookmarkModel* model) { 72 void ChromeBookmarkClient::Init(bookmarks::BookmarkModel* model) {
76 DCHECK(model); 73 if (managed_bookmark_service_)
77 DCHECK(!model_); 74 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 } 75 }
93 76
94 void ChromeBookmarkClient::Shutdown() { 77 void ChromeBookmarkClient::Shutdown() {
95 if (model_) { 78 managed_bookmark_service_ = nullptr;
96 model_->RemoveObserver(this);
97 model_ = nullptr;
98 }
99 BookmarkClient::Shutdown(); 79 BookmarkClient::Shutdown();
100 } 80 }
101 81
102 bool ChromeBookmarkClient::PreferTouchIcon() { 82 bool ChromeBookmarkClient::PreferTouchIcon() {
103 #if !defined(OS_IOS) 83 #if !defined(OS_IOS)
104 return false; 84 return false;
105 #else 85 #else
106 return true; 86 return true;
107 #endif 87 #endif
108 } 88 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 history::URLRow url; 132 history::URLRow url;
153 if (url_db && url_db->GetRowForURL((*i)->url(), &url)) 133 if (url_db && url_db->GetRowForURL((*i)->url(), &url))
154 typed_count = url.typed_count(); 134 typed_count = url.typed_count();
155 135
156 NodeTypedCountPair pair(*i, typed_count); 136 NodeTypedCountPair pair(*i, typed_count);
157 node_typed_count_pairs->push_back(pair); 137 node_typed_count_pairs->push_back(pair);
158 } 138 }
159 } 139 }
160 140
161 bool ChromeBookmarkClient::IsPermanentNodeVisible( 141 bool ChromeBookmarkClient::IsPermanentNodeVisible(
162 const BookmarkPermanentNode* node) { 142 const bookmarks::BookmarkPermanentNode* node) {
163 DCHECK(node->type() == BookmarkNode::BOOKMARK_BAR || 143 DCHECK(IsPermanentNode(node, managed_bookmark_service_));
164 node->type() == BookmarkNode::OTHER_NODE || 144 if (managed_bookmark_service_ &&
165 node->type() == BookmarkNode::MOBILE || 145 (node == managed_bookmark_service_->managed_node() ||
166 node == managed_node_ || 146 node == managed_bookmark_service_->supervised_node())) {
167 node == supervised_node_);
168 if (node == managed_node_ || node == supervised_node_)
169 return false; 147 return false;
148 }
170 #if defined(OS_IOS) || defined(OS_ANDROID) 149 #if defined(OS_IOS) || defined(OS_ANDROID)
171 return node->type() == BookmarkNode::MOBILE; 150 return node->type() == bookmarks::BookmarkNode::MOBILE;
172 #else 151 #else
173 return node->type() != BookmarkNode::MOBILE; 152 return node->type() != bookmarks::BookmarkNode::MOBILE;
174 #endif 153 #endif
175 } 154 }
176 155
177 void ChromeBookmarkClient::RecordAction(const base::UserMetricsAction& action) { 156 void ChromeBookmarkClient::RecordAction(const base::UserMetricsAction& action) {
178 content::RecordAction(action); 157 content::RecordAction(action);
179 } 158 }
180 159
181 bookmarks::LoadExtraCallback ChromeBookmarkClient::GetLoadExtraNodesCallback() { 160 bookmarks::LoadExtraCallback ChromeBookmarkClient::GetLoadExtraNodesCallback() {
182 // Create the managed_node_ and supervised_node_ with a temporary ID of 0 now. 161 if (!managed_bookmark_service_)
183 // They will be populated (and assigned proper IDs) in the LoadExtraNodes 162 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 163
192 return base::Bind( 164 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 } 165 }
200 166
201 bool ChromeBookmarkClient::CanSetPermanentNodeTitle( 167 bool ChromeBookmarkClient::CanSetPermanentNodeTitle(
202 const BookmarkNode* permanent_node) { 168 const bookmarks::BookmarkNode* permanent_node) {
203 // The |managed_node_| can have its title updated if the user signs in or 169 return !managed_bookmark_service_
204 // out, since the name of the managed domain can appear in it. 170 ? true
205 // Also, both |managed_node_| and |supervised_node_| can have their title 171 : managed_bookmark_service_->CanSetPermanentNodeTitle(
206 // updated on locale changes (crbug.com/459448). 172 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 } 173 }
212 174
213 bool ChromeBookmarkClient::CanSyncNode(const BookmarkNode* node) { 175 bool ChromeBookmarkClient::CanSyncNode(const bookmarks::BookmarkNode* node) {
214 return !bookmarks::IsDescendantOf(node, managed_node_) && 176 return !managed_bookmark_service_
215 !bookmarks::IsDescendantOf(node, supervised_node_); 177 ? true
178 : managed_bookmark_service_->CanSyncNode(node);
216 } 179 }
217 180
218 bool ChromeBookmarkClient::CanBeEditedByUser(const BookmarkNode* node) { 181 bool ChromeBookmarkClient::CanBeEditedByUser(
219 return !bookmarks::IsDescendantOf(node, managed_node_) && 182 const bookmarks::BookmarkNode* node) {
220 !bookmarks::IsDescendantOf(node, supervised_node_); 183 return !managed_bookmark_service_
184 ? true
185 : managed_bookmark_service_->CanBeEditedByUser(node);
221 } 186 }
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 }
OLDNEW
« no previous file with comments | « chrome/browser/bookmarks/chrome_bookmark_client.h ('k') | chrome/browser/bookmarks/chrome_bookmark_client_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698