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 12 matching lines...) Expand all Loading... |
23 #include "third_party/WebKit/public/web/WebFrame.h" | 23 #include "third_party/WebKit/public/web/WebFrame.h" |
24 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 24 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
25 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 25 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
26 #include "third_party/WebKit/public/web/WebView.h" | 26 #include "third_party/WebKit/public/web/WebView.h" |
27 #include "v8/include/v8.h" | 27 #include "v8/include/v8.h" |
28 | 28 |
29 using content::V8ValueConverter; | 29 using content::V8ValueConverter; |
30 | 30 |
31 namespace extensions { | 31 namespace extensions { |
32 | 32 |
| 33 namespace { |
| 34 |
| 35 std::string GetContextTypeDescriptionString(Feature::Context context_type) { |
| 36 switch (context_type) { |
| 37 case Feature::UNSPECIFIED_CONTEXT: |
| 38 return "UNSPECIFIED"; |
| 39 case Feature::BLESSED_EXTENSION_CONTEXT: |
| 40 return "BLESSED_EXTENSION"; |
| 41 case Feature::UNBLESSED_EXTENSION_CONTEXT: |
| 42 return "UNBLESSED_EXTENSION"; |
| 43 case Feature::CONTENT_SCRIPT_CONTEXT: |
| 44 return "CONTENT_SCRIPT"; |
| 45 case Feature::WEB_PAGE_CONTEXT: |
| 46 return "WEB_PAGE"; |
| 47 case Feature::BLESSED_WEB_PAGE_CONTEXT: |
| 48 return "BLESSED_WEB_PAGE"; |
| 49 case Feature::WEBUI_CONTEXT: |
| 50 return "WEBUI"; |
| 51 } |
| 52 NOTREACHED(); |
| 53 return std::string(); |
| 54 } |
| 55 |
| 56 } // namespace |
| 57 |
33 ScriptContext::ScriptContext(const v8::Handle<v8::Context>& v8_context, | 58 ScriptContext::ScriptContext(const v8::Handle<v8::Context>& v8_context, |
34 blink::WebFrame* web_frame, | 59 blink::WebFrame* web_frame, |
35 const Extension* extension, | 60 const Extension* extension, |
36 Feature::Context context_type) | 61 Feature::Context context_type, |
| 62 const Extension* effective_extension, |
| 63 Feature::Context effective_context_type) |
37 : v8_context_(v8_context), | 64 : v8_context_(v8_context), |
38 web_frame_(web_frame), | 65 web_frame_(web_frame), |
39 extension_(extension), | 66 extension_(extension), |
40 context_type_(context_type), | 67 context_type_(context_type), |
| 68 effective_extension_(effective_extension), |
| 69 effective_context_type_(effective_context_type), |
41 safe_builtins_(this), | 70 safe_builtins_(this), |
42 isolate_(v8_context->GetIsolate()) { | 71 isolate_(v8_context->GetIsolate()) { |
43 VLOG(1) << "Created context:\n" | 72 VLOG(1) << "Created context:\n" |
44 << " extension id: " << GetExtensionID() << "\n" | 73 << " extension id: " << GetExtensionID() << "\n" |
45 << " frame: " << web_frame_ << "\n" | 74 << " frame: " << web_frame_ << "\n" |
46 << " URL: " << GetURL() << "\n" | 75 << " URL: " << GetURL() << "\n" |
47 << " context type: " << GetContextTypeDescription(); | 76 << " context type: " << GetContextTypeDescription() << "\n" |
| 77 << " effective extension id: " |
| 78 << (effective_extension_.get() ? effective_extension_->id() : "") |
| 79 << " effective context type: " |
| 80 << GetEffectiveContextTypeDescription(); |
48 gin::PerContextData::From(v8_context)->set_runner(this); | 81 gin::PerContextData::From(v8_context)->set_runner(this); |
49 } | 82 } |
50 | 83 |
51 ScriptContext::~ScriptContext() { | 84 ScriptContext::~ScriptContext() { |
52 VLOG(1) << "Destroyed context for extension\n" | 85 VLOG(1) << "Destroyed context for extension\n" |
53 << " extension id: " << GetExtensionID(); | 86 << " extension id: " << GetExtensionID() << "\n" |
| 87 << " effective extension id: " |
| 88 << (effective_extension_.get() ? effective_extension_->id() : ""); |
54 Invalidate(); | 89 Invalidate(); |
55 } | 90 } |
56 | 91 |
57 void ScriptContext::Invalidate() { | 92 void ScriptContext::Invalidate() { |
58 if (!is_valid()) | 93 if (!is_valid()) |
59 return; | 94 return; |
60 if (module_system_) | 95 if (module_system_) |
61 module_system_->Invalidate(); | 96 module_system_->Invalidate(); |
62 web_frame_ = NULL; | 97 web_frame_ = NULL; |
63 v8_context_.reset(); | 98 v8_context_.reset(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 kEventBindings, "dispatchEvent", arraysize(argv), argv); | 161 kEventBindings, "dispatchEvent", arraysize(argv), argv); |
127 } | 162 } |
128 | 163 |
129 void ScriptContext::DispatchOnUnloadEvent() { | 164 void ScriptContext::DispatchOnUnloadEvent() { |
130 v8::HandleScope handle_scope(isolate()); | 165 v8::HandleScope handle_scope(isolate()); |
131 v8::Context::Scope context_scope(v8_context()); | 166 v8::Context::Scope context_scope(v8_context()); |
132 module_system_->CallModuleMethod("unload_event", "dispatch"); | 167 module_system_->CallModuleMethod("unload_event", "dispatch"); |
133 } | 168 } |
134 | 169 |
135 std::string ScriptContext::GetContextTypeDescription() { | 170 std::string ScriptContext::GetContextTypeDescription() { |
136 switch (context_type_) { | 171 return GetContextTypeDescriptionString(context_type_); |
137 case Feature::UNSPECIFIED_CONTEXT: | 172 } |
138 return "UNSPECIFIED"; | 173 |
139 case Feature::BLESSED_EXTENSION_CONTEXT: | 174 std::string ScriptContext::GetEffectiveContextTypeDescription() { |
140 return "BLESSED_EXTENSION"; | 175 return GetContextTypeDescriptionString(effective_context_type_); |
141 case Feature::UNBLESSED_EXTENSION_CONTEXT: | |
142 return "UNBLESSED_EXTENSION"; | |
143 case Feature::CONTENT_SCRIPT_CONTEXT: | |
144 return "CONTENT_SCRIPT"; | |
145 case Feature::WEB_PAGE_CONTEXT: | |
146 return "WEB_PAGE"; | |
147 case Feature::BLESSED_WEB_PAGE_CONTEXT: | |
148 return "BLESSED_WEB_PAGE"; | |
149 case Feature::WEBUI_CONTEXT: | |
150 return "WEBUI"; | |
151 } | |
152 NOTREACHED(); | |
153 return std::string(); | |
154 } | 176 } |
155 | 177 |
156 GURL ScriptContext::GetURL() const { | 178 GURL ScriptContext::GetURL() const { |
157 return web_frame() ? GetDataSourceURLForFrame(web_frame()) : GURL(); | 179 return web_frame() ? GetDataSourceURLForFrame(web_frame()) : GURL(); |
158 } | 180 } |
159 | 181 |
160 bool ScriptContext::IsAnyFeatureAvailableToContext(const Feature& api) { | 182 bool ScriptContext::IsAnyFeatureAvailableToContext(const Feature& api) { |
161 return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext( | 183 return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext( |
162 api, extension(), context_type(), GetDataSourceURLForFrame(web_frame())); | 184 api, extension(), context_type(), GetDataSourceURLForFrame(web_frame())); |
163 } | 185 } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 v8::Handle<v8::Value> argv[]) { | 266 v8::Handle<v8::Value> argv[]) { |
245 return CallFunction(function, argc, argv); | 267 return CallFunction(function, argc, argv); |
246 } | 268 } |
247 | 269 |
248 gin::ContextHolder* ScriptContext::GetContextHolder() { | 270 gin::ContextHolder* ScriptContext::GetContextHolder() { |
249 v8::HandleScope handle_scope(isolate()); | 271 v8::HandleScope handle_scope(isolate()); |
250 return gin::PerContextData::From(v8_context())->context_holder(); | 272 return gin::PerContextData::From(v8_context())->context_holder(); |
251 } | 273 } |
252 | 274 |
253 } // namespace extensions | 275 } // namespace extensions |
OLD | NEW |