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 <set> | 10 #include <set> |
10 #include <string> | 11 #include <string> |
11 | 12 |
| 13 #include "base/callback.h" |
12 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
14 #include "gin/per_context_data.h" | 16 #include "gin/per_context_data.h" |
15 | 17 |
16 namespace gin { | 18 namespace gin { |
17 | 19 |
18 struct PendingModule; | 20 struct PendingModule; |
19 | 21 |
20 // This class implements the Asynchronous Module Definition (AMD) API. | 22 // This class implements the Asynchronous Module Definition (AMD) API. |
21 // https://github.com/amdjs/amdjs-api/wiki/AMD | 23 // https://github.com/amdjs/amdjs-api/wiki/AMD |
22 // | 24 // |
23 // Our implementation isn't complete yet. Missing features: | 25 // Our implementation isn't complete yet. Missing features: |
24 // 1) Built-in support for require, exports, and module. | 26 // 1) Built-in support for require, exports, and module. |
25 // 2) Path resoltuion in module names. | 27 // 2) Path resoltuion in module names. |
26 // | 28 // |
27 // For these reasons, we don't have an "amd" property on the "define" | 29 // For these reasons, we don't have an "amd" property on the "define" |
28 // function. The spec says we should only add that property once our | 30 // function. The spec says we should only add that property once our |
29 // implementation complies with the specification. | 31 // implementation complies with the specification. |
30 // | 32 // |
31 class ModuleRegistry : public ContextSupplement { | 33 class ModuleRegistry : public ContextSupplement { |
32 public: | 34 public: |
| 35 typedef base::Callback<void (v8::Handle<v8::Value>)> LoadModuleCallback; |
| 36 |
33 virtual ~ModuleRegistry(); | 37 virtual ~ModuleRegistry(); |
34 | 38 |
35 static ModuleRegistry* From(v8::Handle<v8::Context> context); | 39 static ModuleRegistry* From(v8::Handle<v8::Context> context); |
36 | 40 |
37 static void RegisterGlobals(v8::Isolate* isolate, | 41 static void RegisterGlobals(v8::Isolate* isolate, |
38 v8::Handle<v8::ObjectTemplate> templ); | 42 v8::Handle<v8::ObjectTemplate> templ); |
39 | 43 |
40 // The caller must have already entered our context. | 44 // The caller must have already entered our context. |
41 void AddBuiltinModule(v8::Isolate* isolate, | 45 void AddBuiltinModule(v8::Isolate* isolate, |
42 const std::string& id, | 46 const std::string& id, |
43 v8::Handle<v8::ObjectTemplate> templ); | 47 v8::Handle<v8::ObjectTemplate> templ); |
44 | 48 |
45 // The caller must have already entered our context. | 49 // The caller must have already entered our context. |
46 void AddPendingModule(v8::Isolate* isolate, | 50 void AddPendingModule(v8::Isolate* isolate, |
47 scoped_ptr<PendingModule> pending); | 51 scoped_ptr<PendingModule> pending); |
48 | 52 |
| 53 void LoadModule(v8::Isolate* isolate, |
| 54 const std::string& id, |
| 55 LoadModuleCallback callback); |
| 56 |
49 // The caller must have already entered our context. | 57 // The caller must have already entered our context. |
50 void AttemptToLoadMoreModules(v8::Isolate* isolate); | 58 void AttemptToLoadMoreModules(v8::Isolate* isolate); |
51 | 59 |
52 const std::set<std::string>& available_modules() const { | 60 const std::set<std::string>& available_modules() const { |
53 return available_modules_; | 61 return available_modules_; |
54 } | 62 } |
55 | 63 |
56 const std::set<std::string>& unsatisfied_dependencies() const { | 64 const std::set<std::string>& unsatisfied_dependencies() const { |
57 return unsatisfied_dependencies_; | 65 return unsatisfied_dependencies_; |
58 } | 66 } |
59 | 67 |
60 private: | 68 private: |
61 typedef ScopedVector<PendingModule> PendingModuleVector; | 69 typedef ScopedVector<PendingModule> PendingModuleVector; |
| 70 typedef std::map<std::string, LoadModuleCallback> LoadModuleCallbackMap; |
62 | 71 |
63 explicit ModuleRegistry(v8::Isolate* isolate); | 72 explicit ModuleRegistry(v8::Isolate* isolate); |
64 | 73 |
65 // From ContextSupplement: | 74 // From ContextSupplement: |
66 virtual void Detach(v8::Handle<v8::Context> context) OVERRIDE; | 75 virtual void Detach(v8::Handle<v8::Context> context) OVERRIDE; |
67 | 76 |
68 void Load(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); | 77 void Load(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); |
69 void RegisterModule(v8::Isolate* isolate, | 78 void RegisterModule(v8::Isolate* isolate, |
70 const std::string& id, | 79 const std::string& id, |
71 v8::Handle<v8::Value> module); | 80 v8::Handle<v8::Value> module); |
72 | 81 |
73 bool CheckDependencies(PendingModule* pending); | 82 bool CheckDependencies(PendingModule* pending); |
74 bool AttemptToLoad(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); | 83 bool AttemptToLoad(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); |
75 | 84 |
| 85 v8::Handle<v8::Value> GetModule(v8::Isolate* isolate, const std::string& id); |
| 86 |
76 std::set<std::string> available_modules_; | 87 std::set<std::string> available_modules_; |
77 std::set<std::string> unsatisfied_dependencies_; | 88 std::set<std::string> unsatisfied_dependencies_; |
78 | 89 |
| 90 LoadModuleCallbackMap waiting_callbacks_; |
| 91 |
79 PendingModuleVector pending_modules_; | 92 PendingModuleVector pending_modules_; |
80 v8::Persistent<v8::Object> modules_; | 93 v8::Persistent<v8::Object> modules_; |
81 | 94 |
82 DISALLOW_COPY_AND_ASSIGN(ModuleRegistry); | 95 DISALLOW_COPY_AND_ASSIGN(ModuleRegistry); |
83 }; | 96 }; |
84 | 97 |
85 } // namespace gin | 98 } // namespace gin |
86 | 99 |
87 #endif // GIN_MODULES_MODULE_REGISTRY_H_ | 100 #endif // GIN_MODULES_MODULE_REGISTRY_H_ |
OLD | NEW |