OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "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/values.h" | 10 #include "base/values.h" |
11 #include "chrome/renderer/extensions/chrome_v8_extension.h" | |
12 #include "chrome/renderer/extensions/module_system.h" | |
13 #include "chrome/renderer/extensions/user_script_slave.h" | |
14 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
15 #include "content/public/renderer/v8_value_converter.h" | 12 #include "content/public/renderer/v8_value_converter.h" |
16 #include "extensions/common/extension.h" | 13 #include "extensions/common/extension.h" |
17 #include "extensions/common/extension_api.h" | 14 #include "extensions/common/extension_api.h" |
18 #include "extensions/common/extension_urls.h" | 15 #include "extensions/common/extension_urls.h" |
19 #include "extensions/common/features/base_feature_provider.h" | 16 #include "extensions/common/features/base_feature_provider.h" |
17 #include "third_party/WebKit/public/web/WebDataSource.h" | |
20 #include "third_party/WebKit/public/web/WebFrame.h" | 18 #include "third_party/WebKit/public/web/WebFrame.h" |
21 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 19 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
22 #include "third_party/WebKit/public/web/WebView.h" | 20 #include "third_party/WebKit/public/web/WebView.h" |
23 #include "v8/include/v8.h" | 21 #include "v8/include/v8.h" |
24 | 22 |
25 using content::V8ValueConverter; | 23 using content::V8ValueConverter; |
26 | 24 |
27 namespace extensions { | 25 namespace extensions { |
28 | 26 |
29 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, | 27 ScriptContext::ScriptContext(v8::Handle<v8::Context> v8_context, |
30 blink::WebFrame* web_frame, | 28 blink::WebFrame* web_frame, |
31 const Extension* extension, | 29 const Extension* extension, |
32 Feature::Context context_type) | 30 Feature::Context context_type) |
33 : v8_context_(v8_context), | 31 : v8_context_(v8_context), |
34 web_frame_(web_frame), | 32 web_frame_(web_frame), |
35 extension_(extension), | 33 extension_(extension), |
36 context_type_(context_type), | 34 context_type_(context_type), |
37 safe_builtins_(this), | 35 safe_builtins_(this), |
38 pepper_request_proxy_(this), | |
39 isolate_(v8_context->GetIsolate()) { | 36 isolate_(v8_context->GetIsolate()) { |
40 VLOG(1) << "Created context:\n" | 37 VLOG(1) << "Created context:\n" |
41 << " extension id: " << GetExtensionID() << "\n" | 38 << " extension id: " << GetExtensionID() << "\n" |
42 << " frame: " << web_frame_ << "\n" | 39 << " frame: " << web_frame_ << "\n" |
43 << " context type: " << GetContextTypeDescription(); | 40 << " context type: " << GetContextTypeDescription(); |
44 } | 41 } |
45 | 42 |
46 ChromeV8Context::~ChromeV8Context() { | 43 ScriptContext::~ScriptContext() { |
47 VLOG(1) << "Destroyed context for extension\n" | 44 VLOG(1) << "Destroyed context for extension\n" |
48 << " extension id: " << GetExtensionID(); | 45 << " extension id: " << GetExtensionID(); |
49 Invalidate(); | 46 Invalidate(); |
50 } | 47 } |
51 | 48 |
52 void ChromeV8Context::Invalidate() { | 49 void ScriptContext::Invalidate() { |
53 if (!is_valid()) | 50 if (!is_valid()) |
54 return; | 51 return; |
55 if (module_system_) | 52 if (module_system_) |
56 module_system_->Invalidate(); | 53 module_system_->Invalidate(); |
57 web_frame_ = NULL; | 54 web_frame_ = NULL; |
58 v8_context_.reset(); | 55 v8_context_.reset(); |
59 } | 56 } |
60 | 57 |
61 std::string ChromeV8Context::GetExtensionID() const { | 58 std::string ScriptContext::GetExtensionID() const { |
62 return extension_.get() ? extension_->id() : std::string(); | 59 return extension_.get() ? extension_->id() : std::string(); |
63 } | 60 } |
64 | 61 |
65 content::RenderView* ChromeV8Context::GetRenderView() const { | 62 content::RenderView* ScriptContext::GetRenderView() const { |
66 if (web_frame_ && web_frame_->view()) | 63 if (web_frame_ && web_frame_->view()) |
67 return content::RenderView::FromWebView(web_frame_->view()); | 64 return content::RenderView::FromWebView(web_frame_->view()); |
68 else | 65 else |
69 return NULL; | 66 return NULL; |
70 } | 67 } |
71 | 68 |
72 GURL ChromeV8Context::GetURL() const { | 69 v8::Local<v8::Value> ScriptContext::CallFunction( |
73 return web_frame_ ? | |
74 UserScriptSlave::GetDataSourceURLForFrame(web_frame_) : GURL(); | |
75 } | |
76 | |
77 v8::Local<v8::Value> ChromeV8Context::CallFunction( | |
78 v8::Handle<v8::Function> function, | 70 v8::Handle<v8::Function> function, |
79 int argc, | 71 int argc, |
80 v8::Handle<v8::Value> argv[]) const { | 72 v8::Handle<v8::Value> argv[]) const { |
81 v8::EscapableHandleScope handle_scope(isolate()); | 73 v8::EscapableHandleScope handle_scope(isolate()); |
82 v8::Context::Scope scope(v8_context()); | 74 v8::Context::Scope scope(v8_context()); |
83 | 75 |
84 blink::WebScopedMicrotaskSuppression suppression; | 76 blink::WebScopedMicrotaskSuppression suppression; |
85 if (!is_valid()) { | 77 if (!is_valid()) { |
86 return handle_scope.Escape( | 78 return handle_scope.Escape( |
87 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); | 79 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); |
88 } | 80 } |
89 | 81 |
90 v8::Handle<v8::Object> global = v8_context()->Global(); | 82 v8::Handle<v8::Object> global = v8_context()->Global(); |
91 if (!web_frame_) | 83 if (!web_frame_) |
92 return handle_scope.Escape(function->Call(global, argc, argv)); | 84 return handle_scope.Escape(function->Call(global, argc, argv)); |
93 return handle_scope.Escape( | 85 return handle_scope.Escape( |
94 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( | 86 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( |
95 function, global, argc, argv))); | 87 function, global, argc, argv))); |
96 } | 88 } |
97 | 89 |
98 bool ChromeV8Context::IsAnyFeatureAvailableToContext(const Feature& api) { | 90 Feature::Availability ScriptContext::GetAvailability( |
99 return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext( | |
100 api, | |
101 extension_.get(), | |
102 context_type_, | |
103 UserScriptSlave::GetDataSourceURLForFrame(web_frame_)); | |
104 } | |
105 | |
106 Feature::Availability ChromeV8Context::GetAvailability( | |
107 const std::string& api_name) { | 91 const std::string& api_name) { |
108 // Hack: Hosted apps should have the availability of messaging APIs based on | 92 // Hack: Hosted apps should have the availability of messaging APIs based on |
109 // the URL of the page (which might have access depending on some extension | 93 // the URL of the page (which might have access depending on some extension |
110 // with externally_connectable), not whether the app has access to messaging | 94 // with externally_connectable), not whether the app has access to messaging |
111 // (which it won't). | 95 // (which it won't). |
112 const Extension* extension = extension_.get(); | 96 const Extension* extension = extension_.get(); |
113 if (extension && extension->is_hosted_app() && | 97 if (extension && extension->is_hosted_app() && |
114 (api_name == "runtime.connect" || api_name == "runtime.sendMessage")) { | 98 (api_name == "runtime.connect" || api_name == "runtime.sendMessage")) { |
115 extension = NULL; | 99 extension = NULL; |
116 } | 100 } |
117 return ExtensionAPI::GetSharedInstance()->IsAvailable(api_name, | 101 return ExtensionAPI::GetSharedInstance()->IsAvailable( |
118 extension, | 102 api_name, extension, context_type_, GetURL()); |
119 context_type_, | |
120 GetURL()); | |
121 } | 103 } |
122 | 104 |
123 void ChromeV8Context::DispatchEvent(const char* event_name, | 105 void ScriptContext::DispatchEvent(const char* event_name, |
124 v8::Handle<v8::Array> args) const { | 106 v8::Handle<v8::Array> args) const { |
125 v8::HandleScope handle_scope(isolate()); | 107 v8::HandleScope handle_scope(isolate()); |
126 v8::Context::Scope context_scope(v8_context()); | 108 v8::Context::Scope context_scope(v8_context()); |
127 | 109 |
128 v8::Handle<v8::Value> argv[] = { | 110 v8::Handle<v8::Value> argv[] = { |
129 v8::String::NewFromUtf8(isolate(), event_name), args}; | 111 v8::String::NewFromUtf8(isolate(), event_name), args}; |
130 module_system_->CallModuleMethod( | 112 module_system_->CallModuleMethod( |
131 kEventBindings, "dispatchEvent", arraysize(argv), argv); | 113 kEventBindings, "dispatchEvent", arraysize(argv), argv); |
132 } | 114 } |
133 | 115 |
134 void ChromeV8Context::DispatchOnUnloadEvent() { | 116 void ScriptContext::DispatchOnUnloadEvent() { |
135 module_system_->CallModuleMethod("unload_event", "dispatch"); | 117 module_system_->CallModuleMethod("unload_event", "dispatch"); |
136 } | 118 } |
137 | 119 |
138 std::string ChromeV8Context::GetContextTypeDescription() { | 120 std::string ScriptContext::GetContextTypeDescription() { |
139 switch (context_type_) { | 121 switch (context_type_) { |
140 case Feature::UNSPECIFIED_CONTEXT: return "UNSPECIFIED"; | 122 case Feature::UNSPECIFIED_CONTEXT: |
141 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; | 123 return "UNSPECIFIED"; |
142 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; | 124 case Feature::BLESSED_EXTENSION_CONTEXT: |
143 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; | 125 return "BLESSED_EXTENSION"; |
144 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; | 126 case Feature::UNBLESSED_EXTENSION_CONTEXT: |
145 case Feature::BLESSED_WEB_PAGE_CONTEXT: return "BLESSED_WEB_PAGE"; | 127 return "UNBLESSED_EXTENSION"; |
128 case Feature::CONTENT_SCRIPT_CONTEXT: | |
129 return "CONTENT_SCRIPT"; | |
130 case Feature::WEB_PAGE_CONTEXT: | |
131 return "WEB_PAGE"; | |
132 case Feature::BLESSED_WEB_PAGE_CONTEXT: | |
133 return "BLESSED_WEB_PAGE"; | |
146 } | 134 } |
147 NOTREACHED(); | 135 NOTREACHED(); |
148 return std::string(); | 136 return std::string(); |
149 } | 137 } |
150 | 138 |
151 ChromeV8Context* ChromeV8Context::GetContext() { | 139 GURL ScriptContext::GetURL() const { |
152 return this; | 140 return web_frame() ? GetDataSourceURLForFrame(web_frame()) : GURL(); |
153 } | 141 } |
154 | 142 |
155 void ChromeV8Context::OnResponseReceived(const std::string& name, | 143 bool ScriptContext::IsAnyFeatureAvailableToContext(const Feature& api) { |
156 int request_id, | 144 return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext( |
157 bool success, | 145 api, extension(), context_type(), GetDataSourceURLForFrame(web_frame())); |
158 const base::ListValue& response, | 146 } |
159 const std::string& error) { | 147 |
148 // static | |
149 GURL ScriptContext::GetDataSourceURLForFrame(const blink::WebFrame* frame) { | |
150 // Normally we would use frame->document().url() to determine the document's | |
151 // URL, but to decide whether to inject a content script, we use the URL from | |
152 // the data source. This "quirk" helps prevents content scripts from | |
153 // inadvertently adding DOM elements to the compose iframe in Gmail because | |
154 // the compose iframe's dataSource URL is about:blank, but the document URL | |
155 // changes to match the parent document after Gmail document.writes into | |
156 // it to create the editor. | |
157 // http://code.google.com/p/chromium/issues/detail?id=86742 | |
158 blink::WebDataSource* data_source = frame->provisionalDataSource() | |
159 ? frame->provisionalDataSource() | |
160 : frame->dataSource(); | |
161 CHECK(data_source); | |
162 return GURL(data_source->request().url()); | |
163 } | |
164 | |
165 ScriptContext* ScriptContext::GetContext() { return this; } | |
166 | |
167 void ScriptContext::OnResponseReceived(const std::string& name, | |
168 int request_id, | |
169 bool success, | |
170 const base::ListValue& response, | |
171 const std::string& error) { | |
Ken Rockot(use gerrit already)
2014/04/11 17:05:27
No reason this shouldn't have been moved already a
| |
160 v8::HandleScope handle_scope(isolate()); | 172 v8::HandleScope handle_scope(isolate()); |
161 | 173 |
162 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 174 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
163 v8::Handle<v8::Value> argv[] = { | 175 v8::Handle<v8::Value> argv[] = { |
164 v8::Integer::New(isolate(), request_id), | 176 v8::Integer::New(isolate(), request_id), |
165 v8::String::NewFromUtf8(isolate(), name.c_str()), | 177 v8::String::NewFromUtf8(isolate(), name.c_str()), |
166 v8::Boolean::New(isolate(), success), | 178 v8::Boolean::New(isolate(), success), |
167 converter->ToV8Value(&response, v8_context_.NewHandle(isolate())), | 179 converter->ToV8Value(&response, v8_context_.NewHandle(isolate())), |
168 v8::String::NewFromUtf8(isolate(), error.c_str()) | 180 v8::String::NewFromUtf8(isolate(), error.c_str())}; |
169 }; | |
170 | 181 |
171 v8::Handle<v8::Value> retval = module_system_->CallModuleMethod( | 182 v8::Handle<v8::Value> retval = module_system()->CallModuleMethod( |
172 "sendRequest", "handleResponse", arraysize(argv), argv); | 183 "sendRequest", "handleResponse", arraysize(argv), argv); |
173 | 184 |
174 // In debug, the js will validate the callback parameters and return a | 185 // In debug, the js will validate the callback parameters and return a |
175 // string if a validation error has occured. | 186 // string if a validation error has occured. |
176 DCHECK(retval.IsEmpty() || retval->IsUndefined()) | 187 DCHECK(retval.IsEmpty() || retval->IsUndefined()) |
177 << *v8::String::Utf8Value(retval); | 188 << *v8::String::Utf8Value(retval); |
178 } | 189 } |
179 | 190 |
180 } // namespace extensions | 191 } // namespace extensions |
OLD | NEW |