1 // Copyright 2014 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.
5 #include "base/json/json_parser.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "base/values.h"
8 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
9 #include "chrome/browser/enhanced_bookmarks/android/bookmark_image_service_android.h"
10 #include "chrome/browser/enhanced_bookmarks/enhanced_bookmark_model_factory.h"
11 #include "chrome/grit/browser_resources.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/referrer.h"
20 #include "net/base/load_flags.h"
21 #include "skia/ext/image_operations.h"
22 #include "ui/base/device_form_factor.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/gfx/android/device_display_info.h"
25 #include "ui/gfx/image/image_skia.h"
27 using content::Referrer
;
28 using bookmarks::BookmarkNode
;
30 namespace enhanced_bookmarks
{
32 BookmarkImageServiceAndroid::BookmarkImageServiceAndroid(
33 content::BrowserContext
* browserContext
)
34 : BookmarkImageService(
35 browserContext
->GetPath(),
36 EnhancedBookmarkModelFactory::GetForBrowserContext(browserContext
),
37 make_scoped_refptr(content::BrowserThread::GetBlockingPool())),
38 browser_context_(browserContext
) {
39 // The images we're saving will be used locally. So it's wasteful to store
40 // images larger than the device resolution.
41 gfx::DeviceDisplayInfo display_info
;
42 int max_length
= std::min(display_info
.GetPhysicalDisplayWidth(),
43 display_info
.GetPhysicalDisplayHeight());
44 // GetPhysicalDisplay*() returns 0 for pre-JB MR1. If so, fall back to the
45 // second best option we have.
46 if (max_length
== 0) {
47 max_length
= std::min(display_info
.GetDisplayWidth(),
48 display_info
.GetDisplayHeight());
51 if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET
) {
52 max_length
= max_length
/ 2;
53 } else if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_PHONE
) {
54 max_length
= max_length
* 3 / 4;
57 DCHECK(max_length
> 0);
58 max_size_
.set_height(max_length
);
59 max_size_
.set_width(max_length
);
62 BookmarkImageServiceAndroid::~BookmarkImageServiceAndroid() {
65 void BookmarkImageServiceAndroid::RetrieveSalientImage(
67 const GURL
& image_url
,
68 const std::string
& referrer
,
69 net::URLRequest::ReferrerPolicy referrer_policy
,
70 bool update_bookmark
) {
71 const BookmarkNode
* bookmark
=
72 enhanced_bookmark_model_
->bookmark_model()
73 ->GetMostRecentlyAddedUserNodeForURL(page_url
);
74 if (!bookmark
|| !image_url
.is_valid()) {
75 ProcessNewImage(page_url
, update_bookmark
, image_url
,
76 scoped_ptr
<gfx::Image
>(new gfx::Image()));
80 BitmapFetcherHandler
* bitmap_fetcher_handler
=
81 new BitmapFetcherHandler(this, image_url
);
82 bitmap_fetcher_handler
->Start(
83 browser_context_
, referrer
, referrer_policy
,
84 net::LOAD_DO_NOT_SEND_COOKIES
| net::LOAD_DO_NOT_SAVE_COOKIES
,
85 update_bookmark
, page_url
);
88 void BookmarkImageServiceAndroid::RetrieveSalientImageFromContext(
89 content::RenderFrameHost
* render_frame_host
,
91 bool update_bookmark
) {
92 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
93 if (IsPageUrlInProgress(page_url
))
94 return; // A request for this URL is already in progress.
96 const BookmarkNode
* bookmark
= enhanced_bookmark_model_
->bookmark_model()
97 ->GetMostRecentlyAddedUserNodeForURL(page_url
);
101 // Stop the image extraction if there is already an image present.
104 if (enhanced_bookmark_model_
->GetOriginalImage(bookmark
, &url
, &width
,
106 enhanced_bookmark_model_
->GetThumbnailImage(bookmark
, &url
, &width
,
111 if (script_
.empty()) {
113 base::UTF8ToUTF16(ResourceBundle::GetSharedInstance()
114 .GetRawDataResource(IDR_GET_SALIENT_IMAGE_URL_JS
)
118 render_frame_host
->ExecuteJavaScript(
121 &BookmarkImageServiceAndroid::RetrieveSalientImageFromContextCallback
,
122 base::Unretained(this), page_url
, update_bookmark
));
125 void BookmarkImageServiceAndroid::FinishSuccessfulPageLoadForTab(
126 content::WebContents
* web_contents
, bool update_bookmark
) {
127 content::NavigationEntry
* entry
=
128 web_contents
->GetController().GetVisibleEntry();
130 // If the navigation is a simple back or forward, do not extract images, those
131 // were extracted already.
132 if (!entry
|| (entry
->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK
))
134 const GURL
& entry_url
= entry
->GetURL();
135 const GURL
& entry_original_url
= entry
->GetOriginalRequestURL();
136 std::vector
<GURL
> urls
;
137 urls
.push_back(entry_url
);
138 if (entry_url
!= entry_original_url
)
139 urls
.push_back(entry_original_url
);
140 for (GURL url
: urls
) {
141 if (enhanced_bookmark_model_
->bookmark_model()->IsBookmarked(url
)) {
142 RetrieveSalientImageFromContext(web_contents
->GetMainFrame(), url
,
148 void BookmarkImageServiceAndroid::RetrieveSalientImageFromContextCallback(
149 const GURL
& page_url
,
150 bool update_bookmark
,
151 const base::Value
* result
) {
156 if (!result
->GetAsString(&json
)) {
158 << "Salient image extracting script returned non-string result.";
162 scoped_ptr
<base::Value
> json_data
;
164 std::string errorMessage
;
165 json_data
.reset(base::JSONReader::ReadAndReturnError(
166 json
, base::JSON_PARSE_RFC
, &error_code
, &errorMessage
));
167 if (error_code
|| !json_data
) {
168 LOG(WARNING
) << "JSON parse error: " << errorMessage
.c_str() << json
;
172 base::DictionaryValue
* dict
;
173 if (!json_data
->GetAsDictionary(&dict
)) {
174 LOG(WARNING
) << "JSON parse error, not a dict: " << json
;
178 std::string referrerPolicy
;
179 std::string image_url
;
180 dict
->GetString("referrerPolicy", &referrerPolicy
);
181 dict
->GetString("imageUrl", &image_url
);
183 // The policy strings are guaranteed to be in lower-case.
184 blink::WebReferrerPolicy policy
= blink::WebReferrerPolicyDefault
;
185 if (referrerPolicy
== "never")
186 policy
= blink::WebReferrerPolicyNever
;
187 if (referrerPolicy
== "always")
188 policy
= blink::WebReferrerPolicyAlways
;
189 if (referrerPolicy
== "origin")
190 policy
= blink::WebReferrerPolicyOrigin
;
192 in_progress_page_urls_
.insert(page_url
);
195 Referrer::SanitizeForRequest(GURL(image_url
), Referrer(page_url
, policy
));
196 net::URLRequest::ReferrerPolicy referrer_policy
=
197 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
;
198 if (!referrer
.url
.is_empty()) {
200 case blink::WebReferrerPolicyDefault
:
202 case blink::WebReferrerPolicyAlways
:
203 case blink::WebReferrerPolicyNever
:
204 case blink::WebReferrerPolicyOrigin
:
205 referrer_policy
= net::URLRequest::NEVER_CLEAR_REFERRER
;
211 RetrieveSalientImage(page_url
, GURL(image_url
), referrer
.url
.spec(),
212 referrer_policy
, update_bookmark
);
215 scoped_ptr
<gfx::Image
> BookmarkImageServiceAndroid::ResizeImage(
216 const gfx::Image
& image
) {
217 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
219 scoped_ptr
<gfx::Image
> result(new gfx::Image());
221 if (image
.Width() > max_size_
.width() &&
222 image
.Height() > max_size_
.height()) {
223 float resize_ratio
= std::min(
224 ((float)max_size_
.width()) / image
.Width(),
225 ((float)max_size_
.height()) / image
.Height());
226 // +0.5f is for correct rounding. Without it, it's possible that dest_width
227 // is smaller than max_size_.Width() by one.
228 int dest_width
= static_cast<int>(resize_ratio
* image
.Width() + 0.5f
);
229 int dest_height
= static_cast<int>(
230 resize_ratio
* image
.Height() + 0.5f
);
232 *result
= gfx::Image::CreateFrom1xBitmap(
233 skia::ImageOperations::Resize(image
.AsBitmap(),
234 skia::ImageOperations::RESIZE_BEST
,
237 result
->AsImageSkia().MakeThreadSafe();
242 return result
.Pass();
245 void BookmarkImageServiceAndroid::BitmapFetcherHandler::Start(
246 content::BrowserContext
* browser_context
,
247 const std::string
& referrer
,
248 net::URLRequest::ReferrerPolicy referrer_policy
,
250 bool update_bookmark
,
251 const GURL
& page_url
) {
252 update_bookmark_
= update_bookmark
;
253 page_url_
= page_url
;
255 bitmap_fetcher_
.Start(browser_context
->GetRequestContext(), referrer
,
256 referrer_policy
, load_flags
);
259 void BookmarkImageServiceAndroid::BitmapFetcherHandler::OnFetchComplete(
261 const SkBitmap
* bitmap
) {
262 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
264 scoped_ptr
<gfx::Image
> image
;
266 gfx::ImageSkia imageSkia
= gfx::ImageSkia::CreateFrom1xBitmap(*bitmap
);
267 imageSkia
.MakeThreadSafe();
268 image
.reset(new gfx::Image(imageSkia
));
270 image
.reset(new gfx::Image());
272 service_
->ProcessNewImage(page_url_
, update_bookmark_
, url
, image
.Pass());
277 } // namespace enhanced_bookmarks