OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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. | |
4 | |
5 #include "webkit/glue/plugins/plugin_stream_url.h" | |
6 | |
7 #include "net/http/http_response_headers.h" | |
8 #include "webkit/glue/plugins/plugin_host.h" | |
9 #include "webkit/glue/plugins/plugin_instance.h" | |
10 #include "webkit/glue/plugins/plugin_lib.h" | |
11 #include "webkit/glue/plugins/webplugin.h" | |
12 | |
13 namespace NPAPI { | |
14 | |
15 PluginStreamUrl::PluginStreamUrl( | |
16 unsigned long resource_id, | |
17 const GURL &url, | |
18 PluginInstance *instance, | |
19 bool notify_needed, | |
20 void *notify_data) | |
21 : PluginStream(instance, url.spec().c_str(), notify_needed, notify_data), | |
22 url_(url), | |
23 id_(resource_id) { | |
24 } | |
25 | |
26 PluginStreamUrl::~PluginStreamUrl() { | |
27 if (instance() && instance()->webplugin()) { | |
28 instance()->webplugin()->ResourceClientDeleted(AsResourceClient()); | |
29 } | |
30 } | |
31 | |
32 bool PluginStreamUrl::Close(NPReason reason) { | |
33 // Protect the stream against it being destroyed or the whole plugin instance | |
34 // being destroyed within the destroy stream handler. | |
35 scoped_refptr<PluginStream> protect(this); | |
36 CancelRequest(); | |
37 bool result = PluginStream::Close(reason); | |
38 instance()->RemoveStream(this); | |
39 return result; | |
40 } | |
41 | |
42 webkit_glue::WebPluginResourceClient* PluginStreamUrl::AsResourceClient() { | |
43 return static_cast<webkit_glue::WebPluginResourceClient*>(this); | |
44 } | |
45 | |
46 void PluginStreamUrl::WillSendRequest(const GURL& url, int http_status_code) { | |
47 if (notify_needed()) { | |
48 // If the plugin participates in HTTP url redirect handling then notify it. | |
49 if (net::HttpResponseHeaders::IsRedirectResponseCode(http_status_code) && | |
50 instance()->handles_url_redirects()) { | |
51 pending_redirect_url_ = url.spec(); | |
52 instance()->NPP_URLRedirectNotify(url.spec().c_str(), http_status_code, | |
53 notify_data()); | |
54 return; | |
55 } | |
56 } | |
57 url_ = url; | |
58 UpdateUrl(url.spec().c_str()); | |
59 } | |
60 | |
61 void PluginStreamUrl::DidReceiveResponse(const std::string& mime_type, | |
62 const std::string& headers, | |
63 uint32 expected_length, | |
64 uint32 last_modified, | |
65 bool request_is_seekable) { | |
66 // Protect the stream against it being destroyed or the whole plugin instance | |
67 // being destroyed within the new stream handler. | |
68 scoped_refptr<PluginStream> protect(this); | |
69 | |
70 bool opened = Open(mime_type, | |
71 headers, | |
72 expected_length, | |
73 last_modified, | |
74 request_is_seekable); | |
75 if (!opened) { | |
76 CancelRequest(); | |
77 instance()->RemoveStream(this); | |
78 } else { | |
79 if (id_ > 0) | |
80 instance()->webplugin()->SetDeferResourceLoading(id_, false); | |
81 } | |
82 } | |
83 | |
84 void PluginStreamUrl::DidReceiveData(const char* buffer, int length, | |
85 int data_offset) { | |
86 if (!open()) | |
87 return; | |
88 | |
89 // Protect the stream against it being destroyed or the whole plugin instance | |
90 // being destroyed within the write handlers | |
91 scoped_refptr<PluginStream> protect(this); | |
92 | |
93 if (length > 0) { | |
94 // The PluginStreamUrl instance could get deleted if the plugin fails to | |
95 // accept data in NPP_Write. | |
96 if (Write(const_cast<char*>(buffer), length, data_offset) > 0) { | |
97 if (id_ > 0) | |
98 instance()->webplugin()->SetDeferResourceLoading(id_, false); | |
99 } | |
100 } | |
101 } | |
102 | |
103 void PluginStreamUrl::DidFinishLoading() { | |
104 if (!seekable()) { | |
105 Close(NPRES_DONE); | |
106 } | |
107 } | |
108 | |
109 void PluginStreamUrl::DidFail() { | |
110 Close(NPRES_NETWORK_ERR); | |
111 } | |
112 | |
113 bool PluginStreamUrl::IsMultiByteResponseExpected() { | |
114 return seekable(); | |
115 } | |
116 | |
117 int PluginStreamUrl::ResourceId() { | |
118 return id_; | |
119 } | |
120 | |
121 void PluginStreamUrl::CancelRequest() { | |
122 if (id_ > 0) { | |
123 if (instance()->webplugin()) { | |
124 instance()->webplugin()->CancelResource(id_); | |
125 } | |
126 id_ = 0; | |
127 } | |
128 } | |
129 | |
130 } // namespace NPAPI | |
OLD | NEW |