1// Copyright (c) 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 "content/shell/shell_message_filter.h"
6
7#include "base/file_util.h"
8#include "base/threading/thread_restrictions.h"
9#include "content/public/browser/child_process_security_policy.h"
10#include "content/shell/common/shell_messages.h"
11#include "content/shell/shell_browser_context.h"
12#include "content/shell/shell_content_browser_client.h"
13#include "content/shell/shell_network_delegate.h"
14#include "net/base/net_errors.h"
15#include "net/cookies/cookie_monster.h"
16#include "net/url_request/url_request_context.h"
17#include "net/url_request/url_request_context_getter.h"
18#include "webkit/browser/database/database_tracker.h"
19#include "webkit/browser/fileapi/isolated_context.h"
20#include "webkit/browser/quota/quota_manager.h"
21
22namespace content {
23
24ShellMessageFilter::ShellMessageFilter(
25    int render_process_id,
26    webkit_database::DatabaseTracker* database_tracker,
27    quota::QuotaManager* quota_manager,
28    net::URLRequestContextGetter* request_context_getter)
29    : render_process_id_(render_process_id),
30      database_tracker_(database_tracker),
31      quota_manager_(quota_manager),
32      request_context_getter_(request_context_getter) {
33}
34
35ShellMessageFilter::~ShellMessageFilter() {
36}
37
38void ShellMessageFilter::OverrideThreadForMessage(const IPC::Message& message,
39                                                  BrowserThread::ID* thread) {
40  if (message.type() == ShellViewHostMsg_ClearAllDatabases::ID)
41    *thread = BrowserThread::FILE;
42}
43
44bool ShellMessageFilter::OnMessageReceived(const IPC::Message& message,
45                                           bool* message_was_ok) {
46  bool handled = true;
47  IPC_BEGIN_MESSAGE_MAP_EX(ShellMessageFilter, message, *message_was_ok)
48    IPC_MESSAGE_HANDLER(ShellViewHostMsg_ReadFileToString, OnReadFileToString)
49    IPC_MESSAGE_HANDLER(ShellViewHostMsg_RegisterIsolatedFileSystem,
50                        OnRegisterIsolatedFileSystem)
51    IPC_MESSAGE_HANDLER(ShellViewHostMsg_ClearAllDatabases, OnClearAllDatabases)
52    IPC_MESSAGE_HANDLER(ShellViewHostMsg_SetDatabaseQuota, OnSetDatabaseQuota)
53    IPC_MESSAGE_HANDLER(ShellViewHostMsg_AcceptAllCookies, OnAcceptAllCookies)
54    IPC_MESSAGE_HANDLER(ShellViewHostMsg_DeleteAllCookies, OnDeleteAllCookies)
55    IPC_MESSAGE_UNHANDLED(handled = false)
56  IPC_END_MESSAGE_MAP()
57
58  return handled;
59}
60
61void ShellMessageFilter::OnReadFileToString(const base::FilePath& local_file,
62                                            std::string* contents) {
63  base::ThreadRestrictions::ScopedAllowIO allow_io;
64  file_util::ReadFileToString(local_file, contents);
65}
66
67void ShellMessageFilter::OnRegisterIsolatedFileSystem(
68    const std::vector<base::FilePath>& absolute_filenames,
69    std::string* filesystem_id) {
70  fileapi::IsolatedContext::FileInfoSet files;
71  ChildProcessSecurityPolicy* policy =
72      ChildProcessSecurityPolicy::GetInstance();
73  for (size_t i = 0; i < absolute_filenames.size(); ++i) {
74    files.AddPath(absolute_filenames[i], NULL);
75    if (!policy->CanReadFile(render_process_id_, absolute_filenames[i]))
76      policy->GrantReadFile(render_process_id_, absolute_filenames[i]);
77  }
78  *filesystem_id =
79      fileapi::IsolatedContext::GetInstance()->RegisterDraggedFileSystem(files);
80  policy->GrantReadFileSystem(render_process_id_, *filesystem_id);
81}
82
83void ShellMessageFilter::OnClearAllDatabases() {
84  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
85  database_tracker_->DeleteDataModifiedSince(
86      base::Time(), net::CompletionCallback());
87}
88
89void ShellMessageFilter::OnSetDatabaseQuota(int quota) {
90  quota_manager_->SetTemporaryGlobalOverrideQuota(
91      quota * quota::QuotaManager::kPerHostTemporaryPortion,
92      quota::QuotaCallback());
93}
94
95void ShellMessageFilter::OnAcceptAllCookies(bool accept) {
96  ShellNetworkDelegate::SetAcceptAllCookies(accept);
97}
98
99void ShellMessageFilter::OnDeleteAllCookies() {
100  request_context_getter_->GetURLRequestContext()->cookie_store()
101      ->GetCookieMonster()
102      ->DeleteAllAsync(net::CookieMonster::DeleteCallback());
103}
104
105}  // namespace content
106