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"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/process/process_handle.h"
12 #include "chrome/common/chrome_utility_messages.h"
13 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "content/public/browser/child_process_data.h"
16 #include "content/public/browser/utility_process_host.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "ipc/ipc_message_macros.h"
19 #include "ipc/ipc_platform_file.h"
20 #include "ui/base/l10n/l10n_util.h"
22 SafeAudioVideoChecker::SafeAudioVideoChecker(
24 const storage::CopyOrMoveFileValidator::ResultCallback
& callback
)
25 : state_(INITIAL_STATE
), file_(file
.Pass()), callback_(callback
) {
26 DCHECK(!callback
.is_null());
29 void SafeAudioVideoChecker::Start() {
30 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
31 if (state_
!= INITIAL_STATE
)
33 state_
= PINGED_STATE
;
35 if (!file_
.IsValid()) {
36 callback_
.Run(base::File::FILE_ERROR_SECURITY
);
37 state_
= FINISHED_STATE
;
41 utility_process_host_
= content::UtilityProcessHost::Create(
42 this, base::MessageLoopProxy::current())->AsWeakPtr();
43 utility_process_host_
->SetName(l10n_util::GetStringUTF16(
44 IDS_UTILITY_PROCESS_MEDIA_FILE_CHECKER_NAME
));
45 utility_process_host_
->Send(new ChromeUtilityMsg_StartupPing
);
48 SafeAudioVideoChecker::~SafeAudioVideoChecker() {}
50 void SafeAudioVideoChecker::OnProcessStarted() {
51 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
52 if (state_
!= PINGED_STATE
)
54 state_
= STARTED_STATE
;
56 if (utility_process_host_
->GetData().handle
== base::kNullProcessHandle
)
57 DLOG(ERROR
) << "Child process handle is null";
58 IPC::PlatformFileForTransit file_for_transit
=
59 IPC::TakeFileHandleForProcess(file_
.Pass(),
60 utility_process_host_
->GetData().handle
);
61 if (file_for_transit
== IPC::InvalidPlatformFileForTransit()) {
62 OnCheckingFinished(false /* valid? */);
65 const int64 kFileDecodeTimeInMS
= 250;
66 utility_process_host_
->Send(new ChromeUtilityMsg_CheckMediaFile(
67 kFileDecodeTimeInMS
, file_for_transit
));
70 void SafeAudioVideoChecker::OnCheckingFinished(bool valid
) {
71 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
72 if (state_
!= STARTED_STATE
)
74 state_
= FINISHED_STATE
;
76 callback_
.Run(valid
? base::File::FILE_OK
:
77 base::File::FILE_ERROR_SECURITY
);
80 void SafeAudioVideoChecker::OnProcessCrashed(int exit_code
) {
81 OnCheckingFinished(false);
84 bool SafeAudioVideoChecker::OnMessageReceived(const IPC::Message
& message
) {
86 IPC_BEGIN_MESSAGE_MAP(SafeAudioVideoChecker
, message
)
87 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted
,
89 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CheckMediaFile_Finished
,
91 IPC_MESSAGE_UNHANDLED(handled
= false)