Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: chrome/browser/media_galleries/fileapi/scanning_file_validator.cc

Issue 21355004: [Downloads] Move client guid for AV scanning of downloaded files to chrome/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add base class Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/media_galleries/fileapi/supported_image_type_validator. h" 5 #include "chrome/browser/media_galleries/fileapi/scanning_file_validator.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/files/scoped_platform_file_closer.h" 9 #include "base/files/scoped_platform_file_closer.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/threading/thread_restrictions.h" 15 #include "base/threading/thread_restrictions.h"
16 #include "chrome/browser/image_decoder.h"
17 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
18 17
18 #if defined(OS_WIN)
19 #include "base/file_util.h"
20 #endif
21
19 using content::BrowserThread; 22 using content::BrowserThread;
20 23
21 namespace chrome { 24 namespace chrome {
22 25
23 namespace { 26 namespace {
24 27
25 // Arbitrary limit to sanity check the file size. 28 // Arbitrary limit to sanity check the file size.
26 const int kMaxImageFileSize = 50*1014*1024; 29 const int kMaxImageFileSize = 50*1014*1024;
27 30
28 scoped_ptr<std::string> ReadOnFileThread(const base::FilePath& path) { 31 scoped_ptr<std::string> ReadOnFileThread(const base::FilePath& path) {
(...skipping 15 matching lines...) Expand all
44 result.reset(new std::string); 47 result.reset(new std::string);
45 result->resize(file_info.size); 48 result->resize(file_info.size);
46 if (base::ReadPlatformFile(file, 0, string_as_array(result.get()), 49 if (base::ReadPlatformFile(file, 0, string_as_array(result.get()),
47 file_info.size) != file_info.size) { 50 file_info.size) != file_info.size) {
48 result.reset(); 51 result.reset();
49 } 52 }
50 53
51 return result.Pass(); 54 return result.Pass();
52 } 55 }
53 56
54 class ImageDecoderDelegateAdapter : public ImageDecoder::Delegate { 57 #if defined(OS_WIN)
55 public: 58 base::PlatformFileError ScanFile(
56 ImageDecoderDelegateAdapter( 59 const base::FilePath& dest_platform_path) {
57 scoped_ptr<std::string> data, 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
58 const fileapi::CopyOrMoveFileValidator::ResultCallback& callback) 61 base::PlatformFileError result = base::PLATFORM_FILE_OK;
59 : data_(data.Pass()),
60 callback_(callback) {
61 DCHECK(data_);
62 }
63 62
64 const std::string& data() { 63 // Execute an AV check of the transferred file. S_OK means
65 return *data_; 64 // the file was OK. Other results may indicate the file was
66 } 65 // deleted. Any other results, including E_FAIL and
66 // INET_E_SECURITY_PROBLEM, are notified as security errors.
67 HRESULT scan_result =
68 base::ScanAndSaveDownloadedFile(dest_platform_path, std::string());
69 if (scan_result != S_OK)
70 result = base::PLATFORM_FILE_ERROR_SECURITY;
67 71
68 // ImageDecoder::Delegate methods. 72 return result;
69 virtual void OnImageDecoded(const ImageDecoder* /*decoder*/, 73 }
70 const SkBitmap& /*decoded_image*/) OVERRIDE { 74 #endif
71 callback_.Run(base::PLATFORM_FILE_OK);
72 delete this;
73 }
74 75
75 virtual void OnDecodeImageFailed(const ImageDecoder* /*decoder*/) OVERRIDE {
76 callback_.Run(base::PLATFORM_FILE_ERROR_SECURITY);
77 delete this;
78 }
79
80 private:
81 scoped_ptr<std::string> data_;
82 fileapi::CopyOrMoveFileValidator::ResultCallback callback_;
83
84 DISALLOW_COPY_AND_ASSIGN(ImageDecoderDelegateAdapter);
85 };
86 76
87 } // namespace 77 } // namespace
88 78
89 SupportedImageTypeValidator::~SupportedImageTypeValidator() {} 79 SupportedImageTypeValidator::~SupportedImageTypeValidator() {}
90 80
91 // static 81 // static
92 bool SupportedImageTypeValidator::SupportsFileType(const base::FilePath& path) { 82 bool SupportedImageTypeValidator::SupportsFileType(const base::FilePath& path) {
93 base::FilePath::StringType extension = path.Extension(); 83 base::FilePath::StringType extension = path.Extension();
94 return extension == FILE_PATH_LITERAL(".bmp") || 84 return extension == FILE_PATH_LITERAL(".bmp") ||
95 extension == FILE_PATH_LITERAL(".gif") || 85 extension == FILE_PATH_LITERAL(".gif") ||
(...skipping 17 matching lines...) Expand all
113 FROM_HERE, 103 FROM_HERE,
114 base::Bind(&ReadOnFileThread, path_), 104 base::Bind(&ReadOnFileThread, path_),
115 base::Bind(&SupportedImageTypeValidator::OnFileOpen, 105 base::Bind(&SupportedImageTypeValidator::OnFileOpen,
116 weak_factory_.GetWeakPtr())); 106 weak_factory_.GetWeakPtr()));
117 } 107 }
118 108
119 void SupportedImageTypeValidator::StartPostWriteValidation( 109 void SupportedImageTypeValidator::StartPostWriteValidation(
120 const base::FilePath& dest_platform_path, 110 const base::FilePath& dest_platform_path,
121 const ResultCallback& result_callback) { 111 const ResultCallback& result_callback) {
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
123 post_write_callback_ = result_callback;
124 113
125 // TODO(gbillock): Insert AV call here in the right validator. 114 #if defined(OS_WIN)
115 BrowserThread::PostTaskAndReplyWithResult(
116 BrowserThread::FILE,
117 FROM_HERE,
118 base::Bind(&ScanFile, dest_platform_path),
119 result_callback);
120 #else
126 BrowserThread::PostTask( 121 BrowserThread::PostTask(
vandebo (ex-Chrome) 2013/08/05 18:05:30 no need to post task here, just run the callback.
Greg Billock 2013/08/05 21:42:30 Done.
127 BrowserThread::IO, 122 BrowserThread::IO,
128 FROM_HERE, 123 FROM_HERE,
129 base::Bind(post_write_callback_, base::PLATFORM_FILE_OK)); 124 base::Bind(result_callback, base::PLATFORM_FILE_OK));
125 #endif
130 } 126 }
131 127
132 SupportedImageTypeValidator::SupportedImageTypeValidator( 128 SupportedImageTypeValidator::SupportedImageTypeValidator(
133 const base::FilePath& path) 129 const base::FilePath& path)
134 : path_(path), 130 : path_(path),
135 weak_factory_(this) { 131 weak_factory_(this) {
136 } 132 }
137 133
138 void SupportedImageTypeValidator::OnFileOpen(scoped_ptr<std::string> data) { 134 void SupportedImageTypeValidator::OnFileOpen(scoped_ptr<std::string> data) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
140 if (!data.get()) { 136 if (!data.get()) {
141 callback_.Run(base::PLATFORM_FILE_ERROR_SECURITY); 137 callback_.Run(base::PLATFORM_FILE_ERROR_SECURITY);
142 return; 138 return;
143 } 139 }
144 140
145 // |adapter| will delete itself after a completion message is received. 141 BrowserThread::PostTask(
146 ImageDecoderDelegateAdapter* adapter = 142 BrowserThread::IO,
147 new ImageDecoderDelegateAdapter(data.Pass(), callback_); 143 FROM_HERE,
148 decoder_ = new ImageDecoder(adapter, adapter->data(), 144 base::Bind(callback_, base::PLATFORM_FILE_OK));
149 ImageDecoder::DEFAULT_CODEC);
150 decoder_->Start(content::BrowserThread::GetMessageLoopProxyForThread(
151 BrowserThread::IO));
152 } 145 }
153 146
154 } // namespace chrome 147 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698