Skip to content
Permalink
Browse files
2010-03-06 MORITA Hajime <morrita@google.com>
        Reviewed by Darin Adler.

        Moved implementations of window.btoa() and window.atob() from
        JSDOMWindow to DOMWindow, and make V8DOMWindow use DOMWindow
        functions instead of having a separate implementation. As a side effect, the
        error message has changed from "Cannot decode base64" to one that
        indicates DOM Exception, which is compatible to Firefox.

        Refactoring: window.btoa() and window.atob() should be implemented on DOMWindow
        https://bugs.webkit.org/show_bug.cgi?id=35723

        * fast/dom/Window/atob-btoa-expected.txt:
          Rebaselined expectation due to change of error message.
2010-03-06  MORITA Hajime  <morrita@google.com>

        Reviewed by Darin Adler.

        Moved implementations of window.btoa() and window.atob() from
        JSDOMWindow to DOMWindow, and make V8DOMWindow use DOMWindow
        functions instead of having a separate implementation. As a side effect, the
        error message has changed from "Cannot decode base64" to one that
        indicates DOM Exception, which is compatible to Firefox.

        Refactoring: window.btoa() and window.atob() should be implemented on DOMWindow
        https://bugs.webkit.org/show_bug.cgi?id=35723

        No new test. No new functionality.

        * bindings/js/JSDOMWindowCustom.cpp:
        (WebCore::JSDOMWindow::atob):
        (WebCore::JSDOMWindow::btoa):
        Moved conversion code to DOMWindow and invoke it. Argument
        checking remains here.

        * bindings/v8/custom/V8DOMWindowCustom.cpp:
        (WebCore::V8DOMWindow::atobCallback):
        (WebCore::V8DOMWindow::btoaCallback):
        Remove conversion code and call DOMWindow APIs. Although argument
        checking remains here.

        * page/DOMWindow.cpp:
        (WebCore::hasMultibyteCharacters):
        (WebCore::DOMWindow::btoa):
        (WebCore::DOMWindow::atob):
        * page/DOMWindow.h:
        Moved Conversion code from JSDOMWindow, modifing to fit JSC independent.

Canonical link: https://commits.webkit.org/46916@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@55619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
eseidel committed Mar 6, 2010
1 parent 5068092 commit 9477e6570b8b8f35e65fe61ebc5a26d10ae12e8a
Showing 7 changed files with 129 additions and 91 deletions.
@@ -1,3 +1,19 @@
2010-03-06 MORITA Hajime <morrita@google.com>

Reviewed by Darin Adler.

Moved implementations of window.btoa() and window.atob() from
JSDOMWindow to DOMWindow, and make V8DOMWindow use DOMWindow
functions instead of having a separate implementation. As a side effect, the
error message has changed from "Cannot decode base64" to one that
indicates DOM Exception, which is compatible to Firefox.

Refactoring: window.btoa() and window.atob() should be implemented on DOMWindow
https://bugs.webkit.org/show_bug.cgi?id=35723

* fast/dom/Window/atob-btoa-expected.txt:
Rebaselined expectation due to change of error message.

2010-03-06 Shu Chang <Chang.Shu@nokia.com>

Reviewed by Kenneth Rohde Christiansen.
@@ -24,22 +24,22 @@ PASS typeof window.atob is "function"
PASS window.atob() threw exception SyntaxError: Not enough arguments.
PASS window.atob("") is ""
PASS window.atob(null) is ""
PASS window.atob(" YQ==") threw exception Error: Cannot decode base64.
PASS window.atob("YQ==\u000a") threw exception Error: Cannot decode base64.
PASS window.atob(" YQ==") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob("YQ==\u000a") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob("6ek=") is "éé"
PASS window.atob("6ek") is "éé"
PASS window.atob("gIE=") is "€"
PASS window.atob("тест") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob("z") threw exception Error: Cannot decode base64.
PASS window.atob("z") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob("zz") is "Ï"
PASS window.atob("zzz") is "Ï<"
PASS window.atob("zzzz") is "Ï<ó"
PASS window.atob("zzzzz") threw exception Error: Cannot decode base64.
PASS window.atob("=") threw exception Error: Cannot decode base64.
PASS window.atob("==") threw exception Error: Cannot decode base64.
PASS window.atob("===") threw exception Error: Cannot decode base64.
PASS window.atob("====") threw exception Error: Cannot decode base64.
PASS window.atob("=====") threw exception Error: Cannot decode base64.
PASS window.atob("zzzzz") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob("=") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob("==") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob("===") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob("====") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob("=====") threw exception Error: INVALID_CHARACTER_ERR: DOM Exception 5.
PASS window.atob is 0
PASS typeof window.atob is "number"
PASS successfullyParsed is true
@@ -1,3 +1,37 @@
2010-03-06 MORITA Hajime <morrita@google.com>

