[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_ |
| 7 | #pragma once |
| 8 | |
| 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/string16.h" |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 16 | #include "net/url_request/url_fetcher_delegate.h" |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 17 | |
[email protected] | 80fe2b36 | 2012-05-02 04:19:38 | [diff] [blame] | 18 | class GURL; |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 19 | class Profile; |
| 20 | class TextCheckClientDelegate; |
[email protected] | 7ed49c2 | 2012-02-17 02:14:53 | [diff] [blame] | 21 | struct SpellCheckResult; |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 22 | |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 23 | namespace net { |
| 24 | class URLFetcher; |
| 25 | } // namespace net |
| 26 | |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 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, |
[email protected] | 80fe2b36 | 2012-05-02 04:19:38 | [diff] [blame] | 42 | // bool success, |
[email protected] | 7ed49c2 | 2012-02-17 02:14:53 | [diff] [blame] | 43 | // const std::vector<SpellCheckResult>& results) { |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 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 | // |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 57 | class SpellingServiceClient : public net::URLFetcherDelegate { |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 58 | public: |
[email protected] | 182bdf0 | 2012-04-07 01:40:41 | [diff] [blame] | 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 | }; |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 69 | typedef base::Callback<void( |
| 70 | int /* tag */, |
[email protected] | 80fe2b36 | 2012-05-02 04:19:38 | [diff] [blame] | 71 | bool /* success */, |
[email protected] | 8ca27e63 | 2012-05-28 07:39:35 | [diff] [blame] | 72 | const string16& /* text */, |
[email protected] | 7ed49c2 | 2012-02-17 02:14:53 | [diff] [blame] | 73 | const std::vector<SpellCheckResult>& /* results */)> |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 74 | TextCheckCompleteCallback; |
| 75 | |
| 76 | SpellingServiceClient(); |
| 77 | virtual ~SpellingServiceClient(); |
| 78 | |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 79 | // net::URLFetcherDelegate implementation. |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 80 | virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 81 | |
| 82 | // Sends a text-check request to the Spelling service. When we send a request |
| 83 | // to the Spelling service successfully, this function returns true. (This |
| 84 | // does not mean the service finishes checking text successfully.) We will |
| 85 | // call |callback| when we receive a text-check response from the service. |
| 86 | bool RequestTextCheck(Profile* profile, |
| 87 | int tag, |
[email protected] | 182bdf0 | 2012-04-07 01:40:41 | [diff] [blame] | 88 | ServiceType type, |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 89 | const string16& text, |
| 90 | const TextCheckCompleteCallback& callback); |
| 91 | |
[email protected] | 12cc3653 | 2012-06-14 05:38:04 | [diff] [blame^] | 92 | // Returns whether the specified service is available for the given profile. |
| 93 | static bool IsAvailable(Profile* profile, ServiceType type); |
| 94 | |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 95 | private: |
[email protected] | 80fe2b36 | 2012-05-02 04:19:38 | [diff] [blame] | 96 | // Creates a URLFetcher object used for sending a JSON-RPC request. This |
| 97 | // function is overriden by unit tests to prevent them from actually sending |
| 98 | // requests to the Spelling service. |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 99 | virtual net::URLFetcher* CreateURLFetcher(const GURL& url); |
[email protected] | 80fe2b36 | 2012-05-02 04:19:38 | [diff] [blame] | 100 | |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 101 | // Parses a JSON-RPC response from the Spelling service. |
| 102 | bool ParseResponse(const std::string& data, |
[email protected] | 7ed49c2 | 2012-02-17 02:14:53 | [diff] [blame] | 103 | std::vector<SpellCheckResult>* results); |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 104 | |
| 105 | // The URLFetcher object used for sending a JSON-RPC request. |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 106 | scoped_ptr<net::URLFetcher> fetcher_; |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 107 | |
| 108 | // The callback function to be called when we receive a response from the |
| 109 | // Spelling service and parse it. |
| 110 | TextCheckCompleteCallback callback_; |
| 111 | |
[email protected] | 8ca27e63 | 2012-05-28 07:39:35 | [diff] [blame] | 112 | // The text checked by the Spelling service. |
| 113 | string16 text_; |
| 114 | |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 115 | // The identifier provided by users so they can identify a text-check request. |
| 116 | // When a JSON-RPC call finishes successfully, this value is used as the |
| 117 | // first parameter to |callback_|. |
| 118 | int tag_; |
| 119 | }; |
| 120 | |
| 121 | #endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_ |