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 "chrome/renderer/extensions/chrome_v8_context.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/common/extensions/api/extension_api.h" | 11 #include "chrome/common/extensions/api/extension_api.h" |
12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
13 #include "chrome/common/extensions/extension_set.h" | 13 #include "chrome/common/extensions/extension_set.h" |
14 #include "chrome/renderer/extensions/chrome_v8_extension.h" | 14 #include "chrome/renderer/extensions/chrome_v8_extension.h" |
| 15 #include "chrome/renderer/extensions/module_system.h" |
15 #include "chrome/renderer/extensions/user_script_slave.h" | 16 #include "chrome/renderer/extensions/user_script_slave.h" |
16 #include "content/public/renderer/render_view.h" | 17 #include "content/public/renderer/render_view.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
19 #include "v8/include/v8.h" | 20 #include "v8/include/v8.h" |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 const char kChromeHidden[] = "chromeHidden"; | 26 const char kChromeHidden[] = "chromeHidden"; |
| 27 const char kUnavailableMessage[] = "You do not have permission to access this " |
| 28 "API. Ensure that the required permission " |
| 29 "or manifest property is included in your " |
| 30 "manifest.json."; |
26 | 31 |
27 #ifndef NDEBUG | 32 #ifndef NDEBUG |
28 const char kValidateCallbacks[] = "validateCallbacks"; | 33 const char kValidateCallbacks[] = "validateCallbacks"; |
29 const char kValidateAPI[] = "validateAPI"; | 34 const char kValidateAPI[] = "validateAPI"; |
30 #endif | 35 #endif |
31 | 36 |
32 } // namespace | 37 } // namespace |
33 | 38 |
34 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, | 39 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, |
35 WebKit::WebFrame* web_frame, | 40 WebKit::WebFrame* web_frame, |
36 const Extension* extension, | 41 const Extension* extension, |
37 Feature::Context context_type) | 42 Feature::Context context_type) |
38 : v8_context_(v8::Persistent<v8::Context>::New(v8_context->GetIsolate(), | 43 : v8_context_(v8::Persistent<v8::Context>::New(v8_context->GetIsolate(), |
39 v8_context)), | 44 v8_context)), |
40 web_frame_(web_frame), | 45 web_frame_(web_frame), |
41 extension_(extension), | 46 extension_(extension), |
42 context_type_(context_type) { | 47 context_type_(context_type), |
| 48 available_extension_apis_initialized_(false) { |
43 VLOG(1) << "Created context:\n" | 49 VLOG(1) << "Created context:\n" |
44 << " extension id: " << GetExtensionID() << "\n" | 50 << " extension id: " << GetExtensionID() << "\n" |
45 << " frame: " << web_frame_ << "\n" | 51 << " frame: " << web_frame_ << "\n" |
46 << " context type: " << GetContextTypeDescription(); | 52 << " context type: " << GetContextTypeDescription(); |
47 } | 53 } |
48 | 54 |
49 ChromeV8Context::~ChromeV8Context() { | 55 ChromeV8Context::~ChromeV8Context() { |
50 VLOG(1) << "Destroyed context for extension\n" | 56 VLOG(1) << "Destroyed context for extension\n" |
51 << " extension id: " << GetExtensionID(); | 57 << " extension id: " << GetExtensionID(); |
52 v8_context_.Dispose(v8_context_->GetIsolate()); | 58 v8_context_.Dispose(v8_context_->GetIsolate()); |
53 } | 59 } |
54 | 60 |
| 61 void ChromeV8Context::Invalidate() { |
| 62 module_system_->Invalidate(); |
| 63 web_frame_ = NULL; |
| 64 } |
| 65 |
55 std::string ChromeV8Context::GetExtensionID() { | 66 std::string ChromeV8Context::GetExtensionID() { |
56 return extension_ ? extension_->id() : ""; | 67 return extension_ ? extension_->id() : ""; |
57 } | 68 } |
58 | 69 |
59 // static | 70 // static |
60 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( | 71 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( |
61 v8::Handle<v8::Context> context) { | 72 v8::Handle<v8::Context> context) { |
62 v8::Local<v8::Object> global = context->Global(); | 73 v8::Local<v8::Object> global = context->Global(); |
63 v8::Local<v8::Value> hidden = global->GetHiddenValue( | 74 v8::Local<v8::Value> hidden = global->GetHiddenValue( |
64 v8::String::New(kChromeHidden)); | 75 v8::String::New(kChromeHidden)); |
65 | 76 |
66 if (hidden.IsEmpty() || hidden->IsUndefined()) { | 77 if (hidden.IsEmpty() || hidden->IsUndefined()) { |
67 hidden = v8::Object::New(); | 78 hidden = v8::Object::New(); |
68 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); | 79 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); |
69 | 80 |
70 #ifndef NDEBUG | 81 #ifndef NDEBUG |
71 // Tell schema_generated_bindings.js to validate callbacks and events | 82 // Tell bindings.js to validate callbacks and events against their schema |
72 // against their schema definitions. | 83 // definitions. |
73 v8::Local<v8::Object>::Cast(hidden)->Set( | 84 v8::Local<v8::Object>::Cast(hidden)->Set( |
74 v8::String::New(kValidateCallbacks), v8::True()); | 85 v8::String::New(kValidateCallbacks), v8::True()); |
75 // Tell schema_generated_bindings.js to validate API for ambiguity. | 86 // Tell bindings.js to validate API for ambiguity. |
76 v8::Local<v8::Object>::Cast(hidden)->Set( | 87 v8::Local<v8::Object>::Cast(hidden)->Set( |
77 v8::String::New(kValidateAPI), v8::True()); | 88 v8::String::New(kValidateAPI), v8::True()); |
78 #endif | 89 #endif |
79 } | 90 } |
80 | 91 |
81 DCHECK(hidden->IsObject()); | 92 DCHECK(hidden->IsObject()); |
82 return v8::Local<v8::Object>::Cast(hidden); | 93 return v8::Local<v8::Object>::Cast(hidden); |
83 } | 94 } |
84 | 95 |
85 v8::Handle<v8::Value> ChromeV8Context::GetChromeHidden() const { | 96 v8::Handle<v8::Value> ChromeV8Context::GetChromeHidden() const { |
86 v8::Local<v8::Object> global = v8_context_->Global(); | 97 v8::Local<v8::Object> global = v8_context_->Global(); |
87 return global->GetHiddenValue(v8::String::New(kChromeHidden)); | 98 return global->GetHiddenValue(v8::String::New(kChromeHidden)); |
88 } | 99 } |
89 | 100 |
90 content::RenderView* ChromeV8Context::GetRenderView() const { | 101 content::RenderView* ChromeV8Context::GetRenderView() const { |
91 if (web_frame_ && web_frame_->view()) | 102 if (web_frame_ && web_frame_->view()) |
92 return content::RenderView::FromWebView(web_frame_->view()); | 103 return content::RenderView::FromWebView(web_frame_->view()); |
93 else | 104 else |
94 return NULL; | 105 return NULL; |
95 } | 106 } |
96 | 107 |
97 bool ChromeV8Context::CallChromeHiddenMethod( | 108 bool ChromeV8Context::CallChromeHiddenMethod( |
98 const std::string& function_name, | 109 const std::string& function_name, |
99 int argc, | 110 int argc, |
100 v8::Handle<v8::Value>* argv, | 111 v8::Handle<v8::Value>* argv, |
101 v8::Handle<v8::Value>* result) const { | 112 v8::Handle<v8::Value>* result) const { |
102 v8::Context::Scope context_scope(v8_context_); | 113 v8::Context::Scope context_scope(v8_context_); |
103 | 114 |
104 // ChromeV8ContextSet calls clear_web_frame() and then schedules a task to | 115 // ChromeV8ContextSet calls Invalidate() and then schedules a task to delete |
105 // delete this object. This check prevents a race from attempting to execute | 116 // this object. This check prevents a race from attempting to execute script |
106 // script on a NULL web_frame_. | 117 // on a NULL web_frame_. |
107 if (!web_frame_) | 118 if (!web_frame_) |
108 return false; | 119 return false; |
109 | 120 |
110 // Look up the function name, which may be a sub-property like | 121 // Look up the function name, which may be a sub-property like |
111 // "Port.dispatchOnMessage" in the hidden global variable. | 122 // "Port.dispatchOnMessage" in the hidden global variable. |
112 v8::Local<v8::Value> value = v8::Local<v8::Value>::New(GetChromeHidden()); | 123 v8::Local<v8::Value> value = v8::Local<v8::Value>::New(GetChromeHidden()); |
113 if (value.IsEmpty()) | 124 if (value.IsEmpty()) |
114 return false; | 125 return false; |
115 | 126 |
116 std::vector<std::string> components; | 127 std::vector<std::string> components; |
(...skipping 19 matching lines...) Expand all Loading... |
136 v8::Object::New(), | 147 v8::Object::New(), |
137 argc, | 148 argc, |
138 argv); | 149 argv); |
139 if (result) | 150 if (result) |
140 *result = result_temp; | 151 *result = result_temp; |
141 | 152 |
142 return true; | 153 return true; |
143 } | 154 } |
144 | 155 |
145 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { | 156 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { |
146 if (!available_extension_apis_.get()) { | 157 if (!available_extension_apis_initialized_) { |
147 available_extension_apis_ = | 158 available_extension_apis_ = |
148 ExtensionAPI::GetSharedInstance()->GetAPIsForContext( | 159 ExtensionAPI::GetSharedInstance()->GetAPIsForContext( |
149 context_type_, | 160 context_type_, |
150 extension_, | 161 extension_, |
151 UserScriptSlave::GetDataSourceURLForFrame( | 162 UserScriptSlave::GetDataSourceURLForFrame(web_frame_)); |
152 web_frame_)).Pass(); | 163 available_extension_apis_initialized_ = true; |
153 } | 164 } |
154 return *(available_extension_apis_.get()); | 165 return available_extension_apis_; |
| 166 } |
| 167 |
| 168 Feature::Availability ChromeV8Context::GetAvailability( |
| 169 const std::string& api_name) { |
| 170 const std::set<std::string>& available_apis = GetAvailableExtensionAPIs(); |
| 171 |
| 172 // TODO(cduvall/kalman): Switch to ExtensionAPI::IsAvailable() once Features |
| 173 // are complete. |
| 174 if (available_apis.find(api_name) != available_apis.end()) |
| 175 return Feature::CreateAvailability(Feature::IS_AVAILABLE, ""); |
| 176 |
| 177 return Feature::CreateAvailability(Feature::INVALID_CONTEXT, |
| 178 kUnavailableMessage); |
155 } | 179 } |
156 | 180 |
157 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, | 181 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, |
158 int manifest_version) { | 182 int manifest_version) { |
159 v8::HandleScope handle_scope; | 183 v8::HandleScope handle_scope; |
160 v8::Handle<v8::Value> argv[] = { | 184 v8::Handle<v8::Value> argv[] = { |
161 v8::String::New(GetExtensionID().c_str()), | 185 v8::String::New(GetExtensionID().c_str()), |
162 v8::String::New(GetContextTypeDescription().c_str()), | 186 v8::String::New(GetContextTypeDescription().c_str()), |
163 v8::Boolean::New(is_incognito_process), | 187 v8::Boolean::New(is_incognito_process), |
164 v8::Integer::New(manifest_version), | 188 v8::Integer::New(manifest_version), |
(...skipping 12 matching lines...) Expand all Loading... |
177 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; | 201 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; |
178 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; | 202 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; |
179 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; | 203 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; |
180 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; | 204 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; |
181 } | 205 } |
182 NOTREACHED(); | 206 NOTREACHED(); |
183 return ""; | 207 return ""; |
184 } | 208 } |
185 | 209 |
186 } // namespace extensions | 210 } // namespace extensions |
OLD | NEW |