172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Use of this source code is governed by a BSD-style license that can be 372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// found in the LICENSE file. 472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_ 672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_ 772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#pragma once 872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include <map> 1072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include <set> 1172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include <string> 1272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 1372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "base/time.h" 1472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "chrome/browser/prefs/value_map_pref_store.h" 1572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 1672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Non-persistent data container that is shared by ExtensionPrefStores. All 1772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// extension pref values (incognito and regular) are stored herein and 1872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// provided to ExtensionPrefStores. 1972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 2072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// The semantics of the ExtensionPrefValueMap are: 2172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// - The precedence of extensions is determined by their installation time. 2272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// The extension that has been installed later takes higher precedence. 2372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// - If two extensions set a value for the same preference, the following 2472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// rules determine which value becomes effective (visible). 2572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// - The effective regular extension pref value is determined by the regular 2672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// extension pref value of the extension with the highest precedence. 2772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// - The effective incognito extension pref value is determined by the incognito 2872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// extension pref value of the extension with the highest precedence, unless 2972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// another extension with higher precedence overrides it with a regular 3072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// extension pref value. 3172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 3272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// The following table illustrates the behavior: 3372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// A.reg | A.inc | B.reg | B.inc | E.reg | E.inc 3472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 1 | - | - | - | 1 | 1 3572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 1 | 2 | - | - | 1 | 2 3672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 1 | - | 3 | - | 3 | 3 3772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 1 | - | - | 4 | 1 | 4 3872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 1 | 2 | 3 | - | 3 | 3(!) 3972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 1 | 2 | - | 4 | 1 | 4 4072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// 1 | 2 | 3 | 4 | 3 | 4 4172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// A = extension A, B = extension B, E = effective value 4272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// .reg = regular value 4372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// .inc = incognito value 4472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Extension B has higher precedence than A. 4572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenclass ExtensionPrefValueMap { 4672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen public: 4772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Observer interface for monitoring ExtensionPrefValueMap. 4872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen class Observer { 4972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen public: 5072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual ~Observer() {} 5172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 5272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Called when the value for the given |key| set by one of the extensions 5372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // changes. This does not necessarily mean that the effective value has 5472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // changed. 5572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual void OnPrefValueChanged(const std::string& key) = 0; 5672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Notification about the ExtensionPrefValueMap being fully initialized. 5772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual void OnInitializationCompleted() = 0; 5872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Called when the ExtensionPrefValueMap is being destroyed. When called, 5972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // observers must unsubscribe. 6072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual void OnExtensionPrefValueMapDestruction() = 0; 6172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen }; 6272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 6372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen ExtensionPrefValueMap(); 6472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen virtual ~ExtensionPrefValueMap(); 6572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 6672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Set an extension preference |value| for |key| of extension |ext_id|. 6772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Takes ownership of |value|. 6872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Note that regular extension pref values need to be reported to 6972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // incognito and to regular ExtensionPrefStores. 7072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Precondition: the extension must be registered. 7172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void SetExtensionPref(const std::string& ext_id, 7272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const std::string& key, 7372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen bool incognito, 7472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen Value* value); 7572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 7672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Remove the extension preference value for |key| of extension |ext_id|. 7772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Precondition: the extension must be registered. 7872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void RemoveExtensionPref(const std::string& ext_id, 7972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const std::string& key, 8072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen bool incognito); 8172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 82dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Returns true if currently no extension with higher precedence controls the 83dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // preference. 84dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Note that the this function does does not consider the existence of 85dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // policies. An extension is only really able to control a preference if 86dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // PrefService::Preference::IsExtensionModifiable() returns true as well. 87dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen bool CanExtensionControlPref(const std::string& extension_id, 88dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const std::string& pref_key, 89dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen bool incognito) const; 90dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 91dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Returns true if an extension identified by |extension_id| controls the 92dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // preference. This means this extension has set a preference value and no 93dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // other extension with higher precedence overrides it. 94dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Note that the this function does does not consider the existence of 95dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // policies. An extension is only really able to control a preference if 96dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // PrefService::Preference::IsExtensionModifiable() returns true as well. 97dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen bool DoesExtensionControlPref(const std::string& extension_id, 98dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const std::string& pref_key, 99dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen bool incognito) const; 100dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 10172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Tell the store it's now fully initialized. 10272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void NotifyInitializationCompleted(); 10372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 10472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Registers the time when an extension |ext_id| is installed. 10572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void RegisterExtension(const std::string& ext_id, 10672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const base::Time& install_time, 10772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen bool is_enabled); 10872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 10972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Deletes all entries related to extension |ext_id|. 11072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void UnregisterExtension(const std::string& ext_id); 11172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 11272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Hides or makes the extension preference values of the specified extension 11372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // visible. 11472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void SetExtensionState(const std::string& ext_id, bool is_enabled); 11572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 11672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Adds an observer and notifies it about the currently stored keys. 11772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void AddObserver(Observer* observer); 11872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 11972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void RemoveObserver(Observer* observer); 12072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 12172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const Value* GetEffectivePrefValue(const std::string& key, 122ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool incognito, 123ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool* from_incognito) const; 12472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 12572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen private: 12672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen struct ExtensionEntry; 12772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 12872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen typedef std::map<std::string, ExtensionEntry*> ExtensionEntryMap; 12972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 13072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const PrefValueMap* GetExtensionPrefValueMap(const std::string& ext_id, 13172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen bool incognito) const; 13272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 13372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen PrefValueMap* GetExtensionPrefValueMap(const std::string& ext_id, 13472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen bool incognito); 13572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 13672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Returns all keys of pref values that are set by the extension of |entry|, 13772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // regardless whether they are set for incognito or regular pref values. 13872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void GetExtensionControlledKeys(const ExtensionEntry& entry, 13972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen std::set<std::string>* out) const; 14072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 141ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen // Returns an iterator to the extension which controls the preference |key|. 142ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen // If |incognito| is true, looks at incognito preferences first. In that case, 143ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen // if |from_incognito| is not NULL, it is set to true if the effective pref 144ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen // value is coming from the incognito preferences, false if it is coming from 145ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen // the normal ones. 146dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ExtensionEntryMap::const_iterator GetEffectivePrefValueController( 147ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const std::string& key, 148ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool incognito, 149ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool* from_incognito) const; 150dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 15172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void NotifyOfDestruction(); 15272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void NotifyPrefValueChanged(const std::string& key); 15372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen void NotifyPrefValueChanged(const std::set<std::string>& keys); 15472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 15572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Mapping of which extension set which preference value. The effective 15672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // preferences values (i.e. the ones with the highest precedence) 15772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // are stored in ExtensionPrefStores. 15872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen ExtensionEntryMap entries_; 15972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 16072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen ObserverList<Observer, true> observers_; 16172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 16272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen DISALLOW_COPY_AND_ASSIGN(ExtensionPrefValueMap); 16372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}; 16472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 16572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_ 166