OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GIN_MODULES_MODULE_REGISTRY_H_ | 5 #ifndef GIN_MODULES_MODULE_REGISTRY_H_ |
6 #define GIN_MODULES_MODULE_REGISTRY_H_ | 6 #define GIN_MODULES_MODULE_REGISTRY_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
| 12 #include <vector> |
12 | 13 |
13 #include "base/callback.h" | 14 #include "base/callback.h" |
14 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
16 #include "gin/gin_export.h" | 17 #include "gin/gin_export.h" |
17 #include "gin/per_context_data.h" | 18 #include "gin/per_context_data.h" |
18 | 19 |
19 namespace gin { | 20 namespace gin { |
20 | 21 |
21 struct PendingModule; | 22 struct GIN_EXPORT PendingModule { |
| 23 PendingModule(); |
| 24 ~PendingModule(); |
| 25 |
| 26 std::string id; |
| 27 std::vector<std::string> dependencies; |
| 28 v8::Persistent<v8::Value> factory; |
| 29 }; |
| 30 |
| 31 // ModuleLoader is responsible for registering the necessary bindings for |
| 32 // modules with v8 as well as responding to the bindings (by way of |
| 33 // Load()). Subclasses should invoke the appropriate RegisterGlobals |
| 34 // function. ModuleLoader is owned by the context. |
| 35 class GIN_EXPORT ModuleLoader : public ContextSupplement { |
| 36 public: |
| 37 static ModuleLoader* From(v8::Handle<v8::Context> context); |
| 38 |
| 39 // Loads the specified module. Loading need not be synchronous, and often is |
| 40 // not. |
| 41 virtual void Load(v8::Handle<v8::Context> context, |
| 42 scoped_ptr<PendingModule> pending) = 0; |
| 43 |
| 44 protected: |
| 45 explicit ModuleLoader(v8::Handle<v8::Context> context); |
| 46 virtual ~ModuleLoader(); |
| 47 |
| 48 static void RegisterGlobals(v8::Isolate* isolate, |
| 49 v8::Handle<v8::ObjectTemplate> templ); |
| 50 static void RegisterGlobalsObject(v8::Isolate* isolate, |
| 51 v8::Handle<v8::Object> obj); |
| 52 |
| 53 private: |
| 54 // From ContextSupplement: |
| 55 virtual void Detach(v8::Handle<v8::Context> context) OVERRIDE; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(ModuleLoader); |
| 58 }; |
22 | 59 |
23 // This class implements the Asynchronous Module Definition (AMD) API. | 60 // This class implements the Asynchronous Module Definition (AMD) API. |
24 // https://github.com/amdjs/amdjs-api/wiki/AMD | 61 // https://github.com/amdjs/amdjs-api/wiki/AMD |
25 // | 62 // |
26 // Our implementation isn't complete yet. Missing features: | 63 // Our implementation isn't complete yet. Missing features: |
27 // 1) Built-in support for require, exports, and module. | 64 // 1) Built-in support for require, exports, and module. |
28 // 2) Path resoltuion in module names. | 65 // 2) Path resoltuion in module names. |
29 // | 66 // |
30 // For these reasons, we don't have an "amd" property on the "define" | 67 // For these reasons, we don't have an "amd" property on the "define" |
31 // function. The spec says we should only add that property once our | 68 // function. The spec says we should only add that property once our |
32 // implementation complies with the specification. | 69 // implementation complies with the specification. |
33 // | 70 // |
34 class GIN_EXPORT ModuleRegistry : public ContextSupplement { | 71 class GIN_EXPORT ModuleRegistry : public ContextSupplement { |
35 public: | 72 public: |
36 typedef base::Callback<void (v8::Handle<v8::Value>)> LoadModuleCallback; | 73 typedef base::Callback<void (v8::Handle<v8::Value>)> LoadModuleCallback; |
37 | 74 |
38 virtual ~ModuleRegistry(); | 75 virtual ~ModuleRegistry(); |
39 | 76 |
40 static ModuleRegistry* From(v8::Handle<v8::Context> context); | 77 static ModuleRegistry* From(v8::Handle<v8::Context> context); |
41 | 78 |
42 static void RegisterGlobals(v8::Isolate* isolate, | |
43 v8::Handle<v8::ObjectTemplate> templ); | |
44 | |
45 // The caller must have already entered our context. | 79 // The caller must have already entered our context. |
46 void AddBuiltinModule(v8::Isolate* isolate, const std::string& id, | 80 void AddBuiltinModule(v8::Isolate* isolate, const std::string& id, |
47 v8::Handle<v8::Value> module); | 81 v8::Handle<v8::Value> module); |
48 | 82 |
49 // The caller must have already entered our context. | 83 // The caller must have already entered our context. |
50 void AddPendingModule(v8::Isolate* isolate, | 84 void AddPendingModule(v8::Isolate* isolate, |
51 scoped_ptr<PendingModule> pending); | 85 scoped_ptr<PendingModule> pending); |
52 | 86 |
53 void LoadModule(v8::Isolate* isolate, | 87 void LoadModule(v8::Isolate* isolate, |
54 const std::string& id, | 88 const std::string& id, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 125 |
92 PendingModuleVector pending_modules_; | 126 PendingModuleVector pending_modules_; |
93 v8::Persistent<v8::Object> modules_; | 127 v8::Persistent<v8::Object> modules_; |
94 | 128 |
95 DISALLOW_COPY_AND_ASSIGN(ModuleRegistry); | 129 DISALLOW_COPY_AND_ASSIGN(ModuleRegistry); |
96 }; | 130 }; |
97 | 131 |
98 } // namespace gin | 132 } // namespace gin |
99 | 133 |
100 #endif // GIN_MODULES_MODULE_REGISTRY_H_ | 134 #endif // GIN_MODULES_MODULE_REGISTRY_H_ |
OLD | NEW |