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