- add sources.
[platform/framework/web/crosswalk.git] / src / content / child / npapi / plugin_stream_win.cc
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 "content/child/npapi/plugin_stream.h"
6
7 #include "base/logging.h"
8 #include "content/child/npapi/plugin_instance.h"
9
10 namespace content {
11
12 void PluginStream::ResetTempFileHandle() {
13   temp_file_handle_ = INVALID_HANDLE_VALUE;
14 }
15
16 void PluginStream::ResetTempFileName() {
17   temp_file_name_[0] = '\0';
18 }
19
20 void PluginStream::WriteAsFile() {
21   if (RequestedPluginModeIsAsFile())
22     instance_->NPP_StreamAsFile(&stream_, temp_file_name_);
23 }
24
25 size_t PluginStream::WriteBytes(const char *buf, size_t length) {
26   DWORD bytes;
27
28   if (!WriteFile(temp_file_handle_, buf, length, &bytes, 0))
29     return 0U;
30
31   return static_cast<size_t>(bytes);
32 }
33
34 bool PluginStream::OpenTempFile() {
35   DCHECK_EQ(INVALID_HANDLE_VALUE, temp_file_handle_);
36
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.
40   //
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.
47
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,
56                                   0,
57                                   CREATE_ALWAYS,
58                                   FILE_ATTRIBUTE_NORMAL,
59                                   0);
60   if (temp_file_handle_ == INVALID_HANDLE_VALUE) {
61     ResetTempFileName();
62     return false;
63   }
64   return true;
65 }
66
67 void PluginStream::CloseTempFile() {
68   if (!TempFileIsValid())
69     return;
70
71   CloseHandle(temp_file_handle_);
72   ResetTempFileHandle();
73 }
74
75 bool PluginStream::TempFileIsValid() const {
76   return temp_file_handle_ != INVALID_HANDLE_VALUE;
77 }
78
79 }  // namespace content