Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / supported_audio_video_checker.cc
blobbed3c2367220132314c1e85ae846d4863a4147db
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"
7 #include <set>
8 #include <vector>
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"
20 namespace {
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]);
38 bool HasSupportedAudioVideoExtension(const base::FilePath& file) {
39 return ContainsKey(audio_video_extensions_, file.Extension());
42 private:
43 std::set<base::FilePath::StringType> audio_video_extensions_;
45 DISALLOW_COPY_AND_ASSIGN(SupportedAudioVideoExtensions);
48 base::LazyInstance<SupportedAudioVideoExtensions> g_audio_video_extensions =
49 LAZY_INSTANCE_INITIALIZER;
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*/);
58 } // namespace
60 SupportedAudioVideoChecker::~SupportedAudioVideoChecker() {}
62 // static
63 bool SupportedAudioVideoChecker::SupportsFileType(const base::FilePath& path) {
64 return g_audio_video_extensions.Get().HasSupportedAudioVideoExtension(path);
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;
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()));
81 SupportedAudioVideoChecker::SupportedAudioVideoChecker(
82 const base::FilePath& path)
83 : path_(path),
84 weak_factory_(this) {
87 void SupportedAudioVideoChecker::OnFileOpen(const base::PlatformFile& file) {
88 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
89 if (file == base::kInvalidPlatformFileValue) {
90 callback_.Run(base::PLATFORM_FILE_ERROR_SECURITY);
91 return;
94 safe_checker_ = new SafeAudioVideoChecker(file, callback_);
95 safe_checker_->Start();