Home | History | Annotate | Download | only in renderer
      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_SET_MANAGER_H_
      6 #define EXTENSIONS_RENDERER_USER_SCRIPT_SET_MANAGER_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 
     12 #include "base/memory/linked_ptr.h"
     13 #include "base/memory/shared_memory.h"
     14 #include "base/observer_list.h"
     15 #include "content/public/renderer/render_process_observer.h"
     16 #include "extensions/common/extension.h"
     17 #include "extensions/common/user_script.h"
     18 #include "extensions/renderer/user_script_set.h"
     19 
     20 namespace IPC {
     21 class Message;
     22 }
     23 
     24 namespace blink {
     25 class WebFrame;
     26 }
     27 
     28 namespace extensions {
     29 
     30 class ExtensionSet;
     31 class ScriptInjection;
     32 
     33 // Manager for separate UserScriptSets, one for each shared memory region.
     34 // Regions are organized as follows:
     35 // static_scripts -- contains all extensions' scripts that are statically
     36 //                   declared in the extension manifest.
     37 // programmatic_scripts -- one region per extension containing only
     38 //                         programmatically-declared scripts, instantiated
     39 //                         when an extension first creates a declarative rule
     40 //                         that would, if triggered, request a script injection.
     41 class UserScriptSetManager : public content::RenderProcessObserver {
     42  public:
     43   // Like a UserScriptSet::Observer, but automatically subscribes to all sets
     44   // associated with the manager.
     45   class Observer {
     46    public:
     47     virtual void OnUserScriptsUpdated(
     48         const std::set<std::string>& changed_extensions,
     49         const std::vector<UserScript*>& scripts) = 0;
     50   };
     51 
     52   UserScriptSetManager(const ExtensionSet* extensions);
     53 
     54   virtual ~UserScriptSetManager();
     55 
     56   void AddObserver(Observer* observer);
     57   void RemoveObserver(Observer* observer);
     58 
     59   // Looks up the script injection associated with |script_id| and |extension|
     60   // in the context of the given |web_frame|, |tab_id|, and |url|.
     61   scoped_ptr<ScriptInjection> GetInjectionForDeclarativeScript(
     62       int script_id,
     63       blink::WebFrame* web_frame,
     64       int tab_id,
     65       const GURL& url,
     66       const Extension* extension);
     67 
     68   // Put all injections from |static_scripts| and each of
     69   // |programmatic_scripts_| into |injections|.
     70   void GetAllInjections(ScopedVector<ScriptInjection>* injections,
     71                         blink::WebFrame* web_frame,
     72                         int tab_id,
     73                         UserScript::RunLocation run_location);
     74 
     75   // Get active extension IDs from |static_scripts| and each of
     76   // |programmatic_scripts_|.
     77   void GetAllActiveExtensionIds(std::set<std::string>* ids) const;
     78 
     79   const UserScriptSet* static_scripts() const { return &static_scripts_; }
     80 
     81  private:
     82   // Map for per-extension sets that may be defined programmatically.
     83   typedef std::map<ExtensionId, linked_ptr<UserScriptSet> > UserScriptSetMap;
     84 
     85   // content::RenderProcessObserver implementation.
     86   virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE;
     87 
     88   UserScriptSet* GetProgrammaticScriptsByExtension(
     89       const ExtensionId& extensionId);
     90 
     91   // Handle the UpdateUserScripts extension message.
     92   void OnUpdateUserScripts(base::SharedMemoryHandle shared_memory,
     93                            const ExtensionId& extension_id,
     94                            const std::set<std::string>& changed_extensions);
     95 
     96   // Scripts statically defined in extension manifests.
     97   UserScriptSet static_scripts_;
     98 
     99   // Scripts programmatically-defined through API calls (initialized and stored
    100   // per-extension).
    101   UserScriptSetMap programmatic_scripts_;
    102 
    103   // The set of all known extensions. Owned by the Dispatcher.
    104   const ExtensionSet* extensions_;
    105 
    106   // The associated observers.
    107   ObserverList<Observer> observers_;
    108 
    109   DISALLOW_COPY_AND_ASSIGN(UserScriptSetManager);
    110 };
    111 
    112 }  // namespace extensions
    113 
    114 #endif  // EXTENSIONS_RENDERER_USER_SCRIPT_SET_MANAGER_H_
    115