Chromium 中JavaScript Screen API接口c++代码实现

news2024/10/6 2:49:52

Screen - Web API | MDN (mozilla.org)

Screen

Screen 接口表示一个屏幕窗口,往往指的是当前正在被渲染的 window 对象,可以使用 window.screen 获取它。

请注意:由浏览器决定提供屏幕对象,此对象一般通过当前浏览器窗口活动状态动态检测来得到。

一、前端示例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test fetch</title>
<script>


function Test() {
 

 // crude way to check that the screen is at 1024x768
if (window.screen.width > 1000) {
  // resolution is below 10 x 7
    alert(window.screen.width);
}

}
</script>
</head>
<body>
 
<button onclick="Test()">Test Sceen API</button>
</body>
</html>

二、c++ Screen接口实现

1、Screen接口定义:

third_party\blink\renderer\core\frame\screen.idl

/*
 * Copyright (C) 2007 Apple Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// https://drafts.csswg.org/cssom-view/#the-screen-interface

[
    Exposed=Window
] interface Screen : EventTarget {
    [HighEntropy=Direct, Measure] readonly attribute long availWidth;
    [HighEntropy=Direct, Measure] readonly attribute long availHeight;
    [HighEntropy=Direct, Measure] readonly attribute long width;
    [HighEntropy=Direct, Measure] readonly attribute long height;
    [HighEntropy=Direct, Measure] readonly attribute unsigned long colorDepth;
    // pixelDepth() is an alias for colorDepth(), no need to instrument it twice.
    [HighEntropy, Measure] readonly attribute unsigned long pixelDepth;

    // Non-standard
    [HighEntropy=Direct, Measure] readonly attribute long availLeft;
    [HighEntropy=Direct, Measure] readonly attribute long availTop;

    // Fired when the window’s screen or that screen's attributes change.
    // https://w3c.github.io/window-placement/
    [SecureContext, HighEntropy, MeasureAs=WindowScreenChange] attribute EventHandler onchange;

    // Whether the device’s visual output extends over multiple screens.
    // https://w3c.github.io/window-placement/
    [SecureContext, HighEntropy=Direct, MeasureAs=WindowScreenIsExtended] readonly attribute boolean isExtended;
};

2、Screen接口c++实现

third_party\blink\renderer\core\frame\screen.h

third_party\blink\renderer\core\frame\screen.cc

/*
 * Copyright (C) 2007 Apple Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_SCREEN_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_SCREEN_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/events/event_target.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/supplementable.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "ui/gfx/geometry/rect.h"

namespace display {
struct ScreenInfo;
}

namespace blink {

class LocalDOMWindow;

class CORE_EXPORT Screen : public EventTarget,
                           public ExecutionContextClient,
                           public Supplementable<Screen> {
  DEFINE_WRAPPERTYPEINFO();

 public:
  Screen(LocalDOMWindow*, int64_t display_id);

  static bool AreWebExposedScreenPropertiesEqual(
      const display::ScreenInfo& prev,
      const display::ScreenInfo& current);

  int height() const;
  int width() const;
  unsigned colorDepth() const;
  unsigned pixelDepth() const;
  int availLeft() const;
  int availTop() const;
  int availHeight() const;
  int availWidth() const;

  void Trace(Visitor*) const override;

  // EventTarget:
  const WTF::AtomicString& InterfaceName() const override;
  ExecutionContext* GetExecutionContext() const override;

  // Whether the device’s visual output extends over multiple screens.
  // https://w3c.github.io/window-placement/
  bool isExtended() const;
  // Fired when the window’s screen or that screen's attributes change.
  // https://w3c.github.io/window-placement/
  DEFINE_ATTRIBUTE_EVENT_LISTENER(change, kChange)

  // Not web-exposed; for internal usage only.
  static constexpr int64_t kInvalidDisplayId = -1;
  int64_t DisplayId() const { return display_id_; }
  void UpdateDisplayId(int64_t display_id) { display_id_ = display_id; }

 protected:
  // Helpers to access screen information.
  gfx::Rect GetRect(bool available) const;
  const display::ScreenInfo& GetScreenInfo() const;

  // The internal id of the underlying display, to support multi-screen devices.
  int64_t display_id_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_SCREEN_H_

三、打开测试用例调试http://192.168.8.1/chfs/shared/test/test02.html

1、附加到刚打开的新标签进程ID=7244

2、vs附加ID=7244

3、点击页面按钮

1)、src\out\Debug\gen\third_party\blink\renderer\bindings\modules\v8\v8_screen.cc

断点WidthAttributeGetCallback()//前端代码执行到 Screen.width.get

void WidthAttributeGetCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
  
RUNTIME_CALL_TIMER_SCOPE_DISABLED_BY_DEFAULT(info.GetIsolate(), "Blink_Screen_width_Getter");
BLINK_BINDINGS_TRACE_EVENT("Screen.width.get");
// [HighEntropy]
const Dactyloscoper::HighEntropyTracer  high_entropy_tracer("Screen.width.get", info);
v8::Isolate* isolate = info.GetIsolate();
v8::Local<v8::Context> current_context = isolate->GetCurrentContext();
ExecutionContext* current_execution_context = ExecutionContext::From(current_context);
// [Measure], [MeasureAs]
UseCounter::Count(current_execution_context, WebFeature::kV8Screen_Width_AttributeGetter);



v8::Local<v8::Object> v8_receiver = info.This();
Screen* blink_receiver = V8Screen::ToWrappableUnsafe(isolate, v8_receiver);
auto&& return_value = blink_receiver->width();
bindings::V8SetReturnValue(info, return_value, bindings::V8ReturnValue::PrimitiveType<int32_t>());
// [HighEntropy=Direct]
Dactyloscoper::RecordDirectSurface(current_execution_context, WebFeature::kV8Screen_Width_AttributeGetter, return_value);
}

2)、third_party\blink\renderer\core\frame\screen.cc

   

int Screen::width() const {
  if (!DomWindow())
    return 0;
  return GetRect(/*available=*/false).width();
}

