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 extern "C" { | 7 extern "C" { |
8 #include <libexif/exif-data.h> | 8 #include <libexif/exif-data.h> |
9 #include <libexif/exif-loader.h> | 9 #include <libexif/exif-loader.h> |
10 } // extern "C" | 10 } // extern "C" |
| 11 #include <stddef.h> |
| 12 #include <stdint.h> |
11 | 13 |
12 #include "base/bind.h" | 14 #include "base/bind.h" |
13 #include "base/callback.h" | 15 #include "base/callback.h" |
14 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
15 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
| 18 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
17 #include "base/numerics/safe_conversions.h" | 20 #include "base/numerics/safe_conversions.h" |
18 #include "base/path_service.h" | 21 #include "base/path_service.h" |
19 #include "base/scoped_native_library.h" | 22 #include "base/scoped_native_library.h" |
20 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
| 24 #include "build/build_config.h" |
21 #include "content/public/common/content_paths.h" | 25 #include "content/public/common/content_paths.h" |
22 #include "media/base/data_source.h" | 26 #include "media/base/data_source.h" |
23 #include "net/base/io_buffer.h" | 27 #include "net/base/io_buffer.h" |
24 | 28 |
25 namespace metadata { | 29 namespace metadata { |
26 | 30 |
27 namespace { | 31 namespace { |
28 | 32 |
29 const size_t kMaxBufferSize = 50 * 1024 * 1024; // Arbitrary maximum of 50MB. | 33 const size_t kMaxBufferSize = 50 * 1024 * 1024; // Arbitrary maximum of 50MB. |
30 | 34 |
31 typedef base::Callback<void(const scoped_refptr<net::DrainableIOBuffer>&)> | 35 typedef base::Callback<void(const scoped_refptr<net::DrainableIOBuffer>&)> |
32 GotImageCallback; | 36 GotImageCallback; |
33 | 37 |
34 void FinishGetImageBytes( | 38 void FinishGetImageBytes( |
35 const scoped_refptr<net::DrainableIOBuffer>& buffer, | 39 const scoped_refptr<net::DrainableIOBuffer>& buffer, |
36 media::DataSource* source, | 40 media::DataSource* source, |
37 const GotImageCallback& callback, | 41 const GotImageCallback& callback, |
38 int bytes_read) { | 42 int bytes_read) { |
39 if (bytes_read == media::DataSource::kReadError) { | 43 if (bytes_read == media::DataSource::kReadError) { |
40 callback.Run(NULL); | 44 callback.Run(NULL); |
41 return; | 45 return; |
42 } | 46 } |
43 | 47 |
44 buffer->DidConsume(bytes_read); | 48 buffer->DidConsume(bytes_read); |
45 // Didn't get the whole file. Continue reading to get the rest. | 49 // Didn't get the whole file. Continue reading to get the rest. |
46 if (buffer->BytesRemaining() > 0) { | 50 if (buffer->BytesRemaining() > 0) { |
47 source->Read( | 51 source->Read(0, buffer->BytesRemaining(), |
48 0, | 52 reinterpret_cast<uint8_t*>(buffer->data()), |
49 buffer->BytesRemaining(), | 53 base::Bind(&FinishGetImageBytes, buffer, |
50 reinterpret_cast<uint8*>(buffer->data()), | 54 base::Unretained(source), callback)); |
51 base::Bind( | |
52 &FinishGetImageBytes, buffer, base::Unretained(source), callback)); | |
53 return; | 55 return; |
54 } | 56 } |
55 | 57 |
56 buffer->SetOffset(0); | 58 buffer->SetOffset(0); |
57 callback.Run(buffer); | 59 callback.Run(buffer); |
58 } | 60 } |
59 | 61 |
60 void GetImageBytes( | 62 void GetImageBytes( |
61 media::DataSource* source, | 63 media::DataSource* source, |
62 const GotImageCallback& callback) { | 64 const GotImageCallback& callback) { |
63 int64 size64 = 0; | 65 int64_t size64 = 0; |
64 if (!source->GetSize(&size64) || | 66 if (!source->GetSize(&size64) || |
65 base::saturated_cast<size_t>(size64) > kMaxBufferSize) { | 67 base::saturated_cast<size_t>(size64) > kMaxBufferSize) { |
66 return callback.Run(NULL); | 68 return callback.Run(NULL); |
67 } | 69 } |
68 int size = base::checked_cast<int>(size64); | 70 int size = base::checked_cast<int>(size64); |
69 | 71 |
70 scoped_refptr<net::DrainableIOBuffer> buffer( | 72 scoped_refptr<net::DrainableIOBuffer> buffer( |
71 new net::DrainableIOBuffer(new net::IOBuffer(size), size)); | 73 new net::DrainableIOBuffer(new net::IOBuffer(size), size)); |
72 source->Read(0, buffer->BytesRemaining(), | 74 source->Read(0, buffer->BytesRemaining(), |
73 reinterpret_cast<uint8*>(buffer->data()), | 75 reinterpret_cast<uint8_t*>(buffer->data()), |
74 base::Bind(&FinishGetImageBytes, buffer, | 76 base::Bind(&FinishGetImageBytes, buffer, |
75 base::Unretained(source), callback)); | 77 base::Unretained(source), callback)); |
76 } | 78 } |
77 | 79 |
78 class ExifFunctions { | 80 class ExifFunctions { |
79 public: | 81 public: |
80 ExifFunctions() : exif_loader_write_func_(NULL), | 82 ExifFunctions() : exif_loader_write_func_(NULL), |
81 exif_loader_new_func_(NULL), | 83 exif_loader_new_func_(NULL), |
82 exif_loader_unref_func_(NULL), | 84 exif_loader_unref_func_(NULL), |
83 exif_loader_get_data_func_(NULL), | 85 exif_loader_get_data_func_(NULL), |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 &iso_equivalent_); | 456 &iso_equivalent_); |
455 | 457 |
456 g_exif_lib.Get().ExifDataFree(data); | 458 g_exif_lib.Get().ExifDataFree(data); |
457 | 459 |
458 extracted_ = true; | 460 extracted_ = true; |
459 | 461 |
460 callback.Run(true); | 462 callback.Run(true); |
461 } | 463 } |
462 | 464 |
463 } // namespace metadata | 465 } // namespace metadata |
OLD | NEW |