| 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/spelling_service_client.h" | 5 #include "chrome/browser/spellchecker/spelling_service_client.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/string_escape.h" | 8 #include "base/json/string_escape.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/common/spellcheck_result.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/common/url_fetcher.h" | 18 #include "content/public/common/url_fetcher.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult
.h" | |
| 19 #include "unicode/uloc.h" | 19 #include "unicode/uloc.h" |
| 20 | 20 |
| 21 #if defined(GOOGLE_CHROME_BUILD) | 21 #if defined(GOOGLE_CHROME_BUILD) |
| 22 #include "chrome/browser/spellchecker/internal/spellcheck_internal.h" | 22 #include "chrome/browser/spellchecker/internal/spellcheck_internal.h" |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 // Use the public URL to the Spelling service on Chromium. Unfortunately, this | 25 // Use the public URL to the Spelling service on Chromium. Unfortunately, this |
| 26 // service is an experimental service and returns an error without a key. | 26 // service is an experimental service and returns an error without a key. |
| 27 #ifndef SPELLING_SERVICE_KEY | 27 #ifndef SPELLING_SERVICE_KEY |
| 28 #define SPELLING_SERVICE_KEY | 28 #define SPELLING_SERVICE_KEY |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 fetcher_->Start(); | 96 fetcher_->Start(); |
| 97 tag_ = tag; | 97 tag_ = tag; |
| 98 callback_ = callback; | 98 callback_ = callback; |
| 99 return true; | 99 return true; |
| 100 } | 100 } |
| 101 | 101 |
| 102 void SpellingServiceClient::OnURLFetchComplete( | 102 void SpellingServiceClient::OnURLFetchComplete( |
| 103 const content::URLFetcher* source) { | 103 const content::URLFetcher* source) { |
| 104 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 104 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 105 scoped_ptr<content::URLFetcher> clean_up_fetcher(fetcher_.release()); | 105 scoped_ptr<content::URLFetcher> clean_up_fetcher(fetcher_.release()); |
| 106 std::vector<WebKit::WebTextCheckingResult> results; | 106 std::vector<SpellCheckResult> results; |
| 107 if (source->GetResponseCode() / 100 == 2) { | 107 if (source->GetResponseCode() / 100 == 2) { |
| 108 std::string data; | 108 std::string data; |
| 109 source->GetResponseAsString(&data); | 109 source->GetResponseAsString(&data); |
| 110 ParseResponse(data, &results); | 110 ParseResponse(data, &results); |
| 111 } | 111 } |
| 112 callback_.Run(tag_, results); | 112 callback_.Run(tag_, results); |
| 113 } | 113 } |
| 114 | 114 |
| 115 bool SpellingServiceClient::ParseResponse( | 115 bool SpellingServiceClient::ParseResponse( |
| 116 const std::string& data, | 116 const std::string& data, |
| 117 std::vector<WebKit::WebTextCheckingResult>* results) { | 117 std::vector<SpellCheckResult>* results) { |
| 118 // When this JSON-RPC call finishes successfully, the Spelling service returns | 118 // When this JSON-RPC call finishes successfully, the Spelling service returns |
| 119 // an JSON object listed below. | 119 // an JSON object listed below. |
| 120 // * result - an envelope object representing the result from the APIARY | 120 // * result - an envelope object representing the result from the APIARY |
| 121 // server, which is the JSON-API front-end for the Spelling service. This | 121 // server, which is the JSON-API front-end for the Spelling service. This |
| 122 // object consists of the following variable: | 122 // object consists of the following variable: |
| 123 // - spellingCheckResponse (SpellingCheckResponse). | 123 // - spellingCheckResponse (SpellingCheckResponse). |
| 124 // * SpellingCheckResponse - an object representing the result from the | 124 // * SpellingCheckResponse - an object representing the result from the |
| 125 // Spelling service. This object consists of the following variable: | 125 // Spelling service. This object consists of the following variable: |
| 126 // - misspellings (optional array of Misspelling) | 126 // - misspellings (optional array of Misspelling) |
| 127 // * Misspelling - an object representing a misspelling region and its | 127 // * Misspelling - an object representing a misspelling region and its |
| (...skipping 27 matching lines...) Expand all Loading... |
| 155 // in the Spelling service, it returns a JSON representing the internal error. | 155 // in the Spelling service, it returns a JSON representing the internal error. |
| 156 // (In this case, its HTTP status is 200.) We just return false for this case. | 156 // (In this case, its HTTP status is 200.) We just return false for this case. |
| 157 ListValue* misspellings = NULL; | 157 ListValue* misspellings = NULL; |
| 158 const char kMisspellings[] = "result.spellingCheckResponse.misspellings"; | 158 const char kMisspellings[] = "result.spellingCheckResponse.misspellings"; |
| 159 if (!value->GetList(kMisspellings, &misspellings)) | 159 if (!value->GetList(kMisspellings, &misspellings)) |
| 160 return false; | 160 return false; |
| 161 | 161 |
| 162 for (size_t i = 0; i < misspellings->GetSize(); ++i) { | 162 for (size_t i = 0; i < misspellings->GetSize(); ++i) { |
| 163 // Retrieve the i-th misspelling region and put it to the given vector. When | 163 // Retrieve the i-th misspelling region and put it to the given vector. When |
| 164 // the Spelling service sends two or more suggestions, we read only the | 164 // the Spelling service sends two or more suggestions, we read only the |
| 165 // first one because WebTextCheckingResult can store only one suggestion. | 165 // first one because SpellCheckResult can store only one suggestion. |
| 166 DictionaryValue* misspelling = NULL; | 166 DictionaryValue* misspelling = NULL; |
| 167 if (!misspellings->GetDictionary(i, &misspelling)) | 167 if (!misspellings->GetDictionary(i, &misspelling)) |
| 168 return false; | 168 return false; |
| 169 | 169 |
| 170 int start = 0; | 170 int start = 0; |
| 171 int length = 0; | 171 int length = 0; |
| 172 ListValue* suggestions = NULL; | 172 ListValue* suggestions = NULL; |
| 173 if (!misspelling->GetInteger("charStart", &start) || | 173 if (!misspelling->GetInteger("charStart", &start) || |
| 174 !misspelling->GetInteger("charLength", &length) || | 174 !misspelling->GetInteger("charLength", &length) || |
| 175 !misspelling->GetList("suggestions", &suggestions)) { | 175 !misspelling->GetList("suggestions", &suggestions)) { |
| 176 return false; | 176 return false; |
| 177 } | 177 } |
| 178 | 178 |
| 179 DictionaryValue* suggestion = NULL; | 179 DictionaryValue* suggestion = NULL; |
| 180 string16 replacement; | 180 string16 replacement; |
| 181 if (!suggestions->GetDictionary(0, &suggestion) || | 181 if (!suggestions->GetDictionary(0, &suggestion) || |
| 182 !suggestion->GetString("suggestion", &replacement)) { | 182 !suggestion->GetString("suggestion", &replacement)) { |
| 183 return false; | 183 return false; |
| 184 } | 184 } |
| 185 WebKit::WebTextCheckingResult result( | 185 SpellCheckResult result( |
| 186 WebKit::WebTextCheckingTypeSpelling, start, length, replacement); | 186 SpellCheckResult::Spelling, start, length, replacement); |
| 187 results->push_back(result); | 187 results->push_back(result); |
| 188 } | 188 } |
| 189 return true; | 189 return true; |
| 190 } | 190 } |
| OLD | NEW |