1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_DEDUPING_FACTORY_H__
#define EXTENSIONS_BROWSER_API_DECLARATIVE_DEDUPING_FACTORY_H__
#include <stddef.h>
#include <list>
#include <string>
#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/memory/ref_counted.h"
namespace extensions {
// Factory class that stores a cache of the last `N` created objects of each
// kind. These objects need to be immutable, refcounted objects that are derived
// from BaseClassT. The objects do not need to be RefCountedThreadSafe. If a new
// instance of an object is created that is identical to a pre-existing object,
// it is discarded and the pre-existing object is recycled.
//
// BaseClassT needs to provide a comparison operations. Like the following:
//
// class BaseClassT {
// virtual bool Equals(const BaseClassT* other) const;
// };
//
// The unit test shows an example.
template <typename BaseClassT, typename ValueT>
class DedupingFactory {
public:
// Factory methods for BaseClass instances. `value` contains e.g. the json
// dictionary that describes the object to be instantiated. `error` is used
// to return error messages in case the extension passed an action that was
// syntactically correct but semantically incorrect. `bad_message` is set to
// true in case `dict` does not confirm to the validated JSON specification.
typedef scoped_refptr<const BaseClassT> (*FactoryMethod)(
const std::string& instance_type,
ValueT /* value */,
std::string* /* error */,
bool* /* bad_message */);
enum Parameterized {
// Two instantiated objects may be different and we need to check for
// equality to see whether we can recycle one.
IS_PARAMETERIZED,
// The objects are not parameterized, i.e. all created instances are the
// same and it is sufficient to create a single one.
IS_NOT_PARAMETERIZED
};
// Creates a DedupingFactory with a MRU cache of size `max_number_prototypes`
// per instance_type. If we find a match within the cache, the factory reuses
// that instance instead of creating a new one. The cache size should not be
// too large because we probe linearly whether an element is in the cache.
explicit DedupingFactory(size_t max_number_prototypes);
DedupingFactory(const DedupingFactory&) = delete;
DedupingFactory& operator=(const DedupingFactory&) = delete;
~DedupingFactory();
void RegisterFactoryMethod(const std::string& instance_type,
Parameterized parameterized,
FactoryMethod factory_method);
scoped_refptr<const BaseClassT> Instantiate(const std::string& instance_type,
ValueT value,
std::string* error,
bool* bad_message);
void ClearPrototypes();
private:
typedef std::string InstanceType;
// Cache of previous prototypes in most-recently-used order. Most recently
// used objects are at the end.
using PrototypeList = std::list<scoped_refptr<const BaseClassT>>;
using ExistingPrototypes = base::flat_map<InstanceType, PrototypeList>;
using FactoryMethods = base::flat_map<InstanceType, FactoryMethod>;
using ParameterizedTypes = base::flat_set<InstanceType>;
const size_t max_number_prototypes_;
ExistingPrototypes prototypes_;
FactoryMethods factory_methods_;
ParameterizedTypes parameterized_types_;
};
template <typename BaseClassT, typename ValueT>
DedupingFactory<BaseClassT, ValueT>::DedupingFactory(
size_t max_number_prototypes)
: max_number_prototypes_(max_number_prototypes) {}
template <typename BaseClassT, typename ValueT>
DedupingFactory<BaseClassT, ValueT>::~DedupingFactory() {}
template <typename BaseClassT, typename ValueT>
void DedupingFactory<BaseClassT, ValueT>::RegisterFactoryMethod(
const std::string& instance_type,
typename DedupingFactory<BaseClassT, ValueT>::Parameterized parameterized,
FactoryMethod factory_method) {
DCHECK(!base::Contains(factory_methods_, instance_type));
factory_methods_[instance_type] = factory_method;
if (parameterized == IS_PARAMETERIZED) {
parameterized_types_.insert(instance_type);
}
}
template <typename BaseClassT, typename ValueT>
scoped_refptr<const BaseClassT>
DedupingFactory<BaseClassT, ValueT>::Instantiate(
const std::string& instance_type,
ValueT value,
std::string* error,
bool* bad_message) {
typename FactoryMethods::const_iterator factory_method_iter =
factory_methods_.find(instance_type);
if (factory_method_iter == factory_methods_.end()) {
*error = "Invalid instance type " + instance_type;
*bad_message = true;
return scoped_refptr<const BaseClassT>();
}
FactoryMethod factory_method = factory_method_iter->second;
PrototypeList& prototypes = prototypes_[instance_type];
// We can take a shortcut for objects that are not parameterized. For those
// only a single instance may ever exist so we can simplify the creation
// logic.
if (!base::Contains(parameterized_types_, instance_type)) {
if (prototypes.empty()) {
scoped_refptr<const BaseClassT> new_object =
(*factory_method)(instance_type, value, error, bad_message);
if (!new_object.get() || !error->empty() || *bad_message) {
return scoped_refptr<const BaseClassT>();
}
prototypes.push_back(new_object);
}
return prototypes.front();
}
// Handle parameterized objects.
scoped_refptr<const BaseClassT> new_object =
(*factory_method)(instance_type, value, error, bad_message);
if (!new_object.get() || !error->empty() || *bad_message) {
return scoped_refptr<const BaseClassT>();
}
size_t length = 0;
for (typename PrototypeList::iterator i = prototypes.begin();
i != prototypes.end();
++i) {
if ((*i)->Equals(new_object.get())) {
// Move the old object to the end of the queue so that it gets
// discarded later.
scoped_refptr<const BaseClassT> old_object = *i;
prototypes.erase(i);
prototypes.push_back(old_object);
return old_object;
}
++length;
}
if (length >= max_number_prototypes_)
prototypes.pop_front();
prototypes.push_back(new_object);
return new_object;
}
template <typename BaseClassT, typename ValueT>
void DedupingFactory<BaseClassT, ValueT>::ClearPrototypes() {
prototypes_.clear();
}
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_DECLARATIVE_DEDUPING_FACTORY_H__
|