OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 // | |
5 // This file provides MIME related utilities. | |
6 | |
7 #ifndef EXTENSIONS_BROWSER_API_FILE_HANDLERS_MIME_UTIL_H_ | |
8 #define EXTENSIONS_BROWSER_API_FILE_HANDLERS_MIME_UTIL_H_ | |
9 | |
10 #include <stddef.h> | |
11 | |
12 #include <memory> | |
13 #include <string> | |
14 #include <vector> | |
15 | |
16 #include "base/callback.h" | |
17 #include "base/macros.h" | |
18 #include "base/memory/weak_ptr.h" | |
19 | |
20 namespace content { | |
21 class BrowserContext; | |
22 } | |
23 | |
24 namespace base { | |
25 class FilePath; | |
26 } // namespace base | |
27 | |
28 namespace storage { | |
29 class FileSystemURL; | |
30 } // namespace storage | |
31 | |
32 namespace extensions { | |
33 namespace app_file_handler_util { | |
34 | |
35 // Gets a MIME type for a local path and returns it with |callback|. If not | |
36 // found, then the MIME type is an empty string. | |
37 void GetMimeTypeForLocalPath( | |
38 content::BrowserContext* context, | |
39 const base::FilePath& local_path, | |
40 const base::Callback<void(const std::string&)>& callback); | |
41 | |
42 // Collects MIME types for files passed in the input vector. For non-native | |
43 // file systems tries to fetch the MIME type from metadata. For native ones, | |
44 // tries to sniff or guess by looking at the extension. If MIME type is not | |
45 // available, then an empty string is returned in the result vector. | |
46 class MimeTypeCollector { | |
47 public: | |
48 typedef base::Callback<void(std::unique_ptr<std::vector<std::string>>)> | |
49 CompletionCallback; | |
50 | |
51 explicit MimeTypeCollector(content::BrowserContext* context); | |
52 virtual ~MimeTypeCollector(); | |
53 | |
54 // Collects all mime types asynchronously for a vector of URLs and upon | |
55 // completion, calls the |callback|. It can be called only once. | |
56 void CollectForURLs(const std::vector<storage::FileSystemURL>& urls, | |
57 const CompletionCallback& callback); | |
58 | |
59 // Collects all mime types asynchronously for a vector of local file paths and | |
60 // upon completion, calls the |callback|. It can be called only once. | |
61 void CollectForLocalPaths(const std::vector<base::FilePath>& local_paths, | |
62 const CompletionCallback& callback); | |
63 | |
64 private: | |
65 // Called, when the |index|-th input file (or URL) got processed. | |
66 void OnMimeTypeCollected(size_t index, const std::string& mime_type); | |
67 | |
68 content::BrowserContext* context_; | |
69 std::unique_ptr<std::vector<std::string>> result_; | |
70 size_t left_; | |
71 CompletionCallback callback_; | |
72 base::WeakPtrFactory<MimeTypeCollector> weak_ptr_factory_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(MimeTypeCollector); | |
75 }; | |
76 | |
77 } // namespace app_file_handler_util | |
78 } // namespace extensions | |
79 | |
80 #endif // EXTENSIONS_BROWSER_API_FILE_HANDLERS_MIME_UTIL_H_ | |
OLD | NEW |