Re-enable child account detection, now behind a flag.
[chromium-blink-merge.git] / chrome / browser / renderer_context_menu / spelling_menu_observer.h
blobe79b1e09bf488ea073257870aab8101948c765a1
1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_RENDERER_CONTEXT_MENU_SPELLING_MENU_OBSERVER_H_
6 #define CHROME_BROWSER_RENDERER_CONTEXT_MENU_SPELLING_MENU_OBSERVER_H_
8 #include <vector>
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/prefs/pref_member.h"
13 #include "base/strings/string16.h"
14 #include "base/timer/timer.h"
15 #include "chrome/browser/spellchecker/spelling_service_client.h"
16 #include "components/renderer_context_menu/render_view_context_menu_observer.h"
18 class RenderViewContextMenuProxy;
19 struct SpellCheckResult;
21 // An observer that listens to events from the RenderViewContextMenu class and
22 // shows suggestions from the Spelling ("do you mean") service to a context menu
23 // while we show it. This class implements two interfaces:
24 // * RenderViewContextMenuObserver
25 // This interface is used for adding a menu item and update it while showing.
26 // * net::URLFetcherDelegate
27 // This interface is used for sending a JSON_RPC request to the Spelling
28 // service and retrieving its response.
29 // These interfaces allow this class to make a JSON-RPC call to the Spelling
30 // service in the background and update the context menu while showing. The
31 // following snippet describes how to add this class to the observer list of the
32 // RenderViewContextMenu class.
34 // void RenderViewContextMenu::InitMenu() {
35 // spelling_menu_observer_.reset(new SpellingMenuObserver(this));
36 // if (spelling_menu_observer_.get())
37 // observers_.AddObserver(spelling_menu_observer.get());
38 // }
40 class SpellingMenuObserver : public RenderViewContextMenuObserver {
41 public:
42 explicit SpellingMenuObserver(RenderViewContextMenuProxy* proxy);
43 ~SpellingMenuObserver() override;
45 // RenderViewContextMenuObserver implementation.
46 void InitMenu(const content::ContextMenuParams& params) override;
47 bool IsCommandIdSupported(int command_id) override;
48 bool IsCommandIdChecked(int command_id) override;
49 bool IsCommandIdEnabled(int command_id) override;
50 void ExecuteCommand(int command_id) override;
52 // A callback function called when the Spelling service finishes checking a
53 // misspelled word.
54 void OnTextCheckComplete(
55 SpellingServiceClient::ServiceType type,
56 bool success,
57 const base::string16& text,
58 const std::vector<SpellCheckResult>& results);
60 private:
61 // The callback function for base::RepeatingTimer<SpellingMenuClient>. This
62 // function updates the "loading..." animation in the context-menu item.
63 void OnAnimationTimerExpired();
65 // The interface to add a context-menu item and update it. This class uses
66 // this interface to avoid accesing context-menu items directly.
67 RenderViewContextMenuProxy* proxy_;
69 // Suggested words from the local spellchecker. If the spelling service
70 // returns a word in this list, we hide the context-menu item to prevent
71 // showing the same word twice.
72 std::vector<base::string16> suggestions_;
74 // The string used for animation until we receive a response from the Spelling
75 // service. The current animation just adds periods at the end of this string:
76 // 'Loading' -> 'Loading.' -> 'Loading..' -> 'Loading...' (-> 'Loading')
77 base::string16 loading_message_;
78 size_t loading_frame_;
80 // A flag represending whether a JSON-RPC call to the Spelling service
81 // finished successfully and its response had a suggestion not included in the
82 // ones provided by the local spellchecker. When this flag is true, we enable
83 // the context-menu item so users can choose it.
84 bool succeeded_;
86 // The misspelled word. When we choose the "Add to dictionary" item, we add
87 // this word to the custom-word dictionary.
88 base::string16 misspelled_word_;
90 // The string representing the result of this call. This string is a
91 // suggestion when this call finished successfully. Otherwise it is error
92 // text. Until we receive a response from the Spelling service, this string
93 // stores the input string. (Since the Spelling service sends only misspelled
94 // words, we replace these misspelled words in the input text with the
95 // suggested words to create suggestion text.
96 base::string16 result_;
98 // The URLFetcher object used for sending a JSON-RPC request.
99 scoped_ptr<SpellingServiceClient> client_;
101 // A timer used for loading animation.
102 base::RepeatingTimer<SpellingMenuObserver> animation_timer_;
104 // Flag indicating whether online spelling correction service is enabled. When
105 // this variable is true and we right-click a misspelled word, we send a
106 // JSON-RPC request to the service and retrieve suggestions.
107 BooleanPrefMember integrate_spelling_service_;
109 // Flag indicating whether automatic spelling correction is enabled.
110 BooleanPrefMember autocorrect_spelling_;
112 DISALLOW_COPY_AND_ASSIGN(SpellingMenuObserver);
115 #endif // CHROME_BROWSER_RENDERER_CONTEXT_MENU_SPELLING_MENU_OBSERVER_H_