| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "webkit/glue/plugins/plugin_instance.h" | |
| 9 | |
| 10 namespace NPAPI { | |
| 11 | |
| 12 PluginStream::PluginStream( | |
| 13 PluginInstance *instance, | |
| 14 const char *url, | |
| 15 bool need_notify, | |
| 16 void *notify_data) | |
| 17 : instance_(instance), | |
| 18 notify_needed_(need_notify), | |
| 19 notify_data_(notify_data), | |
| 20 close_on_write_data_(false), | |
| 21 opened_(false), | |
| 22 requested_plugin_mode_(NP_NORMAL), | |
| 23 temp_file_handle_(INVALID_HANDLE_VALUE), | |
| 24 seekable_stream_(false), | |
| 25 data_offset_(0) { | |
| 26 memset(&stream_, 0, sizeof(stream_)); | |
| 27 stream_.url = _strdup(url); | |
| 28 temp_file_name_[0] = '\0'; | |
| 29 } | |
| 30 | |
| 31 void PluginStream::UpdateUrl(const char* url) { | |
| 32 DCHECK(!opened_); | |
| 33 free(const_cast<char*>(stream_.url)); | |
| 34 stream_.url = _strdup(url); | |
| 35 pending_redirect_url_.clear(); | |
| 36 } | |
| 37 | |
| 38 void PluginStream::WriteAsFile() { | |
| 39 if (requested_plugin_mode_ == NP_ASFILE || | |
| 40 requested_plugin_mode_ == NP_ASFILEONLY) | |
| 41 instance_->NPP_StreamAsFile(&stream_, temp_file_name_); | |
| 42 } | |
| 43 | |
| 44 size_t PluginStream::WriteBytes(const char *buf, size_t length) { | |
| 45 DWORD bytes; | |
| 46 | |
| 47 if (!WriteFile(temp_file_handle_, buf, length, &bytes, 0)) | |
| 48 return 0U; | |
| 49 | |
| 50 return static_cast<size_t>(bytes); | |
| 51 } | |
| 52 | |
| 53 bool PluginStream::OpenTempFile() { | |
| 54 DCHECK(temp_file_handle_ == INVALID_HANDLE_VALUE); | |
| 55 | |
| 56 // The reason for using all the Ascii versions of these filesystem | |
| 57 // calls is that the filename which we pass back to the plugin | |
| 58 // via NPAPI is an ascii filename. Otherwise, we'd use wide-chars. | |
| 59 // | |
| 60 // TODO: | |
| 61 // This is a bug in NPAPI itself, and it needs to be fixed. | |
| 62 // The case which will fail is if a user has a multibyte name, | |
| 63 // but has the system locale set to english. GetTempPathA will | |
| 64 // return junk in this case, causing us to be unable to open the | |
| 65 // file. | |
| 66 | |
| 67 char temp_directory[MAX_PATH]; | |
| 68 if (GetTempPathA(MAX_PATH, temp_directory) == 0) | |
| 69 return false; | |
| 70 if (GetTempFileNameA(temp_directory, "npstream", 0, temp_file_name_) == 0) | |
| 71 return false; | |
| 72 temp_file_handle_ = CreateFileA(temp_file_name_, | |
| 73 FILE_ALL_ACCESS, | |
| 74 FILE_SHARE_READ, | |
| 75 0, | |
| 76 CREATE_ALWAYS, | |
| 77 FILE_ATTRIBUTE_NORMAL, | |
| 78 0); | |
| 79 if (temp_file_handle_ == INVALID_HANDLE_VALUE) { | |
| 80 temp_file_name_[0] = '\0'; | |
| 81 return false; | |
| 82 } | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 void PluginStream::CloseTempFile() { | |
| 87 if (temp_file_handle_ != INVALID_HANDLE_VALUE) { | |
| 88 CloseHandle(temp_file_handle_); | |
| 89 temp_file_handle_ = INVALID_HANDLE_VALUE; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 bool PluginStream::TempFileIsValid() { | |
| 94 return temp_file_handle_ != INVALID_HANDLE_VALUE; | |
| 95 } | |
| 96 | |
| 97 } // namespace NPAPI | |
| OLD | NEW |