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 3 // found in the LICENSE file. 4 5 #ifndef EXTENSIONS_RENDERER_USER_SCRIPT_INJECTOR_H_ 6 #define EXTENSIONS_RENDERER_USER_SCRIPT_INJECTOR_H_ 7 8 #include <string> 9 10 #include "base/memory/scoped_ptr.h" 11 #include "base/scoped_observer.h" 12 #include "extensions/common/user_script.h" 13 #include "extensions/renderer/script_injection.h" 14 #include "extensions/renderer/user_script_set.h" 15 16 namespace blink { 17 class WebFrame; 18 } 19 20 namespace extensions { 21 class Extension; 22 23 // A ScriptInjector for UserScripts. 24 class UserScriptInjector : public ScriptInjector, 25 public UserScriptSet::Observer { 26 public: 27 UserScriptInjector(const UserScript* user_script, 28 UserScriptSet* user_script_set, 29 bool is_declarative); 30 virtual ~UserScriptInjector(); 31 32 private: 33 // UserScriptSet::Observer implementation. 34 virtual void OnUserScriptsUpdated( 35 const std::set<std::string>& changed_extensions, 36 const std::vector<UserScript*>& scripts) OVERRIDE; 37 38 // ScriptInjector implementation. 39 virtual UserScript::InjectionType script_type() const OVERRIDE; 40 virtual bool ShouldExecuteInChildFrames() const OVERRIDE; 41 virtual bool ShouldExecuteInMainWorld() const OVERRIDE; 42 virtual bool IsUserGesture() const OVERRIDE; 43 virtual bool ExpectsResults() const OVERRIDE; 44 virtual bool ShouldInjectJs( 45 UserScript::RunLocation run_location) const OVERRIDE; 46 virtual bool ShouldInjectCss( 47 UserScript::RunLocation run_location) const OVERRIDE; 48 virtual PermissionsData::AccessType CanExecuteOnFrame( 49 const Extension* extension, 50 blink::WebFrame* web_frame, 51 int tab_id, 52 const GURL& top_url) const OVERRIDE; 53 virtual std::vector<blink::WebScriptSource> GetJsSources( 54 UserScript::RunLocation run_location) const OVERRIDE; 55 virtual std::vector<std::string> GetCssSources( 56 UserScript::RunLocation run_location) const OVERRIDE; 57 virtual void OnInjectionComplete( 58 scoped_ptr<base::ListValue> execution_results, 59 ScriptsRunInfo* scripts_run_info, 60 UserScript::RunLocation run_location) OVERRIDE; 61 virtual void OnWillNotInject(InjectFailureReason reason) OVERRIDE; 62 63 // The associated user script. Owned by the UserScriptInjector that created 64 // this object. 65 const UserScript* script_; 66 67 // The id of the associated user script. We cache this because when we update 68 // the |script_| associated with this injection, the old referance may be 69 // deleted. 70 int script_id_; 71 72 // The associated extension id, preserved for the same reason as |script_id|. 73 std::string extension_id_; 74 75 // Indicates whether or not this script is declarative. This influences which 76 // script permissions are checked before injection. 77 bool is_declarative_; 78 79 ScopedObserver<UserScriptSet, UserScriptSet::Observer> 80 user_script_set_observer_; 81 82 DISALLOW_COPY_AND_ASSIGN(UserScriptInjector); 83 }; 84 85 } // namespace extensions 86 87 #endif // EXTENSIONS_RENDERER_USER_SCRIPT_INJECTOR_H_ 88