Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/pdf/pwdinteract.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <sal/config.h>
21
22
#include <cassert>
23
#include <mutex>
24
25
#include <vcl/pdf/pwdinteract.hxx>
26
27
#include <com/sun/star/task/ErrorCodeRequest.hpp>
28
#include <com/sun/star/task/XInteractionHandler.hpp>
29
#include <com/sun/star/task/XInteractionRequest.hpp>
30
#include <com/sun/star/task/XInteractionPassword.hpp>
31
#include <com/sun/star/task/DocumentPasswordRequest2.hpp>
32
33
#include <cppuhelper/implbase.hxx>
34
#include <rtl/ref.hxx>
35
#include <comphelper/errcode.hxx>
36
37
using namespace com::sun::star;
38
39
namespace
40
{
41
42
class PDFPasswordRequest:
43
    public cppu::WeakImplHelper<
44
        task::XInteractionRequest, task::XInteractionPassword >
45
{
46
private:
47
    mutable std::mutex            m_aMutex;
48
    uno::Any                      m_aRequest;
49
    OUString                 m_aPassword;
50
    bool                          m_bSelected;
51
52
public:
53
    explicit PDFPasswordRequest(bool bFirstTry, const OUString& rName);
54
    PDFPasswordRequest(const PDFPasswordRequest&) = delete;
55
    PDFPasswordRequest& operator=(const PDFPasswordRequest&) = delete;
56
57
    // XInteractionRequest
58
    virtual uno::Any SAL_CALL getRequest(  ) override;
59
    virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > > SAL_CALL getContinuations(  ) override;
60
61
    // XInteractionPassword
62
    virtual void SAL_CALL setPassword( const OUString& rPwd ) override;
63
    virtual OUString SAL_CALL getPassword() override;
64
65
    // XInteractionContinuation
66
    virtual void SAL_CALL select() override;
67
68
0
    bool isSelected() const { std::scoped_lock const guard( m_aMutex ); return m_bSelected; }
69
70
private:
71
0
    virtual ~PDFPasswordRequest() override {}
72
};
73
74
PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry, const OUString& rName ) :
75
    m_aRequest(
76
0
        uno::Any(
77
0
            task::DocumentPasswordRequest2(
78
0
                OUString(), uno::Reference< uno::XInterface >(),
79
0
                task::InteractionClassification_QUERY,
80
0
                (bFirstTry
81
0
                 ? task::PasswordRequestMode_PASSWORD_ENTER
82
0
                 : task::PasswordRequestMode_PASSWORD_REENTER),
83
0
                rName, false))),
84
0
    m_bSelected(false)
85
0
{}
86
87
uno::Any PDFPasswordRequest::getRequest()
88
0
{
89
0
    return m_aRequest;
90
0
}
91
92
uno::Sequence< uno::Reference< task::XInteractionContinuation > > PDFPasswordRequest::getContinuations()
93
0
{
94
0
    return { this };
95
0
}
96
97
void PDFPasswordRequest::setPassword( const OUString& rPwd )
98
0
{
99
0
    std::scoped_lock const guard( m_aMutex );
100
101
0
    m_aPassword = rPwd;
102
0
}
103
104
OUString PDFPasswordRequest::getPassword()
105
0
{
106
0
    std::scoped_lock const guard( m_aMutex );
107
108
0
    return m_aPassword;
109
0
}
110
111
void PDFPasswordRequest::select()
112
0
{
113
0
    std::scoped_lock const guard( m_aMutex );
114
115
0
    m_bSelected = true;
116
0
}
117
118
} // namespace
119
120
namespace vcl::pdf
121
{
122
123
bool getPassword( const uno::Reference< task::XInteractionHandler >& xHandler,
124
                  OUString&                                     rOutPwd,
125
                  bool                                               bFirstTry,
126
                  const OUString&                               rDocName
127
                  )
128
0
{
129
0
    bool bSuccess = false;
130
131
0
    rtl::Reference< PDFPasswordRequest > xReq(
132
0
        new PDFPasswordRequest( bFirstTry, rDocName ) );
133
0
    try
134
0
    {
135
0
        xHandler->handle( xReq );
136
0
    }
137
0
    catch( uno::Exception& )
138
0
    {
139
0
    }
140
141
0
    if( xReq->isSelected() )
142
0
    {
143
0
        bSuccess = true;
144
0
        rOutPwd = xReq->getPassword();
145
0
    }
146
147
0
    return bSuccess;
148
0
}
149
150
}
151
152
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */