1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/pdf/pdf_tab_helper.h" 6 7 #include "base/strings/utf_string_conversions.h" 8 #include "chrome/browser/download/download_stats.h" 9 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser_finder.h" 11 #include "chrome/browser/ui/browser_window.h" 12 #include "chrome/browser/ui/omnibox/location_bar.h" 13 #include "chrome/browser/ui/pdf/open_pdf_in_reader_prompt_delegate.h" 14 #include "chrome/browser/ui/pdf/pdf_unsupported_feature.h" 15 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" 16 #include "chrome/common/render_messages.h" 17 #include "content/public/browser/navigation_details.h" 18 19 #if defined(TOOLKIT_GTK) 20 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h" 21 #include "content/public/browser/javascript_dialog_manager.h" 22 #endif 23 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PDFTabHelper); 25 26 PDFTabHelper::PDFTabHelper(content::WebContents* web_contents) 27 : content::WebContentsObserver(web_contents) { 28 } 29 30 PDFTabHelper::~PDFTabHelper() { 31 } 32 33 void PDFTabHelper::ShowOpenInReaderPrompt( 34 scoped_ptr<OpenPDFInReaderPromptDelegate> prompt) { 35 open_in_reader_prompt_ = prompt.Pass(); 36 UpdateLocationBar(); 37 } 38 39 bool PDFTabHelper::OnMessageReceived(const IPC::Message& message) { 40 bool handled = true; 41 IPC_BEGIN_MESSAGE_MAP(PDFTabHelper, message) 42 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFHasUnsupportedFeature, 43 OnHasUnsupportedFeature) 44 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFSaveURLAs, OnSaveURLAs) 45 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFUpdateContentRestrictions, 46 OnUpdateContentRestrictions) 47 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_PDFModalPromptForPassword, 48 OnModalPromptForPassword) 49 IPC_MESSAGE_UNHANDLED(handled = false) 50 IPC_END_MESSAGE_MAP() 51 return handled; 52 } 53 54 void PDFTabHelper::DidNavigateMainFrame( 55 const content::LoadCommittedDetails& details, 56 const content::FrameNavigateParams& params) { 57 if (open_in_reader_prompt_.get() && 58 open_in_reader_prompt_->ShouldExpire(details)) { 59 open_in_reader_prompt_.reset(); 60 UpdateLocationBar(); 61 } 62 } 63 64 void PDFTabHelper::UpdateLocationBar() { 65 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); 66 if (!browser) 67 return; 68 69 BrowserWindow* window = browser->window(); 70 if (!window) 71 return; 72 73 LocationBar* location_bar = window->GetLocationBar(); 74 if (!location_bar) 75 return; 76 77 location_bar->UpdateOpenPDFInReaderPrompt(); 78 } 79 80 void PDFTabHelper::OnHasUnsupportedFeature() { 81 PDFHasUnsupportedFeature(web_contents()); 82 } 83 84 void PDFTabHelper::OnSaveURLAs(const GURL& url, 85 const content::Referrer& referrer) { 86 RecordDownloadSource(DOWNLOAD_INITIATED_BY_PDF_SAVE); 87 web_contents()->SaveFrame(url, referrer); 88 } 89 90 void PDFTabHelper::OnUpdateContentRestrictions(int content_restrictions) { 91 CoreTabHelper* core_tab_helper = 92 CoreTabHelper::FromWebContents(web_contents()); 93 core_tab_helper->UpdateContentRestrictions(content_restrictions); 94 } 95 96 void PDFTabHelper::OnModalPromptForPasswordClosed( 97 IPC::Message* reply_message, 98 bool success, 99 const base::string16& actual_value) { 100 ChromeViewHostMsg_PDFModalPromptForPassword::WriteReplyParams( 101 reply_message, UTF16ToUTF8(actual_value)); 102 Send(reply_message); 103 } 104 105 void PDFTabHelper::OnModalPromptForPassword(const std::string& prompt, 106 IPC::Message* reply_message) { 107 base::Callback<void(bool, const base::string16&)> callback = 108 base::Bind(&PDFTabHelper::OnModalPromptForPasswordClosed, 109 base::Unretained(this), reply_message); 110 #if !defined(TOOLKIT_GTK) 111 ShowPDFPasswordDialog(web_contents(), base::UTF8ToUTF16(prompt), callback); 112 #else 113 // GTK is going away, so it's not worth the effort to create a password dialog 114 // for it. Cheat (for now) until the GTK code is removed. 115 bool did_suppress_message; 116 GetJavaScriptDialogManagerInstance()->RunJavaScriptDialog( 117 web_contents(), 118 GURL(), 119 std::string(), 120 content::JAVASCRIPT_MESSAGE_TYPE_PROMPT, 121 base::UTF8ToUTF16(prompt), 122 base::string16(), 123 callback, 124 &did_suppress_message); 125 #endif // TOOLKIT_GTK 126 } 127