| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/script_context.h" | 5 #include "extensions/renderer/script_context.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 return "BLESSED_WEB_PAGE"; | 49 return "BLESSED_WEB_PAGE"; |
| 50 case Feature::WEBUI_CONTEXT: | 50 case Feature::WEBUI_CONTEXT: |
| 51 return "WEBUI"; | 51 return "WEBUI"; |
| 52 } | 52 } |
| 53 NOTREACHED(); | 53 NOTREACHED(); |
| 54 return std::string(); | 54 return std::string(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 } // namespace | 57 } // namespace |
| 58 | 58 |
| 59 // A gin::Runner that delegates to its ScriptContext. |
| 60 class ScriptContext::Runner : public gin::Runner { |
| 61 public: |
| 62 explicit Runner(ScriptContext* context); |
| 63 |
| 64 // gin::Runner overrides. |
| 65 void Run(const std::string& source, |
| 66 const std::string& resource_name) override; |
| 67 v8::Handle<v8::Value> Call(v8::Handle<v8::Function> function, |
| 68 v8::Handle<v8::Value> receiver, |
| 69 int argc, |
| 70 v8::Handle<v8::Value> argv[]) override; |
| 71 gin::ContextHolder* GetContextHolder() override; |
| 72 |
| 73 private: |
| 74 ScriptContext* context_; |
| 75 }; |
| 76 |
| 59 ScriptContext::ScriptContext(const v8::Handle<v8::Context>& v8_context, | 77 ScriptContext::ScriptContext(const v8::Handle<v8::Context>& v8_context, |
| 60 blink::WebFrame* web_frame, | 78 blink::WebFrame* web_frame, |
| 61 const Extension* extension, | 79 const Extension* extension, |
| 62 Feature::Context context_type, | 80 Feature::Context context_type, |
| 63 const Extension* effective_extension, | 81 const Extension* effective_extension, |
| 64 Feature::Context effective_context_type) | 82 Feature::Context effective_context_type) |
| 65 : v8_context_(v8_context), | 83 : v8_context_(v8_context), |
| 66 web_frame_(web_frame), | 84 web_frame_(web_frame), |
| 67 extension_(extension), | 85 extension_(extension), |
| 68 context_type_(context_type), | 86 context_type_(context_type), |
| 69 effective_extension_(effective_extension), | 87 effective_extension_(effective_extension), |
| 70 effective_context_type_(effective_context_type), | 88 effective_context_type_(effective_context_type), |
| 71 safe_builtins_(this), | 89 safe_builtins_(this), |
| 72 isolate_(v8_context->GetIsolate()), | 90 isolate_(v8_context->GetIsolate()), |
| 73 url_(web_frame_ ? GetDataSourceURLForFrame(web_frame_) : GURL()) { | 91 url_(web_frame_ ? GetDataSourceURLForFrame(web_frame_) : GURL()), |
| 92 runner_(new Runner(this)) { |
| 74 VLOG(1) << "Created context:\n" | 93 VLOG(1) << "Created context:\n" |
| 75 << " extension id: " << GetExtensionID() << "\n" | 94 << " extension id: " << GetExtensionID() << "\n" |
| 76 << " frame: " << web_frame_ << "\n" | 95 << " frame: " << web_frame_ << "\n" |
| 77 << " URL: " << GetURL() << "\n" | 96 << " URL: " << GetURL() << "\n" |
| 78 << " context type: " << GetContextTypeDescription() << "\n" | 97 << " context type: " << GetContextTypeDescription() << "\n" |
| 79 << " effective extension id: " | 98 << " effective extension id: " |
| 80 << (effective_extension_.get() ? effective_extension_->id() : "") | 99 << (effective_extension_.get() ? effective_extension_->id() : "") |
| 81 << " effective context type: " | 100 << " effective context type: " |
| 82 << GetEffectiveContextTypeDescription(); | 101 << GetEffectiveContextTypeDescription(); |
| 83 gin::PerContextData::From(v8_context)->set_runner(this); | 102 gin::PerContextData::From(v8_context)->set_runner(runner_.get()); |
| 84 } | 103 } |
| 85 | 104 |
| 86 ScriptContext::~ScriptContext() { | 105 ScriptContext::~ScriptContext() { |
| 87 VLOG(1) << "Destroyed context for extension\n" | 106 VLOG(1) << "Destroyed context for extension\n" |
| 88 << " extension id: " << GetExtensionID() << "\n" | 107 << " extension id: " << GetExtensionID() << "\n" |
| 89 << " effective extension id: " | 108 << " effective extension id: " |
| 90 << (effective_extension_.get() ? effective_extension_->id() : ""); | 109 << (effective_extension_.get() ? effective_extension_->id() : ""); |
| 91 Invalidate(); | 110 Invalidate(); |
| 92 } | 111 } |
| 93 | 112 |
| 94 void ScriptContext::Invalidate() { | 113 void ScriptContext::Invalidate() { |
| 95 if (!is_valid()) | 114 if (!is_valid()) |
| 96 return; | 115 return; |
| 97 if (module_system_) | 116 if (module_system_) |
| 98 module_system_->Invalidate(); | 117 module_system_->Invalidate(); |
| 99 web_frame_ = NULL; | 118 web_frame_ = NULL; |
| 100 v8_context_.reset(); | 119 v8_context_.reset(); |
| 120 runner_.reset(); |
| 101 } | 121 } |
| 102 | 122 |
| 103 const std::string& ScriptContext::GetExtensionID() const { | 123 const std::string& ScriptContext::GetExtensionID() const { |
| 104 return extension_.get() ? extension_->id() : base::EmptyString(); | 124 return extension_.get() ? extension_->id() : base::EmptyString(); |
| 105 } | 125 } |
| 106 | 126 |
| 107 content::RenderView* ScriptContext::GetRenderView() const { | 127 content::RenderView* ScriptContext::GetRenderView() const { |
| 108 if (web_frame_ && web_frame_->view()) | 128 if (web_frame_ && web_frame_->view()) |
| 109 return content::RenderView::FromWebView(web_frame_->view()); | 129 return content::RenderView::FromWebView(web_frame_->view()); |
| 110 return NULL; | 130 return NULL; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 270 |
| 251 v8::Handle<v8::Value> retval = module_system()->CallModuleMethod( | 271 v8::Handle<v8::Value> retval = module_system()->CallModuleMethod( |
| 252 "sendRequest", "handleResponse", arraysize(argv), argv); | 272 "sendRequest", "handleResponse", arraysize(argv), argv); |
| 253 | 273 |
| 254 // In debug, the js will validate the callback parameters and return a | 274 // In debug, the js will validate the callback parameters and return a |
| 255 // string if a validation error has occured. | 275 // string if a validation error has occured. |
| 256 DCHECK(retval.IsEmpty() || retval->IsUndefined()) | 276 DCHECK(retval.IsEmpty() || retval->IsUndefined()) |
| 257 << *v8::String::Utf8Value(retval); | 277 << *v8::String::Utf8Value(retval); |
| 258 } | 278 } |
| 259 | 279 |
| 260 void ScriptContext::Run(const std::string& source, | |
| 261 const std::string& resource_name) { | |
| 262 module_system_->RunString(source, resource_name); | |
| 263 } | |
| 264 | |
| 265 v8::Handle<v8::Value> ScriptContext::Call(v8::Handle<v8::Function> function, | |
| 266 v8::Handle<v8::Value> receiver, | |
| 267 int argc, | |
| 268 v8::Handle<v8::Value> argv[]) { | |
| 269 return CallFunction(function, argc, argv); | |
| 270 } | |
| 271 | |
| 272 gin::ContextHolder* ScriptContext::GetContextHolder() { | |
| 273 v8::HandleScope handle_scope(isolate()); | |
| 274 return gin::PerContextData::From(v8_context())->context_holder(); | |
| 275 } | |
| 276 | |
| 277 void ScriptContext::SetContentCapabilities( | 280 void ScriptContext::SetContentCapabilities( |
| 278 const APIPermissionSet& permissions) { | 281 const APIPermissionSet& permissions) { |
| 279 content_capabilities_ = permissions; | 282 content_capabilities_ = permissions; |
| 280 } | 283 } |
| 281 | 284 |
| 282 bool ScriptContext::HasAPIPermission(APIPermission::ID permission) const { | 285 bool ScriptContext::HasAPIPermission(APIPermission::ID permission) const { |
| 283 if (effective_extension_.get()) { | 286 if (effective_extension_.get()) { |
| 284 return effective_extension_->permissions_data()->HasAPIPermission( | 287 return effective_extension_->permissions_data()->HasAPIPermission( |
| 285 permission); | 288 permission); |
| 286 } else if (context_type() == Feature::WEB_PAGE_CONTEXT) { | 289 } else if (context_type() == Feature::WEB_PAGE_CONTEXT) { |
| 287 // Only web page contexts may be granted content capabilities. Other | 290 // Only web page contexts may be granted content capabilities. Other |
| 288 // contexts are either privileged WebUI or extensions with their own set of | 291 // contexts are either privileged WebUI or extensions with their own set of |
| 289 // permissions. | 292 // permissions. |
| 290 if (content_capabilities_.find(permission) != content_capabilities_.end()) | 293 if (content_capabilities_.find(permission) != content_capabilities_.end()) |
| 291 return true; | 294 return true; |
| 292 } | 295 } |
| 293 return false; | 296 return false; |
| 294 } | 297 } |
| 295 | 298 |
| 299 ScriptContext::Runner::Runner(ScriptContext* context) : context_(context) { |
| 300 } |
| 301 void ScriptContext::Runner::Run(const std::string& source, |
| 302 const std::string& resource_name) { |
| 303 context_->module_system()->RunString(source, resource_name); |
| 304 } |
| 305 |
| 306 v8::Handle<v8::Value> ScriptContext::Runner::Call( |
| 307 v8::Handle<v8::Function> function, |
| 308 v8::Handle<v8::Value> receiver, |
| 309 int argc, |
| 310 v8::Handle<v8::Value> argv[]) { |
| 311 return context_->CallFunction(function, argc, argv); |
| 312 } |
| 313 |
| 314 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { |
| 315 v8::HandleScope handle_scope(context_->isolate()); |
| 316 return gin::PerContextData::From(context_->v8_context())->context_holder(); |
| 317 } |
| 318 |
| 296 } // namespace extensions | 319 } // namespace extensions |
| OLD | NEW |