Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: chrome/browser/spellchecker/spelling_service_client.h

Issue 2244083002: Componentize spellcheck [4]: spellcheck/browser and android java-side. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/strings/string16.h"
16 #include "net/url_request/url_fetcher_delegate.h"
17
18 class GURL;
19 class TextCheckClientDelegate;
20 struct SpellCheckResult;
21
22 namespace content {
23 class BrowserContext;
24 }
25
26 namespace net {
27 class URLFetcher;
28 } // namespace net
29
30 // A class that encapsulates a JSON-RPC call to the Spelling service to check
31 // text there. This class creates a JSON-RPC request, sends the request to the
32 // service with URLFetcher, parses a response from the service, and calls a
33 // provided callback method. When a user deletes this object before it finishes
34 // a JSON-RPC call, this class cancels the JSON-RPC call without calling the
35 // callback method. A simple usage is creating a SpellingServiceClient and
36 // calling its RequestTextCheck method as listed in the following snippet.
37 //
38 // class MyClient {
39 // public:
40 // MyClient();
41 // virtual ~MyClient();
42 //
43 // void OnTextCheckComplete(
44 // int tag,
45 // bool success,
46 // const std::vector<SpellCheckResult>& results) {
47 // ...
48 // }
49 //
50 // void MyTextCheck(BrowserContext* context, const base::string16& text) {
51 // client_.reset(new SpellingServiceClient);
52 // client_->RequestTextCheck(context, 0, text,
53 // base::Bind(&MyClient::OnTextCheckComplete,
54 // base::Unretained(this));
55 // }
56 // private:
57 // std::unique_ptr<SpellingServiceClient> client_;
58 // };
59 //
60 class SpellingServiceClient : public net::URLFetcherDelegate {
61 public:
62 // Service types provided by the Spelling service. The Spelling service
63 // consists of a couple of backends:
64 // * SUGGEST: Retrieving suggestions for a word (used by Google Search), and;
65 // * SPELLCHECK: Spellchecking text (used by Google Docs).
66 // This type is used for choosing a backend when sending a JSON-RPC request to
67 // the service.
68 enum ServiceType {
69 SUGGEST = 1,
70 SPELLCHECK = 2,
71 };
72 typedef base::Callback<void(
73 bool /* success */,
74 const base::string16& /* text */,
75 const std::vector<SpellCheckResult>& /* results */)>
76 TextCheckCompleteCallback;
77
78 SpellingServiceClient();
79 ~SpellingServiceClient() override;
80
81 // Sends a text-check request to the Spelling service. When we send a request
82 // to the Spelling service successfully, this function returns true. (This
83 // does not mean the service finishes checking text successfully.) We will
84 // call |callback| when we receive a text-check response from the service.
85 bool RequestTextCheck(content::BrowserContext* context,
86 ServiceType type,
87 const base::string16& text,
88 const TextCheckCompleteCallback& callback);
89
90 // Returns whether the specified service is available for the given context.
91 static bool IsAvailable(content::BrowserContext* context, ServiceType type);
92
93 protected:
94 // Parses a JSON-RPC response from the Spelling service.
95 bool ParseResponse(const std::string& data,
96 std::vector<SpellCheckResult>* results);
97
98 private:
99 struct TextCheckCallbackData {
100 TextCheckCallbackData(TextCheckCompleteCallback callback,
101 base::string16 text);
102 ~TextCheckCallbackData();
103
104 // The callback function to be called when we receive a response from the
105 // Spelling service and parse it.
106 TextCheckCompleteCallback callback;
107
108 // The text checked by the Spelling service.
109 base::string16 text;
110 };
111
112 // net::URLFetcherDelegate implementation.
113 void OnURLFetchComplete(const net::URLFetcher* source) override;
114
115 // Creates a URLFetcher object used for sending a JSON-RPC request. This
116 // function is overridden by unit tests to prevent them from actually sending
117 // requests to the Spelling service.
118 virtual std::unique_ptr<net::URLFetcher> CreateURLFetcher(const GURL& url);
119
120 // The URLFetcher object used for sending a JSON-RPC request.
121 std::map<const net::URLFetcher*, TextCheckCallbackData*> spellcheck_fetchers_;
122 };
123
124 #endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698