OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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/file_system/file_system_dispatcher_host.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/file_path.h" | |
11 #include "base/threading/thread.h" | |
12 #include "base/time.h" | |
13 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
14 #include "chrome/browser/net/chrome_url_request_context.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/renderer_host/browser_render_process_host.h" | |
17 #include "chrome/common/net/url_request_context_getter.h" | |
18 #include "chrome/common/render_messages.h" | |
19 #include "chrome/common/render_messages_params.h" | |
20 #include "googleurl/src/gurl.h" | |
21 #include "net/url_request/url_request_context.h" | |
22 #include "webkit/fileapi/file_system_callback_dispatcher.h" | |
23 #include "webkit/fileapi/file_system_context.h" | |
24 #include "webkit/fileapi/file_system_operation.h" | |
25 #include "webkit/fileapi/file_system_operation.h" | |
26 #include "webkit/fileapi/file_system_path_manager.h" | |
27 #include "webkit/fileapi/file_system_quota_manager.h" | |
28 | |
29 using fileapi::FileSystemCallbackDispatcher; | |
30 using fileapi::FileSystemOperation; | |
31 using fileapi::FileSystemQuotaManager; | |
32 | |
33 class BrowserFileSystemCallbackDispatcher | |
34 : public FileSystemCallbackDispatcher { | |
35 public: | |
36 BrowserFileSystemCallbackDispatcher( | |
37 FileSystemDispatcherHost* dispatcher_host, int request_id) | |
38 : dispatcher_host_(dispatcher_host), | |
39 request_id_(request_id) { | |
40 DCHECK(dispatcher_host_); | |
41 } | |
42 | |
43 virtual ~BrowserFileSystemCallbackDispatcher() { | |
44 dispatcher_host_->UnregisterOperation(request_id_); | |
45 } | |
46 | |
47 virtual void DidSucceed() { | |
48 dispatcher_host_->Send(new ViewMsg_FileSystem_DidSucceed(request_id_)); | |
49 } | |
50 | |
51 virtual void DidReadMetadata(const base::PlatformFileInfo& info) { | |
52 dispatcher_host_->Send(new ViewMsg_FileSystem_DidReadMetadata( | |
53 request_id_, info)); | |
54 } | |
55 | |
56 virtual void DidReadDirectory( | |
57 const std::vector<base::FileUtilProxy::Entry>& entries, bool has_more) { | |
58 dispatcher_host_->Send(new ViewMsg_FileSystem_DidReadDirectory( | |
59 request_id_, entries, has_more)); | |
60 } | |
61 | |
62 virtual void DidOpenFileSystem(const std::string& name, | |
63 const FilePath& path) { | |
64 dispatcher_host_->Send( | |
65 new ViewMsg_OpenFileSystemRequest_Complete( | |
66 request_id_, !path.empty(), name, path)); | |
67 } | |
68 | |
69 virtual void DidFail(base::PlatformFileError error_code) { | |
70 dispatcher_host_->Send(new ViewMsg_FileSystem_DidFail( | |
71 request_id_, error_code)); | |
72 } | |
73 | |
74 virtual void DidWrite(int64 bytes, bool complete) { | |
75 dispatcher_host_->Send(new ViewMsg_FileSystem_DidWrite( | |
76 request_id_, bytes, complete)); | |
77 } | |
78 | |
79 private: | |
80 scoped_refptr<FileSystemDispatcherHost> dispatcher_host_; | |
81 int request_id_; | |
82 }; | |
83 | |
84 FileSystemDispatcherHost::FileSystemDispatcherHost(Profile* profile) | |
85 : context_(profile->GetFileSystemContext()), | |
86 host_content_settings_map_(profile->GetHostContentSettingsMap()), | |
87 request_context_getter_(profile->GetRequestContext()) { | |
88 } | |
89 | |
90 FileSystemDispatcherHost::FileSystemDispatcherHost( | |
91 ChromeURLRequestContext* context) | |
92 : context_(context->file_system_context()), | |
93 host_content_settings_map_(context->host_content_settings_map()), | |
94 request_context_(context) { | |
95 } | |
96 | |
97 FileSystemDispatcherHost::~FileSystemDispatcherHost() { | |
98 } | |
99 | |
100 void FileSystemDispatcherHost::OnChannelConnected(int32 peer_pid) { | |
101 BrowserMessageFilter::OnChannelConnected(peer_pid); | |
102 | |
103 if (request_context_getter_.get()) { | |
104 DCHECK(!request_context_.get()); | |
105 request_context_ = request_context_getter_->GetURLRequestContext(); | |
106 } | |
107 DCHECK(request_context_.get()); | |
108 } | |
109 | |
110 bool FileSystemDispatcherHost::OnMessageReceived( | |
111 const IPC::Message& message, bool* message_was_ok) { | |
112 *message_was_ok = true; | |
113 bool handled = true; | |
114 IPC_BEGIN_MESSAGE_MAP_EX(FileSystemDispatcherHost, message, *message_was_ok) | |
115 IPC_MESSAGE_HANDLER(ViewHostMsg_OpenFileSystemRequest, OnOpenFileSystem) | |
116 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_Move, OnMove) | |
117 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_Copy, OnCopy) | |
118 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_Remove, OnRemove) | |
119 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_ReadMetadata, OnReadMetadata) | |
120 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_Create, OnCreate) | |
121 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_Exists, OnExists) | |
122 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_ReadDirectory, OnReadDirectory) | |
123 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_Write, OnWrite) | |
124 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_Truncate, OnTruncate) | |
125 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_TouchFile, OnTouchFile) | |
126 IPC_MESSAGE_HANDLER(ViewHostMsg_FileSystem_CancelWrite, OnCancel) | |
127 IPC_MESSAGE_UNHANDLED(handled = false) | |
128 IPC_END_MESSAGE_MAP_EX() | |
129 return handled; | |
130 } | |
131 | |
132 void FileSystemDispatcherHost::OnOpenFileSystem( | |
133 int request_id, const GURL& origin_url, fileapi::FileSystemType type, | |
134 int64 requested_size, bool create) { | |
135 ContentSetting content_setting = | |
136 host_content_settings_map_->GetContentSetting( | |
137 origin_url, CONTENT_SETTINGS_TYPE_COOKIES, ""); | |
138 DCHECK((content_setting == CONTENT_SETTING_ALLOW) || | |
139 (content_setting == CONTENT_SETTING_BLOCK) || | |
140 (content_setting == CONTENT_SETTING_SESSION_ONLY)); | |
141 if (content_setting == CONTENT_SETTING_BLOCK) { | |
142 // TODO(kinuko): Need to notify the UI thread to indicate that | |
143 // there's a blocked content. | |
144 Send(new ViewMsg_OpenFileSystemRequest_Complete( | |
145 request_id, false, std::string(), FilePath())); | |
146 return; | |
147 } | |
148 | |
149 GetNewOperation(request_id)->OpenFileSystem(origin_url, type, create); | |
150 } | |
151 | |
152 void FileSystemDispatcherHost::OnMove( | |
153 int request_id, const FilePath& src_path, const FilePath& dest_path) { | |
154 GetNewOperation(request_id)->Move(src_path, dest_path); | |
155 } | |
156 | |
157 void FileSystemDispatcherHost::OnCopy( | |
158 int request_id, const FilePath& src_path, const FilePath& dest_path) { | |
159 GetNewOperation(request_id)->Copy(src_path, dest_path); | |
160 } | |
161 | |
162 void FileSystemDispatcherHost::OnRemove( | |
163 int request_id, const FilePath& path, bool recursive) { | |
164 GetNewOperation(request_id)->Remove(path, recursive); | |
165 } | |
166 | |
167 void FileSystemDispatcherHost::OnReadMetadata( | |
168 int request_id, const FilePath& path) { | |
169 GetNewOperation(request_id)->GetMetadata(path); | |
170 } | |
171 | |
172 void FileSystemDispatcherHost::OnCreate( | |
173 int request_id, const FilePath& path, bool exclusive, | |
174 bool is_directory, bool recursive) { | |
175 if (is_directory) | |
176 GetNewOperation(request_id)->CreateDirectory(path, exclusive, recursive); | |
177 else | |
178 GetNewOperation(request_id)->CreateFile(path, exclusive); | |
179 } | |
180 | |
181 void FileSystemDispatcherHost::OnExists( | |
182 int request_id, const FilePath& path, bool is_directory) { | |
183 if (is_directory) | |
184 GetNewOperation(request_id)->DirectoryExists(path); | |
185 else | |
186 GetNewOperation(request_id)->FileExists(path); | |
187 } | |
188 | |
189 void FileSystemDispatcherHost::OnReadDirectory( | |
190 int request_id, const FilePath& path) { | |
191 GetNewOperation(request_id)->ReadDirectory(path); | |
192 } | |
193 | |
194 void FileSystemDispatcherHost::OnWrite( | |
195 int request_id, | |
196 const FilePath& path, | |
197 const GURL& blob_url, | |
198 int64 offset) { | |
199 GetNewOperation(request_id)->Write( | |
200 request_context_, path, blob_url, offset); | |
201 } | |
202 | |
203 void FileSystemDispatcherHost::OnTruncate( | |
204 int request_id, | |
205 const FilePath& path, | |
206 int64 length) { | |
207 GetNewOperation(request_id)->Truncate(path, length); | |
208 } | |
209 | |
210 void FileSystemDispatcherHost::OnTouchFile( | |
211 int request_id, | |
212 const FilePath& path, | |
213 const base::Time& last_access_time, | |
214 const base::Time& last_modified_time) { | |
215 GetNewOperation(request_id)->TouchFile( | |
216 path, last_access_time, last_modified_time); | |
217 } | |
218 | |
219 void FileSystemDispatcherHost::OnCancel( | |
220 int request_id, | |
221 int request_id_to_cancel) { | |
222 FileSystemOperation* write = operations_.Lookup( | |
223 request_id_to_cancel); | |
224 if (write) { | |
225 // The cancel will eventually send both the write failure and the cancel | |
226 // success. | |
227 write->Cancel(GetNewOperation(request_id)); | |
228 } else { | |
229 // The write already finished; report that we failed to stop it. | |
230 Send(new ViewMsg_FileSystem_DidFail( | |
231 request_id, base::PLATFORM_FILE_ERROR_INVALID_OPERATION)); | |
232 } | |
233 } | |
234 | |
235 FileSystemOperation* FileSystemDispatcherHost::GetNewOperation( | |
236 int request_id) { | |
237 BrowserFileSystemCallbackDispatcher* dispatcher = | |
238 new BrowserFileSystemCallbackDispatcher(this, request_id); | |
239 FileSystemOperation* operation = new FileSystemOperation( | |
240 dispatcher, | |
241 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | |
242 context_); | |
243 operations_.AddWithID(operation, request_id); | |
244 return operation; | |
245 } | |
246 | |
247 void FileSystemDispatcherHost::UnregisterOperation(int request_id) { | |
248 DCHECK(operations_.Lookup(request_id)); | |
249 operations_.Remove(request_id); | |
250 } | |
OLD | NEW |