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 #include "chrome/browser/media_galleries/fileapi/supported_audio_video_checker.h " | |
6 | |
7 #include <set> | |
8 #include <vector> | |
9 | |
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 "content/public/browser/browser_thread.h" | |
18 #include "net/base/mime_util.h" | |
19 | |
20 namespace { | |
21 | |
22 class SupportedAudioVideoExtensions { | |
23 public: | |
24 SupportedAudioVideoExtensions() { | |
25 std::vector<base::FilePath::StringType> extensions; | |
26 net::GetExtensionsForMimeType("audio/*", &extensions); | |
27 net::GetExtensionsForMimeType("video/*", &extensions); | |
28 for (size_t i = 0; i < extensions.size(); ++i) { | |
29 std::string mime_type; | |
30 if (net::GetWellKnownMimeTypeFromExtension(extensions[i], &mime_type) && | |
31 net::IsSupportedMimeType(mime_type)) { | |
32 audio_video_extensions_.insert( | |
33 base::FilePath::kExtensionSeparator + extensions[i]); | |
34 } | |
35 } | |
36 }; | |
37 | |
38 bool HasSupportedAudioVideoExtension(const base::FilePath& file) { | |
39 return ContainsKey(audio_video_extensions_, file.Extension()); | |
40 } | |
41 | |
42 private: | |
43 std::set<base::FilePath::StringType> audio_video_extensions_; | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(SupportedAudioVideoExtensions); | |
46 }; | |
47 | |
48 static base::LazyInstance<SupportedAudioVideoExtensions> | |
Lei Zhang
2013/08/08 00:08:02
Doesn't need to be static.
vandebo (ex-Chrome)
2013/08/08 17:34:01
Done.
| |
49 g_audio_video_extensions = LAZY_INSTANCE_INITIALIZER; | |
50 | |
51 base::PlatformFile OpenOnFileThread(const base::FilePath& path) { | |
52 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
53 return base::CreatePlatformFile( | |
54 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | |
55 NULL /*created*/, NULL /*error_code*/); | |
56 } | |
57 | |
58 } // namespace | |
59 | |
60 SupportedAudioVideoChecker::~SupportedAudioVideoChecker() {} | |
61 | |
62 // static | |
63 bool SupportedAudioVideoChecker::SupportsFileType(const base::FilePath& path) { | |
64 return g_audio_video_extensions.Get().HasSupportedAudioVideoExtension(path); | |
65 } | |
66 | |
67 void SupportedAudioVideoChecker::StartPreWriteValidation( | |
68 const fileapi::CopyOrMoveFileValidator::ResultCallback& result_callback) { | |
69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
70 DCHECK(callback_.is_null()); | |
71 callback_ = result_callback; | |
72 | |
73 content::BrowserThread::PostTaskAndReplyWithResult( | |
74 content::BrowserThread::FILE, | |
75 FROM_HERE, | |
76 base::Bind(&OpenOnFileThread, path_), | |
77 base::Bind(&SupportedAudioVideoChecker::OnFileOpen, | |
78 weak_factory_.GetWeakPtr())); | |
79 } | |
80 | |
81 void SupportedAudioVideoChecker::StartPostWriteValidation( | |
82 const base::FilePath& dest_platform_path, | |
83 const ResultCallback& result_callback) { | |
84 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
85 | |
86 // TODO(gbillock): Insert AV call here in the right validator. | |
87 result_callback.Run(base::PLATFORM_FILE_OK); | |
88 } | |
89 | |
90 SupportedAudioVideoChecker::SupportedAudioVideoChecker( | |
91 const base::FilePath& path) | |
92 : path_(path), | |
93 weak_factory_(this) { | |
94 } | |
95 | |
96 void SupportedAudioVideoChecker::OnFileOpen(const base::PlatformFile& file) { | |
97 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
98 if (file == base::kInvalidPlatformFileValue) { | |
99 callback_.Run(base::PLATFORM_FILE_ERROR_SECURITY); | |
100 return; | |
101 } | |
102 | |
103 safe_checker_ = new SafeAudioVideoChecker(file, callback_); | |
104 safe_checker_->Start(); | |
105 } | |
OLD | NEW |