• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "components/pdf/browser/pdf_web_contents_helper.h"
6 
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"
13 
14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(pdf::PDFWebContentsHelper);
15 
16 namespace pdf {
17 
18 // static
CreateForWebContentsWithClient(content::WebContents * contents,scoped_ptr<PDFWebContentsHelperClient> client)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()));
26 }
27 
PDFWebContentsHelper(content::WebContents * web_contents,scoped_ptr<PDFWebContentsHelperClient> client)28 PDFWebContentsHelper::PDFWebContentsHelper(
29     content::WebContents* web_contents,
30     scoped_ptr<PDFWebContentsHelperClient> client)
31     : content::WebContentsObserver(web_contents), client_(client.Pass()) {
32 }
33 
~PDFWebContentsHelper()34 PDFWebContentsHelper::~PDFWebContentsHelper() {
35 }
36 
ShowOpenInReaderPrompt(scoped_ptr<OpenPDFInReaderPromptClient> prompt)37 void PDFWebContentsHelper::ShowOpenInReaderPrompt(
38     scoped_ptr<OpenPDFInReaderPromptClient> prompt) {
39   open_in_reader_prompt_ = prompt.Pass();
40   UpdateLocationBar();
41 }
42 
OnMessageReceived(const IPC::Message & message)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;
56 }
57 
DidNavigateMainFrame(const content::LoadCommittedDetails & details,const content::FrameNavigateParams & params)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();
65   }
66 }
67 
UpdateLocationBar()68 void PDFWebContentsHelper::UpdateLocationBar() {
69   client_->UpdateLocationBar(web_contents());
70 }
71 
OnHasUnsupportedFeature()72 void PDFWebContentsHelper::OnHasUnsupportedFeature() {
73   client_->OnPDFHasUnsupportedFeature(web_contents());
74 }
75 
OnSaveURLAs(const GURL & url,const content::Referrer & referrer)76 void PDFWebContentsHelper::OnSaveURLAs(const GURL& url,
77                                        const content::Referrer& referrer) {
78   client_->OnSaveURL(web_contents());
79   web_contents()->SaveFrame(url, referrer);
80 }
81 
OnUpdateContentRestrictions(int content_restrictions)82 void PDFWebContentsHelper::OnUpdateContentRestrictions(
83     int content_restrictions) {
84   client_->UpdateContentRestrictions(web_contents(), content_restrictions);
85 }
86 
OnModalPromptForPasswordClosed(IPC::Message * reply_message,bool success,const base::string16 & actual_value)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);
94 }
95 
OnModalPromptForPassword(const std::string & prompt,IPC::Message * 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);
105 }
106 
107 }  // namespace pdf
108