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_SCRIPT_INJECTION_MANAGER_H_ 6#define EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_ 7 8#include <map> 9#include <set> 10#include <string> 11 12#include "base/callback.h" 13#include "base/memory/scoped_vector.h" 14#include "base/memory/shared_memory.h" 15#include "base/scoped_observer.h" 16#include "extensions/common/user_script.h" 17#include "extensions/renderer/script_injection.h" 18#include "extensions/renderer/user_script_set_manager.h" 19 20struct ExtensionMsg_ExecuteCode_Params; 21 22namespace blink { 23class WebFrame; 24class WebLocalFrame; 25} 26 27namespace content { 28class RenderView; 29} 30 31namespace extensions { 32class Extension; 33class ExtensionSet; 34 35// The ScriptInjectionManager manages extensions injecting scripts into frames 36// via both content/user scripts and tabs.executeScript(). It is responsible for 37// maintaining any pending injections awaiting permission or the appropriate 38// load point, and injecting them when ready. 39class ScriptInjectionManager : public UserScriptSetManager::Observer { 40 public: 41 ScriptInjectionManager(const ExtensionSet* extensions, 42 UserScriptSetManager* user_script_set_manager); 43 virtual ~ScriptInjectionManager(); 44 45 // Notifies that a new render view has been created. 46 void OnRenderViewCreated(content::RenderView* render_view); 47 48 private: 49 // A RenderViewObserver implementation which watches the various render views 50 // in order to notify the ScriptInjectionManager of different document load 51 // states. 52 class RVOHelper; 53 54 typedef std::map<blink::WebFrame*, UserScript::RunLocation> FrameStatusMap; 55 56 // UserScriptSetManager::Observer implementation. 57 virtual void OnUserScriptsUpdated( 58 const std::set<std::string>& changed_extensions, 59 const std::vector<UserScript*>& scripts) OVERRIDE; 60 61 // Notifies that an RVOHelper should be removed. 62 void RemoveObserver(RVOHelper* helper); 63 64 // Invalidate any pending tasks associated with |frame|. 65 void InvalidateForFrame(blink::WebFrame* frame); 66 67 // Inject appropriate scripts into |frame|. 68 void InjectScripts(blink::WebFrame* frame, 69 UserScript::RunLocation run_location); 70 71 // Handle the ExecuteCode extension message. 72 void HandleExecuteCode(const ExtensionMsg_ExecuteCode_Params& params, 73 content::RenderView* render_view); 74 75 // Handle the ExecuteDeclarativeScript extension message. 76 void HandleExecuteDeclarativeScript(blink::WebFrame* web_frame, 77 int tab_id, 78 const ExtensionId& extension_id, 79 int script_id, 80 const GURL& url); 81 82 // Handle the GrantInjectionPermission extension message. 83 void HandlePermitScriptInjection(int64 request_id); 84 85 // Extensions metadata, owned by Dispatcher (which owns this object). 86 const ExtensionSet* extensions_; 87 88 // The map of active web frames to their corresponding statuses. The 89 // RunLocation of the frame corresponds to the last location that has ran. 90 FrameStatusMap frame_statuses_; 91 92 // The collection of RVOHelpers. 93 ScopedVector<RVOHelper> rvo_helpers_; 94 95 // The set of UserScripts associated with extensions. Owned by the Dispatcher. 96 UserScriptSetManager* user_script_set_manager_; 97 98 // Pending injections which are waiting for either the proper run location or 99 // user consent. 100 ScopedVector<ScriptInjection> pending_injections_; 101 102 ScopedObserver<UserScriptSetManager, UserScriptSetManager::Observer> 103 user_script_set_manager_observer_; 104 105 DISALLOW_COPY_AND_ASSIGN(ScriptInjectionManager); 106}; 107 108} // namespace extensions 109 110#endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_ 111