view_id=>stream_id and add some code comments.

This commit is contained in:
deepak1556 2017-02-03 15:46:23 +05:30
parent e90fd1444f
commit 3aba23b471
8 changed files with 37 additions and 27 deletions

View file

@ -76,13 +76,18 @@ void OnPdfStreamCreated(
auto browser_context =
static_cast<AtomBrowserContext*>(web_contents->GetBrowserContext());
auto stream_manager = browser_context->stream_manager();
std::string view_id = base::GenerateGUID();
std::string stream_id = base::GenerateGUID();
GURL original_url = stream->original_url;
stream_manager->AddStream(std::move(stream), view_id, render_process_id,
stream_manager->AddStream(std::move(stream), stream_id, render_process_id,
render_frame_id);
// The URL passes the stream ID to PDF webui that uses it to extract the
// stream from the StreamManager and also passes the URL that the PDF
// originates from, which is used whenever no stream is available from the
// content layer (this will happen when the webui page is reloaded).
// chrome://pdf-viewer/index.html?streamId=abcd&src=https://somepage/123.pdf
content::NavigationController::LoadURLParams params(GURL(base::StringPrintf(
"%sindex.html?%s=%s&%s=%s", kPdfViewerUIOrigin, kPdfViewerUIId,
view_id.c_str(), kPdfPluginSrc, original_url.spec().c_str())));
"%sindex.html?%s=%s&%s=%s", kPdfViewerUIOrigin, kPdfViewerUIStreamId,
stream_id.c_str(), kPdfPluginSrc, original_url.spec().c_str())));
web_contents->GetController().LoadURLWithParams(params);
}

View file

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

View file

@ -17,32 +17,32 @@ StreamManager::StreamManager() {}
StreamManager::~StreamManager() {}
void StreamManager::AddStream(std::unique_ptr<content::StreamInfo> stream,
const std::string& view_id,
const std::string& stream_id,
int render_process_id,
int render_frame_id) {
streams_.insert(std::make_pair(view_id, std::move(stream)));
observers_[view_id] = base::MakeUnique<EmbedderObserver>(
this, view_id, render_process_id, render_frame_id);
streams_.insert(std::make_pair(stream_id, std::move(stream)));
observers_[stream_id] = base::MakeUnique<EmbedderObserver>(
this, stream_id, render_process_id, render_frame_id);
}
std::unique_ptr<content::StreamInfo> StreamManager::ReleaseStream(
const std::string& view_id) {
auto stream = streams_.find(view_id);
const std::string& stream_id) {
auto stream = streams_.find(stream_id);
if (stream == streams_.end())
return nullptr;
std::unique_ptr<content::StreamInfo> result =
base::WrapUnique(stream->second.release());
streams_.erase(stream);
observers_.erase(view_id);
observers_.erase(stream_id);
return result;
}
StreamManager::EmbedderObserver::EmbedderObserver(StreamManager* stream_manager,
const std::string& view_id,
const std::string& stream_id,
int render_process_id,
int render_frame_id)
: stream_manager_(stream_manager), view_id_(view_id) {
: stream_manager_(stream_manager), stream_id_(stream_id) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(
content::RenderFrameHost::FromID(render_process_id, render_frame_id));
@ -60,7 +60,7 @@ void StreamManager::EmbedderObserver::WebContentsDestroyed() {
void StreamManager::EmbedderObserver::AbortStream() {
Observe(nullptr);
stream_manager_->ReleaseStream(view_id_);
stream_manager_->ReleaseStream(stream_id_);
}
} // namespace atom

View file

@ -22,12 +22,12 @@ class StreamManager {
~StreamManager();
void AddStream(std::unique_ptr<content::StreamInfo> stream,
const std::string& view_id,
const std::string& stream_id,
int render_process_id,
int render_frame_id);
std::unique_ptr<content::StreamInfo> ReleaseStream(
const std::string& view_id);
const std::string& stream_id);
private:
// WebContents observer that deletes an unclaimed stream
@ -35,7 +35,7 @@ class StreamManager {
class EmbedderObserver : public content::WebContentsObserver {
public:
EmbedderObserver(StreamManager* stream_manager,
const std::string& view_id,
const std::string& stream_id,
int render_process_id,
int render_frame_id);
@ -47,13 +47,15 @@ class StreamManager {
void AbortStream();
StreamManager* stream_manager_;
std::string view_id_;
std::string stream_id_;
DISALLOW_COPY_AND_ASSIGN(EmbedderObserver);
};
// StreamID => Stream.
std::map<std::string, std::unique_ptr<content::StreamInfo>> streams_;
// StreamID => WebContents Observer.
std::map<std::string, std::unique_ptr<EmbedderObserver>> observers_;
DISALLOW_COPY_AND_ASSIGN(StreamManager);

View file

@ -23,6 +23,8 @@ namespace atom {
namespace {
// Extracts the path value from the URL without the leading '/',
// which follows the mapping of names in pdf_viewer_resources_map.
std::string PathWithoutParams(const std::string& path) {
return GURL(kPdfViewerUIOrigin + path).path().substr(1);
}
@ -86,14 +88,14 @@ class BundledDataSource : public content::URLDataSource {
PdfViewerUI::PdfViewerUI(content::BrowserContext* browser_context,
content::WebUI* web_ui,
const std::string& view_id,
const std::string& stream_id,
const std::string& src)
: content::WebUIController(web_ui),
content::WebContentsObserver(web_ui->GetWebContents()),
src_(src) {
auto context = static_cast<AtomBrowserContext*>(browser_context);
auto stream_manager = context->stream_manager();
stream_ = stream_manager->ReleaseStream(view_id);
stream_ = stream_manager->ReleaseStream(stream_id);
web_ui->AddMessageHandler(new PdfViewerHandler(stream_.get(), src));
content::URLDataSource::Add(browser_context, new BundledDataSource);
}

View file

@ -24,7 +24,7 @@ class PdfViewerUI : public content::WebUIController,
public:
PdfViewerUI(content::BrowserContext* browser_context,
content::WebUI* web_ui,
const std::string& view_id,
const std::string& stream_id,
const std::string& src);
~PdfViewerUI() override;
@ -35,6 +35,7 @@ class PdfViewerUI : public content::WebUIController,
private:
void OnSaveURLAs(const GURL& url, const content::Referrer& referrer);
// Source URL from where the PDF originates.
std::string src_;
std::unique_ptr<content::StreamInfo> stream_;

View file

@ -30,6 +30,6 @@ const char kPdfPluginSrc[] = "src";
const char kPdfViewerUIOrigin[] = "chrome://pdf-viewer/";
const char kPdfViewerUIHost[] = "pdf-viewer";
const char kPdfViewerUIId[] = "viewId";
const char kPdfViewerUIStreamId[] = "streamId";
} // namespace atom

View file

@ -28,7 +28,7 @@ extern const char kPdfPluginSrc[];
// Constants for PDF viewer webui.
extern const char kPdfViewerUIOrigin[];
extern const char kPdfViewerUIHost[];
extern const char kPdfViewerUIId[];
extern const char kPdfViewerUIStreamId[];
} // namespace atom