Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / safe_audio_video_checker.cc
blob3eb5b6e95c2d30d217e008efb062c100289ad66b
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/safe_audio_video_checker.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/process/process_handle.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/common/chrome_utility_messages.h"
15 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "content/public/browser/child_process_data.h"
18 #include "content/public/browser/utility_process_host.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "ipc/ipc_message_macros.h"
21 #include "ipc/ipc_platform_file.h"
22 #include "ui/base/l10n/l10n_util.h"
24 SafeAudioVideoChecker::SafeAudioVideoChecker(
25 base::File file,
26 const storage::CopyOrMoveFileValidator::ResultCallback& callback)
27 : state_(INITIAL_STATE), file_(file.Pass()), callback_(callback) {
28 DCHECK(!callback.is_null());
31 void SafeAudioVideoChecker::Start() {
32 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
33 if (state_ != INITIAL_STATE)
34 return;
35 state_ = PINGED_STATE;
37 if (!file_.IsValid()) {
38 callback_.Run(base::File::FILE_ERROR_SECURITY);
39 state_ = FINISHED_STATE;
40 return;
43 utility_process_host_ = content::UtilityProcessHost::Create(
44 this, base::ThreadTaskRunnerHandle::Get())->AsWeakPtr();
45 utility_process_host_->SetName(l10n_util::GetStringUTF16(
46 IDS_UTILITY_PROCESS_MEDIA_FILE_CHECKER_NAME));
47 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing);
50 SafeAudioVideoChecker::~SafeAudioVideoChecker() {}
52 void SafeAudioVideoChecker::OnProcessStarted() {
53 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
54 if (state_ != PINGED_STATE)
55 return;
56 state_ = STARTED_STATE;
58 if (utility_process_host_->GetData().handle == base::kNullProcessHandle)
59 DLOG(ERROR) << "Child process handle is null";
60 IPC::PlatformFileForTransit file_for_transit =
61 IPC::TakeFileHandleForProcess(file_.Pass(),
62 utility_process_host_->GetData().handle);
63 if (file_for_transit == IPC::InvalidPlatformFileForTransit()) {
64 OnCheckingFinished(false /* valid? */);
65 return;
67 const int64 kFileDecodeTimeInMS = 250;
68 utility_process_host_->Send(new ChromeUtilityMsg_CheckMediaFile(
69 kFileDecodeTimeInMS, file_for_transit));
72 void SafeAudioVideoChecker::OnCheckingFinished(bool valid) {
73 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
74 if (state_ != STARTED_STATE)
75 return;
76 state_ = FINISHED_STATE;
78 callback_.Run(valid ? base::File::FILE_OK :
79 base::File::FILE_ERROR_SECURITY);
82 void SafeAudioVideoChecker::OnProcessCrashed(int exit_code) {
83 OnCheckingFinished(false);
86 bool SafeAudioVideoChecker::OnMessageReceived(const IPC::Message& message) {
87 bool handled = true;
88 IPC_BEGIN_MESSAGE_MAP(SafeAudioVideoChecker, message)
89 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted,
90 OnProcessStarted)
91 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CheckMediaFile_Finished,
92 OnCheckingFinished)
93 IPC_MESSAGE_UNHANDLED(handled = false)
94 IPC_END_MESSAGE_MAP()
95 return handled;