Reviewed by Darin Adler.

Moved implementations of window.btoa() and window.atob() from
JSDOMWindow to DOMWindow, and make V8DOMWindow use DOMWindow
functions instead of having a separate implementation. As a side effect, the
error message has changed from "Cannot decode base64" to one that
indicates DOM Exception, which is compatible to Firefox.

Refactoring: window.btoa() and window.atob() should be implemented on DOMWindow
https://bugs.webkit.org/show_bug.cgi?id=35723

No new test. No new functionality.

* bindings/js/JSDOMWindowCustom.cpp:
(WebCore::JSDOMWindow::atob):
(WebCore::JSDOMWindow::btoa):
Moved conversion code to DOMWindow and invoke it. Argument
checking remains here.

* bindings/v8/custom/V8DOMWindowCustom.cpp:
(WebCore::V8DOMWindow::atobCallback):
(WebCore::V8DOMWindow::btoaCallback):
Remove conversion code and call DOMWindow APIs. Although argument
checking remains here.

* page/DOMWindow.cpp:
(WebCore::hasMultibyteCharacters):
(WebCore::DOMWindow::btoa):
(WebCore::DOMWindow::atob):
* page/DOMWindow.h:
Moved Conversion code from JSDOMWindow, modifing to fit JSC independent.

2010-03-06 Yuta Kitamura <yutak@chromium.org>

Reviewed by Eric Seidel.
@@ -21,7 +21,6 @@
#include "JSDOMWindowCustom.h"

#include "AtomicString.h"
#include "Base64.h"
#include "Chrome.h"
#include "Database.h"
#include "DOMWindow.h"
@@ -945,51 +944,28 @@ JSValue JSDOMWindow::atob(ExecState* exec, const ArgList& args)
{
if (args.size() < 1)
return throwError(exec, SyntaxError, "Not enough arguments");

JSValue v = args.at(0);
if (v.isNull())
if (args.at(0).isNull())
return jsEmptyString(exec);

UString s = v.toString(exec);
if (!s.is8Bit()) {
setDOMException(exec, INVALID_CHARACTER_ERR);
return jsUndefined();
}

Vector<char> in(s.size());
for (unsigned i = 0; i < s.size(); ++i)
in[i] = static_cast<char>(s.data()[i]);
Vector<char> out;

if (!base64Decode(in, out))
return throwError(exec, GeneralError, "Cannot decode base64");
ExceptionCode ec = 0;
String result = impl()->atob(args.at(0).toString(exec), ec);
setDOMException(exec, ec);

return jsString(exec, String(out.data(), out.size()));
return jsString(exec, result);
}

JSValue JSDOMWindow::btoa(ExecState* exec, const ArgList& args)
{
if (args.size() < 1)
return throwError(exec, SyntaxError, "Not enough arguments");

JSValue v = args.at(0);
if (v.isNull())
if (args.at(0).isNull())
return jsEmptyString(exec);

UString s = v.toString(exec);
if (!s.is8Bit()) {
setDOMException(exec, INVALID_CHARACTER_ERR);
return jsUndefined();
}

Vector<char> in(s.size());
for (unsigned i = 0; i < s.size(); ++i)
in[i] = static_cast<char>(s.data()[i]);
Vector<char> out;

base64Encode(in, out);
ExceptionCode ec = 0;
String result = impl()->btoa(args.at(0).toString(exec), ec);
setDOMException(exec, ec);

return jsString(exec, String(out.data(), out.size()));
return jsString(exec, result);
}

JSValue JSDOMWindow::addEventListener(ExecState* exec, const ArgList& args)
@@ -31,7 +31,6 @@
#include "config.h"
#include "V8DOMWindow.h"

#include "Base64.h"
#include "Chrome.h"
#include "Database.h"
#include "DOMTimer.h"
@@ -148,37 +147,6 @@ v8::Handle<v8::Value> WindowSetTimeoutImpl(const v8::Arguments& args, bool singl
return v8::Integer::New(id);
}

static bool isAscii(const String& str)
{
for (size_t i = 0; i < str.length(); i++) {
if (str[i] > 0xFF)
return false;
}
return true;
}

