| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/chrome_v8_context.h" | 5 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_split.h" | 8 #include "base/string_split.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/common/extensions/extension_set.h" | 10 #include "chrome/common/extensions/extension_set.h" |
| 11 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 11 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
| 12 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 15 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
| 16 | 16 |
| 17 namespace { |
| 18 |
| 19 const char kChromeHidden[] = "chromeHidden"; |
| 20 |
| 21 #ifndef NDEBUG |
| 22 const char kValidateCallbacks[] = "validateCallbacks"; |
| 23 #endif |
| 24 |
| 25 } // namespace |
| 26 |
| 27 |
| 17 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, | 28 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, |
| 18 WebKit::WebFrame* web_frame, | 29 WebKit::WebFrame* web_frame, |
| 19 const std::string& extension_id) | 30 const std::string& extension_id) |
| 20 : v8_context_(v8::Persistent<v8::Context>::New(v8_context)), | 31 : v8_context_(v8::Persistent<v8::Context>::New(v8_context)), |
| 21 web_frame_(web_frame), | 32 web_frame_(web_frame), |
| 22 extension_id_(extension_id) { | 33 extension_id_(extension_id) { |
| 23 VLOG(1) << "Created context for extension\n" | 34 VLOG(1) << "Created context for extension\n" |
| 24 << " id: " << extension_id << "\n" | 35 << " id: " << extension_id << "\n" |
| 25 << " frame: " << web_frame_; | 36 << " frame: " << web_frame_; |
| 26 } | 37 } |
| 27 | 38 |
| 28 ChromeV8Context::~ChromeV8Context() { | 39 ChromeV8Context::~ChromeV8Context() { |
| 29 VLOG(1) << "Destroyed context for extension\n" | 40 VLOG(1) << "Destroyed context for extension\n" |
| 30 << " id: " << extension_id_; | 41 << " id: " << extension_id_; |
| 31 v8_context_.Dispose(); | 42 v8_context_.Dispose(); |
| 32 } | 43 } |
| 33 | 44 |
| 45 // static |
| 46 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( |
| 47 v8::Handle<v8::Context> context) { |
| 48 v8::Local<v8::Object> global = context->Global(); |
| 49 v8::Local<v8::Value> hidden = global->GetHiddenValue( |
| 50 v8::String::New(kChromeHidden)); |
| 51 |
| 52 if (hidden.IsEmpty() || hidden->IsUndefined()) { |
| 53 hidden = v8::Object::New(); |
| 54 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); |
| 55 |
| 56 #ifndef NDEBUG |
| 57 // Tell schema_generated_bindings.js to validate callbacks and events |
| 58 // against their schema definitions in api/extension_api.json. |
| 59 v8::Local<v8::Object>::Cast(hidden) |
| 60 ->Set(v8::String::New(kValidateCallbacks), v8::True()); |
| 61 #endif |
| 62 } |
| 63 |
| 64 DCHECK(hidden->IsObject()); |
| 65 return v8::Local<v8::Object>::Cast(hidden); |
| 66 } |
| 67 |
| 68 v8::Handle<v8::Value> ChromeV8Context::GetChromeHidden() const { |
| 69 v8::Local<v8::Object> global = v8_context_->Global(); |
| 70 return global->GetHiddenValue(v8::String::New(kChromeHidden)); |
| 71 } |
| 72 |
| 34 content::RenderView* ChromeV8Context::GetRenderView() const { | 73 content::RenderView* ChromeV8Context::GetRenderView() const { |
| 35 if (web_frame_ && web_frame_->view()) | 74 if (web_frame_ && web_frame_->view()) |
| 36 return content::RenderView::FromWebView(web_frame_->view()); | 75 return content::RenderView::FromWebView(web_frame_->view()); |
| 37 else | 76 else |
| 38 return NULL; | 77 return NULL; |
| 39 } | 78 } |
| 40 | 79 |
| 41 bool ChromeV8Context::CallChromeHiddenMethod( | 80 bool ChromeV8Context::CallChromeHiddenMethod( |
| 42 const std::string& function_name, | 81 const std::string& function_name, |
| 43 int argc, | 82 int argc, |
| 44 v8::Handle<v8::Value>* argv, | 83 v8::Handle<v8::Value>* argv, |
| 45 v8::Handle<v8::Value>* result) const { | 84 v8::Handle<v8::Value>* result) const { |
| 46 v8::Context::Scope context_scope(v8_context_); | 85 v8::Context::Scope context_scope(v8_context_); |
| 47 | 86 |
| 48 // Look up the function name, which may be a sub-property like | 87 // Look up the function name, which may be a sub-property like |
| 49 // "Port.dispatchOnMessage" in the hidden global variable. | 88 // "Port.dispatchOnMessage" in the hidden global variable. |
| 50 v8::Local<v8::Value> value = v8::Local<v8::Value>::New( | 89 v8::Local<v8::Value> value = v8::Local<v8::Value>::New(GetChromeHidden()); |
| 51 ChromeV8Extension::GetChromeHidden(v8_context_)); | 90 if (value.IsEmpty()) |
| 91 return false; |
| 92 |
| 52 std::vector<std::string> components; | 93 std::vector<std::string> components; |
| 53 base::SplitStringDontTrim(function_name, '.', &components); | 94 base::SplitStringDontTrim(function_name, '.', &components); |
| 54 for (size_t i = 0; i < components.size(); ++i) { | 95 for (size_t i = 0; i < components.size(); ++i) { |
| 55 if (!value.IsEmpty()) { | 96 if (!value.IsEmpty()) { |
| 56 value = v8::Local<v8::Object>::Cast(value)->Get( | 97 value = v8::Local<v8::Object>::Cast(value)->Get( |
| 57 v8::String::New(components[i].c_str())); | 98 v8::String::New(components[i].c_str())); |
| 58 } | 99 } |
| 59 } | 100 } |
| 60 | 101 |
| 61 // TODO(aa): CHECK that this succeeds. Can't do this now because not all | 102 // TODO(aa): CHECK that this succeeds. Can't do this now because not all |
| (...skipping 18 matching lines...) Expand all Loading... |
| 80 argv[0] = v8::String::New(extension_id_.c_str()); | 121 argv[0] = v8::String::New(extension_id_.c_str()); |
| 81 argv[1] = v8::Boolean::New(is_extension_process); | 122 argv[1] = v8::Boolean::New(is_extension_process); |
| 82 argv[2] = v8::Boolean::New(is_incognito_process); | 123 argv[2] = v8::Boolean::New(is_incognito_process); |
| 83 CallChromeHiddenMethod("dispatchOnLoad", arraysize(argv), argv, NULL); | 124 CallChromeHiddenMethod("dispatchOnLoad", arraysize(argv), argv, NULL); |
| 84 } | 125 } |
| 85 | 126 |
| 86 void ChromeV8Context::DispatchOnUnloadEvent() const { | 127 void ChromeV8Context::DispatchOnUnloadEvent() const { |
| 87 v8::HandleScope handle_scope; | 128 v8::HandleScope handle_scope; |
| 88 CallChromeHiddenMethod("dispatchOnUnload", 0, NULL, NULL); | 129 CallChromeHiddenMethod("dispatchOnUnload", 0, NULL, NULL); |
| 89 } | 130 } |
| OLD | NEW |