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