Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / pepper / pepper_file_io_host.cc
index da802e5..56b6d92 100644 (file)
@@ -41,10 +41,10 @@ using ppapi::PPTimeToTime;
 
 namespace {
 
-PepperFileIOHost::UIThreadStuff
-GetUIThreadStuffForInternalFileSystems(int render_process_id) {
+PepperFileIOHost::UIThreadStuff GetUIThreadStuffForInternalFileSystems(
+    int render_process_id) {
   PepperFileIOHost::UIThreadStuff stuff;
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
   RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
   if (host) {
     stuff.resolved_render_process_id = base::GetProcId(host->GetHandle());
@@ -56,7 +56,7 @@ GetUIThreadStuffForInternalFileSystems(int render_process_id) {
 }
 
 base::ProcessId GetResolvedRenderProcessId(int render_process_id) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
   RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
   if (!host)
     return base::kNullProcessId;
@@ -65,9 +65,11 @@ base::ProcessId GetResolvedRenderProcessId(int render_process_id) {
 
 bool GetPluginAllowedToCallRequestOSFileHandle(int render_process_id,
                                                const GURL& document_url) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
   ContentBrowserClient* client = GetContentClient()->browser();
   RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
+  if (!host)
+    return false;
   return client->IsPluginAllowedToCallRequestOSFileHandle(
       host->GetBrowserContext(), document_url);
 }
@@ -91,34 +93,28 @@ PepperFileIOHost::PepperFileIOHost(BrowserPpapiHostImpl* host,
       check_quota_(false),
       weak_factory_(this) {
   int unused;
-  if (!host->GetRenderFrameIDsForInstance(instance,
-                                          &render_process_id_,
-                                          &unused)) {
+  if (!host->GetRenderFrameIDsForInstance(
+          instance, &render_process_id_, &unused)) {
     render_process_id_ = -1;
   }
-  file_message_loop_ = BrowserThread::GetMessageLoopProxyForThread(
-      BrowserThread::FILE);
+  file_message_loop_ =
+      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
 }
 
-PepperFileIOHost::~PepperFileIOHost() {
-}
+PepperFileIOHost::~PepperFileIOHost() {}
 
 int32_t PepperFileIOHost::OnResourceMessageReceived(
     const IPC::Message& msg,
     ppapi::host::HostMessageContext* context) {
   IPC_BEGIN_MESSAGE_MAP(PepperFileIOHost, msg)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Open,
-                                      OnHostMsgOpen)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Touch,
-                                      OnHostMsgTouch)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_SetLength,
-                                      OnHostMsgSetLength)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Flush,
-                                        OnHostMsgFlush)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Close,
-                                      OnHostMsgClose)
-    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_RequestOSFileHandle,
-                                        OnHostMsgRequestOSFileHandle)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Open, OnHostMsgOpen)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Touch, OnHostMsgTouch)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_SetLength,
+                                    OnHostMsgSetLength)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Flush, OnHostMsgFlush)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Close, OnHostMsgClose)
+  PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_RequestOSFileHandle,
+                                      OnHostMsgRequestOSFileHandle)
   IPC_END_MESSAGE_MAP()
   return PP_ERROR_FAILED;
 }
@@ -127,8 +123,7 @@ PepperFileIOHost::UIThreadStuff::UIThreadStuff() {
   resolved_render_process_id = base::kNullProcessId;
 }
 
