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 "components/policy/core/browser/managed_bookmarks_tracker.h" | 5 #include "components/policy/core/browser/managed_bookmarks_tracker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/logging.h" |
12 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
16 #include "components/bookmarks/browser/bookmark_node.h" | 17 #include "components/bookmarks/browser/bookmark_node.h" |
17 #include "components/bookmarks/common/bookmark_pref_names.h" | 18 #include "components/bookmarks/common/bookmark_pref_names.h" |
18 #include "grit/components_strings.h" | 19 #include "grit/components_strings.h" |
19 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
21 | 22 |
22 using bookmarks::BookmarkModel; | 23 using bookmarks::BookmarkModel; |
23 using bookmarks::BookmarkNode; | 24 using bookmarks::BookmarkNode; |
24 using bookmarks::BookmarkPermanentNode; | 25 using bookmarks::BookmarkPermanentNode; |
25 | 26 |
26 namespace policy { | 27 namespace policy { |
27 | 28 |
28 const char ManagedBookmarksTracker::kName[] = "name"; | 29 const char ManagedBookmarksTracker::kName[] = "name"; |
29 const char ManagedBookmarksTracker::kUrl[] = "url"; | 30 const char ManagedBookmarksTracker::kUrl[] = "url"; |
30 const char ManagedBookmarksTracker::kChildren[] = "children"; | 31 const char ManagedBookmarksTracker::kChildren[] = "children"; |
31 | 32 |
32 ManagedBookmarksTracker::ManagedBookmarksTracker( | 33 ManagedBookmarksTracker::ManagedBookmarksTracker( |
33 BookmarkModel* model, | 34 BookmarkModel* model, |
34 PrefService* prefs, | 35 PrefService* prefs, |
| 36 bool is_supervised, |
35 const GetManagementDomainCallback& callback) | 37 const GetManagementDomainCallback& callback) |
36 : model_(model), | 38 : model_(model), |
| 39 is_supervised_(is_supervised), |
37 managed_node_(NULL), | 40 managed_node_(NULL), |
38 prefs_(prefs), | 41 prefs_(prefs), |
39 get_management_domain_callback_(callback) { | 42 get_management_domain_callback_(callback) { |
40 } | 43 } |
41 | 44 |
42 ManagedBookmarksTracker::~ManagedBookmarksTracker() {} | 45 ManagedBookmarksTracker::~ManagedBookmarksTracker() {} |
43 | 46 |
44 scoped_ptr<base::ListValue> | 47 scoped_ptr<base::ListValue> |
45 ManagedBookmarksTracker::GetInitialManagedBookmarks() { | 48 ManagedBookmarksTracker::GetInitialManagedBookmarks() { |
46 const base::ListValue* list = | 49 const base::ListValue* list = prefs_->GetList(GetPrefName()); |
47 prefs_->GetList(bookmarks::prefs::kManagedBookmarks); | |
48 return make_scoped_ptr(list->DeepCopy()); | 50 return make_scoped_ptr(list->DeepCopy()); |
49 } | 51 } |
50 | 52 |
51 // static | 53 // static |
52 int64 ManagedBookmarksTracker::LoadInitial(BookmarkNode* folder, | 54 int64 ManagedBookmarksTracker::LoadInitial(BookmarkNode* folder, |
53 const base::ListValue* list, | 55 const base::ListValue* list, |
54 int64 next_node_id) { | 56 int64 next_node_id) { |
55 for (size_t i = 0; i < list->GetSize(); ++i) { | 57 for (size_t i = 0; i < list->GetSize(); ++i) { |
56 // Extract the data for the next bookmark from the |list|. | 58 // Extract the data for the next bookmark from the |list|. |
57 base::string16 title; | 59 base::string16 title; |
(...skipping 14 matching lines...) Expand all Loading... |
72 child->set_date_added(base::Time::Now()); | 74 child->set_date_added(base::Time::Now()); |
73 } | 75 } |
74 } | 76 } |
75 | 77 |
76 return next_node_id; | 78 return next_node_id; |
77 } | 79 } |
78 | 80 |
79 void ManagedBookmarksTracker::Init(BookmarkPermanentNode* managed_node) { | 81 void ManagedBookmarksTracker::Init(BookmarkPermanentNode* managed_node) { |
80 managed_node_ = managed_node; | 82 managed_node_ = managed_node; |
81 registrar_.Init(prefs_); | 83 registrar_.Init(prefs_); |
82 registrar_.Add(bookmarks::prefs::kManagedBookmarks, | 84 registrar_.Add(GetPrefName(), |
83 base::Bind(&ManagedBookmarksTracker::ReloadManagedBookmarks, | 85 base::Bind(&ManagedBookmarksTracker::ReloadManagedBookmarks, |
84 base::Unretained(this))); | 86 base::Unretained(this))); |
85 // Reload now just in case something changed since the initial load started. | 87 // Reload now just in case something changed since the initial load started. |
86 ReloadManagedBookmarks(); | 88 ReloadManagedBookmarks(); |
87 } | 89 } |
88 | 90 |
| 91 // static |
| 92 const char* ManagedBookmarksTracker::GetPrefName(bool is_supervised) { |
| 93 return is_supervised ? bookmarks::prefs::kSupervisedBookmarks |
| 94 : bookmarks::prefs::kManagedBookmarks; |
| 95 } |
| 96 |
| 97 const char* ManagedBookmarksTracker::GetPrefName() const { |
| 98 return GetPrefName(is_supervised_); |
| 99 } |
| 100 |
| 101 base::string16 ManagedBookmarksTracker::GetBookmarksFolderTitle() const { |
| 102 if (is_supervised_) { |
| 103 return l10n_util::GetStringUTF16( |
| 104 IDS_BOOKMARK_BAR_SUPERVISED_FOLDER_DEFAULT_NAME); |
| 105 } else { |
| 106 const std::string domain = get_management_domain_callback_.Run(); |
| 107 if (domain.empty()) { |
| 108 return l10n_util::GetStringUTF16( |
| 109 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME); |
| 110 } else { |
| 111 return l10n_util::GetStringFUTF16( |
| 112 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DOMAIN_NAME, |
| 113 base::UTF8ToUTF16(domain)); |
| 114 } |
| 115 } |
| 116 } |
| 117 |
89 void ManagedBookmarksTracker::ReloadManagedBookmarks() { | 118 void ManagedBookmarksTracker::ReloadManagedBookmarks() { |
90 // Update the managed bookmarks folder title, in case the user just signed | 119 // In case the user just signed into or out of the account. |
91 // into or out of a managed account. | 120 model_->SetTitle(managed_node_, GetBookmarksFolderTitle()); |
92 base::string16 title; | |
93 std::string domain = get_management_domain_callback_.Run(); | |
94 if (domain.empty()) { | |
95 title = | |
96 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME); | |
97 } else { | |
98 title = l10n_util::GetStringFUTF16( | |
99 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DOMAIN_NAME, base::UTF8ToUTF16(domain)); | |
100 } | |
101 model_->SetTitle(managed_node_, title); | |
102 | 121 |
103 // Recursively update all the managed bookmarks and folders. | 122 // Recursively update all the managed bookmarks and folders. |
104 const base::ListValue* list = | 123 const base::ListValue* list = prefs_->GetList(GetPrefName()); |
105 prefs_->GetList(bookmarks::prefs::kManagedBookmarks); | |
106 UpdateBookmarks(managed_node_, list); | 124 UpdateBookmarks(managed_node_, list); |
107 | 125 |
108 // The managed bookmarks folder isn't visible when that policy isn't present. | 126 // The managed bookmarks folder isn't visible when that pref isn't present. |
109 managed_node_->set_visible(!managed_node_->empty()); | 127 managed_node_->set_visible(!managed_node_->empty()); |
110 } | 128 } |
111 | 129 |
112 void ManagedBookmarksTracker::UpdateBookmarks(const BookmarkNode* folder, | 130 void ManagedBookmarksTracker::UpdateBookmarks(const BookmarkNode* folder, |
113 const base::ListValue* list) { | 131 const base::ListValue* list) { |
114 int folder_index = 0; | 132 int folder_index = 0; |
115 for (size_t i = 0; i < list->GetSize(); ++i) { | 133 for (size_t i = 0; i < list->GetSize(); ++i) { |
116 // Extract the data for the next bookmark from the |list|. | 134 // Extract the data for the next bookmark from the |list|. |
117 base::string16 title; | 135 base::string16 title; |
118 GURL url; | 136 GURL url; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 *children = NULL; | 190 *children = NULL; |
173 const base::DictionaryValue* dict = NULL; | 191 const base::DictionaryValue* dict = NULL; |
174 if (!list->GetDictionary(index, &dict) || | 192 if (!list->GetDictionary(index, &dict) || |
175 !dict->GetString(kName, title) || | 193 !dict->GetString(kName, title) || |
176 (!dict->GetString(kUrl, &spec) && | 194 (!dict->GetString(kUrl, &spec) && |
177 !dict->GetList(kChildren, children))) { | 195 !dict->GetList(kChildren, children))) { |
178 // Should never happen after policy validation. | 196 // Should never happen after policy validation. |
179 NOTREACHED(); | 197 NOTREACHED(); |
180 return false; | 198 return false; |
181 } | 199 } |
182 if (!*children) | 200 if (!*children) { |
183 *url = GURL(spec); | 201 *url = GURL(spec); |
| 202 DCHECK(url->is_valid()); |
| 203 } |
184 return true; | 204 return true; |
185 } | 205 } |
186 | 206 |
187 } // namespace policy | 207 } // namespace policy |
OLD | NEW |