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.
5 #include "content/child/npapi/plugin_stream.h"
7 #include "base/logging.h"
8 #include "content/child/npapi/plugin_instance.h"
12 void PluginStream::ResetTempFileHandle() {
13 temp_file_handle_ = INVALID_HANDLE_VALUE;
16 void PluginStream::ResetTempFileName() {
17 temp_file_name_[0] = '\0';
20 void PluginStream::WriteAsFile() {
21 if (RequestedPluginModeIsAsFile())
22 instance_->NPP_StreamAsFile(&stream_, temp_file_name_);
25 size_t PluginStream::WriteBytes(const char *buf, size_t length) {
28 if (!WriteFile(temp_file_handle_, buf, length, &bytes, 0))
31 return static_cast<size_t>(bytes);
34 bool PluginStream::OpenTempFile() {
35 DCHECK_EQ(INVALID_HANDLE_VALUE, temp_file_handle_);
37 // The reason for using all the Ascii versions of these filesystem
38 // calls is that the filename which we pass back to the plugin
39 // via NPAPI is an ascii filename. Otherwise, we'd use wide-chars.
42 // This is a bug in NPAPI itself, and it needs to be fixed.
43 // The case which will fail is if a user has a multibyte name,
44 // but has the system locale set to english. GetTempPathA will
45 // return junk in this case, causing us to be unable to open the
48 char temp_directory[MAX_PATH];
49 if (GetTempPathA(MAX_PATH, temp_directory) == 0)
51 if (GetTempFileNameA(temp_directory, "npstream", 0, temp_file_name_) == 0)
53 temp_file_handle_ = CreateFileA(temp_file_name_,
58 FILE_ATTRIBUTE_NORMAL,
60 if (temp_file_handle_ == INVALID_HANDLE_VALUE) {
67 void PluginStream::CloseTempFile() {
68 if (!TempFileIsValid())
71 CloseHandle(temp_file_handle_);
72 ResetTempFileHandle();
75 bool PluginStream::TempFileIsValid() const {
76 return temp_file_handle_ != INVALID_HANDLE_VALUE;
79 } // namespace content