Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / apps / launcher.cc
index b19d9a2..8d31a01 100644 (file)
@@ -4,7 +4,9 @@
 
 #include "apps/launcher.h"
 
-#include "apps/apps_client.h"
+#include "apps/browser/api/app_runtime/app_runtime_api.h"
+#include "apps/browser/file_handler_util.h"
+#include "apps/common/api/app_runtime.h"
 #include "base/command_line.h"
 #include "base/file_util.h"
 #include "base/files/file_path.h"
 #include "base/memory/ref_counted.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
-#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h"
 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
 #include "chrome/browser/profiles/profile.h"
-#include "chrome/common/extensions/api/app_runtime.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/render_process_host.h"
 #include "content/public/browser/web_contents.h"
@@ -29,6 +29,8 @@
 #include "extensions/common/extension.h"
 #include "extensions/common/extension_messages.h"
 #include "extensions/common/manifest_handlers/kiosk_mode_info.h"
+#include "net/base/filename_util.h"
+#include "net/base/mime_sniffer.h"
 #include "net/base/mime_util.h"
 #include "net/base/net_util.h"
 #include "url/gurl.h"
 #include "chrome/browser/chromeos/login/user_manager.h"
 #endif
 
-#if defined(OS_WIN)
-#include "win8/util/win8_util.h"
-#endif
-
-namespace app_runtime = extensions::api::app_runtime;
+namespace app_runtime = apps::api::app_runtime;
 
+using apps::file_handler_util::GrantedFileEntry;
 using content::BrowserThread;
 using extensions::app_file_handler_util::CheckWritableFiles;
 using extensions::app_file_handler_util::FileHandlerForId;
 using extensions::app_file_handler_util::FileHandlerCanHandleFile;
 using extensions::app_file_handler_util::FirstFileHandlerForFile;
 using extensions::app_file_handler_util::CreateFileEntry;
-using extensions::app_file_handler_util::GrantedFileEntry;
 using extensions::app_file_handler_util::HasFileSystemWritePermission;
 using extensions::EventRouter;
 using extensions::Extension;
@@ -104,7 +102,7 @@ bool GetAbsolutePathFromCommandLine(const CommandLine& command_line,
 // load or obtain file launch data.
 void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-  extensions::AppEventRouter::DispatchOnLaunchedEvent(profile, extension);
+  AppEventRouter::DispatchOnLaunchedEvent(profile, extension);
 }
 
 // Class to handle launching of platform apps to open a specific path.
@@ -185,8 +183,21 @@ class PlatformAppPathLauncher
     }
 
     std::string mime_type;
-    if (!net::GetMimeTypeFromFile(file_path_, &mime_type))
-      mime_type = kFallbackMimeType;
+    if (!net::GetMimeTypeFromFile(file_path_, &mime_type)) {
+      // If MIME type of the file can't be determined by its path,
+      // try to sniff it by its content.
+      std::vector<char> content(net::kMaxBytesToSniff);
+      int bytes_read = base::ReadFile(file_path_, &content[0], content.size());
+      if (bytes_read >= 0) {
+        net::SniffMimeType(&content[0],
+                           bytes_read,
+                           net::FilePathToFileURL(file_path_),
+                           std::string(),  // type_hint (passes no hint)
+                           &mime_type);
+      }
+      if (mime_type.empty())
+        mime_type = kFallbackMimeType;
+    }
 
     BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
             &PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type));
@@ -292,7 +303,7 @@ class PlatformAppPathLauncher
                         host->render_process_host()->GetID(),
                         file_path_,
                         false);
-    extensions::AppEventRouter::DispatchOnLaunchedEventWithFileEntry(
+    AppEventRouter::DispatchOnLaunchedEventWithFileEntry(
         profile_, extension_, handler_id_, mime_type, file_entry);
   }
 
@@ -314,9 +325,6 @@ void LaunchPlatformAppWithCommandLine(Profile* profile,
                                       const Extension* extension,
                                       const CommandLine& command_line,
                                       const base::FilePath& current_directory) {
-  if (!AppsClient::Get()->CheckAppLaunch(profile, extension))
-    return;
-
   // An app with "kiosk_only" should not be installed and launched
   // outside of ChromeOS kiosk mode in the first place. This is a defensive
   // check in case this scenario does occur.
@@ -372,19 +380,13 @@ void LaunchPlatformAppWithFileHandler(Profile* profile,
 }
 
 void RestartPlatformApp(Profile* profile, const Extension* extension) {
-#if defined(OS_WIN)
-  // On Windows 8's single window Metro mode we can not launch platform apps.
-  // In restart we are just making sure launch doesn't slip through.
-  if (win8::IsSingleWindowMetroMode())
-    return;
-#endif
   EventRouter* event_router = EventRouter::Get(profile);
   bool listening_to_restart = event_router->
       ExtensionHasEventListener(extension->id(),
                                 app_runtime::OnRestarted::kEventName);
 
   if (listening_to_restart) {
-    extensions::AppEventRouter::DispatchOnRestartedEvent(profile, extension);
+    AppEventRouter::DispatchOnRestartedEvent(profile, extension);
     return;
   }
 
@@ -405,7 +407,7 @@ void LaunchPlatformAppWithUrl(Profile* profile,
                               const std::string& handler_id,
                               const GURL& url,
                               const GURL& referrer_url) {
-  extensions::AppEventRouter::DispatchOnLaunchedEventWithUrl(
+  AppEventRouter::DispatchOnLaunchedEventWithUrl(
       profile, extension, handler_id, url, referrer_url);
 }