OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/renderer/extensions/schema_generated_bindings.h" | 5 #include "chrome/renderer/extensions/schema_generated_bindings.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include "v8/include/v8.h" | 44 #include "v8/include/v8.h" |
45 #include "webkit/glue/webkit_glue.h" | 45 #include "webkit/glue/webkit_glue.h" |
46 | 46 |
47 using content::V8ValueConverter; | 47 using content::V8ValueConverter; |
48 using extensions::ExtensionAPI; | 48 using extensions::ExtensionAPI; |
49 using WebKit::WebFrame; | 49 using WebKit::WebFrame; |
50 using WebKit::WebSecurityOrigin; | 50 using WebKit::WebSecurityOrigin; |
51 | 51 |
52 namespace { | 52 namespace { |
53 | 53 |
54 const char* kExtensionDeps[] = { | |
55 "extensions/event.js", | |
56 "extensions/json_schema.js", | |
57 "extensions/miscellaneous_bindings.js", | |
58 "extensions/apitest.js" | |
59 }; | |
60 | |
61 // Contains info relevant to a pending API request. | 54 // Contains info relevant to a pending API request. |
62 struct PendingRequest { | 55 struct PendingRequest { |
63 public : | 56 public : |
64 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name, | 57 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name, |
65 const std::string& extension_id) | 58 const std::string& extension_id) |
66 : context(context), name(name), extension_id(extension_id) { | 59 : context(context), name(name), extension_id(extension_id) { |
67 } | 60 } |
68 v8::Persistent<v8::Context> context; | 61 v8::Persistent<v8::Context> context; |
69 std::string name; | 62 std::string name; |
70 std::string extension_id; | 63 std::string extension_id; |
71 }; | 64 }; |
72 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; | 65 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; |
73 | 66 |
74 base::LazyInstance<PendingRequestMap> g_pending_requests = | 67 base::LazyInstance<PendingRequestMap> g_pending_requests = |
75 LAZY_INSTANCE_INITIALIZER; | 68 LAZY_INSTANCE_INITIALIZER; |
76 | 69 |
77 class ExtensionImpl : public ChromeV8Extension { | 70 class ExtensionImpl : public ChromeV8Extension { |
78 public: | 71 public: |
79 explicit ExtensionImpl(ExtensionDispatcher* extension_dispatcher) | 72 explicit ExtensionImpl(ExtensionDispatcher* extension_dispatcher) |
80 : ChromeV8Extension("extensions/schema_generated_bindings.js", | 73 : ChromeV8Extension(extension_dispatcher) { |
81 IDR_SCHEMA_GENERATED_BINDINGS_JS, | 74 RouteStaticFunction("GetExtensionAPIDefinition", |
82 arraysize(kExtensionDeps), | 75 &GetExtensionAPIDefinition); |
83 kExtensionDeps, | 76 RouteStaticFunction("GetNextRequestId", &GetNextRequestId); |
84 extension_dispatcher) { | 77 RouteStaticFunction("StartRequest", &StartRequest); |
| 78 RouteStaticFunction("SetIconCommon", &SetIconCommon); |
85 } | 79 } |
86 | 80 |
87 ~ExtensionImpl() { | 81 ~ExtensionImpl() { |
88 // TODO(aa): It seems that v8 never deletes us, so this doesn't get called. | 82 // TODO(aa): It seems that v8 never deletes us, so this doesn't get called. |
89 // Leaving this in here in case v8's implementation ever changes. | 83 // Leaving this in here in case v8's implementation ever changes. |
90 for (CachedSchemaMap::iterator it = schemas_.begin(); it != schemas_.end(); | 84 for (CachedSchemaMap::iterator it = schemas_.begin(); it != schemas_.end(); |
91 ++it) { | 85 ++it) { |
92 if (!it->second.IsEmpty()) | 86 if (!it->second.IsEmpty()) |
93 it->second.Dispose(); | 87 it->second.Dispose(); |
94 } | 88 } |
95 } | 89 } |
96 | 90 |
97 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | |
98 v8::Handle<v8::String> name) OVERRIDE { | |
99 if (name->Equals(v8::String::New("GetExtensionAPIDefinition"))) { | |
100 return v8::FunctionTemplate::New(GetExtensionAPIDefinition, | |
101 v8::External::New(this)); | |
102 } else if (name->Equals(v8::String::New("GetNextRequestId"))) { | |
103 return v8::FunctionTemplate::New(GetNextRequestId); | |
104 } else if (name->Equals(v8::String::New("StartRequest"))) { | |
105 return v8::FunctionTemplate::New(StartRequest, | |
106 v8::External::New(this)); | |
107 } else if (name->Equals(v8::String::New("SetIconCommon"))) { | |
108 return v8::FunctionTemplate::New(SetIconCommon, | |
109 v8::External::New(this)); | |
110 } | |
111 | |
112 return ChromeV8Extension::GetNativeFunction(name); | |
113 } | |
114 | |
115 private: | 91 private: |
116 static v8::Handle<v8::Value> GetV8SchemaForAPI( | 92 static v8::Handle<v8::Value> GetV8SchemaForAPI( |
117 ExtensionImpl* self, | 93 ExtensionImpl* self, |
118 v8::Handle<v8::Context> context, | 94 v8::Handle<v8::Context> context, |
119 const std::string& api_name) { | 95 const std::string& api_name) { |
120 CachedSchemaMap::iterator maybe_api = self->schemas_.find(api_name); | 96 CachedSchemaMap::iterator maybe_api = self->schemas_.find(api_name); |
121 if (maybe_api != self->schemas_.end()) | 97 if (maybe_api != self->schemas_.end()) |
122 return maybe_api->second; | 98 return maybe_api->second; |
123 | 99 |
124 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); | 100 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 // We store this so that we don't have to parse it over and over again for | 316 // We store this so that we don't have to parse it over and over again for |
341 // every context that uses it. | 317 // every context that uses it. |
342 typedef std::map<std::string, v8::Persistent<v8::Object> > CachedSchemaMap; | 318 typedef std::map<std::string, v8::Persistent<v8::Object> > CachedSchemaMap; |
343 CachedSchemaMap schemas_; | 319 CachedSchemaMap schemas_; |
344 }; | 320 }; |
345 | 321 |
346 } // namespace | 322 } // namespace |
347 | 323 |
348 namespace extensions { | 324 namespace extensions { |
349 | 325 |
350 v8::Extension* SchemaGeneratedBindings::Get( | 326 ChromeV8Extension* SchemaGeneratedBindings::Get( |
351 ExtensionDispatcher* extension_dispatcher) { | 327 ExtensionDispatcher* extension_dispatcher) { |
352 static v8::Extension* extension = new ExtensionImpl(extension_dispatcher); | 328 return new ExtensionImpl(extension_dispatcher); |
353 CHECK_EQ(extension_dispatcher, | |
354 static_cast<ExtensionImpl*>(extension)->extension_dispatcher()); | |
355 return extension; | |
356 } | 329 } |
357 | 330 |
358 // static | 331 // static |
359 void SchemaGeneratedBindings::HandleResponse(const ChromeV8ContextSet& contexts, | 332 void SchemaGeneratedBindings::HandleResponse(const ChromeV8ContextSet& contexts, |
360 int request_id, | 333 int request_id, |
361 bool success, | 334 bool success, |
362 const std::string& response, | 335 const std::string& response, |
363 const std::string& error, | 336 const std::string& error, |
364 std::string* extension_id) { | 337 std::string* extension_id) { |
365 PendingRequestMap::iterator request = | 338 PendingRequestMap::iterator request = |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 const std::string& extension_id) { | 384 const std::string& extension_id) { |
412 for (PendingRequestMap::const_iterator it = g_pending_requests.Get().begin(); | 385 for (PendingRequestMap::const_iterator it = g_pending_requests.Get().begin(); |
413 it != g_pending_requests.Get().end(); ++it) { | 386 it != g_pending_requests.Get().end(); ++it) { |
414 if (it->second->extension_id == extension_id) | 387 if (it->second->extension_id == extension_id) |
415 return true; | 388 return true; |
416 } | 389 } |
417 return false; | 390 return false; |
418 } | 391 } |
419 | 392 |
420 } // namespace | 393 } // namespace |
OLD | NEW |