forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.cc
More file actions
179 lines (152 loc) · 6.4 KB
/
screen.cc
File metadata and controls
179 lines (152 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2014 Jefry Tedjokusumo <jtg_gg@yahoo.com.sg>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
// pies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in al
// l copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "base/values.h"
#include "content/nw/src/api/screen/screen.h"
#include "content/nw/src/api/screen/desktop_capture_api.h"
#include "content/nw/src/api/dispatcher_host.h"
#include "content/nw/src/api/event/event.h"
#include "content/nw/src/nw_shell.h"
#include "ui/gfx/display_observer.h"
#include "ui/gfx/screen.h"
namespace nwapi {
std::string DisplayToJSON(const gfx::Display& display) {
std::stringstream ret;
gfx::Rect rect = display.bounds();
ret << "{\"id\":" << display.id();
ret << ",\"bounds\":{\"x\":" << rect.x()
<< ", \"y\":" << rect.y()
<< ", \"width\":" << rect.width()
<< ", \"height\":" << rect.height() << "}";
rect = display.work_area();
ret << ",\"work_area\":{\"x\":" << rect.x()
<< ", \"y\":" << rect.y()
<< ", \"width\":" << rect.width()
<< ", \"height\":" << rect.height() << "}";
ret << ",\"scaleFactor\":" << display.device_scale_factor();
ret << ",\"isBuiltIn\":" << (display.IsInternal() ? "true" : "false");
ret << ",\"rotation\":" << display.RotationAsDegree();
ret << ",\"touchSupport\":" << display.touch_support();
ret << "}";
return ret.str();
}
class JavaScriptDisplayObserver : BaseEvent, public gfx::DisplayObserver {
friend class EventListener;
EventListener* object_;
gfx::Screen* screen_;
// Called when the |display|'s bound has changed.
void OnDisplayMetricsChanged(const gfx::Display& display, uint32_t changed_metrics) override {
base::ListValue arguments;
arguments.AppendString(DisplayToJSON(display));
arguments.AppendInteger(changed_metrics);
object_->dispatcher_host()->SendEvent(object_, "displayBoundsChanged", arguments);
}
// Called when |new_display| has been added.
void OnDisplayAdded(const gfx::Display& new_display) override {
base::ListValue arguments;
arguments.AppendString(DisplayToJSON(new_display));
object_->dispatcher_host()->SendEvent(object_, "displayAdded", arguments);
}
// Called when |old_display| has been removed.
void OnDisplayRemoved(const gfx::Display& old_display) override {
base::ListValue arguments;
arguments.AppendString(DisplayToJSON(old_display));
object_->dispatcher_host()->SendEvent(object_, "displayRemoved", arguments);
}
static const int id;
JavaScriptDisplayObserver(EventListener* object) : object_(object), screen_(NULL){
}
~JavaScriptDisplayObserver() override {
if(screen_)
screen_->RemoveObserver(this);
}
public:
void setScreen(gfx::Screen* screen) {
if(screen_) screen_->RemoveObserver(this);
screen_ = screen;
if(screen_) screen_->AddObserver(this);
}
};
const int JavaScriptDisplayObserver::id = EventListener::getUID();
scoped_refptr<DesktopCaptureChooseDesktopMediaFunction> gpDCCDMF;
void ChooseDesktopMediaCallback(EventListener* event_listener,
ExtensionFunction::ResponseType type,
const base::ListValue& results,
const std::string& error) {
event_listener->dispatcher_host()->SendEvent(event_listener, "chooseDesktopMedia", results);
gpDCCDMF = NULL;
}
// static
void Screen::Call(DispatcherHost* dispatcher_host,
const std::string& method,
const base::ListValue& arguments,
base::ListValue* result) {
if (method == "GetScreens") {
std::stringstream ret;
const std::vector<gfx::Display>& displays = gfx::Screen::GetNativeScreen()->GetAllDisplays();
if (displays.size() == 0) {
result->AppendString("{}");
return;
}
for (size_t i=0; i<displays.size(); i++) {
if(i!=0) ret << ",";
ret << DisplayToJSON(displays[i]);
}
result->AppendString("["+ret.str()+"]");
return;
} else if (method == "AddScreenChangeCallback") {
int object_id = 0;
arguments.GetInteger(0, &object_id);
EventListener* event_listener = dispatcher_host->GetApiObject<EventListener>(object_id);
JavaScriptDisplayObserver* listener = event_listener->AddListener<JavaScriptDisplayObserver>();
if (listener) listener->setScreen(gfx::Screen::GetNativeScreen());
result->AppendBoolean(listener != NULL);
return;
} else if (method == "RemoveScreenChangeCallback") {
int object_id = 0;
arguments.GetInteger(0, &object_id);
EventListener* event_listener = dispatcher_host->GetApiObject<EventListener>(object_id);
bool res = event_listener->RemoveListener<JavaScriptDisplayObserver>();
result->AppendBoolean(res);
return;
} else if (method == "ChooseDesktopMedia") {
if (gpDCCDMF == NULL) {
gpDCCDMF = new DesktopCaptureChooseDesktopMediaFunction();
gpDCCDMF->SetArgs(&arguments);
gpDCCDMF->SetRenderViewHost(dispatcher_host->render_view_host());
int object_id = 0;
arguments.GetInteger(0, &object_id);
EventListener* event_listener = dispatcher_host->GetApiObject<EventListener>(object_id);
ExtensionFunction::ResponseCallback callback = base::Bind(&ChooseDesktopMediaCallback, event_listener);
gpDCCDMF->set_response_callback(callback);
result->AppendBoolean(gpDCCDMF->RunSync());
} else {
// Screen Picker GUI is still active, return false;
result->AppendBoolean(false);
}
} else if (method == "CancelChooseDesktopMedia") {
if (gpDCCDMF) {
gpDCCDMF->Cancel();
gpDCCDMF = NULL;
result->AppendBoolean(true);
} else {
result->AppendBoolean(false);
}
}
}
} // namespace nwapi