Note that promise is settled on reject. Add test coverage for Resolver.
[chromium-blink-merge.git] / extensions / renderer / object_backed_native_handler.h
blobf31103fb645bf63db32544aef36f6c32179f4ccb
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_RENDERER_OBJECT_BACKED_NATIVE_HANDLER_H_
6 #define EXTENSIONS_RENDERER_OBJECT_BACKED_NATIVE_HANDLER_H_
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/memory/linked_ptr.h"
13 #include "extensions/renderer/native_handler.h"
14 #include "extensions/renderer/scoped_persistent.h"
15 #include "v8/include/v8-util.h"
16 #include "v8/include/v8.h"
18 namespace extensions {
19 class ScriptContext;
21 // An ObjectBackedNativeHandler is a factory for JS objects with functions on
22 // them that map to native C++ functions. Subclasses should call RouteFunction()
23 // in their constructor to define functions on the created JS objects.
24 class ObjectBackedNativeHandler : public NativeHandler {
25 public:
26 explicit ObjectBackedNativeHandler(ScriptContext* context);
27 ~ObjectBackedNativeHandler() override;
29 // Create an object with bindings to the native functions defined through
30 // RouteFunction().
31 v8::Handle<v8::Object> NewInstance() override;
33 v8::Isolate* GetIsolate() const;
35 protected:
36 typedef base::Callback<void(const v8::FunctionCallbackInfo<v8::Value>&)>
37 HandlerFunction;
39 // Installs a new 'route' from |name| to |handler_function|. This means that
40 // NewInstance()s of this ObjectBackedNativeHandler will have a property
41 // |name| which will be handled by |handler_function|.
42 void RouteFunction(const std::string& name,
43 const HandlerFunction& handler_function);
45 ScriptContext* context() const { return context_; }
47 void Invalidate() override;
49 private:
50 // Callback for RouteFunction which routes the V8 call to the correct
51 // base::Bound callback.
52 static void Router(const v8::FunctionCallbackInfo<v8::Value>& args);
54 // When RouteFunction is called we create a v8::Object to hold the data we
55 // need when handling it in Router() - this is the base::Bound function to
56 // route to.
58 // We need a v8::Object because it's possible for v8 to outlive the
59 // base::Bound function; the lifetime of an ObjectBackedNativeHandler is the
60 // lifetime of webkit's involvement with it, not the life of the v8 context.
61 // A scenario when v8 will outlive us is if a frame holds onto the
62 // contentWindow of an iframe after it's removed.
64 // So, we use v8::Objects here to hold that data, effectively refcounting
65 // the data. When |this| is destroyed we remove the base::Bound function from
66 // the object to indicate that it shoudn't be called.
67 typedef v8::PersistentValueVector<v8::Object> RouterData;
68 RouterData router_data_;
70 ScriptContext* context_;
72 ScopedPersistent<v8::ObjectTemplate> object_template_;
74 DISALLOW_COPY_AND_ASSIGN(ObjectBackedNativeHandler);
77 } // namespace extensions
79 #endif // EXTENSIONS_RENDERER_OBJECT_BACKED_NATIVE_HANDLER_H_