fix crash when pdf viewer is reloaded

This commit is contained in:
deepak1556 2017-01-23 16:42:39 +05:30
parent ddc2e0df71
commit c15f6af892
6 changed files with 42 additions and 28 deletions

View file

@ -77,11 +77,12 @@ void OnPdfStreamCreated(
static_cast<AtomBrowserContext*>(web_contents->GetBrowserContext());
auto stream_manager = browser_context->stream_manager();
std::string view_id = base::GenerateGUID();
GURL original_url = stream->original_url;
stream_manager->AddStream(std::move(stream), view_id, render_process_id,
render_frame_id);
content::NavigationController::LoadURLParams params(
GURL(base::StringPrintf("%sindex.html?%s=%s", PdfViewerUI::kOrigin,
PdfViewerUI::kId, view_id.c_str())));
content::NavigationController::LoadURLParams params(GURL(base::StringPrintf(
"%sindex.html?%s=%s&%s=%s", PdfViewerUI::kOrigin, PdfViewerUI::kId,
view_id.c_str(), PdfViewerUI::kSrc, original_url.spec().c_str())));
web_contents->GetController().LoadURLWithParams(params);
}

View file

@ -50,15 +50,16 @@ AtomWebUIControllerFactory::CreateWebUIControllerForURL(content::WebUI* web_ui,
if (url.host() == PdfViewerUI::kHost) {
base::StringPairs toplevel_params;
base::SplitStringIntoKeyValuePairs(url.query(), '=', '&', &toplevel_params);
std::string view_id;
std::string view_id, src;
for (const auto& param : toplevel_params) {
if (param.first == PdfViewerUI::kId) {
view_id = param.second;
break;
} else if (param.first == PdfViewerUI::kSrc) {
src = param.second;
}
}
auto browser_context = web_ui->GetWebContents()->GetBrowserContext();
return new PdfViewerUI(browser_context, web_ui, view_id);
return new PdfViewerUI(browser_context, web_ui, view_id, src);
}
return nullptr;
}

View file

@ -4,8 +4,7 @@
#include "atom/browser/ui/webui/pdf_viewer_handler.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/stream_manager.h"
#include "base/bind.h"
#include "base/values.h"
#include "content/public/browser/stream_handle.h"
#include "content/public/browser/stream_info.h"
@ -42,17 +41,13 @@ void CreateResponseHeadersDictionary(const net::HttpResponseHeaders* headers,
} // namespace
PdfViewerHandler::PdfViewerHandler(const std::string& view_id)
: view_id_(view_id) {}
PdfViewerHandler::PdfViewerHandler(const content::StreamInfo* stream,
const std::string& src)
: stream_(stream), original_url_(src) {}
PdfViewerHandler::~PdfViewerHandler() {}
void PdfViewerHandler::RegisterMessages() {
auto browser_context = static_cast<AtomBrowserContext*>(
web_ui()->GetWebContents()->GetBrowserContext());
auto stream_manager = browser_context->stream_manager();
stream_ = stream_manager->ReleaseStream(view_id_);
web_ui()->RegisterMessageCallback(
"initialize",
base::Bind(&PdfViewerHandler::Initialize, base::Unretained(this)));
@ -86,14 +81,16 @@ void PdfViewerHandler::Initialize(const base::ListValue* args) {
const base::Value* callback_id;
CHECK(args->Get(0, &callback_id));
std::unique_ptr<base::DictionaryValue> stream_info(new base::DictionaryValue);
auto stream_url = stream_->handle->GetURL().spec();
auto original_url = stream_->original_url.spec();
stream_info->SetString("streamURL", stream_url);
stream_info->SetString("originalURL", original_url);
std::unique_ptr<base::DictionaryValue> headers_dict(
new base::DictionaryValue);
CreateResponseHeadersDictionary(stream_->response_headers.get(),
headers_dict.get());
std::string stream_url = original_url_;
if (stream_) {
stream_url = stream_->handle->GetURL().spec();
CreateResponseHeadersDictionary(stream_->response_headers.get(),
headers_dict.get());
}
stream_info->SetString("streamURL", stream_url);
stream_info->SetString("originalURL", original_url_);
stream_info->Set("responseHeaders", std::move(headers_dict));
ResolveJavascriptCallback(*callback_id, *stream_info);
}

View file

@ -23,7 +23,8 @@ namespace atom {
class PdfViewerHandler : public content::WebUIMessageHandler {
public:
explicit PdfViewerHandler(const std::string& view_id);
PdfViewerHandler(const content::StreamInfo* stream,
const std::string& original_url);
~PdfViewerHandler() override;
// WebUIMessageHandler implementation.
@ -41,8 +42,8 @@ class PdfViewerHandler : public content::WebUIMessageHandler {
// Keeps track of events related to zooming.
std::unique_ptr<content::HostZoomMap::Subscription>
host_zoom_map_subscription_;
std::unique_ptr<content::StreamInfo> stream_;
std::string view_id_;
const content::StreamInfo* stream_;
std::string original_url_;
DISALLOW_COPY_AND_ASSIGN(PdfViewerHandler);
};

View file

@ -6,6 +6,8 @@
#include <map>
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/stream_manager.h"
#include "atom/browser/ui/webui/pdf_viewer_handler.h"
#include "components/pdf/common/pdf_messages.h"
#include "content/public/browser/render_view_host.h"
@ -82,13 +84,19 @@ class BundledDataSource : public content::URLDataSource {
const char PdfViewerUI::kOrigin[] = "chrome://pdf-viewer/";
const char PdfViewerUI::kHost[] = "pdf-viewer";
const char PdfViewerUI::kId[] = "viewId";
const char PdfViewerUI::kSrc[] = "src";
PdfViewerUI::PdfViewerUI(content::BrowserContext* browser_context,
content::WebUI* web_ui,
const std::string& view_id)
: content::WebUIController(web_ui),
const std::string& view_id,
const std::string& src)
: src_(src),
content::WebUIController(web_ui),
content::WebContentsObserver(web_ui->GetWebContents()) {
web_ui->AddMessageHandler(new PdfViewerHandler(view_id));
auto context = static_cast<AtomBrowserContext*>(browser_context);
auto stream_manager = context->stream_manager();
stream_ = stream_manager->ReleaseStream(view_id);
web_ui->AddMessageHandler(new PdfViewerHandler(stream_.get(), src));
content::URLDataSource::Add(browser_context, new BundledDataSource);
}

View file

@ -14,6 +14,7 @@
namespace content {
class BrowserContext;
class StreamInfo;
}
namespace atom {
@ -24,10 +25,12 @@ class PdfViewerUI : public content::WebUIController,
static const char kOrigin[];
static const char kHost[];
static const char kId[];
static const char kSrc[];
PdfViewerUI(content::BrowserContext* browser_context,
content::WebUI* web_ui,
const std::string& view_id);
const std::string& view_id,
const std::string& src);
~PdfViewerUI() override;
// content::WebUIController implementation.
@ -40,6 +43,9 @@ class PdfViewerUI : public content::WebUIController,
private:
void OnSaveURLAs(const GURL& url, const content::Referrer& referrer);
std::string src_;
std::unique_ptr<content::StreamInfo> stream_;
DISALLOW_COPY_AND_ASSIGN(PdfViewerUI);
};