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 "content/shell/browser/shell_message_filter.h"
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/browser/shell_browser_context.h"
11 #include "content/shell/browser/shell_content_browser_client.h"
12 #include "content/shell/browser/shell_network_delegate.h"
13 #include "content/shell/browser/shell_notification_manager.h"
14 #include "content/shell/common/shell_messages.h"
15 #include "net/base/net_errors.h"
16 #include "net/cookies/cookie_monster.h"
17 #include "net/url_request/url_request_context.h"
18 #include "net/url_request/url_request_context_getter.h"
19 #include "webkit/browser/database/database_tracker.h"
20 #include "webkit/browser/fileapi/isolated_context.h"
21 #include "webkit/browser/quota/quota_manager.h"
25 ShellMessageFilter::ShellMessageFilter(
26 int render_process_id,
27 webkit_database::DatabaseTracker* database_tracker,
28 quota::QuotaManager* quota_manager,
29 net::URLRequestContextGetter* request_context_getter)
30 : BrowserMessageFilter(ShellMsgStart),
31 render_process_id_(render_process_id),
32 database_tracker_(database_tracker),
33 quota_manager_(quota_manager),
34 request_context_getter_(request_context_getter) {
37 ShellMessageFilter::~ShellMessageFilter() {
40 void ShellMessageFilter::OverrideThreadForMessage(const IPC::Message& message,
41 BrowserThread::ID* thread) {
42 if (message.type() == ShellViewHostMsg_ClearAllDatabases::ID)
43 *thread = BrowserThread::FILE;
46 bool ShellMessageFilter::OnMessageReceived(const IPC::Message& message) {
48 IPC_BEGIN_MESSAGE_MAP(ShellMessageFilter, message)
49 IPC_MESSAGE_HANDLER(ShellViewHostMsg_ReadFileToString, OnReadFileToString)
50 IPC_MESSAGE_HANDLER(ShellViewHostMsg_RegisterIsolatedFileSystem,
51 OnRegisterIsolatedFileSystem)
52 IPC_MESSAGE_HANDLER(ShellViewHostMsg_ClearAllDatabases, OnClearAllDatabases)
53 IPC_MESSAGE_HANDLER(ShellViewHostMsg_SetDatabaseQuota, OnSetDatabaseQuota)
54 IPC_MESSAGE_HANDLER(ShellViewHostMsg_CheckWebNotificationPermission,
55 OnCheckWebNotificationPermission)
56 IPC_MESSAGE_HANDLER(ShellViewHostMsg_GrantWebNotificationPermission,
57 OnGrantWebNotificationPermission)
58 IPC_MESSAGE_HANDLER(ShellViewHostMsg_ClearWebNotificationPermissions,
59 OnClearWebNotificationPermissions)
60 IPC_MESSAGE_HANDLER(ShellViewHostMsg_AcceptAllCookies, OnAcceptAllCookies)
61 IPC_MESSAGE_HANDLER(ShellViewHostMsg_DeleteAllCookies, OnDeleteAllCookies)
62 IPC_MESSAGE_UNHANDLED(handled = false)
68 void ShellMessageFilter::OnReadFileToString(const base::FilePath& local_file,
69 std::string* contents) {
70 base::ThreadRestrictions::ScopedAllowIO allow_io;
71 base::ReadFileToString(local_file, contents);
74 void ShellMessageFilter::OnRegisterIsolatedFileSystem(
75 const std::vector<base::FilePath>& absolute_filenames,
76 std::string* filesystem_id) {
77 fileapi::IsolatedContext::FileInfoSet files;
78 ChildProcessSecurityPolicy* policy =
79 ChildProcessSecurityPolicy::GetInstance();
80 for (size_t i = 0; i < absolute_filenames.size(); ++i) {
81 files.AddPath(absolute_filenames[i], NULL);
82 if (!policy->CanReadFile(render_process_id_, absolute_filenames[i]))
83 policy->GrantReadFile(render_process_id_, absolute_filenames[i]);
86 fileapi::IsolatedContext::GetInstance()->RegisterDraggedFileSystem(files);
87 policy->GrantReadFileSystem(render_process_id_, *filesystem_id);
90 void ShellMessageFilter::OnClearAllDatabases() {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
92 database_tracker_->DeleteDataModifiedSince(
93 base::Time(), net::CompletionCallback());
96 void ShellMessageFilter::OnSetDatabaseQuota(int quota) {
97 quota_manager_->SetTemporaryGlobalOverrideQuota(
98 quota * quota::QuotaManager::kPerHostTemporaryPortion,
99 quota::QuotaCallback());
102 void ShellMessageFilter::OnCheckWebNotificationPermission(const GURL& origin,
104 ShellNotificationManager* manager =
105 ShellContentBrowserClient::Get()->GetShellNotificationManager();
107 *result = manager->CheckPermission(origin);
109 *result = blink::WebNotificationPermissionAllowed;
112 void ShellMessageFilter::OnGrantWebNotificationPermission(
113 const GURL& origin, bool permission_granted) {
114 ShellNotificationManager* manager =
115 ShellContentBrowserClient::Get()->GetShellNotificationManager();
117 manager->SetPermission(origin, permission_granted ?
118 blink::WebNotificationPermissionAllowed :
119 blink::WebNotificationPermissionDenied);
123 void ShellMessageFilter::OnClearWebNotificationPermissions() {
124 ShellNotificationManager* manager =
125 ShellContentBrowserClient::Get()->GetShellNotificationManager();
127 manager->ClearPermissions();
130 void ShellMessageFilter::OnAcceptAllCookies(bool accept) {
131 ShellNetworkDelegate::SetAcceptAllCookies(accept);
134 void ShellMessageFilter::OnDeleteAllCookies() {
135 request_context_getter_->GetURLRequestContext()->cookie_store()
137 ->DeleteAllAsync(net::CookieMonster::DeleteCallback());
140 } // namespace content