| OLD | NEW |
| 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/supported_image_type_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/file.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" | 16 #include "chrome/browser/image_decoder.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 | 18 |
| 19 using content::BrowserThread; | 19 using content::BrowserThread; |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Arbitrary limit to sanity check the file size. | 23 // Arbitrary limit to sanity check the file size. |
| 24 const int kMaxImageFileSize = 50*1014*1024; | 24 const int kMaxImageFileSize = 50*1014*1024; |
| 25 | 25 |
| 26 scoped_ptr<std::string> ReadOnFileThread(const base::FilePath& path) { | 26 scoped_ptr<std::string> ReadOnFileThread(const base::FilePath& path) { |
| 27 base::ThreadRestrictions::AssertIOAllowed(); | 27 base::ThreadRestrictions::AssertIOAllowed(); |
| 28 scoped_ptr<std::string> result; | 28 scoped_ptr<std::string> result; |
| 29 | 29 |
| 30 base::PlatformFile file = base::CreatePlatformFile( | 30 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 31 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, NULL, NULL); | 31 if (!file.IsValid()) |
| 32 if (file == base::kInvalidPlatformFileValue) | |
| 33 return result.Pass(); | 32 return result.Pass(); |
| 34 base::ScopedPlatformFileCloser file_closer(&file); | |
| 35 | 33 |
| 36 base::PlatformFileInfo file_info; | 34 base::File::Info file_info; |
| 37 if (!base::GetPlatformFileInfo(file, &file_info) || | 35 if (!file.GetInfo(&file_info) || |
| 38 file_info.size > kMaxImageFileSize) { | 36 file_info.size > kMaxImageFileSize) { |
| 39 return result.Pass(); | 37 return result.Pass(); |
| 40 } | 38 } |
| 41 | 39 |
| 42 result.reset(new std::string); | 40 result.reset(new std::string); |
| 43 result->resize(file_info.size); | 41 result->resize(file_info.size); |
| 44 if (base::ReadPlatformFile(file, 0, string_as_array(result.get()), | 42 if (file.Read(0, string_as_array(result.get()), file_info.size) != |
| 45 file_info.size) != file_info.size) { | 43 file_info.size) { |
| 46 result.reset(); | 44 result.reset(); |
| 47 } | 45 } |
| 48 | 46 |
| 49 return result.Pass(); | 47 return result.Pass(); |
| 50 } | 48 } |
| 51 | 49 |
| 52 class ImageDecoderDelegateAdapter : public ImageDecoder::Delegate { | 50 class ImageDecoderDelegateAdapter : public ImageDecoder::Delegate { |
| 53 public: | 51 public: |
| 54 ImageDecoderDelegateAdapter( | 52 ImageDecoderDelegateAdapter( |
| 55 scoped_ptr<std::string> data, | 53 scoped_ptr<std::string> data, |
| 56 const fileapi::CopyOrMoveFileValidator::ResultCallback& callback) | 54 const fileapi::CopyOrMoveFileValidator::ResultCallback& callback) |
| 57 : data_(data.Pass()), | 55 : data_(data.Pass()), |
| 58 callback_(callback) { | 56 callback_(callback) { |
| 59 DCHECK(data_); | 57 DCHECK(data_); |
| 60 } | 58 } |
| 61 | 59 |
| 62 const std::string& data() { | 60 const std::string& data() { |
| 63 return *data_; | 61 return *data_; |
| 64 } | 62 } |
| 65 | 63 |
| 66 // ImageDecoder::Delegate methods. | 64 // ImageDecoder::Delegate methods. |
| 67 virtual void OnImageDecoded(const ImageDecoder* /*decoder*/, | 65 virtual void OnImageDecoded(const ImageDecoder* /*decoder*/, |
| 68 const SkBitmap& /*decoded_image*/) OVERRIDE { | 66 const SkBitmap& /*decoded_image*/) OVERRIDE { |
| 69 callback_.Run(base::PLATFORM_FILE_OK); | 67 callback_.Run(base::File::FILE_OK); |
| 70 delete this; | 68 delete this; |
| 71 } | 69 } |
| 72 | 70 |
| 73 virtual void OnDecodeImageFailed(const ImageDecoder* /*decoder*/) OVERRIDE { | 71 virtual void OnDecodeImageFailed(const ImageDecoder* /*decoder*/) OVERRIDE { |
| 74 callback_.Run(base::PLATFORM_FILE_ERROR_SECURITY); | 72 callback_.Run(base::File::FILE_ERROR_SECURITY); |
| 75 delete this; | 73 delete this; |
| 76 } | 74 } |
| 77 | 75 |
| 78 private: | 76 private: |
| 79 scoped_ptr<std::string> data_; | 77 scoped_ptr<std::string> data_; |
| 80 fileapi::CopyOrMoveFileValidator::ResultCallback callback_; | 78 fileapi::CopyOrMoveFileValidator::ResultCallback callback_; |
| 81 | 79 |
| 82 DISALLOW_COPY_AND_ASSIGN(ImageDecoderDelegateAdapter); | 80 DISALLOW_COPY_AND_ASSIGN(ImageDecoderDelegateAdapter); |
| 83 }; | 81 }; |
| 84 | 82 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 114 |
| 117 SupportedImageTypeValidator::SupportedImageTypeValidator( | 115 SupportedImageTypeValidator::SupportedImageTypeValidator( |
| 118 const base::FilePath& path) | 116 const base::FilePath& path) |
| 119 : path_(path), | 117 : path_(path), |
| 120 weak_factory_(this) { | 118 weak_factory_(this) { |
| 121 } | 119 } |
| 122 | 120 |
| 123 void SupportedImageTypeValidator::OnFileOpen(scoped_ptr<std::string> data) { | 121 void SupportedImageTypeValidator::OnFileOpen(scoped_ptr<std::string> data) { |
| 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 125 if (!data.get()) { | 123 if (!data.get()) { |
| 126 callback_.Run(base::PLATFORM_FILE_ERROR_SECURITY); | 124 callback_.Run(base::File::FILE_ERROR_SECURITY); |
| 127 return; | 125 return; |
| 128 } | 126 } |
| 129 | 127 |
| 130 // |adapter| will delete itself after a completion message is received. | 128 // |adapter| will delete itself after a completion message is received. |
| 131 ImageDecoderDelegateAdapter* adapter = | 129 ImageDecoderDelegateAdapter* adapter = |
| 132 new ImageDecoderDelegateAdapter(data.Pass(), callback_); | 130 new ImageDecoderDelegateAdapter(data.Pass(), callback_); |
| 133 decoder_ = new ImageDecoder(adapter, adapter->data(), | 131 decoder_ = new ImageDecoder(adapter, adapter->data(), |
| 134 ImageDecoder::DEFAULT_CODEC); | 132 ImageDecoder::DEFAULT_CODEC); |
| 135 decoder_->Start(content::BrowserThread::GetMessageLoopProxyForThread( | 133 decoder_->Start(content::BrowserThread::GetMessageLoopProxyForThread( |
| 136 BrowserThread::IO)); | 134 BrowserThread::IO)); |
| 137 } | 135 } |
| OLD | NEW |