Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / child / npapi / plugin_stream_win.cc
blobaf55f2f18bbc9fff24bf69d948225dac433b30d3
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"
10 namespace content {
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) {
26 DWORD bytes;
28 if (!WriteFile(temp_file_handle_, buf, length, &bytes, 0))
29 return 0U;
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.
41 // TODO:
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
46 // file.
48 char temp_directory[MAX_PATH];
49 if (GetTempPathA(MAX_PATH, temp_directory) == 0)
50 return false;
51 if (GetTempFileNameA(temp_directory, "npstream", 0, temp_file_name_) == 0)
52 return false;
53 temp_file_handle_ = CreateFileA(temp_file_name_,
54 FILE_ALL_ACCESS,
55 FILE_SHARE_READ,
57 CREATE_ALWAYS,
58 FILE_ATTRIBUTE_NORMAL,
59 0);
60 if (temp_file_handle_ == INVALID_HANDLE_VALUE) {
61 ResetTempFileName();
62 return false;
64 return true;
67 void PluginStream::CloseTempFile() {
68 if (!TempFileIsValid())
69 return;
71 CloseHandle(temp_file_handle_);
72 ResetTempFileHandle();
75 bool PluginStream::TempFileIsValid() const {
76 return temp_file_handle_ != INVALID_HANDLE_VALUE;
79 } // namespace content