From 644d4a006cd1466cbef51542b299316c57ac5104 Mon Sep 17 00:00:00 2001 From: "thestig@chromium.org" Date: Thu, 25 Aug 2011 21:11:11 +0000 Subject: Merge 95850 - Add content-security-policy directive to print-preview pages. Since print-preview uses an object tag, relax the object-src restriction in the content_security_policy.html file. Also fix typo in print_preview.html where the object tag has a src="", not a data="" attribute. Review URL: http://codereview.chromium.org/7566049 BUG=91057 TBR=tsepez@chromium.org Review URL: http://codereview.chromium.org/7745033 git-svn-id: svn://svn.chromium.org/chrome/branches/835/src@98300 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/browser_resources.grd | 3 +- .../browser/resources/content_security_policy.html | 2 +- .../resources/print_preview/print_preview.html | 16 +- .../resources/print_preview/print_preview.js | 9 + .../browser/ui/webui/chrome_web_ui_data_source.cc | 3 + .../browser/ui/webui/print_preview_data_source.cc | 231 ++++++++------------- .../browser/ui/webui/print_preview_data_source.h | 11 +- 7 files changed, 114 insertions(+), 161 deletions(-) diff --git a/chrome/browser/browser_resources.grd b/chrome/browser/browser_resources.grd index 9ce816b..109ada4 100644 --- a/chrome/browser/browser_resources.grd +++ b/chrome/browser/browser_resources.grd @@ -83,7 +83,8 @@ - + + diff --git a/chrome/browser/resources/content_security_policy.html b/chrome/browser/resources/content_security_policy.html index a0a6581..efeeb78 100644 --- a/chrome/browser/resources/content_security_policy.html +++ b/chrome/browser/resources/content_security_policy.html @@ -11,7 +11,7 @@ --> + @@ -10,15 +11,8 @@ - - - - - - - - - + + + data="chrome://print/dummy.pdf"> + + diff --git a/chrome/browser/resources/print_preview/print_preview.js b/chrome/browser/resources/print_preview/print_preview.js index f775d9c..f142247 100644 --- a/chrome/browser/resources/print_preview/print_preview.js +++ b/chrome/browser/resources/print_preview/print_preview.js @@ -966,3 +966,12 @@ PrintSettings.prototype.save = function() { this.isLandscape = layoutSettings.isLandscape(); } +/// Pull in all other scripts in a single shot. + + + + + + + + diff --git a/chrome/browser/ui/webui/chrome_web_ui_data_source.cc b/chrome/browser/ui/webui/chrome_web_ui_data_source.cc index e4ca9b1..563f1c7 100644 --- a/chrome/browser/ui/webui/chrome_web_ui_data_source.cc +++ b/chrome/browser/ui/webui/chrome_web_ui_data_source.cc @@ -33,6 +33,9 @@ std::string ChromeWebUIDataSource::GetMimeType(const std::string& path) const { if (EndsWith(path, ".js", false)) return "application/javascript"; + if (EndsWith(path, ".pdf", false)) + return "application/pdf"; + return "text/html"; } diff --git a/chrome/browser/ui/webui/print_preview_data_source.cc b/chrome/browser/ui/webui/print_preview_data_source.cc index 5cda03d..f5c112a 100644 --- a/chrome/browser/ui/webui/print_preview_data_source.cc +++ b/chrome/browser/ui/webui/print_preview_data_source.cc @@ -23,117 +23,80 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -namespace { +PrintPreviewDataSource::PrintPreviewDataSource() + : ChromeWebUIDataSource(chrome::kChromeUIPrintHost) { -void SetLocalizedStrings(DictionaryValue* localized_strings) { - localized_strings->SetString(std::string("title"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE)); - localized_strings->SetString(std::string("loading"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LOADING)); + AddLocalizedString("title", IDS_PRINT_PREVIEW_TITLE); + AddLocalizedString("loading", IDS_PRINT_PREVIEW_LOADING); #if defined(GOOGLE_CHROME_BUILD) - localized_strings->SetString(std::string("noPlugin"), - l10n_util::GetStringFUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN, - ASCIIToUTF16("chrome://plugins/"))); + AddString("noPlugin", l10n_util::GetStringFUTF8( + IDS_PRINT_PREVIEW_NO_PLUGIN, ASCIIToUTF16("chrome://plugins/")); #else - localized_strings->SetString(std::string("noPlugin"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN)); + AddLocalizedString("noPlugin", IDS_PRINT_PREVIEW_NO_PLUGIN); #endif - localized_strings->SetString(std::string("launchNativeDialog"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NATIVE_DIALOG)); - localized_strings->SetString(std::string("previewFailed"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_FAILED)); - localized_strings->SetString(std::string("initiatorTabClosed"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INITIATOR_TAB_CLOSED)); - localized_strings->SetString(std::string("reopenPage"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_REOPEN_PAGE)); - - localized_strings->SetString(std::string("printButton"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_BUTTON)); - localized_strings->SetString(std::string("cancelButton"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_CANCEL_BUTTON)); - localized_strings->SetString(std::string("printing"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINTING)); - - localized_strings->SetString(std::string("destinationLabel"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DESTINATION_LABEL)); - localized_strings->SetString(std::string("copiesLabel"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_LABEL)); - localized_strings->SetString(std::string("examplePageRangeText"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT)); - localized_strings->SetString(std::string("invalidNumberOfCopies"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INVALID_NUMBER_OF_COPIES)); - localized_strings->SetString(std::string("layoutLabel"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LAYOUT_LABEL)); - localized_strings->SetString(std::string("optionAllPages"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_ALL_PAGES)); - localized_strings->SetString(std::string("optionBw"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_BW)); - localized_strings->SetString(std::string("optionCollate"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLLATE)); - localized_strings->SetString(std::string("optionColor"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLOR)); - localized_strings->SetString(std::string("optionLandscape"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_LANDSCAPE)); - localized_strings->SetString(std::string("optionPortrait"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_PORTRAIT)); - localized_strings->SetString(std::string("optionTwoSided"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_TWO_SIDED)); - localized_strings->SetString(std::string("pagesLabel"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGES_LABEL)); - localized_strings->SetString(std::string("pageRangeTextBox"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT)); - localized_strings->SetString(std::string("pageRangeRadio"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO)); - localized_strings->SetString(std::string("printToPDF"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_TO_PDF)); - localized_strings->SetString(std::string("printPreviewTitleFormat"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE_FORMAT)); - localized_strings->SetString(std::string("printPreviewSummaryFormatShort"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT)); - localized_strings->SetString(std::string("printPreviewSummaryFormatLong"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG)); - localized_strings->SetString(std::string("printPreviewSheetsLabelSingular"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR)); - localized_strings->SetString(std::string("printPreviewSheetsLabelPlural"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL)); - localized_strings->SetString(std::string("printPreviewPageLabelSingular"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR)); - localized_strings->SetString(std::string("printPreviewPageLabelPlural"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL)); - localized_strings->SetString(std::string("systemDialogOption"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION)); - localized_strings->SetString(std::string("pageRangeInstruction"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION)); - localized_strings->SetString(std::string("copiesInstruction"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION)); - localized_strings->SetString(std::string("signIn"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SIGN_IN)); - localized_strings->SetString(std::string("morePrinters"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MORE_PRINTERS)); - localized_strings->SetString(std::string("addCloudPrinter"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_ADD_CLOUD_PRINTER)); - localized_strings->SetString(std::string("cloudPrinters"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_CLOUD_PRINTERS)); - localized_strings->SetString(std::string("localPrinters"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LOCAL_PRINTERS)); - localized_strings->SetString(std::string("manageCloudPrinters"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_CLOUD_PRINTERS)); - localized_strings->SetString(std::string("manageLocalPrinters"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_LOCAL_PRINTERS)); - localized_strings->SetString(std::string("managePrinters"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS)); - localized_strings->SetString(std::string("incrementTitle"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INCREMENT_TITLE)); - localized_strings->SetString(std::string("decrementTitle"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DECREMENT_TITLE)); - localized_strings->SetString(std::string("printPagesLabel"), - l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_PAGES_LABEL)); -} - -} // namespace - -PrintPreviewDataSource::PrintPreviewDataSource() - : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) { + AddLocalizedString("launchNativeDialog", IDS_PRINT_PREVIEW_NATIVE_DIALOG); + AddLocalizedString("previewFailed", IDS_PRINT_PREVIEW_FAILED); + AddLocalizedString("initiatorTabClosed", + IDS_PRINT_PREVIEW_INITIATOR_TAB_CLOSED); + AddLocalizedString("reopenPage", IDS_PRINT_PREVIEW_REOPEN_PAGE); + + AddLocalizedString("printButton", IDS_PRINT_PREVIEW_PRINT_BUTTON); + AddLocalizedString("cancelButton", IDS_PRINT_PREVIEW_CANCEL_BUTTON); + AddLocalizedString("printing", IDS_PRINT_PREVIEW_PRINTING); + + AddLocalizedString("destinationLabel", IDS_PRINT_PREVIEW_DESTINATION_LABEL); + AddLocalizedString("copiesLabel", IDS_PRINT_PREVIEW_COPIES_LABEL); + AddLocalizedString("examplePageRangeText", + IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT); + AddLocalizedString("invalidNumberOfCopies", + IDS_PRINT_PREVIEW_INVALID_NUMBER_OF_COPIES); + AddLocalizedString("layoutLabel", IDS_PRINT_PREVIEW_LAYOUT_LABEL); + AddLocalizedString("optionAllPages", IDS_PRINT_PREVIEW_OPTION_ALL_PAGES); + AddLocalizedString("optionBw", IDS_PRINT_PREVIEW_OPTION_BW); + AddLocalizedString("optionCollate", IDS_PRINT_PREVIEW_OPTION_COLLATE); + AddLocalizedString("optionColor", IDS_PRINT_PREVIEW_OPTION_COLOR); + AddLocalizedString("optionLandscape", IDS_PRINT_PREVIEW_OPTION_LANDSCAPE); + AddLocalizedString("optionPortrait", IDS_PRINT_PREVIEW_OPTION_PORTRAIT); + AddLocalizedString("optionTwoSided", IDS_PRINT_PREVIEW_OPTION_TWO_SIDED); + AddLocalizedString("pagesLabel", IDS_PRINT_PREVIEW_PAGES_LABEL); + AddLocalizedString("pageRangeTextBox", IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT); + AddLocalizedString("pageRangeRadio", IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO); + AddLocalizedString("printToPDF", IDS_PRINT_PREVIEW_PRINT_TO_PDF); + AddLocalizedString("printPreviewTitleFormat", IDS_PRINT_PREVIEW_TITLE_FORMAT); + AddLocalizedString("printPreviewSummaryFormatShort", + IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT); + AddLocalizedString("printPreviewSummaryFormatLong", + IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG); + AddLocalizedString("printPreviewSheetsLabelSingular", + IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR); + AddLocalizedString("printPreviewSheetsLabelPlural", + IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL); + AddLocalizedString("printPreviewPageLabelSingular", + IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR); + AddLocalizedString("printPreviewPageLabelPlural", + IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL); + AddLocalizedString("systemDialogOption", + IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION); + AddLocalizedString("pageRangeInstruction", + IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION); + AddLocalizedString("copiesInstruction", IDS_PRINT_PREVIEW_COPIES_INSTRUCTION); + AddLocalizedString("signIn", IDS_PRINT_PREVIEW_SIGN_IN); + AddLocalizedString("morePrinters", IDS_PRINT_PREVIEW_MORE_PRINTERS); + AddLocalizedString("addCloudPrinter", IDS_PRINT_PREVIEW_ADD_CLOUD_PRINTER); + AddLocalizedString("cloudPrinters", IDS_PRINT_PREVIEW_CLOUD_PRINTERS); + AddLocalizedString("localPrinters", IDS_PRINT_PREVIEW_LOCAL_PRINTERS); + AddLocalizedString("manageCloudPrinters", + IDS_PRINT_PREVIEW_MANAGE_CLOUD_PRINTERS); + AddLocalizedString("manageLocalPrinters", + IDS_PRINT_PREVIEW_MANAGE_LOCAL_PRINTERS); + AddLocalizedString("managePrinters", IDS_PRINT_PREVIEW_MANAGE_PRINTERS); + AddLocalizedString("incrementTitle", IDS_PRINT_PREVIEW_INCREMENT_TITLE); + AddLocalizedString("decrementTitle", IDS_PRINT_PREVIEW_DECREMENT_TITLE); + AddLocalizedString("printPagesLabel", IDS_PRINT_PREVIEW_PRINT_PAGES_LABEL); + + set_json_path("strings.js"); + add_resource_path("print_preview.js", IDR_PRINT_PREVIEW_JS); + set_default_resource(IDR_PRINT_PREVIEW_HTML); } PrintPreviewDataSource::~PrintPreviewDataSource() { @@ -142,46 +105,26 @@ PrintPreviewDataSource::~PrintPreviewDataSource() { void PrintPreviewDataSource::StartDataRequest(const std::string& path, bool is_incognito, int request_id) { - scoped_refptr data; - - bool preview_data_requested = EndsWith(path, "/print.pdf", true); - if (preview_data_requested) { - std::vector url_substr; - base::SplitString(path, '/', &url_substr); - int page_index = 0; - if (url_substr.size() == 3 && base::StringToInt(url_substr[1], - &page_index)) { - PrintPreviewDataService::GetInstance()->GetDataEntry(url_substr[0], - page_index, &data); - } + // Parent class handles most requests except for the print preview data. + if (!EndsWith(path, "/print.pdf", true)) { + ChromeWebUIDataSource::StartDataRequest(path, is_incognito, request_id); + return; } - if (path.empty()) { - // Print Preview Index page. - DictionaryValue localized_strings; - SetLocalizedStrings(&localized_strings); - SetFontAndTextDirection(&localized_strings); - - static const base::StringPiece print_html( - ResourceBundle::GetSharedInstance().GetRawDataResource( - IDR_PRINT_PREVIEW_HTML)); - std::string full_html = jstemplate_builder::GetI18nTemplateHtml( - print_html, &localized_strings); - - SendResponse(request_id, base::RefCountedString::TakeString(&full_html)); - return; - } else if (preview_data_requested && data.get()) { - // Print Preview data. + // Print Preview data. + scoped_refptr data; + std::vector url_substr; + base::SplitString(path, '/', &url_substr); + int page_index = 0; + if (url_substr.size() == 3 && base::StringToInt(url_substr[1], &page_index)) { + PrintPreviewDataService::GetInstance()->GetDataEntry( + url_substr[0], page_index, &data); + } + if (data.get()) { SendResponse(request_id, data); return; - } else { - // Invalid request. - scoped_refptr empty_bytes(new RefCountedBytes); - SendResponse(request_id, empty_bytes); - return; } -} - -std::string PrintPreviewDataSource::GetMimeType(const std::string& path) const { - return path.empty() ? "text/html" : "application/pdf"; + // Invalid request. + scoped_refptr empty_bytes(new RefCountedBytes); + SendResponse(request_id, empty_bytes); } diff --git a/chrome/browser/ui/webui/print_preview_data_source.h b/chrome/browser/ui/webui/print_preview_data_source.h index 7f82d00..9c9d66e 100644 --- a/chrome/browser/ui/webui/print_preview_data_source.h +++ b/chrome/browser/ui/webui/print_preview_data_source.h @@ -10,10 +10,11 @@ #include "base/compiler_specific.h" #include "chrome/browser/ui/webui/chrome_url_data_manager.h" +#include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" // PrintPreviewDataSource serves data for chrome://print requests. // -// The format for requesting data is as follows: +// The format for requesting PDF data is as follows: // chrome://print///print.pdf // // Parameters (< > required): @@ -24,8 +25,10 @@ // // Example: // chrome://print/0xab0123ef/10/print.pdf - -class PrintPreviewDataSource : public ChromeURLDataManager::DataSource { +// +// Requests to chrome://print with paths not ending in /print.pdf are used +// to return the markup or other resources for the print preview page itself. +class PrintPreviewDataSource : public ChromeWebUIDataSource { public: PrintPreviewDataSource(); @@ -33,8 +36,6 @@ class PrintPreviewDataSource : public ChromeURLDataManager::DataSource { virtual void StartDataRequest(const std::string& path, bool is_incognito, int request_id) OVERRIDE; - virtual std::string GetMimeType(const std::string& path) const OVERRIDE; - private: virtual ~PrintPreviewDataSource(); -- cgit v1.1