Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / input_ime / input_ime_api.cc
index a309e41..f37bac0 100644 (file)
@@ -6,18 +6,17 @@
 
 #include "base/strings/string_number_conversions.h"
 #include "base/values.h"
-#include "chrome/browser/chrome_notification_types.h"
-#include "chrome/browser/chromeos/input_method/input_method_engine.h"
 #include "chrome/browser/chromeos/profiles/profile_helper.h"
-#include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/profiles/profile_manager.h"
 #include "chrome/common/extensions/api/input_ime.h"
 #include "chrome/common/extensions/api/input_ime/input_components_handler.h"
-#include "content/public/browser/notification_details.h"
-#include "content/public/browser/notification_source.h"
 #include "extensions/browser/event_router.h"
 #include "extensions/browser/extension_function_registry.h"
-#include "extensions/browser/extension_system.h"
+#include "extensions/browser/extension_registry.h"
+
+#if defined(USE_X11)
+#include "chrome/browser/chromeos/input_method/input_method_engine.h"
+#endif
 
 namespace input_ime = extensions::api::input_ime;
 namespace KeyEventHandled = extensions::api::input_ime::KeyEventHandled;
@@ -77,8 +76,8 @@ static void DispatchEventToExtension(Profile* profile,
   scoped_ptr<extensions::Event> event(new extensions::Event(
       event_name, args.Pass()));
   event->restrict_to_browser_context = profile;
-  extensions::ExtensionSystem::Get(profile)->event_router()->
-      DispatchEventToExtension(extension_id, event.Pass());
+  extensions::EventRouter::Get(profile)
+      ->DispatchEventToExtension(extension_id, event.Pass());
 }
 
 }  // namespace
