summaryrefslogtreecommitdiffstats
path: root/ui/keyboard/keyboard.cc
blob: d7901270a03d7e1a902aad33e147dd6017fc8f8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/keyboard/keyboard.h"

#include "base/files/file_path.h"
#include "base/memory/singleton.h"
#include "base/path_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_ui_controller_factory.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/keyboard/keyboard_constants.h"
#include "ui/keyboard/keyboard_ui_controller.h"
#include "url/gurl.h"

namespace {

using content::BrowserContext;
using content::WebUI;
using content::WebUIController;

class KeyboardWebUIControllerFactory : public content::WebUIControllerFactory {
 public:
  // |WebUIControllerFactory| implementation:
  virtual content::WebUI::TypeID GetWebUIType(
      content::BrowserContext* browser_context,
      const GURL& url) const OVERRIDE {
    if (url == GURL(keyboard::kKeyboardWebUIURL))
      return const_cast<KeyboardWebUIControllerFactory*>(this);

    return WebUI::kNoWebUI;
  }
  virtual bool UseWebUIForURL(content::BrowserContext* browser_context,
                              const GURL& url) const OVERRIDE {
    return GetWebUIType(browser_context, url) != WebUI::kNoWebUI;
  }
  virtual bool UseWebUIBindingsForURL(content::BrowserContext* browser_context,
                                      const GURL& url) const OVERRIDE {
    return UseWebUIForURL(browser_context, url);
  }
  virtual content::WebUIController* CreateWebUIControllerForURL(
      content::WebUI* web_ui,
      const GURL& url) const OVERRIDE {
    if (url == GURL(keyboard::kKeyboardWebUIURL))
      return new keyboard::KeyboardUIController(web_ui);
    return NULL;
  }

  static KeyboardWebUIControllerFactory* GetInstance() {
    return Singleton<KeyboardWebUIControllerFactory>::get();
  }

 protected:
  KeyboardWebUIControllerFactory() {}
  virtual ~KeyboardWebUIControllerFactory() {}

 private:
  friend struct DefaultSingletonTraits<KeyboardWebUIControllerFactory>;

  DISALLOW_COPY_AND_ASSIGN(KeyboardWebUIControllerFactory);
};

}  // namespace

namespace keyboard {

static bool initialized = false;

void ResetKeyboardForTesting() {
  content::WebUIControllerFactory::UnregisterFactoryForTesting(
      KeyboardWebUIControllerFactory::GetInstance());
  initialized = false;
}

void InitializeKeyboard() {
  if (initialized)
    return;
  initialized = true;

  base::FilePath pak_dir;
  PathService::Get(base::DIR_MODULE, &pak_dir);
  base::FilePath pak_file = pak_dir.Append(
      FILE_PATH_LITERAL("keyboard_resources.pak"));
  ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
      pak_file, ui::SCALE_FACTOR_100P);

  content::WebUIControllerFactory::RegisterFactory(
      KeyboardWebUIControllerFactory::GetInstance());
}

}  // namespace keyboard