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