Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/programmatic_script_injector.h" | 5 #include "extensions/renderer/programmatic_script_injector.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "content/public/common/url_constants.h" | 10 #include "content/public/common/url_constants.h" |
| 11 #include "content/public/renderer/render_frame.h" | 11 #include "content/public/renderer/render_frame.h" |
| 12 #include "content/public/renderer/render_frame_observer.h" | |
| 12 #include "extensions/common/error_utils.h" | 13 #include "extensions/common/error_utils.h" |
| 13 #include "extensions/common/extension_messages.h" | 14 #include "extensions/common/extension_messages.h" |
| 14 #include "extensions/common/manifest_constants.h" | 15 #include "extensions/common/manifest_constants.h" |
| 15 #include "extensions/common/permissions/permissions_data.h" | 16 #include "extensions/common/permissions/permissions_data.h" |
| 16 #include "extensions/renderer/injection_host.h" | 17 #include "extensions/renderer/injection_host.h" |
| 17 #include "extensions/renderer/script_context.h" | 18 #include "extensions/renderer/script_context.h" |
| 18 #include "third_party/WebKit/public/platform/WebString.h" | 19 #include "third_party/WebKit/public/platform/WebString.h" |
| 19 #include "third_party/WebKit/public/web/WebDocument.h" | 20 #include "third_party/WebKit/public/web/WebDocument.h" |
| 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 21 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 21 #include "third_party/WebKit/public/web/WebScriptSource.h" | 22 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 22 | 23 |
| 23 namespace extensions { | 24 namespace extensions { |
| 24 | 25 |
| 26 class ProgrammaticScriptInjector::FrameWatcher | |
|
not at google - send to devlin
2015/06/29 18:28:11
// Watches for the deletion of a RenderFrame, afte
Devlin
2015/06/29 19:41:44
Done.
| |
| 27 : public content::RenderFrameObserver { | |
| 28 public: | |
| 29 explicit FrameWatcher(content::RenderFrame* render_frame) | |
| 30 : content::RenderFrameObserver(render_frame), is_valid_(true) {} | |
| 31 ~FrameWatcher() override {} | |
| 32 | |
| 33 bool is_frame_valid() const { return is_valid_; } | |
| 34 | |
| 35 private: | |
| 36 void FrameDetached() override { is_valid_ = false; } | |
| 37 void OnDestruct() override { is_valid_ = false; } | |
| 38 | |
| 39 bool is_valid_; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(FrameWatcher); | |
| 42 }; | |
| 43 | |
| 25 ProgrammaticScriptInjector::ProgrammaticScriptInjector( | 44 ProgrammaticScriptInjector::ProgrammaticScriptInjector( |
| 26 const ExtensionMsg_ExecuteCode_Params& params, | 45 const ExtensionMsg_ExecuteCode_Params& params, |
| 27 content::RenderFrame* render_frame) | 46 content::RenderFrame* render_frame) |
| 28 : params_(new ExtensionMsg_ExecuteCode_Params(params)), | 47 : params_(new ExtensionMsg_ExecuteCode_Params(params)), |
| 29 url_(ScriptContext::GetDataSourceURLForFrame( | 48 url_( |
| 30 render_frame->GetWebFrame())), | 49 ScriptContext::GetDataSourceURLForFrame(render_frame->GetWebFrame())), |
| 31 render_frame_(render_frame), | 50 frame_watcher_(new FrameWatcher(render_frame)), |
| 32 finished_(false) { | 51 finished_(false) { |
| 33 effective_url_ = ScriptContext::GetEffectiveDocumentURL( | 52 effective_url_ = ScriptContext::GetEffectiveDocumentURL( |
| 34 render_frame->GetWebFrame(), url_, params.match_about_blank); | 53 render_frame->GetWebFrame(), url_, params.match_about_blank); |
| 35 } | 54 } |
| 36 | 55 |
| 37 ProgrammaticScriptInjector::~ProgrammaticScriptInjector() { | 56 ProgrammaticScriptInjector::~ProgrammaticScriptInjector() { |
| 38 } | 57 } |
| 39 | 58 |
| 40 UserScript::InjectionType ProgrammaticScriptInjector::script_type() | 59 UserScript::InjectionType ProgrammaticScriptInjector::script_type() |
| 41 const { | 60 const { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 } | 162 } |
| 144 | 163 |
| 145 UserScript::RunLocation ProgrammaticScriptInjector::GetRunLocation() const { | 164 UserScript::RunLocation ProgrammaticScriptInjector::GetRunLocation() const { |
| 146 return static_cast<UserScript::RunLocation>(params_->run_at); | 165 return static_cast<UserScript::RunLocation>(params_->run_at); |
| 147 } | 166 } |
| 148 | 167 |
| 149 void ProgrammaticScriptInjector::Finish(const std::string& error) { | 168 void ProgrammaticScriptInjector::Finish(const std::string& error) { |
| 150 DCHECK(!finished_); | 169 DCHECK(!finished_); |
| 151 finished_ = true; | 170 finished_ = true; |
| 152 | 171 |
| 153 render_frame_->Send(new ExtensionHostMsg_ExecuteCodeFinished( | 172 // It's possible that the render frame was destroyed in the course of |
| 154 render_frame_->GetRoutingID(), | 173 // injecting scripts. Don't respond if it was (the browser side watches for |
| 155 params_->request_id, | 174 // frame deletions so nothing is left hanging). |
| 156 error, | 175 if (frame_watcher_->is_frame_valid()) { |
| 157 url_, | 176 frame_watcher_->render_frame()->Send( |
| 158 results_)); | 177 new ExtensionHostMsg_ExecuteCodeFinished( |
| 178 frame_watcher_->render_frame()->GetRoutingID(), params_->request_id, | |
| 179 error, url_, results_)); | |
| 180 } | |
| 159 } | 181 } |
| 160 | 182 |
| 161 } // namespace extensions | 183 } // namespace extensions |
| OLD | NEW |