| OLD | NEW |
| 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/dom_distiller_service.h" | 5 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/guid.h" | 9 #include "base/guid.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "components/dom_distiller/core/distilled_content_store.h" | 14 #include "components/dom_distiller/core/distilled_content_store.h" |
| 14 #include "components/dom_distiller/core/dom_distiller_store.h" | 15 #include "components/dom_distiller/core/dom_distiller_store.h" |
| 15 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 16 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
| 16 #include "components/dom_distiller/core/task_tracker.h" | 17 #include "components/dom_distiller/core/task_tracker.h" |
| 17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 18 | 19 |
| 19 namespace dom_distiller { | 20 namespace dom_distiller { |
| 20 | 21 |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 if (*task_tracker) { | 209 if (*task_tracker) { |
| 209 return false; | 210 return false; |
| 210 } | 211 } |
| 211 | 212 |
| 212 ArticleEntry skeleton_entry = CreateSkeletonEntryForUrl(url); | 213 ArticleEntry skeleton_entry = CreateSkeletonEntryForUrl(url); |
| 213 *task_tracker = CreateTaskTracker(skeleton_entry); | 214 *task_tracker = CreateTaskTracker(skeleton_entry); |
| 214 return true; | 215 return true; |
| 215 } | 216 } |
| 216 | 217 |
| 217 TaskTracker* DomDistillerService::GetTaskTrackerForUrl(const GURL& url) const { | 218 TaskTracker* DomDistillerService::GetTaskTrackerForUrl(const GURL& url) const { |
| 218 for (TaskList::const_iterator it = tasks_.begin(); it != tasks_.end(); ++it) { | 219 for (auto it = tasks_.begin(); it != tasks_.end(); ++it) { |
| 219 if ((*it)->HasUrl(url)) { | 220 if ((*it)->HasUrl(url)) { |
| 220 return *it; | 221 return (*it).get(); |
| 221 } | 222 } |
| 222 } | 223 } |
| 223 return nullptr; | 224 return nullptr; |
| 224 } | 225 } |
| 225 | 226 |
| 226 TaskTracker* DomDistillerService::GetTaskTrackerForEntry( | 227 TaskTracker* DomDistillerService::GetTaskTrackerForEntry( |
| 227 const ArticleEntry& entry) const { | 228 const ArticleEntry& entry) const { |
| 228 const std::string& entry_id = entry.entry_id(); | 229 const std::string& entry_id = entry.entry_id(); |
| 229 for (TaskList::const_iterator it = tasks_.begin(); it != tasks_.end(); ++it) { | 230 for (auto it = tasks_.begin(); it != tasks_.end(); ++it) { |
| 230 if ((*it)->HasEntryId(entry_id)) { | 231 if ((*it)->HasEntryId(entry_id)) { |
| 231 return *it; | 232 return (*it).get(); |
| 232 } | 233 } |
| 233 } | 234 } |
| 234 return nullptr; | 235 return nullptr; |
| 235 } | 236 } |
| 236 | 237 |
| 237 bool DomDistillerService::GetOrCreateTaskTrackerForEntry( | 238 bool DomDistillerService::GetOrCreateTaskTrackerForEntry( |
| 238 const ArticleEntry& entry, | 239 const ArticleEntry& entry, |
| 239 TaskTracker** task_tracker) { | 240 TaskTracker** task_tracker) { |
| 240 *task_tracker = GetTaskTrackerForEntry(entry); | 241 *task_tracker = GetTaskTrackerForEntry(entry); |
| 241 if (!*task_tracker) { | 242 if (!*task_tracker) { |
| 242 *task_tracker = CreateTaskTracker(entry); | 243 *task_tracker = CreateTaskTracker(entry); |
| 243 return true; | 244 return true; |
| 244 } | 245 } |
| 245 return false; | 246 return false; |
| 246 } | 247 } |
| 247 | 248 |
| 248 TaskTracker* DomDistillerService::CreateTaskTracker(const ArticleEntry& entry) { | 249 TaskTracker* DomDistillerService::CreateTaskTracker(const ArticleEntry& entry) { |
| 249 TaskTracker::CancelCallback cancel_callback = | 250 TaskTracker::CancelCallback cancel_callback = |
| 250 base::Bind(&DomDistillerService::CancelTask, base::Unretained(this)); | 251 base::Bind(&DomDistillerService::CancelTask, base::Unretained(this)); |
| 251 TaskTracker* tracker = | 252 tasks_.push_back(base::MakeUnique<TaskTracker>(entry, cancel_callback, |
| 252 new TaskTracker(entry, cancel_callback, content_store_.get()); | 253 content_store_.get())); |
| 253 tasks_.push_back(tracker); | 254 return tasks_.back().get(); |
| 254 return tracker; | |
| 255 } | 255 } |
| 256 | 256 |
| 257 void DomDistillerService::CancelTask(TaskTracker* task) { | 257 void DomDistillerService::CancelTask(TaskTracker* task) { |
| 258 TaskList::iterator it = std::find(tasks_.begin(), tasks_.end(), task); | 258 auto it = std::find_if(tasks_.begin(), tasks_.end(), |
| 259 [task](const std::unique_ptr<TaskTracker>& t) { |
| 260 return task == t.get(); |
| 261 }); |
| 259 if (it != tasks_.end()) { | 262 if (it != tasks_.end()) { |
| 260 tasks_.weak_erase(it); | 263 it->release(); |
| 264 tasks_.erase(it); |
| 261 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, task); | 265 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, task); |
| 262 } | 266 } |
| 263 } | 267 } |
| 264 | 268 |
| 265 void DomDistillerService::AddDistilledPageToList( | 269 void DomDistillerService::AddDistilledPageToList( |
| 266 const ArticleEntry& entry, | 270 const ArticleEntry& entry, |
| 267 const DistilledArticleProto* article_proto, | 271 const DistilledArticleProto* article_proto, |
| 268 bool distillation_succeeded) { | 272 bool distillation_succeeded) { |
| 269 DCHECK(IsEntryValid(entry)); | 273 DCHECK(IsEntryValid(entry)); |
| 270 if (store_ && distillation_succeeded) { | 274 if (store_ && distillation_succeeded) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 287 if (store_) { | 291 if (store_) { |
| 288 store_->RemoveObserver(observer); | 292 store_->RemoveObserver(observer); |
| 289 } | 293 } |
| 290 } | 294 } |
| 291 | 295 |
| 292 DistilledPagePrefs* DomDistillerService::GetDistilledPagePrefs() { | 296 DistilledPagePrefs* DomDistillerService::GetDistilledPagePrefs() { |
| 293 return distilled_page_prefs_.get(); | 297 return distilled_page_prefs_.get(); |
| 294 } | 298 } |
| 295 | 299 |
| 296 } // namespace dom_distiller | 300 } // namespace dom_distiller |
| OLD | NEW |