| 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/user_script_slave.h" | 5 #include "chrome/renderer/extensions/user_script_slave.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/shared_memory.h" | 11 #include "base/memory/shared_memory.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/pickle.h" | 13 #include "base/pickle.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/timer/elapsed_timer.h" | 15 #include "base/timer/elapsed_timer.h" |
| 16 #include "chrome/common/url_constants.h" | |
| 17 #include "chrome/renderer/chrome_render_process_observer.h" | |
| 18 #include "chrome/renderer/extensions/dom_activity_logger.h" | 16 #include "chrome/renderer/extensions/dom_activity_logger.h" |
| 19 #include "chrome/renderer/extensions/extension_groups.h" | 17 #include "chrome/renderer/extensions/extension_groups.h" |
| 20 #include "chrome/renderer/isolated_world_ids.h" | 18 #include "content/public/common/url_constants.h" |
| 21 #include "content/public/renderer/render_thread.h" | 19 #include "content/public/renderer/render_thread.h" |
| 22 #include "content/public/renderer/render_view.h" | 20 #include "content/public/renderer/render_view.h" |
| 23 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
| 24 #include "extensions/common/extension_messages.h" | 22 #include "extensions/common/extension_messages.h" |
| 25 #include "extensions/common/extension_set.h" | 23 #include "extensions/common/extension_set.h" |
| 26 #include "extensions/common/manifest_handlers/csp_info.h" | 24 #include "extensions/common/manifest_handlers/csp_info.h" |
| 27 #include "extensions/common/permissions/permissions_data.h" | 25 #include "extensions/common/permissions/permissions_data.h" |
| 26 #include "extensions/renderer/extensions_renderer_client.h" |
| 28 #include "extensions/renderer/script_context.h" | 27 #include "extensions/renderer/script_context.h" |
| 29 #include "grit/renderer_resources.h" | 28 #include "grit/renderer_resources.h" |
| 30 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 29 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 31 #include "third_party/WebKit/public/platform/WebVector.h" | 30 #include "third_party/WebKit/public/platform/WebVector.h" |
| 32 #include "third_party/WebKit/public/web/WebDocument.h" | 31 #include "third_party/WebKit/public/web/WebDocument.h" |
| 33 #include "third_party/WebKit/public/web/WebFrame.h" | 32 #include "third_party/WebKit/public/web/WebFrame.h" |
| 34 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 33 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
| 35 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" | 34 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" |
| 36 #include "third_party/WebKit/public/web/WebView.h" | 35 #include "third_party/WebKit/public/web/WebView.h" |
| 37 #include "ui/base/resource/resource_bundle.h" | 36 #include "ui/base/resource/resource_bundle.h" |
| 38 #include "url/gurl.h" | 37 #include "url/gurl.h" |
| 39 | 38 |
| 40 using blink::WebFrame; | 39 using blink::WebFrame; |
| 41 using blink::WebSecurityOrigin; | 40 using blink::WebSecurityOrigin; |
| 42 using blink::WebSecurityPolicy; | 41 using blink::WebSecurityPolicy; |
| 43 using blink::WebString; | 42 using blink::WebString; |
| 44 using blink::WebVector; | 43 using blink::WebVector; |
| 45 using blink::WebView; | 44 using blink::WebView; |
| 46 using content::RenderThread; | 45 using content::RenderThread; |
| 47 | 46 |
| 48 namespace extensions { | 47 namespace extensions { |
| 49 | 48 |
| 50 // These two strings are injected before and after the Greasemonkey API and | 49 // These two strings are injected before and after the Greasemonkey API and |
| 51 // user script to wrap it in an anonymous scope. | 50 // user script to wrap it in an anonymous scope. |
| 52 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; | 51 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
| 53 static const char kUserScriptTail[] = "\n})(window);"; | 52 static const char kUserScriptTail[] = "\n})(window);"; |
| 54 | 53 |
| 55 int UserScriptSlave::GetIsolatedWorldIdForExtension(const Extension* extension, | 54 int UserScriptSlave::GetIsolatedWorldIdForExtension(const Extension* extension, |
| 56 WebFrame* frame) { | 55 WebFrame* frame) { |
| 57 static int g_next_isolated_world_id = chrome::ISOLATED_WORLD_ID_EXTENSIONS; | 56 static int g_next_isolated_world_id = |
| 57 ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId(); |
| 58 | 58 |
| 59 IsolatedWorldMap::iterator iter = isolated_world_ids_.find(extension->id()); | 59 IsolatedWorldMap::iterator iter = isolated_world_ids_.find(extension->id()); |
| 60 if (iter != isolated_world_ids_.end()) { | 60 if (iter != isolated_world_ids_.end()) { |
| 61 // We need to set the isolated world origin and CSP even if it's not a new | 61 // We need to set the isolated world origin and CSP even if it's not a new |
| 62 // world since these are stored per frame, and we might not have used this | 62 // world since these are stored per frame, and we might not have used this |
| 63 // isolated world in this frame before. | 63 // isolated world in this frame before. |
| 64 frame->setIsolatedWorldSecurityOrigin( | 64 frame->setIsolatedWorldSecurityOrigin( |
| 65 iter->second, | 65 iter->second, |
| 66 WebSecurityOrigin::create(extension->url())); | 66 WebSecurityOrigin::create(extension->url())); |
| 67 frame->setIsolatedWorldContentSecurityPolicy( | 67 frame->setIsolatedWorldContentSecurityPolicy( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 for (size_t i = 0; i < scripts_.size(); ++i) { | 112 for (size_t i = 0; i < scripts_.size(); ++i) { |
| 113 DCHECK(!scripts_[i]->extension_id().empty()); | 113 DCHECK(!scripts_[i]->extension_id().empty()); |
| 114 extension_ids->insert(scripts_[i]->extension_id()); | 114 extension_ids->insert(scripts_[i]->extension_id()); |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { | 118 bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { |
| 119 scripts_.clear(); | 119 scripts_.clear(); |
| 120 | 120 |
| 121 bool only_inject_incognito = | 121 bool only_inject_incognito = |
| 122 ChromeRenderProcessObserver::is_incognito_process(); | 122 extensions::ExtensionsRendererClient::Get()->IsIncognitoProcess(); |
| 123 | 123 |
| 124 // Create the shared memory object (read only). | 124 // Create the shared memory object (read only). |
| 125 shared_memory_.reset(new base::SharedMemory(shared_memory, true)); | 125 shared_memory_.reset(new base::SharedMemory(shared_memory, true)); |
| 126 if (!shared_memory_.get()) | 126 if (!shared_memory_.get()) |
| 127 return false; | 127 return false; |
| 128 | 128 |
| 129 // First get the size of the memory block. | 129 // First get the size of the memory block. |
| 130 if (!shared_memory_->Map(sizeof(Pickle::Header))) | 130 if (!shared_memory_->Map(sizeof(Pickle::Header))) |
| 131 return false; | 131 return false; |
| 132 Pickle::Header* pickle_header = | 132 Pickle::Header* pickle_header = |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 } else if (location == UserScript::DOCUMENT_IDLE) { | 303 } else if (location == UserScript::DOCUMENT_IDLE) { |
| 304 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); | 304 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); |
| 305 if (num_scripts) | 305 if (num_scripts) |
| 306 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); | 306 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); |
| 307 } else { | 307 } else { |
| 308 NOTREACHED(); | 308 NOTREACHED(); |
| 309 } | 309 } |
| 310 } | 310 } |
| 311 | 311 |
| 312 } // namespace extensions | 312 } // namespace extensions |
| OLD | NEW |