Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(956)

Side by Side Diff: content/browser/fileapi/fileapi_message_filter.cc

Issue 15817013: Add Stream support to WebBlobRegistry and FileAPIMessageFilter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/fileapi/fileapi_message_filter.h" 5 #include "content/browser/fileapi/fileapi_message_filter.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "base/platform_file.h" 14 #include "base/platform_file.h"
14 #include "base/sequenced_task_runner.h" 15 #include "base/sequenced_task_runner.h"
16 #include "base/strings/string_util.h"
15 #include "base/threading/thread.h" 17 #include "base/threading/thread.h"
16 #include "base/time/time.h" 18 #include "base/time/time.h"
17 #include "content/browser/child_process_security_policy_impl.h" 19 #include "content/browser/child_process_security_policy_impl.h"
18 #include "content/browser/fileapi/browser_file_system_helper.h" 20 #include "content/browser/fileapi/browser_file_system_helper.h"
19 #include "content/browser/fileapi/chrome_blob_storage_context.h" 21 #include "content/browser/fileapi/chrome_blob_storage_context.h"
22 #include "content/browser/streams/stream_registry.h"
20 #include "content/common/fileapi/file_system_messages.h" 23 #include "content/common/fileapi/file_system_messages.h"
21 #include "content/common/fileapi/webblob_messages.h" 24 #include "content/common/fileapi/webblob_messages.h"
22 #include "content/public/browser/user_metrics.h" 25 #include "content/public/browser/user_metrics.h"
23 #include "ipc/ipc_platform_file.h" 26 #include "ipc/ipc_platform_file.h"
24 #include "net/base/mime_util.h" 27 #include "net/base/mime_util.h"
25 #include "net/url_request/url_request_context.h" 28 #include "net/url_request/url_request_context.h"
26 #include "net/url_request/url_request_context_getter.h" 29 #include "net/url_request/url_request_context_getter.h"
27 #include "url/gurl.h" 30 #include "url/gurl.h"
28 #include "webkit/browser/blob/blob_storage_controller.h" 31 #include "webkit/browser/blob/blob_storage_controller.h"
29 #include "webkit/browser/fileapi/file_observers.h" 32 #include "webkit/browser/fileapi/file_observers.h"
(...skipping 24 matching lines...) Expand all
54 ChildProcessSecurityPolicyImpl::GetInstance()->RevokeAllPermissionsForFile( 57 ChildProcessSecurityPolicyImpl::GetInstance()->RevokeAllPermissionsForFile(
55 child_id, path); 58 child_id, path);
56 } 59 }
57 60
58 } // namespace 61 } // namespace
59 62
60 FileAPIMessageFilter::FileAPIMessageFilter( 63 FileAPIMessageFilter::FileAPIMessageFilter(
61 int process_id, 64 int process_id,
62 net::URLRequestContextGetter* request_context_getter, 65 net::URLRequestContextGetter* request_context_getter,
63 fileapi::FileSystemContext* file_system_context, 66 fileapi::FileSystemContext* file_system_context,
64 ChromeBlobStorageContext* blob_storage_context) 67 ChromeBlobStorageContext* blob_storage_context,
68 StreamContext* stream_context)
65 : process_id_(process_id), 69 : process_id_(process_id),
66 context_(file_system_context), 70 context_(file_system_context),
67 request_context_getter_(request_context_getter), 71 request_context_getter_(request_context_getter),
68 request_context_(NULL), 72 request_context_(NULL),
69 blob_storage_context_(blob_storage_context) { 73 blob_storage_context_(blob_storage_context),
74 stream_context_(stream_context) {
70 DCHECK(context_); 75 DCHECK(context_);
71 DCHECK(request_context_getter_.get()); 76 DCHECK(request_context_getter_.get());
72 DCHECK(blob_storage_context); 77 DCHECK(blob_storage_context);
78 DCHECK(stream_context);
73 } 79 }
74 80
75 FileAPIMessageFilter::FileAPIMessageFilter( 81 FileAPIMessageFilter::FileAPIMessageFilter(
76 int process_id, 82 int process_id,
77 net::URLRequestContext* request_context, 83 net::URLRequestContext* request_context,
78 fileapi::FileSystemContext* file_system_context, 84 fileapi::FileSystemContext* file_system_context,
79 ChromeBlobStorageContext* blob_storage_context) 85 ChromeBlobStorageContext* blob_storage_context,
86 StreamContext* stream_context)
80 : process_id_(process_id), 87 : process_id_(process_id),
81 context_(file_system_context), 88 context_(file_system_context),
82 request_context_(request_context), 89 request_context_(request_context),
83 blob_storage_context_(blob_storage_context) { 90 blob_storage_context_(blob_storage_context),
91 stream_context_(stream_context) {
84 DCHECK(context_); 92 DCHECK(context_);
85 DCHECK(request_context_); 93 DCHECK(request_context_);
86 DCHECK(blob_storage_context); 94 DCHECK(blob_storage_context);
95 DCHECK(stream_context);
87 } 96 }
88 97
89 void FileAPIMessageFilter::OnChannelConnected(int32 peer_pid) { 98 void FileAPIMessageFilter::OnChannelConnected(int32 peer_pid) {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
91 BrowserMessageFilter::OnChannelConnected(peer_pid); 100 BrowserMessageFilter::OnChannelConnected(peer_pid);
92 101
93 if (request_context_getter_.get()) { 102 if (request_context_getter_.get()) {
94 DCHECK(!request_context_); 103 DCHECK(!request_context_);
95 request_context_ = request_context_getter_->GetURLRequestContext(); 104 request_context_ = request_context_getter_->GetURLRequestContext();
96 request_context_getter_ = NULL; 105 request_context_getter_ = NULL;
97 DCHECK(request_context_); 106 DCHECK(request_context_);
98 } 107 }
99 108
100 operation_runner_ = context_->CreateFileSystemOperationRunner(); 109 operation_runner_ = context_->CreateFileSystemOperationRunner();
101 } 110 }
102 111
103 void FileAPIMessageFilter::OnChannelClosing() { 112 void FileAPIMessageFilter::OnChannelClosing() {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
105 BrowserMessageFilter::OnChannelClosing(); 114 BrowserMessageFilter::OnChannelClosing();
106 115
107 // Unregister all the blob URLs that are previously registered in this 116 // Unregister all the blob and stream URLs that are previously registered in
108 // process. 117 // this process.
109 for (base::hash_set<std::string>::const_iterator iter = blob_urls_.begin(); 118 for (base::hash_set<std::string>::const_iterator iter = blob_urls_.begin();
110 iter != blob_urls_.end(); ++iter) { 119 iter != blob_urls_.end(); ++iter) {
111 blob_storage_context_->controller()->RemoveBlob(GURL(*iter)); 120 blob_storage_context_->controller()->RemoveBlob(GURL(*iter));
112 } 121 }
122 for (base::hash_set<std::string>::const_iterator iter = stream_urls_.begin();
123 iter != stream_urls_.end(); ++iter) {
124 stream_context_->registry()->UnregisterStream(GURL(*iter));
125 }
113 126
114 in_transit_snapshot_files_.clear(); 127 in_transit_snapshot_files_.clear();
115 128
116 // Close all files that are previously OpenFile()'ed in this process. 129 // Close all files that are previously OpenFile()'ed in this process.
117 if (!on_close_callbacks_.IsEmpty()) { 130 if (!on_close_callbacks_.IsEmpty()) {
118 DLOG(INFO) 131 DLOG(INFO)
119 << "File API: Renderer process shut down before NotifyCloseFile" 132 << "File API: Renderer process shut down before NotifyCloseFile"
120 << " for " << on_close_callbacks_.size() << " files opened in PPAPI"; 133 << " for " << on_close_callbacks_.size() << " files opened in PPAPI";
121 } 134 }
122 135
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 IPC_MESSAGE_HANDLER(FileSystemHostMsg_OpenFile, OnOpenFile) 174 IPC_MESSAGE_HANDLER(FileSystemHostMsg_OpenFile, OnOpenFile)
162 IPC_MESSAGE_HANDLER(FileSystemHostMsg_NotifyCloseFile, OnNotifyCloseFile) 175 IPC_MESSAGE_HANDLER(FileSystemHostMsg_NotifyCloseFile, OnNotifyCloseFile)
163 IPC_MESSAGE_HANDLER(FileSystemHostMsg_CreateSnapshotFile, 176 IPC_MESSAGE_HANDLER(FileSystemHostMsg_CreateSnapshotFile,
164 OnCreateSnapshotFile) 177 OnCreateSnapshotFile)
165 IPC_MESSAGE_HANDLER(FileSystemHostMsg_DidReceiveSnapshotFile, 178 IPC_MESSAGE_HANDLER(FileSystemHostMsg_DidReceiveSnapshotFile,
166 OnDidReceiveSnapshotFile) 179 OnDidReceiveSnapshotFile)
167 IPC_MESSAGE_HANDLER(FileSystemHostMsg_WillUpdate, OnWillUpdate) 180 IPC_MESSAGE_HANDLER(FileSystemHostMsg_WillUpdate, OnWillUpdate)
168 IPC_MESSAGE_HANDLER(FileSystemHostMsg_DidUpdate, OnDidUpdate) 181 IPC_MESSAGE_HANDLER(FileSystemHostMsg_DidUpdate, OnDidUpdate)
169 IPC_MESSAGE_HANDLER(FileSystemHostMsg_SyncGetPlatformPath, 182 IPC_MESSAGE_HANDLER(FileSystemHostMsg_SyncGetPlatformPath,
170 OnSyncGetPlatformPath) 183 OnSyncGetPlatformPath)
171 IPC_MESSAGE_HANDLER(BlobHostMsg_StartBuildingBlob, OnStartBuildingBlob) 184 IPC_MESSAGE_HANDLER(BlobHostMsg_StartBuilding, OnStartBuildingBlob)
172 IPC_MESSAGE_HANDLER(BlobHostMsg_AppendBlobDataItem, OnAppendBlobDataItem) 185 IPC_MESSAGE_HANDLER(BlobHostMsg_AppendBlobDataItem,
186 OnAppendBlobDataItemToBlob)
173 IPC_MESSAGE_HANDLER(BlobHostMsg_SyncAppendSharedMemory, 187 IPC_MESSAGE_HANDLER(BlobHostMsg_SyncAppendSharedMemory,
174 OnAppendSharedMemory) 188 OnAppendSharedMemoryToBlob)
175 IPC_MESSAGE_HANDLER(BlobHostMsg_FinishBuildingBlob, OnFinishBuildingBlob) 189 IPC_MESSAGE_HANDLER(BlobHostMsg_FinishBuilding, OnFinishBuildingBlob)
176 IPC_MESSAGE_HANDLER(BlobHostMsg_CloneBlob, OnCloneBlob) 190 IPC_MESSAGE_HANDLER(BlobHostMsg_Clone, OnCloneBlob)
177 IPC_MESSAGE_HANDLER(BlobHostMsg_RemoveBlob, OnRemoveBlob) 191 IPC_MESSAGE_HANDLER(BlobHostMsg_Remove, OnRemoveBlob)
192 IPC_MESSAGE_HANDLER(StreamHostMsg_StartBuilding, OnStartBuildingStream)
193 IPC_MESSAGE_HANDLER(StreamHostMsg_AppendBlobDataItem,
194 OnAppendBlobDataItemToStream)
195 IPC_MESSAGE_HANDLER(StreamHostMsg_SyncAppendSharedMemory,
196 OnAppendSharedMemoryToStream)
197 IPC_MESSAGE_HANDLER(StreamHostMsg_FinishBuilding, OnFinishBuildingStream)
198 IPC_MESSAGE_HANDLER(StreamHostMsg_Clone, OnCloneStream)
199 IPC_MESSAGE_HANDLER(StreamHostMsg_Remove, OnRemoveStream)
178 IPC_MESSAGE_UNHANDLED(handled = false) 200 IPC_MESSAGE_UNHANDLED(handled = false)
179 IPC_END_MESSAGE_MAP_EX() 201 IPC_END_MESSAGE_MAP_EX()
180 return handled; 202 return handled;
181 } 203 }
182 204
183 FileAPIMessageFilter::~FileAPIMessageFilter() {} 205 FileAPIMessageFilter::~FileAPIMessageFilter() {}
184 206
185 void FileAPIMessageFilter::BadMessageReceived() { 207 void FileAPIMessageFilter::BadMessageReceived() {
186 RecordAction(UserMetricsAction("BadMessageTerminate_FAMF")); 208 RecordAction(UserMetricsAction("BadMessageTerminate_FAMF"));
187 BrowserMessageFilter::BadMessageReceived(); 209 BrowserMessageFilter::BadMessageReceived();
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 535 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
514 in_transit_snapshot_files_.erase(request_id); 536 in_transit_snapshot_files_.erase(request_id);
515 } 537 }
516 538
517 void FileAPIMessageFilter::OnStartBuildingBlob(const GURL& url) { 539 void FileAPIMessageFilter::OnStartBuildingBlob(const GURL& url) {
518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 540 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
519 blob_storage_context_->controller()->StartBuildingBlob(url); 541 blob_storage_context_->controller()->StartBuildingBlob(url);
520 blob_urls_.insert(url.spec()); 542 blob_urls_.insert(url.spec());
521 } 543 }
522 544
523 void FileAPIMessageFilter::OnAppendBlobDataItem( 545 void FileAPIMessageFilter::OnAppendBlobDataItemToBlob(
524 const GURL& url, const BlobData::Item& item) { 546 const GURL& url, const BlobData::Item& item) {
525 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 547 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
526 if (item.type() == BlobData::Item::TYPE_FILE_FILESYSTEM) { 548 if (item.type() == BlobData::Item::TYPE_FILE_FILESYSTEM) {
527 base::PlatformFileError error; 549 base::PlatformFileError error;
528 FileSystemURL filesystem_url(context_->CrackURL(item.url())); 550 FileSystemURL filesystem_url(context_->CrackURL(item.url()));
529 if (!HasPermissionsForFile(filesystem_url, 551 if (!HasPermissionsForFile(filesystem_url,
530 fileapi::kReadFilePermissions, &error)) { 552 fileapi::kReadFilePermissions, &error)) {
531 OnRemoveBlob(url); 553 OnRemoveBlob(url);
532 return; 554 return;
533 } 555 }
534 } 556 }
535 if (item.type() == BlobData::Item::TYPE_FILE && 557 if (item.type() == BlobData::Item::TYPE_FILE &&
536 !ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( 558 !ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
537 process_id_, item.path())) { 559 process_id_, item.path())) {
538 OnRemoveBlob(url); 560 OnRemoveBlob(url);
539 return; 561 return;
540 } 562 }
541 if (item.length() == 0) { 563 if (item.length() == 0) {
542 BadMessageReceived(); 564 BadMessageReceived();
543 return; 565 return;
544 } 566 }
545 blob_storage_context_->controller()->AppendBlobDataItem(url, item); 567 blob_storage_context_->controller()->AppendBlobDataItem(url, item);
546 } 568 }
547 569
548 void FileAPIMessageFilter::OnAppendSharedMemory( 570 void FileAPIMessageFilter::OnAppendSharedMemoryToBlob(
549 const GURL& url, base::SharedMemoryHandle handle, size_t buffer_size) { 571 const GURL& url, base::SharedMemoryHandle handle, size_t buffer_size) {
550 DCHECK(base::SharedMemory::IsHandleValid(handle)); 572 DCHECK(base::SharedMemory::IsHandleValid(handle));
551 if (!buffer_size) { 573 if (!buffer_size) {
552 BadMessageReceived(); 574 BadMessageReceived();
553 return; 575 return;
554 } 576 }
555 #if defined(OS_WIN) 577 #if defined(OS_WIN)
556 base::SharedMemory shared_memory(handle, true, PeerHandle()); 578 base::SharedMemory shared_memory(handle, true, PeerHandle());
557 #else 579 #else
558 base::SharedMemory shared_memory(handle, true); 580 base::SharedMemory shared_memory(handle, true);
(...skipping 21 matching lines...) Expand all
580 blob_storage_context_->controller()->CloneBlob(url, src_url); 602 blob_storage_context_->controller()->CloneBlob(url, src_url);
581 blob_urls_.insert(url.spec()); 603 blob_urls_.insert(url.spec());
582 } 604 }
583 605
584 void FileAPIMessageFilter::OnRemoveBlob(const GURL& url) { 606 void FileAPIMessageFilter::OnRemoveBlob(const GURL& url) {
585 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 607 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
586 blob_storage_context_->controller()->RemoveBlob(url); 608 blob_storage_context_->controller()->RemoveBlob(url);
587 blob_urls_.erase(url.spec()); 609 blob_urls_.erase(url.spec());
588 } 610 }
589 611
612 void FileAPIMessageFilter::OnStartBuildingStream(
613 const GURL& url, const std::string& content_type) {
614 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
615 // Only an internal Blob URL is expected here. See the BlobURL of the Blink.
616 if (!StartsWithASCII(
617 url.path(), "blobinternal%3A///", true /* case_sensitive */)) {
618 NOTREACHED() << "Malformed Stream URL: " << url.spec();
619 BadMessageReceived();
620 return;
621 }
622 // Use an empty security origin for now. Stream accepts a security origin
623 // but how it's handled is not fixed yet.
624 new Stream(stream_context_->registry(),
625 NULL /* write_observer */,
626 url);
627 stream_urls_.insert(url.spec());
628 }
629
630 void FileAPIMessageFilter::OnAppendBlobDataItemToStream(
631 const GURL& url, const BlobData::Item& item) {
632 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
633
634 scoped_refptr<Stream> stream(GetStreamForURL(url));
635 if (!stream.get()) {
636 NOTREACHED();
637 return;
638 }
639
640 // Data for stream is delivered as TYPE_BYTES item.
641 if (item.type() != BlobData::Item::TYPE_BYTES) {
642 BadMessageReceived();
643 return;
644 }
645 stream->AddData(item.bytes(), item.length());
646 }
647
648 void FileAPIMessageFilter::OnAppendSharedMemoryToStream(
649 const GURL& url, base::SharedMemoryHandle handle, size_t buffer_size) {
650 DCHECK(base::SharedMemory::IsHandleValid(handle));
651 if (!buffer_size) {
652 BadMessageReceived();
653 return;
654 }
655 #if defined(OS_WIN)
656 base::SharedMemory shared_memory(handle, true, PeerHandle());
657 #else
658 base::SharedMemory shared_memory(handle, true);
659 #endif
660 if (!shared_memory.Map(buffer_size)) {
661 OnRemoveStream(url);
662 return;
663 }
664
665 scoped_refptr<Stream> stream(GetStreamForURL(url));
666 if (!stream.get()) {
667 NOTREACHED();
668 return;
669 }
670
671 stream->AddData(static_cast<char*>(shared_memory.memory()), buffer_size);
672 }
673
674 void FileAPIMessageFilter::OnFinishBuildingStream(const GURL& url) {
675 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
676 scoped_refptr<Stream> stream(GetStreamForURL(url));
677 if (stream.get())
678 stream->Finalize();
679 else
680 NOTREACHED();
681 }
682
683 void FileAPIMessageFilter::OnCloneStream(
684 const GURL& url, const GURL& src_url) {
685 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
686 if (!GetStreamForURL(src_url)) {
687 NOTREACHED();
688 return;
689 }
690
691 stream_context_->registry()->CloneStream(url, src_url);
692 stream_urls_.insert(url.spec());
693 }
694
695 void FileAPIMessageFilter::OnRemoveStream(const GURL& url) {
696 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
697
698 if (!GetStreamForURL(url).get()) {
699 NOTREACHED();
700 return;
701 }
702
703 stream_context_->registry()->UnregisterStream(url);
704 stream_urls_.erase(url.spec());
705 }
706
590 void FileAPIMessageFilter::DidFinish(int request_id, 707 void FileAPIMessageFilter::DidFinish(int request_id,
591 base::PlatformFileError result) { 708 base::PlatformFileError result) {
592 if (result == base::PLATFORM_FILE_OK) 709 if (result == base::PLATFORM_FILE_OK)
593 Send(new FileSystemMsg_DidSucceed(request_id)); 710 Send(new FileSystemMsg_DidSucceed(request_id));
594 else 711 else
595 Send(new FileSystemMsg_DidFail(request_id, result)); 712 Send(new FileSystemMsg_DidFail(request_id, result));
596 operations_.erase(request_id); 713 operations_.erase(request_id);
597 } 714 }
598 715
599 void FileAPIMessageFilter::DidGetMetadata( 716 void FileAPIMessageFilter::DidGetMetadata(
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 Send(new FileSystemMsg_DidCreateSnapshotFile( 849 Send(new FileSystemMsg_DidCreateSnapshotFile(
733 request_id, info, platform_path)); 850 request_id, info, platform_path));
734 } 851 }
735 852
736 bool FileAPIMessageFilter::HasPermissionsForFile( 853 bool FileAPIMessageFilter::HasPermissionsForFile(
737 const FileSystemURL& url, int permissions, base::PlatformFileError* error) { 854 const FileSystemURL& url, int permissions, base::PlatformFileError* error) {
738 return CheckFileSystemPermissionsForProcess(context_, process_id_, url, 855 return CheckFileSystemPermissionsForProcess(context_, process_id_, url,
739 permissions, error); 856 permissions, error);
740 } 857 }
741 858
859 scoped_refptr<Stream> FileAPIMessageFilter::GetStreamForURL(const GURL& url) {
860 return stream_context_->registry()->GetStream(url);
861 }
862
742 } // namespace content 863 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/fileapi/fileapi_message_filter.h ('k') | content/browser/fileapi/fileapi_message_filter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698