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_SCRIPT_CONTEXT_SET_H_
      6 #define EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/bind.h"
     13 #include "v8/include/v8.h"
     14 
     15 class GURL;
     16 
     17 namespace base {
     18 class ListValue;
     19 }
     20 
     21 namespace content {
     22 class RenderView;
     23 }
     24 
     25 namespace v8 {
     26 class Context;
     27 }
     28 
     29 namespace extensions {
     30 class ScriptContext;
     31 
     32 // A container of ExtensionBindingsContext. Since calling JavaScript within a
     33 // context can cause any number of contexts to be created or destroyed, this
     34 // has additional smarts to help with the set changing underneath callers.
     35 class ScriptContextSet {
     36  public:
     37   ScriptContextSet();
     38   ~ScriptContextSet();
     39 
     40   int size() const;
     41 
     42   // Takes ownership of |context|.
     43   void Add(ScriptContext* context);
     44 
     45   // If the specified context is contained in this set, remove it, then delete
     46   // it asynchronously. After this call returns the context object will still
     47   // be valid, but its frame() pointer will be cleared.
     48   void Remove(ScriptContext* context);
     49 
     50   // Returns a copy to protect against changes.
     51   typedef std::set<ScriptContext*> ContextSet;
     52   ContextSet GetAll() const;
     53 
     54   // Gets the ScriptContext corresponding to v8::Context::GetCurrent(), or
     55   // NULL if no such context exists.
     56   ScriptContext* GetCurrent() const;
     57 
     58   // Gets the ScriptContext corresponding to v8::Context::GetCalling(), or
     59   // NULL if no such context exists.
     60   ScriptContext* GetCalling() const;
     61 
     62   // Gets the ScriptContext corresponding to the specified
     63   // v8::Context or NULL if no such context exists.
     64   ScriptContext* GetByV8Context(v8::Handle<v8::Context> context) const;
     65 
     66   // Synchronously runs |callback| with each ScriptContext that belongs to
     67   // |extension_id| in |render_view|.
     68   //
     69   // An empty |extension_id| will match all extensions, and a NULL |render_view|
     70   // will match all render views, but try to use the inline variants of these
     71   // methods instead.
     72   void ForEach(const std::string& extension_id,
     73                content::RenderView* render_view,
     74                const base::Callback<void(ScriptContext*)>& callback) const;
     75   // ForEach which matches all extensions.
     76   void ForEach(content::RenderView* render_view,
     77                const base::Callback<void(ScriptContext*)>& callback) const {
     78     ForEach("", render_view, callback);
     79   }
     80   // ForEach which matches all render views.
     81   void ForEach(const std::string& extension_id,
     82                const base::Callback<void(ScriptContext*)>& callback) const {
     83     ForEach(extension_id, NULL, callback);
     84   }
     85 
     86   // Cleans up contexts belonging to an unloaded extension.
     87   //
     88   // Returns the set of ScriptContexts that were removed as a result. These
     89   // are safe to interact with until the end of the current event loop, since
     90   // they're deleted asynchronously.
     91   ContextSet OnExtensionUnloaded(const std::string& extension_id);
     92 
     93  private:
     94   ContextSet contexts_;
     95 
     96   DISALLOW_COPY_AND_ASSIGN(ScriptContextSet);
     97 };
     98 
     99 }  // namespace extensions
    100 
    101 #endif  // EXTENSIONS_RENDERER_SCRIPT_CONTEXT_SET_H_
    102