app_shell: Add version number in user agent
[chromium-blink-merge.git] / chrome / browser / ui / pdf / pdf_tab_helper.cc
blob71edc67de929b744152b539232319c045feaa09d
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.
5 #include "chrome/browser/ui/pdf/pdf_tab_helper.h"
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/location_bar/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"
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PDFTabHelper);
21 PDFTabHelper::PDFTabHelper(content::WebContents* web_contents)
22 : content::WebContentsObserver(web_contents) {
25 PDFTabHelper::~PDFTabHelper() {
28 void PDFTabHelper::ShowOpenInReaderPrompt(
29 scoped_ptr<OpenPDFInReaderPromptDelegate> prompt) {
30 open_in_reader_prompt_ = prompt.Pass();
31 UpdateLocationBar();
34 bool PDFTabHelper::OnMessageReceived(const IPC::Message& message) {
35 bool handled = true;
36 IPC_BEGIN_MESSAGE_MAP(PDFTabHelper, message)
37 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFHasUnsupportedFeature,
38 OnHasUnsupportedFeature)
39 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFSaveURLAs, OnSaveURLAs)
40 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFUpdateContentRestrictions,
41 OnUpdateContentRestrictions)
42 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_PDFModalPromptForPassword,
43 OnModalPromptForPassword)
44 IPC_MESSAGE_UNHANDLED(handled = false)
45 IPC_END_MESSAGE_MAP()
46 return handled;
49 void PDFTabHelper::DidNavigateMainFrame(
50 const content::LoadCommittedDetails& details,
51 const content::FrameNavigateParams& params) {
52 if (open_in_reader_prompt_.get() &&
53 open_in_reader_prompt_->ShouldExpire(details)) {
54 open_in_reader_prompt_.reset();
55 UpdateLocationBar();
59 void PDFTabHelper::UpdateLocationBar() {
60 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
61 if (!browser)
62 return;
64 BrowserWindow* window = browser->window();
65 if (!window)
66 return;
68 LocationBar* location_bar = window->GetLocationBar();
69 if (!location_bar)
70 return;
72 location_bar->UpdateOpenPDFInReaderPrompt();
75 void PDFTabHelper::OnHasUnsupportedFeature() {
76 PDFHasUnsupportedFeature(web_contents());
79 void PDFTabHelper::OnSaveURLAs(const GURL& url,
80 const content::Referrer& referrer) {
81 RecordDownloadSource(DOWNLOAD_INITIATED_BY_PDF_SAVE);
82 web_contents()->SaveFrame(url, referrer);
85 void PDFTabHelper::OnUpdateContentRestrictions(int content_restrictions) {
86 CoreTabHelper* core_tab_helper =
87 CoreTabHelper::FromWebContents(web_contents());
88 // |core_tab_helper| is NULL for WebViewGuest.
89 if (core_tab_helper)
90 core_tab_helper->UpdateContentRestrictions(content_restrictions);
93 void PDFTabHelper::OnModalPromptForPasswordClosed(
94 IPC::Message* reply_message,
95 bool success,
96 const base::string16& actual_value) {
97 ChromeViewHostMsg_PDFModalPromptForPassword::WriteReplyParams(
98 reply_message, base::UTF16ToUTF8(actual_value));
99 Send(reply_message);
102 void PDFTabHelper::OnModalPromptForPassword(const std::string& prompt,
103 IPC::Message* reply_message) {
104 base::Callback<void(bool, const base::string16&)> callback =
105 base::Bind(&PDFTabHelper::OnModalPromptForPasswordClosed,
106 base::Unretained(this), reply_message);
107 ShowPDFPasswordDialog(web_contents(), base::UTF8ToUTF16(prompt), callback);