Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(801)

Side by Side Diff: components/dom_distiller/core/distiller.cc

Issue 178303004: Add incremental updates for multipage distillation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile by adding a header. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/dom_distiller/core/distiller.h" 5 #include "components/dom_distiller/core/distiller.h"
6 6
7 #include <map> 7 #include <map>
8 #include <vector>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/callback.h" 11 #include "base/callback.h"
11 #include "base/location.h" 12 #include "base/location.h"
12 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h" 16 #include "base/values.h"
16 #include "components/dom_distiller/core/distiller_page.h" 17 #include "components/dom_distiller/core/distiller_page.h"
17 #include "components/dom_distiller/core/distiller_url_fetcher.h" 18 #include "components/dom_distiller/core/distiller_url_fetcher.h"
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 90
90 DistillerImpl::DistilledPageData* DistillerImpl::GetPageAtIndex(size_t index) 91 DistillerImpl::DistilledPageData* DistillerImpl::GetPageAtIndex(size_t index)
91 const { 92 const {
92 DCHECK_LT(index, pages_.size()); 93 DCHECK_LT(index, pages_.size());
93 DistilledPageData* page_data = pages_[index]; 94 DistilledPageData* page_data = pages_[index];
94 DCHECK(page_data); 95 DCHECK(page_data);
95 return page_data; 96 return page_data;
96 } 97 }
97 98
98 void DistillerImpl::DistillPage(const GURL& url, 99 void DistillerImpl::DistillPage(const GURL& url,
99 const DistillerCallback& distillation_cb) { 100 const ArticleDistillationCallback& article_cb,
101 const PageDistillationCallback& page_cb) {
100 DCHECK(AreAllPagesFinished()); 102 DCHECK(AreAllPagesFinished());
101 distillation_cb_ = distillation_cb; 103 article_cb_ = article_cb;
104 page_cb_ = page_cb;
102 105
103 AddToDistillationQueue(0, url); 106 AddToDistillationQueue(0, url);
104 DistillNextPage(); 107 DistillNextPage();
105 } 108 }
106 109
107 void DistillerImpl::DistillNextPage() { 110 void DistillerImpl::DistillNextPage() {
108 if (!waiting_pages_.empty()) { 111 if (!waiting_pages_.empty()) {
109 std::map<int, GURL>::iterator front = waiting_pages_.begin(); 112 std::map<int, GURL>::iterator front = waiting_pages_.begin();
110 int page_num = front->first; 113 int page_num = front->first;
111 const GURL url = front->second; 114 const GURL url = front->second;
(...skipping 17 matching lines...) Expand all
129 void DistillerImpl::OnPageDistillationFinished( 132 void DistillerImpl::OnPageDistillationFinished(
130 int page_num, 133 int page_num,
131 const GURL& page_url, 134 const GURL& page_url,
132 scoped_ptr<DistilledPageInfo> distilled_page, 135 scoped_ptr<DistilledPageInfo> distilled_page,
133 bool distillation_successful) { 136 bool distillation_successful) {
134 DCHECK(distilled_page.get()); 137 DCHECK(distilled_page.get());
135 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); 138 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end());
136 if (distillation_successful) { 139 if (distillation_successful) {
137 DistilledPageData* page_data = 140 DistilledPageData* page_data =
138 GetPageAtIndex(started_pages_index_[page_num]); 141 GetPageAtIndex(started_pages_index_[page_num]);
139 DistilledPageProto* current_page = new DistilledPageProto(); 142 base::RefCountedData<DistilledPageProto>* current_page =
cjhopman 2014/02/28 02:38:14 This isn't used again so just create the new one w
shashi 2014/03/02 02:53:40 Done.
140 page_data->proto.reset(current_page); 143 new base::RefCountedData<DistilledPageProto>();
144 page_data->proto = current_page;
141 page_data->page_num = page_num; 145 page_data->page_num = page_num;
142 page_data->title = distilled_page->title; 146 page_data->title = distilled_page->title;
143 147
144 current_page->set_url(page_url.spec()); 148 page_data->proto->data.set_url(page_url.spec());
145 current_page->set_html(distilled_page->html); 149 page_data->proto->data.set_html(distilled_page->html);
146 150
147 GURL next_page_url(distilled_page->next_page_url); 151 GURL next_page_url(distilled_page->next_page_url);
148 if (next_page_url.is_valid()) { 152 if (next_page_url.is_valid()) {
149 // The pages should be in same origin. 153 // The pages should be in same origin.
150 DCHECK_EQ(next_page_url.GetOrigin(), page_url.GetOrigin()); 154 DCHECK_EQ(next_page_url.GetOrigin(), page_url.GetOrigin());
151 AddToDistillationQueue(page_num + 1, next_page_url); 155 AddToDistillationQueue(page_num + 1, next_page_url);
152 } 156 }
153 157
154 GURL prev_page_url(distilled_page->prev_page_url); 158 GURL prev_page_url(distilled_page->prev_page_url);
155 if (prev_page_url.is_valid()) { 159 if (prev_page_url.is_valid()) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 std::find(page_data->image_fetchers_.begin(), 205 std::find(page_data->image_fetchers_.begin(),
202 page_data->image_fetchers_.end(), 206 page_data->image_fetchers_.end(),
203 url_fetcher); 207 url_fetcher);
204 208
205 DCHECK(fetcher_it != page_data->image_fetchers_.end()); 209 DCHECK(fetcher_it != page_data->image_fetchers_.end());
206 // Delete the |url_fetcher| by DeleteSoon since the OnFetchImageDone 210 // Delete the |url_fetcher| by DeleteSoon since the OnFetchImageDone
207 // callback is invoked by the |url_fetcher|. 211 // callback is invoked by the |url_fetcher|.
208 page_data->image_fetchers_.weak_erase(fetcher_it); 212 page_data->image_fetchers_.weak_erase(fetcher_it);
209 base::MessageLoop::current()->DeleteSoon(FROM_HERE, url_fetcher); 213 base::MessageLoop::current()->DeleteSoon(FROM_HERE, url_fetcher);
210 214
211 DistilledPageProto_Image* image = page_data->proto->add_image(); 215 DistilledPageProto_Image* image = page_data->proto->data.add_image();
212 image->set_name(id); 216 image->set_name(id);
213 image->set_data(response); 217 image->set_data(response);
214 218
215 AddPageIfDone(page_num); 219 AddPageIfDone(page_num);
216 } 220 }
217 221
218 void DistillerImpl::AddPageIfDone(int page_num) { 222 void DistillerImpl::AddPageIfDone(int page_num) {
219 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end()); 223 DCHECK(started_pages_index_.find(page_num) != started_pages_index_.end());
220 DCHECK(finished_pages_index_.find(page_num) == finished_pages_index_.end()); 224 DCHECK(finished_pages_index_.find(page_num) == finished_pages_index_.end());
221 DistilledPageData* page_data = GetPageAtIndex(started_pages_index_[page_num]); 225 DistilledPageData* page_data = GetPageAtIndex(started_pages_index_[page_num]);
222 if (page_data->image_fetchers_.empty()) { 226 if (page_data->image_fetchers_.empty()) {
223 finished_pages_index_[page_num] = started_pages_index_[page_num]; 227 finished_pages_index_[page_num] = started_pages_index_[page_num];
224 started_pages_index_.erase(page_num); 228 started_pages_index_.erase(page_num);
229 bool has_prev_page = false;
cjhopman 2014/02/28 02:38:14 I think I'd extract all this to a new method so th
shashi 2014/03/02 02:53:40 Done.
230 bool has_next_page = false;
231 if (!finished_pages_index_.empty()) {
232 int prev_page_num = finished_pages_index_.begin()->first - 1;
233 int next_page_num = finished_pages_index_.rbegin()->first + 1;
234 has_prev_page =
235 (waiting_pages_.find(prev_page_num) != waiting_pages_.end()) ||
cjhopman 2014/02/28 02:38:14 Can't we just use IsPageNumberInUse?
shashi 2014/03/02 02:53:40 We can since finished_pages is sorted :), done.
236 started_pages_index_.find(prev_page_num) !=
237 started_pages_index_.end();
238
239 has_next_page =
240 (waiting_pages_.find(next_page_num) != waiting_pages_.end()) ||
241 started_pages_index_.find(next_page_num) !=
242 started_pages_index_.end();
243 }
244
245 std::vector<ArticleDistillationUpdate::RefCountedDistilledPageProto>
246 update_pages;
247 for (std::map<int, size_t>::const_iterator it =
248 finished_pages_index_.begin();
249 it != finished_pages_index_.end();
250 ++it) {
251 update_pages.push_back(pages_[it->second]->proto);
252 }
253 ArticleDistillationUpdate article_update(
254 update_pages, has_next_page, has_prev_page);
255 DCHECK_EQ(article_update.getPagesSize(), finished_pages_index_.size());
256 page_cb_.Run(article_update);
225 RunDistillerCallbackIfDone(); 257 RunDistillerCallbackIfDone();
cjhopman 2014/02/28 02:38:14 Do we really want to call both the update and fini
shashi 2014/03/02 02:53:40 For last page, we can skip the update, I did not m
226 } 258 }
227 } 259 }
228 260
229 void DistillerImpl::RunDistillerCallbackIfDone() { 261 void DistillerImpl::RunDistillerCallbackIfDone() {
230 DCHECK(!distillation_cb_.is_null()); 262 DCHECK(!article_cb_.is_null());
231 if (AreAllPagesFinished()) { 263 if (AreAllPagesFinished()) {
232 bool first_page = true; 264 bool first_page = true;
233 scoped_ptr<DistilledArticleProto> article_proto( 265 scoped_ptr<DistilledArticleProto> article_proto(
234 new DistilledArticleProto()); 266 new DistilledArticleProto());
235 // Stitch the pages back into the article. 267 // Stitch the pages back into the article.
236 for (std::map<int, size_t>::iterator it = finished_pages_index_.begin(); 268 for (std::map<int, size_t>::iterator it = finished_pages_index_.begin();
237 it != finished_pages_index_.end();) { 269 it != finished_pages_index_.end();) {
238 DistilledPageData* page_data = GetPageAtIndex(it->second); 270 DistilledPageData* page_data = GetPageAtIndex(it->second);
239 *(article_proto->add_pages()) = *(page_data->proto); 271 *(article_proto->add_pages()) = page_data->proto.get()->data;
240 272
241 if (first_page) { 273 if (first_page) {
242 article_proto->set_title(page_data->title); 274 article_proto->set_title(page_data->title);
243 first_page = false; 275 first_page = false;
244 } 276 }
245 277
246 finished_pages_index_.erase(it++); 278 finished_pages_index_.erase(it++);
247 } 279 }
248 280
249 pages_.clear(); 281 pages_.clear();
250 DCHECK_LE(static_cast<size_t>(article_proto->pages_size()), 282 DCHECK_LE(static_cast<size_t>(article_proto->pages_size()),
251 max_pages_in_article_); 283 max_pages_in_article_);
252 284
253 DCHECK(pages_.empty()); 285 DCHECK(pages_.empty());
254 DCHECK(finished_pages_index_.empty()); 286 DCHECK(finished_pages_index_.empty());
255 distillation_cb_.Run(article_proto.Pass()); 287 article_cb_.Run(article_proto.Pass());
256 distillation_cb_.Reset(); 288 article_cb_.Reset();
257 } 289 }
258 } 290 }
259 291
260 } // namespace dom_distiller 292 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698