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 #ifndef CEF_INCLUDE_INTERNAL_CEF_TYPES_WRAPPERS_H_
31 #define CEF_INCLUDE_INTERNAL_CEF_TYPES_WRAPPERS_H_
34 #include "include/internal/cef_string.h"
35 #include "include/internal/cef_string_list.h"
36 #include "include/internal/cef_types.h"
39 // Template class that provides common functionality for CEF structure wrapping.
41 template <class traits>
42 class CefStructBase : public traits::struct_type {
44 typedef typename traits::struct_type struct_type;
46 CefStructBase() : attached_to_(NULL) {
49 virtual ~CefStructBase() {
50 // Only clear this object's data if it isn't currently attached to a
56 CefStructBase(const CefStructBase& r) {
60 CefStructBase(const struct_type& r) { // NOLINT(runtime/explicit)
66 // Clear this object's values.
74 // Attach to the source structure's existing values. DetachTo() can be called
75 // to insert the values back into the existing structure.
77 void AttachTo(struct_type& source) {
78 // Only clear this object's data if it isn't currently attached to a
83 // This object is now attached to the new structure.
84 attached_to_ = &source;
86 // Transfer ownership of the values from the source structure.
87 memcpy(static_cast<struct_type*>(this), &source, sizeof(struct_type));
91 // Relinquish ownership of values to the target structure.
93 void DetachTo(struct_type& target) {
94 if (attached_to_ != &target) {
95 // Clear the target structure's values only if we are not currently
96 // attached to that structure.
100 // Transfer ownership of the values to the target structure.
101 memcpy(&target, static_cast<struct_type*>(this), sizeof(struct_type));
103 // Remove the references from this object.
108 // Set this object's values. If |copy| is true the source structure's values
109 // will be copied instead of referenced.
111 void Set(const struct_type& source, bool copy) {
112 traits::set(&source, this, copy);
115 CefStructBase& operator=(const CefStructBase& s) {
116 return operator=(static_cast<const struct_type&>(s));
119 CefStructBase& operator=(const struct_type& s) {
126 memset(static_cast<struct_type*>(this), 0, sizeof(struct_type));
131 static void Clear(struct_type* s) { traits::clear(s); }
133 struct_type* attached_to_;
137 struct CefPointTraits {
138 typedef cef_point_t struct_type;
140 static inline void init(struct_type* s) {}
141 static inline void clear(struct_type* s) {}
143 static inline void set(const struct_type* src, struct_type* target,
150 // Class representing a point.
152 class CefPoint : public CefStructBase<CefPointTraits> {
154 typedef CefStructBase<CefPointTraits> parent;
156 CefPoint() : parent() {}
157 CefPoint(const cef_point_t& r) : parent(r) {} // NOLINT(runtime/explicit)
158 CefPoint(const CefPoint& r) : parent(r) {} // NOLINT(runtime/explicit)
159 CefPoint(int x, int y) : parent() {
163 bool IsEmpty() const { return x <= 0 && y <= 0; }
164 void Set(int x, int y) {
165 this->x = x, this->y = y;
169 inline bool operator==(const CefPoint& a, const CefPoint& b) {
170 return a.x == b.x && a.y == b.y;
173 inline bool operator!=(const CefPoint& a, const CefPoint& b) {
178 struct CefRectTraits {
179 typedef cef_rect_t struct_type;
181 static inline void init(struct_type* s) {}
182 static inline void clear(struct_type* s) {}
184 static inline void set(const struct_type* src, struct_type* target,
191 // Class representing a rectangle.
193 class CefRect : public CefStructBase<CefRectTraits> {
195 typedef CefStructBase<CefRectTraits> parent;
197 CefRect() : parent() {}
198 CefRect(const cef_rect_t& r) : parent(r) {} // NOLINT(runtime/explicit)
199 CefRect(const CefRect& r) : parent(r) {} // NOLINT(runtime/explicit)
200 CefRect(int x, int y, int width, int height) : parent() {
201 Set(x, y, width, height);
204 bool IsEmpty() const { return width <= 0 || height <= 0; }
205 void Set(int x, int y, int width, int height) {
206 this->x = x, this->y = y, this->width = width, this->height = height;
210 inline bool operator==(const CefRect& a, const CefRect& b) {
211 return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height;
214 inline bool operator!=(const CefRect& a, const CefRect& b) {
219 struct CefSizeTraits {
220 typedef cef_size_t struct_type;
222 static inline void init(struct_type* s) {}
223 static inline void clear(struct_type* s) {}
225 static inline void set(const struct_type* src, struct_type* target,
232 // Class representing a size.
234 class CefSize : public CefStructBase<CefSizeTraits> {
236 typedef CefStructBase<CefSizeTraits> parent;
238 CefSize() : parent() {}
239 CefSize(const cef_size_t& r) : parent(r) {} // NOLINT(runtime/explicit)
240 CefSize(const CefSize& r) : parent(r) {} // NOLINT(runtime/explicit)
241 CefSize(int width, int height) : parent() {
245 bool IsEmpty() const { return width <= 0 || height <= 0; }
246 void Set(int width, int height) {
247 this->width = width, this->height = height;
251 inline bool operator==(const CefSize& a, const CefSize& b) {
252 return a.width == b.width && a.height == b.height;
255 inline bool operator!=(const CefSize& a, const CefSize& b) {
260 struct CefScreenInfoTraits {
261 typedef cef_screen_info_t struct_type;
263 static inline void init(struct_type* s) {}
265 static inline void clear(struct_type* s) {}
267 static inline void set(const struct_type* src, struct_type* target,
269 target->device_scale_factor = src->device_scale_factor;
270 target->depth = src->depth;
271 target->depth_per_component = src->depth_per_component;
272 target->is_monochrome = src->is_monochrome;
273 target->rect = src->rect;
274 target->available_rect = src->available_rect;
279 // Class representing the virtual screen information for use when window
280 // rendering is disabled.
282 class CefScreenInfo : public CefStructBase<CefScreenInfoTraits> {
284 typedef CefStructBase<CefScreenInfoTraits> parent;
286 CefScreenInfo() : parent() {}
287 CefScreenInfo(const cef_screen_info_t& r) : parent(r) {} // NOLINT(runtime/explicit)
288 CefScreenInfo(const CefScreenInfo& r) : parent(r) {} // NOLINT(runtime/explicit)
289 CefScreenInfo(float device_scale_factor,
291 int depth_per_component,
294 const CefRect& available_rect) : parent() {
295 Set(device_scale_factor, depth, depth_per_component,
296 is_monochrome, rect, available_rect);
299 void Set(float device_scale_factor,
301 int depth_per_component,
304 const CefRect& available_rect) {
305 this->device_scale_factor = device_scale_factor;
307 this->depth_per_component = depth_per_component;
308 this->is_monochrome = is_monochrome;
310 this->available_rect = available_rect;
315 struct CefKeyEventTraits {
316 typedef cef_key_event_t struct_type;
318 static inline void init(struct_type* s) {}
320 static inline void clear(struct_type* s) {}
322 static inline void set(const struct_type* src, struct_type* target,
324 target->type = src->type;
325 target->modifiers = src->modifiers;
326 target->windows_key_code = src->windows_key_code;
327 target->native_key_code = src->native_key_code;
328 target->is_system_key = src->is_system_key;
329 target->character = src->character;
330 target->unmodified_character = src->unmodified_character;
331 target->focus_on_editable_field = src->focus_on_editable_field;
336 // Class representing a a keyboard event.
338 typedef CefStructBase<CefKeyEventTraits> CefKeyEvent;
341 struct CefMouseEventTraits {
342 typedef cef_mouse_event_t struct_type;
344 static inline void init(struct_type* s) {}
346 static inline void clear(struct_type* s) {}
348 static inline void set(const struct_type* src, struct_type* target,
352 target->modifiers = src->modifiers;
357 // Class representing a mouse event.
359 typedef CefStructBase<CefMouseEventTraits> CefMouseEvent;
362 struct CefPopupFeaturesTraits {
363 typedef cef_popup_features_t struct_type;
365 static inline void init(struct_type* s) {
366 s->menuBarVisible = true;
367 s->statusBarVisible = true;
368 s->toolBarVisible = true;
369 s->locationBarVisible = true;
370 s->scrollbarsVisible = true;
374 static inline void clear(struct_type* s) {
375 if (s->additionalFeatures)
376 cef_string_list_free(s->additionalFeatures);
379 static inline void set(const struct_type* src, struct_type* target,
381 if (target->additionalFeatures)
382 cef_string_list_free(target->additionalFeatures);
383 target->additionalFeatures = src->additionalFeatures ?
384 cef_string_list_copy(src->additionalFeatures) : NULL;
387 target->xSet = src->xSet;
389 target->ySet = src->ySet;
390 target->width = src->width;
391 target->widthSet = src->widthSet;
392 target->height = src->height;
393 target->heightSet = src->heightSet;
394 target->menuBarVisible = src->menuBarVisible;
395 target->statusBarVisible = src->statusBarVisible;
396 target->toolBarVisible = src->toolBarVisible;
397 target->locationBarVisible = src->locationBarVisible;
398 target->scrollbarsVisible = src->scrollbarsVisible;
399 target->resizable = src->resizable;
400 target->fullscreen = src->fullscreen;
401 target->dialog = src->dialog;
406 // Class representing popup window features.
408 typedef CefStructBase<CefPopupFeaturesTraits> CefPopupFeatures;
411 struct CefSettingsTraits {
412 typedef cef_settings_t struct_type;
414 static inline void init(struct_type* s) {
415 s->size = sizeof(struct_type);
418 static inline void clear(struct_type* s) {
419 cef_string_clear(&s->browser_subprocess_path);
420 cef_string_clear(&s->cache_path);
421 cef_string_clear(&s->user_data_path);
422 cef_string_clear(&s->user_agent);
423 cef_string_clear(&s->product_version);
424 cef_string_clear(&s->locale);
425 cef_string_clear(&s->log_file);
426 cef_string_clear(&s->javascript_flags);
427 cef_string_clear(&s->resources_dir_path);
428 cef_string_clear(&s->locales_dir_path);
429 cef_string_clear(&s->accept_language_list);
432 static inline void set(const struct_type* src, struct_type* target,
434 target->single_process = src->single_process;
435 target->no_sandbox = src->no_sandbox;
436 cef_string_set(src->browser_subprocess_path.str,
437 src->browser_subprocess_path.length,
438 &target->browser_subprocess_path, copy);
439 target->multi_threaded_message_loop = src->multi_threaded_message_loop;
440 target->windowless_rendering_enabled = src->windowless_rendering_enabled;
441 target->command_line_args_disabled = src->command_line_args_disabled;
443 cef_string_set(src->cache_path.str, src->cache_path.length,
444 &target->cache_path, copy);
445 cef_string_set(src->user_data_path.str, src->user_data_path.length,
446 &target->user_data_path, copy);
447 target->persist_session_cookies = src->persist_session_cookies;
449 cef_string_set(src->user_agent.str, src->user_agent.length,
450 &target->user_agent, copy);
451 cef_string_set(src->product_version.str, src->product_version.length,
452 &target->product_version, copy);
453 cef_string_set(src->locale.str, src->locale.length, &target->locale, copy);
455 cef_string_set(src->log_file.str, src->log_file.length, &target->log_file,
457 target->log_severity = src->log_severity;
458 cef_string_set(src->javascript_flags.str, src->javascript_flags.length,
459 &target->javascript_flags, copy);
461 cef_string_set(src->resources_dir_path.str, src->resources_dir_path.length,
462 &target->resources_dir_path, copy);
463 cef_string_set(src->locales_dir_path.str, src->locales_dir_path.length,
464 &target->locales_dir_path, copy);
465 target->pack_loading_disabled = src->pack_loading_disabled;
466 target->remote_debugging_port = src->remote_debugging_port;
467 target->uncaught_exception_stack_size = src->uncaught_exception_stack_size;
468 target->context_safety_implementation = src->context_safety_implementation;
469 target->ignore_certificate_errors = src->ignore_certificate_errors;
470 target->background_color = src->background_color;
472 cef_string_set(src->accept_language_list.str,
473 src->accept_language_list.length, &target->accept_language_list, copy);
478 // Class representing initialization settings.
480 typedef CefStructBase<CefSettingsTraits> CefSettings;
483 struct CefRequestContextSettingsTraits {
484 typedef cef_request_context_settings_t struct_type;
486 static inline void init(struct_type* s) {
487 s->size = sizeof(struct_type);
490 static inline void clear(struct_type* s) {
491 cef_string_clear(&s->cache_path);
492 cef_string_clear(&s->accept_language_list);
495 static inline void set(const struct_type* src, struct_type* target,
497 cef_string_set(src->cache_path.str, src->cache_path.length,
498 &target->cache_path, copy);
499 target->persist_session_cookies = src->persist_session_cookies;
500 target->ignore_certificate_errors = src->ignore_certificate_errors;
501 cef_string_set(src->accept_language_list.str,
502 src->accept_language_list.length, &target->accept_language_list, copy);
507 // Class representing request context initialization settings.
509 typedef CefStructBase<CefRequestContextSettingsTraits>
510 CefRequestContextSettings;
513 struct CefBrowserSettingsTraits {
514 typedef cef_browser_settings_t struct_type;
516 static inline void init(struct_type* s) {
517 s->size = sizeof(struct_type);
520 static inline void clear(struct_type* s) {
521 cef_string_clear(&s->standard_font_family);
522 cef_string_clear(&s->fixed_font_family);
523 cef_string_clear(&s->serif_font_family);
524 cef_string_clear(&s->sans_serif_font_family);
525 cef_string_clear(&s->cursive_font_family);
526 cef_string_clear(&s->fantasy_font_family);
527 cef_string_clear(&s->default_encoding);
528 cef_string_clear(&s->accept_language_list);
531 static inline void set(const struct_type* src, struct_type* target,
533 target->windowless_frame_rate = src->windowless_frame_rate;
535 cef_string_set(src->standard_font_family.str,
536 src->standard_font_family.length, &target->standard_font_family, copy);
537 cef_string_set(src->fixed_font_family.str, src->fixed_font_family.length,
538 &target->fixed_font_family, copy);
539 cef_string_set(src->serif_font_family.str, src->serif_font_family.length,
540 &target->serif_font_family, copy);
541 cef_string_set(src->sans_serif_font_family.str,
542 src->sans_serif_font_family.length, &target->sans_serif_font_family,
544 cef_string_set(src->cursive_font_family.str,
545 src->cursive_font_family.length, &target->cursive_font_family, copy);
546 cef_string_set(src->fantasy_font_family.str,
547 src->fantasy_font_family.length, &target->fantasy_font_family, copy);
549 target->default_font_size = src->default_font_size;
550 target->default_fixed_font_size = src->default_fixed_font_size;
551 target->minimum_font_size = src->minimum_font_size;
552 target->minimum_logical_font_size = src->minimum_logical_font_size;
554 cef_string_set(src->default_encoding.str, src->default_encoding.length,
555 &target->default_encoding, copy);
557 target->remote_fonts = src->remote_fonts;
558 target->javascript = src->javascript;
559 target->javascript_open_windows = src->javascript_open_windows;
560 target->javascript_close_windows = src->javascript_close_windows;
561 target->javascript_access_clipboard = src->javascript_access_clipboard;
562 target->javascript_dom_paste = src->javascript_dom_paste;
563 target->caret_browsing = src->caret_browsing;
564 target->java = src->java;
565 target->plugins = src->plugins;
566 target->universal_access_from_file_urls =
567 src->universal_access_from_file_urls;
568 target->file_access_from_file_urls = src->file_access_from_file_urls;
569 target->web_security = src->web_security;
570 target->image_loading = src->image_loading;
571 target->image_shrink_standalone_to_fit =
572 src->image_shrink_standalone_to_fit;
573 target->text_area_resize = src->text_area_resize;
574 target->tab_to_links = src->tab_to_links;
575 target->local_storage = src->local_storage;
576 target->databases= src->databases;
577 target->application_cache = src->application_cache;
578 target->webgl = src->webgl;
580 target->background_color = src->background_color;
582 cef_string_set(src->accept_language_list.str,
583 src->accept_language_list.length, &target->accept_language_list, copy);
588 // Class representing browser initialization settings.
590 typedef CefStructBase<CefBrowserSettingsTraits> CefBrowserSettings;
593 struct CefURLPartsTraits {
594 typedef cef_urlparts_t struct_type;
596 static inline void init(struct_type* s) {}
598 static inline void clear(struct_type* s) {
599 cef_string_clear(&s->spec);
600 cef_string_clear(&s->scheme);
601 cef_string_clear(&s->username);
602 cef_string_clear(&s->password);
603 cef_string_clear(&s->host);
604 cef_string_clear(&s->port);
605 cef_string_clear(&s->origin);
606 cef_string_clear(&s->path);
607 cef_string_clear(&s->query);
610 static inline void set(const struct_type* src, struct_type* target,
612 cef_string_set(src->spec.str, src->spec.length, &target->spec, copy);
613 cef_string_set(src->scheme.str, src->scheme.length, &target->scheme, copy);
614 cef_string_set(src->username.str, src->username.length, &target->username,
616 cef_string_set(src->password.str, src->password.length, &target->password,
618 cef_string_set(src->host.str, src->host.length, &target->host, copy);
619 cef_string_set(src->port.str, src->port.length, &target->port, copy);
620 cef_string_set(src->origin.str, src->origin.length, &target->origin, copy);
621 cef_string_set(src->path.str, src->path.length, &target->path, copy);
622 cef_string_set(src->query.str, src->query.length, &target->query, copy);
627 // Class representing a URL's component parts.
629 typedef CefStructBase<CefURLPartsTraits> CefURLParts;
632 struct CefTimeTraits {
633 typedef cef_time_t struct_type;
635 static inline void init(struct_type* s) {}
637 static inline void clear(struct_type* s) {}
639 static inline void set(const struct_type* src, struct_type* target,
646 // Class representing a time.
648 class CefTime : public CefStructBase<CefTimeTraits> {
650 typedef CefStructBase<CefTimeTraits> parent;
652 CefTime() : parent() {}
653 CefTime(const cef_time_t& r) : parent(r) {} // NOLINT(runtime/explicit)
654 CefTime(const CefTime& r) : parent(r) {} // NOLINT(runtime/explicit)
655 explicit CefTime(time_t r) : parent() { SetTimeT(r); }
656 explicit CefTime(double r) : parent() { SetDoubleT(r); }
658 // Converts to/from time_t.
659 void SetTimeT(time_t r) {
660 cef_time_from_timet(r, this);
662 time_t GetTimeT() const {
664 cef_time_to_timet(this, &time);
668 // Converts to/from a double which is the number of seconds since epoch
669 // (Jan 1, 1970). Webkit uses this format to represent time. A value of 0
670 // means "not initialized".
671 void SetDoubleT(double r) {
672 cef_time_from_doublet(r, this);
674 double GetDoubleT() const {
676 cef_time_to_doublet(this, &time);
680 // Set this object to now.
685 // Return the delta between this object and |other| in milliseconds.
686 long long Delta(const CefTime& other) {
688 cef_time_delta(this, &other, &delta);
694 struct CefCookieTraits {
695 typedef cef_cookie_t struct_type;
697 static inline void init(struct_type* s) {}
699 static inline void clear(struct_type* s) {
700 cef_string_clear(&s->name);
701 cef_string_clear(&s->value);
702 cef_string_clear(&s->domain);
703 cef_string_clear(&s->path);
706 static inline void set(const struct_type* src, struct_type* target,
708 cef_string_set(src->name.str, src->name.length, &target->name, copy);
709 cef_string_set(src->value.str, src->value.length, &target->value, copy);
710 cef_string_set(src->domain.str, src->domain.length, &target->domain, copy);
711 cef_string_set(src->path.str, src->path.length, &target->path, copy);
712 target->secure = src->secure;
713 target->httponly = src->httponly;
714 target->creation = src->creation;
715 target->last_access = src->last_access;
716 target->has_expires = src->has_expires;
717 target->expires = src->expires;
722 // Class representing a cookie.
724 typedef CefStructBase<CefCookieTraits> CefCookie;
727 struct CefGeopositionTraits {
728 typedef cef_geoposition_t struct_type;
730 static inline void init(struct_type* s) {}
732 static inline void clear(struct_type* s) {
733 cef_string_clear(&s->error_message);
736 static inline void set(const struct_type* src, struct_type* target,
738 target->latitude = src->latitude;
739 target->longitude = src->longitude;
740 target->altitude = src->altitude;
741 target->accuracy = src->accuracy;
742 target->altitude_accuracy = src->altitude_accuracy;
743 target->heading = src->heading;
744 target->speed = src->speed;
745 target->timestamp = src->timestamp;
746 target->error_code = src->error_code;
747 cef_string_set(src->error_message.str, src->error_message.length,
748 &target->error_message, copy);
753 // Class representing a geoposition.
755 typedef CefStructBase<CefGeopositionTraits> CefGeoposition;
758 struct CefPageRangeTraits {
759 typedef cef_page_range_t struct_type;
761 static inline void init(struct_type* s) {}
762 static inline void clear(struct_type* s) {}
764 static inline void set(const struct_type* src, struct_type* target,
771 // Class representing a print job page range.
773 class CefPageRange : public CefStructBase<CefPageRangeTraits> {
775 typedef CefStructBase<CefPageRangeTraits> parent;
777 CefPageRange() : parent() {}
778 CefPageRange(const cef_page_range_t& r) // NOLINT(runtime/explicit)
780 CefPageRange(const CefPageRange& r) // NOLINT(runtime/explicit)
782 CefPageRange(int from, int to) : parent() {
786 void Set(int from, int to) {
787 this->from = from, this->to = to;
791 inline bool operator==(const CefPageRange& a, const CefPageRange& b) {
792 return a.from == b.from && a.to == b.to;
795 inline bool operator!=(const CefPageRange& a, const CefPageRange& b) {
800 struct CefCursorInfoTraits {
801 typedef cef_cursor_info_t struct_type;
803 static inline void init(struct_type* s) {}
805 static inline void clear(struct_type* s) {}
807 static inline void set(const struct_type* src, struct_type* target,
809 target->hotspot = src->hotspot;
810 target->image_scale_factor = src->image_scale_factor;
811 target->buffer = src->buffer;
812 target->size = src->size;
817 // Class representing cursor information.
819 typedef CefStructBase<CefCursorInfoTraits> CefCursorInfo;
821 #endif // CEF_INCLUDE_INTERNAL_CEF_TYPES_WRAPPERS_H_