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 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_ 6#define CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_ 7 8#include <map> 9#include <string> 10#include <vector> 11 12#include "base/callback.h" 13#include "base/compiler_specific.h" 14#include "base/memory/scoped_ptr.h" 15#include "base/strings/string16.h" 16#include "net/url_request/url_fetcher_delegate.h" 17 18class GURL; 19class Profile; 20class TextCheckClientDelegate; 21struct SpellCheckResult; 22 23namespace net { 24class URLFetcher; 25} // namespace net 26 27// A class that encapsulates a JSON-RPC call to the Spelling service to check 28// text there. This class creates a JSON-RPC request, sends the request to the 29// service with URLFetcher, parses a response from the service, and calls a 30// provided callback method. When a user deletes this object before it finishes 31// a JSON-RPC call, this class cancels the JSON-RPC call without calling the 32// callback method. A simple usage is creating a SpellingServiceClient and 33// calling its RequestTextCheck method as listed in the following snippet. 34// 35// class MyClient { 36// public: 37// MyClient(); 38// virtual ~MyClient(); 39// 40// void OnTextCheckComplete( 41// int tag, 42// bool success, 43// const std::vector<SpellCheckResult>& results) { 44// ... 45// } 46// 47// void MyTextCheck(Profile* profile, const string16& text) { 48// client_.reset(new SpellingServiceClient); 49// client_->RequestTextCheck(profile, 0, text, 50// base::Bind(&MyClient::OnTextCheckComplete, 51// base::Unretained(this)); 52// } 53// private: 54// scoped_ptr<SpellingServiceClient> client_; 55// }; 56// 57class SpellingServiceClient : public net::URLFetcherDelegate { 58 public: 59 // Service types provided by the Spelling service. The Spelling service 60 // consists of a couple of backends: 61 // * SUGGEST: Retrieving suggestions for a word (used by Google Search), and; 62 // * SPELLCHECK: Spellchecking text (used by Google Docs). 63 // This type is used for choosing a backend when sending a JSON-RPC request to 64 // the service. 65 enum ServiceType { 66 SUGGEST = 1, 67 SPELLCHECK = 2, 68 }; 69 typedef base::Callback<void( 70 bool /* success */, 71 const string16& /* text */, 72 const std::vector<SpellCheckResult>& /* results */)> 73 TextCheckCompleteCallback; 74 75 SpellingServiceClient(); 76 virtual ~SpellingServiceClient(); 77 78 // Sends a text-check request to the Spelling service. When we send a request 79 // to the Spelling service successfully, this function returns true. (This 80 // does not mean the service finishes checking text successfully.) We will 81 // call |callback| when we receive a text-check response from the service. 82 bool RequestTextCheck(Profile* profile, 83 ServiceType type, 84 const string16& text, 85 const TextCheckCompleteCallback& callback); 86 87 // Returns whether the specified service is available for the given profile. 88 static bool IsAvailable(Profile* profile, ServiceType type); 89 90 protected: 91 // Parses a JSON-RPC response from the Spelling service. 92 bool ParseResponse(const std::string& data, 93 std::vector<SpellCheckResult>* results); 94 95 private: 96 struct TextCheckCallbackData { 97 TextCheckCallbackData(TextCheckCompleteCallback callback, string16 text); 98 ~TextCheckCallbackData(); 99 100 // The callback function to be called when we receive a response from the 101 // Spelling service and parse it. 102 TextCheckCompleteCallback callback; 103 104 // The text checked by the Spelling service. 105 string16 text; 106 }; 107 108 // net::URLFetcherDelegate implementation. 109 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 110 111 // Creates a URLFetcher object used for sending a JSON-RPC request. This 112 // function is overridden by unit tests to prevent them from actually sending 113 // requests to the Spelling service. 114 virtual net::URLFetcher* CreateURLFetcher(const GURL& url); 115 116 // The URLFetcher object used for sending a JSON-RPC request. 117 std::map<const net::URLFetcher*, TextCheckCallbackData*> spellcheck_fetchers_; 118}; 119 120#endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_ 121