Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / client_plugin_host_desktop_impl.js
blob23213ec6b9c3b9b3e25aba110afde86e19873b82
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /**
6 * @fileoverview
7 * Provides an interface to manage the Host Desktop of a remoting session.
8 */
10 var remoting = remoting || {};
11 remoting.ClientPlugin = remoting.ClientPlugin || {};
13 (function() {
15 'use strict';
17 /**
18 * @param {remoting.ClientPlugin} plugin
19 * @param {function(Object):void} postMessageCallback Callback to post a message
20 * to the Client Plugin.
22 * @implements {remoting.HostDesktop}
23 * @extends {base.EventSourceImpl}
24 * @constructor
26 remoting.ClientPlugin.HostDesktopImpl = function(plugin, postMessageCallback) {
27 base.inherits(this, base.EventSourceImpl);
28 /** @private */
29 this.plugin_ = plugin;
30 /** @private */
31 this.width_ = 0;
32 /** @private */
33 this.height_ = 0;
34 /** @private */
35 this.xDpi_ = 96;
36 /** @private */
37 this.yDpi_ = 96;
38 /** @private */
39 this.postMessageCallback_ = postMessageCallback;
41 this.defineEvents(base.values(remoting.HostDesktop.Events));
44 /** @return {{width:number, height:number, xDpi:number, yDpi:number}} */
45 remoting.ClientPlugin.HostDesktopImpl.prototype.getDimensions = function() {
46 return {
47 width: this.width_,
48 height: this.height_,
49 xDpi: this.xDpi_,
50 yDpi: this.yDpi_
54 /**
55 * @param {number} width
56 * @param {number} height
57 * @param {number} deviceScale
59 remoting.ClientPlugin.HostDesktopImpl.prototype.resize = function(
60 width, height, deviceScale) {
61 var dpi = Math.floor(deviceScale * 96);
62 this.postMessageCallback_({
63 method: 'notifyClientResolution',
64 data: {
65 width: Math.floor(width * deviceScale),
66 height: Math.floor(height * deviceScale),
67 x_dpi: dpi,
68 y_dpi: dpi
70 });
73 /**
74 * This function is called by |this.plugin_| when the size of the host
75 * desktop is changed.
77 * @param {remoting.ClientPluginMessage} message
79 remoting.ClientPlugin.HostDesktopImpl.prototype.onSizeUpdated = function(
80 message) {
81 this.width_ = base.getNumberAttr(message.data, 'width');
82 this.height_ = base.getNumberAttr(message.data, 'height');
83 this.xDpi_ = base.getNumberAttr(message.data, 'x_dpi', 96);
84 this.yDpi_ = base.getNumberAttr(message.data, 'y_dpi', 96);
85 this.raiseEvent(remoting.HostDesktop.Events.sizeChanged,
86 this.getDimensions());
89 /**
90 * This function is called by |this.plugin_| when the shape of the host
91 * desktop is changed.
93 * @param {remoting.ClientPluginMessage} message
94 * @return {Array<{left:number, top:number, width:number, height:number}>}
95 * rectangles of the desktop shape.
97 remoting.ClientPlugin.HostDesktopImpl.prototype.onShapeUpdated =
98 function(message) {
99 var shapes = base.getArrayAttr(message.data, 'rects');
100 var rects = shapes.map(
101 /** @param {Array<number>} shape */
102 function(shape) {
103 if (!Array.isArray(shape) || shape.length != 4) {
104 throw 'Received invalid onDesktopShape message';
106 var rect = {};
107 rect.left = shape[0];
108 rect.top = shape[1];
109 rect.width = shape[2];
110 rect.height = shape[3];
111 return rect;
114 this.raiseEvent(remoting.HostDesktop.Events.shapeChanged, rects);
115 return rects;
118 }());