pdf_viewer_handler.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright (c) 2017 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/ui/webui/pdf_viewer_handler.h"
  5. #include "atom/common/atom_constants.h"
  6. #include "base/bind.h"
  7. #include "base/values.h"
  8. #include "chrome/browser/browser_process.h"
  9. #include "content/public/browser/stream_handle.h"
  10. #include "content/public/browser/stream_info.h"
  11. #include "content/public/browser/web_contents.h"
  12. #include "content/public/browser/web_ui.h"
  13. #include "content/public/common/page_zoom.h"
  14. #include "content/public/common/url_constants.h"
  15. #include "net/http/http_response_headers.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #include "ui/base/webui/web_ui_util.h"
  18. namespace atom {
  19. namespace {
  20. void CreateResponseHeadersDictionary(const net::HttpResponseHeaders* headers,
  21. base::DictionaryValue* result) {
  22. if (!headers)
  23. return;
  24. size_t iter = 0;
  25. std::string header_name;
  26. std::string header_value;
  27. while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) {
  28. base::Value* existing_value = nullptr;
  29. if (result->Get(header_name, &existing_value)) {
  30. std::string src = existing_value->GetString();
  31. result->SetString(header_name, src + ", " + header_value);
  32. } else {
  33. result->SetString(header_name, header_value);
  34. }
  35. }
  36. }
  37. void PopulateStreamInfo(base::DictionaryValue* stream_info,
  38. content::StreamInfo* stream,
  39. const std::string& original_url) {
  40. auto headers_dict = std::make_unique<base::DictionaryValue>();
  41. auto stream_url = stream->handle->GetURL().spec();
  42. CreateResponseHeadersDictionary(stream->response_headers.get(),
  43. headers_dict.get());
  44. stream_info->SetString("streamURL", stream_url);
  45. stream_info->SetString("originalURL", original_url);
  46. stream_info->Set("responseHeaders", std::move(headers_dict));
  47. }
  48. } // namespace
  49. PdfViewerHandler::PdfViewerHandler(const std::string& src)
  50. : original_url_(src) {}
  51. PdfViewerHandler::~PdfViewerHandler() {
  52. RemoveObserver();
  53. }
  54. void PdfViewerHandler::SetPdfResourceStream(content::StreamInfo* stream) {
  55. stream_ = stream;
  56. if (!!initialize_callback_id_.get()) {
  57. auto list = std::make_unique<base::ListValue>();
  58. list->Set(0, std::move(initialize_callback_id_));
  59. Initialize(list.get());
  60. }
  61. }
  62. void PdfViewerHandler::RegisterMessages() {
  63. web_ui()->RegisterMessageCallback(
  64. "initialize",
  65. base::Bind(&PdfViewerHandler::Initialize, base::Unretained(this)));
  66. web_ui()->RegisterMessageCallback(
  67. "getDefaultZoom",
  68. base::Bind(&PdfViewerHandler::GetInitialZoom, base::Unretained(this)));
  69. web_ui()->RegisterMessageCallback(
  70. "getInitialZoom",
  71. base::Bind(&PdfViewerHandler::GetInitialZoom, base::Unretained(this)));
  72. web_ui()->RegisterMessageCallback(
  73. "setZoom",
  74. base::Bind(&PdfViewerHandler::SetZoom, base::Unretained(this)));
  75. web_ui()->RegisterMessageCallback(
  76. "getStrings",
  77. base::Bind(&PdfViewerHandler::GetStrings, base::Unretained(this)));
  78. web_ui()->RegisterMessageCallback(
  79. "reload", base::Bind(&PdfViewerHandler::Reload, base::Unretained(this)));
  80. }
  81. void PdfViewerHandler::OnJavascriptAllowed() {
  82. AddObserver();
  83. }
  84. void PdfViewerHandler::OnJavascriptDisallowed() {
  85. RemoveObserver();
  86. }
  87. void PdfViewerHandler::Initialize(const base::ListValue* args) {
  88. CHECK_EQ(1U, args->GetSize());
  89. const base::Value* callback_id;
  90. CHECK(args->Get(0, &callback_id));
  91. if (stream_) {
  92. CHECK(!initialize_callback_id_.get());
  93. AllowJavascript();
  94. auto stream_info = std::make_unique<base::DictionaryValue>();
  95. PopulateStreamInfo(stream_info.get(), stream_, original_url_);
  96. ResolveJavascriptCallback(*callback_id, *stream_info);
  97. } else {
  98. initialize_callback_id_ = callback_id->CreateDeepCopy();
  99. }
  100. auto zoom_controller =
  101. WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
  102. zoom_controller->SetZoomMode(WebContentsZoomController::ZOOM_MODE_MANUAL);
  103. zoom_controller->SetZoomLevel(0);
  104. }
  105. void PdfViewerHandler::GetDefaultZoom(const base::ListValue* args) {
  106. if (!IsJavascriptAllowed())
  107. return;
  108. CHECK_EQ(1U, args->GetSize());
  109. const base::Value* callback_id;
  110. CHECK(args->Get(0, &callback_id));
  111. auto zoom_controller =
  112. WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
  113. double zoom_level = zoom_controller->GetDefaultZoomLevel();
  114. ResolveJavascriptCallback(
  115. *callback_id, base::Value(content::ZoomLevelToZoomFactor(zoom_level)));
  116. }
  117. void PdfViewerHandler::GetInitialZoom(const base::ListValue* args) {
  118. if (!IsJavascriptAllowed())
  119. return;
  120. CHECK_EQ(1U, args->GetSize());
  121. const base::Value* callback_id;
  122. CHECK(args->Get(0, &callback_id));
  123. auto zoom_controller =
  124. WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
  125. double zoom_level = zoom_controller->GetZoomLevel();
  126. ResolveJavascriptCallback(
  127. *callback_id, base::Value(content::ZoomLevelToZoomFactor(zoom_level)));
  128. }
  129. void PdfViewerHandler::SetZoom(const base::ListValue* args) {
  130. if (!IsJavascriptAllowed())
  131. return;
  132. CHECK_EQ(2U, args->GetSize());
  133. const base::Value* callback_id;
  134. CHECK(args->Get(0, &callback_id));
  135. double zoom_level = 0.0;
  136. CHECK(args->GetDouble(1, &zoom_level));
  137. auto zoom_controller =
  138. WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
  139. zoom_controller->SetZoomLevel(zoom_level);
  140. ResolveJavascriptCallback(*callback_id, base::Value(zoom_level));
  141. }
  142. void PdfViewerHandler::GetStrings(const base::ListValue* args) {
  143. if (!IsJavascriptAllowed())
  144. return;
  145. CHECK_EQ(1U, args->GetSize());
  146. const base::Value* callback_id;
  147. CHECK(args->Get(0, &callback_id));
  148. auto result = std::make_unique<base::DictionaryValue>();
  149. // TODO(deepak1556): Generate strings from components/pdf_strings.grdp.
  150. #define SET_STRING(id, resource) result->SetString(id, resource)
  151. SET_STRING("passwordPrompt",
  152. "This document is password protected. Please enter a password.");
  153. SET_STRING("passwordSubmit", "Submit");
  154. SET_STRING("passwordInvalid", "Incorrect password");
  155. SET_STRING("pageLoading", "Loading...");
  156. SET_STRING("pageLoadFailed", "Failed to load PDF document");
  157. SET_STRING("pageReload", "Reload");
  158. SET_STRING("bookmarks", "Bookmarks");
  159. SET_STRING("labelPageNumber", "Page number");
  160. SET_STRING("tooltipRotateCW", "Rotate clockwise");
  161. SET_STRING("tooltipDownload", "Download");
  162. SET_STRING("tooltipFitToPage", "Fit to page");
  163. SET_STRING("tooltipFitToWidth", "Fit to width");
  164. SET_STRING("tooltipZoomIn", "Zoom in");
  165. SET_STRING("tooltipZoomOut", "Zoom out");
  166. #undef SET_STRING
  167. webui::SetLoadTimeDataDefaults(g_browser_process->GetApplicationLocale(),
  168. result.get());
  169. ResolveJavascriptCallback(*callback_id, *result);
  170. }
  171. void PdfViewerHandler::Reload(const base::ListValue* args) {
  172. CHECK_EQ(0U, args->GetSize());
  173. web_ui()->GetWebContents()->ReloadFocusedFrame(false);
  174. }
  175. void PdfViewerHandler::OnZoomLevelChanged(content::WebContents* web_contents,
  176. double level,
  177. bool is_temporary) {
  178. if (web_ui()->GetWebContents() == web_contents) {
  179. CallJavascriptFunction("cr.webUIListenerCallback",
  180. base::Value("onZoomLevelChanged"),
  181. base::Value(content::ZoomLevelToZoomFactor(level)));
  182. }
  183. }
  184. void PdfViewerHandler::AddObserver() {
  185. auto zoom_controller =
  186. WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
  187. zoom_controller->AddObserver(this);
  188. }
  189. void PdfViewerHandler::RemoveObserver() {
  190. auto zoom_controller =
  191. WebContentsZoomController::FromWebContents(web_ui()->GetWebContents());
  192. zoom_controller->RemoveObserver(this);
  193. }
  194. } // namespace atom