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

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: Reverted one auto 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
(...skipping 77 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)
groby-ooo-7-16 2016/02/09 20:20:26 Since this is purely a size limiter, just make it
Kevin Bailey 2016/02/09 22:26:39 Done.
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));
343 ++i;
346 } 344 }
347 return data; 345 return data;
348 } 346 }
349 347
350 syncer::SyncError SpellcheckCustomDictionary::ProcessSyncChanges( 348 syncer::SyncError SpellcheckCustomDictionary::ProcessSyncChanges(
351 const tracked_objects::Location& from_here, 349 const tracked_objects::Location& from_here,
352 const syncer::SyncChangeList& change_list) { 350 const syncer::SyncChangeList& change_list) {
353 DCHECK_CURRENTLY_ON(BrowserThread::UI); 351 DCHECK_CURRENTLY_ON(BrowserThread::UI);
354 scoped_ptr<Change> dictionary_change(new Change); 352 scoped_ptr<Change> dictionary_change(new Change);
355 for (const syncer::SyncChange& change : change_list) { 353 for (const syncer::SyncChange& change : change_list) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 fix_invalid_file_.callback()); 436 fix_invalid_file_.callback());
439 } 437 }
440 } 438 }
441 439
442 void SpellcheckCustomDictionary::Apply(const Change& dictionary_change) { 440 void SpellcheckCustomDictionary::Apply(const Change& dictionary_change) {
443 DCHECK_CURRENTLY_ON(BrowserThread::UI); 441 DCHECK_CURRENTLY_ON(BrowserThread::UI);
444 if (!dictionary_change.to_add().empty()) { 442 if (!dictionary_change.to_add().empty()) {
445 words_.insert(dictionary_change.to_add().begin(), 443 words_.insert(dictionary_change.to_add().begin(),
446 dictionary_change.to_add().end()); 444 dictionary_change.to_add().end());
447 } 445 }
448 if (!dictionary_change.to_remove().empty()) { 446 for (const auto& word : dictionary_change.to_remove())
449 std::set<std::string> updated_words = 447 words_.erase(word);
450 base::STLSetDifference<std::set<std::string>>(
451 words_, dictionary_change.to_remove());
452 std::swap(words_, updated_words);
453 }
454 } 448 }
455 449
456 void SpellcheckCustomDictionary::FixInvalidFile( 450 void SpellcheckCustomDictionary::FixInvalidFile(
457 scoped_ptr<LoadFileResult> load_file_result) { 451 scoped_ptr<LoadFileResult> load_file_result) {
458 DCHECK_CURRENTLY_ON(BrowserThread::UI); 452 DCHECK_CURRENTLY_ON(BrowserThread::UI);
459 BrowserThread::PostTask( 453 BrowserThread::PostTask(
460 BrowserThread::FILE, FROM_HERE, 454 BrowserThread::FILE, FROM_HERE,
461 base::Bind(&SavePassedWordsToDictionaryFileReliably, 455 base::Bind(&SavePassedWordsToDictionaryFileReliably,
462 custom_dictionary_path_, base::Passed(&load_file_result))); 456 custom_dictionary_path_, base::Passed(&load_file_result)));
463 } 457 }
(...skipping 22 matching lines...) Expand all
486 static_cast<int>( 480 static_cast<int>(
487 chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS) - 481 chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS) -
488 server_size); 482 server_size);
489 int upload_size = std::min( 483 int upload_size = std::min(
490 static_cast<int>(dictionary_change.to_add().size()), 484 static_cast<int>(dictionary_change.to_add().size()),
491 max_upload_size); 485 max_upload_size);
492 486
493 syncer::SyncChangeList sync_change_list; 487 syncer::SyncChangeList sync_change_list;
494 int i = 0; 488 int i = 0;
495 489
496 for (auto it = dictionary_change.to_add().begin(); 490 for (const auto& word : dictionary_change.to_add()) {
497 it != dictionary_change.to_add().end() && i < upload_size; ++it, ++i) { 491 if (i >= upload_size)
498 const std::string& word = *it; 492 break;
499 sync_pb::EntitySpecifics specifics; 493 sync_pb::EntitySpecifics specifics;
500 specifics.mutable_dictionary()->set_word(word); 494 specifics.mutable_dictionary()->set_word(word);
501 sync_change_list.push_back(syncer::SyncChange( 495 sync_change_list.push_back(syncer::SyncChange(
502 FROM_HERE, syncer::SyncChange::ACTION_ADD, 496 FROM_HERE, syncer::SyncChange::ACTION_ADD,
503 syncer::SyncData::CreateLocalData(word, word, specifics))); 497 syncer::SyncData::CreateLocalData(word, word, specifics)));
498 ++i;
groby-ooo-7-16 2016/02/09 20:20:26 See above.
Kevin Bailey 2016/02/09 22:26:39 Done.
504 } 499 }
505 500
506 for (const std::string& word : dictionary_change.to_remove()) { 501 for (const std::string& word : dictionary_change.to_remove()) {
507 sync_pb::EntitySpecifics specifics; 502 sync_pb::EntitySpecifics specifics;
508 specifics.mutable_dictionary()->set_word(word); 503 specifics.mutable_dictionary()->set_word(word);
509 sync_change_list.push_back(syncer::SyncChange( 504 sync_change_list.push_back(syncer::SyncChange(
510 FROM_HERE, 505 FROM_HERE,
511 syncer::SyncChange::ACTION_DELETE, 506 syncer::SyncChange::ACTION_DELETE,
512 syncer::SyncData::CreateLocalData(word, word, specifics))); 507 syncer::SyncData::CreateLocalData(word, word, specifics)));
513 } 508 }
(...skipping 12 matching lines...) Expand all
526 } 521 }
527 522
528 void SpellcheckCustomDictionary::Notify(const Change& dictionary_change) { 523 void SpellcheckCustomDictionary::Notify(const Change& dictionary_change) {
529 DCHECK_CURRENTLY_ON(BrowserThread::UI); 524 DCHECK_CURRENTLY_ON(BrowserThread::UI);
530 if (!IsLoaded() || dictionary_change.empty()) 525 if (!IsLoaded() || dictionary_change.empty())
531 return; 526 return;
532 FOR_EACH_OBSERVER(Observer, 527 FOR_EACH_OBSERVER(Observer,
533 observers_, 528 observers_,
534 OnCustomDictionaryChanged(dictionary_change)); 529 OnCustomDictionaryChanged(dictionary_change));
535 } 530 }
OLDNEW
« chrome/browser/spellchecker/feedback_sender.cc ('K') | « chrome/browser/spellchecker/misspelling.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698