| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/json/json_parser.h" | 5 #include "base/json/json_parser.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 8 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
| 9 #include "chrome/browser/enhanced_bookmarks/android/bookmark_image_service_andro
id.h" | 9 #include "chrome/browser/enhanced_bookmarks/android/bookmark_image_service_andro
id.h" |
| 10 #include "chrome/browser/enhanced_bookmarks/enhanced_bookmark_model_factory.h" | 10 #include "chrome/browser/enhanced_bookmarks/enhanced_bookmark_model_factory.h" |
| 11 #include "chrome/grit/browser_resources.h" | 11 #include "chrome/grit/browser_resources.h" |
| 12 #include "chrome/renderer/chrome_isolated_world_ids.h" | 12 #include "chrome/renderer/chrome_isolated_world_ids.h" |
| 13 #include "components/bookmarks/browser/bookmark_model.h" | 13 #include "components/bookmarks/browser/bookmark_model.h" |
| 14 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h" | 14 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h" |
| 15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/navigation_entry.h" | 17 #include "content/public/browser/navigation_entry.h" |
| 18 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
| 19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/referrer.h" | 20 #include "grit/enhanced_bookmarks_resources.h" |
| 21 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 22 #include "skia/ext/image_operations.h" | 22 #include "skia/ext/image_operations.h" |
| 23 #include "ui/base/device_form_factor.h" | 23 #include "ui/base/device_form_factor.h" |
| 24 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
| 25 #include "ui/gfx/android/device_display_info.h" | 25 #include "ui/gfx/android/device_display_info.h" |
| 26 #include "ui/gfx/image/image_skia.h" | 26 #include "ui/gfx/image/image_skia.h" |
| 27 | 27 |
| 28 using content::Referrer; | 28 using content::Referrer; |
| 29 using bookmarks::BookmarkNode; | 29 using bookmarks::BookmarkNode; |
| 30 | 30 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 80 |
| 81 BitmapFetcherHandler* bitmap_fetcher_handler = | 81 BitmapFetcherHandler* bitmap_fetcher_handler = |
| 82 new BitmapFetcherHandler(this, image_url); | 82 new BitmapFetcherHandler(this, image_url); |
| 83 bitmap_fetcher_handler->Start( | 83 bitmap_fetcher_handler->Start( |
| 84 browser_context_, referrer, referrer_policy, | 84 browser_context_, referrer, referrer_policy, |
| 85 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, | 85 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, |
| 86 update_bookmark, page_url); | 86 update_bookmark, page_url); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void BookmarkImageServiceAndroid::RetrieveSalientImageFromContext( | 89 void BookmarkImageServiceAndroid::RetrieveSalientImageFromContext( |
| 90 content::RenderFrameHost* render_frame_host, | 90 content::WebContents* web_contents, |
| 91 const GURL& page_url, | 91 const GURL& page_url, |
| 92 bool update_bookmark) { | 92 bool update_bookmark) { |
| 93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 94 if (IsPageUrlInProgress(page_url)) | 94 if (IsPageUrlInProgress(page_url)) |
| 95 return; // A request for this URL is already in progress. | 95 return; // A request for this URL is already in progress. |
| 96 | 96 |
| 97 const BookmarkNode* bookmark = enhanced_bookmark_model_->bookmark_model() | 97 const BookmarkNode* bookmark = enhanced_bookmark_model_->bookmark_model() |
| 98 ->GetMostRecentlyAddedUserNodeForURL(page_url); | 98 ->GetMostRecentlyAddedUserNodeForURL(page_url); |
| 99 if (!bookmark) | 99 if (!bookmark) |
| 100 return; | 100 return; |
| 101 | 101 |
| 102 // Stop the image extraction if there is already an image present. | 102 // Stop the image extraction if there is already an image present. |
| 103 GURL url; | 103 GURL url; |
| 104 int height, width; | 104 int height, width; |
| 105 if (enhanced_bookmark_model_->GetOriginalImage(bookmark, &url, &width, | 105 if (enhanced_bookmark_model_->GetOriginalImage(bookmark, &url, &width, |
| 106 &height) || | 106 &height) || |
| 107 enhanced_bookmark_model_->GetThumbnailImage(bookmark, &url, &width, | 107 enhanced_bookmark_model_->GetThumbnailImage(bookmark, &url, &width, |
| 108 &height)) { | 108 &height)) { |
| 109 return; | 109 return; |
| 110 } | 110 } |
| 111 | 111 |
| 112 if (script_.empty()) { | 112 if (dom_initializer_script_.empty()) { |
| 113 script_ = | 113 dom_initializer_script_ = |
| 114 base::UTF8ToUTF16(ResourceBundle::GetSharedInstance() | 114 base::UTF8ToUTF16( |
| 115 .GetRawDataResource(IDR_GET_SALIENT_IMAGE_URL_JS) | 115 ResourceBundle::GetSharedInstance() |
| 116 .as_string()); | 116 .GetRawDataResource(IDR_DOM_INITIALIZER_GEN_JS) |
| 117 .as_string()); |
| 117 } | 118 } |
| 118 | 119 |
| 120 blink::WebReferrerPolicy policy = |
| 121 web_contents->GetController().GetVisibleEntry()->GetReferrer().policy; |
| 122 |
| 123 content::RenderFrameHost* render_frame_host = web_contents->GetMainFrame(); |
| 124 |
| 119 render_frame_host->ExecuteJavaScriptInIsolatedWorld( | 125 render_frame_host->ExecuteJavaScriptInIsolatedWorld( |
| 120 script_, | 126 dom_initializer_script_, |
| 121 base::Bind( | 127 base::Bind( |
| 122 &BookmarkImageServiceAndroid::RetrieveSalientImageFromContextCallback, | 128 &BookmarkImageServiceAndroid::InitializeDomCallback, |
| 123 base::Unretained(this), page_url, update_bookmark), | 129 base::Unretained(this), policy, render_frame_host, page_url, |
| 130 update_bookmark), |
| 124 chrome::ISOLATED_WORLD_ID_CHROME_INTERNAL); | 131 chrome::ISOLATED_WORLD_ID_CHROME_INTERNAL); |
| 125 } | 132 } |
| 126 | 133 |
| 127 void BookmarkImageServiceAndroid::FinishSuccessfulPageLoadForTab( | 134 void BookmarkImageServiceAndroid::FinishSuccessfulPageLoadForTab( |
| 128 content::WebContents* web_contents, bool update_bookmark) { | 135 content::WebContents* web_contents, bool update_bookmark) { |
| 129 content::NavigationEntry* entry = | 136 content::NavigationEntry* entry = |
| 130 web_contents->GetController().GetVisibleEntry(); | 137 web_contents->GetController().GetVisibleEntry(); |
| 131 | 138 |
| 132 // If the navigation is a simple back or forward, do not extract images, those | 139 // If the navigation is a simple back or forward, do not extract images, those |
| 133 // were extracted already. | 140 // were extracted already. |
| 134 if (!entry || (entry->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK)) | 141 if (!entry || (entry->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK)) |
| 135 return; | 142 return; |
| 136 const GURL& entry_url = entry->GetURL(); | 143 const GURL& entry_url = entry->GetURL(); |
| 137 const GURL& entry_original_url = entry->GetOriginalRequestURL(); | 144 const GURL& entry_original_url = entry->GetOriginalRequestURL(); |
| 138 std::vector<GURL> urls; | 145 std::vector<GURL> urls; |
| 139 urls.push_back(entry_url); | 146 urls.push_back(entry_url); |
| 140 if (entry_url != entry_original_url) | 147 if (entry_url != entry_original_url) |
| 141 urls.push_back(entry_original_url); | 148 urls.push_back(entry_original_url); |
| 142 for (GURL url : urls) { | 149 for (GURL url : urls) { |
| 143 if (enhanced_bookmark_model_->bookmark_model()->IsBookmarked(url)) { | 150 if (enhanced_bookmark_model_->bookmark_model()->IsBookmarked(url)) { |
| 144 RetrieveSalientImageFromContext(web_contents->GetMainFrame(), url, | 151 RetrieveSalientImageFromContext(web_contents, url, |
| 145 update_bookmark); | 152 update_bookmark); |
| 146 } | 153 } |
| 147 } | 154 } |
| 148 } | 155 } |
| 149 | 156 |
| 150 void BookmarkImageServiceAndroid::RetrieveSalientImageFromContextCallback( | 157 void BookmarkImageServiceAndroid::InitializeDomCallback( |
| 158 blink::WebReferrerPolicy policy, |
| 159 content::RenderFrameHost* render_frame_host, |
| 151 const GURL& page_url, | 160 const GURL& page_url, |
| 152 bool update_bookmark, | 161 bool update_bookmark, |
| 153 const base::Value* result) { | 162 const base::Value* result) { |
| 163 if (get_salient_image_url_script_.empty()) { |
| 164 get_salient_image_url_script_ = |
| 165 base::UTF8ToUTF16( |
| 166 ResourceBundle::GetSharedInstance() |
| 167 .GetRawDataResource(IDR_GET_SALIENT_IMAGE_URL_GEN_JS) |
| 168 .as_string()); |
| 169 } |
| 170 |
| 171 render_frame_host->ExecuteJavaScriptInIsolatedWorld( |
| 172 get_salient_image_url_script_, |
| 173 base::Bind( |
| 174 &BookmarkImageServiceAndroid::RetrieveSalientImageFromContextCallback, |
| 175 base::Unretained(this), policy, page_url, update_bookmark), |
| 176 chrome::ISOLATED_WORLD_ID_CHROME_INTERNAL); |
| 177 } |
| 178 |
| 179 void BookmarkImageServiceAndroid::RetrieveSalientImageFromContextCallback( |
| 180 blink::WebReferrerPolicy policy, |
| 181 const GURL& page_url, |
| 182 bool update_bookmark, |
| 183 const base::Value* result) { |
| 154 if (!result) | 184 if (!result) |
| 155 return; | 185 return; |
| 156 | 186 |
| 157 std::string json; | 187 std::string json; |
| 158 if (!result->GetAsString(&json)) { | 188 if (!result->GetAsString(&json)) { |
| 159 LOG(WARNING) | 189 LOG(WARNING) |
| 160 << "Salient image extracting script returned non-string result."; | 190 << "Salient image extracting script returned non-string result."; |
| 161 return; | 191 return; |
| 162 } | 192 } |
| 163 | 193 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 174 if (!json_data->GetAsDictionary(&dict)) { | 204 if (!json_data->GetAsDictionary(&dict)) { |
| 175 LOG(WARNING) << "JSON parse error, not a dict: " << json; | 205 LOG(WARNING) << "JSON parse error, not a dict: " << json; |
| 176 return; | 206 return; |
| 177 } | 207 } |
| 178 | 208 |
| 179 std::string referrerPolicy; | 209 std::string referrerPolicy; |
| 180 std::string image_url; | 210 std::string image_url; |
| 181 dict->GetString("referrerPolicy", &referrerPolicy); | 211 dict->GetString("referrerPolicy", &referrerPolicy); |
| 182 dict->GetString("imageUrl", &image_url); | 212 dict->GetString("imageUrl", &image_url); |
| 183 | 213 |
| 184 // The policy strings are guaranteed to be in lower-case. | |
| 185 blink::WebReferrerPolicy policy = blink::WebReferrerPolicyDefault; | |
| 186 if (referrerPolicy == "never") | |
| 187 policy = blink::WebReferrerPolicyNever; | |
| 188 if (referrerPolicy == "always") | |
| 189 policy = blink::WebReferrerPolicyAlways; | |
| 190 if (referrerPolicy == "origin") | |
| 191 policy = blink::WebReferrerPolicyOrigin; | |
| 192 | |
| 193 in_progress_page_urls_.insert(page_url); | 214 in_progress_page_urls_.insert(page_url); |
| 194 | 215 |
| 195 Referrer referrer = | 216 Referrer referrer = |
| 196 Referrer::SanitizeForRequest(GURL(image_url), Referrer(page_url, policy)); | 217 Referrer::SanitizeForRequest(GURL(image_url), Referrer(page_url, policy)); |
| 197 net::URLRequest::ReferrerPolicy referrer_policy = | 218 net::URLRequest::ReferrerPolicy referrer_policy = |
| 198 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE; | 219 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE; |
| 199 if (!referrer.url.is_empty()) { | 220 if (!referrer.url.is_empty()) { |
| 200 switch (policy) { | 221 switch (policy) { |
| 201 case blink::WebReferrerPolicyDefault: | 222 case blink::WebReferrerPolicyDefault: |
| 202 break; | 223 break; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 image.reset(new gfx::Image(imageSkia)); | 290 image.reset(new gfx::Image(imageSkia)); |
| 270 } else { | 291 } else { |
| 271 image.reset(new gfx::Image()); | 292 image.reset(new gfx::Image()); |
| 272 } | 293 } |
| 273 service_->ProcessNewImage(page_url_, update_bookmark_, url, image.Pass()); | 294 service_->ProcessNewImage(page_url_, update_bookmark_, url, image.Pass()); |
| 274 | 295 |
| 275 delete this; | 296 delete this; |
| 276 } | 297 } |
| 277 | 298 |
| 278 } // namespace enhanced_bookmarks | 299 } // namespace enhanced_bookmarks |
| OLD | NEW |