github.com/ckxng/wakeup@v0.0.0-20190105202853-90356a5f5a15/include/internal/cef_types_wrappers.h (about) 1 // Copyright (c) 2013 Marshall A. Greenblatt. All rights reserved. 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 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 12 // distribution. 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. 17 // 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. 29 30 #ifndef CEF_INCLUDE_INTERNAL_CEF_TYPES_WRAPPERS_H_ 31 #define CEF_INCLUDE_INTERNAL_CEF_TYPES_WRAPPERS_H_ 32 #pragma once 33 34 #include "include/internal/cef_string.h" 35 #include "include/internal/cef_string_list.h" 36 #include "include/internal/cef_types.h" 37 38 /// 39 // Template class that provides common functionality for CEF structure wrapping. 40 /// 41 template <class traits> 42 class CefStructBase : public traits::struct_type { 43 public: 44 typedef typename traits::struct_type struct_type; 45 46 CefStructBase() : attached_to_(NULL) { 47 Init(); 48 } 49 virtual ~CefStructBase() { 50 // Only clear this object's data if it isn't currently attached to a 51 // structure. 52 if (!attached_to_) 53 Clear(this); 54 } 55 56 CefStructBase(const CefStructBase& r) { 57 Init(); 58 *this = r; 59 } 60 CefStructBase(const struct_type& r) { // NOLINT(runtime/explicit) 61 Init(); 62 *this = r; 63 } 64 65 /// 66 // Clear this object's values. 67 /// 68 void Reset() { 69 Clear(this); 70 Init(); 71 } 72 73 /// 74 // Attach to the source structure's existing values. DetachTo() can be called 75 // to insert the values back into the existing structure. 76 /// 77 void AttachTo(struct_type& source) { 78 // Only clear this object's data if it isn't currently attached to a 79 // structure. 80 if (!attached_to_) 81 Clear(this); 82 83 // This object is now attached to the new structure. 84 attached_to_ = &source; 85 86 // Transfer ownership of the values from the source structure. 87 memcpy(static_cast<struct_type*>(this), &source, sizeof(struct_type)); 88 } 89 90 /// 91 // Relinquish ownership of values to the target structure. 92 /// 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. 97 Clear(&target); 98 } 99 100 // Transfer ownership of the values to the target structure. 101 memcpy(&target, static_cast<struct_type*>(this), sizeof(struct_type)); 102 103 // Remove the references from this object. 104 Init(); 105 } 106 107 /// 108 // Set this object's values. If |copy| is true the source structure's values 109 // will be copied instead of referenced. 110 /// 111 void Set(const struct_type& source, bool copy) { 112 traits::set(&source, this, copy); 113 } 114 115 CefStructBase& operator=(const CefStructBase& s) { 116 return operator=(static_cast<const struct_type&>(s)); 117 } 118 119 CefStructBase& operator=(const struct_type& s) { 120 Set(s, true); 121 return *this; 122 } 123 124 protected: 125 void Init() { 126 memset(static_cast<struct_type*>(this), 0, sizeof(struct_type)); 127 attached_to_ = NULL; 128 traits::init(this); 129 } 130 131 static void Clear(struct_type* s) { traits::clear(s); } 132 133 struct_type* attached_to_; 134 }; 135 136 137 struct CefRectTraits { 138 typedef cef_rect_t struct_type; 139 140 static inline void init(struct_type* s) {} 141 static inline void clear(struct_type* s) {} 142 143 static inline void set(const struct_type* src, struct_type* target, 144 bool copy) { 145 *target = *src; 146 } 147 }; 148 149 /// 150 // Class representing a rectangle. 151 /// 152 class CefRect : public CefStructBase<CefRectTraits> { 153 public: 154 typedef CefStructBase<CefRectTraits> parent; 155 156 CefRect() : parent() {} 157 CefRect(const cef_rect_t& r) : parent(r) {} // NOLINT(runtime/explicit) 158 CefRect(const CefRect& r) : parent(r) {} // NOLINT(runtime/explicit) 159 CefRect(int x, int y, int width, int height) : parent() { 160 Set(x, y, width, height); 161 } 162 163 bool IsEmpty() const { return width <= 0 || height <= 0; } 164 void Set(int x, int y, int width, int height) { 165 this->x = x, this->y = y, this->width = width, this->height = height; 166 } 167 }; 168 169 inline bool operator==(const CefRect& a, const CefRect& b) { 170 return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height; 171 } 172 173 inline bool operator!=(const CefRect& a, const CefRect& b) { 174 return !(a == b); 175 } 176 177 struct CefScreenInfoTraits { 178 typedef cef_screen_info_t struct_type; 179 180 static inline void init(struct_type* s) {} 181 182 static inline void clear(struct_type* s) {} 183 184 static inline void set(const struct_type* src, struct_type* target, 185 bool copy) { 186 target->device_scale_factor = src->device_scale_factor; 187 target->depth = src->depth; 188 target->depth_per_component = src->depth_per_component; 189 target->is_monochrome = src->is_monochrome; 190 target->rect = src->rect; 191 target->available_rect = src->available_rect; 192 } 193 }; 194 195 /// 196 // Class representing the virtual screen information for use when window rendering 197 // is disabled. 198 /// 199 class CefScreenInfo : public CefStructBase<CefScreenInfoTraits> { 200 public: 201 typedef CefStructBase<CefScreenInfoTraits> parent; 202 203 CefScreenInfo() : parent() {} 204 CefScreenInfo(const cef_screen_info_t& r) : parent(r) {} // NOLINT(runtime/explicit) 205 CefScreenInfo(const CefScreenInfo& r) : parent(r) {} // NOLINT(runtime/explicit) 206 CefScreenInfo(float device_scale_factor, 207 int depth, 208 int depth_per_component, 209 bool is_monochrome, 210 const CefRect& rect, 211 const CefRect& available_rect) : parent() { 212 Set(device_scale_factor, depth, depth_per_component, 213 is_monochrome, rect, available_rect); 214 } 215 216 void Set(float device_scale_factor, 217 int depth, 218 int depth_per_component, 219 bool is_monochrome, 220 const CefRect& rect, 221 const CefRect& available_rect) { 222 this->device_scale_factor = device_scale_factor; 223 this->depth = depth; 224 this->depth_per_component = depth_per_component; 225 this->is_monochrome = is_monochrome; 226 this->rect = rect; 227 this->available_rect = available_rect; 228 } 229 }; 230 231 struct CefKeyEventTraits { 232 typedef cef_key_event_t struct_type; 233 234 static inline void init(struct_type* s) {} 235 236 static inline void clear(struct_type* s) {} 237 238 static inline void set(const struct_type* src, struct_type* target, 239 bool copy) { 240 target->type = src->type; 241 target->modifiers = src->modifiers; 242 target->windows_key_code = src->windows_key_code; 243 target->native_key_code = src->native_key_code; 244 target->is_system_key = src->is_system_key; 245 target->character = src->character; 246 target->unmodified_character = src->unmodified_character; 247 target->focus_on_editable_field = src->focus_on_editable_field; 248 } 249 }; 250 251 /// 252 // Class representing a a keyboard event. 253 /// 254 typedef CefStructBase<CefKeyEventTraits> CefKeyEvent; 255 256 struct CefMouseEventTraits { 257 typedef cef_mouse_event_t struct_type; 258 259 static inline void init(struct_type* s) {} 260 261 static inline void clear(struct_type* s) {} 262 263 static inline void set(const struct_type* src, struct_type* target, 264 bool copy) { 265 target->x = src->x; 266 target->y = src->y; 267 target->modifiers = src->modifiers; 268 } 269 }; 270 271 /// 272 // Class representing a mouse event. 273 /// 274 typedef CefStructBase<CefMouseEventTraits> CefMouseEvent; 275 276 struct CefPopupFeaturesTraits { 277 typedef cef_popup_features_t struct_type; 278 279 static inline void init(struct_type* s) { 280 s->menuBarVisible = true; 281 s->statusBarVisible = true; 282 s->toolBarVisible = true; 283 s->locationBarVisible = true; 284 s->scrollbarsVisible = true; 285 s->resizable = true; 286 } 287 288 static inline void clear(struct_type* s) { 289 if (s->additionalFeatures) 290 cef_string_list_free(s->additionalFeatures); 291 } 292 293 static inline void set(const struct_type* src, struct_type* target, 294 bool copy) { 295 if (target->additionalFeatures) 296 cef_string_list_free(target->additionalFeatures); 297 target->additionalFeatures = src->additionalFeatures ? 298 cef_string_list_copy(src->additionalFeatures) : NULL; 299 300 target->x = src->x; 301 target->xSet = src->xSet; 302 target->y = src->y; 303 target->ySet = src->ySet; 304 target->width = src->width; 305 target->widthSet = src->widthSet; 306 target->height = src->height; 307 target->heightSet = src->heightSet; 308 target->menuBarVisible = src->menuBarVisible; 309 target->statusBarVisible = src->statusBarVisible; 310 target->toolBarVisible = src->toolBarVisible; 311 target->locationBarVisible = src->locationBarVisible; 312 target->scrollbarsVisible = src->scrollbarsVisible; 313 target->resizable = src->resizable; 314 target->fullscreen = src->fullscreen; 315 target->dialog = src->dialog; 316 } 317 }; 318 319 /// 320 // Class representing popup window features. 321 /// 322 typedef CefStructBase<CefPopupFeaturesTraits> CefPopupFeatures; 323 324 325 struct CefSettingsTraits { 326 typedef cef_settings_t struct_type; 327 328 static inline void init(struct_type* s) { 329 s->size = sizeof(struct_type); 330 } 331 332 static inline void clear(struct_type* s) { 333 cef_string_clear(&s->browser_subprocess_path); 334 cef_string_clear(&s->cache_path); 335 cef_string_clear(&s->user_agent); 336 cef_string_clear(&s->product_version); 337 cef_string_clear(&s->locale); 338 cef_string_clear(&s->log_file); 339 cef_string_clear(&s->javascript_flags); 340 cef_string_clear(&s->resources_dir_path); 341 cef_string_clear(&s->locales_dir_path); 342 } 343 344 static inline void set(const struct_type* src, struct_type* target, 345 bool copy) { 346 target->single_process = src->single_process; 347 target->no_sandbox = src->no_sandbox; 348 cef_string_set(src->browser_subprocess_path.str, 349 src->browser_subprocess_path.length, 350 &target->browser_subprocess_path, copy); 351 target->multi_threaded_message_loop = src->multi_threaded_message_loop; 352 target->command_line_args_disabled = src->command_line_args_disabled; 353 354 cef_string_set(src->cache_path.str, src->cache_path.length, 355 &target->cache_path, copy); 356 target->persist_session_cookies = src->persist_session_cookies; 357 358 cef_string_set(src->user_agent.str, src->user_agent.length, 359 &target->user_agent, copy); 360 cef_string_set(src->product_version.str, src->product_version.length, 361 &target->product_version, copy); 362 cef_string_set(src->locale.str, src->locale.length, &target->locale, copy); 363 364 cef_string_set(src->log_file.str, src->log_file.length, &target->log_file, 365 copy); 366 target->log_severity = src->log_severity; 367 target->release_dcheck_enabled = src->release_dcheck_enabled; 368 cef_string_set(src->javascript_flags.str, src->javascript_flags.length, 369 &target->javascript_flags, copy); 370 371 cef_string_set(src->resources_dir_path.str, src->resources_dir_path.length, 372 &target->resources_dir_path, copy); 373 cef_string_set(src->locales_dir_path.str, src->locales_dir_path.length, 374 &target->locales_dir_path, copy); 375 target->pack_loading_disabled = src->pack_loading_disabled; 376 target->remote_debugging_port = src->remote_debugging_port; 377 target->uncaught_exception_stack_size = src->uncaught_exception_stack_size; 378 target->context_safety_implementation = src->context_safety_implementation; 379 target->ignore_certificate_errors = src->ignore_certificate_errors; 380 target->background_color = src->background_color; 381 } 382 }; 383 384 /// 385 // Class representing initialization settings. 386 /// 387 typedef CefStructBase<CefSettingsTraits> CefSettings; 388 389 390 struct CefBrowserSettingsTraits { 391 typedef cef_browser_settings_t struct_type; 392 393 static inline void init(struct_type* s) { 394 s->size = sizeof(struct_type); 395 } 396 397 static inline void clear(struct_type* s) { 398 cef_string_clear(&s->standard_font_family); 399 cef_string_clear(&s->fixed_font_family); 400 cef_string_clear(&s->serif_font_family); 401 cef_string_clear(&s->sans_serif_font_family); 402 cef_string_clear(&s->cursive_font_family); 403 cef_string_clear(&s->fantasy_font_family); 404 cef_string_clear(&s->default_encoding); 405 } 406 407 static inline void set(const struct_type* src, struct_type* target, 408 bool copy) { 409 cef_string_set(src->standard_font_family.str, 410 src->standard_font_family.length, &target->standard_font_family, copy); 411 cef_string_set(src->fixed_font_family.str, src->fixed_font_family.length, 412 &target->fixed_font_family, copy); 413 cef_string_set(src->serif_font_family.str, src->serif_font_family.length, 414 &target->serif_font_family, copy); 415 cef_string_set(src->sans_serif_font_family.str, 416 src->sans_serif_font_family.length, &target->sans_serif_font_family, 417 copy); 418 cef_string_set(src->cursive_font_family.str, 419 src->cursive_font_family.length, &target->cursive_font_family, copy); 420 cef_string_set(src->fantasy_font_family.str, 421 src->fantasy_font_family.length, &target->fantasy_font_family, copy); 422 423 target->default_font_size = src->default_font_size; 424 target->default_fixed_font_size = src->default_fixed_font_size; 425 target->minimum_font_size = src->minimum_font_size; 426 target->minimum_logical_font_size = src->minimum_logical_font_size; 427 428 cef_string_set(src->default_encoding.str, src->default_encoding.length, 429 &target->default_encoding, copy); 430 431 target->remote_fonts = src->remote_fonts; 432 target->javascript = src->javascript; 433 target->javascript_open_windows = src->javascript_open_windows; 434 target->javascript_close_windows = src->javascript_close_windows; 435 target->javascript_access_clipboard = src->javascript_access_clipboard; 436 target->javascript_dom_paste = src->javascript_dom_paste; 437 target->caret_browsing = src->caret_browsing; 438 target->java = src->java; 439 target->plugins = src->plugins; 440 target->universal_access_from_file_urls = 441 src->universal_access_from_file_urls; 442 target->file_access_from_file_urls = src->file_access_from_file_urls; 443 target->web_security = src->web_security; 444 target->image_loading = src->image_loading; 445 target->image_shrink_standalone_to_fit = 446 src->image_shrink_standalone_to_fit; 447 target->text_area_resize = src->text_area_resize; 448 target->tab_to_links = src->tab_to_links; 449 target->local_storage = src->local_storage; 450 target->databases= src->databases; 451 target->application_cache = src->application_cache; 452 target->webgl = src->webgl; 453 target->accelerated_compositing = src->accelerated_compositing; 454 } 455 }; 456 457 /// 458 // Class representing browser initialization settings. 459 /// 460 typedef CefStructBase<CefBrowserSettingsTraits> CefBrowserSettings; 461 462 463 struct CefURLPartsTraits { 464 typedef cef_urlparts_t struct_type; 465 466 static inline void init(struct_type* s) {} 467 468 static inline void clear(struct_type* s) { 469 cef_string_clear(&s->spec); 470 cef_string_clear(&s->scheme); 471 cef_string_clear(&s->username); 472 cef_string_clear(&s->password); 473 cef_string_clear(&s->host); 474 cef_string_clear(&s->port); 475 cef_string_clear(&s->origin); 476 cef_string_clear(&s->path); 477 cef_string_clear(&s->query); 478 } 479 480 static inline void set(const struct_type* src, struct_type* target, 481 bool copy) { 482 cef_string_set(src->spec.str, src->spec.length, &target->spec, copy); 483 cef_string_set(src->scheme.str, src->scheme.length, &target->scheme, copy); 484 cef_string_set(src->username.str, src->username.length, &target->username, 485 copy); 486 cef_string_set(src->password.str, src->password.length, &target->password, 487 copy); 488 cef_string_set(src->host.str, src->host.length, &target->host, copy); 489 cef_string_set(src->port.str, src->port.length, &target->port, copy); 490 cef_string_set(src->origin.str, src->origin.length, &target->origin, copy); 491 cef_string_set(src->path.str, src->path.length, &target->path, copy); 492 cef_string_set(src->query.str, src->query.length, &target->query, copy); 493 } 494 }; 495 496 /// 497 // Class representing a URL's component parts. 498 /// 499 typedef CefStructBase<CefURLPartsTraits> CefURLParts; 500 501 502 struct CefTimeTraits { 503 typedef cef_time_t struct_type; 504 505 static inline void init(struct_type* s) {} 506 507 static inline void clear(struct_type* s) {} 508 509 static inline void set(const struct_type* src, struct_type* target, 510 bool copy) { 511 *target = *src; 512 } 513 }; 514 515 /// 516 // Class representing a time. 517 /// 518 class CefTime : public CefStructBase<CefTimeTraits> { 519 public: 520 typedef CefStructBase<CefTimeTraits> parent; 521 522 CefTime() : parent() {} 523 CefTime(const cef_time_t& r) : parent(r) {} // NOLINT(runtime/explicit) 524 CefTime(const CefTime& r) : parent(r) {} // NOLINT(runtime/explicit) 525 explicit CefTime(time_t r) : parent() { SetTimeT(r); } 526 explicit CefTime(double r) : parent() { SetDoubleT(r); } 527 528 // Converts to/from time_t. 529 void SetTimeT(time_t r) { 530 cef_time_from_timet(r, this); 531 } 532 time_t GetTimeT() const { 533 time_t time = 0; 534 cef_time_to_timet(this, &time); 535 return time; 536 } 537 538 // Converts to/from a double which is the number of seconds since epoch 539 // (Jan 1, 1970). Webkit uses this format to represent time. A value of 0 540 // means "not initialized". 541 void SetDoubleT(double r) { 542 cef_time_from_doublet(r, this); 543 } 544 double GetDoubleT() const { 545 double time = 0; 546 cef_time_to_doublet(this, &time); 547 return time; 548 } 549 550 // Set this object to now. 551 void Now() { 552 cef_time_now(this); 553 } 554 555 // Return the delta between this object and |other| in milliseconds. 556 long long Delta(const CefTime& other) { 557 long long delta = 0; 558 cef_time_delta(this, &other, &delta); 559 return delta; 560 } 561 }; 562 563 564 struct CefCookieTraits { 565 typedef cef_cookie_t struct_type; 566 567 static inline void init(struct_type* s) {} 568 569 static inline void clear(struct_type* s) { 570 cef_string_clear(&s->name); 571 cef_string_clear(&s->value); 572 cef_string_clear(&s->domain); 573 cef_string_clear(&s->path); 574 } 575 576 static inline void set(const struct_type* src, struct_type* target, 577 bool copy) { 578 cef_string_set(src->name.str, src->name.length, &target->name, copy); 579 cef_string_set(src->value.str, src->value.length, &target->value, copy); 580 cef_string_set(src->domain.str, src->domain.length, &target->domain, copy); 581 cef_string_set(src->path.str, src->path.length, &target->path, copy); 582 target->secure = src->secure; 583 target->httponly = src->httponly; 584 target->creation = src->creation; 585 target->last_access = src->last_access; 586 target->has_expires = src->has_expires; 587 target->expires = src->expires; 588 } 589 }; 590 591 /// 592 // Class representing a cookie. 593 /// 594 typedef CefStructBase<CefCookieTraits> CefCookie; 595 596 597 struct CefGeopositionTraits { 598 typedef cef_geoposition_t struct_type; 599 600 static inline void init(struct_type* s) {} 601 602 static inline void clear(struct_type* s) { 603 cef_string_clear(&s->error_message); 604 } 605 606 static inline void set(const struct_type* src, struct_type* target, 607 bool copy) { 608 target->latitude = src->latitude; 609 target->longitude = src->longitude; 610 target->altitude = src->altitude; 611 target->accuracy = src->accuracy; 612 target->altitude_accuracy = src->altitude_accuracy; 613 target->heading = src->heading; 614 target->speed = src->speed; 615 target->timestamp = src->timestamp; 616 target->error_code = src->error_code; 617 cef_string_set(src->error_message.str, src->error_message.length, 618 &target->error_message, copy); 619 } 620 }; 621 622 /// 623 // Class representing a geoposition. 624 /// 625 typedef CefStructBase<CefGeopositionTraits> CefGeoposition; 626 627 #endif // CEF_INCLUDE_INTERNAL_CEF_TYPES_WRAPPERS_H_