summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/input_method/virtual_keyboard_selector.cc
blob: ac7394df43fde8fc2f2e65d5059c17cf6d84904a (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
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2011 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 "chrome/browser/chromeos/input_method/virtual_keyboard_selector.h"

#include "base/logging.h"
#include "base/stl_util-inl.h"

namespace {
const char kDefaultURLPath[] = "index.html";
const size_t kDefaultURLPathLen = arraysize(kDefaultURLPath) - 1;
}  // namespace

namespace chromeos {
namespace input_method {

VirtualKeyboard::VirtualKeyboard(const GURL& url,
                                 const std::set<std::string>& supported_layouts,
                                 bool is_system)
    : url_(url),
      supported_layouts_(supported_layouts),
      is_system_(is_system) {
}

VirtualKeyboard::~VirtualKeyboard() {
}

GURL VirtualKeyboard::GetURLForLayout(const std::string& layout) const {
  if (layout.empty()) {
    return url_;
  }
  url_canon::Replacements<char> replacements;
  replacements.SetPath(
      kDefaultURLPath, url_parse::Component(0, kDefaultURLPathLen));
  // TODO(yusukes): would be better to URL-encode the |layout|?
  replacements.SetRef(layout.c_str(), url_parse::Component(0, layout.length()));
  return url_.ReplaceComponents(replacements);
}

VirtualKeyboardSelector::VirtualKeyboardSelector()
    : current_(NULL) {
}

VirtualKeyboardSelector::~VirtualKeyboardSelector() {
  STLDeleteElements(&keyboards_);
  STLDeleteElements(&system_keyboards_);
}

void VirtualKeyboardSelector::AddVirtualKeyboard(
    const GURL& url,
    const std::set<std::string>& supported_layouts,
    bool is_system) {
  const VirtualKeyboard* new_keyboard = new VirtualKeyboard(url,
                                                            supported_layouts,
                                                            is_system);
  if (is_system) {
    system_keyboards_.push_front(new_keyboard);
  } else {
    keyboards_.push_front(new_keyboard);
  }
}

const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboard(
    const std::string& layout) {
  if (layout.empty()) {
    LOG(ERROR) << "No layout is specified";
    return NULL;
  }

  // First, check whether the current keyboard supports the layout.
  if (current_ && current_->supported_layouts().count(layout) > 0) {
    return current_;
  }

  const VirtualKeyboard* keyboard = SelectVirtualKeyboardInternal(layout);
  if (!keyboard) {
    VLOG(1) << "No virtual keyboard for " << layout << " is found";
    return NULL;
  }

  current_ = keyboard;
  return keyboard;
}

const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboardInternal(
    const std::string& layout) {
  std::list<const VirtualKeyboard*>::const_iterator iter;
  for (iter = keyboards_.begin(); iter != keyboards_.end(); ++iter) {
    if ((*iter)->supported_layouts().count(layout) > 0) {
      return *iter;
    }
  }
  for (iter = system_keyboards_.begin();
       iter != system_keyboards_.end(); ++iter) {
    if ((*iter)->supported_layouts().count(layout) > 0) {
      return *iter;
    }
  }
  return NULL;
}

}  // namespace input_method
}  // namespace chromeos