最后看下调用堆栈图:

如果想要定制此接口在third_party\blink\renderer\core\frame\screen.h里面修改即可。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2190991.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学

希望这个下集里能有完整的代码 一、containsPoint实现 先从网上找一下Statement expected, found Py:DEDENTTAB还是空格呢??小小总结如何拆分矩形的四个点呢.我们来小小的测试一下这个函数结果出在哪里呢???修改完成variable in function should be lowercase 函数变量应该…

No.2 笔记 | 网络安全攻防:PC、CS工具与移动应用分析

引言 在当今数字化时代,网络安全已成为每个人都应该关注的重要话题。本文将总结一次关于网络安全攻防技术的学习内容,涵盖PC端和移动端的恶意程序利用,以及强大的渗透测试工具Cobalt Strike的使用。通过学习这些内容,我们不仅能够了解攻击者的手法,更能提高自身的安全意识和防…

【牛顿迭代法求极小值】

牛顿迭代法求极小值 仅供参考 作业内容与要求 作业内容 作业要求 递交报告 代码 编程实现 计算偏导数 故上述非线性方程组的根可能为 f ( x , y ) f(x, y) f(x,y)的极值点&#xff0c;至于是极小值点还是极大值点或鞍点&#xff0c;就需要使用微积分中的黑塞矩阵来判断了。…

网络基础 【HTTPS】

&#x1f493;博主CSDN主页:麻辣韭菜&#x1f493;   ⏩专栏分类&#xff1a;Linux初窥门径⏪   &#x1f69a;代码仓库:Linux代码练习&#x1f69a; &#x1f4bb;操作环境&#xff1a; CentOS 7.6 华为云远程服务器 &#x1f339;关注我&#x1faf5;带你学习更多Linux知识…

Linux之实战命令26:timeout应用实例(六十)

简介&#xff1a; CSDN博客专家、《Android系统多媒体进阶实战》一书作者 新书发布&#xff1a;《Android系统多媒体进阶实战》&#x1f680; 优质专栏&#xff1a; Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a; 多媒体系统工程师系列【…

安卓手机密码忘了怎么办?(只做科普)

注意&#xff1a;只做科普&#xff0c;拒绝利用技术做一些非法事情 科普人&#xff1a;网络安全工程师~DL 科普平台&#xff1a;快手&#xff0c;CSDN&#xff0c;微信公众号&#xff0c;小红书&#xff0c;百度&#xff0c;360 本期文章耗时比较大&#xff0c;如果喜欢&…

数学题-分糖果-答案解析

PDF文档回复:20241005 1[题目描述] 幼儿园有7个小朋友&#xff0c;你是其中之一&#xff0c;有一天你发现无穷多颗糖&#xff0c;最少可以拿16个&#xff0c;最多可以拿23个&#xff0c;你打算拿一些分给小朋友们&#xff0c;分配原则是如果每人(包括你)都可以拿1块糖&#xf…

快速上手C语言【上】(非常详细!!!)

目录 1. 基本数据类型 2. 变量 2.1 定义格式 和 命名规范 2.2 格式化输入和输出&#xff08;scanf 和 printf&#xff09; ​编辑 2.3 作用域和生命周期 3. 常量 4. 字符串转义字符注释 5. 操作符 5.1 双目操作符 5.1.1 算数操作符 5.1.2 移位操作符 5.1.3 位操作符…

IDEA下“File is read-only”可能原因及“找不到或无法加载主类”问题的解决

