1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file. 4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ 6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ 7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/basictypes.h" 9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/macros.h" 10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "extensions/common/user_script.h" 11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "extensions/renderer/script_injector.h" 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace blink { 141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass WebLocalFrame; 15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace extensions { 18116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass Extension; 19116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstruct ScriptsRunInfo; 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// A script wrapper which is aware of whether or not it is allowed to execute, 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// and contains the implementation to do so. 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ScriptInjection { 24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public: 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Return the id of the extension associated with the given world. 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static std::string GetExtensionIdForIsolatedWorld(int world_id); 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Remove the isolated world associated with the given extension. 29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static void RemoveIsolatedWorld(const std::string& extension_id); 30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ScriptInjection(scoped_ptr<ScriptInjector> injector, 321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci blink::WebLocalFrame* web_frame, 33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& extension_id, 34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch UserScript::RunLocation run_location, 35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int tab_id); 36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ~ScriptInjection(); 37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Try to inject the script at the |current_location|. This returns true if 39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // the script has either injected or will never inject (i.e., if the object 40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // is done), and false if injection is delayed (either for permission purposes 41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // or because |current_location| is not the designated |run_location_|). 42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // NOTE: |extension| may be NULL, if the extension is removed! 43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool TryToInject(UserScript::RunLocation current_location, 44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const Extension* extension, 45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ScriptsRunInfo* scripts_run_info); 46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Called when permission for the given injection has been granted. 48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Returns true if the injection ran. 49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool OnPermissionGranted(const Extension* extension, 50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ScriptsRunInfo* scripts_run_info); 51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Accessors. 531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci blink::WebLocalFrame* web_frame() const { return web_frame_; } 54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const std::string& extension_id() const { return extension_id_; } 55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int64 request_id() const { return request_id_; } 56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) private: 58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Send a message to the browser requesting permission to execute. 59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void RequestPermission(); 60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Injects the script, optionally populating |scripts_run_info|. 62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void Inject(const Extension* extension, ScriptsRunInfo* scripts_run_info); 63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Inject any JS scripts into the |frame|, optionally populating 65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |execution_results|. 66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void InjectJs(const Extension* extension, 671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci blink::WebLocalFrame* frame, 68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::ListValue* execution_results); 69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Inject any CSS source into the |frame|. 711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void InjectCss(blink::WebLocalFrame* frame); 72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Notify that we will not inject, and mark it as acknowledged. 74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); 75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The injector for this injection. 77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_ptr<ScriptInjector> injector_; 78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The (main) WebFrame into which this should inject the script. 801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci blink::WebLocalFrame* web_frame_; 81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The id of the associated extension. 8346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) std::string extension_id_; 84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The location in the document load at which we inject the script. 86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch UserScript::RunLocation run_location_; 87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The tab id associated with the frame. 89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int tab_id_; 90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // This injection's request id. This will be -1 unless the injection is 92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // currently waiting on permission. 93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch int64 request_id_; 94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Whether or not the injection is complete, either via injecting the script 96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // or because it will never complete. 97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool complete_; 98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ScriptInjection); 100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}; 101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} // namespace extensions 103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ 105