VirtualBox

Changeset 83632 in vbox for trunk


Ignore:
Timestamp:
Apr 8, 2020 11:04:54 PM (5 years ago)
Author:
vboxsync
Message:

SharedClipboard: Modified queryNewPasteboardFormats to make absolutely sure we don't mistake the guest's pasteboard content for someone else's (shhouldn't happen, but I'm paranoid). bugref:9620

Location:
trunk/src/VBox/HostServices/SharedClipboard
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-darwin.cpp

    r83628 r83632  
    5050    /** Random 64-bit number embedded into szGuestOwnershipFlavor. */
    5151    uint64_t                idGuestOwnership;
     52    /** Ownership flavor CFStringRef returned by takePasteboardOwnership().
     53     * This is the same a szGuestOwnershipFlavor only in core foundation terms. */
     54    void                   *hStrOwnershipFlavor;
    5255    /** The guest ownership flavor (type) string. */
    5356    char                    szGuestOwnershipFlavor[64];
     
    6770 * @returns IPRT status code (ignored).
    6871 * @param   pCtx    The context.
     72 *
     73 * @note    Call must own lock.
    6974 */
    7075static int vboxClipboardChanged(SHCLCONTEXT *pCtx)
     
    7378        return VINF_SUCCESS;
    7479
     80    /* Retrieve the formats currently in the clipboard and supported by vbox */
    7581    uint32_t fFormats = 0;
    76     bool fChanged = false;
    77     /* Retrieve the formats currently in the clipboard and supported by vbox */
    78     int rc = queryNewPasteboardFormats(pCtx->hPasteboard, &fFormats, &fChanged);
     82    bool     fChanged = false;
     83    int rc = queryNewPasteboardFormats(pCtx->hPasteboard, pCtx->idGuestOwnership, pCtx->hStrOwnershipFlavor,
     84                                      &fFormats, &fChanged);
    7985    if (   RT_SUCCESS(rc)
    8086        && fChanged)
     
    197203    LogFlowFunc(("fFormats=%02X\n", fFormats));
    198204
     205    /** @todo r=bird: BUGBUG: The following is probably a mistake. */
    199206    if (fFormats == VBOX_SHCL_FMT_NONE)
    200207    {
     
    226233    RTStrPrintf(szValue, sizeof(szValue), "%#x", fFormats);
    227234
    228     takePasteboardOwnership(pCtx->hPasteboard, pCtx->idGuestOwnership, pCtx->szGuestOwnershipFlavor, szValue);
     235    takePasteboardOwnership(pCtx->hPasteboard, pCtx->idGuestOwnership, pCtx->szGuestOwnershipFlavor, szValue,
     236                            &pCtx->hStrOwnershipFlavor);
    229237
    230238    ShClSvcUnlock();
  • trunk/src/VBox/HostServices/SharedClipboard/darwin-pasteboard.cpp

    r83631 r83632  
    7575 * that is supported by vbox and return it.
    7676 *
    77  * @param   pPasteboard    Reference to the global pasteboard.
    78  * @param   pfFormats      Pointer for the bit combination of the
    79  *                         supported types.
    80  * @param   pfChanged      True if something has changed after the
    81  *                         last call.
    82  *
    83  * @returns IPRT status code. (Always VINF_SUCCESS atm.)
    84  */
    85 int queryNewPasteboardFormats(PasteboardRef pPasteboard, uint32_t *pfFormats, bool *pfChanged)
     77 * @param   hPasteboard         Reference to the global pasteboard.
     78 * @param   idOwnership         Our ownership ID.
     79 * @param   hStrOwnershipFlavor The ownership flavor string reference returned
     80 *                              by takePasteboardOwnership().
     81 * @param   pfFormats           Pointer for the bit combination of the
     82 *                              supported types.
     83 * @param   pfChanged           True if something has changed after the
     84 *                              last call.
     85 *
     86 * @returns VINF_SUCCESS.
     87 */
     88int queryNewPasteboardFormats(PasteboardRef hPasteboard, uint64_t idOwnership, void *hStrOwnershipFlavor,
     89                              uint32_t *pfFormats, bool *pfChanged)
    8690{
    87     OSStatus err = noErr;
     91    OSStatus orc;
     92
    8893    *pfFormats = 0;
    8994    *pfChanged = true;
     
    9196    PasteboardSyncFlags syncFlags;
    9297    /* Make sure all is in sync */
    93     syncFlags = PasteboardSynchronize(pPasteboard);
     98    syncFlags = PasteboardSynchronize(hPasteboard);
    9499    /* If nothing changed return */
    95100    if (!(syncFlags & kPasteboardModified))
     
    101106
    102107    /* Are some items in the pasteboard? */
    103     ItemCount itemCount;
    104     err = PasteboardGetItemCount(pPasteboard, &itemCount);
    105     if (itemCount < 1)
    106     {
    107         Log(("queryNewPasteboardFormats: changed: No items on the pasteboard\n"));
    108         return VINF_SUCCESS;
    109     }
    110 
    111     /* The id of the first element in the pasteboard */
    112     int rc = VINF_SUCCESS;
    113     PasteboardItemID itemID;
    114     if (!(err = PasteboardGetItemIdentifier(pPasteboard, 1, &itemID)))
    115     {
    116         /* Retrieve all flavors in the pasteboard, maybe there
    117          * is something we can use. */
    118         CFArrayRef flavorTypeArray;
    119         if (!(err = PasteboardCopyItemFlavors(pPasteboard, itemID, &flavorTypeArray)))
    120         {
    121             CFIndex flavorCount;
    122             flavorCount = CFArrayGetCount(flavorTypeArray);
    123             for (CFIndex flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++)
    124             {
    125                 CFStringRef flavorType;
    126                 flavorType = static_cast <CFStringRef>(CFArrayGetValueAtIndex(flavorTypeArray, flavorIndex));
    127                 /* Currently only unicode supported */
    128                 if (   UTTypeConformsTo(flavorType, kUTTypeUTF8PlainText)
    129                     || UTTypeConformsTo(flavorType, kUTTypeUTF16PlainText))
    130                 {
    131                     Log(("queryNewPasteboardFormats: Unicode flavor detected.\n"));
    132                     *pfFormats |= VBOX_SHCL_FMT_UNICODETEXT;
    133                 }
    134                 else if (UTTypeConformsTo(flavorType, kUTTypeBMP))
    135                 {
    136                     Log(("queryNewPasteboardFormats: BMP flavor detected.\n"));
    137                     *pfFormats |= VBOX_SHCL_FMT_BITMAP;
    138                 }
    139             }
    140             CFRelease(flavorTypeArray);
    141         }
    142     }
    143 
    144     Log(("queryNewPasteboardFormats: changed: *pfFormats=%#x\n", *pfFormats));
    145     return rc;
     108    ItemCount cItems = 0;
     109    orc = PasteboardGetItemCount(hPasteboard, &cItems);
     110    if (orc == 0)
     111    {
     112        if (cItems < 1)
     113            Log(("queryNewPasteboardFormats: changed: No items on the pasteboard\n"));
     114        else
     115        {
     116            /* The id of the first element in the pasteboard */
     117            PasteboardItemID idItem = 0;
     118            orc = PasteboardGetItemIdentifier(hPasteboard, 1, &idItem);
     119            if (orc == 0)
     120            {
     121                /*
     122                 * Retrieve all flavors on the pasteboard, maybe there
     123                 * is something we can use.  Or maybe we're the owner.
     124                 */
     125                CFArrayRef hFlavors = 0;
     126                orc = PasteboardCopyItemFlavors(hPasteboard, idItem, &hFlavors);
     127                if (orc == 0)
     128                {
     129                    CFIndex cFlavors = CFArrayGetCount(hFlavors);
     130                    for (CFIndex idxFlavor = 0; idxFlavor < cFlavors; idxFlavor++)
     131                    {
     132                        CFStringRef hStrFlavor = (CFStringRef)CFArrayGetValueAtIndex(hFlavors, idxFlavor);
     133                        if (   idItem == (PasteboardItemID)idOwnership
     134                            && hStrOwnershipFlavor
     135                            && CFStringCompare(hStrFlavor, (CFStringRef)hStrOwnershipFlavor, 0) == kCFCompareEqualTo)
     136                        {
     137                            /* We made the changes ourselves. */
     138                            Log2(("queryNewPasteboardFormats: no-changed: our clipboard!\n"));
     139                            *pfChanged = false;
     140                            *pfFormats = 0;
     141                            break;
     142                        }
     143
     144                        if (UTTypeConformsTo(hStrFlavor, kUTTypeBMP))
     145                        {
     146                            Log(("queryNewPasteboardFormats: BMP flavor detected.\n"));
     147                            *pfFormats |= VBOX_SHCL_FMT_BITMAP;
     148                        }
     149                        else if (   UTTypeConformsTo(hStrFlavor, kUTTypeUTF8PlainText)
     150                                 || UTTypeConformsTo(hStrFlavor, kUTTypeUTF16PlainText))
     151                        {
     152                            Log(("queryNewPasteboardFormats: Unicode flavor detected.\n"));
     153                            *pfFormats |= VBOX_SHCL_FMT_UNICODETEXT;
     154                        }
     155#ifdef LOG_ENABLED
     156                        else if (LogIs2Enabled())
     157                        {
     158                            if (CFStringGetCharactersPtr(hStrFlavor))
     159                                Log2(("queryNewPasteboardFormats: Unknown flavor: %ls.\n", CFStringGetCharactersPtr(hStrFlavor)));
     160                            else if (CFStringGetCStringPtr(hStrFlavor, kCFStringEncodingUTF8))
     161                                Log2(("queryNewPasteboardFormats: Unknown flavor: %s.\n",
     162                                      CFStringGetCStringPtr(hStrFlavor, kCFStringEncodingUTF8)));
     163                            else
     164                                Log2(("queryNewPasteboardFormats: Unknown flavor: ???\n"));
     165                        }
     166#endif
     167                    }
     168
     169                    CFRelease(hFlavors);
     170                }
     171                else
     172                    Log(("queryNewPasteboardFormats: PasteboardCopyItemFlavors failed - %d (%#x)\n", orc, orc));
     173            }
     174            else
     175                Log(("queryNewPasteboardFormats: PasteboardGetItemIdentifier failed - %d (%#x)\n", orc, orc));
     176
     177            if (*pfChanged)
     178                Log(("queryNewPasteboardFormats: changed: *pfFormats=%#x\n", *pfFormats));
     179        }
     180    }
     181    else
     182        Log(("queryNewPasteboardFormats: PasteboardGetItemCount failed - %d (%#x)\n", orc, orc));
     183    return VINF_SUCCESS;
    146184}
    147185
     
    289327 *
    290328 * @returns VBox status code.
    291  * @param   hPasteboard         The pastboard handle (reference).
    292  * @param   idOwnership         The ownership ID to use now.
    293  * @param   pszOwnershipFlavor  The ownership indicator flavor
    294  * @param   pszOwnershipValue   The ownership value (stringified format mask).
     329 * @param   hPasteboard             The pastboard handle (reference).
     330 * @param   idOwnership             The ownership ID to use now.
     331 * @param   pszOwnershipFlavor      The ownership indicator flavor
     332 * @param   pszOwnershipValue       The ownership value (stringified format mask).
     333 * @param   phStrOwnershipFlavor    Pointer to a CFStringRef variable holding
     334 *                                  the current ownership flavor string.  This
     335 *                                  will always be released, and set again on
     336 *                                  success.
    295337 *
    296338 * @todo    Add fFormats so we can make promises about available formats at once
     
    298340 *          flavor priority.
    299341 */
    300 int takePasteboardOwnership(PasteboardRef hPasteboard, uint64_t idOwnership,
    301                             const char *pszOwnershipFlavor, const char *pszOwnershipValue)
     342int takePasteboardOwnership(PasteboardRef hPasteboard, uint64_t idOwnership, const char *pszOwnershipFlavor,
     343                            const char *pszOwnershipValue, void **phStrOwnershipFlavor)
    302344{
     345    /*
     346     * Release the old string.
     347     */
     348    if (*phStrOwnershipFlavor)
     349    {
     350        CFStringRef hOldFlavor = (CFStringRef)*phStrOwnershipFlavor;
     351        CFRelease(hOldFlavor);
     352        *phStrOwnershipFlavor = NULL;
     353    }
     354
    303355    /*
    304356     * Clear the pasteboard and take ownership over it.
     
    322374                                              hFlavor, hData, kPasteboardFlavorNoFlags);
    323375                if (orc == 0)
     376                {
     377                    *phStrOwnershipFlavor = (void *)hFlavor;
    324378                    Log(("takePasteboardOwnership: idOwnership=%RX64 flavor=%s value=%s\n",
    325379                         idOwnership, pszOwnershipFlavor, pszOwnershipValue));
     380                }
    326381                else
     382                {
    327383                    Log(("takePasteboardOwnership: PasteboardPutItemFlavor -> %d (%#x)!\n", orc, orc));
    328                 CFRelease(hFlavor);
     384                    CFRelease(hFlavor);
     385                }
    329386            }
    330387            else
     
    414471                        rc = VERR_GENERAL_FAILURE;
    415472                    }
    416                     else
    417                     {
    418                         Log(("writeToPasteboard: CFDataCreate/UTF16 failed!\n"));
    419                         rc = VERR_NO_MEMORY;
    420                     }
    421473                    CFRelease(hData);
     474                }
     475                else
     476                {
     477                    Log(("writeToPasteboard: CFDataCreate/UTF16 failed!\n"));
     478                    rc = VERR_NO_MEMORY;
    422479                }
    423480            }
  • trunk/src/VBox/HostServices/SharedClipboard/darwin-pasteboard.h

    r83630 r83632  
    2727void destroyPasteboard(PasteboardRef *pPasteboardRef);
    2828
    29 int queryNewPasteboardFormats(PasteboardRef pPasteboard, uint32_t *pfFormats, bool *pfChanged);
     29int queryNewPasteboardFormats(PasteboardRef hPasteboard, uint64_t idOwnership, void *hStrOwnershipFlavor,
     30                              uint32_t *pfFormats, bool *pfChanged);
    3031int readFromPasteboard(PasteboardRef pPasteboard, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcbActual);
    31 int takePasteboardOwnership(PasteboardRef pPasteboard, uint64_t idOwnership,
    32                             const char *pszOwnershipFlavor, const char *pszOwnershipValue);
     32int takePasteboardOwnership(PasteboardRef pPasteboard, uint64_t idOwnership, const char *pszOwnershipFlavor,
     33                            const char *pszOwnershipValue, void **phStrOwnershipFlavor);
    3334int writeToPasteboard(PasteboardRef hPasteboard, uint64_t idOwnership, void const *pv, uint32_t cb, uint32_t fFormat);
    3435
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette