Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / pdf / browser / pdf_web_contents_helper.cc
blob59a17a88180cdf4471062aad0d4b028c3b598116
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 "components/pdf/browser/pdf_web_contents_helper.h"
7 #include "base/bind.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/pdf/browser/open_pdf_in_reader_prompt_client.h"
10 #include "components/pdf/browser/pdf_web_contents_helper_client.h"
11 #include "components/pdf/common/pdf_messages.h"
12 #include "content/public/browser/navigation_details.h"
14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(pdf::PDFWebContentsHelper);
16 namespace pdf {
18 // static
19 void PDFWebContentsHelper::CreateForWebContentsWithClient(
20 content::WebContents* contents,
21 scoped_ptr<PDFWebContentsHelperClient> client) {
22 if (FromWebContents(contents))
23 return;
24 contents->SetUserData(UserDataKey(),
25 new PDFWebContentsHelper(contents, client.Pass()));
28 PDFWebContentsHelper::PDFWebContentsHelper(
29 content::WebContents* web_contents,
30 scoped_ptr<PDFWebContentsHelperClient> client)
31 : content::WebContentsObserver(web_contents), client_(client.Pass()) {
34 PDFWebContentsHelper::~PDFWebContentsHelper() {
37 void PDFWebContentsHelper::ShowOpenInReaderPrompt(
38 scoped_ptr<OpenPDFInReaderPromptClient> prompt) {
39 open_in_reader_prompt_ = prompt.Pass();
40 UpdateLocationBar();
43 bool PDFWebContentsHelper::OnMessageReceived(const IPC::Message& message) {
44 bool handled = true;
45 IPC_BEGIN_MESSAGE_MAP(PDFWebContentsHelper, message)
46 IPC_MESSAGE_HANDLER(PDFHostMsg_PDFHasUnsupportedFeature,
47 OnHasUnsupportedFeature)
48 IPC_MESSAGE_HANDLER(PDFHostMsg_PDFSaveURLAs, OnSaveURLAs)
49 IPC_MESSAGE_HANDLER(PDFHostMsg_PDFUpdateContentRestrictions,
50 OnUpdateContentRestrictions)
51 IPC_MESSAGE_HANDLER_DELAY_REPLY(PDFHostMsg_PDFModalPromptForPassword,
52 OnModalPromptForPassword)
53 IPC_MESSAGE_UNHANDLED(handled = false)
54 IPC_END_MESSAGE_MAP()
55 return handled;
58 void PDFWebContentsHelper::DidNavigateMainFrame(
59 const content::LoadCommittedDetails& details,
60 const content::FrameNavigateParams& params) {
61 if (open_in_reader_prompt_.get() &&
62 open_in_reader_prompt_->ShouldExpire(details)) {
63 open_in_reader_prompt_.reset();
64 UpdateLocationBar();
68 void PDFWebContentsHelper::UpdateLocationBar() {
69 client_->UpdateLocationBar(web_contents());
72 void PDFWebContentsHelper::OnHasUnsupportedFeature() {
73 client_->OnPDFHasUnsupportedFeature(web_contents());
76 void PDFWebContentsHelper::OnSaveURLAs(const GURL& url,
77 const content::Referrer& referrer) {
78 client_->OnSaveURL(web_contents());
79 web_contents()->SaveFrame(url, referrer);
82 void PDFWebContentsHelper::OnUpdateContentRestrictions(
83 int content_restrictions) {
84 client_->UpdateContentRestrictions(web_contents(), content_restrictions);
87 void PDFWebContentsHelper::OnModalPromptForPasswordClosed(
88 IPC::Message* reply_message,
89 bool success,
90 const base::string16& actual_value) {
91 PDFHostMsg_PDFModalPromptForPassword::WriteReplyParams(
92 reply_message, base::UTF16ToUTF8(actual_value));
93 Send(reply_message);
96 void PDFWebContentsHelper::OnModalPromptForPassword(
97 const std::string& prompt,
98 IPC::Message* reply_message) {
99 base::Callback<void(bool, const base::string16&)> callback =
100 base::Bind(&PDFWebContentsHelper::OnModalPromptForPasswordClosed,
101 base::Unretained(this),
102 reply_message);
103 client_->OnShowPDFPasswordDialog(
104 web_contents(), base::UTF8ToUTF16(prompt), callback);
107 } // namespace pdf