/chromium-webcl/src/chrome/browser/extensions/api/input_ime/input_ime_api.h

https://bitbucket.org/peixuan/chromium_r197479_base · C Header · 223 lines · 152 code · 54 blank · 17 comment · 0 complexity · 21dade3ce1af2f60a1f5ddf9add0a550 MD5 · raw file

  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. #ifndef CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_
  5. #define CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/singleton.h"
  10. #include "base/values.h"
  11. #include "chrome/browser/chromeos/input_method/input_method_engine.h"
  12. #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
  13. #include "chrome/browser/extensions/extension_function.h"
  14. #include "chrome/browser/profiles/profile_keyed_service.h"
  15. #include "chrome/common/extensions/api/input_ime/input_components_handler.h"
  16. #include "chrome/common/extensions/extension.h"
  17. #include "content/public/browser/notification_observer.h"
  18. #include "content/public/browser/notification_registrar.h"
  19. class Profile;
  20. namespace chromeos {
  21. class InputMethodEngine;
  22. class ImeObserver;
  23. }
  24. namespace extensions {
  25. class InputImeEventRouter {
  26. public:
  27. static InputImeEventRouter* GetInstance();
  28. bool RegisterIme(Profile* profile,
  29. const std::string& extension_id,
  30. const extensions::InputComponentInfo& component);
  31. void UnregisterAllImes(Profile* profile, const std::string& extension_id);
  32. chromeos::InputMethodEngine* GetEngine(const std::string& extension_id,
  33. const std::string& engine_id);
  34. chromeos::InputMethodEngine* GetActiveEngine(const std::string& extension_id);
  35. // Called when a key event was handled.
  36. void OnKeyEventHandled(const std::string& extension_id,
  37. const std::string& request_id,
  38. bool handled);
  39. std::string AddRequest(const std::string& engine_id,
  40. chromeos::input_method::KeyEventHandle* key_data);
  41. private:
  42. friend struct DefaultSingletonTraits<InputImeEventRouter>;
  43. typedef std::map<std::string, std::pair<std::string,
  44. chromeos::input_method::KeyEventHandle*> > RequestMap;
  45. InputImeEventRouter();
  46. ~InputImeEventRouter();
  47. std::map<std::string, std::map<std::string, chromeos::InputMethodEngine*> >
  48. engines_;
  49. std::map<std::string, std::map<std::string, chromeos::ImeObserver*> >
  50. observers_;
  51. unsigned int next_request_id_;
  52. RequestMap request_map_;
  53. DISALLOW_COPY_AND_ASSIGN(InputImeEventRouter);
  54. };
  55. class SetCompositionFunction : public SyncExtensionFunction {
  56. public:
  57. DECLARE_EXTENSION_FUNCTION("input.ime.setComposition",
  58. INPUT_IME_SETCOMPOSITION)
  59. protected:
  60. virtual ~SetCompositionFunction() {}
  61. // ExtensionFunction:
  62. virtual bool RunImpl() OVERRIDE;
  63. };
  64. class ClearCompositionFunction : public SyncExtensionFunction {
  65. public:
  66. DECLARE_EXTENSION_FUNCTION("input.ime.clearComposition",
  67. INPUT_IME_CLEARCOMPOSITION)
  68. protected:
  69. virtual ~ClearCompositionFunction() {}
  70. // ExtensionFunction:
  71. virtual bool RunImpl() OVERRIDE;
  72. };
  73. class CommitTextFunction : public SyncExtensionFunction {
  74. public:
  75. DECLARE_EXTENSION_FUNCTION("input.ime.commitText", INPUT_IME_COMMITTEXT)
  76. protected:
  77. virtual ~CommitTextFunction() {}
  78. // ExtensionFunction:
  79. virtual bool RunImpl() OVERRIDE;
  80. };
  81. class SetCandidateWindowPropertiesFunction : public SyncExtensionFunction {
  82. public:
  83. DECLARE_EXTENSION_FUNCTION("input.ime.setCandidateWindowProperties",
  84. INPUT_IME_SETCANDIDATEWINDOWPROPERTIES)
  85. protected:
  86. virtual ~SetCandidateWindowPropertiesFunction() {}
  87. // ExtensionFunction:
  88. virtual bool RunImpl() OVERRIDE;
  89. };
  90. class SetCandidatesFunction : public SyncExtensionFunction {
  91. public:
  92. DECLARE_EXTENSION_FUNCTION("input.ime.setCandidates", INPUT_IME_SETCANDIDATES)
  93. protected:
  94. virtual ~SetCandidatesFunction() {}
  95. // ExtensionFunction:
  96. virtual bool RunImpl() OVERRIDE;
  97. private:
  98. bool ReadCandidates(
  99. ListValue* candidates,
  100. std::vector<chromeos::InputMethodEngine::Candidate>* output);
  101. };
  102. class SetCursorPositionFunction : public SyncExtensionFunction {
  103. public:
  104. DECLARE_EXTENSION_FUNCTION("input.ime.setCursorPosition",
  105. INPUT_IME_SETCURSORPOSITION)
  106. protected:
  107. virtual ~SetCursorPositionFunction() {}
  108. // ExtensionFunction:
  109. virtual bool RunImpl() OVERRIDE;
  110. };
  111. class SetMenuItemsFunction : public SyncExtensionFunction {
  112. public:
  113. DECLARE_EXTENSION_FUNCTION("input.ime.setMenuItems", INPUT_IME_SETMENUITEMS)
  114. protected:
  115. virtual ~SetMenuItemsFunction() {}
  116. // ExtensionFunction:
  117. virtual bool RunImpl() OVERRIDE;
  118. };
  119. class UpdateMenuItemsFunction : public SyncExtensionFunction {
  120. public:
  121. DECLARE_EXTENSION_FUNCTION("input.ime.updateMenuItems",
  122. INPUT_IME_UPDATEMENUITEMS)
  123. protected:
  124. virtual ~UpdateMenuItemsFunction() {}
  125. // ExtensionFunction:
  126. virtual bool RunImpl() OVERRIDE;
  127. };
  128. class DeleteSurroundingTextFunction : public SyncExtensionFunction {
  129. public:
  130. DECLARE_EXTENSION_FUNCTION("input.ime.deleteSurroundingText",
  131. INPUT_IME_DELETESURROUNDINGTEXT)
  132. protected:
  133. virtual ~DeleteSurroundingTextFunction() {}
  134. // ExtensionFunction:
  135. virtual bool RunImpl() OVERRIDE;
  136. };
  137. class KeyEventHandled : public AsyncExtensionFunction {
  138. public:
  139. DECLARE_EXTENSION_FUNCTION("input.ime.keyEventHandled",
  140. INPUT_IME_KEYEVENTHANDLED)
  141. protected:
  142. virtual ~KeyEventHandled() {}
  143. // ExtensionFunction:
  144. virtual bool RunImpl() OVERRIDE;
  145. };
  146. class InputImeAPI : public ProfileKeyedAPI,
  147. public content::NotificationObserver {
  148. public:
  149. explicit InputImeAPI(Profile* profile);
  150. virtual ~InputImeAPI();
  151. // ProfileKeyedAPI implementation.
  152. static ProfileKeyedAPIFactory<InputImeAPI>* GetFactoryInstance();
  153. // content::NotificationObserver implementation.
  154. virtual void Observe(int type,
  155. const content::NotificationSource& source,
  156. const content::NotificationDetails& details) OVERRIDE;
  157. private:
  158. friend class ProfileKeyedAPIFactory<InputImeAPI>;
  159. InputImeEventRouter* input_ime_event_router();
  160. // ProfileKeyedAPI implementation.
  161. static const char* service_name() {
  162. return "InputImeAPI";
  163. }
  164. static const bool kServiceIsNULLWhileTesting = true;
  165. Profile* const profile_;
  166. content::NotificationRegistrar registrar_;
  167. };
  168. } // namespace extensions
  169. #endif // CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_