1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef COMPONENTS_POLICY_CORE_COMMON_POLICY_MAP_H_
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define COMPONENTS_POLICY_CORE_COMMON_POLICY_MAP_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/external_data_fetcher.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/policy_types.h"
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/policy_export.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace policy {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A mapping of policy names to policy values for a given policy namespace.
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class POLICY_EXPORT PolicyMap {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Each policy maps to an Entry which keeps the policy value as well as other
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // relevant data about the policy.
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  struct POLICY_EXPORT Entry {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PolicyLevel level;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PolicyScope scope;
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::Value* value;
297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ExternalDataFetcher* external_data_fetcher;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Entry();
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
33a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // Deletes all members owned by |this|.
34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    void DeleteOwnedMembers();
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // Returns a copy of |this|.
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    scoped_ptr<Entry> DeepCopy() const;
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Returns true if |this| has higher priority than |other|.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool has_higher_priority_than(const Entry& other) const;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Returns true if |this| equals |other|.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool Equals(const Entry& other) const;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<std::string, Entry> PolicyMapType;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef PolicyMapType::const_iterator const_iterator;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PolicyMap();
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~PolicyMap();
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a weak reference to the entry currently stored for key |policy|,
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or NULL if not found. Ownership is retained by the PolicyMap.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Entry* Get(const std::string& policy) const;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a weak reference to the value currently stored for key |policy|,
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or NULL if not found. Ownership is retained by the PolicyMap.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is equivalent to Get(policy)->value, when it doesn't return NULL.
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const base::Value* GetValue(const std::string& policy) const;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Takes ownership of |value| and |external_data_fetcher|. Overwrites any
627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // existing information stored in the map for the key |policy|.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Set(const std::string& policy,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           PolicyLevel level,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           PolicyScope scope,
667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch           base::Value* value,
677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch           ExternalDataFetcher* external_data_fetcher);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Erase the given |policy|, if it exists in this map.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Erase(const std::string& policy);
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Swaps the internal representation of |this| with |other|.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Swap(PolicyMap* other);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |this| becomes a copy of |other|. Any existing policies are dropped.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CopyFrom(const PolicyMap& other);
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a copy of |this|.
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<PolicyMap> DeepCopy() const;
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Merges policies from |other| into |this|. Existing policies are only
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // overridden by those in |other| if they have a higher priority, as defined
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // by Entry::has_higher_priority_than(). If a policy is contained in both
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // maps with the same priority, the current value in |this| is preserved.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void MergeFrom(const PolicyMap& other);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Loads the values in |policies| into this PolicyMap. All policies loaded
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will have |level| and |scope| in their entries. Existing entries are
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // replaced.
90eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void LoadFrom(const base::DictionaryValue* policies,
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                PolicyLevel level,
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                PolicyScope scope);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compares this value map against |other| and stores all key names that have
957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // different values or reference different external data in |differing_keys|.
967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // This includes keys that are present only in one of the maps.
977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // |differing_keys| is not cleared before the keys are added.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetDifferingKeys(const PolicyMap& other,
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        std::set<std::string>* differing_keys) const;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes all policies that don't have the specified |level|. This is a
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // temporary helper method, until mandatory and recommended levels are served
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // by a single provider.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(joaodasilva): Remove this. http://crbug.com/108999
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FilterLevel(PolicyLevel level);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Equals(const PolicyMap& other) const;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool empty() const;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t size() const;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const_iterator begin() const;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const_iterator end() const;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Clear();
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function for Equals().
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool MapEntryEquals(const PolicyMapType::value_type& a,
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const PolicyMapType::value_type& b);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PolicyMapType map_;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PolicyMap);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace policy
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif  // COMPONENTS_POLICY_CORE_COMMON_POLICY_MAP_H_
128