| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/bindings_utils.h" | 5 #include "chrome/renderer/extensions/bindings_utils.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/renderer/render_view.h" | 8 #include "chrome/renderer/render_view.h" |
| 9 #include "webkit/api/public/WebFrame.h" | 9 #include "webkit/api/public/WebFrame.h" |
| 10 | 10 |
| 11 using WebKit::WebFrame; | 11 using WebKit::WebFrame; |
| 12 | 12 |
| 13 namespace bindings_utils { | 13 namespace bindings_utils { |
| 14 | 14 |
| 15 const char* kChromeHidden = "chromeHidden"; | 15 const char* kChromeHidden = "chromeHidden"; |
| 16 const char* kValidateCallbacks = "validateCallbacks"; |
| 16 | 17 |
| 17 struct SingletonData { | 18 struct SingletonData { |
| 18 ContextList contexts; | 19 ContextList contexts; |
| 19 PendingRequestMap pending_requests; | 20 PendingRequestMap pending_requests; |
| 20 }; | 21 }; |
| 21 | 22 |
| 22 // ExtensionBase | 23 // ExtensionBase |
| 23 | 24 |
| 24 v8::Handle<v8::FunctionTemplate> | 25 v8::Handle<v8::FunctionTemplate> |
| 25 ExtensionBase::GetNativeFunction(v8::Handle<v8::String> name) { | 26 ExtensionBase::GetNativeFunction(v8::Handle<v8::String> name) { |
| 26 if (name->Equals(v8::String::New("GetChromeHidden"))) { | 27 if (name->Equals(v8::String::New("GetChromeHidden"))) { |
| 27 return v8::FunctionTemplate::New(GetChromeHidden); | 28 return v8::FunctionTemplate::New(GetChromeHidden); |
| 28 } | 29 } |
| 29 | 30 |
| 30 return v8::Handle<v8::FunctionTemplate>(); | 31 return v8::Handle<v8::FunctionTemplate>(); |
| 31 } | 32 } |
| 32 | 33 |
| 33 v8::Handle<v8::Value> ExtensionBase::GetChromeHidden( | 34 v8::Handle<v8::Value> ExtensionBase::GetChromeHidden( |
| 34 const v8::Arguments& args) { | 35 const v8::Arguments& args) { |
| 35 v8::Local<v8::Context> context = v8::Context::GetCurrent(); | 36 v8::Local<v8::Context> context = v8::Context::GetCurrent(); |
| 36 v8::Local<v8::Object> global = context->Global(); | 37 v8::Local<v8::Object> global = context->Global(); |
| 37 v8::Local<v8::Value> hidden = global->GetHiddenValue( | 38 v8::Local<v8::Value> hidden = global->GetHiddenValue( |
| 38 v8::String::New(kChromeHidden)); | 39 v8::String::New(kChromeHidden)); |
| 39 | 40 |
| 40 if (hidden.IsEmpty() || hidden->IsUndefined()) { | 41 if (hidden.IsEmpty() || hidden->IsUndefined()) { |
| 41 hidden = v8::Object::New(); | 42 hidden = v8::Object::New(); |
| 42 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); | 43 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); |
| 44 |
| 45 #ifdef _DEBUG |
| 46 // Tell extension_process_bindings.js to validate callbacks and events |
| 47 // against their schema definitions in api/extension_api.json. |
| 48 v8::Local<v8::Object>::Cast(hidden) |
| 49 ->Set(v8::String::New(kValidateCallbacks), v8::True()); |
| 50 #endif |
| 43 } | 51 } |
| 44 | 52 |
| 45 DCHECK(hidden->IsObject()); | 53 DCHECK(hidden->IsObject()); |
| 46 return hidden; | 54 return hidden; |
| 47 } | 55 } |
| 48 | 56 |
| 49 ContextList& GetContexts() { | 57 ContextList& GetContexts() { |
| 50 return Singleton<SingletonData>::get()->contexts; | 58 return Singleton<SingletonData>::get()->contexts; |
| 51 } | 59 } |
| 52 | 60 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 95 |
| 88 WebView* webview = webframe->view(); | 96 WebView* webview = webframe->view(); |
| 89 if (!webview) | 97 if (!webview) |
| 90 return NULL; // can happen during closing | 98 return NULL; // can happen during closing |
| 91 | 99 |
| 92 RenderView* renderview = static_cast<RenderView*>(webview->GetDelegate()); | 100 RenderView* renderview = static_cast<RenderView*>(webview->GetDelegate()); |
| 93 DCHECK(renderview) << "Encountered a WebView without a WebViewDelegate"; | 101 DCHECK(renderview) << "Encountered a WebView without a WebViewDelegate"; |
| 94 return renderview; | 102 return renderview; |
| 95 } | 103 } |
| 96 | 104 |
| 97 void CallFunctionInContext(v8::Handle<v8::Context> context, | 105 v8::Handle<v8::Value> CallFunctionInContext(v8::Handle<v8::Context> context, |
| 98 const std::string& function_name, int argc, | 106 const std::string& function_name, int argc, |
| 99 v8::Handle<v8::Value>* argv) { | 107 v8::Handle<v8::Value>* argv) { |
| 100 v8::Context::Scope context_scope(context); | 108 v8::Context::Scope context_scope(context); |
| 101 | 109 |
| 102 // Look up the function name, which may be a sub-property like | 110 // Look up the function name, which may be a sub-property like |
| 103 // "Port.dispatchOnMessage" in the hidden global variable. | 111 // "Port.dispatchOnMessage" in the hidden global variable. |
| 104 v8::Local<v8::Value> value = | 112 v8::Local<v8::Value> value = |
| 105 context->Global()->GetHiddenValue(v8::String::New(kChromeHidden)); | 113 context->Global()->GetHiddenValue(v8::String::New(kChromeHidden)); |
| 106 std::vector<std::string> components; | 114 std::vector<std::string> components; |
| 107 SplitStringDontTrim(function_name, '.', &components); | 115 SplitStringDontTrim(function_name, '.', &components); |
| 108 for (size_t i = 0; i < components.size(); ++i) { | 116 for (size_t i = 0; i < components.size(); ++i) { |
| 109 if (!value.IsEmpty() && value->IsObject()) | 117 if (!value.IsEmpty() && value->IsObject()) |
| 110 value = value->ToObject()->Get(v8::String::New(components[i].c_str())); | 118 value = value->ToObject()->Get(v8::String::New(components[i].c_str())); |
| 111 } | 119 } |
| 112 if (value.IsEmpty() || !value->IsFunction()) { | 120 if (value.IsEmpty() || !value->IsFunction()) { |
| 113 NOTREACHED(); | 121 NOTREACHED(); |
| 114 return; | 122 return v8::Undefined(); |
| 115 } | 123 } |
| 116 | 124 |
| 117 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); | 125 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); |
| 118 if (!function.IsEmpty()) | 126 if (!function.IsEmpty()) |
| 119 function->Call(v8::Object::New(), argc, argv); | 127 return function->Call(v8::Object::New(), argc, argv); |
| 128 |
| 129 return v8::Undefined(); |
| 120 } | 130 } |
| 121 | 131 |
| 122 } // namespace bindings_utils | 132 } // namespace bindings_utils |
| OLD | NEW |