create webui message handler
This commit is contained in:
parent
8a2b9c893e
commit
a2db14476a
7 changed files with 101 additions and 17 deletions
51
atom/browser/ui/webui/pdf_viewer_handler.cc
Normal file
51
atom/browser/ui/webui/pdf_viewer_handler.cc
Normal file
|
@ -0,0 +1,51 @@
|
|||
// Copyright (c) 2017 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "atom/browser/ui/webui/pdf_viewer_handler.h"
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/bind_helpers.h"
|
||||
#include "base/values.h"
|
||||
#include "content/public/browser/web_contents.h"
|
||||
#include "content/public/browser/web_ui.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
PdfViewerHandler::PdfViewerHandler(const std::string& stream_url,
|
||||
const std::string& original_url)
|
||||
: stream_url_(stream_url), original_url_(original_url) {}
|
||||
|
||||
PdfViewerHandler::~PdfViewerHandler() {}
|
||||
|
||||
void PdfViewerHandler::RegisterMessages() {
|
||||
web_ui()->RegisterMessageCallback(
|
||||
"initialize",
|
||||
base::Bind(&PdfViewerHandler::Initialize, base::Unretained(this)));
|
||||
web_ui()->RegisterMessageCallback(
|
||||
"getTabId",
|
||||
base::Bind(&PdfViewerHandler::GetTabId, base::Unretained(this)));
|
||||
}
|
||||
|
||||
void PdfViewerHandler::Initialize(const base::ListValue* args) {
|
||||
AllowJavascript();
|
||||
|
||||
CHECK_EQ(1U, args->GetSize());
|
||||
const base::Value* callback_id;
|
||||
CHECK(args->Get(0, &callback_id));
|
||||
std::unique_ptr<base::DictionaryValue> stream_info(new base::DictionaryValue);
|
||||
stream_info->SetString("streamURL", stream_url_);
|
||||
stream_info->SetString("originalURL", original_url_);
|
||||
ResolveJavascriptCallback(*callback_id, *stream_info);
|
||||
}
|
||||
|
||||
void PdfViewerHandler::GetTabId(const base::ListValue* args) {
|
||||
if (!IsJavascriptAllowed())
|
||||
return;
|
||||
CHECK_EQ(1U, args->GetSize());
|
||||
const base::Value* callback_id;
|
||||
CHECK(args->Get(0, &callback_id));
|
||||
ResolveJavascriptCallback(*callback_id, base::FundamentalValue(-1));
|
||||
}
|
||||
|
||||
} // namespace atom
|
41
atom/browser/ui/webui/pdf_viewer_handler.h
Normal file
41
atom/browser/ui/webui/pdf_viewer_handler.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
// Copyright (c) 2017 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_HANDLER_H_
|
||||
#define ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_HANDLER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/compiler_specific.h"
|
||||
#include "base/macros.h"
|
||||
#include "content/public/browser/web_ui_message_handler.h"
|
||||
|
||||
namespace base {
|
||||
class ListValue;
|
||||
}
|
||||
|
||||
namespace atom {
|
||||
|
||||
class PdfViewerHandler : public content::WebUIMessageHandler {
|
||||
public:
|
||||
PdfViewerHandler(const std::string& stream_url,
|
||||
const std::string& original_url);
|
||||
~PdfViewerHandler() override;
|
||||
|
||||
// WebUIMessageHandler implementation.
|
||||
void RegisterMessages() override;
|
||||
|
||||
private:
|
||||
void Initialize(const base::ListValue* args);
|
||||
void GetTabId(const base::ListValue* args);
|
||||
|
||||
std::string stream_url_;
|
||||
std::string original_url_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(PdfViewerHandler);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_HANDLER_H_
|
Loading…
Add table
Add a link
Reference in a new issue