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 "chrome/browser/download/download_stats.h" 8 #include "chrome/browser/ui/browser.h" 9 #include "chrome/browser/ui/browser_finder.h" 10 #include "chrome/browser/ui/browser_window.h" 11 #include "chrome/browser/ui/omnibox/location_bar.h" 12 #include "chrome/browser/ui/pdf/open_pdf_in_reader_prompt_delegate.h" 13 #include "chrome/browser/ui/pdf/pdf_unsupported_feature.h" 14 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" 15 #include "chrome/common/render_messages.h" 16 #include "content/public/browser/navigation_details.h" 17 18 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PDFTabHelper); 19 20 PDFTabHelper::PDFTabHelper(content::WebContents* web_contents) 21 : content::WebContentsObserver(web_contents) { 22 } 23 24 PDFTabHelper::~PDFTabHelper() { 25 } 26 27 void PDFTabHelper::ShowOpenInReaderPrompt( 28 scoped_ptr<OpenPDFInReaderPromptDelegate> prompt) { 29 open_in_reader_prompt_ = prompt.Pass(); 30 UpdateLocationBar(); 31 } 32 33 bool PDFTabHelper::OnMessageReceived(const IPC::Message& message) { 34 bool handled = true; 35 IPC_BEGIN_MESSAGE_MAP(PDFTabHelper, message) 36 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFHasUnsupportedFeature, 37 OnHasUnsupportedFeature) 38 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFSaveURLAs, OnSaveURLAs) 39 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFUpdateContentRestrictions, 40 OnUpdateContentRestrictions) 41 IPC_MESSAGE_UNHANDLED(handled = false) 42 IPC_END_MESSAGE_MAP() 43 return handled; 44 } 45 46 void PDFTabHelper::DidNavigateMainFrame( 47 const content::LoadCommittedDetails& details, 48 const content::FrameNavigateParams& params) { 49 if (open_in_reader_prompt_.get() && 50 open_in_reader_prompt_->ShouldExpire(details)) { 51 open_in_reader_prompt_.reset(); 52 UpdateLocationBar(); 53 } 54 } 55 56 void PDFTabHelper::UpdateLocationBar() { 57 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); 58 if (!browser) 59 return; 60 61 BrowserWindow* window = browser->window(); 62 if (!window) 63 return; 64 65 LocationBar* location_bar = window->GetLocationBar(); 66 if (!location_bar) 67 return; 68 69 location_bar->UpdateOpenPDFInReaderPrompt(); 70 } 71 72 void PDFTabHelper::OnHasUnsupportedFeature() { 73 PDFHasUnsupportedFeature(web_contents()); 74 } 75 76 void PDFTabHelper::OnSaveURLAs(const GURL& url, 77 const content::Referrer& referrer) { 78 RecordDownloadSource(DOWNLOAD_INITIATED_BY_PDF_SAVE); 79 web_contents()->SaveFrame(url, referrer); 80 } 81 82 void PDFTabHelper::OnUpdateContentRestrictions(int content_restrictions) { 83 CoreTabHelper* core_tab_helper = 84 CoreTabHelper::FromWebContents(web_contents()); 85 core_tab_helper->UpdateContentRestrictions(content_restrictions); 86 } 87