-PepperFileIOHost::UIThreadStuff::~UIThreadStuff() {
-}
+PepperFileIOHost::UIThreadStuff::~UIThreadStuff() {}
 
 int32_t PepperFileIOHost::OnHostMsgOpen(
     ppapi::host::HostMessageContext* context,
@@ -159,18 +154,36 @@ int32_t PepperFileIOHost::OnHostMsgOpen(
   file_system_type_ = file_ref_host->GetFileSystemType();
   file_system_url_ = file_ref_host->GetFileSystemURL();
 
-  if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) {
+  // For external file systems, if there is a valid FileSystemURL, then treat
+  // it like internal file systems and access it via the FileSystemURL.
+  bool is_internal_type = (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) ||
+                          file_system_url_.is_valid();
+
+  if (is_internal_type) {
     if (!file_system_url_.is_valid())
       return PP_ERROR_BADARGUMENT;
-    if (!CanOpenFileSystemURLWithPepperFlags(open_flags,
-                                             render_process_id_,
-                                             file_system_url_))
+
+    // Not all external file systems are fully supported yet.
+    // Whitelist the supported ones.
+    if (file_system_url_.mount_type() == fileapi::kFileSystemTypeExternal) {
+      switch (file_system_url_.type()) {
+        case fileapi::kFileSystemTypeNativeMedia:
+        case fileapi::kFileSystemTypeDeviceMedia:
+        case fileapi::kFileSystemTypePicasa:
+        case fileapi::kFileSystemTypeItunes:
+        case fileapi::kFileSystemTypeIphoto:
+          break;
+        default:
+          return PP_ERROR_NOACCESS;
+      }
+    }
+    if (!CanOpenFileSystemURLWithPepperFlags(
+            open_flags, render_process_id_, file_system_url_))
       return PP_ERROR_NOACCESS;
     BrowserThread::PostTaskAndReplyWithResult(
         BrowserThread::UI,
         FROM_HERE,
-        base::Bind(&GetUIThreadStuffForInternalFileSystems,
-                   render_process_id_),
+        base::Bind(&GetUIThreadStuffForInternalFileSystems, render_process_id_),
         base::Bind(&PepperFileIOHost::GotUIThreadStuffForInternalFileSystems,
                    weak_factory_.GetWeakPtr(),
                    context->MakeReplyMessageContext(),
@@ -197,7 +210,7 @@ void PepperFileIOHost::GotUIThreadStuffForInternalFileSystems(
     ppapi::host::ReplyMessageContext reply_context,
     int platform_file_flags,
     UIThreadStuff ui_thread_stuff) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+  DCHECK_CURRENTLY_ON(BrowserThread::IO);
   file_system_context_ = ui_thread_stuff.file_system_context;
   resolved_render_process_id_ = ui_thread_stuff.resolved_render_process_id;
   if (resolved_render_process_id_ == base::kNullProcessId ||
@@ -253,7 +266,7 @@ void PepperFileIOHost::GotResolvedRenderProcessId(
     base::FilePath path,
     int platform_file_flags,
     base::ProcessId resolved_render_process_id) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+  DCHECK_CURRENTLY_ON(BrowserThread::IO);
   resolved_render_process_id_ = resolved_render_process_id;
   base::FileUtilProxy::CreateOrOpen(
       file_message_loop_,
@@ -301,12 +314,12 @@ int32_t PepperFileIOHost::OnHostMsgSetLength(
   // quota reservation and request system as Write.
 
   if (!base::FileUtilProxy::Truncate(
-      file_message_loop_,
-      file_,
-      length,
-      base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
-                 weak_factory_.GetWeakPtr(),
-                 context->MakeReplyMessageContext())))
+          file_message_loop_,
+          file_,
+          length,
+          base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
+                     weak_factory_.GetWeakPtr(),
+                     context->MakeReplyMessageContext())))
     return PP_ERROR_FAILED;
 
   state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
@@ -341,11 +354,10 @@ int32_t PepperFileIOHost::OnHostMsgClose(
   }
 
   if (file_ != base::kInvalidPlatformFileValue) {
-    base::FileUtilProxy::Close(
-        file_message_loop_,
-        file_,
-        base::Bind(&PepperFileIOHost::DidCloseFile,
-                   weak_factory_.GetWeakPtr()));
+    base::FileUtilProxy::Close(file_message_loop_,
+                               file_,
+                               base::Bind(&PepperFileIOHost::DidCloseFile,
+                                          weak_factory_.GetWeakPtr()));
     file_ = base::kInvalidPlatformFileValue;
   }
   return PP_OK;
@@ -358,11 +370,10 @@ void PepperFileIOHost::DidOpenQuotaFile(
   max_written_offset_ = max_written_offset;
 
   ExecutePlatformOpenFileCallback(
-      reply_context, base::File::FILE_OK, base::PassPlatformFile(&file),
-      true);
+      reply_context, base::File::FILE_OK, base::PassPlatformFile(&file), true);
 }
 
-void PepperFileIOHost::DidCloseFile(base::File::Error error) {
+void PepperFileIOHost::DidCloseFile(base::File::Error /*error*/) {
   // Silently ignore if we fail to close the file.
   if (!on_close_callback_.is_null()) {
     on_close_callback_.Run();
@@ -392,7 +403,7 @@ int32_t PepperFileIOHost::OnHostMsgRequestOSFileHandle(
 void PepperFileIOHost::GotPluginAllowedToCallRequestOSFileHandle(
     ppapi::host::ReplyMessageContext reply_context,
     bool plugin_allowed) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+  DCHECK_CURRENTLY_ON(BrowserThread::IO);
   if (!browser_ppapi_host_->external_plugin() ||
       host()->permissions().HasPermission(ppapi::PERMISSION_PRIVATE) ||
       plugin_allowed) {
@@ -408,8 +419,7 @@ void PepperFileIOHost::GotPluginAllowedToCallRequestOSFileHandle(
 void PepperFileIOHost::ExecutePlatformGeneralCallback(
     ppapi::host::ReplyMessageContext reply_context,
     base::File::Error error_code) {
-  reply_context.params.set_result(
-      ppapi::FileErrorToPepperError(error_code));
+  reply_context.params.set_result(ppapi::FileErrorToPepperError(error_code));
   host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply());
   state_manager_.SetOperationFinished();
 }
@@ -436,9 +446,9 @@ void PepperFileIOHost::ExecutePlatformOpenFileCallback(
   }
 
   reply_context.params.set_result(pp_error);
-  host()->SendReply(reply_context,
-                  PpapiPluginMsg_FileIO_OpenReply(quota_file_system,
-                                                  max_written_offset_));
+  host()->SendReply(
+      reply_context,
+      PpapiPluginMsg_FileIO_OpenReply(quota_file_system, max_written_offset_));
   state_manager_.SetOperationFinished();
 }
 
@@ -455,8 +465,8 @@ bool PepperFileIOHost::AddFileToReplyContext(
   if (plugin_process_id == base::kNullProcessId)
     plugin_process_id = resolved_render_process_id_;
 
-  IPC::PlatformFileForTransit transit_file = BrokerGetFileHandleForProcess(
-      file_, plugin_process_id, false);
+  IPC::PlatformFileForTransit transit_file =
+      BrokerGetFileHandleForProcess(file_, plugin_process_id, false);
   if (transit_file == IPC::InvalidPlatformFileForTransit())
     return false;