OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/utility/media_galleries/image_metadata_extractor.h" | 5 #include "chrome/utility/media_galleries/image_metadata_extractor.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/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include <libexif/exif-data.h> | 21 #include <libexif/exif-data.h> |
22 #include <libexif/exif-loader.h> | 22 #include <libexif/exif-loader.h> |
23 } // extern "C" | 23 } // extern "C" |
24 | 24 |
25 namespace metadata { | 25 namespace metadata { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 const size_t kMaxBufferSize = 50 * 1024 * 1024; // Arbitrary maximum of 50MB. | 29 const size_t kMaxBufferSize = 50 * 1024 * 1024; // Arbitrary maximum of 50MB. |
30 | 30 |
| 31 typedef base::Callback<void(const scoped_refptr<net::DrainableIOBuffer>&)> |
| 32 GotImageCallback; |
| 33 |
31 void FinishGetImageBytes( | 34 void FinishGetImageBytes( |
32 net::DrainableIOBuffer* buffer, | 35 const scoped_refptr<net::DrainableIOBuffer>& buffer, |
33 media::DataSource* source, | 36 media::DataSource* source, |
34 const base::Callback<void(net::DrainableIOBuffer*)>& callback, | 37 const GotImageCallback& callback, |
35 int bytes_read) { | 38 int bytes_read) { |
36 if (bytes_read == media::DataSource::kReadError) { | 39 if (bytes_read == media::DataSource::kReadError) { |
37 callback.Run(NULL); | 40 callback.Run(NULL); |
38 return; | 41 return; |
39 } | 42 } |
40 | 43 |
41 buffer->DidConsume(bytes_read); | 44 buffer->DidConsume(bytes_read); |
42 // Didn't get the whole file. Continue reading to get the rest. | 45 // Didn't get the whole file. Continue reading to get the rest. |
43 if (buffer->BytesRemaining() > 0) { | 46 if (buffer->BytesRemaining() > 0) { |
44 source->Read(0, buffer->BytesRemaining(), | 47 source->Read( |
45 reinterpret_cast<uint8*>(buffer->data()), | 48 0, |
46 base::Bind(&FinishGetImageBytes, make_scoped_refptr(buffer), | 49 buffer->BytesRemaining(), |
47 base::Unretained(source), callback)); | 50 reinterpret_cast<uint8*>(buffer->data()), |
| 51 base::Bind( |
| 52 &FinishGetImageBytes, buffer, base::Unretained(source), callback)); |
48 return; | 53 return; |
49 } | 54 } |
50 | 55 |
51 buffer->SetOffset(0); | 56 buffer->SetOffset(0); |
52 callback.Run(make_scoped_refptr(buffer)); | 57 callback.Run(buffer); |
53 } | 58 } |
54 | 59 |
55 void GetImageBytes( | 60 void GetImageBytes( |
56 media::DataSource* source, | 61 media::DataSource* source, |
57 const base::Callback<void(net::DrainableIOBuffer*)>& callback) { | 62 const GotImageCallback& callback) { |
58 int64 size64 = 0; | 63 int64 size64 = 0; |
59 if (!source->GetSize(&size64) || | 64 if (!source->GetSize(&size64) || |
60 base::saturated_cast<size_t>(size64) > kMaxBufferSize) { | 65 base::saturated_cast<size_t>(size64) > kMaxBufferSize) { |
61 return callback.Run(NULL); | 66 return callback.Run(NULL); |
62 } | 67 } |
63 int size = base::checked_cast<int>(size64); | 68 int size = base::checked_cast<int>(size64); |
64 | 69 |
65 scoped_refptr<net::DrainableIOBuffer> buffer( | 70 scoped_refptr<net::DrainableIOBuffer> buffer( |
66 new net::DrainableIOBuffer(new net::IOBuffer(size), size)); | 71 new net::DrainableIOBuffer(new net::IOBuffer(size), size)); |
67 source->Read(0, buffer->BytesRemaining(), | 72 source->Read(0, buffer->BytesRemaining(), |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 DCHECK(extracted_); | 381 DCHECK(extracted_); |
377 return focal_length_mm_; | 382 return focal_length_mm_; |
378 } | 383 } |
379 | 384 |
380 int ImageMetadataExtractor::iso_equivalent() const { | 385 int ImageMetadataExtractor::iso_equivalent() const { |
381 DCHECK(extracted_); | 386 DCHECK(extracted_); |
382 return iso_equivalent_; | 387 return iso_equivalent_; |
383 } | 388 } |
384 | 389 |
385 void ImageMetadataExtractor::FinishExtraction( | 390 void ImageMetadataExtractor::FinishExtraction( |
386 const DoneCallback& callback, net::DrainableIOBuffer* buffer) { | 391 const DoneCallback& callback, |
387 if (!buffer) { | 392 const scoped_refptr<net::DrainableIOBuffer>& buffer) { |
| 393 if (!buffer.get()) { |
388 callback.Run(false); | 394 callback.Run(false); |
389 return; | 395 return; |
390 } | 396 } |
391 | 397 |
392 ExifData* data = g_exif_lib.Get().ParseExifFromBuffer( | 398 ExifData* data = g_exif_lib.Get().ParseExifFromBuffer( |
393 reinterpret_cast<unsigned char*>(buffer->data()), | 399 reinterpret_cast<unsigned char*>(buffer->data()), |
394 buffer->BytesRemaining()); | 400 buffer->BytesRemaining()); |
395 | 401 |
396 if (!data) { | 402 if (!data) { |
397 callback.Run(false); | 403 callback.Run(false); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 &iso_equivalent_); | 453 &iso_equivalent_); |
448 | 454 |
449 g_exif_lib.Get().ExifDataFree(data); | 455 g_exif_lib.Get().ExifDataFree(data); |
450 | 456 |
451 extracted_ = true; | 457 extracted_ = true; |
452 | 458 |
453 callback.Run(true); | 459 callback.Run(true); |
454 } | 460 } |
455 | 461 |
456 } // namespace metadata | 462 } // namespace metadata |
OLD | NEW |