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

Side by Side Diff: components/upload_list/upload_list.cc

Issue 1830383002: Fix thread safety issues with //components/upload_list. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 years, 8 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
« no previous file with comments | « components/upload_list/upload_list.h ('k') | components/upload_list/upload_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/upload_list/upload_list.h" 5 #include "components/upload_list/upload_list.h"
6 6
7 #include <stddef.h>
8
9 #include <algorithm> 7 #include <algorithm>
10 #include <iterator> 8 #include <iterator>
9 #include <utility>
11 10
12 #include "base/bind.h" 11 #include "base/bind.h"
13 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
14 #include "base/location.h" 13 #include "base/location.h"
15 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
17 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
18 #include "base/thread_task_runner_handle.h" 17 #include "base/thread_task_runner_handle.h"
19 #include "base/threading/sequenced_worker_pool.h" 18 #include "base/threading/sequenced_worker_pool.h"
20 19
(...skipping 17 matching lines...) Expand all
38 : delegate_(delegate), 37 : delegate_(delegate),
39 upload_log_path_(upload_log_path), 38 upload_log_path_(upload_log_path),
40 worker_pool_(worker_pool) {} 39 worker_pool_(worker_pool) {}
41 40
42 UploadList::~UploadList() {} 41 UploadList::~UploadList() {}
43 42
44 void UploadList::LoadUploadListAsynchronously() { 43 void UploadList::LoadUploadListAsynchronously() {
45 DCHECK(thread_checker_.CalledOnValidThread()); 44 DCHECK(thread_checker_.CalledOnValidThread());
46 worker_pool_->PostTask( 45 worker_pool_->PostTask(
47 FROM_HERE, 46 FROM_HERE,
48 base::Bind(&UploadList::LoadUploadListAndInformDelegateOfCompletion, 47 base::Bind(&UploadList::PerformLoadAndNotifyDelegate,
49 this, base::ThreadTaskRunnerHandle::Get())); 48 this, base::ThreadTaskRunnerHandle::Get()));
50 } 49 }
51 50
52 void UploadList::ClearDelegate() { 51 void UploadList::ClearDelegate() {
53 DCHECK(thread_checker_.CalledOnValidThread()); 52 DCHECK(thread_checker_.CalledOnValidThread());
54 delegate_ = NULL; 53 delegate_ = NULL;
55 } 54 }
56 55
57 void UploadList::LoadUploadListAndInformDelegateOfCompletion( 56 void UploadList::PerformLoadAndNotifyDelegate(
58 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { 57 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
59 LoadUploadList(); 58 std::vector<UploadInfo> uploads;
59 LoadUploadList(&uploads);
60 task_runner->PostTask( 60 task_runner->PostTask(
61 FROM_HERE, 61 FROM_HERE,
62 base::Bind(&UploadList::InformDelegateOfCompletion, this)); 62 base::Bind(&UploadList::SetUploadsAndNotifyDelegate, this,
63 std::move(uploads)));
63 } 64 }
64 65
65 void UploadList::LoadUploadList() { 66 void UploadList::LoadUploadList(std::vector<UploadInfo>* uploads) {
66 if (base::PathExists(upload_log_path_)) { 67 if (base::PathExists(upload_log_path_)) {
67 std::string contents; 68 std::string contents;
68 base::ReadFileToString(upload_log_path_, &contents); 69 base::ReadFileToString(upload_log_path_, &contents);
69 std::vector<std::string> log_entries = base::SplitString( 70 std::vector<std::string> log_entries = base::SplitString(
70 contents, base::kWhitespaceASCII, base::KEEP_WHITESPACE, 71 contents, base::kWhitespaceASCII, base::KEEP_WHITESPACE,
71 base::SPLIT_WANT_NONEMPTY); 72 base::SPLIT_WANT_NONEMPTY);
72 ClearUploads(); 73 ParseLogEntries(log_entries, uploads);
73 ParseLogEntries(log_entries);
74 } 74 }
75 } 75 }
76 76
77 void UploadList::AppendUploadInfo(const UploadInfo& info) {
78 uploads_.push_back(info);
79 }
80
81 void UploadList::ClearUploads() {
82 uploads_.clear();
83 }
84
85 void UploadList::ParseLogEntries( 77 void UploadList::ParseLogEntries(
86 const std::vector<std::string>& log_entries) { 78 const std::vector<std::string>& log_entries,
79 std::vector<UploadInfo>* uploads) {
87 std::vector<std::string>::const_reverse_iterator i; 80 std::vector<std::string>::const_reverse_iterator i;
88 for (i = log_entries.rbegin(); i != log_entries.rend(); ++i) { 81 for (i = log_entries.rbegin(); i != log_entries.rend(); ++i) {
89 std::vector<std::string> components = base::SplitString( 82 std::vector<std::string> components = base::SplitString(
90 *i, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 83 *i, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
91 // Skip any blank (or corrupted) lines. 84 // Skip any blank (or corrupted) lines.
92 if (components.size() < 2 || components.size() > 4) 85 if (components.size() < 2 || components.size() > 4)
93 continue; 86 continue;
94 base::Time upload_time; 87 base::Time upload_time;
95 double seconds_since_epoch; 88 double seconds_since_epoch;
96 if (!components[0].empty()) { 89 if (!components[0].empty()) {
97 if (!base::StringToDouble(components[0], &seconds_since_epoch)) 90 if (!base::StringToDouble(components[0], &seconds_since_epoch))
98 continue; 91 continue;
99 upload_time = base::Time::FromDoubleT(seconds_since_epoch); 92 upload_time = base::Time::FromDoubleT(seconds_since_epoch);
100 } 93 }
101 UploadInfo info(components[1], upload_time); 94 UploadInfo info(components[1], upload_time);
102 95
103 // Add local ID if present. 96 // Add local ID if present.
104 if (components.size() > 2) 97 if (components.size() > 2)
105 info.local_id = components[2]; 98 info.local_id = components[2];
106 99
107 // Add capture time if present. 100 // Add capture time if present.
108 if (components.size() > 3 && 101 if (components.size() > 3 &&
109 !components[3].empty() && 102 !components[3].empty() &&
110 base::StringToDouble(components[3], &seconds_since_epoch)) { 103 base::StringToDouble(components[3], &seconds_since_epoch)) {
111 info.capture_time = base::Time::FromDoubleT(seconds_since_epoch); 104 info.capture_time = base::Time::FromDoubleT(seconds_since_epoch);
112 } 105 }
113 106
114 uploads_.push_back(info); 107 uploads->push_back(info);
115 } 108 }
116 } 109 }
117 110
118 void UploadList::InformDelegateOfCompletion() { 111 void UploadList::SetUploadsAndNotifyDelegate(std::vector<UploadInfo> uploads) {
119 DCHECK(thread_checker_.CalledOnValidThread()); 112 DCHECK(thread_checker_.CalledOnValidThread());
113 uploads_ = std::move(uploads);
120 if (delegate_) 114 if (delegate_)
121 delegate_->OnUploadListAvailable(); 115 delegate_->OnUploadListAvailable();
122 } 116 }
123 117
124 void UploadList::GetUploads(unsigned int max_count, 118 void UploadList::GetUploads(size_t max_count,
125 std::vector<UploadInfo>* uploads) { 119 std::vector<UploadInfo>* uploads) {
126 DCHECK(thread_checker_.CalledOnValidThread()); 120 DCHECK(thread_checker_.CalledOnValidThread());
127 std::copy(uploads_.begin(), 121 std::copy(uploads_.begin(),
128 uploads_.begin() + std::min<size_t>(uploads_.size(), max_count), 122 uploads_.begin() + std::min(uploads_.size(), max_count),
129 std::back_inserter(*uploads)); 123 std::back_inserter(*uploads));
130 } 124 }
OLDNEW
« no previous file with comments | « components/upload_list/upload_list.h ('k') | components/upload_list/upload_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698