Skip to content

Commit

Permalink
Added native c++ client for testing Google-Chrome-Native-Messaging pl…
Browse files Browse the repository at this point in the history
…ugin
  • Loading branch information
Mandar-Shinde committed Jun 9, 2016
1 parent 6516505 commit 9cc0b3a
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Client-C++/NativeMessagingApp.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#-------------------------------------------------
#
# Project created by Mandar Shinde 2016-06-06T14:38:49
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = testui

TEMPLATE = app

DEFINES += QT_NO_ICU
SOURCES += main.cpp\
mainwindow.cpp \
Worker.cpp

HEADERS += mainwindow.h \
Worker.h

FORMS += mainwindow.ui
21 changes: 21 additions & 0 deletions Client-C++/Worker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "Worker.h"
#include <iostream>
#include <string>

using namespace std;

void Worker::run()
{
do{
char len[4];
cin.read(len, 4);
unsigned int ilen = *reinterpret_cast<unsigned int *>(len);
char *inMsg = new char[ilen];
memset(inMsg,0,ilen+1);
cin.read(inMsg, ilen);
string inStr(inMsg);
delete[] inMsg;
QString sline(inStr.c_str());
emit UpdateMessage(sline);
}while(true);
}
15 changes: 15 additions & 0 deletions Client-C++/Worker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef PARA_H
#define PARA_H

#include <qthread.h>
#include <iostream>

class Worker : public QThread {
Q_OBJECT
public:
void run();
signals:
void UpdateMessage(const QString &);
};

#endif
18 changes: 18 additions & 0 deletions Client-C++/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Title : Google Chrome Native Messaging API Qt C++ Client
* Author : Mandar Shinde
* Interface : GUI
* IDE : Qt5.4
* Operating System : Windows 10
*/

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
55 changes: 55 additions & 0 deletions Client-C++/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"

using namespace std;
#define CLIENT_START "{\"text\":\"Client Started\"}"
#define CLIENT_STOP "{\"text\":\"Client Stopped\"}"
#define CLIENT_CLICK "{\"text\":\"Client Click\"}"

inline QString sendMessage(QString data)
{
unsigned int len = data.length();
cout.write(reinterpret_cast<const char *>(&len), 4);
cout << data.toUtf8().data() << flush;
return "";
}

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
QString datas="App running with args=";
datas.append(QString::number(qApp->arguments().count())).append("\n");
int i=0;
foreach(QString s,qApp->arguments())
{
datas.append(QString::number(i)).append(" : ").append(s); i++;
}
QThread *thread = new QThread(this);
Worker *updater = new Worker();
updater->moveToThread(thread);
connect(updater, SIGNAL(UpdateMessage(QString)), this, SLOT(UpdateNewMessage(QString)));
connect(thread, SIGNAL(destroyed()), updater, SLOT(deleteLater()));
updater->start();
ui->plainTextEdit->appendPlainText(datas+"\n\nClient Started..");
sendMessage(CLIENT_START);
}

MainWindow::~MainWindow()
{
ui->plainTextEdit->appendPlainText("Client Stopped..");
sendMessage(CLIENT_STOP);
delete ui;
}

void MainWindow::UpdateNewMessage(const QString &msg)
{
ui->plainTextEdit->appendPlainText("Receving data from plugin <-<- "+msg);
}

void MainWindow::on_SendMessage_clicked()
{
// sendMessage(CLIENT_CLICK);
QString msg;
msg.append("{\"text\":\"").append(ui->teMessage->document()->toPlainText()).append("\"}");
sendMessage(msg);
ui->plainTextEdit->appendPlainText(msg.prepend("Sending data to plugin ->-> "));
}
30 changes: 30 additions & 0 deletions Client-C++/mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <Worker.h>
#include <iostream>

using namespace std;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void UpdateNewMessage(const QString &imgSource);
void on_SendMessage_clicked();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
68 changes: 68 additions & 0 deletions Client-C++/mainwindow.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>643</width>
<height>396</height>
</rect>
</property>
<property name="windowTitle">
<string>Google Chrome Native Messaging App</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="1,0">
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="autoFillBackground">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<item>
<widget class="QPlainTextEdit" name="teMessage">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>50</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="SendMessage">
<property name="text">
<string>Send Message</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

0 comments on commit 9cc0b3a

Please sign in to comment.