Home | History | Annotate | Download | only in fileapi
      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/safe_audio_video_checker.h"
      6 
      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 "chrome/common/chrome_utility_messages.h"
     13 #include "content/public/browser/child_process_data.h"
     14 #include "content/public/browser/utility_process_host.h"
     15 #include "content/public/browser/browser_thread.h"
     16 #include "ipc/ipc_message_macros.h"
     17 #include "ipc/ipc_platform_file.h"
     18 
     19 SafeAudioVideoChecker::SafeAudioVideoChecker(
     20     const base::PlatformFile& file,
     21     const fileapi::CopyOrMoveFileValidator::ResultCallback& callback)
     22     : state_(INITIAL_STATE),
     23       file_(file),
     24       file_closer_(&file_),
     25       callback_(callback) {
     26   DCHECK(!callback.is_null());
     27 }
     28 
     29 void SafeAudioVideoChecker::Start() {
     30   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
     31   if (state_ != INITIAL_STATE)
     32     return;
     33   state_ = PINGED_STATE;
     34 
     35   DCHECK(file_closer_);
     36   if (*file_closer_.get() == base::kInvalidPlatformFileValue) {
     37     callback_.Run(base::PLATFORM_FILE_ERROR_SECURITY);
     38     state_ = FINISHED_STATE;
     39     return;
     40   }
     41 
     42   utility_process_host_ = content::UtilityProcessHost::Create(
     43       this, base::MessageLoopProxy::current())->AsWeakPtr();
     44   utility_process_host_->EnableZygote();
     45   utility_process_host_->Send(new ChromeUtilityMsg_StartupPing);
     46 }
     47 
     48 SafeAudioVideoChecker::~SafeAudioVideoChecker() {}
     49 
     50 void SafeAudioVideoChecker::OnProcessStarted() {
     51   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
     52   if (state_ != PINGED_STATE)
     53     return;
     54   state_ = STARTED_STATE;
     55 
     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::GetFileHandleForProcess(*file_closer_.release(),
     60                                    utility_process_host_->GetData().handle,
     61                                    true /* close_source_handle */);
     62   const int64 kFileDecodeTimeInMS = 250;
     63   utility_process_host_->Send(new ChromeUtilityMsg_CheckMediaFile(
     64       kFileDecodeTimeInMS, file_for_transit));
     65 }
     66 
     67 void SafeAudioVideoChecker::OnCheckingFinished(bool valid) {
     68   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
     69   if (state_ != STARTED_STATE)
     70     return;
     71   state_ = FINISHED_STATE;
     72 
     73   callback_.Run(valid ? base::PLATFORM_FILE_OK
     74                       : base::PLATFORM_FILE_ERROR_SECURITY);
     75 }
     76 
     77 void SafeAudioVideoChecker::OnProcessCrashed(int exit_code) {
     78   OnCheckingFinished(false);
     79 }
     80 
     81 bool SafeAudioVideoChecker::OnMessageReceived(const IPC::Message& message) {
     82   bool handled = true;
     83   IPC_BEGIN_MESSAGE_MAP(SafeAudioVideoChecker, message)
     84     IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted,
     85         OnProcessStarted)
     86     IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CheckMediaFile_Finished,
     87         OnCheckingFinished)
     88     IPC_MESSAGE_UNHANDLED(handled = false)
     89   IPC_END_MESSAGE_MAP()
     90   return handled;
     91 }
     92