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