Modernize to C++11: Use auto key to improve code readability and maintainability.

This commit is contained in:
Haojian Wu 2016-07-10 13:09:55 +02:00
parent fab02809c6
commit 04f9d35312
22 changed files with 43 additions and 61 deletions

View file

@ -21,7 +21,7 @@ UvTaskRunner::~UvTaskRunner() {
bool UvTaskRunner::PostDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) {
uv_timer_t* timer = new uv_timer_t;
auto* timer = new uv_timer_t;
timer->data = this;
uv_timer_init(loop_, timer);
uv_timer_start(timer, UvTaskRunner::OnTimeout, delay.InMilliseconds(), 0);

View file

@ -266,7 +266,7 @@ void DoCacheActionInIO(
// Call GetBackend and make the backend's ptr accessable in OnGetBackend.
using BackendPtr = disk_cache::Backend*;
BackendPtr* backend_ptr = new BackendPtr(nullptr);
auto* backend_ptr = new BackendPtr(nullptr);
net::CompletionCallback on_get_backend =
base::Bind(&OnGetBackend, base::Owned(backend_ptr), action, callback);
int rv = http_cache->GetBackend(backend_ptr, on_get_backend);

View file

@ -94,7 +94,7 @@ FileSystem CreateFileSystemStruct(
}
base::DictionaryValue* CreateFileSystemValue(const FileSystem& file_system) {
base::DictionaryValue* file_system_value = new base::DictionaryValue();
auto* file_system_value = new base::DictionaryValue();
file_system_value->SetString("fileSystemName", file_system.file_system_name);
file_system_value->SetString("rootURL", file_system.root_url);
file_system_value->SetString("fileSystemPath", file_system.file_system_path);
@ -377,7 +377,7 @@ content::SecurityStyle CommonWebContentsDelegate::GetSecurityStyle(
void CommonWebContentsDelegate::DevToolsSaveToFile(
const std::string& url, const std::string& content, bool save_as) {
base::FilePath path;
PathsMap::iterator it = saved_files_.find(url);
auto it = saved_files_.find(url);
if (it != saved_files_.end() && !save_as) {
path = it->second;
} else {
@ -402,7 +402,7 @@ void CommonWebContentsDelegate::DevToolsSaveToFile(
void CommonWebContentsDelegate::DevToolsAppendToFile(
const std::string& url, const std::string& content) {
PathsMap::iterator it = saved_files_.find(url);
auto it = saved_files_.find(url);
if (it == saved_files_.end())
return;
@ -610,8 +610,7 @@ void CommonWebContentsDelegate::OnDevToolsSearchCompleted(
const std::string& file_system_path,
const std::vector<std::string>& file_paths) {
base::ListValue file_paths_value;
for (std::vector<std::string>::const_iterator it(file_paths.begin());
it != file_paths.end(); ++it) {
for (auto it(file_paths.begin()); it != file_paths.end(); ++it) {
file_paths_value.AppendString(*it);
}
base::FundamentalValue request_id_value(request_id);

View file

@ -22,7 +22,7 @@ net::URLRequestJob* AsarProtocolHandler::MaybeCreateJob(
net::NetworkDelegate* network_delegate) const {
base::FilePath full_path;
net::FileURLToFilePath(request->url(), &full_path);
URLRequestAsarJob* job = new URLRequestAsarJob(request, network_delegate);
auto* job = new URLRequestAsarJob(request, network_delegate);
job->Initialize(file_task_runner_, full_path);
return job;
}

View file

@ -111,7 +111,7 @@ void URLRequestAsarJob::Start() {
if (rv != net::ERR_IO_PENDING)
DidOpen(rv);
} else if (type_ == TYPE_FILE) {
FileMetaInfo* meta_info = new FileMetaInfo();
auto* meta_info = new FileMetaInfo();
file_task_runner_->PostTaskAndReply(
FROM_HERE,
base::Bind(&URLRequestAsarJob::FetchMetaInfo, file_path_,
@ -224,7 +224,7 @@ int URLRequestAsarJob::GetResponseCode() const {
void URLRequestAsarJob::GetResponseInfo(net::HttpResponseInfo* info) {
std::string status("HTTP/1.1 200 OK");
net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status);
auto* headers = new net::HttpResponseHeaders(status);
headers->AddHeader(atom::kCORSHeader);
info->headers = headers;

View file

@ -27,7 +27,7 @@ bool AtomURLRequestJobFactory::SetProtocolHandler(
const std::string& scheme,
std::unique_ptr<ProtocolHandler> protocol_handler) {
if (!protocol_handler) {
ProtocolHandlerMap::iterator it = protocol_handler_map_.find(scheme);
auto it = protocol_handler_map_.find(scheme);
if (it == protocol_handler_map_.end())
return false;
@ -66,7 +66,7 @@ ProtocolHandler* AtomURLRequestJobFactory::GetProtocolHandler(
const std::string& scheme) const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find(scheme);
auto it = protocol_handler_map_.find(scheme);
if (it == protocol_handler_map_.end())
return nullptr;
return it->second;
@ -87,7 +87,7 @@ net::URLRequestJob* AtomURLRequestJobFactory::MaybeCreateJobWithProtocolHandler(
net::NetworkDelegate* network_delegate) const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
ProtocolHandlerMap::const_iterator it = protocol_handler_map_.find(scheme);
auto it = protocol_handler_map_.find(scheme);
if (it == protocol_handler_map_.end())
return nullptr;
return it->second->MaybeCreateJob(request, network_delegate);

View file

@ -40,7 +40,7 @@ void URLRequestAsyncAsarJob::StartAsync(std::unique_ptr<base::Value> options) {
void URLRequestAsyncAsarJob::GetResponseInfo(net::HttpResponseInfo* info) {
std::string status("HTTP/1.1 200 OK");
net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status);
auto* headers = new net::HttpResponseHeaders(status);
headers->AddHeader(kCORSHeader);
info->headers = headers;

View file

@ -72,7 +72,7 @@ void URLRequestBufferJob::GetResponseInfo(net::HttpResponseInfo* info) {
status.append(" ");
status.append(net::GetHttpReasonPhrase(status_code_));
status.append("\0\0", 2);
net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status);
auto* headers = new net::HttpResponseHeaders(status);
headers->AddHeader(kCORSHeader);

View file

@ -31,7 +31,7 @@ void URLRequestStringJob::StartAsync(std::unique_ptr<base::Value> options) {
void URLRequestStringJob::GetResponseInfo(net::HttpResponseInfo* info) {
std::string status("HTTP/1.1 200 OK");
net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status);
auto* headers = new net::HttpResponseHeaders(status);
headers->AddHeader(kCORSHeader);

View file

@ -123,7 +123,7 @@ v8::Local<v8::Value> CreateFunctionFromTranslater(
v8::Local<v8::FunctionTemplate> call_translater =
v8::Local<v8::FunctionTemplate>::New(isolate, g_call_translater);
TranslaterHolder* holder = new TranslaterHolder;
auto* holder = new TranslaterHolder;
holder->translater = translater;
return BindFunctionWith(isolate,
isolate->GetCurrentContext(),

View file

@ -55,7 +55,7 @@ class V8ValueConverter::FromV8ValueState {
// hash. Different hash obviously means different objects, but two objects
// in a couple of thousands could have the same identity hash.
std::pair<Iterator, Iterator> range = unique_map_.equal_range(hash);
for (Iterator it = range.first; it != range.second; ++it) {
for (auto it = range.first; it != range.second; ++it) {
// Operator == for handles actually compares the underlying objects.
if (it->second == handle)
return false;
@ -295,7 +295,7 @@ base::Value* V8ValueConverter::FromV8Array(
val->CreationContext() != isolate->GetCurrentContext())
scope.reset(new v8::Context::Scope(val->CreationContext()));
base::ListValue* result = new base::ListValue();
auto* result = new base::ListValue();
// Only fields with integer keys are carried over to the ListValue.
for (uint32_t i = 0; i < val->Length(); ++i) {

View file

@ -58,8 +58,7 @@ bool AtomContentUtilityClient::OnMessageReceived(
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
for (Handlers::iterator it = handlers_.begin();
!handled && it != handlers_.end(); ++it) {
for (auto it = handlers_.begin(); !handled && it != handlers_.end(); ++it) {
handled = (*it)->OnMessageReceived(message);
}

View file

@ -53,7 +53,7 @@ void GlobalShortcutListener::UnregisterAccelerator(
if (IsShortcutHandlingSuspended())
return;
AcceleratorMap::iterator it = accelerator_map_.find(accelerator);
auto it = accelerator_map_.find(accelerator);
// We should never get asked to unregister something that we didn't register.
DCHECK(it != accelerator_map_.end());
// The caller should call this function with the right observer.
@ -70,10 +70,10 @@ void GlobalShortcutListener::UnregisterAccelerators(Observer* observer) {
if (IsShortcutHandlingSuspended())
return;
AcceleratorMap::iterator it = accelerator_map_.begin();
auto it = accelerator_map_.begin();
while (it != accelerator_map_.end()) {
if (it->second == observer) {
AcceleratorMap::iterator to_remove = it++;
auto to_remove = it++;
UnregisterAccelerator(to_remove->first, observer);
} else {
++it;
@ -87,9 +87,7 @@ void GlobalShortcutListener::SetShortcutHandlingSuspended(bool suspended) {
return;
shortcut_handling_suspended_ = suspended;
for (AcceleratorMap::iterator it = accelerator_map_.begin();
it != accelerator_map_.end();
++it) {
for (auto it = accelerator_map_.begin(); it != accelerator_map_.end(); ++it) {
// On Linux, when shortcut handling is suspended we cannot simply early
// return in NotifyKeyPressed (similar to what we do for non-global
// shortcuts) because we'd eat the keyboard event thereby preventing the
@ -108,7 +106,7 @@ bool GlobalShortcutListener::IsShortcutHandlingSuspended() const {
void GlobalShortcutListener::NotifyKeyPressed(
const ui::Accelerator& accelerator) {
AcceleratorMap::iterator iter = accelerator_map_.find(accelerator);
auto iter = accelerator_map_.find(accelerator);
if (iter == accelerator_map_.end()) {
// This should never occur, because if it does, we have failed to unregister
// or failed to clean up the map after unregistering the shortcut.

View file

@ -152,8 +152,7 @@ void NativeDesktopMediaList::Worker::Refresh(
if (window_capturer_) {
webrtc::WindowCapturer::WindowList windows;
if (window_capturer_->GetWindowList(&windows)) {
for (webrtc::WindowCapturer::WindowList::iterator it = windows.begin();
it != windows.end(); ++it) {
for (auto it = windows.begin(); it != windows.end(); ++it) {
// Skip the picker dialog window.
if (it->id != view_dialog_id) {
sources.push_back(SourceDescription(
@ -199,7 +198,7 @@ void NativeDesktopMediaList::Worker::Refresh(
new_image_hashes[source.id] = frame_hash;
// Scale the image only if it has changed.
ImageHashesMap::iterator it = image_hashes_.find(source.id);
auto it = image_hashes_.find(source.id);
if (it == image_hashes_.end() || it->second != frame_hash) {
gfx::ImageSkia thumbnail =
ScaleDesktopFrame(std::move(current_frame_), thumbnail_size);

View file

@ -71,10 +71,8 @@ void PrintJob::Initialize(PrintJobWorkerOwner* job,
worker_.reset(job->DetachWorker(this));
settings_ = job->settings();
PrintedDocument* new_doc =
new PrintedDocument(settings_,
source_,
job->cookie(),
auto* new_doc =
new PrintedDocument(settings_, source_, job->cookie(),
content::BrowserThread::GetBlockingPool());
new_doc->set_page_count(page_count);
UpdatePrintedDocument(new_doc);

View file

@ -32,8 +32,8 @@ void PrintQueriesQueue::QueuePrinterQuery(PrinterQuery* job) {
scoped_refptr<PrinterQuery> PrintQueriesQueue::PopPrinterQuery(
int document_cookie) {
base::AutoLock lock(lock_);
for (PrinterQueries::iterator itr = queued_queries_.begin();
itr != queued_queries_.end(); ++itr) {
for (auto itr = queued_queries_.begin(); itr != queued_queries_.end();
++itr) {
if ((*itr)->cookie() == document_cookie && !(*itr)->is_callback_pending()) {
scoped_refptr<printing::PrinterQuery> current_query(*itr);
queued_queries_.erase(itr);
@ -61,8 +61,8 @@ void PrintQueriesQueue::Shutdown() {
// Stop all pending queries, requests to generate print preview do not have
// corresponding PrintJob, so any pending preview requests are not covered
// by PrintJobManager::StopJobs and should be stopped explicitly.
for (PrinterQueries::iterator itr = queries_to_stop.begin();
itr != queries_to_stop.end(); ++itr) {
for (auto itr = queries_to_stop.begin(); itr != queries_to_stop.end();
++itr) {
(*itr)->PostTask(FROM_HERE, base::Bind(&PrinterQuery::StopWorker, *itr));
}
}
@ -99,8 +99,7 @@ void PrintJobManager::StopJobs(bool wait_for_finish) {
PrintJobs to_stop;
to_stop.swap(current_jobs_);
for (PrintJobs::const_iterator job = to_stop.begin(); job != to_stop.end();
++job) {
for (auto job = to_stop.begin(); job != to_stop.end(); ++job) {
// Wait for two minutes for the print job to be spooled.
if (wait_for_finish)
(*job)->FlushJob(base::TimeDelta::FromMinutes(2));

View file

@ -75,7 +75,7 @@ void NotificationCallback(PrintJobWorkerOwner* print_job,
JobEventDetails::Type detail_type,
PrintedDocument* document,
PrintedPage* page) {
JobEventDetails* details = new JobEventDetails(detail_type, document, page);
auto* details = new JobEventDetails(detail_type, document, page);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PRINT_JOB_EVENT,
// We know that is is a PrintJob object in this circumstance.

View file

@ -48,7 +48,7 @@ char* CopyPDFDataOnIOThread(
if (!shared_buf->Map(params.data_size))
return nullptr;
char* memory_pdf_data = static_cast<char*>(shared_buf->memory());
char* pdf_data = new char[params.data_size];
auto* pdf_data = new char[params.data_size];
memcpy(pdf_data, memory_pdf_data, params.data_size);
return pdf_data;
}

View file

@ -579,9 +579,7 @@ void ProcessSingleton::LinuxWatcher::OnFileCanReadWithoutBlocking(int fd) {
}
int rv = base::SetNonBlocking(connection_socket);
DCHECK_EQ(0, rv) << "Failed to make non-blocking socket.";
SocketReader* reader = new SocketReader(this,
ui_message_loop_,
connection_socket);
auto* reader = new SocketReader(this, ui_message_loop_, connection_socket);
readers_.insert(reader);
}
@ -822,8 +820,7 @@ ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessWithTimeout(
to_send.append(current_dir.value());
const std::vector<std::string>& argv = atom::AtomCommandLine::argv();
for (std::vector<std::string>::const_iterator it = argv.begin();
it != argv.end(); ++it) {
for (auto it = argv.begin(); it != argv.end(); ++it) {
to_send.push_back(kTokenDelimiter);
to_send.append(*it);
}

View file

@ -86,9 +86,7 @@ std::string ReadDataFromPickle(const base::string16& format,
bool WriteDataToPickle(const std::map<base::string16, std::string>& data,
base::Pickle* pickle) {
pickle->WriteUInt32(data.size());
for (std::map<base::string16, std::string>::const_iterator it = data.begin();
it != data.end();
++it) {
for (auto it = data.begin(); it != data.end(); ++it) {
if (!pickle->WriteString16(it->first))
return false;
if (!pickle->WriteString(it->second))

View file

@ -415,10 +415,8 @@ int TtsControllerImpl::GetMatchingVoice(
if (utterance->required_event_types().size() > 0) {
bool has_all_required_event_types = true;
for (std::set<TtsEventType>::const_iterator iter =
utterance->required_event_types().begin();
iter != utterance->required_event_types().end();
++iter) {
for (auto iter = utterance->required_event_types().begin();
iter != utterance->required_event_types().end(); ++iter) {
if (voice.events.find(*iter) == voice.events.end()) {
has_all_required_event_types = false;
break;
@ -436,8 +434,7 @@ int TtsControllerImpl::GetMatchingVoice(
}
void TtsControllerImpl::VoicesChanged() {
for (std::set<VoicesChangedDelegate*>::iterator iter =
voices_changed_delegates_.begin();
for (auto iter = voices_changed_delegates_.begin();
iter != voices_changed_delegates_.end(); ++iter) {
(*iter)->OnVoicesChanged();
}

View file

@ -543,8 +543,7 @@ bool URLPattern::Contains(const URLPattern& other) const {
bool URLPattern::MatchesAnyScheme(
const std::vector<std::string>& schemes) const {
for (std::vector<std::string>::const_iterator i = schemes.begin();
i != schemes.end(); ++i) {
for (auto i = schemes.begin(); i != schemes.end(); ++i) {
if (MatchesScheme(*i))
return true;
}
@ -554,8 +553,7 @@ bool URLPattern::MatchesAnyScheme(
bool URLPattern::MatchesAllSchemes(
const std::vector<std::string>& schemes) const {
for (std::vector<std::string>::const_iterator i = schemes.begin();
i != schemes.end(); ++i) {
for (auto i = schemes.begin(); i != schemes.end(); ++i) {
if (!MatchesScheme(*i))
return false;
}