| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_ |
| 6 #define CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 // Chrome's wrapper for a v8 context. | 24 // Chrome's wrapper for a v8 context. |
| 25 // | 25 // |
| 26 // TODO(aa): Consider converting this back to a set of bindings_utils. It would | 26 // TODO(aa): Consider converting this back to a set of bindings_utils. It would |
| 27 // require adding WebFrame::GetIsolatedWorldIdByV8Context() to WebCore, but then | 27 // require adding WebFrame::GetIsolatedWorldIdByV8Context() to WebCore, but then |
| 28 // we won't need this object and it's a bit less state to keep track of. | 28 // we won't need this object and it's a bit less state to keep track of. |
| 29 class ChromeV8Context { | 29 class ChromeV8Context { |
| 30 public: | 30 public: |
| 31 ChromeV8Context(v8::Handle<v8::Context> context, | 31 ChromeV8Context(v8::Handle<v8::Context> context, |
| 32 WebKit::WebFrame* frame, | 32 WebKit::WebFrame* frame, |
| 33 const std::string& extension_id, | 33 const Extension* extension, |
| 34 extensions::Feature::Context context_type); | 34 extensions::Feature::Context context_type); |
| 35 ~ChromeV8Context(); | 35 ~ChromeV8Context(); |
| 36 | 36 |
| 37 v8::Handle<v8::Context> v8_context() const { | 37 v8::Handle<v8::Context> v8_context() const { |
| 38 return v8_context_; | 38 return v8_context_; |
| 39 } | 39 } |
| 40 | 40 |
| 41 const std::string& extension_id() const { | 41 const Extension* extension() const { |
| 42 return extension_id_; | 42 return extension_; |
| 43 } | 43 } |
| 44 | 44 |
| 45 WebKit::WebFrame* web_frame() const { | 45 WebKit::WebFrame* web_frame() const { |
| 46 return web_frame_; | 46 return web_frame_; |
| 47 } | 47 } |
| 48 void clear_web_frame() { | 48 void clear_web_frame() { |
| 49 web_frame_ = NULL; | 49 web_frame_ = NULL; |
| 50 } | 50 } |
| 51 | 51 |
| 52 extensions::Feature::Context context_type() const { | 52 extensions::Feature::Context context_type() const { |
| 53 return context_type_; | 53 return context_type_; |
| 54 } | 54 } |
| 55 | 55 |
| 56 void set_module_system(scoped_ptr<ModuleSystem> module_system) { | 56 void set_module_system(scoped_ptr<ModuleSystem> module_system) { |
| 57 module_system_ = module_system.Pass(); | 57 module_system_ = module_system.Pass(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Returns the ID of the extension associated with this context, or empty |
| 61 // string if there is no such extension. |
| 62 std::string GetExtensionID(); |
| 63 |
| 60 // Returns a special Chrome-specific hidden object that is associated with a | 64 // Returns a special Chrome-specific hidden object that is associated with a |
| 61 // context, but not reachable from the JavaScript in that context. This is | 65 // context, but not reachable from the JavaScript in that context. This is |
| 62 // used by our v8::Extension implementations as a way to share code and as a | 66 // used by our v8::Extension implementations as a way to share code and as a |
| 63 // bridge between C++ and JavaScript. | 67 // bridge between C++ and JavaScript. |
| 64 static v8::Handle<v8::Value> GetOrCreateChromeHidden( | 68 static v8::Handle<v8::Value> GetOrCreateChromeHidden( |
| 65 v8::Handle<v8::Context> context); | 69 v8::Handle<v8::Context> context); |
| 66 | 70 |
| 67 // Return the chromeHidden object associated with this context, or an empty | 71 // Return the chromeHidden object associated with this context, or an empty |
| 68 // handle if no chrome hidden has been created (by GetOrCreateChromeHidden) | 72 // handle if no chrome hidden has been created (by GetOrCreateChromeHidden) |
| 69 // yet for this context. | 73 // yet for this context. |
| 70 v8::Handle<v8::Value> GetChromeHidden() const; | 74 v8::Handle<v8::Value> GetChromeHidden() const; |
| 71 | 75 |
| 72 // Returns the RenderView associated with this context. Can return NULL if the | 76 // Returns the RenderView associated with this context. Can return NULL if the |
| 73 // context is in the process of being destroyed. | 77 // context is in the process of being destroyed. |
| 74 content::RenderView* GetRenderView() const; | 78 content::RenderView* GetRenderView() const; |
| 75 | 79 |
| 76 // Fires the onload and onunload events on the chromeHidden object. | 80 // Fires the onload and onunload events on the chromeHidden object. |
| 77 // TODO(aa): Move this to EventBindings. | 81 // TODO(aa): Move this to EventBindings. |
| 78 void DispatchOnLoadEvent(bool is_extension_process, | 82 void DispatchOnLoadEvent(bool is_extension_process, |
| 79 bool is_incognito_process, | 83 bool is_incognito_process, |
| 80 int manifest_version) const; | 84 int manifest_version); |
| 81 void DispatchOnUnloadEvent() const; | 85 void DispatchOnUnloadEvent(); |
| 82 | 86 |
| 83 // Call the named method of the chromeHidden object in this context. | 87 // Call the named method of the chromeHidden object in this context. |
| 84 // The function can be a sub-property like "Port.dispatchOnMessage". Returns | 88 // The function can be a sub-property like "Port.dispatchOnMessage". Returns |
| 85 // the result of the function call in |result| if |result| is non-NULL. If the | 89 // the result of the function call in |result| if |result| is non-NULL. If the |
| 86 // named method does not exist, returns false. | 90 // named method does not exist, returns false. |
| 87 bool CallChromeHiddenMethod( | 91 bool CallChromeHiddenMethod( |
| 88 const std::string& function_name, | 92 const std::string& function_name, |
| 89 int argc, | 93 int argc, |
| 90 v8::Handle<v8::Value>* argv, | 94 v8::Handle<v8::Value>* argv, |
| 91 v8::Handle<v8::Value>* result) const; | 95 v8::Handle<v8::Value>* result) const; |
| 92 | 96 |
| 97 // Returns the set of extension APIs that are available to this context. If no |
| 98 // APIs are available, returns an empty set. |
| 99 const std::set<std::string>& GetAvailableExtensionAPIs(); |
| 100 |
| 93 private: | 101 private: |
| 94 // The v8 context the bindings are accessible to. We keep a strong reference | 102 // The v8 context the bindings are accessible to. We keep a strong reference |
| 95 // to it for simplicity. In the case of content scripts, this is necessary | 103 // to it for simplicity. In the case of content scripts, this is necessary |
| 96 // because we want all scripts from the same extension for the same frame to | 104 // because we want all scripts from the same extension for the same frame to |
| 97 // run in the same context, so we can't have the contexts being GC'd if | 105 // run in the same context, so we can't have the contexts being GC'd if |
| 98 // nothing is happening. In the case of page contexts, this isn't necessary | 106 // nothing is happening. In the case of page contexts, this isn't necessary |
| 99 // since the DOM keeps the context alive, but it makes things simpler to not | 107 // since the DOM keeps the context alive, but it makes things simpler to not |
| 100 // distinguish the two cases. | 108 // distinguish the two cases. |
| 101 v8::Persistent<v8::Context> v8_context_; | 109 v8::Persistent<v8::Context> v8_context_; |
| 102 | 110 |
| 103 // The WebFrame associated with this context. This can be NULL because this | 111 // The WebFrame associated with this context. This can be NULL because this |
| 104 // object can outlive is destroyed asynchronously. | 112 // object can outlive is destroyed asynchronously. |
| 105 WebKit::WebFrame* web_frame_; | 113 WebKit::WebFrame* web_frame_; |
| 106 | 114 |
| 107 // The extension ID this context is associated with. | 115 // The extension associated with this context, or NULL if there is none. This |
| 108 std::string extension_id_; | 116 // might be a hosted app in the case that this context is hosting a web URL. |
| 117 const Extension* extension_; |
| 109 | 118 |
| 110 // The type of context. | 119 // The type of context. |
| 111 extensions::Feature::Context context_type_; | 120 extensions::Feature::Context context_type_; |
| 112 | 121 |
| 113 // Owns and structures the JS that is injected to set up extension bindings. | 122 // Owns and structures the JS that is injected to set up extension bindings. |
| 114 scoped_ptr<ModuleSystem> module_system_; | 123 scoped_ptr<ModuleSystem> module_system_; |
| 115 | 124 |
| 125 // The extension APIs available to this context. |
| 126 scoped_ptr<std::set<std::string> > available_extension_apis_; |
| 127 |
| 116 DISALLOW_COPY_AND_ASSIGN(ChromeV8Context); | 128 DISALLOW_COPY_AND_ASSIGN(ChromeV8Context); |
| 117 }; | 129 }; |
| 118 | 130 |
| 119 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_ | 131 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_ |
| OLD | NEW |