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/user_script_injector.h" | 5 #include "extensions/renderer/user_script_injector.h" |
6 | 6 |
7 #include <tuple> | 7 #include <tuple> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 user_script_set_observer_(this) { | 97 user_script_set_observer_(this) { |
98 user_script_set_observer_.Add(script_list); | 98 user_script_set_observer_.Add(script_list); |
99 } | 99 } |
100 | 100 |
101 UserScriptInjector::~UserScriptInjector() { | 101 UserScriptInjector::~UserScriptInjector() { |
102 } | 102 } |
103 | 103 |
104 void UserScriptInjector::OnUserScriptsUpdated( | 104 void UserScriptInjector::OnUserScriptsUpdated( |
105 const std::set<HostID>& changed_hosts, | 105 const std::set<HostID>& changed_hosts, |
106 const UserScriptList& scripts) { | 106 const UserScriptList& scripts) { |
| 107 // When user scripts are updated, all the old script pointers are invalidated. |
| 108 script_ = nullptr; |
107 // If the host causing this injection changed, then this injection | 109 // If the host causing this injection changed, then this injection |
108 // will be removed, and there's no guarantee the backing script still exists. | 110 // will be removed, and there's no guarantee the backing script still exists. |
109 if (changed_hosts.count(host_id_) > 0) { | 111 if (changed_hosts.count(host_id_) > 0) |
110 script_ = nullptr; | |
111 return; | 112 return; |
112 } | |
113 | 113 |
114 for (const std::unique_ptr<UserScript>& script : scripts) { | 114 for (const std::unique_ptr<UserScript>& script : scripts) { |
115 // We need to compare to |script_id_| (and not to script_->id()) because the | |
116 // old |script_| may be deleted by now. | |
117 if (script->id() == script_id_) { | 115 if (script->id() == script_id_) { |
118 script_ = script.get(); | 116 script_ = script.get(); |
119 break; | 117 break; |
120 } | 118 } |
121 } | 119 } |
| 120 // If |host_id_| wasn't in |changed_hosts|, then the script for this injection |
| 121 // should be guaranteed to exist. |
| 122 DCHECK(script_); |
122 } | 123 } |
123 | 124 |
124 UserScript::InjectionType UserScriptInjector::script_type() const { | 125 UserScript::InjectionType UserScriptInjector::script_type() const { |
125 return UserScript::CONTENT_SCRIPT; | 126 return UserScript::CONTENT_SCRIPT; |
126 } | 127 } |
127 | 128 |
128 bool UserScriptInjector::ShouldExecuteInMainWorld() const { | 129 bool UserScriptInjector::ShouldExecuteInMainWorld() const { |
129 return false; | 130 return false; |
130 } | 131 } |
131 | 132 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 void UserScriptInjector::OnInjectionComplete( | 282 void UserScriptInjector::OnInjectionComplete( |
282 std::unique_ptr<base::Value> execution_result, | 283 std::unique_ptr<base::Value> execution_result, |
283 UserScript::RunLocation run_location, | 284 UserScript::RunLocation run_location, |
284 content::RenderFrame* render_frame) {} | 285 content::RenderFrame* render_frame) {} |
285 | 286 |
286 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, | 287 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, |
287 content::RenderFrame* render_frame) { | 288 content::RenderFrame* render_frame) { |
288 } | 289 } |
289 | 290 |
290 } // namespace extensions | 291 } // namespace extensions |
OLD | NEW |