summaryrefslogtreecommitdiff
path: root/chromium/components/payments/content/payment_credential.cc
blob: 89671e2cd170b0765ea518be98a69879efabe5ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/payments/content/payment_credential.h"

#include <algorithm>
#include <memory>

#include "base/memory/ref_counted_memory.h"
#include "base/strings/utf_string_conversions.h"
#include "components/payments/content/payment_manifest_web_data_service.h"
#include "components/payments/core/secure_payment_confirmation_instrument.h"
#include "components/payments/core/url_util.h"
#include "content/public/browser/web_contents.h"
#include "ui/gfx/image/image.h"

namespace payments {

PaymentCredential::PaymentCredential(
    content::WebContents* web_contents,
    content::GlobalFrameRoutingId initiator_frame_routing_id,
    scoped_refptr<PaymentManifestWebDataService> web_data_service,
    mojo::PendingReceiver<mojom::PaymentCredential> receiver)
    : WebContentsObserver(web_contents),
      initiator_frame_routing_id_(initiator_frame_routing_id),
      web_data_service_(web_data_service) {
  DCHECK(web_contents);
  receiver_.Bind(std::move(receiver));
}

PaymentCredential::~PaymentCredential() {
  if (web_data_service_) {
    std::for_each(callbacks_.begin(), callbacks_.end(), [&](const auto& pair) {
      web_data_service_->CancelRequest(pair.first);
    });
  }
}

void PaymentCredential::StorePaymentCredential(
    payments::mojom::PaymentCredentialInstrumentPtr instrument,
    const std::vector<uint8_t>& credential_id,
    const std::string& rp_id,
    StorePaymentCredentialCallback callback) {
  if (!web_data_service_) {
    std::move(callback).Run(
        mojom::PaymentCredentialCreationStatus::FAILED_TO_STORE_INSTRUMENT);
    return;
  }

  if (!web_contents() ||
      !UrlUtil::IsOriginAllowedToUseWebPaymentApis(instrument->icon)) {
    std::move(callback).Run(
        mojom::PaymentCredentialCreationStatus::FAILED_TO_DOWNLOAD_ICON);
    return;
  }

  // If the initiator frame doesn't exist any more, e.g. the frame has
  // navigated away, don't download the icon.
  content::RenderFrameHost* render_frame_host =
      content::RenderFrameHost::FromID(initiator_frame_routing_id_);
  if (!render_frame_host || !render_frame_host->IsCurrent()) {
    std::move(callback).Run(
        mojom::PaymentCredentialCreationStatus::FAILED_TO_DOWNLOAD_ICON);
    return;
  }

  const GURL icon_url = instrument->icon;
  int request_id = web_contents()->DownloadImageInFrame(
      initiator_frame_routing_id_,
      icon_url,  // source URL
      true,      // is_favicon
      0,         // no preferred size
      0,         // no max size
      false,     // normal cache policy (a.k.a. do not bypass cache)
      base::BindOnce(&PaymentCredential::DidDownloadFavicon,
                     weak_ptr_factory_.GetWeakPtr(), std::move(instrument),
                     credential_id, rp_id, std::move(callback)));
  pending_icon_download_request_ids_.insert(request_id);
}

void PaymentCredential::DidDownloadFavicon(
    payments::mojom::PaymentCredentialInstrumentPtr instrument,
    const std::vector<uint8_t>& credential_id,
    const std::string& rp_id,
    StorePaymentCredentialCallback callback,
    int request_id,
    int unused_http_status_code,
    const GURL& image_url,
    const std::vector<SkBitmap>& bitmaps,
    const std::vector<gfx::Size>& unused_sizes) {
  auto iterator = pending_icon_download_request_ids_.find(request_id);
  DCHECK(iterator != pending_icon_download_request_ids_.end());
  pending_icon_download_request_ids_.erase(iterator);

  if (bitmaps.empty()) {
    std::move(callback).Run(
        mojom::PaymentCredentialCreationStatus::FAILED_TO_DOWNLOAD_ICON);
    return;
  }

  // TODO(https://crbug.com/1110320): Get the best icon using |preferred size|
  // rather than the first one if multiple downloaded.
  gfx::Image downloaded_image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]);
  scoped_refptr<base::RefCountedMemory> raw_data =
      downloaded_image.As1xPNGBytes();
  auto encoded_icon =
      std::vector<uint8_t>(raw_data->front_as<uint8_t>(),
                           raw_data->front_as<uint8_t>() + raw_data->size());

  WebDataServiceBase::Handle handle =
      web_data_service_->AddSecurePaymentConfirmationInstrument(
          std::make_unique<SecurePaymentConfirmationInstrument>(
              credential_id, rp_id, base::UTF8ToUTF16(instrument->display_name),
              std::move(encoded_icon)),
          /*consumer=*/this);
  callbacks_[handle] = std::move(callback);
}

void PaymentCredential::OnWebDataServiceRequestDone(
    WebDataServiceBase::Handle h,
    std::unique_ptr<WDTypedResult> result) {
  auto iterator = callbacks_.find(h);
  if (iterator == callbacks_.end())
    return;

  auto callback = std::move(iterator->second);
  DCHECK(callback);
  callbacks_.erase(iterator);

  std::move(callback).Run(
      static_cast<WDResult<bool>*>(result.get())->GetValue()
          ? mojom::PaymentCredentialCreationStatus::SUCCESS
          : mojom::PaymentCredentialCreationStatus::FAILED_TO_STORE_INSTRUMENT);
}

}  // namespace payments