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