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.
5 #include "chrome/browser/media_galleries/fileapi/supported_audio_video_checker.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/lazy_instance.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/stl_util.h"
16 #include "chrome/browser/media_galleries/fileapi/safe_audio_video_checker.h"
17 #include "components/mime_util/mime_util.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "net/base/mime_util.h"
23 class SupportedAudioVideoExtensions
{
25 SupportedAudioVideoExtensions() {
26 std::vector
<base::FilePath::StringType
> extensions
;
27 net::GetExtensionsForMimeType("audio/*", &extensions
);
28 net::GetExtensionsForMimeType("video/*", &extensions
);
29 for (size_t i
= 0; i
< extensions
.size(); ++i
) {
30 std::string mime_type
;
31 if (net::GetWellKnownMimeTypeFromExtension(extensions
[i
], &mime_type
) &&
32 mime_util::IsSupportedMimeType(mime_type
)) {
33 audio_video_extensions_
.insert(
34 base::FilePath::kExtensionSeparator
+ extensions
[i
]);
39 bool HasSupportedAudioVideoExtension(const base::FilePath
& file
) {
40 return ContainsKey(audio_video_extensions_
, file
.Extension());
44 std::set
<base::FilePath::StringType
> audio_video_extensions_
;
46 DISALLOW_COPY_AND_ASSIGN(SupportedAudioVideoExtensions
);
49 base::LazyInstance
<SupportedAudioVideoExtensions
> g_audio_video_extensions
=
50 LAZY_INSTANCE_INITIALIZER
;
52 base::File
OpenOnFileThread(const base::FilePath
& path
) {
53 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
54 return base::File(path
, base::File::FLAG_OPEN
| base::File::FLAG_READ
);
59 SupportedAudioVideoChecker::~SupportedAudioVideoChecker() {}
62 bool SupportedAudioVideoChecker::SupportsFileType(const base::FilePath
& path
) {
63 return g_audio_video_extensions
.Get().HasSupportedAudioVideoExtension(path
);
66 void SupportedAudioVideoChecker::StartPreWriteValidation(
67 const storage::CopyOrMoveFileValidator::ResultCallback
& result_callback
) {
68 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
69 DCHECK(callback_
.is_null());
70 callback_
= result_callback
;
72 content::BrowserThread::PostTaskAndReplyWithResult(
73 content::BrowserThread::FILE,
75 base::Bind(&OpenOnFileThread
, path_
),
76 base::Bind(&SupportedAudioVideoChecker::OnFileOpen
,
77 weak_factory_
.GetWeakPtr()));
80 SupportedAudioVideoChecker::SupportedAudioVideoChecker(
81 const base::FilePath
& path
)
86 void SupportedAudioVideoChecker::OnFileOpen(base::File file
) {
87 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
88 if (!file
.IsValid()) {
89 callback_
.Run(base::File::FILE_ERROR_SECURITY
);
93 safe_checker_
= new SafeAudioVideoChecker(file
.Pass(), callback_
);
94 safe_checker_
->Start();