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