OLD | NEW |
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 |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/files/important_file_writer.h" | 12 #include "base/files/important_file_writer.h" |
13 #include "base/md5.h" | 13 #include "base/md5.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | 17 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" |
18 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
19 #include "chrome/common/spellcheck_common.h" | 19 #include "components/spellcheck/common/spellcheck_common.h" |
20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
21 #include "sync/api/sync_change.h" | 21 #include "sync/api/sync_change.h" |
22 #include "sync/api/sync_error_factory.h" | 22 #include "sync/api/sync_error_factory.h" |
23 #include "sync/protocol/sync.pb.h" | 23 #include "sync/protocol/sync.pb.h" |
24 | 24 |
25 using content::BrowserThread; | 25 using content::BrowserThread; |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Filename extension for backup dictionary file. | 29 // Filename extension for backup dictionary file. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 base::TrimWhitespaceASCII(contents, base::TRIM_ALL), "\n", | 75 base::TrimWhitespaceASCII(contents, base::TRIM_ALL), "\n", |
76 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 76 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
77 words->insert(word_list.begin(), word_list.end()); | 77 words->insert(word_list.begin(), word_list.end()); |
78 return VALID_CHECKSUM; | 78 return VALID_CHECKSUM; |
79 } | 79 } |
80 | 80 |
81 // Returns true for valid custom dictionary words. | 81 // Returns true for valid custom dictionary words. |
82 bool IsValidWord(const std::string& word) { | 82 bool IsValidWord(const std::string& word) { |
83 std::string tmp; | 83 std::string tmp; |
84 return !word.empty() && | 84 return !word.empty() && |
85 word.size() <= | 85 word.size() <= spellcheck::MAX_CUSTOM_DICTIONARY_WORD_BYTES && |
86 chrome::spellcheck_common::MAX_CUSTOM_DICTIONARY_WORD_BYTES && | |
87 base::IsStringUTF8(word) && | 86 base::IsStringUTF8(word) && |
88 base::TRIM_NONE == | 87 base::TRIM_NONE == |
89 base::TrimWhitespaceASCII(word, base::TRIM_ALL, &tmp); | 88 base::TrimWhitespaceASCII(word, base::TRIM_ALL, &tmp); |
90 } | 89 } |
91 | 90 |
92 // Removes duplicate and invalid words from |to_add| word list. Looks for | 91 // 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 | 92 // duplicates in both |to_add| and |existing| word lists. Returns a bitmap of |
94 // |ChangeSanitationResult| values. | 93 // |ChangeSanitationResult| values. |
95 int SanitizeWordsToAdd(const std::set<std::string>& existing, | 94 int SanitizeWordsToAdd(const std::set<std::string>& existing, |
96 std::set<std::string>* to_add) { | 95 std::set<std::string>* to_add) { |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 sync_error_handler_.reset(); | 328 sync_error_handler_.reset(); |
330 } | 329 } |
331 | 330 |
332 syncer::SyncDataList SpellcheckCustomDictionary::GetAllSyncData( | 331 syncer::SyncDataList SpellcheckCustomDictionary::GetAllSyncData( |
333 syncer::ModelType type) const { | 332 syncer::ModelType type) const { |
334 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 333 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
335 DCHECK_EQ(syncer::DICTIONARY, type); | 334 DCHECK_EQ(syncer::DICTIONARY, type); |
336 syncer::SyncDataList data; | 335 syncer::SyncDataList data; |
337 size_t i = 0; | 336 size_t i = 0; |
338 for (const auto& word : words_) { | 337 for (const auto& word : words_) { |
339 if (i++ >= chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS) | 338 if (i++ >= spellcheck::MAX_SYNCABLE_DICTIONARY_WORDS) |
340 break; | 339 break; |
341 sync_pb::EntitySpecifics specifics; | 340 sync_pb::EntitySpecifics specifics; |
342 specifics.mutable_dictionary()->set_word(word); | 341 specifics.mutable_dictionary()->set_word(word); |
343 data.push_back(syncer::SyncData::CreateLocalData(word, word, specifics)); | 342 data.push_back(syncer::SyncData::CreateLocalData(word, word, specifics)); |
344 } | 343 } |
345 return data; | 344 return data; |
346 } | 345 } |
347 | 346 |
348 syncer::SyncError SpellcheckCustomDictionary::ProcessSyncChanges( | 347 syncer::SyncError SpellcheckCustomDictionary::ProcessSyncChanges( |
349 const tracked_objects::Location& from_here, | 348 const tracked_objects::Location& from_here, |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 syncer::SyncError SpellcheckCustomDictionary::Sync( | 468 syncer::SyncError SpellcheckCustomDictionary::Sync( |
470 const Change& dictionary_change) { | 469 const Change& dictionary_change) { |
471 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 470 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
472 syncer::SyncError error; | 471 syncer::SyncError error; |
473 if (!IsSyncing() || dictionary_change.empty()) | 472 if (!IsSyncing() || dictionary_change.empty()) |
474 return error; | 473 return error; |
475 | 474 |
476 // The number of words on the sync server should not exceed the limits. | 475 // The number of words on the sync server should not exceed the limits. |
477 int server_size = static_cast<int>(words_.size()) - | 476 int server_size = static_cast<int>(words_.size()) - |
478 static_cast<int>(dictionary_change.to_add().size()); | 477 static_cast<int>(dictionary_change.to_add().size()); |
479 int max_upload_size = std::max( | 478 int max_upload_size = |
480 0, | 479 std::max(0, static_cast<int>(spellcheck::MAX_SYNCABLE_DICTIONARY_WORDS) - |
481 static_cast<int>( | 480 server_size); |
482 chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS) - | |
483 server_size); | |
484 int upload_size = std::min( | 481 int upload_size = std::min( |
485 static_cast<int>(dictionary_change.to_add().size()), | 482 static_cast<int>(dictionary_change.to_add().size()), |
486 max_upload_size); | 483 max_upload_size); |
487 | 484 |
488 syncer::SyncChangeList sync_change_list; | 485 syncer::SyncChangeList sync_change_list; |
489 int i = 0; | 486 int i = 0; |
490 | 487 |
491 for (const auto& word : dictionary_change.to_add()) { | 488 for (const auto& word : dictionary_change.to_add()) { |
492 if (i++ >= upload_size) | 489 if (i++ >= upload_size) |
493 break; | 490 break; |
(...skipping 13 matching lines...) Expand all Loading... |
507 syncer::SyncData::CreateLocalData(word, word, specifics))); | 504 syncer::SyncData::CreateLocalData(word, word, specifics))); |
508 } | 505 } |
509 | 506 |
510 // Send the changes to the sync processor. | 507 // Send the changes to the sync processor. |
511 error = sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list); | 508 error = sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list); |
512 if (error.IsSet()) | 509 if (error.IsSet()) |
513 return error; | 510 return error; |
514 | 511 |
515 // Turn off syncing of this dictionary if the server already has the maximum | 512 // Turn off syncing of this dictionary if the server already has the maximum |
516 // number of words. | 513 // number of words. |
517 if (words_.size() > chrome::spellcheck_common::MAX_SYNCABLE_DICTIONARY_WORDS) | 514 if (words_.size() > spellcheck::MAX_SYNCABLE_DICTIONARY_WORDS) |
518 StopSyncing(syncer::DICTIONARY); | 515 StopSyncing(syncer::DICTIONARY); |
519 | 516 |
520 return error; | 517 return error; |
521 } | 518 } |
522 | 519 |
523 void SpellcheckCustomDictionary::Notify(const Change& dictionary_change) { | 520 void SpellcheckCustomDictionary::Notify(const Change& dictionary_change) { |
524 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 521 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
525 if (!IsLoaded() || dictionary_change.empty()) | 522 if (!IsLoaded() || dictionary_change.empty()) |
526 return; | 523 return; |
527 FOR_EACH_OBSERVER(Observer, | 524 FOR_EACH_OBSERVER(Observer, |
528 observers_, | 525 observers_, |
529 OnCustomDictionaryChanged(dictionary_change)); | 526 OnCustomDictionaryChanged(dictionary_change)); |
530 } | 527 } |
OLD | NEW |