1// Copyright (c) 2012 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 "content/browser/renderer_host/file_utilities_message_filter.h"
6
7#include "base/files/file_util.h"
8#include "content/browser/child_process_security_policy_impl.h"
9#include "content/common/file_utilities_messages.h"
10
11namespace content {
12
13FileUtilitiesMessageFilter::FileUtilitiesMessageFilter(int process_id)
14    : BrowserMessageFilter(FileUtilitiesMsgStart),
15      process_id_(process_id) {
16}
17
18FileUtilitiesMessageFilter::~FileUtilitiesMessageFilter() {
19}
20
21void FileUtilitiesMessageFilter::OverrideThreadForMessage(
22    const IPC::Message& message,
23    BrowserThread::ID* thread) {
24  if (IPC_MESSAGE_CLASS(message) == FileUtilitiesMsgStart)
25    *thread = BrowserThread::FILE;
26}
27
28bool FileUtilitiesMessageFilter::OnMessageReceived(
29    const IPC::Message& message) {
30  bool handled = true;
31  IPC_BEGIN_MESSAGE_MAP(FileUtilitiesMessageFilter, message)
32    IPC_MESSAGE_HANDLER(FileUtilitiesMsg_GetFileInfo, OnGetFileInfo)
33    IPC_MESSAGE_UNHANDLED(handled = false)
34  IPC_END_MESSAGE_MAP()
35  return handled;
36}
37
38void FileUtilitiesMessageFilter::OnGetFileInfo(
39    const base::FilePath& path,
40    base::File::Info* result,
41    base::File::Error* status) {
42  *result = base::File::Info();
43  *status = base::File::FILE_OK;
44
45  // Get file metadata only when the child process has been granted
46  // permission to read the file.
47  if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
48      process_id_, path)) {
49    return;
50  }
51
52  if (!base::GetFileInfo(path, result))
53    *status = base::File::FILE_ERROR_FAILED;
54}
55
56}  // namespace content
57