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

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里面修改即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值