1.File is read-only”可能原因 写代码时想要修改这个静态变量的值&#xff0c;把这个语句注释掉&#xff0c;发现在这个文件中File is read-only无法编辑修改&#xff0c;于是想去掉这个状态 网上查看的解释大多是在File栏目或File->File Properties下可以找到Make File W…

Git介绍--github/gitee/gitlab使用

一、Git的介绍 1.1、学习Git的原因&#xff1a;资源管理 1.2、SCM软件的介绍 软件配置管理(SCM)是指通过执行版本控制、变更控制的规程&#xff0c;以及使用合适的配置管理软件来保证所有配置项的完整性和可跟踪性。配置管理是对工作成果的一种有效保护。 二、版本控制软件 …

常见的基础系统

权限管理系统支付系统搜索系统报表系统API网关系统待定。。。 Java 优质开源系统设计项目 来源&#xff1a;Java 优质开源系统设计项目 | JavaGuide 备注&#xff1a;github和gitee上可以搜索到相关项目

企业必备:搭建大模型应用平台实操教程

最近AI智能体很火&#xff0c;AI智能体平台化产品肯定属于大公司的。但在一些场景下&#xff0c;尤其是对业务数据要求很高的公司&#xff0c;那就只能用私有大模型。不一定完全是为了对外提供服务&#xff0c;对内改造工作流也是需要的。所以 我感觉未来大部分企业都会搞一个…

软考系统分析师知识点二:经济管理

前言 今年报考了11月份的软考高级&#xff1a;系统分析师。 考试时间为&#xff1a;11月9日。 倒计时&#xff1a;35天。 目标&#xff1a;优先应试&#xff0c;其次学习&#xff0c;再次实践。 复习计划第一阶段&#xff1a;扫平基础知识点&#xff0c;仅抽取有用信息&am…

数字乡村综合解决方案

1. 项目背景与战略 《中共中央、国务院关于实施乡村振兴战略的意见》强调实施数字乡村战略的重要性&#xff0c;旨在通过信息技术和产品服务推动农业农村现代化&#xff0c;实现城乡数字鸿沟的弥合。 2. 数字乡村发展纲要 《数字乡村发展战略纲要》明确了全面建成数字乡村的…

颍川陈氏始祖陈寔逆势崛起的原由(二)有贵人相助

园子说颍川 陈寔崛起之初&#xff0c;有两个贵人发挥了关键作用。 第一个就是许县县令邓邵&#xff0c;如果不是他推荐青年陈寔去太学读书&#xff0c;陈寔可能一辈子就要待在许县县衙当小吏了。关于他的记载不详&#xff0c;光这一件事就让他名垂青史&#xff0c;帮助一个穷…

为Floorp浏览器添加搜索引擎及搜索栏相关设置. 2024-10-05

Floorp浏览器开源项目地址: https://github.com/floorp-Projects/floorp/ 1.第一步 为Floorp浏览器添加搜索栏 (1.工具栏空白处 次键选择 定制工具栏 (2. 把 搜索框 拖动至工具栏 2.添加搜索引擎 以添加 搜狗搜索 为例 (1.访问 搜索引擎网址 搜狗搜索引擎 - 上网从搜狗开始 (2…

【AIGC】ChatGPT提示词Prompt助力自媒体内容创作升级

博客主页&#xff1a; [小ᶻZ࿆] 本文专栏: AIGC | ChatGPT 文章目录 &#x1f4af;前言&#x1f4af;高效仿写专家级文章提示词使用方法 &#x1f4af;CSDN博主账号分析提示词使用方法 &#x1f4af;自媒体爆款文案优化助手提示词使用方法 &#x1f4af;小结 &#x1f4af…

王者农药更新版

一、启动文件配置 二、GPIO使用 2.1基本步骤 1.配置GPIO&#xff0c;所以RCC开启APB2时钟 2.GPIO初始化&#xff08;结构体&#xff09; 3.给GPIO引脚设置高/低电平&#xff08;WriteBit&#xff09; 2.2Led循环点亮&#xff08;GPIO输出&#xff09; 1.RCC开启APB2时钟。…

Transformer架构概述(二)

目录 1. Transformer架构概述 1.1 《Attention is All You Need》论文概述 1.2 Transformer的模块组成 1.3 Encoder 和 Decoder 的区别与联系 2. Transformer的并行计算效率相对于RNN的提升 2.1 RNN中的顺序处理问题 2.2 Transformer中的并行化优势 3. Self-Attention机…

Spring Boot框架下的大学生就业招聘平台

5系统详细实现 5.1 用户模块的实现 5.1.1 求职信息管理 大学生就业招聘系统的用户可以管理自己的求职信息&#xff0c;可以对自己的求职信息添加修改删除操作。具体界面的展示如图5.1所示。 图5.1 求职信息管理界面 5.1.2 首页 用户登录可以在首页看到招聘信息展示也一些求职…