Componentize AccountReconcilor.
[chromium-blink-merge.git] / chrome / browser / ui / pdf / pdf_tab_helper.cc
bloba01b3a43e5859a028a7ebd442d3d01a32c505905
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/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"
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
24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PDFTabHelper);
26 PDFTabHelper::PDFTabHelper(content::WebContents* web_contents)
27 : content::WebContentsObserver(web_contents) {
30 PDFTabHelper::~PDFTabHelper() {
33 void PDFTabHelper::ShowOpenInReaderPrompt(
34 scoped_ptr<OpenPDFInReaderPromptDelegate> prompt) {
35 open_in_reader_prompt_ = prompt.Pass();
36 UpdateLocationBar();
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;
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();
64 void PDFTabHelper::UpdateLocationBar() {
65 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
66 if (!browser)
67 return;
69 BrowserWindow* window = browser->window();
70 if (!window)
71 return;
73 LocationBar* location_bar = window->GetLocationBar();
74 if (!location_bar)
75 return;
77 location_bar->UpdateOpenPDFInReaderPrompt();
80 void PDFTabHelper::OnHasUnsupportedFeature() {
81 PDFHasUnsupportedFeature(web_contents());
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);
90 void PDFTabHelper::OnUpdateContentRestrictions(int content_restrictions) {
91 CoreTabHelper* core_tab_helper =
92 CoreTabHelper::FromWebContents(web_contents());
93 core_tab_helper->UpdateContentRestrictions(content_restrictions);
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, base::UTF16ToUTF8(actual_value));
102 Send(reply_message);
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