Change AbstractFactory to allow dynamic creation.

For the keymaster0 adapter we need to be able to select during adapter
creation time which factories to create.  This will allow selction of
RSA and ECDSA key factories which support selective delegation of
operations to a non-default OpenSSL/BoringSSL ENGINE.

Cherry-picked from internal.

Bug: 20912868
Change-Id: I925f0406c5b2443ff973b7fdfd37eba7ca2edf85
diff --git a/abstract_factory_registry.h b/abstract_factory_registry.h
index 3590f15..10f0ef1 100644
--- a/abstract_factory_registry.h
+++ b/abstract_factory_registry.h
@@ -55,10 +55,7 @@
  *
  * 4.  Register each of the concrete factories by creating an instance of
  *     AbstractFactoryRegistry<AbstractFactory>::Registration<ConcreteFactory> for each concrete
- *     factory.  The best way to do this is to create static Registration instances at file scope in
- *     appropriate compilation units.  Their constructors will create and register the concrete
- *     factories during startup and their destructors will clean up during shutdown.  Registration
- *     is not new'able, specifically to discourage dynamic allocation.
+ *     factory.
  *
  * 5.  At run-time call Get() or GetAll() to retrieve AbstractFactory-typed pointers to the concrete
  *     factories, then use the factories.
@@ -100,13 +97,18 @@
             AbstractFactoryRegistry::instance()->Register(factory_.get());
         }
 
+        template <typename Arg1Type>
+        Registration(Arg1Type arg1)
+            : factory_(new ConcreteFactoryType(arg1)) {
+            AbstractFactoryRegistry::instance()->Register(factory_.get());
+        }
+
         ~Registration() {
             if (instance_ptr)
                 instance_ptr->Deregister(factory_.get());
         }
 
       private:
-        void* operator new(size_t);  // Prevent heap allocation
         UniquePtr<ConcreteFactoryType> factory_;
     };
 
@@ -128,7 +130,7 @@
 
     AbstractFactoryRegistry()
         : capacity_(DEFAULT_REGISTRY_CAPACITY), size_(0),
-          entries_(new AbstractFactoryType* [capacity_]) {}
+          entries_(new AbstractFactoryType*[capacity_]) {}
     ~AbstractFactoryRegistry() {
         assert(this == instance_ptr);
         instance_ptr = 0;
@@ -181,7 +183,7 @@
 
     if (size_ == capacity_) {
         size_t new_capacity = capacity_ * 2;
-        UniquePtr<AbstractFactoryType* []> new_entries(new AbstractFactoryType* [new_capacity]);
+        UniquePtr<AbstractFactoryType* []> new_entries(new AbstractFactoryType*[new_capacity]);
         if (!new_entries.get()) {
             LOG_S("Tried to register multiple abstract factories for the same type", 0);
             return;
diff --git a/abstract_factory_registry_test.cpp b/abstract_factory_registry_test.cpp
index e08f1ef..605a79b 100644
--- a/abstract_factory_registry_test.cpp
+++ b/abstract_factory_registry_test.cpp
@@ -140,6 +140,10 @@
     EXPECT_TRUE(TestRegistry::instance_ptr == NULL);
 }
 
+template <typename Registry> void register_and_deregister_factory() {
+    TestRegistry::Registration<Registry> registration;
+}
+
 TEST(RegistryTest, DoubleRegister) {
     // Registry instance hasn't been created.
     EXPECT_TRUE(TestRegistry::instance_ptr == NULL);
@@ -156,7 +160,6 @@
 
     // Register another with the same key.
     TestRegistry::Registration<TestFactory<1>> registration2;
-
     // Registry should have been deleted, and pointer zeroed
     EXPECT_TRUE(TestRegistry::instance_ptr == NULL);
 }