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

Side by Side Diff: chrome/browser/spellchecker/spellcheck_custom_dictionary.cc

Issue 1665023002: Cheer up spell-checking code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More descriptive variable name Created 4 years, 10 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 | « chrome/browser/spellchecker/misspelling.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" 5 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <functional> 8 #include <functional>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 base::TrimWhitespaceASCII(word, base::TRIM_ALL, &tmp); 89 base::TrimWhitespaceASCII(word, base::TRIM_ALL, &tmp);
90 } 90 }
91 91
92 // Removes duplicate and invalid words from |to_add| word list. Looks for 92 // Removes duplicate and invalid words from |to_add| word list. Looks for
93 // duplicates in both |to_add| and |existing| word lists. Returns a bitmap of 93 // duplicates in both |to_add| and |existing| word lists. Returns a bitmap of
94 // |ChangeSanitationResult| values. 94 // |ChangeSanitationResult| values.
95 int SanitizeWordsToAdd(const std::set<std::string>& existing, 95 int SanitizeWordsToAdd(const std::set<std::string>& existing,
96 std::set<std::string>* to_add) { 96 std::set<std::string>* to_add) {
97 DCHECK(to_add); 97 DCHECK(to_add);
98 // Do not add duplicate words. 98 // Do not add duplicate words.
99 std::set<std::string> new_words = 99 std::vector<std::string> new_words =
100 base::STLSetDifference<std::set<std::string>>(*to_add, existing); 100 base::STLSetDifference<std::vector<std::string>>(*to_add, existing);
101 int result = VALID_CHANGE; 101 int result = VALID_CHANGE;
102 if (to_add->size() != new_words.size()) 102 if (to_add->size() != new_words.size())
103 result |= DETECTED_DUPLICATE_WORDS; 103 result |= DETECTED_DUPLICATE_WORDS;
104 // Do not add invalid words. 104 // Do not add invalid words.
105 std::set<std::string> valid_new_words; 105 std::set<std::string> valid_new_words;
106 for (const std::string& word : new_words) { 106 for (const auto& word : new_words) {
107 if (IsValidWord(word)) 107 if (IsValidWord(word))
108 valid_new_words.insert(valid_new_words.end(), word); 108 valid_new_words.insert(valid_new_words.end(), word);
109 } 109 }
110 if (valid_new_words.size() != new_words.size()) 110 if (valid_new_words.size() != new_words.size())
111 result |= DETECTED_INVALID_WORDS; 111 result |= DETECTED_INVALID_WORDS;
112 // Save the sanitized words to be added. 112 // Save the sanitized words to be added.
113 std::swap(*to_add, valid_new_words); 113 std::swap(*to_add, valid_new_words);
114 return result; 114 return result;
115 } 115 }
116 116
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 DCHECK_EQ(syncer::DICTIONARY, type); 326 DCHECK_EQ(syncer::DICTIONARY, type);
327 sync_processor_.reset(); 327 sync_processor_.reset();
328 sync_error_handler_.reset(); 328 sync_error_handler_.reset();
329 } 329 }
330 330
331 syncer::SyncDataList SpellcheckCustomDictionary::GetAllSyncData( 331 syncer::SyncDataList SpellcheckCustomDictionary::GetAllSyncData(
332 syncer::ModelType type) const { 332 syncer::ModelType type) const {
333 DCHECK_CURRENTLY_ON(BrowserThread::UI); 333 DCHECK_CURRENTLY_ON(BrowserThread::UI);
334 DCHECK_EQ(syncer::DICTIONARY, type); 334 DCHECK_EQ(syncer::DICTIONARY, type);
335 syncer::SyncDataList data; 335 syncer::SyncDataList data;
336 std::string word;
337 size_t i = 0; 336 size_t i = 0;
338 for (auto it = words_.begin(); 337 for (const auto& word : words_) {
339 it != words_.end() && 338 if (i++ >= chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS)
340 i < chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS; 339 break;
341 ++it, ++i) {
342 word = *it;
343 sync_pb::EntitySpecifics specifics; 340 sync_pb::EntitySpecifics specifics;
344 specifics.mutable_dictionary()->set_word(word); 341 specifics.mutable_dictionary()->set_word(word);
345 data.push_back(syncer::SyncData::CreateLocalData(word, word, specifics)); 342 data.push_back(syncer::SyncData::CreateLocalData(word, word, specifics));
346 } 343 }
347 return data; 344 return data;
348 } 345 }
349 346
350 syncer::SyncError SpellcheckCustomDictionary::ProcessSyncChanges( 347 syncer::SyncError SpellcheckCustomDictionary::ProcessSyncChanges(
351 const tracked_objects::Location& from_here, 348 const tracked_objects::Location& from_here,
352 const syncer::SyncChangeList& change_list) { 349 const syncer::SyncChangeList& change_list) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 fix_invalid_file_.callback()); 435 fix_invalid_file_.callback());
439 } 436 }
440 } 437 }
441 438
442 void SpellcheckCustomDictionary::Apply(const Change& dictionary_change) { 439 void SpellcheckCustomDictionary::Apply(const Change& dictionary_change) {
443 DCHECK_CURRENTLY_ON(BrowserThread::UI); 440 DCHECK_CURRENTLY_ON(BrowserThread::UI);
444 if (!dictionary_change.to_add().empty()) { 441 if (!dictionary_change.to_add().empty()) {
445 words_.insert(dictionary_change.to_add().begin(), 442 words_.insert(dictionary_change.to_add().begin(),
446 dictionary_change.to_add().end()); 443 dictionary_change.to_add().end());
447 } 444 }
448 if (!dictionary_change.to_remove().empty()) { 445 for (const auto& word : dictionary_change.to_remove())
449 std::set<std::string> updated_words = 446 words_.erase(word);
450 base::STLSetDifference<std::set<std::string>>(
451 words_, dictionary_change.to_remove());
452 std::swap(words_, updated_words);
453 }
454 } 447 }
455 448
456 void SpellcheckCustomDictionary::FixInvalidFile( 449 void SpellcheckCustomDictionary::FixInvalidFile(
457 scoped_ptr<LoadFileResult> load_file_result) { 450 scoped_ptr<LoadFileResult> load_file_result) {
458 DCHECK_CURRENTLY_ON(BrowserThread::UI); 451 DCHECK_CURRENTLY_ON(BrowserThread::UI);
459 BrowserThread::PostTask( 452 BrowserThread::PostTask(
460 BrowserThread::FILE, FROM_HERE, 453 BrowserThread::FILE, FROM_HERE,
461 base::Bind(&SavePassedWordsToDictionaryFileReliably, 454 base::Bind(&SavePassedWordsToDictionaryFileReliably,
462 custom_dictionary_path_, base::Passed(&load_file_result))); 455 custom_dictionary_path_, base::Passed(&load_file_result)));
463 } 456 }
(...skipping 22 matching lines...) Expand all
486 static_cast<int>( 479 static_cast<int>(
487 chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS) - 480 chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS) -
488 server_size); 481 server_size);
489 int upload_size = std::min( 482 int upload_size = std::min(
490 static_cast<int>(dictionary_change.to_add().size()), 483 static_cast<int>(dictionary_change.to_add().size()),
491 max_upload_size); 484 max_upload_size);
492 485
493 syncer::SyncChangeList sync_change_list; 486 syncer::SyncChangeList sync_change_list;
494 int i = 0; 487 int i = 0;
495 488
496 for (auto it = dictionary_change.to_add().begin(); 489 for (const auto& word : dictionary_change.to_add()) {
497 it != dictionary_change.to_add().end() && i < upload_size; ++it, ++i) { 490 if (i++ >= upload_size)
498 const std::string& word = *it; 491 break;
499 sync_pb::EntitySpecifics specifics; 492 sync_pb::EntitySpecifics specifics;
500 specifics.mutable_dictionary()->set_word(word); 493 specifics.mutable_dictionary()->set_word(word);
501 sync_change_list.push_back(syncer::SyncChange( 494 sync_change_list.push_back(syncer::SyncChange(
502 FROM_HERE, syncer::SyncChange::ACTION_ADD, 495 FROM_HERE, syncer::SyncChange::ACTION_ADD,
503 syncer::SyncData::CreateLocalData(word, word, specifics))); 496 syncer::SyncData::CreateLocalData(word, word, specifics)));
504 } 497 }
505 498
506 for (const std::string& word : dictionary_change.to_remove()) { 499 for (const std::string& word : dictionary_change.to_remove()) {
507 sync_pb::EntitySpecifics specifics; 500 sync_pb::EntitySpecifics specifics;
508 specifics.mutable_dictionary()->set_word(word); 501 specifics.mutable_dictionary()->set_word(word);
(...skipping 17 matching lines...) Expand all
526 } 519 }
527 520
528 void SpellcheckCustomDictionary::Notify(const Change& dictionary_change) { 521 void SpellcheckCustomDictionary::Notify(const Change& dictionary_change) {
529 DCHECK_CURRENTLY_ON(BrowserThread::UI); 522 DCHECK_CURRENTLY_ON(BrowserThread::UI);
530 if (!IsLoaded() || dictionary_change.empty()) 523 if (!IsLoaded() || dictionary_change.empty())
531 return; 524 return;
532 FOR_EACH_OBSERVER(Observer, 525 FOR_EACH_OBSERVER(Observer,
533 observers_, 526 observers_,
534 OnCustomDictionaryChanged(dictionary_change)); 527 OnCustomDictionaryChanged(dictionary_change));
535 } 528 }
OLDNEW
« no previous file with comments | « chrome/browser/spellchecker/misspelling.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698