1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/android/bookmarks/managed_bookmarks_shim.h" 6 7 #include "base/bind.h" 8 #include "base/bind_helpers.h" 9 #include "base/prefs/pref_service.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "chrome/browser/bookmarks/bookmark_model.h" 12 #include "chrome/browser/policy/configuration_policy_handler_android.h" 13 #include "chrome/common/pref_names.h" 14 #include "google_apis/gaia/gaia_auth_util.h" 15 #include "grit/component_strings.h" 16 #include "ui/base/l10n/l10n_util.h" 17 18 using policy::ManagedBookmarksPolicyHandler; 19 ManagedBookmarksShim(PrefService * prefs)20ManagedBookmarksShim::ManagedBookmarksShim(PrefService* prefs) 21 : prefs_(prefs) { 22 registrar_.Init(prefs_); 23 registrar_.Add( 24 prefs::kManagedBookmarks, 25 base::Bind(&ManagedBookmarksShim::Reload, base::Unretained(this))); 26 Reload(); 27 } 28 ~ManagedBookmarksShim()29ManagedBookmarksShim::~ManagedBookmarksShim() {} 30 AddObserver(Observer * observer)31void ManagedBookmarksShim::AddObserver(Observer* observer) { 32 observers_.AddObserver(observer); 33 } 34 RemoveObserver(Observer * observer)35void ManagedBookmarksShim::RemoveObserver(Observer* observer) { 36 observers_.RemoveObserver(observer); 37 } 38 HasManagedBookmarks() const39bool ManagedBookmarksShim::HasManagedBookmarks() const { 40 return !root_->empty(); 41 } 42 IsManagedBookmark(const BookmarkNode * node) const43bool ManagedBookmarksShim::IsManagedBookmark(const BookmarkNode* node) const { 44 while (node != NULL) { 45 if (node == root_.get()) 46 return true; 47 node = node->parent(); 48 } 49 return false; 50 } 51 GetManagedBookmarksRoot() const52const BookmarkNode* ManagedBookmarksShim::GetManagedBookmarksRoot() const { 53 return root_.get(); 54 } 55 GetNodeByID(int64 id) const56const BookmarkNode* ManagedBookmarksShim::GetNodeByID(int64 id) const { 57 if (root_->id() == id) 58 return root_.get(); 59 for (int i = 0; i < root_->child_count(); ++i) { 60 const BookmarkNode* child = root_->GetChild(i); 61 if (child->id() == id) 62 return child; 63 } 64 return NULL; 65 } 66 Reload()67void ManagedBookmarksShim::Reload() { 68 std::string domain; 69 std::string username = prefs_->GetString(prefs::kGoogleServicesUsername); 70 if (!username.empty()) 71 domain = gaia::ExtractDomainName(username); 72 base::string16 root_node_name; 73 if (domain.empty()) { 74 root_node_name = 75 l10n_util::GetStringUTF16(IDS_POLICY_MANAGED_BOOKMARKS_DEFAULT_NAME); 76 } else { 77 root_node_name = l10n_util::GetStringFUTF16(IDS_POLICY_MANAGED_BOOKMARKS, 78 base::UTF8ToUTF16(domain)); 79 } 80 81 root_.reset(new BookmarkPermanentNode(0)); 82 root_->SetTitle(root_node_name); 83 84 const base::ListValue* list = prefs_->GetList(prefs::kManagedBookmarks); 85 int64 id = 1; 86 if (list) { 87 for (base::ListValue::const_iterator it = list->begin(); 88 it != list->end(); ++it) { 89 const base::DictionaryValue* dict = NULL; 90 if (!*it || !(*it)->GetAsDictionary(&dict)) { 91 NOTREACHED(); 92 continue; 93 } 94 95 base::string16 name; 96 std::string url; 97 if (!dict->GetString(ManagedBookmarksPolicyHandler::kName, &name) || 98 !dict->GetString(ManagedBookmarksPolicyHandler::kUrl, &url)) { 99 NOTREACHED(); 100 continue; 101 } 102 103 BookmarkNode* node = new BookmarkNode(id++, GURL(url)); 104 node->set_type(BookmarkNode::URL); 105 node->SetTitle(name); 106 root_->Add(node, root_->child_count()); 107 } 108 } 109 110 FOR_EACH_OBSERVER(Observer, observers_, OnManagedBookmarksChanged()); 111 } 112