Home | History | Annotate | Download | only in input
      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 #include "chrome/browser/extensions/api/input/input.h"
      6 
      7 #include "base/lazy_instance.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/strings/string16.h"
     10 #include "chrome/browser/extensions/extension_function_registry.h"
     11 #include "content/public/browser/browser_thread.h"
     12 #include "ui/base/events/event.h"
     13 
     14 #if defined(USE_ASH)
     15 #include "ash/shell.h"
     16 #include "ui/keyboard/keyboard_util.h"
     17 #endif
     18 
     19 namespace {
     20 
     21 const char kNotYetImplementedError[] =
     22     "API is not implemented on this platform.";
     23 
     24 }  // namespace
     25 
     26 namespace extensions {
     27 
     28 bool InsertTextInputFunction::RunImpl() {
     29 #if defined(USE_ASH)
     30   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     31 
     32   string16 text;
     33   EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text));
     34 
     35   return keyboard::InsertText(text, ash::Shell::GetPrimaryRootWindow());
     36 #endif
     37   error_ = kNotYetImplementedError;
     38   return false;
     39 }
     40 
     41 bool MoveCursorFunction::RunImpl() {
     42 #if defined(USE_ASH)
     43   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     44 
     45   int swipe_direction;
     46   int modifier_flags;
     47   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &swipe_direction));
     48   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &modifier_flags));
     49 
     50   return keyboard::MoveCursor(swipe_direction, modifier_flags,
     51                               ash::Shell::GetPrimaryRootWindow());
     52 #endif
     53   error_ = kNotYetImplementedError;
     54   return false;
     55 }
     56 
     57 InputAPI::InputAPI(Profile* profile) {
     58   ExtensionFunctionRegistry* registry =
     59       ExtensionFunctionRegistry::GetInstance();
     60   registry->RegisterFunction<InsertTextInputFunction>();
     61   registry->RegisterFunction<MoveCursorFunction>();
     62 }
     63 
     64 InputAPI::~InputAPI() {
     65 }
     66 
     67 static base::LazyInstance<ProfileKeyedAPIFactory<InputAPI> >
     68 g_factory = LAZY_INSTANCE_INITIALIZER;
     69 
     70 // static
     71 ProfileKeyedAPIFactory<InputAPI>* InputAPI::GetFactoryInstance() {
     72   return &g_factory.Get();
     73 }
     74 
     75 }  // namespace extensions
     76