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 <memory> | |
8 | |
9 #include "base/command_line.h" | 7 #include "base/command_line.h" |
10 #include "base/logging.h" | 8 #include "base/logging.h" |
11 #include "base/macros.h" | 9 #include "base/macros.h" |
12 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
13 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
15 #include "base/values.h" | 13 #include "base/values.h" |
16 #include "content/public/child/v8_value_converter.h" | 14 #include "content/public/child/v8_value_converter.h" |
17 #include "content/public/common/content_switches.h" | 15 #include "content/public/common/content_switches.h" |
18 #include "content/public/common/url_constants.h" | 16 #include "content/public/common/url_constants.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 Feature::Context effective_context_type) | 99 Feature::Context effective_context_type) |
102 : is_valid_(true), | 100 : is_valid_(true), |
103 v8_context_(v8_context->GetIsolate(), v8_context), | 101 v8_context_(v8_context->GetIsolate(), v8_context), |
104 web_frame_(web_frame), | 102 web_frame_(web_frame), |
105 extension_(extension), | 103 extension_(extension), |
106 context_type_(context_type), | 104 context_type_(context_type), |
107 effective_extension_(effective_extension), | 105 effective_extension_(effective_extension), |
108 effective_context_type_(effective_context_type), | 106 effective_context_type_(effective_context_type), |
109 safe_builtins_(this), | 107 safe_builtins_(this), |
110 isolate_(v8_context->GetIsolate()), | 108 isolate_(v8_context->GetIsolate()), |
111 url_(web_frame_ ? GetDataSourceURLForFrame(web_frame_) : GURL()), | |
112 runner_(new Runner(this)) { | 109 runner_(new Runner(this)) { |
113 VLOG(1) << "Created context:\n" << GetDebugString(); | 110 VLOG(1) << "Created context:\n" << GetDebugString(); |
114 gin::PerContextData* gin_data = gin::PerContextData::From(v8_context); | 111 gin::PerContextData* gin_data = gin::PerContextData::From(v8_context); |
115 CHECK(gin_data); | 112 CHECK(gin_data); |
116 gin_data->set_runner(runner_.get()); | 113 gin_data->set_runner(runner_.get()); |
| 114 if (web_frame_) |
| 115 url_ = GetAccessCheckedFrameURL(web_frame_); |
117 } | 116 } |
118 | 117 |
119 ScriptContext::~ScriptContext() { | 118 ScriptContext::~ScriptContext() { |
120 VLOG(1) << "Destroyed context for extension\n" | 119 VLOG(1) << "Destroyed context for extension\n" |
121 << " extension id: " << GetExtensionID() << "\n" | 120 << " extension id: " << GetExtensionID() << "\n" |
122 << " effective extension id: " | 121 << " effective extension id: " |
123 << (effective_extension_.get() ? effective_extension_->id() : ""); | 122 << (effective_extension_.get() ? effective_extension_->id() : ""); |
124 CHECK(!is_valid_) << "ScriptContexts must be invalidated before destruction"; | 123 CHECK(!is_valid_) << "ScriptContexts must be invalidated before destruction"; |
125 } | 124 } |
126 | 125 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 // changes to match the parent document after Gmail document.writes into | 273 // changes to match the parent document after Gmail document.writes into |
275 // it to create the editor. | 274 // it to create the editor. |
276 // http://code.google.com/p/chromium/issues/detail?id=86742 | 275 // http://code.google.com/p/chromium/issues/detail?id=86742 |
277 blink::WebDataSource* data_source = frame->provisionalDataSource() | 276 blink::WebDataSource* data_source = frame->provisionalDataSource() |
278 ? frame->provisionalDataSource() | 277 ? frame->provisionalDataSource() |
279 : frame->dataSource(); | 278 : frame->dataSource(); |
280 return data_source ? GURL(data_source->request().url()) : GURL(); | 279 return data_source ? GURL(data_source->request().url()) : GURL(); |
281 } | 280 } |
282 | 281 |
283 // static | 282 // static |
| 283 GURL ScriptContext::GetAccessCheckedFrameURL(const blink::WebFrame* frame) { |
| 284 const blink::WebURL& weburl = frame->document().url(); |
| 285 if (weburl.isEmpty()) { |
| 286 blink::WebDataSource* data_source = frame->provisionalDataSource() |
| 287 ? frame->provisionalDataSource() |
| 288 : frame->dataSource(); |
| 289 if (data_source && |
| 290 frame->getSecurityOrigin().canAccess( |
| 291 blink::WebSecurityOrigin::create(data_source->request().url()))) { |
| 292 return GURL(data_source->request().url()); |
| 293 } |
| 294 } |
| 295 return GURL(weburl); |
| 296 } |
| 297 |
| 298 // static |
284 GURL ScriptContext::GetEffectiveDocumentURL(const blink::WebFrame* frame, | 299 GURL ScriptContext::GetEffectiveDocumentURL(const blink::WebFrame* frame, |
285 const GURL& document_url, | 300 const GURL& document_url, |
286 bool match_about_blank) { | 301 bool match_about_blank) { |
287 // Common scenario. If |match_about_blank| is false (as is the case in most | 302 // Common scenario. If |match_about_blank| is false (as is the case in most |
288 // extensions), or if the frame is not an about:-page, just return | 303 // extensions), or if the frame is not an about:-page, just return |
289 // |document_url| (supposedly the URL of the frame). | 304 // |document_url| (supposedly the URL of the frame). |
290 if (!match_about_blank || !document_url.SchemeIs(url::kAboutScheme)) | 305 if (!match_about_blank || !document_url.SchemeIs(url::kAboutScheme)) |
291 return document_url; | 306 return document_url; |
292 | 307 |
293 // Non-sandboxed about:blank and about:srcdoc pages inherit their security | 308 // Non-sandboxed about:blank and about:srcdoc pages inherit their security |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 v8::Local<v8::Value> argv[]) { | 500 v8::Local<v8::Value> argv[]) { |
486 return context_->CallFunction(function, argc, argv); | 501 return context_->CallFunction(function, argc, argv); |
487 } | 502 } |
488 | 503 |
489 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { | 504 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { |
490 v8::HandleScope handle_scope(context_->isolate()); | 505 v8::HandleScope handle_scope(context_->isolate()); |
491 return gin::PerContextData::From(context_->v8_context())->context_holder(); | 506 return gin::PerContextData::From(context_->v8_context())->context_holder(); |
492 } | 507 } |
493 | 508 |
494 } // namespace extensions | 509 } // namespace extensions |
OLD | NEW |