|
8 | 8 | #include "base/strings/stringprintf.h"
|
9 | 9 | #include "base/strings/utf_string_conversions.h"
|
10 | 10 | #include "base/values.h"
|
| 11 | +#include "components/image_fetcher/ios/ios_image_data_fetcher_wrapper.h" |
| 12 | +#include "ios/web/public/browser_state.h" |
| 13 | +#include "ios/web/public/referrer_util.h" |
11 | 14 | #import "ios/web/public/web_state/navigation_context.h"
|
12 | 15 | #include "ios/web/public/web_thread.h"
|
| 16 | +#include "services/network/public/cpp/shared_url_loader_factory.h" |
13 | 17 |
|
14 | 18 | #if !defined(__has_feature) || !__has_feature(objc_arc)
|
15 | 19 | #error "This file requires ARC support."
|
|
20 | 24 | namespace {
|
21 | 25 | // Command prefix for injected JavaScript.
|
22 | 26 | const char kCommandPrefix[] = "imageFetch";
|
| 27 | +// Key for image_fetcher |
| 28 | +const char kImageFetcherKeyName[] = "0"; |
| 29 | + |
| 30 | +// Wrapper class for image_fetcher::IOSImageDataFetcherWrapper. ImageFetcher is |
| 31 | +// attached to web::BrowserState instead of web::WebState, because if a user |
| 32 | +// closes the tab immediately after Copy/Save image, the web::WebState will be |
| 33 | +// destroyed thus fail the download. |
| 34 | +class ImageFetcher : public image_fetcher::IOSImageDataFetcherWrapper, |
| 35 | + public base::SupportsUserData::Data { |
| 36 | + public: |
| 37 | + ~ImageFetcher() override = default; |
| 38 | + |
| 39 | + ImageFetcher( |
| 40 | + scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) |
| 41 | + : image_fetcher::IOSImageDataFetcherWrapper(url_loader_factory) {} |
| 42 | + |
| 43 | + static ImageFetcher* FromBrowserState(web::BrowserState* browser_state) { |
| 44 | + if (!browser_state->GetUserData(&kImageFetcherKeyName)) { |
| 45 | + browser_state->SetUserData( |
| 46 | + &kImageFetcherKeyName, |
| 47 | + std::make_unique<ImageFetcher>( |
| 48 | + browser_state->GetSharedURLLoaderFactory())); |
| 49 | + } |
| 50 | + return static_cast<ImageFetcher*>( |
| 51 | + browser_state->GetUserData(&kImageFetcherKeyName)); |
| 52 | + } |
| 53 | + |
| 54 | + DISALLOW_COPY_AND_ASSIGN(ImageFetcher); |
| 55 | +}; |
23 | 56 | }
|
24 | 57 |
|
25 | 58 | ImageFetchTabHelper::ImageFetchTabHelper(web::WebState* web_state)
|
|
32 | 65 | [](base::WeakPtr<ImageFetchTabHelper> ptr,
|
33 | 66 | const base::DictionaryValue& message, const GURL& page_url,
|
34 | 67 | bool has_user_gesture, bool form_in_main_frame) {
|
35 |
| - return ptr ? ptr->OnImageDataReceived(message) : true; |
| 68 | + return ptr ? ptr->OnJsMessage(message) : true; |
36 | 69 | },
|
37 | 70 | weak_ptr_factory_.GetWeakPtr()),
|
38 | 71 | kCommandPrefix);
|
|
46 | 79 | if (navigation_context->IsSameDocument()) {
|
47 | 80 | return;
|
48 | 81 | }
|
49 |
| - for (auto&& pair : callbacks_) |
| 82 | + for (auto&& pair : js_callbacks_) |
50 | 83 | std::move(pair.second).Run(nullptr);
|
51 |
| - callbacks_.clear(); |
| 84 | + js_callbacks_.clear(); |
52 | 85 | }
|
53 | 86 |
|
54 | 87 | void ImageFetchTabHelper::WebStateDestroyed(web::WebState* web_state) {
|
55 | 88 | web_state->RemoveScriptCommandCallback(kCommandPrefix);
|
56 |
| - for (auto&& pair : callbacks_) |
| 89 | + for (auto&& pair : js_callbacks_) |
57 | 90 | std::move(pair.second).Run(nullptr);
|
58 | 91 | web_state->RemoveObserver(this);
|
59 | 92 | web_state_ = nullptr;
|
60 | 93 | }
|
61 | 94 |
|
62 | 95 | void ImageFetchTabHelper::GetImageData(const GURL& url,
|
63 |
| - base::TimeDelta timeout, |
64 |
| - ImageDataCallback&& callback) { |
| 96 | + const web::Referrer& referrer, |
| 97 | + ImageDataCallback callback) { |
| 98 | + // |this| is captured into the callback of GetImageDataByJs, which will always |
| 99 | + // be invoked before the |this| is destroyed, so it's safe. |
| 100 | + GetImageDataByJs( |
| 101 | + url, base::TimeDelta::FromMilliseconds(300), |
| 102 | + base::BindOnce(&ImageFetchTabHelper::JsCallbackOfGetImageData, |
| 103 | + base::Unretained(this), url, referrer, callback)); |
| 104 | +} |
| 105 | + |
| 106 | +void ImageFetchTabHelper::JsCallbackOfGetImageData( |
| 107 | + const GURL& url, |
| 108 | + const web::Referrer& referrer, |
| 109 | + ImageDataCallback callback, |
| 110 | + const std::string* data) { |
| 111 | + if (data) { |
| 112 | + callback([NSData dataWithBytes:data->c_str() length:data->size()]); |
| 113 | + return; |
| 114 | + } |
| 115 | + ImageFetcher::FromBrowserState(web_state_->GetBrowserState()) |
| 116 | + ->FetchImageDataWebpDecoded( |
| 117 | + url, |
| 118 | + ^(NSData* data, const image_fetcher::RequestMetadata& metadata) { |
| 119 | + callback(data); |
| 120 | + }, |
| 121 | + web::ReferrerHeaderValueForNavigation(url, referrer), |
| 122 | + web::PolicyForNavigation(url, referrer), /*send_cookies=*/true); |
| 123 | +} |
| 124 | + |
| 125 | +void ImageFetchTabHelper::GetImageDataByJs(const GURL& url, |
| 126 | + base::TimeDelta timeout, |
| 127 | + JsCallback&& callback) { |
65 | 128 | ++call_id_;
|
66 |
| - DCHECK_EQ(callbacks_.count(call_id_), 0UL); |
67 |
| - callbacks_.insert({call_id_, std::move(callback)}); |
| 129 | + DCHECK_EQ(js_callbacks_.count(call_id_), 0UL); |
| 130 | + js_callbacks_.insert({call_id_, std::move(callback)}); |
68 | 131 |
|
69 | 132 | web::WebThread::PostDelayedTask(
|
70 | 133 | web::WebThread::UI, FROM_HERE,
|
71 |
| - base::BindRepeating(&ImageFetchTabHelper::OnImageDataTimeout, |
| 134 | + base::BindRepeating(&ImageFetchTabHelper::OnJsTimeout, |
72 | 135 | weak_ptr_factory_.GetWeakPtr(), call_id_),
|
73 | 136 | timeout);
|
74 | 137 |
|
|
89 | 152 | // For failure:
|
90 | 153 | // {'command': 'image.getImageData',
|
91 | 154 | // 'id': id_sent_to_gCrWeb_image_getImageData}
|
92 |
| -bool ImageFetchTabHelper::OnImageDataReceived( |
93 |
| - const base::DictionaryValue& message) { |
| 155 | +bool ImageFetchTabHelper::OnJsMessage(const base::DictionaryValue& message) { |
94 | 156 | const base::Value* id_key = message.FindKey("id");
|
95 | 157 | if (!id_key || !id_key->is_double()) {
|
96 | 158 | return false;
|
97 | 159 | }
|
98 | 160 | int id_value = static_cast<int>(id_key->GetDouble());
|
99 |
| - if (!callbacks_.count(id_value)) { |
| 161 | + if (!js_callbacks_.count(id_value)) { |
100 | 162 | return true;
|
101 | 163 | }
|
102 |
| - ImageDataCallback callback = std::move(callbacks_[id_value]); |
103 |
| - callbacks_.erase(id_value); |
| 164 | + JsCallback callback = std::move(js_callbacks_[id_value]); |
| 165 | + js_callbacks_.erase(id_value); |
104 | 166 |
|
105 | 167 | const base::Value* data = message.FindKey("data");
|
106 | 168 | std::string decoded_data;
|
|
113 | 175 | return true;
|
114 | 176 | }
|
115 | 177 |
|
116 |
| -void ImageFetchTabHelper::OnImageDataTimeout(int call_id) { |
117 |
| - if (callbacks_.count(call_id)) { |
118 |
| - ImageDataCallback callback = std::move(callbacks_[call_id]); |
119 |
| - callbacks_.erase(call_id); |
| 178 | +void ImageFetchTabHelper::OnJsTimeout(int call_id) { |
| 179 | + if (js_callbacks_.count(call_id)) { |
| 180 | + JsCallback callback = std::move(js_callbacks_[call_id]); |
| 181 | + js_callbacks_.erase(call_id); |
120 | 182 | std::move(callback).Run(nullptr);
|
121 | 183 | }
|
122 | 184 | }
|
0 commit comments