15
15
#include " atom/browser/web_view_guest_delegate.h"
16
16
#include " atom/common/api/api_messages.h"
17
17
#include " atom/common/api/event_emitter_caller.h"
18
+ #include " atom/common/native_mate_converters/blink_converter.h"
18
19
#include " atom/common/native_mate_converters/callback.h"
19
20
#include " atom/common/native_mate_converters/file_path_converter.h"
20
21
#include " atom/common/native_mate_converters/gfx_converter.h"
27
28
#include " brightray/browser/inspectable_web_contents.h"
28
29
#include " chrome/browser/printing/print_view_manager_basic.h"
29
30
#include " chrome/browser/printing/print_preview_message_handler.h"
31
+ #include " content/common/view_messages.h"
30
32
#include " content/public/browser/favicon_status.h"
33
+ #include " content/public/browser/native_web_keyboard_event.h"
31
34
#include " content/public/browser/navigation_details.h"
32
35
#include " content/public/browser/navigation_entry.h"
33
36
#include " content/public/browser/plugin_service.h"
34
37
#include " content/public/browser/render_frame_host.h"
35
38
#include " content/public/browser/render_process_host.h"
36
39
#include " content/public/browser/render_view_host.h"
40
+ #include " content/public/browser/render_widget_host_view.h"
37
41
#include " content/public/browser/resource_request_details.h"
38
42
#include " content/public/browser/service_worker_context.h"
39
43
#include " content/public/browser/storage_partition.h"
44
48
#include " net/http/http_response_headers.h"
45
49
#include " net/url_request/static_http_user_agent_settings.h"
46
50
#include " net/url_request/url_request_context.h"
51
+ #include " third_party/WebKit/public/web/WebInputEvent.h"
47
52
48
53
#include " atom/common/node_includes.h"
49
54
@@ -115,6 +120,33 @@ struct Converter<WindowOpenDisposition> {
115
120
}
116
121
};
117
122
123
+ template <>
124
+ struct Converter <net::HttpResponseHeaders*> {
125
+ static v8::Local<v8::Value> ToV8 (v8::Isolate* isolate,
126
+ net::HttpResponseHeaders* headers) {
127
+ base::DictionaryValue response_headers;
128
+ if (headers) {
129
+ void * iter = nullptr ;
130
+ std::string key;
131
+ std::string value;
132
+ while (headers->EnumerateHeaderLines (&iter, &key, &value)) {
133
+ key = base::StringToLowerASCII (key);
134
+ value = base::StringToLowerASCII (value);
135
+ if (response_headers.HasKey (key)) {
136
+ base::ListValue* values = nullptr ;
137
+ if (response_headers.GetList (key, &values))
138
+ values->AppendString (value);
139
+ } else {
140
+ scoped_ptr<base::ListValue> values (new base::ListValue ());
141
+ values->AppendString (value);
142
+ response_headers.Set (key, values.Pass ());
143
+ }
144
+ }
145
+ }
146
+ return ConvertToV8 (isolate, response_headers);
147
+ }
148
+ };
149
+
118
150
} // namespace mate
119
151
120
152
@@ -126,7 +158,7 @@ namespace {
126
158
127
159
v8::Persistent<v8::ObjectTemplate> template_;
128
160
129
- // The wrapWebContents funtion which is implemented in JavaScript
161
+ // The wrapWebContents function which is implemented in JavaScript
130
162
using WrapWebContentsCallback = base::Callback<void (v8::Local<v8::Value>)>;
131
163
WrapWebContentsCallback g_wrap_web_contents;
132
164
@@ -196,7 +228,7 @@ WebContents::WebContents(v8::Isolate* isolate,
196
228
AttachAsUserData (web_contents);
197
229
InitWithWebContents (web_contents);
198
230
199
- // Save the preferences.
231
+ // Save the preferences in C++ .
200
232
base::DictionaryValue web_preferences;
201
233
mate::ConvertFromV8 (isolate, options.GetHandle (), &web_preferences);
202
234
new WebContentsPreferences (web_contents, &web_preferences);
@@ -409,38 +441,14 @@ void WebContents::DidStopLoading() {
409
441
410
442
void WebContents::DidGetResourceResponseStart (
411
443
const content::ResourceRequestDetails& details) {
412
- v8::Locker locker (isolate ());
413
- v8::HandleScope handle_scope (isolate ());
414
- base::DictionaryValue response_headers;
415
-
416
- net::HttpResponseHeaders* headers = details.headers .get ();
417
- if (!headers)
418
- return ;
419
- void * iter = nullptr ;
420
- std::string key;
421
- std::string value;
422
- while (headers->EnumerateHeaderLines (&iter, &key, &value)) {
423
- key = base::StringToLowerASCII (key);
424
- value = base::StringToLowerASCII (value);
425
- if (response_headers.HasKey (key)) {
426
- base::ListValue* values = nullptr ;
427
- if (response_headers.GetList (key, &values))
428
- values->AppendString (value);
429
- } else {
430
- scoped_ptr<base::ListValue> values (new base::ListValue ());
431
- values->AppendString (value);
432
- response_headers.Set (key, values.Pass ());
433
- }
434
- }
435
-
436
444
Emit (" did-get-response-details" ,
437
445
details.socket_address .IsEmpty (),
438
446
details.url ,
439
447
details.original_url ,
440
448
details.http_response_code ,
441
449
details.method ,
442
450
details.referrer ,
443
- response_headers );
451
+ details. headers . get () );
444
452
}
445
453
446
454
void WebContents::DidGetRedirectForResourceRequest (
@@ -449,7 +457,11 @@ void WebContents::DidGetRedirectForResourceRequest(
449
457
Emit (" did-get-redirect-request" ,
450
458
details.url ,
451
459
details.new_url ,
452
- (details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME));
460
+ (details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME),
461
+ details.http_response_code ,
462
+ details.method ,
463
+ details.referrer ,
464
+ details.headers .get ());
453
465
}
454
466
455
467
void WebContents::DidNavigateMainFrame (
@@ -640,6 +652,21 @@ bool WebContents::IsDevToolsOpened() {
640
652
return managed_web_contents ()->IsDevToolsViewShowing ();
641
653
}
642
654
655
+ void WebContents::EnableDeviceEmulation (
656
+ const blink::WebDeviceEmulationParams& params) {
657
+ if (type_ == REMOTE)
658
+ return ;
659
+
660
+ Send (new ViewMsg_EnableDeviceEmulation (routing_id (), params));
661
+ }
662
+
663
+ void WebContents::DisableDeviceEmulation () {
664
+ if (type_ == REMOTE)
665
+ return ;
666
+
667
+ Send (new ViewMsg_DisableDeviceEmulation (routing_id ()));
668
+ }
669
+
643
670
void WebContents::ToggleDevTools () {
644
671
if (IsDevToolsOpened ())
645
672
CloseDevTools ();
@@ -796,6 +823,56 @@ bool WebContents::SendIPCMessage(const base::string16& channel,
796
823
return Send (new AtomViewMsg_Message (routing_id (), channel, args));
797
824
}
798
825
826
+ void WebContents::SendInputEvent (v8::Isolate* isolate,
827
+ v8::Local<v8::Value> input_event) {
828
+ const auto view = web_contents ()->GetRenderWidgetHostView ();
829
+ if (!view)
830
+ return ;
831
+ const auto host = view->GetRenderWidgetHost ();
832
+ if (!host)
833
+ return ;
834
+
835
+ int type = mate::GetWebInputEventType (isolate, input_event);
836
+ if (blink::WebInputEvent::isMouseEventType (type)) {
837
+ blink::WebMouseEvent mouse_event;
838
+ if (mate::ConvertFromV8 (isolate, input_event, &mouse_event)) {
839
+ host->ForwardMouseEvent (mouse_event);
840
+ return ;
841
+ }
842
+ } else if (blink::WebInputEvent::isKeyboardEventType (type)) {
843
+ content::NativeWebKeyboardEvent keyboard_event;;
844
+ if (mate::ConvertFromV8 (isolate, input_event, &keyboard_event)) {
845
+ host->ForwardKeyboardEvent (keyboard_event);
846
+ return ;
847
+ }
848
+ } else if (type == blink::WebInputEvent::MouseWheel) {
849
+ blink::WebMouseWheelEvent mouse_wheel_event;
850
+ if (mate::ConvertFromV8 (isolate, input_event, &mouse_wheel_event)) {
851
+ host->ForwardWheelEvent (mouse_wheel_event);
852
+ return ;
853
+ }
854
+ }
855
+
856
+ isolate->ThrowException (v8::Exception::Error (mate::StringToV8 (
857
+ isolate, " Invalid event object" )));
858
+ }
859
+
860
+ void WebContents::BeginFrameSubscription (
861
+ const FrameSubscriber::FrameCaptureCallback& callback) {
862
+ const auto view = web_contents ()->GetRenderWidgetHostView ();
863
+ if (view) {
864
+ scoped_ptr<FrameSubscriber> frame_subscriber (new FrameSubscriber (
865
+ isolate (), view->GetVisibleViewportSize (), callback));
866
+ view->BeginFrameSubscription (frame_subscriber.Pass ());
867
+ }
868
+ }
869
+
870
+ void WebContents::EndFrameSubscription () {
871
+ const auto view = web_contents ()->GetRenderWidgetHostView ();
872
+ if (view)
873
+ view->EndFrameSubscription ();
874
+ }
875
+
799
876
void WebContents::SetSize (const SetSizeParams& params) {
800
877
if (guest_delegate_)
801
878
guest_delegate_->SetSize (params);
@@ -810,6 +887,12 @@ bool WebContents::IsGuest() const {
810
887
return type_ == WEB_VIEW;
811
888
}
812
889
890
+ v8::Local<v8::Value> WebContents::GetWebPreferences (v8::Isolate* isolate) {
891
+ WebContentsPreferences* web_preferences =
892
+ WebContentsPreferences::FromWebContents (web_contents ());
893
+ return mate::ConvertToV8 (isolate, *web_preferences->web_preferences ());
894
+ }
895
+
813
896
mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder (
814
897
v8::Isolate* isolate) {
815
898
if (template_.IsEmpty ())
@@ -836,6 +919,10 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
836
919
.SetMethod (" openDevTools" , &WebContents::OpenDevTools)
837
920
.SetMethod (" closeDevTools" , &WebContents::CloseDevTools)
838
921
.SetMethod (" isDevToolsOpened" , &WebContents::IsDevToolsOpened)
922
+ .SetMethod (" enableDeviceEmulation" ,
923
+ &WebContents::EnableDeviceEmulation)
924
+ .SetMethod (" disableDeviceEmulation" ,
925
+ &WebContents::DisableDeviceEmulation)
839
926
.SetMethod (" toggleDevTools" , &WebContents::ToggleDevTools)
840
927
.SetMethod (" inspectElement" , &WebContents::InspectElement)
841
928
.SetMethod (" setAudioMuted" , &WebContents::SetAudioMuted)
@@ -854,9 +941,14 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
854
941
.SetMethod (" focus" , &WebContents::Focus)
855
942
.SetMethod (" tabTraverse" , &WebContents::TabTraverse)
856
943
.SetMethod (" _send" , &WebContents::SendIPCMessage, true )
944
+ .SetMethod (" sendInputEvent" , &WebContents::SendInputEvent)
945
+ .SetMethod (" beginFrameSubscription" ,
946
+ &WebContents::BeginFrameSubscription)
947
+ .SetMethod (" endFrameSubscription" , &WebContents::EndFrameSubscription)
857
948
.SetMethod (" setSize" , &WebContents::SetSize)
858
949
.SetMethod (" setAllowTransparency" , &WebContents::SetAllowTransparency)
859
950
.SetMethod (" isGuest" , &WebContents::IsGuest)
951
+ .SetMethod (" getWebPreferences" , &WebContents::GetWebPreferences)
860
952
.SetMethod (" hasServiceWorker" , &WebContents::HasServiceWorker)
861
953
.SetMethod (" unregisterServiceWorker" ,
862
954
&WebContents::UnregisterServiceWorker)
@@ -910,7 +1002,7 @@ mate::Handle<WebContents> WebContents::CreateFrom(
910
1002
// static
911
1003
mate::Handle <WebContents> WebContents::Create (
912
1004
v8::Isolate* isolate, const mate::Dictionary& options) {
913
- auto handle = mate::CreateHandle (isolate, new WebContents (isolate, options));
1005
+ auto handle = mate::CreateHandle (isolate, new WebContents (isolate, options));
914
1006
g_wrap_web_contents.Run (handle.ToV8 ());
915
1007
return handle;
916
1008
}
0 commit comments