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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
|
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/gfx/rect.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "net/base/escape.h"
#include "skia/ext/platform_canvas.h"
#include "webkit/api/public/WebConsoleMessage.h"
#include "webkit/api/public/WebCString.h"
#include "webkit/api/public/WebCursorInfo.h"
#include "webkit/api/public/WebData.h"
#include "webkit/api/public/WebFrame.h"
#include "webkit/api/public/WebHTTPBody.h"
#include "webkit/api/public/WebHTTPHeaderVisitor.h"
#include "webkit/api/public/WebInputEvent.h"
#include "webkit/api/public/WebKit.h"
#include "webkit/api/public/WebKitClient.h"
#include "webkit/api/public/WebPluginContainer.h"
#include "webkit/api/public/WebPluginParams.h"
#include "webkit/api/public/WebRect.h"
#include "webkit/api/public/WebURL.h"
#include "webkit/api/public/WebURLLoader.h"
#include "webkit/api/public/WebURLLoaderClient.h"
#include "webkit/api/public/WebURLResponse.h"
#include "webkit/glue/multipart_response_delegate.h"
#include "webkit/glue/webplugin_impl.h"
#include "webkit/glue/plugins/plugin_host.h"
#include "webkit/glue/plugins/plugin_instance.h"
#include "webkit/glue/webplugin_delegate.h"
#include "webkit/glue/webplugin_page_delegate.h"
#include "webkit/glue/webview.h"
#include "googleurl/src/gurl.h"
using WebKit::WebCanvas;
using WebKit::WebConsoleMessage;
using WebKit::WebCString;
using WebKit::WebCursorInfo;
using WebKit::WebData;
using WebKit::WebDataSource;
using WebKit::WebFrame;
using WebKit::WebHTTPBody;
using WebKit::WebHTTPHeaderVisitor;
using WebKit::WebInputEvent;
using WebKit::WebKeyboardEvent;
using WebKit::WebMouseEvent;
using WebKit::WebPluginContainer;
using WebKit::WebPluginParams;
using WebKit::WebRect;
using WebKit::WebString;
using WebKit::WebURL;
using WebKit::WebURLError;
using WebKit::WebURLLoader;
using WebKit::WebURLLoaderClient;
using WebKit::WebURLRequest;
using WebKit::WebURLResponse;
using WebKit::WebVector;
using webkit_glue::MultipartResponseDelegate;
namespace webkit_glue {
namespace {
// This class handles individual multipart responses. It is instantiated when
// we receive HTTP status code 206 in the HTTP response. This indicates
// that the response could have multiple parts each separated by a boundary
// specified in the response header.
class MultiPartResponseClient : public WebURLLoaderClient {
public:
MultiPartResponseClient(WebPluginResourceClient* resource_client)
: resource_client_(resource_client) {
Clear();
}
virtual void willSendRequest(
WebURLLoader*, WebURLRequest&, const WebURLResponse&) {}
virtual void didSendData(
WebURLLoader*, unsigned long long, unsigned long long) {}
// Called when the multipart parser encounters an embedded multipart
// response.
virtual void didReceiveResponse(
WebURLLoader*, const WebURLResponse& response) {
if (!MultipartResponseDelegate::ReadContentRanges(
response,
&byte_range_lower_bound_,
&byte_range_upper_bound_)) {
NOTREACHED();
return;
}
resource_response_ = response;
}
// Receives individual part data from a multipart response.
virtual void didReceiveData(
WebURLLoader*, const char* data, int data_size, long long) {
// TODO(ananta)
// We should defer further loads on multipart resources on the same lines
// as regular resources requested by plugins to prevent reentrancy.
resource_client_->DidReceiveData(
data, data_size, byte_range_lower_bound_);
}
virtual void didFinishLoading(WebURLLoader*) {}
virtual void didFail(WebURLLoader*, const WebURLError&) {}
void Clear() {
resource_response_.reset();
byte_range_lower_bound_ = 0;
byte_range_upper_bound_ = 0;
}
private:
WebURLResponse resource_response_;
// The lower bound of the byte range.
int byte_range_lower_bound_;
// The upper bound of the byte range.
int byte_range_upper_bound_;
// The handler for the data.
WebPluginResourceClient* resource_client_;
};
class HeaderFlattener : public WebHTTPHeaderVisitor {
public:
HeaderFlattener(std::string* buf) : buf_(buf) {
}
virtual void visitHeader(const WebString& name, const WebString& value) {
// TODO(darin): Should we really exclude headers with an empty value?
if (!name.isEmpty() && !value.isEmpty()) {
buf_->append(name.utf8());
buf_->append(": ");
buf_->append(value.utf8());
buf_->append("\n");
}
}
private:
std::string* buf_;
};
std::string GetAllHeaders(const WebURLResponse& response) {
// TODO(darin): It is possible for httpStatusText to be empty and still have
// an interesting response, so this check seems wrong.
std::string result;
const WebString& status = response.httpStatusText();
if (status.isEmpty())
return result;
// TODO(darin): Shouldn't we also report HTTP version numbers?
result.append("HTTP ");
result.append(WideToUTF8(FormatNumber(response.httpStatusCode())));
result.append(" ");
result.append(status.utf8());
result.append("\n");
HeaderFlattener flattener(&result);
response.visitHTTPHeaderFields(&flattener);
return result;
}
struct ResponseInfo {
GURL url;
std::string mime_type;
uint32 last_modified;
uint32 expected_length;
};
void GetResponseInfo(const WebURLResponse& response,
ResponseInfo* response_info) {
response_info->url = response.url();
response_info->mime_type = response.mimeType().utf8();
// Measured in seconds since 12:00 midnight GMT, January 1, 1970.
response_info->last_modified =
static_cast<uint32>(response.lastModifiedDate());
// If the length comes in as -1, then it indicates that it was not
// read off the HTTP headers. We replicate Safari webkit behavior here,
// which is to set it to 0.
response_info->expected_length =
static_cast<uint32>(std::max(response.expectedContentLength(), 0LL));
WebString content_encoding =
response.httpHeaderField(WebString::fromUTF8("Content-Encoding"));
if (!content_encoding.isNull() &&
!EqualsASCII(content_encoding, "identity")) {
// Don't send the compressed content length to the plugin, which only
// cares about the decoded length.
response_info->expected_length = 0;
}
}
} // namespace
// WebKit::WebPlugin ----------------------------------------------------------
bool WebPluginImpl::initialize(WebPluginContainer* container) {
if (!page_delegate_)
return false;
std::string actual_mime_type;
WebPluginDelegate* plugin_delegate = page_delegate_->CreatePluginDelegate(
plugin_url_, mime_type_, &actual_mime_type);
if (!plugin_delegate)
return NULL;
// Set the container before Initialize because the plugin may
// synchronously call NPN_GetValue to get its container during its
// initialization.
SetContainer(container);
bool ok = plugin_delegate->Initialize(
plugin_url_, arg_names_, arg_values_, this, load_manually_);
if (!ok) {
plugin_delegate->PluginDestroyed();
return false;
}
if (!actual_mime_type.empty())
mime_type_ = actual_mime_type;
delegate_ = plugin_delegate;
return true;
}
void WebPluginImpl::destroy() {
SetContainer(NULL);
MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
NPObject* WebPluginImpl::scriptableObject() {
return delegate_->GetPluginScriptableObject();
}
void WebPluginImpl::paint(WebCanvas* canvas, const WebRect& paint_rect) {
if (!delegate_)
return;
// Note that |context| is only used when in windowless mode.
#if WEBKIT_USING_SKIA
gfx::NativeDrawingContext context = canvas->beginPlatformPaint();
#elif WEBKIT_USING_CG
gfx::NativeDrawingContext context = canvas;
#endif
delegate_->Paint(context, paint_rect);
#if WEBKIT_USING_SKIA
canvas->endPlatformPaint();
#endif
}
void WebPluginImpl::updateGeometry(
const WebRect& window_rect, const WebRect& clip_rect,
const WebVector<WebRect>& cutout_rects, bool is_visible) {
if (window_ && page_delegate_) {
// Notify the window hosting the plugin (the WebViewDelegate) that
// it needs to adjust the plugin, so that all the HWNDs can be moved
// at the same time.
WebPluginGeometry move;
move.window = window_;
move.window_rect = window_rect;
move.clip_rect = clip_rect;
for (size_t i = 0; i < cutout_rects.size(); ++i)
move.cutout_rects.push_back(cutout_rects[i]);
move.rects_valid = true;
move.visible = is_visible;
page_delegate_->DidMovePlugin(move);
}
if (first_geometry_update_ || window_rect != window_rect_ ||
clip_rect != clip_rect_) {
window_rect_ = window_rect;
clip_rect_ = clip_rect;
// Notify the plugin that its parameters have changed.
delegate_->UpdateGeometry(window_rect_, clip_rect_);
// Initiate a download on the plugin url. This should be done for the
// first update geometry sequence. We need to ensure that the plugin
// receives the geometry update before it starts receiving data.
if (first_geometry_update_) {
first_geometry_update_ = false;
// An empty url corresponds to an EMBED tag with no src attribute.
if (!load_manually_ && plugin_url_.is_valid()) {
// The Flash plugin hangs for a while if it receives data before
// receiving valid plugin geometry. By valid geometry we mean the
// geometry received by a call to setFrameRect in the Webkit
// layout code path. To workaround this issue we download the
// plugin source url on a timer.
MessageLoop::current()->PostDelayedTask(
FROM_HERE, method_factory_.NewRunnableMethod(
&WebPluginImpl::OnDownloadPluginSrcUrl), 0);
}
}
}
}
void WebPluginImpl::updateFocus(bool focused) {
if (focused && windowless_)
delegate_->SetFocus();
}
void WebPluginImpl::updateVisibility(bool visible) {
if (!window_ || !page_delegate_)
return;
WebPluginGeometry move;
move.window = window_;
move.window_rect = gfx::Rect();
move.clip_rect = gfx::Rect();
move.rects_valid = false;
move.visible = visible;
page_delegate_->DidMovePlugin(move);
}
bool WebPluginImpl::acceptsInputEvents() {
return windowless_;
}
bool WebPluginImpl::handleInputEvent(
const WebInputEvent& event, WebCursorInfo& cursor_info) {
return delegate_->HandleInputEvent(event, &cursor_info);
}
void WebPluginImpl::didReceiveResponse(const WebURLResponse& response) {
ignore_response_error_ = false;
ResponseInfo response_info;
GetResponseInfo(response, &response_info);
delegate_->DidReceiveManualResponse(
response_info.url,
response_info.mime_type,
GetAllHeaders(response),
response_info.expected_length,
response_info.last_modified);
}
void WebPluginImpl::didReceiveData(const char* data, int data_length) {
delegate_->DidReceiveManualData(data, data_length);
}
void WebPluginImpl::didFinishLoading() {
delegate_->DidFinishManualLoading();
}
void WebPluginImpl::didFailLoading(const WebURLError& error) {
if (!ignore_response_error_)
delegate_->DidManualLoadFail();
}
void WebPluginImpl::didFinishLoadingFrameRequest(
const WebURL& url, void* notify_data) {
if (delegate_) {
delegate_->DidFinishLoadWithReason(
url, NPRES_DONE, reinterpret_cast<intptr_t>(notify_data));
}
}
void WebPluginImpl::didFailLoadingFrameRequest(
const WebURL& url, void* notify_data, const WebURLError& error) {
// TODO(darin): Map net::ERR_ABORTED to NPRES_USER_BREAK?
if (delegate_) {
delegate_->DidFinishLoadWithReason(
url, NPRES_NETWORK_ERR, reinterpret_cast<intptr_t>(notify_data));
}
}
// -----------------------------------------------------------------------------
WebPluginImpl::WebPluginImpl(
WebFrame* webframe, const WebPluginParams& params,
const base::WeakPtr<WebPluginPageDelegate>& page_delegate)
: windowless_(false),
window_(NULL),
page_delegate_(page_delegate),
webframe_(webframe),
delegate_(NULL),
container_(NULL),
plugin_url_(params.url),
load_manually_(params.loadManually),
first_geometry_update_(true),
ignore_response_error_(false),
mime_type_(params.mimeType.utf8()),
ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
DCHECK_EQ(params.attributeNames.size(), params.attributeValues.size());
StringToLowerASCII(&mime_type_);
for (size_t i = 0; i < params.attributeNames.size(); ++i) {
arg_names_.push_back(params.attributeNames[i].utf8());
arg_values_.push_back(params.attributeValues[i].utf8());
}
}
WebPluginImpl::~WebPluginImpl() {
}
void WebPluginImpl::SetWindow(gfx::PluginWindowHandle window) {
if (window) {
DCHECK(!windowless_); // Make sure not called twice.
window_ = window;
if (page_delegate_) {
// Tell the view delegate that the plugin window was created, so that it
// can create necessary container widgets.
page_delegate_->CreatedPluginWindow(window);
}
} else {
DCHECK(!window_); // Make sure not called twice.
windowless_ = true;
}
}
void WebPluginImpl::WillDestroyWindow(gfx::PluginWindowHandle window) {
DCHECK_EQ(window, window_);
window_ = NULL;
if (page_delegate_)
page_delegate_->WillDestroyPluginWindow(window);
}
GURL WebPluginImpl::CompleteURL(const char* url) {
if (!webframe_) {
NOTREACHED();
return GURL();
}
// TODO(darin): Is conversion from UTF8 correct here?
return webframe_->completeURL(WebString::fromUTF8(url));
}
void WebPluginImpl::CancelResource(int id) {
for (size_t i = 0; i < clients_.size(); ++i) {
if (clients_[i].id == id) {
if (clients_[i].loader.get()) {
clients_[i].loader->setDefersLoading(false);
clients_[i].loader->cancel();
RemoveClient(i);
}
return;
}
}
}
bool WebPluginImpl::SetPostData(WebURLRequest* request,
const char *buf,
uint32 length) {
std::vector<std::string> names;
std::vector<std::string> values;
std::vector<char> body;
bool rv = NPAPI::PluginHost::SetPostData(buf, length, &names, &values, &body);
for (size_t i = 0; i < names.size(); ++i) {
request->addHTTPHeaderField(WebString::fromUTF8(names[i]),
WebString::fromUTF8(values[i]));
}
WebString content_type_header = WebString::fromUTF8("Content-Type");
const WebString& content_type =
request->httpHeaderField(content_type_header);
if (content_type.isEmpty()) {
request->setHTTPHeaderField(
content_type_header,
WebString::fromUTF8("application/x-www-form-urlencoded"));
}
WebHTTPBody http_body;
if (body.size()) {
http_body.initialize();
http_body.appendData(WebData(&body[0], body.size()));
}
request->setHTTPBody(http_body);
return rv;
}
WebPluginImpl::RoutingStatus WebPluginImpl::RouteToFrame(
const char *method,
bool is_javascript_url,
const char* target,
unsigned int len,
const char* buf,
bool is_file_data,
bool notify_needed,
intptr_t notify_data,
const char* url) {
// If there is no target, there is nothing to do
if (!target)
return NOT_ROUTED;
// This could happen if the WebPluginContainer was already deleted.
if (!webframe_)
return NOT_ROUTED;
WebString target_str = WebString::fromUTF8(target);
// Take special action for JavaScript URLs
if (is_javascript_url) {
WebFrame* target_frame = webframe_->view()->findFrameByName(target_str);
// For security reasons, do not allow JavaScript on frames
// other than this frame.
if (target_frame != webframe_) {
// TODO(darin): Localize this message.
const char kMessage[] =
"Ignoring cross-frame javascript URL load requested by plugin.";
webframe_->addMessageToConsole(
WebConsoleMessage(WebConsoleMessage::LevelError,
WebString::fromUTF8(kMessage)));
return ROUTED;
}
// Route javascript calls back to the plugin.
return NOT_ROUTED;
}
// If we got this far, we're routing content to a target frame.
// Go fetch the URL.
GURL complete_url = CompleteURL(url);
if (strcmp(method, "GET") != 0) {
// We're only going to route HTTP/HTTPS requests
if (!(complete_url.SchemeIs("http") || complete_url.SchemeIs("https")))
return INVALID_URL;
}
WebURLRequest request(complete_url);
request.setHTTPMethod(WebString::fromUTF8(method));
if (len > 0) {
if (!is_file_data) {
if (!SetPostData(&request, buf, len)) {
// Uhoh - we're in trouble. There isn't a good way
// to recover at this point. Break out.
NOTREACHED();
return ROUTED;
}
} else {
// TODO: Support "file" mode. For now, just break out
// since proceeding may do something unintentional.
NOTREACHED();
return ROUTED;
}
}
container_->loadFrameRequest(request, target_str, notify_needed,
reinterpret_cast<void*>(notify_data));
return ROUTED;
}
NPObject* WebPluginImpl::GetWindowScriptNPObject() {
if (!webframe_) {
NOTREACHED();
return NULL;
}
return webframe_->windowObject();
}
NPObject* WebPluginImpl::GetPluginElement() {
return container_->scriptableObjectForElement();
}
void WebPluginImpl::SetCookie(const GURL& url,
const GURL& policy_url,
const std::string& cookie) {
WebKit::webKitClient()->setCookies(
url, policy_url, WebString::fromUTF8(cookie));
}
std::string WebPluginImpl::GetCookies(const GURL& url, const GURL& policy_url) {
return UTF16ToUTF8(WebKit::webKitClient()->cookies(url, policy_url));
}
void WebPluginImpl::ShowModalHTMLDialog(const GURL& url, int width, int height,
const std::string& json_arguments,
std::string* json_retval) {
if (page_delegate_) {
page_delegate_->ShowModalHTMLDialogForPlugin(
url, gfx::Size(width, height), json_arguments, json_retval);
}
}
void WebPluginImpl::OnMissingPluginStatus(int status) {
NOTREACHED();
}
void WebPluginImpl::Invalidate() {
if (container_)
container_->invalidate();
}
void WebPluginImpl::InvalidateRect(const gfx::Rect& rect) {
if (container_)
container_->invalidateRect(rect);
}
void WebPluginImpl::OnDownloadPluginSrcUrl() {
HandleURLRequestInternal("GET", false, NULL, 0, NULL, false, false,
plugin_url_.spec().c_str(), NULL, false,
false);
}
WebPluginResourceClient* WebPluginImpl::GetClientFromLoader(
WebURLLoader* loader) {
ClientInfo* client_info = GetClientInfoFromLoader(loader);
if (client_info)
return client_info->client;
return NULL;
}
WebPluginImpl::ClientInfo* WebPluginImpl::GetClientInfoFromLoader(
WebURLLoader* loader) {
for (size_t i = 0; i < clients_.size(); ++i) {
if (clients_[i].loader.get() == loader)
return &clients_[i];
}
NOTREACHED();
return 0;
}
void WebPluginImpl::willSendRequest(WebURLLoader* loader,
WebURLRequest& request,
const WebURLResponse&) {
WebPluginResourceClient* client = GetClientFromLoader(loader);
if (client)
client->WillSendRequest(request.url());
}
void WebPluginImpl::didSendData(WebURLLoader* loader,
unsigned long long bytes_sent,
unsigned long long total_bytes_to_be_sent) {
}
void WebPluginImpl::didReceiveResponse(WebURLLoader* loader,
const WebURLResponse& response) {
static const int kHttpPartialResponseStatusCode = 206;
static const int kHttpResponseSuccessStatusCode = 200;
WebPluginResourceClient* client = GetClientFromLoader(loader);
if (!client)
return;
ResponseInfo response_info;
GetResponseInfo(response, &response_info);
bool request_is_seekable = true;
if (client->IsMultiByteResponseExpected()) {
if (response.httpStatusCode() == kHttpPartialResponseStatusCode) {
HandleHttpMultipartResponse(response, client);
return;
} else if (response.httpStatusCode() == kHttpResponseSuccessStatusCode) {
// If the client issued a byte range request and the server responds with
// HTTP 200 OK, it indicates that the server does not support byte range
// requests.
// We need to emulate Firefox behavior by doing the following:-
// 1. Destroy the plugin instance in the plugin process. Ensure that
// existing resource requests initiated for the plugin instance
// continue to remain valid.
// 2. Create a new plugin instance and notify it about the response
// received here.
if (!ReinitializePluginForResponse(loader)) {
NOTREACHED();
return;
}
// The server does not support byte range requests. No point in creating
// seekable streams.
request_is_seekable = false;
delete client;
client = NULL;
// Create a new resource client for this request.
for (size_t i = 0; i < clients_.size(); ++i) {
if (clients_[i].loader.get() == loader) {
WebPluginResourceClient* resource_client =
delegate_->CreateResourceClient(clients_[i].id, plugin_url_,
false, 0, NULL);
clients_[i].client = resource_client;
client = resource_client;
break;
}
}
DCHECK(client != NULL);
}
}
// Calling into a plugin could result in reentrancy if the plugin yields
// control to the OS like entering a modal loop etc. Prevent this by
// stopping further loading until the plugin notifies us that it is ready to
// accept data
loader->setDefersLoading(true);
client->DidReceiveResponse(
response_info.mime_type,
GetAllHeaders(response),
response_info.expected_length,
response_info.last_modified,
request_is_seekable);
// Bug http://b/issue?id=925559. The flash plugin would not handle the HTTP
// error codes in the stream header and as a result, was unaware of the
// fate of the HTTP requests issued via NPN_GetURLNotify. Webkit and FF
// destroy the stream and invoke the NPP_DestroyStream function on the
// plugin if the HTTP request fails.
const GURL& url = response.url();
if (url.SchemeIs("http") || url.SchemeIs("https")) {
if (response.httpStatusCode() < 100 || response.httpStatusCode() >= 400) {
// The plugin instance could be in the process of deletion here.
// Verify if the WebPluginResourceClient instance still exists before
// use.
ClientInfo* client_info = GetClientInfoFromLoader(loader);
if (client_info) {
client_info->pending_failure_notification = true;
}
}
}
}
void WebPluginImpl::didReceiveData(WebURLLoader* loader,
const char *buffer,
int length, long long) {
WebPluginResourceClient* client = GetClientFromLoader(loader);
if (!client)
return;
MultiPartResponseHandlerMap::iterator index =
multi_part_response_map_.find(client);
if (index != multi_part_response_map_.end()) {
MultipartResponseDelegate* multi_part_handler = (*index).second;
DCHECK(multi_part_handler != NULL);
multi_part_handler->OnReceivedData(buffer, length);
} else {
loader->setDefersLoading(true);
client->DidReceiveData(buffer, length, 0);
}
}
void WebPluginImpl::didFinishLoading(WebURLLoader* loader) {
ClientInfo* client_info = GetClientInfoFromLoader(loader);
if (client_info && client_info->client) {
MultiPartResponseHandlerMap::iterator index =
multi_part_response_map_.find(client_info->client);
if (index != multi_part_response_map_.end()) {
delete (*index).second;
multi_part_response_map_.erase(index);
if (page_delegate_)
page_delegate_->DidStopLoadingForPlugin();
}
loader->setDefersLoading(true);
WebPluginResourceClient* resource_client = client_info->client;
// The ClientInfo can get deleted in the call to DidFinishLoading below.
// It is not safe to access this structure after that.
client_info->client = NULL;
resource_client->DidFinishLoading();
}
}
void WebPluginImpl::didFail(WebURLLoader* loader,
const WebURLError&) {
ClientInfo* client_info = GetClientInfoFromLoader(loader);
if (client_info && client_info->client) {
loader->setDefersLoading(true);
WebPluginResourceClient* resource_client = client_info->client;
// The ClientInfo can get deleted in the call to DidFail below.
// It is not safe to access this structure after that.
client_info->client = NULL;
resource_client->DidFail();
}
}
void WebPluginImpl::RemoveClient(size_t i) {
clients_.erase(clients_.begin() + i);
}
void WebPluginImpl::RemoveClient(WebURLLoader* loader) {
for (size_t i = 0; i < clients_.size(); ++i) {
if (clients_[i].loader.get() == loader) {
RemoveClient(i);
return;
}
}
}
void WebPluginImpl::SetContainer(WebPluginContainer* container) {
if (!container)
TearDownPluginInstance(NULL);
container_ = container;
}
void WebPluginImpl::HandleURLRequest(const char *method,
bool is_javascript_url,
const char* target, unsigned int len,
const char* buf, bool is_file_data,
bool notify, const char* url,
intptr_t notify_data, bool popups_allowed) {
HandleURLRequestInternal(method, is_javascript_url, target, len, buf,
is_file_data, notify, url, notify_data,
popups_allowed, true);
}
void WebPluginImpl::HandleURLRequestInternal(
const char *method, bool is_javascript_url, const char* target,
unsigned int len, const char* buf, bool is_file_data, bool notify,
const char* url, intptr_t notify_data, bool popups_allowed,
bool use_plugin_src_as_referrer) {
// For this request, we either route the output to a frame
// because a target has been specified, or we handle the request
// here, i.e. by executing the script if it is a javascript url
// or by initiating a download on the URL, etc. There is one special
// case in that the request is a javascript url and the target is "_self",
// in which case we route the output to the plugin rather than routing it
// to the plugin's frame.
RoutingStatus routing_status =
RouteToFrame(method, is_javascript_url, target, len, buf, is_file_data,
notify, notify_data, url);
if (routing_status == ROUTED)
return;
if (is_javascript_url) {
GURL gurl(url);
WebString result = container_->executeScriptURL(gurl, popups_allowed);
// delegate_ could be NULL because executeScript caused the container to
// be deleted.
if (delegate_) {
delegate_->SendJavaScriptStream(
gurl, result.utf8(), !result.isNull(), notify, notify_data);
}
} else {
GURL complete_url = CompleteURL(url);
int resource_id = GetNextResourceId();
WebPluginResourceClient* resource_client = delegate_->CreateResourceClient(
resource_id, complete_url, notify, notify_data, NULL);
// If the RouteToFrame call returned a failure then inform the result
// back to the plugin asynchronously.
if ((routing_status == INVALID_URL) ||
(routing_status == GENERAL_FAILURE)) {
resource_client->DidFail();
return;
}
InitiateHTTPRequest(resource_id, resource_client, method, buf, len,
complete_url, NULL, use_plugin_src_as_referrer);
}
}
int WebPluginImpl::GetNextResourceId() {
static int next_id = 0;
return ++next_id;
}
bool WebPluginImpl::InitiateHTTPRequest(int resource_id,
WebPluginResourceClient* client,
const char* method, const char* buf,
int buf_len,
const GURL& url,
const char* range_info,
bool use_plugin_src_as_referrer) {
if (!client) {
NOTREACHED();
return false;
}
ClientInfo info;
info.id = resource_id;
info.client = client;
info.request.initialize();
info.request.setURL(url);
info.request.setRequestorProcessID(delegate_->GetProcessId());
info.request.setTargetType(WebURLRequest::TargetIsObject);
info.request.setHTTPMethod(WebString::fromUTF8(method));
info.pending_failure_notification = false;
if (range_info) {
info.request.addHTTPHeaderField(WebString::fromUTF8("Range"),
WebString::fromUTF8(range_info));
}
if (strcmp(method, "POST") == 0) {
// Adds headers or form data to a request. This must be called before
// we initiate the actual request.
SetPostData(&info.request, buf, buf_len);
}
// GetURL/PostURL requests initiated explicitly by plugins should specify the
// plugin SRC url as the referrer if it is available.
GURL referrer_url;
if (use_plugin_src_as_referrer && !plugin_url_.spec().empty())
referrer_url = plugin_url_;
webframe_->setReferrerForRequest(info.request, referrer_url);
// Sets the routing id to associate the ResourceRequest with the RenderView.
webframe_->dispatchWillSendRequest(info.request);
info.loader.reset(WebKit::webKitClient()->createURLLoader());
if (!info.loader.get())
return false;
info.loader->loadAsynchronously(info.request, this);
clients_.push_back(info);
return true;
}
void WebPluginImpl::CancelDocumentLoad() {
if (webframe_) {
ignore_response_error_ = true;
webframe_->stopLoading();
}
}
void WebPluginImpl::InitiateHTTPRangeRequest(const char* url,
const char* range_info,
intptr_t existing_stream,
bool notify_needed,
intptr_t notify_data) {
int resource_id = GetNextResourceId();
GURL complete_url = CompleteURL(url);
WebPluginResourceClient* resource_client = delegate_->CreateResourceClient(
resource_id, complete_url, notify_needed, notify_data, existing_stream);
InitiateHTTPRequest(
resource_id, resource_client, "GET", NULL, 0, complete_url, range_info,
true);
}
void WebPluginImpl::SetDeferResourceLoading(int resource_id, bool defer) {
std::vector<ClientInfo>::iterator client_index = clients_.begin();
while (client_index != clients_.end()) {
ClientInfo& client_info = *client_index;
if (client_info.id == resource_id) {
client_info.loader->setDefersLoading(defer);
// If we determined that the request had failed via the HTTP headers
// in the response then we send out a failure notification to the
// plugin process, as certain plugins don't handle HTTP failure codes
// correctly.
if (!defer && client_info.client &&
client_info.pending_failure_notification) {
// The ClientInfo and the iterator can become invalid due to the call
// to DidFail below.
WebPluginResourceClient* resource_client = client_info.client;
client_info.loader->cancel();
clients_.erase(client_index++);
resource_client->DidFail();
}
break;
}
client_index++;
}
}
void WebPluginImpl::HandleHttpMultipartResponse(
const WebURLResponse& response, WebPluginResourceClient* client) {
std::string multipart_boundary;
if (!MultipartResponseDelegate::ReadMultipartBoundary(
response, &multipart_boundary)) {
NOTREACHED();
return;
}
if (page_delegate_)
page_delegate_->DidStartLoadingForPlugin();
MultiPartResponseClient* multi_part_response_client =
new MultiPartResponseClient(client);
MultipartResponseDelegate* multi_part_response_handler =
new MultipartResponseDelegate(multi_part_response_client, NULL,
response,
multipart_boundary);
multi_part_response_map_[client] = multi_part_response_handler;
}
bool WebPluginImpl::ReinitializePluginForResponse(
WebURLLoader* loader) {
WebFrame* webframe = webframe_;
if (!webframe)
return false;
WebView* webview = webframe->view();
if (!webview)
return false;
WebPluginContainer* container_widget = container_;
// Destroy the current plugin instance.
TearDownPluginInstance(loader);
container_ = container_widget;
webframe_ = webframe;
std::string actual_mime_type;
WebPluginDelegate* plugin_delegate = page_delegate_->CreatePluginDelegate(
plugin_url_, mime_type_, &actual_mime_type);
bool ok = plugin_delegate->Initialize(
plugin_url_, arg_names_, arg_values_, this, load_manually_);
if (!ok) {
container_ = NULL;
// TODO(iyengar) Should we delete the current plugin instance here?
return false;
}
mime_type_ = actual_mime_type;
delegate_ = plugin_delegate;
// Force a geometry update to occur to ensure that the plugin becomes
// visible.
container_->reportGeometry();
// The plugin move sequences accumulated via DidMove are sent to the browser
// whenever the renderer paints. Force a paint here to ensure that changes
// to the plugin window are propagated to the browser.
container_->invalidate();
return true;
}
void WebPluginImpl::TearDownPluginInstance(
WebURLLoader* loader_to_ignore) {
// The container maintains a list of JSObjects which are related to this
// plugin. Tell the frame we're gone so that it can invalidate all of
// those sub JSObjects.
if (container_)
container_->clearScriptObjects();
if (delegate_) {
// Call PluginDestroyed() first to prevent the plugin from calling us back
// in the middle of tearing down the render tree.
delegate_->PluginDestroyed();
delegate_ = NULL;
}
// Cancel any pending requests because otherwise this deleted object will
// be called by the ResourceDispatcher.
std::vector<ClientInfo>::iterator client_index = clients_.begin();
while (client_index != clients_.end()) {
ClientInfo& client_info = *client_index;
if (loader_to_ignore == client_info.loader) {
client_index++;
continue;
}
if (client_info.loader.get())
client_info.loader->cancel();
client_index = clients_.erase(client_index);
}
// This needs to be called now and not in the destructor since the
// webframe_ might not be valid anymore.
webframe_ = NULL;
method_factory_.RevokeAll();
}
} // namespace webkit_glue
|