Browse Source

win: Fix openExternal not working with non-ASCII characters

Cheng Zhao 8 years ago
parent
commit
86338290fb

+ 8 - 1
atom/browser/atom_resource_dispatcher_host_delegate.cc

@@ -7,6 +7,7 @@
 #include "atom/browser/login_handler.h"
 #include "atom/browser/web_contents_permission_helper.h"
 #include "atom/common/platform_util.h"
+#include "base/strings/utf_string_conversions.h"
 #include "content/public/browser/browser_thread.h"
 #include "net/base/escape.h"
 #include "url/gurl.h"
@@ -20,7 +21,13 @@ namespace {
 void OnOpenExternal(const GURL& escaped_url,
                     bool allowed) {
   if (allowed)
-    platform_util::OpenExternal(escaped_url, true);
+    platform_util::OpenExternal(
+#if defined(OS_WIN)
+        base::UTF8ToUTF16(escaped_url.spec()),
+#else
+        escaped_url,
+#endif
+        true);
 }
 
 void HandleExternalProtocolInUI(

+ 8 - 1
atom/common/api/atom_api_shell.cc

@@ -7,12 +7,19 @@
 #include "atom/common/platform_util.h"
 #include "atom/common/native_mate_converters/file_path_converter.h"
 #include "atom/common/native_mate_converters/gurl_converter.h"
+#include "atom/common/native_mate_converters/string16_converter.h"
 #include "atom/common/node_includes.h"
 #include "native_mate/dictionary.h"
 
 namespace {
 
-bool OpenExternal(const GURL& url, mate::Arguments* args) {
+bool OpenExternal(
+#if defined(OS_WIN)
+    const base::string16& url,
+#else
+    const GURL& url,
+#endif
+    mate::Arguments* args) {
   bool activate = true;
   if (args->Length() == 2) {
     mate::Dictionary options;

+ 13 - 1
atom/common/platform_util.h

@@ -5,6 +5,12 @@
 #ifndef ATOM_COMMON_PLATFORM_UTIL_H_
 #define ATOM_COMMON_PLATFORM_UTIL_H_
 
+#include "build/build_config.h"
+
+#if defined(OS_WIN)
+#include "base/strings/string16.h"
+#endif
+
 class GURL;
 
 namespace base {
@@ -23,7 +29,13 @@ void OpenItem(const base::FilePath& full_path);
 
 // Open the given external protocol URL in the desktop's default manner.
 // (For example, mailto: URLs in the default mail user agent.)
-bool OpenExternal(const GURL& url, bool activate);
+bool OpenExternal(
+#if defined(OS_WIN)
+    const base::string16& url,
+#else
+    const GURL& url,
+#endif
+    bool activate);
 
 // Move a file to trash.
 bool MoveItemToTrash(const base::FilePath& full_path);

+ 5 - 21
atom/common/platform_util_win.cc

@@ -24,8 +24,9 @@
 #include "base/win/scoped_com_initializer.h"
 #include "base/win/scoped_comptr.h"
 #include "base/win/windows_version.h"
-#include "url/gurl.h"
+#include "net/base/escape.h"
 #include "ui/base/win/shell.h"
+#include "url/gurl.h"
 
 namespace {
 
@@ -301,30 +302,13 @@ void OpenItem(const base::FilePath& full_path) {
     ui::win::OpenFileViaShell(full_path);
 }
 
-bool OpenExternal(const GURL& url, bool activate) {
+bool OpenExternal(const base::string16& url, bool activate) {
   // Quote the input scheme to be sure that the command does not have
   // parameters unexpected by the external program. This url should already
   // have been escaped.
-  std::string escaped_url = url.spec();
-  escaped_url.insert(0, "\"");
-  escaped_url += "\"";
-
-  // According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp:
-  // "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in
-  // ShellExecute on long URLs (bug 161357 on bugzilla.mozilla.org). IE 5 and 6
-  // support URLS of 2083 chars in length, 2K is safe."
-  const size_t kMaxURLLength = 2048;
-  if (escaped_url.length() > kMaxURLLength) {
-    NOTREACHED();
-    return false;
-  }
-
-  if (base::win::GetVersion() < base::win::VERSION_WIN7) {
-    if (!ValidateShellCommandForScheme(url.scheme()))
-      return false;
-  }
+  base::string16 escaped_url = L"\"" + url + L"\"";
 
-  if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(NULL, "open",
+  if (reinterpret_cast<ULONG_PTR>(ShellExecuteW(NULL, L"open",
                                                 escaped_url.c_str(), NULL, NULL,
                                                 SW_SHOWNORMAL)) <= 32) {
     // We fail to execute the call. We could display a message to the user.