| 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_idle_scheduler.h" | 5 #include "chrome/renderer/extensions/user_script_idle_scheduler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "chrome/common/extensions/extension_error_utils.h" | 9 #include "chrome/common/extensions/extension_error_utils.h" |
| 10 #include "chrome/common/extensions/extension_messages.h" | 10 #include "chrome/common/extensions/extension_messages.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 const ExtensionMsg_ExecuteCode_Params& params) { | 100 const ExtensionMsg_ExecuteCode_Params& params) { |
| 101 const Extension* extension = extension_dispatcher_->extensions()->GetByID( | 101 const Extension* extension = extension_dispatcher_->extensions()->GetByID( |
| 102 params.extension_id); | 102 params.extension_id); |
| 103 content::RenderView* render_view = | 103 content::RenderView* render_view = |
| 104 content::RenderView::FromWebView(frame_->view()); | 104 content::RenderView::FromWebView(frame_->view()); |
| 105 | 105 |
| 106 // Since extension info is sent separately from user script info, they can | 106 // Since extension info is sent separately from user script info, they can |
| 107 // be out of sync. We just ignore this situation. | 107 // be out of sync. We just ignore this situation. |
| 108 if (!extension) { | 108 if (!extension) { |
| 109 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( | 109 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( |
| 110 render_view->GetRoutingId(), params.request_id, true, "")); | 110 render_view->GetRoutingID(), params.request_id, true, "")); |
| 111 return; | 111 return; |
| 112 } | 112 } |
| 113 | 113 |
| 114 std::vector<WebFrame*> frame_vector; | 114 std::vector<WebFrame*> frame_vector; |
| 115 frame_vector.push_back(frame_); | 115 frame_vector.push_back(frame_); |
| 116 if (params.all_frames) | 116 if (params.all_frames) |
| 117 GetAllChildFrames(frame_, &frame_vector); | 117 GetAllChildFrames(frame_, &frame_vector); |
| 118 | 118 |
| 119 for (std::vector<WebFrame*>::iterator frame_it = frame_vector.begin(); | 119 for (std::vector<WebFrame*>::iterator frame_it = frame_vector.begin(); |
| 120 frame_it != frame_vector.end(); ++frame_it) { | 120 frame_it != frame_vector.end(); ++frame_it) { |
| 121 WebFrame* frame = *frame_it; | 121 WebFrame* frame = *frame_it; |
| 122 if (params.is_javascript) { | 122 if (params.is_javascript) { |
| 123 // We recheck access here in the renderer for extra safety against races | 123 // We recheck access here in the renderer for extra safety against races |
| 124 // with navigation. | 124 // with navigation. |
| 125 // | 125 // |
| 126 // But different frames can have different URLs, and the extension might | 126 // But different frames can have different URLs, and the extension might |
| 127 // only have access to a subset of them. For the top frame, we can | 127 // only have access to a subset of them. For the top frame, we can |
| 128 // immediately send an error and stop because the browser process | 128 // immediately send an error and stop because the browser process |
| 129 // considers that an error too. | 129 // considers that an error too. |
| 130 // | 130 // |
| 131 // For child frames, we just skip ones the extension doesn't have access | 131 // For child frames, we just skip ones the extension doesn't have access |
| 132 // to and carry on. | 132 // to and carry on. |
| 133 if (!extension->CanExecuteScriptOnPage(frame->document().url(), | 133 if (!extension->CanExecuteScriptOnPage(frame->document().url(), |
| 134 NULL, NULL)) { | 134 NULL, NULL)) { |
| 135 if (frame->parent()) { | 135 if (frame->parent()) { |
| 136 continue; | 136 continue; |
| 137 } else { | 137 } else { |
| 138 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( | 138 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( |
| 139 render_view->GetRoutingId(), params.request_id, false, | 139 render_view->GetRoutingID(), params.request_id, false, |
| 140 ExtensionErrorUtils::FormatErrorMessage( | 140 ExtensionErrorUtils::FormatErrorMessage( |
| 141 extension_manifest_errors::kCannotAccessPage, | 141 extension_manifest_errors::kCannotAccessPage, |
| 142 frame->document().url().spec()))); | 142 frame->document().url().spec()))); |
| 143 return; | 143 return; |
| 144 } | 144 } |
| 145 } | 145 } |
| 146 | 146 |
| 147 WebScriptSource source(WebString::fromUTF8(params.code)); | 147 WebScriptSource source(WebString::fromUTF8(params.code)); |
| 148 if (params.in_main_world) { | 148 if (params.in_main_world) { |
| 149 frame->executeScript(source); | 149 frame->executeScript(source); |
| 150 } else { | 150 } else { |
| 151 std::vector<WebScriptSource> sources; | 151 std::vector<WebScriptSource> sources; |
| 152 sources.push_back(source); | 152 sources.push_back(source); |
| 153 frame->executeScriptInIsolatedWorld( | 153 frame->executeScriptInIsolatedWorld( |
| 154 extension_dispatcher_->user_script_slave()-> | 154 extension_dispatcher_->user_script_slave()-> |
| 155 GetIsolatedWorldIdForExtension(extension, frame), | 155 GetIsolatedWorldIdForExtension(extension, frame), |
| 156 &sources.front(), sources.size(), EXTENSION_GROUP_CONTENT_SCRIPTS); | 156 &sources.front(), sources.size(), EXTENSION_GROUP_CONTENT_SCRIPTS); |
| 157 } | 157 } |
| 158 } else { | 158 } else { |
| 159 frame->document().insertUserStyleSheet( | 159 frame->document().insertUserStyleSheet( |
| 160 WebString::fromUTF8(params.code), | 160 WebString::fromUTF8(params.code), |
| 161 // Author level is consistent with WebView::addUserStyleSheet. | 161 // Author level is consistent with WebView::addUserStyleSheet. |
| 162 WebDocument::UserStyleAuthorLevel); | 162 WebDocument::UserStyleAuthorLevel); |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 | 165 |
| 166 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( | 166 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( |
| 167 render_view->GetRoutingId(), params.request_id, true, "")); | 167 render_view->GetRoutingID(), params.request_id, true, "")); |
| 168 } | 168 } |
| 169 | 169 |
| 170 bool UserScriptIdleScheduler::GetAllChildFrames( | 170 bool UserScriptIdleScheduler::GetAllChildFrames( |
| 171 WebFrame* parent_frame, | 171 WebFrame* parent_frame, |
| 172 std::vector<WebFrame*>* frames_vector) const { | 172 std::vector<WebFrame*>* frames_vector) const { |
| 173 if (!parent_frame) | 173 if (!parent_frame) |
| 174 return false; | 174 return false; |
| 175 | 175 |
| 176 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame; | 176 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame; |
| 177 child_frame = child_frame->nextSibling()) { | 177 child_frame = child_frame->nextSibling()) { |
| 178 frames_vector->push_back(child_frame); | 178 frames_vector->push_back(child_frame); |
| 179 GetAllChildFrames(child_frame, frames_vector); | 179 GetAllChildFrames(child_frame, frames_vector); |
| 180 } | 180 } |
| 181 return true; | 181 return true; |
| 182 } | 182 } |
| OLD | NEW |