| 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 "content/public/common/url_fetcher_delegate.h" |
| 17 | 17 |
| 18 class Profile; | 18 class Profile; |
| 19 class TextCheckClientDelegate; | 19 class TextCheckClientDelegate; |
| 20 | 20 struct SpellCheckResult; |
| 21 namespace WebKit { | |
| 22 struct WebTextCheckingResult; | |
| 23 } | |
| 24 | 21 |
| 25 // A class that encapsulates a JSON-RPC call to the Spelling service to check | 22 // A class that encapsulates a JSON-RPC call to the Spelling service to check |
| 26 // text there. This class creates a JSON-RPC request, sends the request to the | 23 // text there. This class creates a JSON-RPC request, sends the request to the |
| 27 // service with URLFetcher, parses a response from the service, and calls a | 24 // service with URLFetcher, parses a response from the service, and calls a |
| 28 // provided callback method. When a user deletes this object before it finishes | 25 // provided callback method. When a user deletes this object before it finishes |
| 29 // a JSON-RPC call, this class cancels the JSON-RPC call without calling the | 26 // a JSON-RPC call, this class cancels the JSON-RPC call without calling the |
| 30 // callback method. A simple usage is creating a SpellingServiceClient and | 27 // callback method. A simple usage is creating a SpellingServiceClient and |
| 31 // calling its RequestTextCheck method as listed in the following snippet. | 28 // calling its RequestTextCheck method as listed in the following snippet. |
| 32 // | 29 // |
| 33 // class MyClient { | 30 // class MyClient { |
| 34 // public: | 31 // public: |
| 35 // MyClient(); | 32 // MyClient(); |
| 36 // virtual ~MyClient(); | 33 // virtual ~MyClient(); |
| 37 // | 34 // |
| 38 // void OnTextCheckComplete( | 35 // void OnTextCheckComplete( |
| 39 // int tag, | 36 // int tag, |
| 40 // const std::vector<WebKit::WebTextCheckingResult>& results) { | 37 // const std::vector<SpellCheckResult>& results) { |
| 41 // ... | 38 // ... |
| 42 // } | 39 // } |
| 43 // | 40 // |
| 44 // void MyTextCheck(Profile* profile, const string16& text) { | 41 // void MyTextCheck(Profile* profile, const string16& text) { |
| 45 // client_.reset(new SpellingServiceClient); | 42 // client_.reset(new SpellingServiceClient); |
| 46 // client_->RequestTextCheck(profile, 0, text, | 43 // client_->RequestTextCheck(profile, 0, text, |
| 47 // base::Bind(&MyClient::OnTextCheckComplete, | 44 // base::Bind(&MyClient::OnTextCheckComplete, |
| 48 // base::Unretained(this)); | 45 // base::Unretained(this)); |
| 49 // } | 46 // } |
| 50 // private: | 47 // private: |
| 51 // scoped_ptr<SpellingServiceClient> client_; | 48 // scoped_ptr<SpellingServiceClient> client_; |
| 52 // }; | 49 // }; |
| 53 // | 50 // |
| 54 class SpellingServiceClient : public content::URLFetcherDelegate { | 51 class SpellingServiceClient : public content::URLFetcherDelegate { |
| 55 public: | 52 public: |
| 56 typedef base::Callback<void( | 53 typedef base::Callback<void( |
| 57 int /* tag */, | 54 int /* tag */, |
| 58 const std::vector<WebKit::WebTextCheckingResult>& /* results */)> | 55 const std::vector<SpellCheckResult>& /* results */)> |
| 59 TextCheckCompleteCallback; | 56 TextCheckCompleteCallback; |
| 60 | 57 |
| 61 SpellingServiceClient(); | 58 SpellingServiceClient(); |
| 62 virtual ~SpellingServiceClient(); | 59 virtual ~SpellingServiceClient(); |
| 63 | 60 |
| 64 // content::URLFetcherDelegate implementation. | 61 // content::URLFetcherDelegate implementation. |
| 65 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | 62 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 66 | 63 |
| 67 // Sends a text-check request to the Spelling service. When we send a request | 64 // Sends a text-check request to the Spelling service. When we send a request |
| 68 // to the Spelling service successfully, this function returns true. (This | 65 // to the Spelling service successfully, this function returns true. (This |
| 69 // does not mean the service finishes checking text successfully.) We will | 66 // does not mean the service finishes checking text successfully.) We will |
| 70 // call |callback| when we receive a text-check response from the service. | 67 // call |callback| when we receive a text-check response from the service. |
| 71 bool RequestTextCheck(Profile* profile, | 68 bool RequestTextCheck(Profile* profile, |
| 72 int tag, | 69 int tag, |
| 73 const string16& text, | 70 const string16& text, |
| 74 const TextCheckCompleteCallback& callback); | 71 const TextCheckCompleteCallback& callback); |
| 75 | 72 |
| 76 private: | 73 private: |
| 77 // Parses a JSON-RPC response from the Spelling service. | 74 // Parses a JSON-RPC response from the Spelling service. |
| 78 bool ParseResponse(const std::string& data, | 75 bool ParseResponse(const std::string& data, |
| 79 std::vector<WebKit::WebTextCheckingResult>* results); | 76 std::vector<SpellCheckResult>* results); |
| 80 | 77 |
| 81 // The URLFetcher object used for sending a JSON-RPC request. | 78 // The URLFetcher object used for sending a JSON-RPC request. |
| 82 scoped_ptr<content::URLFetcher> fetcher_; | 79 scoped_ptr<content::URLFetcher> fetcher_; |
| 83 | 80 |
| 84 // The callback function to be called when we receive a response from the | 81 // The callback function to be called when we receive a response from the |
| 85 // Spelling service and parse it. | 82 // Spelling service and parse it. |
| 86 TextCheckCompleteCallback callback_; | 83 TextCheckCompleteCallback callback_; |
| 87 | 84 |
| 88 // The identifier provided by users so they can identify a text-check request. | 85 // The identifier provided by users so they can identify a text-check request. |
| 89 // When a JSON-RPC call finishes successfully, this value is used as the | 86 // When a JSON-RPC call finishes successfully, this value is used as the |
| 90 // first parameter to |callback_|. | 87 // first parameter to |callback_|. |
| 91 int tag_; | 88 int tag_; |
| 92 }; | 89 }; |
| 93 | 90 |
| 94 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_ | 91 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_ |
| OLD | NEW |