OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "content/browser/payments/payment_instrument_icon_fetcher.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "content/browser/storage_partition_impl.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "content/public/common/service_manager_connection.h" | |
12 #include "net/base/load_flags.h" | |
13 #include "net/http/http_status_code.h" | |
14 #include "net/traffic_annotation/network_traffic_annotation.h" | |
15 #include "services/data_decoder/public/cpp/decode_image.h" | |
16 #include "services/service_manager/public/cpp/connector.h" | |
17 #include "ui/gfx/geometry/size.h" | |
18 #include "ui/gfx/image/image.h" | |
19 | |
20 namespace content { | |
21 | |
22 namespace { | |
23 | |
24 net::NetworkTrafficAnnotationTag g_traffic_annotation = | |
25 net::DefineNetworkTrafficAnnotation("payment_instrument_icon_fetcher", R"( | |
26 semantics { | |
27 sender: "Web Payments" | |
28 description: | |
29 "Chromium downloads payment instrument icons when registering | |
30 payment instruments." | |
31 trigger: | |
32 "When user navigates to a website to register web payment apps." | |
33 data: | |
34 "URL of the required icon to fetch. No user information is sent." | |
35 destination: WEBSITE | |
36 } | |
37 policy { | |
38 cookies_allowed: false | |
39 setting: | |
40 "This feature cannot be disabled in settings. Users can refuse | |
41 to install web payment apps." | |
42 policy_exception_justification: "Not implemented." | |
43 })"); | |
44 | |
45 } // namespace | |
46 | |
47 PaymentInstrumentIconFetcher::PaymentInstrumentIconFetcher() {} | |
please use gerrit instead
2017/06/09 14:52:53
Please initialize the checking_image_object_index_
gogerald1
2017/06/09 18:50:56
Done.
| |
48 PaymentInstrumentIconFetcher::~PaymentInstrumentIconFetcher() {} | |
49 | |
50 void PaymentInstrumentIconFetcher::start( | |
51 const GURL& scope, | |
52 const std::vector<payments::mojom::ImageObjectPtr>& image_objects, | |
53 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, | |
54 PaymentInstrumentIconFetcherCallback callback) { | |
55 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
56 | |
57 scope_ = scope; | |
58 DCHECK(scope_.is_valid()); | |
59 | |
60 for (const auto& obj : image_objects) { | |
61 payments::mojom::ImageObjectPtr image_object = | |
62 payments::mojom::ImageObject::New(); | |
63 image_object->src = obj->src; | |
64 image_object->sizes = obj->sizes; | |
65 image_object->type = obj->type; | |
66 image_objects_.emplace_back(std::move(image_object)); | |
67 } | |
68 DCHECK_GT(image_objects_.size(), 0U); | |
69 | |
70 callback_ = std::move(callback); | |
71 | |
72 BrowserThread::PostTask( | |
73 BrowserThread::UI, FROM_HERE, | |
74 base::Bind(&PaymentInstrumentIconFetcher::StartFromUIThread, | |
75 base::Unretained(this), service_worker_context)); | |
76 } | |
77 | |
78 void PaymentInstrumentIconFetcher::StartFromUIThread( | |
79 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { | |
80 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
81 | |
82 url_request_context_getter_ = scoped_refptr<net::URLRequestContextGetter>( | |
83 service_worker_context->storage_partition()->GetURLRequestContext()); | |
84 if (url_request_context_getter_.get() == nullptr) { | |
85 PostCallbackToIOThread(""); | |
86 return; | |
87 } | |
88 | |
89 checking_image_object_index_ = 0; | |
90 FetchIcon(); | |
91 } | |
92 | |
93 void PaymentInstrumentIconFetcher::FetchIcon() { | |
94 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
95 | |
96 if (checking_image_object_index_ >= image_objects_.size()) { | |
97 PostCallbackToIOThread(""); | |
98 return; | |
99 } | |
100 | |
101 GURL icon_src_url(image_objects_[checking_image_object_index_]->src); | |
102 if (!icon_src_url.is_valid()) { | |
103 icon_src_url = | |
104 scope_.Resolve(image_objects_[checking_image_object_index_]->src); | |
105 } | |
106 | |
107 if (!icon_src_url.is_valid()) { | |
108 checking_image_object_index_++; | |
109 FetchIcon(); | |
110 return; | |
111 } | |
112 | |
113 fetcher_ = net::URLFetcher::Create(icon_src_url, net::URLFetcher::GET, this, | |
114 g_traffic_annotation); | |
115 fetcher_->SetRequestContext(url_request_context_getter_.get()); | |
116 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
117 net::LOAD_DO_NOT_SAVE_COOKIES); | |
118 fetcher_->SetStopOnRedirect(true); | |
119 fetcher_->Start(); | |
120 } | |
121 | |
122 void PaymentInstrumentIconFetcher::OnURLFetchComplete( | |
123 const net::URLFetcher* source) { | |
124 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
125 | |
126 DCHECK_EQ(fetcher_.get(), source); | |
127 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); | |
128 | |
129 std::string data; | |
130 if (!(source->GetStatus().is_success() && | |
131 source->GetResponseCode() == net::HTTP_OK && | |
132 source->GetResponseAsString(&data))) { | |
133 checking_image_object_index_++; | |
134 FetchIcon(); | |
135 return; | |
136 } | |
137 | |
138 service_manager::mojom::ConnectorRequest connector_request; | |
139 std::unique_ptr<service_manager::Connector> connector = | |
140 service_manager::Connector::Create(&connector_request); | |
141 content::ServiceManagerConnection::GetForProcess() | |
142 ->GetConnector() | |
143 ->BindConnectorRequest(std::move(connector_request)); | |
144 | |
145 std::vector<uint8_t> image_data(data.begin(), data.end()); | |
146 data_decoder::DecodeImage( | |
147 connector.get(), image_data, data_decoder::mojom::ImageCodec::DEFAULT, | |
148 false, data_decoder::kDefaultMaxSizeInBytes, gfx::Size(), | |
149 base::Bind(&PaymentInstrumentIconFetcher::DecodeImageCallback, | |
150 base::Unretained(this))); | |
151 } | |
152 | |
153 void PaymentInstrumentIconFetcher::DecodeImageCallback(const SkBitmap& bitmap) { | |
154 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
155 | |
156 if (bitmap.isNull() || bitmap.empty()) { | |
157 checking_image_object_index_++; | |
158 FetchIcon(); | |
159 return; | |
160 } | |
161 | |
162 gfx::Image decoded_image = gfx::Image::CreateFrom1xBitmap(bitmap); | |
163 scoped_refptr<base::RefCountedMemory> raw_data = decoded_image.As1xPNGBytes(); | |
164 std::string base_64; | |
165 base::Base64Encode( | |
166 base::StringPiece(raw_data->front_as<char>(), raw_data->size()), | |
167 &base_64); | |
168 | |
169 PostCallbackToIOThread(base_64); | |
170 } | |
171 | |
172 void PaymentInstrumentIconFetcher::PostCallbackToIOThread( | |
173 const std::string& decoded_data) { | |
174 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
175 base::BindOnce(std::move(callback_), decoded_data)); | |
176 } | |
177 | |
178 } // namespace content | |
OLD | NEW |