1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1.  Redistributions of source code must retain the above copyright
8 *     notice, this list of conditions and the following disclaimer.
9 * 2.  Redistributions in binary form must reproduce the above copyright
10 *     notice, this list of conditions and the following disclaimer in the
11 *     documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "config.h"
26#include "V8Navigator.h"
27
28#if ENABLE(MEDIA_STREAM) || (PLATFORM(ANDROID) && ENABLE(APPLICATION_INSTALLED))
29
30#include "Navigator.h"
31#include "V8Binding.h"
32#include "V8NavigatorUserMediaErrorCallback.h"
33#include "V8NavigatorUserMediaSuccessCallback.h"
34#include "V8Utilities.h"
35
36#if PLATFORM(ANDROID) && ENABLE(APPLICATION_INSTALLED)
37#include "ExceptionCode.h"
38#include "V8CustomApplicationInstalledCallback.h"
39#include "V8Proxy.h"
40#endif
41
42using namespace WTF;
43
44namespace WebCore {
45#if ENABLE(MEDIA_STREAM) // ANDROID
46v8::Handle<v8::Value> V8Navigator::webkitGetUserMediaCallback(const v8::Arguments& args)
47{
48    INC_STATS("DOM.Navigator.webkitGetUserMedia()");
49
50    v8::TryCatch exceptionCatcher;
51    String options = toWebCoreString(args[0]);
52    if (exceptionCatcher.HasCaught())
53        return throwError(exceptionCatcher.Exception());
54
55    bool succeeded = false;
56
57    RefPtr<NavigatorUserMediaSuccessCallback> successCallback = createFunctionOnlyCallback<V8NavigatorUserMediaSuccessCallback>(args[1], succeeded);
58    if (!succeeded)
59        return v8::Undefined();
60
61    // Argument is optional, hence undefined is allowed.
62    RefPtr<NavigatorUserMediaErrorCallback> errorCallback = createFunctionOnlyCallback<V8NavigatorUserMediaErrorCallback>(args[2], succeeded, CallbackAllowUndefined);
63    if (!succeeded)
64        return v8::Undefined();
65
66    Navigator* navigator = V8Navigator::toNative(args.Holder());
67    navigator->webkitGetUserMedia(options, successCallback.release(), errorCallback.release());
68    return v8::Undefined();
69}
70#endif // ANDROID
71
72#if PLATFORM(ANDROID) && ENABLE(APPLICATION_INSTALLED)
73static PassRefPtr<ApplicationInstalledCallback> createApplicationInstalledCallback(
74        v8::Local<v8::Value> value, bool& succeeded)
75{
76    succeeded = true;
77
78    if (!value->IsFunction()) {
79        succeeded = false;
80        throwError("The second argument should be a function");
81        return 0;
82    }
83
84    Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
85    return V8CustomApplicationInstalledCallback::create(value, frame);
86}
87
88v8::Handle<v8::Value> V8Navigator::isApplicationInstalledCallback(const v8::Arguments& args)
89{
90    INC_STATS("DOM.isApplicationInstalled()");
91    bool succeeded = false;
92
93    if (args.Length() < 2)
94        return throwError("Two arguments required: an application name and a callback.", V8Proxy::SyntaxError);
95
96    if (!args[0]->IsString())
97        return throwError("The first argument should be a string.");
98
99    RefPtr<ApplicationInstalledCallback> callback =
100        createApplicationInstalledCallback(args[1], succeeded);
101    if (!succeeded)
102        return v8::Undefined();
103
104    ASSERT(callback);
105
106    Navigator* navigator = V8Navigator::toNative(args.Holder());
107    if (!navigator->isApplicationInstalled(toWebCoreString(args[0]), callback.release()))
108        return throwError(INVALID_STATE_ERR);
109
110    return v8::Undefined();
111}
112#endif // PLATFORM(ANDROID) && ENABLE(APPLICATION_INSTALLED)
113
114} // namespace WebCore
115
116#endif // ENABLE(MEDIA_STREAM) || PLATFORM(ANDROID) && ENABLE(APPLICATION_INSTALLED)
117
118