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 // Watches for the deletion of a RenderFrame, after which is_valid will return |
| 27 // false. |
| 28 class ProgrammaticScriptInjector::FrameWatcher |
| 29 : public content::RenderFrameObserver { |
| 30 public: |
| 31 explicit FrameWatcher(content::RenderFrame* render_frame) |
| 32 : content::RenderFrameObserver(render_frame), is_valid_(true) {} |
| 33 ~FrameWatcher() override {} |
| 34 |
| 35 bool is_frame_valid() const { return is_valid_; } |
| 36 |
| 37 private: |
| 38 void FrameDetached() override { is_valid_ = false; } |
| 39 void OnDestruct() override { is_valid_ = false; } |
| 40 |
| 41 bool is_valid_; |
| 42 |
| 43 DISALLOW_COPY_AND_ASSIGN(FrameWatcher); |
| 44 }; |
| 45 |
25 ProgrammaticScriptInjector::ProgrammaticScriptInjector( | 46 ProgrammaticScriptInjector::ProgrammaticScriptInjector( |
26 const ExtensionMsg_ExecuteCode_Params& params, | 47 const ExtensionMsg_ExecuteCode_Params& params, |
27 content::RenderFrame* render_frame) | 48 content::RenderFrame* render_frame) |
28 : params_(new ExtensionMsg_ExecuteCode_Params(params)), | 49 : params_(new ExtensionMsg_ExecuteCode_Params(params)), |
29 url_(ScriptContext::GetDataSourceURLForFrame( | 50 url_( |
30 render_frame->GetWebFrame())), | 51 ScriptContext::GetDataSourceURLForFrame(render_frame->GetWebFrame())), |
31 render_frame_(render_frame), | 52 frame_watcher_(new FrameWatcher(render_frame)), |
32 finished_(false) { | 53 finished_(false) { |
33 effective_url_ = ScriptContext::GetEffectiveDocumentURL( | 54 effective_url_ = ScriptContext::GetEffectiveDocumentURL( |
34 render_frame->GetWebFrame(), url_, params.match_about_blank); | 55 render_frame->GetWebFrame(), url_, params.match_about_blank); |
35 } | 56 } |
36 | 57 |
37 ProgrammaticScriptInjector::~ProgrammaticScriptInjector() { | 58 ProgrammaticScriptInjector::~ProgrammaticScriptInjector() { |
38 } | 59 } |
39 | 60 |
40 UserScript::InjectionType ProgrammaticScriptInjector::script_type() | 61 UserScript::InjectionType ProgrammaticScriptInjector::script_type() |
41 const { | 62 const { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 } | 164 } |
144 | 165 |
145 UserScript::RunLocation ProgrammaticScriptInjector::GetRunLocation() const { | 166 UserScript::RunLocation ProgrammaticScriptInjector::GetRunLocation() const { |
146 return static_cast<UserScript::RunLocation>(params_->run_at); | 167 return static_cast<UserScript::RunLocation>(params_->run_at); |
147 } | 168 } |
148 | 169 |
149 void ProgrammaticScriptInjector::Finish(const std::string& error) { | 170 void ProgrammaticScriptInjector::Finish(const std::string& error) { |
150 DCHECK(!finished_); | 171 DCHECK(!finished_); |
151 finished_ = true; | 172 finished_ = true; |
152 | 173 |
153 render_frame_->Send(new ExtensionHostMsg_ExecuteCodeFinished( | 174 // It's possible that the render frame was destroyed in the course of |
154 render_frame_->GetRoutingID(), | 175 // injecting scripts. Don't respond if it was (the browser side watches for |
155 params_->request_id, | 176 // frame deletions so nothing is left hanging). |
156 error, | 177 if (frame_watcher_->is_frame_valid()) { |
157 url_, | 178 frame_watcher_->render_frame()->Send( |
158 results_)); | 179 new ExtensionHostMsg_ExecuteCodeFinished( |
| 180 frame_watcher_->render_frame()->GetRoutingID(), params_->request_id, |
| 181 error, url_, results_)); |
| 182 } |
159 } | 183 } |
160 | 184 |
161 } // namespace extensions | 185 } // namespace extensions |
OLD | NEW |