| 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 std::string GetExtensionID(); |
| 61 |
| 60 // Returns a special Chrome-specific hidden object that is associated with a | 62 // 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 | 63 // 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 | 64 // used by our v8::Extension implementations as a way to share code and as a |
| 63 // bridge between C++ and JavaScript. | 65 // bridge between C++ and JavaScript. |
| 64 static v8::Handle<v8::Value> GetOrCreateChromeHidden( | 66 static v8::Handle<v8::Value> GetOrCreateChromeHidden( |
| 65 v8::Handle<v8::Context> context); | 67 v8::Handle<v8::Context> context); |
| 66 | 68 |
| 67 // Return the chromeHidden object associated with this context, or an empty | 69 // Return the chromeHidden object associated with this context, or an empty |
| 68 // handle if no chrome hidden has been created (by GetOrCreateChromeHidden) | 70 // handle if no chrome hidden has been created (by GetOrCreateChromeHidden) |
| 69 // yet for this context. | 71 // yet for this context. |
| 70 v8::Handle<v8::Value> GetChromeHidden() const; | 72 v8::Handle<v8::Value> GetChromeHidden() const; |
| 71 | 73 |
| 72 // Returns the RenderView associated with this context. Can return NULL if the | 74 // Returns the RenderView associated with this context. Can return NULL if the |
| 73 // context is in the process of being destroyed. | 75 // context is in the process of being destroyed. |
| 74 content::RenderView* GetRenderView() const; | 76 content::RenderView* GetRenderView() const; |
| 75 | 77 |
| 76 // Fires the onload and onunload events on the chromeHidden object. | 78 // Fires the onload and onunload events on the chromeHidden object. |
| 77 // TODO(aa): Move this to EventBindings. | 79 // TODO(aa): Move this to EventBindings. |
| 78 void DispatchOnLoadEvent(bool is_extension_process, | 80 void DispatchOnLoadEvent(bool is_extension_process, |
| 79 bool is_incognito_process, | 81 bool is_incognito_process, |
| 80 int manifest_version) const; | 82 int manifest_version); |
| 81 void DispatchOnUnloadEvent() const; | 83 void DispatchOnUnloadEvent(); |
| 82 | 84 |
| 83 // Call the named method of the chromeHidden object in this context. | 85 // Call the named method of the chromeHidden object in this context. |
| 84 // The function can be a sub-property like "Port.dispatchOnMessage". Returns | 86 // 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 | 87 // the result of the function call in |result| if |result| is non-NULL. If the |
| 86 // named method does not exist, returns false. | 88 // named method does not exist, returns false. |
| 87 bool CallChromeHiddenMethod( | 89 bool CallChromeHiddenMethod( |
| 88 const std::string& function_name, | 90 const std::string& function_name, |
| 89 int argc, | 91 int argc, |
| 90 v8::Handle<v8::Value>* argv, | 92 v8::Handle<v8::Value>* argv, |
| 91 v8::Handle<v8::Value>* result) const; | 93 v8::Handle<v8::Value>* result) const; |
| 92 | 94 |
| 95 std::set<std::string>* GetAvailableExtensionAPIs(); |
| 96 |
| 93 private: | 97 private: |
| 94 // The v8 context the bindings are accessible to. We keep a strong reference | 98 // 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 | 99 // 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 | 100 // 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 | 101 // 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 | 102 // 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 | 103 // since the DOM keeps the context alive, but it makes things simpler to not |
| 100 // distinguish the two cases. | 104 // distinguish the two cases. |
| 101 v8::Persistent<v8::Context> v8_context_; | 105 v8::Persistent<v8::Context> v8_context_; |
| 102 | 106 |
| 103 // The WebFrame associated with this context. This can be NULL because this | 107 // The WebFrame associated with this context. This can be NULL because this |
| 104 // object can outlive is destroyed asynchronously. | 108 // object can outlive is destroyed asynchronously. |
| 105 WebKit::WebFrame* web_frame_; | 109 WebKit::WebFrame* web_frame_; |
| 106 | 110 |
| 107 // The extension ID this context is associated with. | 111 const Extension* extension_; |
| 108 std::string extension_id_; | |
| 109 | 112 |
| 110 // The type of context. | 113 // The type of context. |
| 111 extensions::Feature::Context context_type_; | 114 extensions::Feature::Context context_type_; |
| 112 | 115 |
| 113 // Owns and structures the JS that is injected to set up extension bindings. | 116 // Owns and structures the JS that is injected to set up extension bindings. |
| 114 scoped_ptr<ModuleSystem> module_system_; | 117 scoped_ptr<ModuleSystem> module_system_; |
| 115 | 118 |
| 119 scoped_ptr<std::set<std::string> > available_extension_apis_; |
| 120 |
| 116 DISALLOW_COPY_AND_ASSIGN(ChromeV8Context); | 121 DISALLOW_COPY_AND_ASSIGN(ChromeV8Context); |
| 117 }; | 122 }; |
| 118 | 123 |
| 119 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_ | 124 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_CONTEXT_H_ |
| OLD | NEW |