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 11 matching lines...) Expand all Loading... | |
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 void FinishGetImageBytes( | 31 void FinishGetImageBytes( |
32 net::DrainableIOBuffer* buffer, | 32 const scoped_refptr<net::DrainableIOBuffer>& buffer, |
33 media::DataSource* source, | 33 media::DataSource* source, |
34 const base::Callback<void(net::DrainableIOBuffer*)>& callback, | 34 const base::Callback<void(const scoped_refptr<net::DrainableIOBuffer>&)>& |
Lei Zhang
2014/08/29 15:25:56
can you add a typedef for the callback?
dcheng
2014/08/29 16:32:01
Done.
| |
35 callback, | |
35 int bytes_read) { | 36 int bytes_read) { |
36 if (bytes_read == media::DataSource::kReadError) { | 37 if (bytes_read == media::DataSource::kReadError) { |
37 callback.Run(NULL); | 38 callback.Run(NULL); |
38 return; | 39 return; |
39 } | 40 } |
40 | 41 |
41 buffer->DidConsume(bytes_read); | 42 buffer->DidConsume(bytes_read); |
42 // Didn't get the whole file. Continue reading to get the rest. | 43 // Didn't get the whole file. Continue reading to get the rest. |
43 if (buffer->BytesRemaining() > 0) { | 44 if (buffer->BytesRemaining() > 0) { |
44 source->Read(0, buffer->BytesRemaining(), | 45 source->Read( |
45 reinterpret_cast<uint8*>(buffer->data()), | 46 0, |
46 base::Bind(&FinishGetImageBytes, make_scoped_refptr(buffer), | 47 buffer->BytesRemaining(), |
47 base::Unretained(source), callback)); | 48 reinterpret_cast<uint8*>(buffer->data()), |
49 base::Bind( | |
50 &FinishGetImageBytes, buffer, base::Unretained(source), callback)); | |
48 return; | 51 return; |
49 } | 52 } |
50 | 53 |
51 buffer->SetOffset(0); | 54 buffer->SetOffset(0); |
52 callback.Run(make_scoped_refptr(buffer)); | 55 callback.Run(buffer); |
53 } | 56 } |
54 | 57 |
55 void GetImageBytes( | 58 void GetImageBytes( |
56 media::DataSource* source, | 59 media::DataSource* source, |
57 const base::Callback<void(net::DrainableIOBuffer*)>& callback) { | 60 const base::Callback<void(const scoped_refptr<net::DrainableIOBuffer>&)>& |
61 callback) { | |
58 int64 size64 = 0; | 62 int64 size64 = 0; |
59 if (!source->GetSize(&size64) || | 63 if (!source->GetSize(&size64) || |
60 base::saturated_cast<size_t>(size64) > kMaxBufferSize) { | 64 base::saturated_cast<size_t>(size64) > kMaxBufferSize) { |
61 return callback.Run(NULL); | 65 return callback.Run(NULL); |
62 } | 66 } |
63 int size = base::checked_cast<int>(size64); | 67 int size = base::checked_cast<int>(size64); |
64 | 68 |
65 scoped_refptr<net::DrainableIOBuffer> buffer( | 69 scoped_refptr<net::DrainableIOBuffer> buffer( |
66 new net::DrainableIOBuffer(new net::IOBuffer(size), size)); | 70 new net::DrainableIOBuffer(new net::IOBuffer(size), size)); |
67 source->Read(0, buffer->BytesRemaining(), | 71 source->Read(0, buffer->BytesRemaining(), |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 DCHECK(extracted_); | 380 DCHECK(extracted_); |
377 return focal_length_mm_; | 381 return focal_length_mm_; |
378 } | 382 } |
379 | 383 |
380 int ImageMetadataExtractor::iso_equivalent() const { | 384 int ImageMetadataExtractor::iso_equivalent() const { |
381 DCHECK(extracted_); | 385 DCHECK(extracted_); |
382 return iso_equivalent_; | 386 return iso_equivalent_; |
383 } | 387 } |
384 | 388 |
385 void ImageMetadataExtractor::FinishExtraction( | 389 void ImageMetadataExtractor::FinishExtraction( |
386 const DoneCallback& callback, net::DrainableIOBuffer* buffer) { | 390 const DoneCallback& callback, |
387 if (!buffer) { | 391 const scoped_refptr<net::DrainableIOBuffer>& buffer) { |
392 if (!buffer.get()) { | |
388 callback.Run(false); | 393 callback.Run(false); |
389 return; | 394 return; |
390 } | 395 } |
391 | 396 |
392 ExifData* data = g_exif_lib.Get().ParseExifFromBuffer( | 397 ExifData* data = g_exif_lib.Get().ParseExifFromBuffer( |
393 reinterpret_cast<unsigned char*>(buffer->data()), | 398 reinterpret_cast<unsigned char*>(buffer->data()), |
394 buffer->BytesRemaining()); | 399 buffer->BytesRemaining()); |
395 | 400 |
396 if (!data) { | 401 if (!data) { |
397 callback.Run(false); | 402 callback.Run(false); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
447 &iso_equivalent_); | 452 &iso_equivalent_); |
448 | 453 |
449 g_exif_lib.Get().ExifDataFree(data); | 454 g_exif_lib.Get().ExifDataFree(data); |
450 | 455 |
451 extracted_ = true; | 456 extracted_ = true; |
452 | 457 |
453 callback.Run(true); | 458 callback.Run(true); |
454 } | 459 } |
455 | 460 |
456 } // namespace metadata | 461 } // namespace metadata |
OLD | NEW |