Home | History | Annotate | Download | only in policy
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/policy/managed_bookmarks_policy_handler.h"
      6 
      7 #include "base/prefs/pref_value_map.h"
      8 #include "base/values.h"
      9 #include "components/bookmarks/common/bookmark_pref_names.h"
     10 #include "components/policy/core/browser/managed_bookmarks_tracker.h"
     11 #include "components/policy/core/browser/policy_error_map.h"
     12 #include "components/policy/core/common/policy_map.h"
     13 #include "components/url_fixer/url_fixer.h"
     14 #include "grit/components_strings.h"
     15 #include "policy/policy_constants.h"
     16 #include "url/gurl.h"
     17 
     18 namespace policy {
     19 
     20 ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler(
     21     Schema chrome_schema)
     22     : SchemaValidatingPolicyHandler(
     23           key::kManagedBookmarks,
     24           chrome_schema.GetKnownProperty(key::kManagedBookmarks),
     25           SCHEMA_ALLOW_INVALID) {}
     26 
     27 ManagedBookmarksPolicyHandler::~ManagedBookmarksPolicyHandler() {}
     28 
     29 void ManagedBookmarksPolicyHandler::ApplyPolicySettings(
     30     const PolicyMap& policies,
     31     PrefValueMap* prefs) {
     32   scoped_ptr<base::Value> value;
     33   if (!CheckAndGetValue(policies, NULL, &value))
     34     return;
     35 
     36   base::ListValue* list = NULL;
     37   if (!value || !value->GetAsList(&list))
     38     return;
     39 
     40   FilterBookmarks(list);
     41   prefs->SetValue(prefs::kManagedBookmarks, value.release());
     42 }
     43 
     44 void ManagedBookmarksPolicyHandler::FilterBookmarks(base::ListValue* list) {
     45   // Remove any non-conforming values found.
     46   base::ListValue::iterator it = list->begin();
     47   while (it != list->end()) {
     48     base::DictionaryValue* dict = NULL;
     49     if (!*it || !(*it)->GetAsDictionary(&dict)) {
     50       it = list->Erase(it, NULL);
     51       continue;
     52     }
     53 
     54     std::string name;
     55     std::string url;
     56     base::ListValue* children = NULL;
     57     // Every bookmark must have a name, and then either a URL of a list of
     58     // child bookmarks.
     59     if (!dict->GetString(ManagedBookmarksTracker::kName, &name) ||
     60         (!dict->GetList(ManagedBookmarksTracker::kChildren, &children) &&
     61          !dict->GetString(ManagedBookmarksTracker::kUrl, &url))) {
     62       it = list->Erase(it, NULL);
     63       continue;
     64     }
     65 
     66     if (children) {
     67       // Ignore the URL if this bookmark has child nodes.
     68       dict->Remove(ManagedBookmarksTracker::kUrl, NULL);
     69       FilterBookmarks(children);
     70     } else {
     71       // Make sure the URL is valid before passing a bookmark to the pref.
     72       dict->Remove(ManagedBookmarksTracker::kChildren, NULL);
     73       GURL gurl = url_fixer::FixupURL(url, "");
     74       if (!gurl.is_valid()) {
     75         it = list->Erase(it, NULL);
     76         continue;
     77       }
     78       dict->SetString(ManagedBookmarksTracker::kUrl, gurl.spec());
     79     }
     80 
     81     ++it;
     82   }
     83 }
     84 
     85 }  // namespace policy
     86