static v8::Handle<v8::Value> convertBase64(const String& str, bool encode)
{
if (!isAscii(str)) {
V8Proxy::setDOMException(INVALID_CHARACTER_ERR);
return notHandledByInterceptor();
}

Vector<char> inputCharacters(str.length());
for (size_t i = 0; i < str.length(); i++)
inputCharacters[i] = static_cast<char>(str[i]);
Vector<char> outputCharacters;

if (encode)
base64Encode(inputCharacters, outputCharacters);
else {
if (!base64Decode(inputCharacters, outputCharacters))
return throwError("Cannot decode base64", V8Proxy::GeneralError);
}

return v8String(String(outputCharacters.data(), outputCharacters.size()));
}

v8::Handle<v8::Value> V8DOMWindow::eventAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
v8::Handle<v8::Object> holder = V8DOMWrapper::lookupDOMWrapper(V8DOMWindow::GetTemplate(), info.This());
@@ -378,38 +346,34 @@ v8::Handle<v8::Value> V8DOMWindow::atobCallback(const v8::Arguments& args)
{
INC_STATS("DOM.DOMWindow.atob()");

if (args.Length() < 1)
return throwError("Not enough arguments", V8Proxy::SyntaxError);
if (args[0]->IsNull())
return v8String("");
String str = toWebCoreString(args[0]);

DOMWindow* imp = V8DOMWindow::toNative(args.Holder());
ExceptionCode ec = 0;
String result = imp->atob(toWebCoreString(args[0]), ec);
throwError(ec);

if (!V8BindingSecurity::canAccessFrame(V8BindingState::Only(), imp->frame(), true))
return v8::Undefined();

if (args.Length() < 1)
return throwError("Not enough arguments", V8Proxy::SyntaxError);

return convertBase64(str, false);
return v8String(result);
}

v8::Handle<v8::Value> V8DOMWindow::btoaCallback(const v8::Arguments& args)
{
INC_STATS("DOM.DOMWindow.btoa()");

if (args.Length() < 1)
return throwError("Not enough arguments", V8Proxy::SyntaxError);
if (args[0]->IsNull())
return v8String("");
String str = toWebCoreString(args[0]);

DOMWindow* imp = V8DOMWindow::toNative(args.Holder());
ExceptionCode ec = 0;
String result = imp->btoa(toWebCoreString(args[0]), ec);
throwError(ec);

if (!V8BindingSecurity::canAccessFrame(V8BindingState::Only(), imp->frame(), true))
return v8::Undefined();

if (args.Length() < 1)
return throwError("Not enough arguments", V8Proxy::SyntaxError);

return convertBase64(str, true);
return v8String(result);
}

// FIXME(fqian): returning string is cheating, and we should
@@ -26,6 +26,7 @@
#include "config.h"
#include "DOMWindow.h"

#include "Base64.h"
#include "BarInfo.h"
#include "BeforeUnloadEvent.h"
#include "CSSComputedStyleDeclaration.h"
@@ -827,6 +828,51 @@ String DOMWindow::prompt(const String& message, const String& defaultValue)
return String();
}

static bool isSafeToConvertCharList(const String& string)
{
for (unsigned i = 0; i < string.length(); i++) {
if (string[i] > 0xFF)
return false;
}

return true;
}

String DOMWindow::btoa(const String& stringToEncode, ExceptionCode& ec)
{
if (!isSafeToConvertCharList(stringToEncode)) {
ec = INVALID_CHARACTER_ERR;
return String();
}

Vector<char> in;
in.append(stringToEncode.characters(), stringToEncode.length());
Vector<char> out;

base64Encode(in, out);

return String(out.data(), out.size());
}

String DOMWindow::atob(const String& encodedString, ExceptionCode& ec)
{
if (!isSafeToConvertCharList(encodedString)) {
ec = INVALID_CHARACTER_ERR;
return String();
}

Vector<char> in;
in.append(encodedString.characters(), encodedString.length());
Vector<char> out;

if (!base64Decode(in, out)) {
ec = INVALID_CHARACTER_ERR;
return String();
}

return String(out.data(), out.size());
}

bool DOMWindow::find(const String& string, bool caseSensitive, bool backwards, bool wrap, bool /*wholeWord*/, bool /*searchInFrames*/, bool /*showDialog*/) const
{
if (!m_frame)
@@ -140,6 +140,8 @@ namespace WebCore {
void alert(const String& message);
bool confirm(const String& message);
String prompt(const String& message, const String& defaultValue);
String btoa(const String& stringToEncode, ExceptionCode&);
String atob(const String& encodedString, ExceptionCode&);

bool find(const String&, bool caseSensitive, bool backwards, bool wrap, bool wholeWord, bool searchInFrames, bool showDialog) const;

0 comments on commit 9477e65

Please sign in to comment.