|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ | |
6 #define CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/string16.h" | |
13 #include "base/timer.h" | |
14 #include "chrome/browser/tab_contents/render_view_context_menu_observer.h" | |
15 #include "content/common/url_fetcher.h" | |
16 | |
17 class GURL; | |
18 class RenderViewContextMenuDelegate; | |
19 | |
20 // An observer that listens events from the RenderViewContextMenu class and show | |
Avi (use Gerrit)
2011/08/23 18:38:19
...listens to events... and shows...
Hironori Bono
2011/08/25 11:18:58
Done.
| |
21 // suggestions from the Spelling ("do you mean") service to a context menu while | |
22 // we show it. This class implements two interfaces: | |
23 // * RenderViewContextMenuObserver | |
24 // This interface is used for adding a menu item and update it while showing. | |
25 // * URLFethcer::Delegate | |
Avi (use Gerrit)
2011/08/23 18:38:19
URLFetcher
Hironori Bono
2011/08/25 11:18:58
Done.
| |
26 // This interface is used for sending a JSON_RPC request to the Spelling | |
27 // service and retrieving its response. | |
28 // these interfaces allows this class to make a JSON-RPC call to the Spelling | |
Avi (use Gerrit)
2011/08/23 18:38:19
Capitalize "these". allows->allow.
Hironori Bono
2011/08/25 11:18:58
Done.
| |
29 // service in the background and updates the context menu while showing. The | |
Avi (use Gerrit)
2011/08/23 18:38:19
s/updates/update/
Hironori Bono
2011/08/25 11:18:58
Done.
| |
30 // following snippet describes how to add this class to the observer list of the | |
31 // RenderViewContextMenu class. | |
32 // | |
33 // void RenderViewContextMenu::InitMenu() { | |
34 // spelling_menu_observer_.reset(new SpellingMenuObserver(this)); | |
35 // if (spelling_menu_observer_.get()) | |
36 // observers_.AddObserver(spelling_menu_observer.get()); | |
37 // } | |
38 // | |
39 class SpellingMenuObserver : public RenderViewContextMenuObserver, | |
40 public URLFetcher::Delegate { | |
41 public: | |
42 explicit SpellingMenuObserver(RenderViewContextMenuDelegate* delegate); | |
43 virtual ~SpellingMenuObserver(); | |
44 | |
45 // RenderViewContextMenuObserver implementation. | |
46 virtual void InitMenu(const ContextMenuParams& params) OVERRIDE; | |
47 virtual bool IsCommandIdSupported(int command_id) OVERRIDE; | |
48 virtual bool IsCommandIdEnabled(int command_id) OVERRIDE; | |
49 virtual void ExecuteCommand(int command_id) OVERRIDE; | |
50 | |
51 // URLFetcher::Delegate implementation. | |
52 virtual void OnURLFetchComplete(const URLFetcher* source, | |
53 const GURL& url, | |
54 const net::URLRequestStatus& status, | |
55 int response_code, | |
56 const net::ResponseCookies& cookies, | |
57 const std::string& data) OVERRIDE; | |
58 | |
59 private: | |
60 // Invokes a JSON-RPC call in the background. This function sends a JSON-RPC | |
61 // request to the Spelling servive. Chrome will call ParseResponse() when it | |
62 // receives the response from the service. | |
63 bool Invoke(const string16& text, | |
64 const std::string& locale, | |
65 net::URLRequestContextGetter* context); | |
66 | |
67 // Parses the specified response from the Spelling service. | |
68 bool ParseResponse(int code, const std::string& data); | |
69 | |
70 // The callback function for base::RepeatingTimer<SpellingMenuClient>. This | |
71 // function updates the animation in the context-menu item. | |
72 void OnAnimationTimerExpired(); | |
73 | |
74 // The interface to add a context-menu item and update it. This class uses | |
75 // this interface to avoid accesing context-menu items directly. | |
76 RenderViewContextMenuDelegate* delegate_; | |
77 | |
78 // The string used for animation until we receive a response from the Spelling | |
79 // service. | |
80 string16 loading_message_; | |
81 int loading_frame_; | |
82 | |
83 // A flag represending whether this call finished successfully. | |
84 bool succeeded_; | |
85 | |
86 // The string representing the result of this call. This string is a | |
87 // suggestion when this call finished successfully. Otherwise it is error | |
88 // text. Until we receive a response from the Spelling service, this string | |
89 // stores the input string. (Since the Spelling service sends only misspelled | |
90 // words, we replace these misspelled words in the input text with the | |
91 // suggested words to create suggestion text. | |
92 string16 result_; | |
93 | |
94 // The URLFetcher object used for sending a JSON-RPC request. | |
95 scoped_ptr<URLFetcher> fetcher_; | |
96 | |
97 // A timer used for loading animation. | |
98 base::RepeatingTimer<SpellingMenuObserver> animation_timer_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(SpellingMenuObserver); | |
101 }; | |
102 | |
103 #endif // CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ | |
OLD | NEW |