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/user_script_slave.h" | 15 #include "chrome/renderer/extensions/user_script_slave.h" |
16 #include "content/public/renderer/render_view.h" | 16 #include "content/public/renderer/render_view.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
19 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
20 | 20 |
21 namespace extensions { | 21 namespace extensions { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const char kChromeHidden[] = "chromeHidden"; | 25 const char kChromeHidden[] = "chromeHidden"; |
26 const char kUnavailableMessage[] = "API is unavailable."; | |
not at google - send to devlin
2013/02/15 22:26:17
Ok, let's make this nice and informative.
"you do
cduvall
2013/02/19 23:58:49
Done.
not at google - send to devlin
2013/02/20 00:36:38
Nice, could you make "this API" be %s and use Stri
not at google - send to devlin
2013/02/20 00:49:29
Never mind, being dumb. Ignore.
| |
26 | 27 |
27 #ifndef NDEBUG | 28 #ifndef NDEBUG |
28 const char kValidateCallbacks[] = "validateCallbacks"; | 29 const char kValidateCallbacks[] = "validateCallbacks"; |
29 const char kValidateAPI[] = "validateAPI"; | 30 const char kValidateAPI[] = "validateAPI"; |
30 #endif | 31 #endif |
31 | 32 |
32 } // namespace | 33 } // namespace |
33 | 34 |
34 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, | 35 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, |
35 WebKit::WebFrame* web_frame, | 36 WebKit::WebFrame* web_frame, |
36 const Extension* extension, | 37 const Extension* extension, |
37 Feature::Context context_type) | 38 Feature::Context context_type) |
38 : v8_context_(v8::Persistent<v8::Context>::New(v8_context->GetIsolate(), | 39 : v8_context_(v8::Persistent<v8::Context>::New(v8_context->GetIsolate(), |
39 v8_context)), | 40 v8_context)), |
40 web_frame_(web_frame), | 41 web_frame_(web_frame), |
41 extension_(extension), | 42 extension_(extension), |
42 context_type_(context_type) { | 43 context_type_(context_type), |
44 available_extension_apis_initialized_(false) { | |
43 VLOG(1) << "Created context:\n" | 45 VLOG(1) << "Created context:\n" |
44 << " extension id: " << GetExtensionID() << "\n" | 46 << " extension id: " << GetExtensionID() << "\n" |
45 << " frame: " << web_frame_ << "\n" | 47 << " frame: " << web_frame_ << "\n" |
46 << " context type: " << GetContextTypeDescription(); | 48 << " context type: " << GetContextTypeDescription(); |
47 } | 49 } |
48 | 50 |
49 ChromeV8Context::~ChromeV8Context() { | 51 ChromeV8Context::~ChromeV8Context() { |
50 VLOG(1) << "Destroyed context for extension\n" | 52 VLOG(1) << "Destroyed context for extension\n" |
51 << " extension id: " << GetExtensionID(); | 53 << " extension id: " << GetExtensionID(); |
52 v8_context_.Dispose(v8_context_->GetIsolate()); | 54 v8_context_.Dispose(v8_context_->GetIsolate()); |
53 } | 55 } |
54 | 56 |
55 std::string ChromeV8Context::GetExtensionID() { | 57 std::string ChromeV8Context::GetExtensionID() { |
56 return extension_ ? extension_->id() : ""; | 58 return extension_ ? extension_->id() : ""; |
57 } | 59 } |
58 | 60 |
59 // static | 61 // static |
60 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( | 62 v8::Handle<v8::Value> ChromeV8Context::GetOrCreateChromeHidden( |
61 v8::Handle<v8::Context> context) { | 63 v8::Handle<v8::Context> context) { |
62 v8::Local<v8::Object> global = context->Global(); | 64 v8::Local<v8::Object> global = context->Global(); |
63 v8::Local<v8::Value> hidden = global->GetHiddenValue( | 65 v8::Local<v8::Value> hidden = global->GetHiddenValue( |
64 v8::String::New(kChromeHidden)); | 66 v8::String::New(kChromeHidden)); |
65 | 67 |
66 if (hidden.IsEmpty() || hidden->IsUndefined()) { | 68 if (hidden.IsEmpty() || hidden->IsUndefined()) { |
67 hidden = v8::Object::New(); | 69 hidden = v8::Object::New(); |
68 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); | 70 global->SetHiddenValue(v8::String::New(kChromeHidden), hidden); |
69 | 71 |
70 #ifndef NDEBUG | 72 #ifndef NDEBUG |
71 // Tell schema_generated_bindings.js to validate callbacks and events | 73 // Tell bindings.js to validate callbacks and events against their schema |
72 // against their schema definitions. | 74 // definitions. |
73 v8::Local<v8::Object>::Cast(hidden)->Set( | 75 v8::Local<v8::Object>::Cast(hidden)->Set( |
74 v8::String::New(kValidateCallbacks), v8::True()); | 76 v8::String::New(kValidateCallbacks), v8::True()); |
75 // Tell schema_generated_bindings.js to validate API for ambiguity. | 77 // Tell bindings.js to validate API for ambiguity. |
76 v8::Local<v8::Object>::Cast(hidden)->Set( | 78 v8::Local<v8::Object>::Cast(hidden)->Set( |
77 v8::String::New(kValidateAPI), v8::True()); | 79 v8::String::New(kValidateAPI), v8::True()); |
78 #endif | 80 #endif |
79 } | 81 } |
80 | 82 |
81 DCHECK(hidden->IsObject()); | 83 DCHECK(hidden->IsObject()); |
82 return v8::Local<v8::Object>::Cast(hidden); | 84 return v8::Local<v8::Object>::Cast(hidden); |
83 } | 85 } |
84 | 86 |
85 v8::Handle<v8::Value> ChromeV8Context::GetChromeHidden() const { | 87 v8::Handle<v8::Value> ChromeV8Context::GetChromeHidden() const { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 v8::Object::New(), | 138 v8::Object::New(), |
137 argc, | 139 argc, |
138 argv); | 140 argv); |
139 if (result) | 141 if (result) |
140 *result = result_temp; | 142 *result = result_temp; |
141 | 143 |
142 return true; | 144 return true; |
143 } | 145 } |
144 | 146 |
145 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { | 147 const std::set<std::string>& ChromeV8Context::GetAvailableExtensionAPIs() { |
146 if (!available_extension_apis_.get()) { | 148 if (!available_extension_apis_initialized_) { |
147 available_extension_apis_ = | 149 available_extension_apis_ = |
148 ExtensionAPI::GetSharedInstance()->GetAPIsForContext( | 150 ExtensionAPI::GetSharedInstance()->GetAPIsForContext( |
149 context_type_, | 151 context_type_, |
150 extension_, | 152 extension_, |
151 UserScriptSlave::GetDataSourceURLForFrame( | 153 UserScriptSlave::GetDataSourceURLForFrame(web_frame_)); |
152 web_frame_)).Pass(); | 154 available_extension_apis_initialized_ = true; |
153 } | 155 } |
154 return *(available_extension_apis_.get()); | 156 return available_extension_apis_; |
157 } | |
158 | |
159 Feature::Availability ChromeV8Context::GetAvailability( | |
160 const std::string& api_name) { | |
161 const std::set<std::string>& available_apis = GetAvailableExtensionAPIs(); | |
162 | |
163 if (available_apis.find(api_name) != available_apis.end()) | |
not at google - send to devlin
2013/02/15 22:26:17
Call ExtensionAPI::IsAvailable?
cduvall
2013/02/19 23:58:49
IsAvailable() only works for Features. I added a T
| |
164 return Feature::CreateAvailability(Feature::IS_AVAILABLE, ""); | |
165 | |
166 return Feature::CreateAvailability(Feature::INVALID_CONTEXT, | |
167 kUnavailableMessage); | |
155 } | 168 } |
156 | 169 |
157 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, | 170 void ChromeV8Context::DispatchOnLoadEvent(bool is_incognito_process, |
158 int manifest_version) { | 171 int manifest_version) { |
159 v8::HandleScope handle_scope; | 172 v8::HandleScope handle_scope; |
160 v8::Handle<v8::Value> argv[] = { | 173 v8::Handle<v8::Value> argv[] = { |
161 v8::String::New(GetExtensionID().c_str()), | 174 v8::String::New(GetExtensionID().c_str()), |
162 v8::String::New(GetContextTypeDescription().c_str()), | 175 v8::String::New(GetContextTypeDescription().c_str()), |
163 v8::Boolean::New(is_incognito_process), | 176 v8::Boolean::New(is_incognito_process), |
164 v8::Integer::New(manifest_version), | 177 v8::Integer::New(manifest_version), |
(...skipping 12 matching lines...) Expand all Loading... | |
177 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; | 190 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; |
178 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; | 191 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; |
179 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; | 192 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; |
180 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; | 193 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; |
181 } | 194 } |
182 NOTREACHED(); | 195 NOTREACHED(); |
183 return ""; | 196 return ""; |
184 } | 197 } |
185 | 198 |
186 } // namespace extensions | 199 } // namespace extensions |
OLD | NEW |