| 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/dispatcher.h" | 5 #include "chrome/renderer/extensions/dispatcher.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/debug/alias.h" | 9 #include "base/debug/alias.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 916 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 927 // CSP blocks extension page loading by switching the extension ID to | 927 // CSP blocks extension page loading by switching the extension ID to |
| 928 // "invalid". This isn't interesting. | 928 // "invalid". This isn't interesting. |
| 929 if (extension_id != "invalid") { | 929 if (extension_id != "invalid") { |
| 930 LOG(ERROR) << "Extension \"" << extension_id << "\" not found"; | 930 LOG(ERROR) << "Extension \"" << extension_id << "\" not found"; |
| 931 RenderThread::Get()->RecordUserMetrics("ExtensionNotFound_ED"); | 931 RenderThread::Get()->RecordUserMetrics("ExtensionNotFound_ED"); |
| 932 } | 932 } |
| 933 | 933 |
| 934 extension_id = ""; | 934 extension_id = ""; |
| 935 } | 935 } |
| 936 | 936 |
| 937 ExtensionURLInfo url_info(frame->document().securityOrigin(), | 937 // Frames loaded on a unique security origin are not accessible to extensions. |
| 938 UserScriptSlave::GetDataSourceURLForFrame(frame)); | 938 GURL effective_frame_url; |
| 939 if (!frame->document().securityOrigin().isUnique()) |
| 940 effective_frame_url = UserScriptSlave::GetDataSourceURLForFrame(frame); |
| 939 | 941 |
| 940 Feature::Context context_type = | 942 Feature::Context context_type = ClassifyJavaScriptContext( |
| 941 ClassifyJavaScriptContext(extension_id, extension_group, url_info); | 943 extension_id, extension_group, effective_frame_url); |
| 942 | 944 |
| 943 ChromeV8Context* context = | 945 ChromeV8Context* context = |
| 944 new ChromeV8Context(v8_context, frame, extension, context_type); | 946 new ChromeV8Context(v8_context, frame, extension, context_type); |
| 945 v8_context_set_.Add(context); | 947 v8_context_set_.Add(context); |
| 946 | 948 |
| 947 { | 949 { |
| 948 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(context, | 950 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(context, |
| 949 &source_map_)); | 951 &source_map_)); |
| 950 context->set_module_system(module_system.Pass()); | 952 context->set_module_system(module_system.Pass()); |
| 951 } | 953 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 | 1051 |
| 1050 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); | 1052 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); |
| 1051 } | 1053 } |
| 1052 | 1054 |
| 1053 std::string Dispatcher::GetExtensionID(const WebFrame* frame, int world_id) { | 1055 std::string Dispatcher::GetExtensionID(const WebFrame* frame, int world_id) { |
| 1054 if (world_id != 0) { | 1056 if (world_id != 0) { |
| 1055 // Isolated worlds (content script). | 1057 // Isolated worlds (content script). |
| 1056 return user_script_slave_->GetExtensionIdForIsolatedWorld(world_id); | 1058 return user_script_slave_->GetExtensionIdForIsolatedWorld(world_id); |
| 1057 } | 1059 } |
| 1058 | 1060 |
| 1061 if (frame->document().securityOrigin().isUnique()) |
| 1062 return std::string(); |
| 1063 |
| 1059 // Extension pages (chrome-extension:// URLs). | 1064 // Extension pages (chrome-extension:// URLs). |
| 1060 GURL frame_url = UserScriptSlave::GetDataSourceURLForFrame(frame); | 1065 GURL frame_url = UserScriptSlave::GetDataSourceURLForFrame(frame); |
| 1061 return extensions_.GetExtensionOrAppIDByURL( | 1066 return extensions_.GetExtensionOrAppIDByURL(frame_url); |
| 1062 ExtensionURLInfo(frame->document().securityOrigin(), frame_url)); | |
| 1063 } | 1067 } |
| 1064 | 1068 |
| 1065 bool Dispatcher::IsWithinPlatformApp(const WebFrame* frame) { | 1069 bool Dispatcher::IsWithinPlatformApp(const WebFrame* frame) { |
| 1066 // We intentionally don't use the origin parameter for ExtensionURLInfo since | 1070 GURL url(UserScriptSlave::GetDataSourceURLForFrame(frame->top())); |
| 1067 // it would be empty (i.e. unique) for sandboxed resources and thus not match. | 1071 const Extension* extension = extensions_.GetExtensionOrAppByURL(url); |
| 1068 ExtensionURLInfo url_info( | |
| 1069 UserScriptSlave::GetDataSourceURLForFrame(frame->top())); | |
| 1070 const Extension* extension = extensions_.GetExtensionOrAppByURL(url_info); | |
| 1071 | 1072 |
| 1072 return extension && extension->is_platform_app(); | 1073 return extension && extension->is_platform_app(); |
| 1073 } | 1074 } |
| 1074 | 1075 |
| 1075 void Dispatcher::WillReleaseScriptContext( | 1076 void Dispatcher::WillReleaseScriptContext( |
| 1076 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) { | 1077 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) { |
| 1077 ChromeV8Context* context = v8_context_set_.GetByV8Context(v8_context); | 1078 ChromeV8Context* context = v8_context_set_.GetByV8Context(v8_context); |
| 1078 if (!context) | 1079 if (!context) |
| 1079 return; | 1080 return; |
| 1080 | 1081 |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1290 RenderThread::Get()->Send(new ExtensionHostMsg_SuspendAck(extension_id)); | 1291 RenderThread::Get()->Send(new ExtensionHostMsg_SuspendAck(extension_id)); |
| 1291 } | 1292 } |
| 1292 | 1293 |
| 1293 void Dispatcher::OnCancelSuspend(const std::string& extension_id) { | 1294 void Dispatcher::OnCancelSuspend(const std::string& extension_id) { |
| 1294 DispatchEvent(extension_id, kOnSuspendCanceledEvent); | 1295 DispatchEvent(extension_id, kOnSuspendCanceledEvent); |
| 1295 } | 1296 } |
| 1296 | 1297 |
| 1297 Feature::Context Dispatcher::ClassifyJavaScriptContext( | 1298 Feature::Context Dispatcher::ClassifyJavaScriptContext( |
| 1298 const std::string& extension_id, | 1299 const std::string& extension_id, |
| 1299 int extension_group, | 1300 int extension_group, |
| 1300 const ExtensionURLInfo& url_info) { | 1301 const GURL& url) { |
| 1301 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) { | 1302 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) { |
| 1302 return extensions_.Contains(extension_id) ? | 1303 return extensions_.Contains(extension_id) ? |
| 1303 Feature::CONTENT_SCRIPT_CONTEXT : Feature::UNSPECIFIED_CONTEXT; | 1304 Feature::CONTENT_SCRIPT_CONTEXT : Feature::UNSPECIFIED_CONTEXT; |
| 1304 } | 1305 } |
| 1305 | 1306 |
| 1306 // We have an explicit check for sandboxed pages before checking whether the | 1307 // We have an explicit check for sandboxed pages before checking whether the |
| 1307 // extension is active in this process because: | 1308 // extension is active in this process because: |
| 1308 // 1. Sandboxed pages run in the same process as regular extension pages, so | 1309 // 1. Sandboxed pages run in the same process as regular extension pages, so |
| 1309 // the extension is considered active. | 1310 // the extension is considered active. |
| 1310 // 2. ScriptContext creation (which triggers bindings injection) happens | 1311 // 2. ScriptContext creation (which triggers bindings injection) happens |
| 1311 // before the SecurityContext is updated with the sandbox flags (after | 1312 // before the SecurityContext is updated with the sandbox flags (after |
| 1312 // reading the CSP header), so url_info.url().securityOrigin() is not | 1313 // reading the CSP header), so the caller can't check if the context's |
| 1313 // unique yet. | 1314 // security origin is unique yet. |
| 1314 if (extensions_.IsSandboxedPage(url_info)) | 1315 if (extensions_.IsSandboxedPage(url)) |
| 1315 return Feature::WEB_PAGE_CONTEXT; | 1316 return Feature::WEB_PAGE_CONTEXT; |
| 1316 | 1317 |
| 1317 if (IsExtensionActive(extension_id)) | 1318 if (IsExtensionActive(extension_id)) |
| 1318 return Feature::BLESSED_EXTENSION_CONTEXT; | 1319 return Feature::BLESSED_EXTENSION_CONTEXT; |
| 1319 | 1320 |
| 1320 if (extensions_.ExtensionBindingsAllowed(url_info)) { | 1321 if (extensions_.ExtensionBindingsAllowed(url)) { |
| 1321 return extensions_.Contains(extension_id) ? | 1322 return extensions_.Contains(extension_id) ? |
| 1322 Feature::UNBLESSED_EXTENSION_CONTEXT : Feature::UNSPECIFIED_CONTEXT; | 1323 Feature::UNBLESSED_EXTENSION_CONTEXT : Feature::UNSPECIFIED_CONTEXT; |
| 1323 } | 1324 } |
| 1324 | 1325 |
| 1325 if (url_info.url().is_valid()) | 1326 if (url.is_valid()) |
| 1326 return Feature::WEB_PAGE_CONTEXT; | 1327 return Feature::WEB_PAGE_CONTEXT; |
| 1327 | 1328 |
| 1328 return Feature::UNSPECIFIED_CONTEXT; | 1329 return Feature::UNSPECIFIED_CONTEXT; |
| 1329 } | 1330 } |
| 1330 | 1331 |
| 1331 void Dispatcher::OnExtensionResponse(int request_id, | 1332 void Dispatcher::OnExtensionResponse(int request_id, |
| 1332 bool success, | 1333 bool success, |
| 1333 const base::ListValue& response, | 1334 const base::ListValue& response, |
| 1334 const std::string& error) { | 1335 const std::string& error) { |
| 1335 request_sender_->HandleResponse(request_id, success, response, error); | 1336 request_sender_->HandleResponse(request_id, success, response, error); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1366 "%s can only be used in an extension process."; | 1367 "%s can only be used in an extension process."; |
| 1367 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 1368 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 1368 v8::ThrowException( | 1369 v8::ThrowException( |
| 1369 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 1370 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 1370 return false; | 1371 return false; |
| 1371 } | 1372 } |
| 1372 | 1373 |
| 1373 // Theoretically we could end up with bindings being injected into sandboxed | 1374 // Theoretically we could end up with bindings being injected into sandboxed |
| 1374 // frames, for example content scripts. Don't let them execute API functions. | 1375 // frames, for example content scripts. Don't let them execute API functions. |
| 1375 WebKit::WebFrame* frame = context->web_frame(); | 1376 WebKit::WebFrame* frame = context->web_frame(); |
| 1376 ExtensionURLInfo url_info(frame->document().securityOrigin(), | 1377 if (frame->document().securityOrigin().isUnique() || |
| 1377 UserScriptSlave::GetDataSourceURLForFrame(frame)); | 1378 extensions_.IsSandboxedPage( |
| 1378 if (extensions_.IsSandboxedPage(url_info)) { | 1379 UserScriptSlave::GetDataSourceURLForFrame(frame))) { |
| 1379 static const char kMessage[] = | 1380 static const char kMessage[] = |
| 1380 "%s cannot be used within a sandboxed frame."; | 1381 "%s cannot be used within a sandboxed frame."; |
| 1381 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); | 1382 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); |
| 1382 v8::ThrowException( | 1383 v8::ThrowException( |
| 1383 v8::Exception::Error(v8::String::New(error_msg.c_str()))); | 1384 v8::Exception::Error(v8::String::New(error_msg.c_str()))); |
| 1384 return false; | 1385 return false; |
| 1385 } | 1386 } |
| 1386 | 1387 |
| 1387 return true; | 1388 return true; |
| 1388 } | 1389 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1433 RenderView* background_view = | 1434 RenderView* background_view = |
| 1434 ExtensionHelper::GetBackgroundPage(extension_id); | 1435 ExtensionHelper::GetBackgroundPage(extension_id); |
| 1435 if (background_view) { | 1436 if (background_view) { |
| 1436 background_view->Send(new ExtensionHostMsg_EventAck( | 1437 background_view->Send(new ExtensionHostMsg_EventAck( |
| 1437 background_view->GetRoutingID())); | 1438 background_view->GetRoutingID())); |
| 1438 } | 1439 } |
| 1439 } | 1440 } |
| 1440 } | 1441 } |
| 1441 | 1442 |
| 1442 } // namespace extensions | 1443 } // namespace extensions |
| OLD | NEW |