@@ -87,16 +86,7 @@ namespace chromeos {
 class ImeObserver : public InputMethodEngineInterface::Observer {
  public:
   ImeObserver(Profile* profile, const std::string& extension_id)
-      : profile_(profile), extension_id_(extension_id) {
-    // The original profile for login screen is called signin profile.
-    // And the active profile is the incognito profile based on signin profile.
-    // So if |profile| is signin profile, we need to make sure the
-    // observer runs under its incognito profile, because the component
-    // extensions were installed under its incognito profile.
-    if (ProfileHelper::IsSigninProfile(profile)) {
-      profile_ = ProfileManager::GetActiveUserProfile();
-    }
-  }
+      : profile_(profile), extension_id_(extension_id) {}
 
   virtual ~ImeObserver() {}
 
@@ -171,19 +161,24 @@ class ImeObserver : public InputMethodEngineInterface::Observer {
     if (profile_ == NULL || extension_id_.empty())
       return;
 
-    extensions::InputImeEventRouter* ime_event_router =
-        extensions::InputImeEventRouter::GetInstance();
-
-    const std::string request_id =
-        ime_event_router->AddRequest(engine_id, key_data);
-
     // If there is no listener for the event, no need to dispatch the event to
     // extension. Instead, releases the key event for default system behavior.
     if (!HasKeyEventListener()) {
-      ime_event_router->OnKeyEventHandled(extension_id_, request_id, false);
+      // Continue processing the key event so that the physical keyboard can
+      // still work.
+      base::Callback<void(bool consumed)>* callback =
+          reinterpret_cast<base::Callback<void(bool consumed)>*>(key_data);
+      callback->Run(false);
+      delete callback;
       return;
     }
 
+    extensions::InputImeEventRouter* ime_event_router =
+        extensions::InputImeEventRouter::GetInstance();
+
+    const std::string request_id =
+        ime_event_router->AddRequest(engine_id, key_data);
+
     input_ime::KeyboardEvent key_data_value;
     key_data_value.type = input_ime::KeyboardEvent::ParseType(event.type);
     key_data_value.request_id = request_id;
@@ -287,8 +282,7 @@ class ImeObserver : public InputMethodEngineInterface::Observer {
 
  private:
   bool HasKeyEventListener() const {
-    return extensions::ExtensionSystem::Get(profile_)
-        ->event_router()
+    return extensions::EventRouter::Get(profile_)
         ->ExtensionHasEventListener(extension_id_,
                                     input_ime::OnKeyEvent::kEventName);
   }
@@ -308,22 +302,12 @@ InputImeEventRouter::GetInstance() {
   return Singleton<InputImeEventRouter>::get();
 }
 
-#if defined(OS_CHROMEOS)
 bool InputImeEventRouter::RegisterIme(
-    Profile* profile,
     const std::string& extension_id,
     const extensions::InputComponentInfo& component) {
+#if defined(USE_X11)
   VLOG(1) << "RegisterIme: " << extension_id << " id: " << component.id;
 
-  // If the engine exists already, it may be registered with an old profile.
-  // So overwrite it with the new engine instance.
-  EngineMap& engine_map = engines_[extension_id];
-  EngineMap::iterator engine_ix = engine_map.find(component.id);
-  if (engine_ix != engine_map.end()) {
-    delete engine_ix->second;
-    engine_map.erase(engine_ix);
-  }
-
   std::vector<std::string> layouts;
   layouts.assign(component.layouts.begin(), component.layouts.end());
 
@@ -335,6 +319,7 @@ bool InputImeEventRouter::RegisterIme(
   // to maintain an internal map for observers which does nearly nothing
   // but just make sure they can properly deleted.
   // Making Obesrver per InputMethodEngine can make things cleaner.
+  Profile* profile = ProfileManager::GetActiveUserProfile();
   scoped_ptr<chromeos::InputMethodEngineInterface::Observer> observer(
       new chromeos::ImeObserver(profile, extension_id));
   chromeos::InputMethodEngine* engine = new chromeos::InputMethodEngine();
@@ -346,51 +331,69 @@ bool InputImeEventRouter::RegisterIme(
                      layouts,
                      component.options_page_url,
                      component.input_view_url);
-  engine_map[component.id] = engine;
+  profile_engine_map_[profile][extension_id][component.id] = engine;
 
   return true;
+#else
+  // TODO(spang): IME support under ozone.
+  NOTIMPLEMENTED();
+  return false;
+#endif
 }
 
-void InputImeEventRouter::UnregisterAllImes(
-    Profile* profile, const std::string& extension_id) {
-  std::map<std::string, EngineMap>::iterator engine_map =
-      engines_.find(extension_id);
-  if (engine_map != engines_.end()) {
-    STLDeleteContainerPairSecondPointers(engine_map->second.begin(),
-                                         engine_map->second.end());
-    engines_.erase(engine_map);
-  }
+void InputImeEventRouter::UnregisterAllImes(const std::string& extension_id) {
+  Profile* profile = ProfileManager::GetActiveUserProfile();
+  ProfileEngineMap::iterator extension_map =
+      profile_engine_map_.find(profile);
+  if (extension_map == profile_engine_map_.end())
+    return;
+  ExtensionMap::iterator engine_map = extension_map->second.find(extension_id);
+  if (engine_map == extension_map->second.end())
+    return;
+  STLDeleteContainerPairSecondPointers(engine_map->second.begin(),
+                                       engine_map->second.end());
+  extension_map->second.erase(extension_id);
+  profile_engine_map_.erase(profile);
 }
-#endif
 
 InputMethodEngineInterface* InputImeEventRouter::GetEngine(
     const std::string& extension_id, const std::string& engine_id) {
-  std::map<std::string,
-           std::map<std::string, InputMethodEngineInterface*> >::const_iterator
-               engine_list = engines_.find(extension_id);
-  if (engine_list != engines_.end()) {
-    std::map<std::string, InputMethodEngineInterface*>::const_iterator
-        engine_ix = engine_list->second.find(engine_id);
-    if (engine_ix != engine_list->second.end())
-      return engine_ix->second;
-  }
-  return NULL;
+  // IME can only work on active user profile.
+  Profile* profile = ProfileManager::GetActiveUserProfile();
+
+  ProfileEngineMap::const_iterator extension_map =
+      profile_engine_map_.find(profile);
+  if (extension_map == profile_engine_map_.end())
+    return NULL;
+  ExtensionMap::const_iterator engine_map =
+      extension_map->second.find(extension_id);
+  if (engine_map == extension_map->second.end())
+    return NULL;
+  EngineMap::const_iterator engine = engine_map->second.find(engine_id);
+  if (engine == engine_map->second.end())
+    return NULL;
+  return engine->second;
 }
 
 InputMethodEngineInterface* InputImeEventRouter::GetActiveEngine(
     const std::string& extension_id) {
-  std::map<std::string,
-           std::map<std::string, InputMethodEngineInterface*> >::const_iterator
-               engine_list = engines_.find(extension_id);
-  if (engine_list != engines_.end()) {
-    std::map<std::string, InputMethodEngineInterface*>::const_iterator
-        engine_ix;
-    for (engine_ix = engine_list->second.begin();
-         engine_ix != engine_list->second.end();
-         ++engine_ix) {
-      if (engine_ix->second->IsActive())
-        return engine_ix->second;
-    }
+  // IME can only work on active user profile.
+  Profile* profile = ProfileManager::GetActiveUserProfile();
+
+  ProfileEngineMap::const_iterator extension_map =
+      profile_engine_map_.find(profile);
+  if (extension_map == profile_engine_map_.end())
+    return NULL;
+  ExtensionMap::const_iterator engine_map =
+      extension_map->second.find(extension_id);
+  if (engine_map == extension_map->second.end())
+    return NULL;
+
+  for (EngineMap::const_iterator i = engine_map->second.begin();
+       i != engine_map->second.end();
+       ++i) {
+    if (i->second->IsActive())
+      return i->second;
   }
   return NULL;
 }
@@ -435,7 +438,7 @@ InputImeEventRouter::InputImeEventRouter()
 
 InputImeEventRouter::~InputImeEventRouter() {}
 
-bool InputImeSetCompositionFunction::RunImpl() {
+bool InputImeSetCompositionFunction::RunSync() {
   InputMethodEngineInterface* engine =
       InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id());
   if (!engine) {
@@ -481,7 +484,7 @@ bool InputImeSetCompositionFunction::RunImpl() {
   return true;
 }
 
-bool InputImeClearCompositionFunction::RunImpl() {
+bool InputImeClearCompositionFunction::RunSync() {
   InputMethodEngineInterface* engine =
       InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id());
   if (!engine) {
@@ -499,7 +502,7 @@ bool InputImeClearCompositionFunction::RunImpl() {
   return true;
 }
 
-bool InputImeCommitTextFunction::RunImpl() {
+bool InputImeCommitTextFunction::RunSync() {
   // TODO(zork): Support committing when not active.
   InputMethodEngineInterface* engine =
       InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id());
@@ -518,7 +521,7 @@ bool InputImeCommitTextFunction::RunImpl() {
   return true;
 }
 
-bool InputImeHideInputViewFunction::RunImpl() {
+bool InputImeHideInputViewFunction::RunAsync() {
   InputMethodEngineInterface* engine =
       InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id());
   if (!engine) {
@@ -528,7 +531,7 @@ bool InputImeHideInputViewFunction::RunImpl() {
   return true;
 }
 
-bool InputImeSendKeyEventsFunction::RunImpl() {
+bool InputImeSendKeyEventsFunction::RunAsync() {
   scoped_ptr<SendKeyEvents::Params> parent_params(
       SendKeyEvents::Params::Create(*args_));
   const SendKeyEvents::Params::Parameters& params =
@@ -542,10 +545,10 @@ bool InputImeSendKeyEventsFunction::RunImpl() {
 
   const std::vector<linked_ptr<input_ime::KeyboardEvent> >& key_data =
       params.key_data;
-  std::vector<chromeos::InputMethodEngine::KeyboardEvent> key_data_out;
+  std::vector<chromeos::InputMethodEngineInterface::KeyboardEvent> key_data_out;
 
   for (size_t i = 0; i < key_data.size(); ++i) {
-    chromeos::InputMethodEngine::KeyboardEvent event;
+    chromeos::InputMethodEngineInterface::KeyboardEvent event;
     event.type = input_ime::KeyboardEvent::ToString(key_data[i]->type);
     event.key = key_data[i]->key;
     event.code = key_data[i]->code;
@@ -564,7 +567,7 @@ bool InputImeSendKeyEventsFunction::RunImpl() {
   return true;
 }
 
-bool InputImeSetCandidateWindowPropertiesFunction::RunImpl() {
+bool InputImeSetCandidateWindowPropertiesFunction::RunSync() {
   scoped_ptr<SetCandidateWindowProperties::Params> parent_params(
       SetCandidateWindowProperties::Params::Create(*args_));
   const SetCandidateWindowProperties::Params::Parameters&
@@ -639,8 +642,7 @@ bool InputImeSetCandidateWindowPropertiesFunction::RunImpl() {
   return true;
 }
 
-#if defined(OS_CHROMEOS)
-bool InputImeSetCandidatesFunction::RunImpl() {
+bool InputImeSetCandidatesFunction::RunSync() {
   InputMethodEngineInterface* engine =
       InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id());
   if (!engine) {
@@ -676,7 +678,7 @@ bool InputImeSetCandidatesFunction::RunImpl() {
   return true;
 }
 
-bool InputImeSetCursorPositionFunction::RunImpl() {
+bool InputImeSetCursorPositionFunction::RunSync() {
   InputMethodEngineInterface* engine =
       InputImeEventRouter::GetInstance()->GetActiveEngine(extension_id());
   if (!engine) {
@@ -695,7 +697,7 @@ bool InputImeSetCursorPositionFunction::RunImpl() {
   return true;
 }
 
-bool InputImeSetMenuItemsFunction::RunImpl() {
+bool InputImeSetMenuItemsFunction::RunSync() {
   scoped_ptr<SetMenuItems::Params> parent_params(
       SetMenuItems::Params::Create(*args_));
   const SetMenuItems::Params::Parameters& params =
@@ -722,7 +724,7 @@ bool InputImeSetMenuItemsFunction::RunImpl() {
   return true;
 }
 
-bool InputImeUpdateMenuItemsFunction::RunImpl() {
+bool InputImeUpdateMenuItemsFunction::RunSync() {
   scoped_ptr<UpdateMenuItems::Params> parent_params(
       UpdateMenuItems::Params::Create(*args_));
   const UpdateMenuItems::Params::Parameters& params =
@@ -749,7 +751,7 @@ bool InputImeUpdateMenuItemsFunction::RunImpl() {
   return true;
 }
 
-bool InputImeDeleteSurroundingTextFunction::RunImpl() {
+bool InputImeDeleteSurroundingTextFunction::RunSync() {
   scoped_ptr<DeleteSurroundingText::Params> parent_params(
       DeleteSurroundingText::Params::Create(*args_));
   const DeleteSurroundingText::Params::Parameters& params =
@@ -768,31 +770,25 @@ bool InputImeDeleteSurroundingTextFunction::RunImpl() {
   return true;
 }
 
-bool InputImeKeyEventHandledFunction::RunImpl() {
+bool InputImeKeyEventHandledFunction::RunAsync() {
   scoped_ptr<KeyEventHandled::Params> params(
       KeyEventHandled::Params::Create(*args_));
   InputImeEventRouter::GetInstance()->OnKeyEventHandled(
       extension_id(), params->request_id, params->response);
   return true;
 }
-#endif
 
 InputImeAPI::InputImeAPI(content::BrowserContext* context)
-    : profile_(Profile::FromBrowserContext(context)) {
-  registrar_.Add(this,
-                 chrome::NOTIFICATION_EXTENSION_LOADED,
-                 content::Source<Profile>(profile_));
-  registrar_.Add(this,
-                 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
-                 content::Source<Profile>(profile_));
-
-  EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router();
+    : browser_context_(context), extension_registry_observer_(this) {
+  extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
+
+  EventRouter* event_router = EventRouter::Get(browser_context_);
   event_router->RegisterObserver(this, input_ime::OnActivate::kEventName);
   event_router->RegisterObserver(this, input_ime::OnFocus::kEventName);
 }
 
 InputImeAPI::~InputImeAPI() {
-  ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
+  EventRouter::Get(browser_context_)->UnregisterObserver(this);
 }
 
 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> >
@@ -803,36 +799,40 @@ BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() {
   return g_factory.Pointer();
 }
 
-void InputImeAPI::Observe(int type,
-                          const content::NotificationSource& source,
-                          const content::NotificationDetails& details) {
-  if (type == chrome::NOTIFICATION_EXTENSION_LOADED) {
-    const Extension* extension =
-        content::Details<const Extension>(details).ptr();
-    const std::vector<InputComponentInfo>* input_components =
-        extensions::InputComponents::GetInputComponents(extension);
-    if (!input_components)
-      return;
-    for (std::vector<extensions::InputComponentInfo>::const_iterator component =
-        input_components->begin(); component != input_components->end();
-        ++component) {
-      if (component->type == extensions::INPUT_COMPONENT_TYPE_IME) {
-        input_ime_event_router()->RegisterIme(
-            profile_, extension->id(), *component);
-      }
+void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context,
+                                    const Extension* extension) {
+  const std::vector<InputComponentInfo>* input_components =
+      extensions::InputComponents::GetInputComponents(extension);
+  if (!input_components)
+    return;
+  for (std::vector<extensions::InputComponentInfo>::const_iterator component =
+           input_components->begin();
+       component != input_components->end();
+       ++component) {
+    if (component->type == extensions::INPUT_COMPONENT_TYPE_IME) {
+      // Don't pass profile_ to register ime, instead always use
+      // GetActiveUserProfile. It is because:
+      // The original profile for login screen is called signin profile.
+      // And the active profile is the incognito profile based on signin
+      // profile. So if |profile_| is signin profile, we need to make sure
+      // the router/observer runs under its incognito profile, because the
+      // component extensions were installed under its incognito profile.
+      input_ime_event_router()->RegisterIme(extension->id(), *component);
     }
-  } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) {
-    const Extension* extension =
-        content::Details<const UnloadedExtensionInfo>(details)->extension;
-    const std::vector<InputComponentInfo>* input_components =
-        extensions::InputComponents::GetInputComponents(extension);
-    if (!input_components)
-      return;
-    if (input_components->size() > 0)
-      input_ime_event_router()->UnregisterAllImes(profile_, extension->id());
   }
 }
 
+void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context,
+                                      const Extension* extension,
+                                      UnloadedExtensionInfo::Reason reason) {
+  const std::vector<InputComponentInfo>* input_components =
+      extensions::InputComponents::GetInputComponents(extension);
+  if (!input_components)
+    return;
+  if (input_components->size() > 0)
+    input_ime_event_router()->UnregisterAllImes(extension->id());
+}
+
 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) {
   InputMethodEngineInterface* engine =
       input_ime_event_router()->GetActiveEngine(details.extension_id);