| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/plugins/npapi/plugin_stream_url.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "net/http/http_response_headers.h" | |
| 10 #include "webkit/plugins/npapi/plugin_host.h" | |
| 11 #include "webkit/plugins/npapi/plugin_instance.h" | |
| 12 #include "webkit/plugins/npapi/plugin_lib.h" | |
| 13 #include "webkit/plugins/npapi/webplugin.h" | |
| 14 | |
| 15 namespace webkit { | |
| 16 namespace npapi { | |
| 17 | |
| 18 PluginStreamUrl::PluginStreamUrl( | |
| 19 unsigned long resource_id, | |
| 20 const GURL &url, | |
| 21 PluginInstance *instance, | |
| 22 bool notify_needed, | |
| 23 void *notify_data) | |
| 24 : PluginStream(instance, url.spec().c_str(), notify_needed, notify_data), | |
| 25 url_(url), | |
| 26 id_(resource_id) { | |
| 27 } | |
| 28 | |
| 29 bool PluginStreamUrl::Close(NPReason reason) { | |
| 30 // Protect the stream against it being destroyed or the whole plugin instance | |
| 31 // being destroyed within the destroy stream handler. | |
| 32 scoped_refptr<PluginStream> protect(this); | |
| 33 CancelRequest(); | |
| 34 bool result = PluginStream::Close(reason); | |
| 35 instance()->RemoveStream(this); | |
| 36 return result; | |
| 37 } | |
| 38 | |
| 39 WebPluginResourceClient* PluginStreamUrl::AsResourceClient() { | |
| 40 return static_cast<WebPluginResourceClient*>(this); | |
| 41 } | |
| 42 | |
| 43 void PluginStreamUrl::CancelRequest() { | |
| 44 if (id_ > 0) { | |
| 45 if (instance()->webplugin()) { | |
| 46 instance()->webplugin()->CancelResource(id_); | |
| 47 } | |
| 48 id_ = 0; | |
| 49 } | |
| 50 if (instance()->webplugin()) { | |
| 51 for (size_t i = 0; i < range_requests_.size(); ++i) | |
| 52 instance()->webplugin()->CancelResource(range_requests_[i]); | |
| 53 } | |
| 54 range_requests_.clear(); | |
| 55 } | |
| 56 | |
| 57 void PluginStreamUrl::WillSendRequest(const GURL& url, int http_status_code) { | |
| 58 if (notify_needed()) { | |
| 59 // If the plugin participates in HTTP url redirect handling then notify it. | |
| 60 if (net::HttpResponseHeaders::IsRedirectResponseCode(http_status_code) && | |
| 61 instance()->handles_url_redirects()) { | |
| 62 pending_redirect_url_ = url.spec(); | |
| 63 instance()->NPP_URLRedirectNotify(url.spec().c_str(), http_status_code, | |
| 64 notify_data()); | |
| 65 return; | |
| 66 } | |
| 67 } | |
| 68 url_ = url; | |
| 69 UpdateUrl(url.spec().c_str()); | |
| 70 } | |
| 71 | |
| 72 void PluginStreamUrl::DidReceiveResponse(const std::string& mime_type, | |
| 73 const std::string& headers, | |
| 74 uint32 expected_length, | |
| 75 uint32 last_modified, | |
| 76 bool request_is_seekable) { | |
| 77 // Protect the stream against it being destroyed or the whole plugin instance | |
| 78 // being destroyed within the new stream handler. | |
| 79 scoped_refptr<PluginStream> protect(this); | |
| 80 | |
| 81 bool opened = Open(mime_type, | |
| 82 headers, | |
| 83 expected_length, | |
| 84 last_modified, | |
| 85 request_is_seekable); | |
| 86 if (!opened) { | |
| 87 CancelRequest(); | |
| 88 instance()->RemoveStream(this); | |
| 89 } else { | |
| 90 SetDeferLoading(false); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void PluginStreamUrl::DidReceiveData(const char* buffer, int length, | |
| 95 int data_offset) { | |
| 96 if (!open()) | |
| 97 return; | |
| 98 | |
| 99 // Protect the stream against it being destroyed or the whole plugin instance | |
| 100 // being destroyed within the write handlers | |
| 101 scoped_refptr<PluginStream> protect(this); | |
| 102 | |
| 103 if (length > 0) { | |
| 104 // The PluginStreamUrl instance could get deleted if the plugin fails to | |
| 105 // accept data in NPP_Write. | |
| 106 if (Write(const_cast<char*>(buffer), length, data_offset) > 0) { | |
| 107 SetDeferLoading(false); | |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void PluginStreamUrl::DidFinishLoading(unsigned long resource_id) { | |
| 113 if (!seekable()) { | |
| 114 Close(NPRES_DONE); | |
| 115 } else { | |
| 116 std::vector<unsigned long>::iterator it_resource = std::find( | |
| 117 range_requests_.begin(), | |
| 118 range_requests_.end(), | |
| 119 resource_id); | |
| 120 // Resource id must be known to us - either main resource id, or one | |
| 121 // of the resources, created for range requests. | |
| 122 DCHECK(resource_id == id_ || it_resource != range_requests_.end()); | |
| 123 // We should notify the plugin about failed/finished requests to ensure | |
| 124 // that the number of active resource clients does not continue to grow. | |
| 125 if (instance()->webplugin()) | |
| 126 instance()->webplugin()->CancelResource(resource_id); | |
| 127 if (it_resource != range_requests_.end()) | |
| 128 range_requests_.erase(it_resource); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void PluginStreamUrl::DidFail(unsigned long resource_id) { | |
| 133 Close(NPRES_NETWORK_ERR); | |
| 134 } | |
| 135 | |
| 136 bool PluginStreamUrl::IsMultiByteResponseExpected() { | |
| 137 return seekable(); | |
| 138 } | |
| 139 | |
| 140 int PluginStreamUrl::ResourceId() { | |
| 141 return id_; | |
| 142 } | |
| 143 | |
| 144 PluginStreamUrl::~PluginStreamUrl() { | |
| 145 if (instance() && instance()->webplugin()) { | |
| 146 instance()->webplugin()->ResourceClientDeleted(AsResourceClient()); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void PluginStreamUrl::AddRangeRequestResourceId(unsigned long resource_id) { | |
| 151 DCHECK_NE(resource_id, 0u); | |
| 152 range_requests_.push_back(resource_id); | |
| 153 } | |
| 154 | |
| 155 void PluginStreamUrl::SetDeferLoading(bool value) { | |
| 156 if (id_ > 0) | |
| 157 instance()->webplugin()->SetDeferResourceLoading(id_, value); | |
| 158 for (size_t i = 0; i < range_requests_.size(); ++i) | |
| 159 instance()->webplugin()->SetDeferResourceLoading(range_requests_[i], | |
| 160 value); | |
| 161 } | |
| 162 | |
| 163 } // namespace npapi | |
| 164 } // namespace webkit | |
| OLD | NEW |