| 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 | 7 |
| 8 #include <map> | 8 #include <map> |
| 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/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
| 17 | 17 |
| 18 class GURL; | 18 class GURL; |
| 19 class Profile; | |
| 20 class TextCheckClientDelegate; | 19 class TextCheckClientDelegate; |
| 21 struct SpellCheckResult; | 20 struct SpellCheckResult; |
| 22 | 21 |
| 22 namespace content { |
| 23 class BrowserContext; |
| 24 } |
| 25 |
| 23 namespace net { | 26 namespace net { |
| 24 class URLFetcher; | 27 class URLFetcher; |
| 25 } // namespace net | 28 } // namespace net |
| 26 | 29 |
| 27 // A class that encapsulates a JSON-RPC call to the Spelling service to check | 30 // 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 | 31 // 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 | 32 // 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 | 33 // 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 | 34 // 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 | 35 // callback method. A simple usage is creating a SpellingServiceClient and |
| 33 // calling its RequestTextCheck method as listed in the following snippet. | 36 // calling its RequestTextCheck method as listed in the following snippet. |
| 34 // | 37 // |
| 35 // class MyClient { | 38 // class MyClient { |
| 36 // public: | 39 // public: |
| 37 // MyClient(); | 40 // MyClient(); |
| 38 // virtual ~MyClient(); | 41 // virtual ~MyClient(); |
| 39 // | 42 // |
| 40 // void OnTextCheckComplete( | 43 // void OnTextCheckComplete( |
| 41 // int tag, | 44 // int tag, |
| 42 // bool success, | 45 // bool success, |
| 43 // const std::vector<SpellCheckResult>& results) { | 46 // const std::vector<SpellCheckResult>& results) { |
| 44 // ... | 47 // ... |
| 45 // } | 48 // } |
| 46 // | 49 // |
| 47 // void MyTextCheck(Profile* profile, const string16& text) { | 50 // void MyTextCheck(BrowserContext* context, const string16& text) { |
| 48 // client_.reset(new SpellingServiceClient); | 51 // client_.reset(new SpellingServiceClient); |
| 49 // client_->RequestTextCheck(profile, 0, text, | 52 // client_->RequestTextCheck(context, 0, text, |
| 50 // base::Bind(&MyClient::OnTextCheckComplete, | 53 // base::Bind(&MyClient::OnTextCheckComplete, |
| 51 // base::Unretained(this)); | 54 // base::Unretained(this)); |
| 52 // } | 55 // } |
| 53 // private: | 56 // private: |
| 54 // scoped_ptr<SpellingServiceClient> client_; | 57 // scoped_ptr<SpellingServiceClient> client_; |
| 55 // }; | 58 // }; |
| 56 // | 59 // |
| 57 class SpellingServiceClient : public net::URLFetcherDelegate { | 60 class SpellingServiceClient : public net::URLFetcherDelegate { |
| 58 public: | 61 public: |
| 59 // Service types provided by the Spelling service. The Spelling service | 62 // Service types provided by the Spelling service. The Spelling service |
| (...skipping 12 matching lines...) Expand all Loading... |
| 72 const std::vector<SpellCheckResult>& /* results */)> | 75 const std::vector<SpellCheckResult>& /* results */)> |
| 73 TextCheckCompleteCallback; | 76 TextCheckCompleteCallback; |
| 74 | 77 |
| 75 SpellingServiceClient(); | 78 SpellingServiceClient(); |
| 76 virtual ~SpellingServiceClient(); | 79 virtual ~SpellingServiceClient(); |
| 77 | 80 |
| 78 // 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 |
| 79 // to the Spelling service successfully, this function returns true. (This | 82 // to the Spelling service successfully, this function returns true. (This |
| 80 // does not mean the service finishes checking text successfully.) We will | 83 // does not mean the service finishes checking text successfully.) We will |
| 81 // 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. |
| 82 bool RequestTextCheck(Profile* profile, | 85 bool RequestTextCheck(content::BrowserContext* context, |
| 83 ServiceType type, | 86 ServiceType type, |
| 84 const string16& text, | 87 const string16& text, |
| 85 const TextCheckCompleteCallback& callback); | 88 const TextCheckCompleteCallback& callback); |
| 86 | 89 |
| 87 // Returns whether the specified service is available for the given profile. | 90 // Returns whether the specified service is available for the given context. |
| 88 static bool IsAvailable(Profile* profile, ServiceType type); | 91 static bool IsAvailable(content::BrowserContext* context, ServiceType type); |
| 89 | 92 |
| 90 protected: | 93 protected: |
| 91 // Parses a JSON-RPC response from the Spelling service. | 94 // Parses a JSON-RPC response from the Spelling service. |
| 92 bool ParseResponse(const std::string& data, | 95 bool ParseResponse(const std::string& data, |
| 93 std::vector<SpellCheckResult>* results); | 96 std::vector<SpellCheckResult>* results); |
| 94 | 97 |
| 95 private: | 98 private: |
| 96 struct TextCheckCallbackData { | 99 struct TextCheckCallbackData { |
| 97 TextCheckCallbackData(TextCheckCompleteCallback callback, string16 text); | 100 TextCheckCallbackData(TextCheckCompleteCallback callback, string16 text); |
| 98 ~TextCheckCallbackData(); | 101 ~TextCheckCallbackData(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 111 // Creates a URLFetcher object used for sending a JSON-RPC request. This | 114 // 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 | 115 // function is overridden by unit tests to prevent them from actually sending |
| 113 // requests to the Spelling service. | 116 // requests to the Spelling service. |
| 114 virtual net::URLFetcher* CreateURLFetcher(const GURL& url); | 117 virtual net::URLFetcher* CreateURLFetcher(const GURL& url); |
| 115 | 118 |
| 116 // The URLFetcher object used for sending a JSON-RPC request. | 119 // The URLFetcher object used for sending a JSON-RPC request. |
| 117 std::map<const net::URLFetcher*, TextCheckCallbackData*> spellcheck_fetchers_; | 120 std::map<const net::URLFetcher*, TextCheckCallbackData*> spellcheck_fetchers_; |
| 118 }; | 121 }; |
| 119 | 122 |
| 120 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_ | 123 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_ |
| OLD | NEW |