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_PROGRAMMATIC_SCRIPT_INJECTOR_H_ 6 #define EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_ 7 8 #include "base/macros.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "extensions/renderer/script_injection.h" 11 #include "url/gurl.h" 12 13 struct ExtensionMsg_ExecuteCode_Params; 14 15 namespace blink { 16 class WebFrame; 17 } 18 19 namespace content { 20 class RenderView; 21 } 22 23 namespace extensions { 24 class Extension; 25 26 // A ScriptInjector to handle tabs.executeScript(). 27 class ProgrammaticScriptInjector : public ScriptInjector { 28 public: 29 ProgrammaticScriptInjector(const ExtensionMsg_ExecuteCode_Params& params, 30 blink::WebFrame* web_frame); 31 virtual ~ProgrammaticScriptInjector(); 32 33 private: 34 // ScriptInjector implementation. 35 virtual UserScript::InjectionType script_type() const OVERRIDE; 36 virtual bool ShouldExecuteInChildFrames() const OVERRIDE; 37 virtual bool ShouldExecuteInMainWorld() const OVERRIDE; 38 virtual bool IsUserGesture() const OVERRIDE; 39 virtual bool ExpectsResults() const OVERRIDE; 40 virtual bool ShouldInjectJs( 41 UserScript::RunLocation run_location) const OVERRIDE; 42 virtual bool ShouldInjectCss( 43 UserScript::RunLocation run_location) const OVERRIDE; 44 virtual PermissionsData::AccessType CanExecuteOnFrame( 45 const Extension* extension, 46 blink::WebFrame* web_frame, 47 int tab_id, 48 const GURL& top_url) const OVERRIDE; 49 virtual std::vector<blink::WebScriptSource> GetJsSources( 50 UserScript::RunLocation run_location) const OVERRIDE; 51 virtual std::vector<std::string> GetCssSources( 52 UserScript::RunLocation run_location) const OVERRIDE; 53 virtual void OnInjectionComplete( 54 scoped_ptr<base::ListValue> execution_results, 55 ScriptsRunInfo* scripts_run_info, 56 UserScript::RunLocation run_location) OVERRIDE; 57 virtual void OnWillNotInject(InjectFailureReason reason) OVERRIDE; 58 59 // Return the run location for this injector. 60 UserScript::RunLocation GetRunLocation() const; 61 62 // Notify the browser that the script was injected (or never will be), and 63 // send along any results or errors. 64 void Finish(const std::string& error); 65 66 // The parameters for injecting the script. 67 scoped_ptr<ExtensionMsg_ExecuteCode_Params> params_; 68 69 // The url of the frame into which we are injecting. 70 GURL url_; 71 72 // The RenderView to which we send the response upon completion. 73 content::RenderView* render_view_; 74 75 // The results of the script execution. 76 scoped_ptr<base::ListValue> results_; 77 78 // Whether or not this script injection has finished. 79 bool finished_; 80 81 DISALLOW_COPY_AND_ASSIGN(ProgrammaticScriptInjector); 82 }; 83 84 } // namespace extensions 85 86 #endif // EXTENSIONS_RENDERER_PROGRAMMATIC_SCRIPT_INJECTOR_H_ 87