Revert 185709
> Set up V8 bindings for extension/app APIs when they're first used, not on
> context creation. This should gives us a significant reduction in extension/app
> startup time and slightly better memory usage.
> 
> It also gives us better error messages, the chance to complete the 
> implementation of API features, and eventually the ability to expose select
> extension APIs (e.g. extension.sendMessage) to web pages.
> 
> BUG=163678,120070,55316,177163
> [email protected]
> 
> Review URL: https://chromiumcodereview.appspot.com/11571014

[email protected]

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@185815 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/renderer/extensions/native_handler.h b/chrome/renderer/extensions/native_handler.h
index 36ef9918..fd6bda69 100644
--- a/chrome/renderer/extensions/native_handler.h
+++ b/chrome/renderer/extensions/native_handler.h
@@ -5,21 +5,55 @@
 #ifndef CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_
 #define CHROME_RENDERER_EXTENSIONS_NATIVE_HANDLER_H_
 
+#include "base/bind.h"
+#include "base/memory/linked_ptr.h"
 #include "v8/include/v8.h"
 
+#include <string>
+#include <vector>
+
 namespace extensions {
 
+// A NativeHandler is a factory for JS objects with functions on them that map
+// to native C++ functions. Subclasses should call RouteFunction() in their
+// constructor to define functions on the created JS objects.
+//
 // NativeHandlers are intended to be used with a ModuleSystem. The ModuleSystem
 // will assume ownership of the NativeHandler, and as a ModuleSystem is tied to
 // a single v8::Context, this implies that NativeHandlers will also be tied to
-// a single v8::Context.
+// a single v8::context.
 // TODO(koz): Rename this to NativeJavaScriptModule.
 class NativeHandler {
  public:
-  virtual ~NativeHandler() {}
+  explicit NativeHandler(v8::Isolate* isolate);
+  virtual ~NativeHandler();
 
-  // Create a new instance of the object this handler specifies.
-  virtual v8::Handle<v8::Object> NewInstance() = 0;
+  // Create an object with bindings to the native functions defined through
+  // RouteFunction().
+  virtual v8::Handle<v8::Object> NewInstance();
+
+ protected:
+  typedef v8::Handle<v8::Value> (*HandlerFunc)(const v8::Arguments&);
+  typedef base::Callback<v8::Handle<v8::Value>(const v8::Arguments&)>
+      HandlerFunction;
+
+  // Installs a new 'route' from |name| to |handler_function|. This means that
+  // NewInstance()s of this NativeHandler will have a property |name| which
+  // will be handled by |handler_function|.
+  void RouteFunction(const std::string& name,
+                     const HandlerFunction& handler_function);
+
+  void RouteStaticFunction(const std::string& name,
+                           const HandlerFunc handler_func);
+
+ private:
+  static v8::Handle<v8::Value> Router(const v8::Arguments& args);
+
+  std::vector<linked_ptr<HandlerFunction> > handler_functions_;
+  v8::Isolate* isolate_;
+  v8::Persistent<v8::ObjectTemplate> object_template_;
+
+  DISALLOW_COPY_AND_ASSIGN(NativeHandler);
 };
 
 }  // extensions