| 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/command_line.h" |    7 #include "base/command_line.h" | 
|    8 #include "base/logging.h" |    8 #include "base/logging.h" | 
|    9 #include "base/macros.h" |    9 #include "base/macros.h" | 
|   10 #include "base/strings/string_split.h" |   10 #include "base/strings/string_split.h" | 
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  214     web_frame_->requestExecuteV8Function(v8_context(), function, global, argc, |  214     web_frame_->requestExecuteV8Function(v8_context(), function, global, argc, | 
|  215                                          argv, nullptr); |  215                                          argv, nullptr); | 
|  216   } else { |  216   } else { | 
|  217     // TODO(devlin): This probably isn't safe. |  217     // TODO(devlin): This probably isn't safe. | 
|  218     function->Call(global, argc, argv); |  218     function->Call(global, argc, argv); | 
|  219   } |  219   } | 
|  220 } |  220 } | 
|  221  |  221  | 
|  222 Feature::Availability ScriptContext::GetAvailability( |  222 Feature::Availability ScriptContext::GetAvailability( | 
|  223     const std::string& api_name) { |  223     const std::string& api_name) { | 
 |  224   return GetAvailability(api_name, true); | 
 |  225 } | 
 |  226  | 
 |  227 Feature::Availability ScriptContext::GetAvailability( | 
 |  228     const std::string& api_name, | 
 |  229     bool allow_alias) { | 
|  224   DCHECK(thread_checker_.CalledOnValidThread()); |  230   DCHECK(thread_checker_.CalledOnValidThread()); | 
|  225   if (base::StartsWith(api_name, "test", base::CompareCase::SENSITIVE)) { |  231   if (base::StartsWith(api_name, "test", base::CompareCase::SENSITIVE)) { | 
|  226     bool allowed = base::CommandLine::ForCurrentProcess()-> |  232     bool allowed = base::CommandLine::ForCurrentProcess()-> | 
|  227                        HasSwitch(::switches::kTestType); |  233                        HasSwitch(::switches::kTestType); | 
|  228     Feature::AvailabilityResult result = |  234     Feature::AvailabilityResult result = | 
|  229         allowed ? Feature::IS_AVAILABLE : Feature::MISSING_COMMAND_LINE_SWITCH; |  235         allowed ? Feature::IS_AVAILABLE : Feature::MISSING_COMMAND_LINE_SWITCH; | 
|  230     return Feature::Availability(result, |  236     return Feature::Availability(result, | 
|  231                                  allowed ? "" : "Only allowed in tests"); |  237                                  allowed ? "" : "Only allowed in tests"); | 
|  232   } |  238   } | 
|  233   // Hack: Hosted apps should have the availability of messaging APIs based on |  239   // Hack: Hosted apps should have the availability of messaging APIs based on | 
|  234   // the URL of the page (which might have access depending on some extension |  240   // the URL of the page (which might have access depending on some extension | 
|  235   // with externally_connectable), not whether the app has access to messaging |  241   // with externally_connectable), not whether the app has access to messaging | 
|  236   // (which it won't). |  242   // (which it won't). | 
|  237   const Extension* extension = extension_.get(); |  243   const Extension* extension = extension_.get(); | 
|  238   if (extension && extension->is_hosted_app() && |  244   if (extension && extension->is_hosted_app() && | 
|  239       (api_name == "runtime.connect" || api_name == "runtime.sendMessage")) { |  245       (api_name == "runtime.connect" || api_name == "runtime.sendMessage")) { | 
|  240     extension = NULL; |  246     extension = NULL; | 
|  241   } |  247   } | 
|  242   return ExtensionAPI::GetSharedInstance()->IsAvailable(api_name, extension, |  248   return ExtensionAPI::GetSharedInstance()->IsAvailable( | 
|  243                                                         context_type_, url()); |  249       api_name, extension, context_type_, url(), allow_alias); | 
|  244 } |  250 } | 
|  245  |  251  | 
|  246 void ScriptContext::DispatchEvent(const char* event_name, |  252 void ScriptContext::DispatchEvent(const char* event_name, | 
|  247                                   v8::Local<v8::Array> args) const { |  253                                   v8::Local<v8::Array> args) const { | 
|  248   DCHECK(thread_checker_.CalledOnValidThread()); |  254   DCHECK(thread_checker_.CalledOnValidThread()); | 
|  249   v8::HandleScope handle_scope(isolate()); |  255   v8::HandleScope handle_scope(isolate()); | 
|  250   v8::Context::Scope context_scope(v8_context()); |  256   v8::Context::Scope context_scope(v8_context()); | 
|  251  |  257  | 
|  252   v8::Local<v8::Value> argv[] = {v8::String::NewFromUtf8(isolate(), event_name), |  258   v8::Local<v8::Value> argv[] = {v8::String::NewFromUtf8(isolate(), event_name), | 
|  253                                  args}; |  259                                  args}; | 
|  254   module_system_->CallModuleMethodSafe(kEventBindings, "dispatchEvent", |  260   module_system_->CallModuleMethodSafe(kEventBindings, "dispatchEvent", | 
|  255                                        arraysize(argv), argv); |  261                                        arraysize(argv), argv); | 
|  256 } |  262 } | 
|  257  |  263  | 
|  258 std::string ScriptContext::GetContextTypeDescription() const { |  264 std::string ScriptContext::GetContextTypeDescription() const { | 
|  259   DCHECK(thread_checker_.CalledOnValidThread()); |  265   DCHECK(thread_checker_.CalledOnValidThread()); | 
|  260   return GetContextTypeDescriptionString(context_type_); |  266   return GetContextTypeDescriptionString(context_type_); | 
|  261 } |  267 } | 
|  262  |  268  | 
|  263 std::string ScriptContext::GetEffectiveContextTypeDescription() const { |  269 std::string ScriptContext::GetEffectiveContextTypeDescription() const { | 
|  264   DCHECK(thread_checker_.CalledOnValidThread()); |  270   DCHECK(thread_checker_.CalledOnValidThread()); | 
|  265   return GetContextTypeDescriptionString(effective_context_type_); |  271   return GetContextTypeDescriptionString(effective_context_type_); | 
|  266 } |  272 } | 
|  267  |  273  | 
|  268 bool ScriptContext::IsAnyFeatureAvailableToContext(const Feature& api) { |  274 bool ScriptContext::IsAnyFeatureAvailableToContext(const Feature& api, | 
 |  275                                                    bool allow_alias) { | 
|  269   DCHECK(thread_checker_.CalledOnValidThread()); |  276   DCHECK(thread_checker_.CalledOnValidThread()); | 
|  270   // TODO(lazyboy): Decide what we should do for SERVICE_WORKER_CONTEXT. |  277   // TODO(lazyboy): Decide what we should do for SERVICE_WORKER_CONTEXT. | 
|  271   GURL url = context_type() == Feature::SERVICE_WORKER_CONTEXT |  278   GURL url = context_type() == Feature::SERVICE_WORKER_CONTEXT | 
|  272                  ? url_ |  279                  ? url_ | 
|  273                  : GetDataSourceURLForFrame(web_frame()); |  280                  : GetDataSourceURLForFrame(web_frame()); | 
|  274   return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext( |  281   return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext( | 
|  275       api, extension(), context_type(), url); |  282       api, extension(), context_type(), url, allow_alias); | 
|  276 } |  283 } | 
|  277  |  284  | 
|  278 // static |  285 // static | 
|  279 GURL ScriptContext::GetDataSourceURLForFrame(const blink::WebFrame* frame) { |  286 GURL ScriptContext::GetDataSourceURLForFrame(const blink::WebFrame* frame) { | 
|  280   // Normally we would use frame->document().url() to determine the document's |  287   // Normally we would use frame->document().url() to determine the document's | 
|  281   // URL, but to decide whether to inject a content script, we use the URL from |  288   // URL, but to decide whether to inject a content script, we use the URL from | 
|  282   // the data source. This "quirk" helps prevents content scripts from |  289   // the data source. This "quirk" helps prevents content scripts from | 
|  283   // inadvertently adding DOM elements to the compose iframe in Gmail because |  290   // inadvertently adding DOM elements to the compose iframe in Gmail because | 
|  284   // the compose iframe's dataSource URL is about:blank, but the document URL |  291   // the compose iframe's dataSource URL is about:blank, but the document URL | 
|  285   // changes to match the parent document after Gmail document.writes into |  292   // changes to match the parent document after Gmail document.writes into | 
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  512     v8::Local<v8::Value> argv[]) { |  519     v8::Local<v8::Value> argv[]) { | 
|  513   return context_->CallFunction(function, argc, argv); |  520   return context_->CallFunction(function, argc, argv); | 
|  514 } |  521 } | 
|  515  |  522  | 
|  516 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { |  523 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { | 
|  517   v8::HandleScope handle_scope(context_->isolate()); |  524   v8::HandleScope handle_scope(context_->isolate()); | 
|  518   return gin::PerContextData::From(context_->v8_context())->context_holder(); |  525   return gin::PerContextData::From(context_->v8_context())->context_holder(); | 
|  519 } |  526 } | 
|  520  |  527  | 
|  521 }  // namespace extensions |  528 }  // namespace extensions | 
| OLD | NEW |