1 // Copyright (c) 2013 Marshall A. Greenblatt. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
13 // * Neither the name of Google Inc. nor the name Chromium Embedded
14 // Framework nor the names of its contributors may be used to endorse
15 // or promote products derived from this software without specific prior
16 // written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // ---------------------------------------------------------------------------
32 // The contents of this file must follow a specific format in order to
33 // support the CEF translator tool. See the translator.README.txt file in the
34 // tools directory for more information.
38 #ifndef CEF_INCLUDE_CEF_V8_H_
39 #define CEF_INCLUDE_CEF_V8_H_
42 #include "include/cef_base.h"
43 #include "include/cef_browser.h"
44 #include "include/cef_frame.h"
45 #include "include/cef_task.h"
50 class CefV8StackFrame;
55 // Register a new V8 extension with the specified JavaScript extension code and
56 // handler. Functions implemented by the handler are prototyped using the
57 // keyword 'native'. The calling of a native function is restricted to the scope
58 // in which the prototype of the native function is defined. This function may
59 // only be called on the render process main thread.
61 // Example JavaScript extension code:
63 // // create the 'example' global object if it doesn't already exist.
66 // // create the 'example.test' global object if it doesn't already exist.
70 // // Define the function 'example.test.myfunction'.
71 // example.test.myfunction = function() {
72 // // Call CefV8Handler::Execute() with the function name 'MyFunction'
73 // // and no arguments.
74 // native function MyFunction();
75 // return MyFunction();
77 // // Define the getter function for parameter 'example.test.myparam'.
78 // example.test.__defineGetter__('myparam', function() {
79 // // Call CefV8Handler::Execute() with the function name 'GetMyParam'
80 // // and no arguments.
81 // native function GetMyParam();
82 // return GetMyParam();
84 // // Define the setter function for parameter 'example.test.myparam'.
85 // example.test.__defineSetter__('myparam', function(b) {
86 // // Call CefV8Handler::Execute() with the function name 'SetMyParam'
87 // // and a single argument.
88 // native function SetMyParam();
89 // if(b) SetMyParam(b);
92 // // Extension definitions can also contain normal JavaScript variables
95 // example.test.increment = function() {
101 // Example usage in the page:
103 // // Call the function.
104 // example.test.myfunction();
105 // // Set the parameter.
106 // example.test.myparam = value;
107 // // Get the parameter.
108 // value = example.test.myparam;
109 // // Call another function.
110 // example.test.increment();
113 /*--cef(optional_param=handler)--*/
114 bool CefRegisterExtension(const CefString& extension_name,
115 const CefString& javascript_code,
116 CefRefPtr<CefV8Handler> handler);
120 // Class representing a V8 context handle. V8 handles can only be accessed from
121 // the thread on which they are created. Valid threads for creating a V8 handle
122 // include the render process main thread (TID_RENDERER) and WebWorker threads.
123 // A task runner for posting tasks on the associated thread can be retrieved via
124 // the CefV8Context::GetTaskRunner() method.
126 /*--cef(source=library)--*/
127 class CefV8Context : public virtual CefBase {
130 // Returns the current (top) context object in the V8 context stack.
133 static CefRefPtr<CefV8Context> GetCurrentContext();
136 // Returns the entered (bottom) context object in the V8 context stack.
139 static CefRefPtr<CefV8Context> GetEnteredContext();
142 // Returns true if V8 is currently inside a context.
145 static bool InContext();
148 // Returns the task runner associated with this context. V8 handles can only
149 // be accessed from the thread on which they are created. This method can be
150 // called on any render process thread.
153 virtual CefRefPtr<CefTaskRunner> GetTaskRunner() =0;
156 // Returns true if the underlying handle is valid and it can be accessed on
157 // the current thread. Do not call any other methods if this method returns
161 virtual bool IsValid() =0;
164 // Returns the browser for this context. This method will return an empty
165 // reference for WebWorker contexts.
168 virtual CefRefPtr<CefBrowser> GetBrowser() =0;
171 // Returns the frame for this context. This method will return an empty
172 // reference for WebWorker contexts.
175 virtual CefRefPtr<CefFrame> GetFrame() =0;
178 // Returns the global object for this context. The context must be entered
179 // before calling this method.
182 virtual CefRefPtr<CefV8Value> GetGlobal() =0;
185 // Enter this context. A context must be explicitly entered before creating a
186 // V8 Object, Array, Function or Date asynchronously. Exit() must be called
187 // the same number of times as Enter() before releasing this context. V8
188 // objects belong to the context in which they are created. Returns true if
189 // the scope was entered successfully.
192 virtual bool Enter() =0;
195 // Exit this context. Call this method only after calling Enter(). Returns
196 // true if the scope was exited successfully.
199 virtual bool Exit() =0;
202 // Returns true if this object is pointing to the same handle as |that|
206 virtual bool IsSame(CefRefPtr<CefV8Context> that) =0;
209 // Evaluates the specified JavaScript code using this context's global object.
210 // On success |retval| will be set to the return value, if any, and the
211 // function will return true. On failure |exception| will be set to the
212 // exception, if any, and the function will return false.
215 virtual bool Eval(const CefString& code,
216 CefRefPtr<CefV8Value>& retval,
217 CefRefPtr<CefV8Exception>& exception) =0;
221 typedef std::vector<CefRefPtr<CefV8Value> > CefV8ValueList;
224 // Interface that should be implemented to handle V8 function calls. The methods
225 // of this class will be called on the thread associated with the V8 function.
227 /*--cef(source=client)--*/
228 class CefV8Handler : public virtual CefBase {
231 // Handle execution of the function identified by |name|. |object| is the
232 // receiver ('this' object) of the function. |arguments| is the list of
233 // arguments passed to the function. If execution succeeds set |retval| to the
234 // function return value. If execution fails set |exception| to the exception
235 // that will be thrown. Return true if execution was handled.
238 virtual bool Execute(const CefString& name,
239 CefRefPtr<CefV8Value> object,
240 const CefV8ValueList& arguments,
241 CefRefPtr<CefV8Value>& retval,
242 CefString& exception) =0;
246 // Interface that should be implemented to handle V8 accessor calls. Accessor
247 // identifiers are registered by calling CefV8Value::SetValue(). The methods
248 // of this class will be called on the thread associated with the V8 accessor.
250 /*--cef(source=client)--*/
251 class CefV8Accessor : public virtual CefBase {
254 // Handle retrieval the accessor value identified by |name|. |object| is the
255 // receiver ('this' object) of the accessor. If retrieval succeeds set
256 // |retval| to the return value. If retrieval fails set |exception| to the
257 // exception that will be thrown. Return true if accessor retrieval was
261 virtual bool Get(const CefString& name,
262 const CefRefPtr<CefV8Value> object,
263 CefRefPtr<CefV8Value>& retval,
264 CefString& exception) =0;
267 // Handle assignment of the accessor value identified by |name|. |object| is
268 // the receiver ('this' object) of the accessor. |value| is the new value
269 // being assigned to the accessor. If assignment fails set |exception| to the
270 // exception that will be thrown. Return true if accessor assignment was
274 virtual bool Set(const CefString& name,
275 const CefRefPtr<CefV8Value> object,
276 const CefRefPtr<CefV8Value> value,
277 CefString& exception) =0;
281 // Class representing a V8 exception. The methods of this class may be called on
282 // any render process thread.
284 /*--cef(source=library)--*/
285 class CefV8Exception : public virtual CefBase {
288 // Returns the exception message.
291 virtual CefString GetMessage() =0;
294 // Returns the line of source code that the exception occurred within.
297 virtual CefString GetSourceLine() =0;
300 // Returns the resource name for the script from where the function causing
301 // the error originates.
304 virtual CefString GetScriptResourceName() =0;
307 // Returns the 1-based number of the line where the error occurred or 0 if the
308 // line number is unknown.
311 virtual int GetLineNumber() =0;
314 // Returns the index within the script of the first character where the error
318 virtual int GetStartPosition() =0;
321 // Returns the index within the script of the last character where the error
325 virtual int GetEndPosition() =0;
328 // Returns the index within the line of the first character where the error
332 virtual int GetStartColumn() =0;
335 // Returns the index within the line of the last character where the error
339 virtual int GetEndColumn() =0;
343 // Class representing a V8 value handle. V8 handles can only be accessed from
344 // the thread on which they are created. Valid threads for creating a V8 handle
345 // include the render process main thread (TID_RENDERER) and WebWorker threads.
346 // A task runner for posting tasks on the associated thread can be retrieved via
347 // the CefV8Context::GetTaskRunner() method.
349 /*--cef(source=library)--*/
350 class CefV8Value : public virtual CefBase {
352 typedef cef_v8_accesscontrol_t AccessControl;
353 typedef cef_v8_propertyattribute_t PropertyAttribute;
356 // Create a new CefV8Value object of type undefined.
359 static CefRefPtr<CefV8Value> CreateUndefined();
362 // Create a new CefV8Value object of type null.
365 static CefRefPtr<CefV8Value> CreateNull();
368 // Create a new CefV8Value object of type bool.
371 static CefRefPtr<CefV8Value> CreateBool(bool value);
374 // Create a new CefV8Value object of type int.
377 static CefRefPtr<CefV8Value> CreateInt(int32 value);
380 // Create a new CefV8Value object of type unsigned int.
383 static CefRefPtr<CefV8Value> CreateUInt(uint32 value);
386 // Create a new CefV8Value object of type double.
389 static CefRefPtr<CefV8Value> CreateDouble(double value);
392 // Create a new CefV8Value object of type Date. This method should only be
393 // called from within the scope of a CefRenderProcessHandler, CefV8Handler or
394 // CefV8Accessor callback, or in combination with calling Enter() and Exit()
395 // on a stored CefV8Context reference.
398 static CefRefPtr<CefV8Value> CreateDate(const CefTime& date);
401 // Create a new CefV8Value object of type string.
403 /*--cef(optional_param=value)--*/
404 static CefRefPtr<CefV8Value> CreateString(const CefString& value);
407 // Create a new CefV8Value object of type object with optional accessor. This
408 // method should only be called from within the scope of a
409 // CefRenderProcessHandler, CefV8Handler or CefV8Accessor callback, or in
410 // combination with calling Enter() and Exit() on a stored CefV8Context
413 /*--cef(optional_param=accessor)--*/
414 static CefRefPtr<CefV8Value> CreateObject(CefRefPtr<CefV8Accessor> accessor);
417 // Create a new CefV8Value object of type array with the specified |length|.
418 // If |length| is negative the returned array will have length 0. This method
419 // should only be called from within the scope of a CefRenderProcessHandler,
420 // CefV8Handler or CefV8Accessor callback, or in combination with calling
421 // Enter() and Exit() on a stored CefV8Context reference.
424 static CefRefPtr<CefV8Value> CreateArray(int length);
427 // Create a new CefV8Value object of type function. This method should only be
428 // called from within the scope of a CefRenderProcessHandler, CefV8Handler or
429 // CefV8Accessor callback, or in combination with calling Enter() and Exit()
430 // on a stored CefV8Context reference.
433 static CefRefPtr<CefV8Value> CreateFunction(const CefString& name,
434 CefRefPtr<CefV8Handler> handler);
437 // Returns true if the underlying handle is valid and it can be accessed on
438 // the current thread. Do not call any other methods if this method returns
442 virtual bool IsValid() =0;
445 // True if the value type is undefined.
448 virtual bool IsUndefined() =0;
451 // True if the value type is null.
454 virtual bool IsNull() =0;
457 // True if the value type is bool.
460 virtual bool IsBool() =0;
463 // True if the value type is int.
466 virtual bool IsInt() =0;
469 // True if the value type is unsigned int.
472 virtual bool IsUInt() =0;
475 // True if the value type is double.
478 virtual bool IsDouble() =0;
481 // True if the value type is Date.
484 virtual bool IsDate() =0;
487 // True if the value type is string.
490 virtual bool IsString() =0;
493 // True if the value type is object.
496 virtual bool IsObject() =0;
499 // True if the value type is array.
502 virtual bool IsArray() =0;
505 // True if the value type is function.
508 virtual bool IsFunction() =0;
511 // Returns true if this object is pointing to the same handle as |that|
515 virtual bool IsSame(CefRefPtr<CefV8Value> that) =0;
518 // Return a bool value. The underlying data will be converted to if
522 virtual bool GetBoolValue() =0;
525 // Return an int value. The underlying data will be converted to if
529 virtual int32 GetIntValue() =0;
532 // Return an unisgned int value. The underlying data will be converted to if
536 virtual uint32 GetUIntValue() =0;
539 // Return a double value. The underlying data will be converted to if
543 virtual double GetDoubleValue() =0;
546 // Return a Date value. The underlying data will be converted to if
550 virtual CefTime GetDateValue() =0;
553 // Return a string value. The underlying data will be converted to if
557 virtual CefString GetStringValue() =0;
560 // OBJECT METHODS - These methods are only available on objects. Arrays and
561 // functions are also objects. String- and integer-based keys can be used
562 // interchangably with the framework converting between them as necessary.
565 // Returns true if this is a user created object.
568 virtual bool IsUserCreated() =0;
571 // Returns true if the last method call resulted in an exception. This
572 // attribute exists only in the scope of the current CEF value object.
575 virtual bool HasException() =0;
578 // Returns the exception resulting from the last method call. This attribute
579 // exists only in the scope of the current CEF value object.
582 virtual CefRefPtr<CefV8Exception> GetException() =0;
585 // Clears the last exception and returns true on success.
588 virtual bool ClearException() =0;
591 // Returns true if this object will re-throw future exceptions. This attribute
592 // exists only in the scope of the current CEF value object.
595 virtual bool WillRethrowExceptions() =0;
598 // Set whether this object will re-throw future exceptions. By default
599 // exceptions are not re-thrown. If a exception is re-thrown the current
600 // context should not be accessed again until after the exception has been
601 // caught and not re-thrown. Returns true on success. This attribute exists
602 // only in the scope of the current CEF value object.
605 virtual bool SetRethrowExceptions(bool rethrow) =0;
608 // Returns true if the object has a value with the specified identifier.
610 /*--cef(capi_name=has_value_bykey,optional_param=key)--*/
611 virtual bool HasValue(const CefString& key) =0;
614 // Returns true if the object has a value with the specified identifier.
616 /*--cef(capi_name=has_value_byindex,index_param=index)--*/
617 virtual bool HasValue(int index) =0;
620 // Deletes the value with the specified identifier and returns true on
621 // success. Returns false if this method is called incorrectly or an exception
622 // is thrown. For read-only and don't-delete values this method will return
623 // true even though deletion failed.
625 /*--cef(capi_name=delete_value_bykey,optional_param=key)--*/
626 virtual bool DeleteValue(const CefString& key) =0;
629 // Deletes the value with the specified identifier and returns true on
630 // success. Returns false if this method is called incorrectly, deletion fails
631 // or an exception is thrown. For read-only and don't-delete values this
632 // method will return true even though deletion failed.
634 /*--cef(capi_name=delete_value_byindex,index_param=index)--*/
635 virtual bool DeleteValue(int index) =0;
638 // Returns the value with the specified identifier on success. Returns NULL
639 // if this method is called incorrectly or an exception is thrown.
641 /*--cef(capi_name=get_value_bykey,optional_param=key)--*/
642 virtual CefRefPtr<CefV8Value> GetValue(const CefString& key) =0;
645 // Returns the value with the specified identifier on success. Returns NULL
646 // if this method is called incorrectly or an exception is thrown.
648 /*--cef(capi_name=get_value_byindex,index_param=index)--*/
649 virtual CefRefPtr<CefV8Value> GetValue(int index) =0;
652 // Associates a value with the specified identifier and returns true on
653 // success. Returns false if this method is called incorrectly or an exception
654 // is thrown. For read-only values this method will return true even though
655 // assignment failed.
657 /*--cef(capi_name=set_value_bykey,optional_param=key)--*/
658 virtual bool SetValue(const CefString& key, CefRefPtr<CefV8Value> value,
659 PropertyAttribute attribute) =0;
662 // Associates a value with the specified identifier and returns true on
663 // success. Returns false if this method is called incorrectly or an exception
664 // is thrown. For read-only values this method will return true even though
665 // assignment failed.
667 /*--cef(capi_name=set_value_byindex,index_param=index)--*/
668 virtual bool SetValue(int index, CefRefPtr<CefV8Value> value) =0;
671 // Registers an identifier and returns true on success. Access to the
672 // identifier will be forwarded to the CefV8Accessor instance passed to
673 // CefV8Value::CreateObject(). Returns false if this method is called
674 // incorrectly or an exception is thrown. For read-only values this method
675 // will return true even though assignment failed.
677 /*--cef(capi_name=set_value_byaccessor,optional_param=key)--*/
678 virtual bool SetValue(const CefString& key, AccessControl settings,
679 PropertyAttribute attribute) =0;
682 // Read the keys for the object's values into the specified vector. Integer-
683 // based keys will also be returned as strings.
686 virtual bool GetKeys(std::vector<CefString>& keys) =0;
689 // Sets the user data for this object and returns true on success. Returns
690 // false if this method is called incorrectly. This method can only be called
691 // on user created objects.
693 /*--cef(optional_param=user_data)--*/
694 virtual bool SetUserData(CefRefPtr<CefBase> user_data) =0;
697 // Returns the user data, if any, assigned to this object.
700 virtual CefRefPtr<CefBase> GetUserData() =0;
703 // Returns the amount of externally allocated memory registered for the
707 virtual int GetExternallyAllocatedMemory() =0;
710 // Adjusts the amount of registered external memory for the object. Used to
711 // give V8 an indication of the amount of externally allocated memory that is
712 // kept alive by JavaScript objects. V8 uses this information to decide when
713 // to perform global garbage collection. Each CefV8Value tracks the amount of
714 // external memory associated with it and automatically decreases the global
715 // total by the appropriate amount on its destruction. |change_in_bytes|
716 // specifies the number of bytes to adjust by. This method returns the number
717 // of bytes associated with the object after the adjustment. This method can
718 // only be called on user created objects.
721 virtual int AdjustExternallyAllocatedMemory(int change_in_bytes) =0;
724 // ARRAY METHODS - These methods are only available on arrays.
727 // Returns the number of elements in the array.
730 virtual int GetArrayLength() =0;
733 // FUNCTION METHODS - These methods are only available on functions.
736 // Returns the function name.
739 virtual CefString GetFunctionName() =0;
742 // Returns the function handler or NULL if not a CEF-created function.
745 virtual CefRefPtr<CefV8Handler> GetFunctionHandler() =0;
748 // Execute the function using the current V8 context. This method should only
749 // be called from within the scope of a CefV8Handler or CefV8Accessor
750 // callback, or in combination with calling Enter() and Exit() on a stored
751 // CefV8Context reference. |object| is the receiver ('this' object) of the
752 // function. If |object| is empty the current context's global object will be
753 // used. |arguments| is the list of arguments that will be passed to the
754 // function. Returns the function return value on success. Returns NULL if
755 // this method is called incorrectly or an exception is thrown.
757 /*--cef(optional_param=object)--*/
758 virtual CefRefPtr<CefV8Value> ExecuteFunction(
759 CefRefPtr<CefV8Value> object,
760 const CefV8ValueList& arguments) =0;
763 // Execute the function using the specified V8 context. |object| is the
764 // receiver ('this' object) of the function. If |object| is empty the
765 // specified context's global object will be used. |arguments| is the list of
766 // arguments that will be passed to the function. Returns the function return
767 // value on success. Returns NULL if this method is called incorrectly or an
768 // exception is thrown.
770 /*--cef(optional_param=object)--*/
771 virtual CefRefPtr<CefV8Value> ExecuteFunctionWithContext(
772 CefRefPtr<CefV8Context> context,
773 CefRefPtr<CefV8Value> object,
774 const CefV8ValueList& arguments) =0;
778 // Class representing a V8 stack trace handle. V8 handles can only be accessed
779 // from the thread on which they are created. Valid threads for creating a V8
780 // handle include the render process main thread (TID_RENDERER) and WebWorker
781 // threads. A task runner for posting tasks on the associated thread can be
782 // retrieved via the CefV8Context::GetTaskRunner() method.
784 /*--cef(source=library)--*/
785 class CefV8StackTrace : public virtual CefBase {
788 // Returns the stack trace for the currently active context. |frame_limit| is
789 // the maximum number of frames that will be captured.
792 static CefRefPtr<CefV8StackTrace> GetCurrent(int frame_limit);
795 // Returns true if the underlying handle is valid and it can be accessed on
796 // the current thread. Do not call any other methods if this method returns
800 virtual bool IsValid() =0;
803 // Returns the number of stack frames.
806 virtual int GetFrameCount() =0;
809 // Returns the stack frame at the specified 0-based index.
812 virtual CefRefPtr<CefV8StackFrame> GetFrame(int index) =0;
816 // Class representing a V8 stack frame handle. V8 handles can only be accessed
817 // from the thread on which they are created. Valid threads for creating a V8
818 // handle include the render process main thread (TID_RENDERER) and WebWorker
819 // threads. A task runner for posting tasks on the associated thread can be
820 // retrieved via the CefV8Context::GetTaskRunner() method.
822 /*--cef(source=library)--*/
823 class CefV8StackFrame : public virtual CefBase {
826 // Returns true if the underlying handle is valid and it can be accessed on
827 // the current thread. Do not call any other methods if this method returns
831 virtual bool IsValid() =0;
834 // Returns the name of the resource script that contains the function.
837 virtual CefString GetScriptName() =0;
840 // Returns the name of the resource script that contains the function or the
841 // sourceURL value if the script name is undefined and its source ends with
842 // a "//@ sourceURL=..." string.
845 virtual CefString GetScriptNameOrSourceURL() =0;
848 // Returns the name of the function.
851 virtual CefString GetFunctionName() =0;
854 // Returns the 1-based line number for the function call or 0 if unknown.
857 virtual int GetLineNumber() =0;
860 // Returns the 1-based column offset on the line for the function call or 0 if
864 virtual int GetColumn() =0;
867 // Returns true if the function was compiled using eval().
870 virtual bool IsEval() =0;
873 // Returns true if the function was called as a constructor via "new".
876 virtual bool IsConstructor() =0;
879 #endif // CEF_INCLUDE_CEF_V8_H_