| 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 #include "components/enhanced_bookmarks/bookmark_image_service.h" | 4 #include "components/enhanced_bookmarks/bookmark_image_service.h" |
| 5 | 5 |
| 6 #include "base/single_thread_task_runner.h" | 6 #include "base/single_thread_task_runner.h" |
| 7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
| 8 #include "base/threading/sequenced_worker_pool.h" | 8 #include "base/threading/sequenced_worker_pool.h" |
| 9 #include "components/bookmarks/browser/bookmark_model.h" | 9 #include "components/bookmarks/browser/bookmark_model.h" |
| 10 #include "components/bookmarks/browser/bookmark_model_observer.h" | 10 #include "components/bookmarks/browser/bookmark_model_observer.h" |
| 11 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h" |
| 11 #include "components/enhanced_bookmarks/enhanced_bookmark_utils.h" | 12 #include "components/enhanced_bookmarks/enhanced_bookmark_utils.h" |
| 12 #include "components/enhanced_bookmarks/metadata_accessor.h" | |
| 13 #include "components/enhanced_bookmarks/persistent_image_store.h" | 13 #include "components/enhanced_bookmarks/persistent_image_store.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 const char kSequenceToken[] = "BookmarkImagesSequenceToken"; | 17 const char kSequenceToken[] = "BookmarkImagesSequenceToken"; |
| 18 | 18 |
| 19 void ConstructPersistentImageStore(PersistentImageStore* store, | 19 void ConstructPersistentImageStore(PersistentImageStore* store, |
| 20 const base::FilePath& path) { | 20 const base::FilePath& path) { |
| 21 DCHECK(store); | 21 DCHECK(store); |
| 22 new (store) PersistentImageStore(path); | 22 new (store) PersistentImageStore(path); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 35 std::pair<gfx::Image, GURL> image_data = store->Get(page_url); | 35 std::pair<gfx::Image, GURL> image_data = store->Get(page_url); |
| 36 origin_loop->PostTask( | 36 origin_loop->PostTask( |
| 37 FROM_HERE, base::Bind(callback, image_data.first, image_data.second)); | 37 FROM_HERE, base::Bind(callback, image_data.first, image_data.second)); |
| 38 } | 38 } |
| 39 | 39 |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 namespace enhanced_bookmarks { | 42 namespace enhanced_bookmarks { |
| 43 BookmarkImageService::BookmarkImageService( | 43 BookmarkImageService::BookmarkImageService( |
| 44 scoped_ptr<ImageStore> store, | 44 scoped_ptr<ImageStore> store, |
| 45 BookmarkModel* bookmark_model, | 45 EnhancedBookmarkModel* enhanced_bookmark_model, |
| 46 scoped_refptr<base::SequencedWorkerPool> pool) | 46 scoped_refptr<base::SequencedWorkerPool> pool) |
| 47 : bookmark_model_(bookmark_model), store_(store.Pass()), pool_(pool) { | 47 : enhanced_bookmark_model_(enhanced_bookmark_model), |
| 48 store_(store.Pass()), |
| 49 pool_(pool) { |
| 48 DCHECK(CalledOnValidThread()); | 50 DCHECK(CalledOnValidThread()); |
| 49 bookmark_model_->AddObserver(this); | 51 enhanced_bookmark_model_->bookmark_model()->AddObserver(this); |
| 50 } | 52 } |
| 51 | 53 |
| 52 BookmarkImageService::BookmarkImageService( | 54 BookmarkImageService::BookmarkImageService( |
| 53 const base::FilePath& path, | 55 const base::FilePath& path, |
| 54 BookmarkModel* bookmark_model, | 56 EnhancedBookmarkModel* enhanced_bookmark_model, |
| 55 scoped_refptr<base::SequencedWorkerPool> pool) | 57 scoped_refptr<base::SequencedWorkerPool> pool) |
| 56 : bookmark_model_(bookmark_model), pool_(pool) { | 58 : enhanced_bookmark_model_(enhanced_bookmark_model), pool_(pool) { |
| 57 DCHECK(CalledOnValidThread()); | 59 DCHECK(CalledOnValidThread()); |
| 58 // PersistentImageStore has to be constructed in the thread it will be used, | 60 // PersistentImageStore has to be constructed in the thread it will be used, |
| 59 // so we are posting the construction to the thread. However, we first | 61 // so we are posting the construction to the thread. However, we first |
| 60 // allocate memory and keep here. The reason is that, before | 62 // allocate memory and keep here. The reason is that, before |
| 61 // PersistentImageStore construction is done, it's possible that | 63 // PersistentImageStore construction is done, it's possible that |
| 62 // another member function, that posts store_ to the thread, is called. | 64 // another member function, that posts store_ to the thread, is called. |
| 63 // Although the construction might not be finished yet, we still want to post | 65 // Although the construction might not be finished yet, we still want to post |
| 64 // the task since it's guaranteed to be constructed by the time it is used, by | 66 // the task since it's guaranteed to be constructed by the time it is used, by |
| 65 // the sequential thread task pool. | 67 // the sequential thread task pool. |
| 66 // | 68 // |
| 67 // Other alternatives: | 69 // Other alternatives: |
| 68 // - Using a lock or WaitableEvent for PersistentImageStore construction. | 70 // - Using a lock or WaitableEvent for PersistentImageStore construction. |
| 69 // But waiting on UI thread is discouraged. | 71 // But waiting on UI thread is discouraged. |
| 70 // - Posting the current BookmarkImageService instance instead of store_. | 72 // - Posting the current BookmarkImageService instance instead of store_. |
| 71 // But this will require using a weak pointer and can potentially block | 73 // But this will require using a weak pointer and can potentially block |
| 72 // destroying BookmarkImageService. | 74 // destroying BookmarkImageService. |
| 73 PersistentImageStore* store = | 75 PersistentImageStore* store = |
| 74 (PersistentImageStore*)::operator new(sizeof(PersistentImageStore)); | 76 (PersistentImageStore*)::operator new(sizeof(PersistentImageStore)); |
| 75 store_.reset(store); | 77 store_.reset(store); |
| 76 pool_->PostNamedSequencedWorkerTask( | 78 pool_->PostNamedSequencedWorkerTask( |
| 77 kSequenceToken, | 79 kSequenceToken, |
| 78 FROM_HERE, | 80 FROM_HERE, |
| 79 base::Bind(&ConstructPersistentImageStore, store, path)); | 81 base::Bind(&ConstructPersistentImageStore, store, path)); |
| 80 } | 82 } |
| 81 | 83 |
| 82 BookmarkImageService::~BookmarkImageService() { | 84 BookmarkImageService::~BookmarkImageService() { |
| 83 DCHECK(CalledOnValidThread()); | 85 DCHECK(CalledOnValidThread()); |
| 84 bookmark_model_->RemoveObserver(this); | 86 enhanced_bookmark_model_->bookmark_model()->RemoveObserver(this); |
| 85 pool_->PostNamedSequencedWorkerTask( | 87 pool_->PostNamedSequencedWorkerTask( |
| 86 kSequenceToken, | 88 kSequenceToken, |
| 87 FROM_HERE, | 89 FROM_HERE, |
| 88 base::Bind(&DeleteImageStore, store_.release())); | 90 base::Bind(&DeleteImageStore, store_.release())); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void BookmarkImageService::SalientImageForUrl(const GURL& page_url, | 93 void BookmarkImageService::SalientImageForUrl(const GURL& page_url, |
| 92 Callback callback) { | 94 Callback callback) { |
| 93 DCHECK(CalledOnValidThread()); | 95 DCHECK(CalledOnValidThread()); |
| 94 SalientImageForUrl(page_url, true, callback); | 96 SalientImageForUrl(page_url, true, callback); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 111 | 113 |
| 112 void BookmarkImageService::RetrieveSalientImageForPageUrl( | 114 void BookmarkImageService::RetrieveSalientImageForPageUrl( |
| 113 const GURL& page_url) { | 115 const GURL& page_url) { |
| 114 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
| 115 if (IsPageUrlInProgress(page_url)) | 117 if (IsPageUrlInProgress(page_url)) |
| 116 return; // A request for this URL is already in progress. | 118 return; // A request for this URL is already in progress. |
| 117 | 119 |
| 118 in_progress_page_urls_.insert(page_url); | 120 in_progress_page_urls_.insert(page_url); |
| 119 | 121 |
| 120 const BookmarkNode* bookmark = | 122 const BookmarkNode* bookmark = |
| 121 bookmark_model_->GetMostRecentlyAddedUserNodeForURL(page_url); | 123 enhanced_bookmark_model_->bookmark_model() |
| 124 ->GetMostRecentlyAddedUserNodeForURL(page_url); |
| 122 GURL image_url; | 125 GURL image_url; |
| 123 if (bookmark) { | 126 if (bookmark) { |
| 124 int width; | 127 int width; |
| 125 int height; | 128 int height; |
| 126 enhanced_bookmarks::ThumbnailImageFromBookmark( | 129 enhanced_bookmark_model_->GetThumbnailImage( |
| 127 bookmark, &image_url, &width, &height); | 130 bookmark, &image_url, &width, &height); |
| 128 } | 131 } |
| 129 | 132 |
| 130 RetrieveSalientImage( | 133 RetrieveSalientImage( |
| 131 page_url, | 134 page_url, |
| 132 image_url, | 135 image_url, |
| 133 "", | 136 "", |
| 134 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | 137 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 135 false); | 138 false); |
| 136 } | 139 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 void BookmarkImageService::ProcessNewImage(const GURL& page_url, | 184 void BookmarkImageService::ProcessNewImage(const GURL& page_url, |
| 182 bool update_bookmarks, | 185 bool update_bookmarks, |
| 183 const gfx::Image& image, | 186 const gfx::Image& image, |
| 184 const GURL& image_url) { | 187 const GURL& image_url) { |
| 185 DCHECK(CalledOnValidThread()); | 188 DCHECK(CalledOnValidThread()); |
| 186 StoreImage(image, image_url, page_url); | 189 StoreImage(image, image_url, page_url); |
| 187 in_progress_page_urls_.erase(page_url); | 190 in_progress_page_urls_.erase(page_url); |
| 188 ProcessRequests(page_url, image, image_url); | 191 ProcessRequests(page_url, image, image_url); |
| 189 if (update_bookmarks && image_url.is_valid()) { | 192 if (update_bookmarks && image_url.is_valid()) { |
| 190 const BookmarkNode* bookmark = | 193 const BookmarkNode* bookmark = |
| 191 bookmark_model_->GetMostRecentlyAddedUserNodeForURL(page_url); | 194 enhanced_bookmark_model_->bookmark_model() |
| 195 ->GetMostRecentlyAddedUserNodeForURL(page_url); |
| 192 if (bookmark) { | 196 if (bookmark) { |
| 193 const gfx::Size& size = image.Size(); | 197 const gfx::Size& size = image.Size(); |
| 194 bool result = enhanced_bookmarks::SetOriginalImageForBookmark( | 198 bool result = enhanced_bookmark_model_->SetOriginalImage( |
| 195 bookmark_model_, bookmark, image_url, size.width(), size.height()); | 199 bookmark, image_url, size.width(), size.height()); |
| 196 DCHECK(result); | 200 DCHECK(result); |
| 197 } | 201 } |
| 198 } | 202 } |
| 199 } | 203 } |
| 200 | 204 |
| 201 bool BookmarkImageService::IsPageUrlInProgress(const GURL& page_url) { | 205 bool BookmarkImageService::IsPageUrlInProgress(const GURL& page_url) { |
| 202 DCHECK(CalledOnValidThread()); | 206 DCHECK(CalledOnValidThread()); |
| 203 return in_progress_page_urls_.find(page_url) != in_progress_page_urls_.end(); | 207 return in_progress_page_urls_.find(page_url) != in_progress_page_urls_.end(); |
| 204 } | 208 } |
| 205 | 209 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 const BookmarkNode* node) { | 335 const BookmarkNode* node) { |
| 332 } | 336 } |
| 333 | 337 |
| 334 void BookmarkImageService::BookmarkAllUserNodesRemoved( | 338 void BookmarkImageService::BookmarkAllUserNodesRemoved( |
| 335 BookmarkModel* model, | 339 BookmarkModel* model, |
| 336 const std::set<GURL>& removed_urls) { | 340 const std::set<GURL>& removed_urls) { |
| 337 ClearAll(); | 341 ClearAll(); |
| 338 } | 342 } |
| 339 | 343 |
| 340 } // namespace enhanced_bookmarks | 344 } // namespace enhanced_bookmarks |
| OLD | NEW |