Пример #1
0
static void methodCallCallback(GDBusConnection* connection, const char* sender, const char* objectPath, const char* interfaceName, const char* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData)
{
    if (g_strcmp0(interfaceName, "org.webkit.gtk.WebExtensionTest"))
        return;

    if (!g_strcmp0(methodName, "GetTitle")) {
        uint64_t pageID;
        g_variant_get(parameters, "(t)", &pageID);
        WebKitWebPage* page = getWebPage(WEBKIT_WEB_EXTENSION(userData), pageID, invocation);
        if (!page)
            return;

        WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
        GUniquePtr<char> title(webkit_dom_document_get_title(document));
        g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", title.get()));
    } else if (!g_strcmp0(methodName, "RunJavaScriptInIsolatedWorld")) {
        uint64_t pageID;
        const char* script;
        g_variant_get(parameters, "(t&s)", &pageID, &script);
        WebKitWebPage* page = getWebPage(WEBKIT_WEB_EXTENSION(userData), pageID, invocation);
        if (!page)
            return;

        GRefPtr<WebKitScriptWorld> world = adoptGRef(webkit_script_world_new());
        g_assert(webkit_script_world_get_default() != world.get());
        WebKitFrame* frame = webkit_web_page_get_main_frame(page);
        JSGlobalContextRef jsContext = webkit_frame_get_javascript_context_for_script_world(frame, world.get());
        JSRetainPtr<JSStringRef> jsScript(Adopt, JSStringCreateWithUTF8CString(script));
        JSEvaluateScript(jsContext, jsScript.get(), 0, 0, 0, 0);
        g_dbus_method_invocation_return_value(invocation, 0);
    } else if (!g_strcmp0(methodName, "AbortProcess")) {
        abort();
    } else if (!g_strcmp0(methodName, "GetInitializationUserData")) {
        g_assert(initializationUserData);
        g_assert(g_variant_is_of_type(initializationUserData.get(), G_VARIANT_TYPE_STRING));
        g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)",
            g_variant_get_string(initializationUserData.get(), nullptr)));
    } else if (!g_strcmp0(methodName, "GetProcessIdentifier")) {
        g_dbus_method_invocation_return_value(invocation,
            g_variant_new("(u)", static_cast<guint32>(getCurrentProcessID())));
    }
}
Пример #2
0
   /**
    * Loads script by given script name.
    */
   static JSBool JSR_fn_loadScript( JSContext *cx, unsigned argc, Value *vp ) {

       Logger &log = LoggerFactory::getLogger();

       if( argc != 1 ) {
           JS_ReportError( cx, "Only one argument is allowed." );
           return JS_FALSE;
       }

       // Gets engine for context.
       JSDebuggerEngine *engine = JSDebuggerEngine::getEngineForContext(cx);
       if( !engine ) {
           log.error( "JSR_fn_loadScript:: There is no engine installed for given context." );
           JS_ReportError( cx, "There is no engine installed for given context." );
           return JS_FALSE;
       }

       MozJSUtils jsUtils(cx);

       CallArgs args = CallArgsFromVp(argc, vp);

       string filePath;
       RootedString filePathArg( cx, args.get(0).toString() );
       if( !jsUtils.toString(filePathArg, filePath) ) {
           log.error("JSR_fn_loadScript:: Cannot convert file name to C string.");
           JS_ReportError( cx, "Cannot convert file name to C string." );
           return JS_FALSE;
       }

       JSEngineEventHandler &handler = engine->getEngineEventHandler();

       string script;

       // Pass processing to a custom handler.
       int rc;
       if( ( rc = handler.loadScript( cx, filePath, script ) ) ) {
           log.error( "JSR_fn_loadScript:: Cannot read string using provided callback: %d", rc );
           string msg;
           if( rc == JSR_ERROR_CANNOT_READ_FILE ) {
               msg = "Cannot read string using provided callback. Source file not found: " + filePath;
           } else {
               msg = "Cannot read string using provided callback.";
           }
           JS_ReportError( cx, msg.c_str() );
           return JS_FALSE;
       }

       try {

           if( !script.empty() ) {

               JCharEncoder encoder;
               jstring jscript = encoder.utf8ToWide(script);

               RootedString jsScript(cx);
               if( !jsUtils.fromString( jscript, &jsScript ) ) {
                   log.error("JSR_fn_loadScript:: Cannot read string using provided callback.");
                   JS_ReportError( cx, "Cannot read string using provided callback." );
               }

               args.rval().setString( jsScript );

           } else {
               args.rval().setNull();
           }

       } catch( EncodingFailedException &exc ) {
           log.error("JSR_fn_loadScript:: Encoding failed, cannot encode script to UTF-16.");
           JS_ReportError( cx, "Encoding failed, cannot encode script to UTF-16." );
           return JS_FALSE;
       }

       return JS_TRUE;
   }