OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/input_method/virtual_keyboard_selector.h" | 5 #include "chrome/browser/chromeos/input_method/virtual_keyboard_selector.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 | 9 |
10 namespace { | |
11 const char kDefaultURLPath[] = "index.html"; | |
12 const size_t kDefaultURLPathLen = arraysize(kDefaultURLPath) - 1; | |
13 } // namespace | |
14 | |
15 namespace chromeos { | 10 namespace chromeos { |
16 namespace input_method { | 11 namespace input_method { |
17 | 12 |
13 namespace { | |
bryeung
2011/08/03 22:04:46
I don't think the anonymous namespace should be ne
Yusuke Sato
2011/08/04 06:26:56
Done. Added a namespace alias (which is explicitly
| |
14 | |
15 const char kDefaultURLPath[] = "index.html"; | |
16 const size_t kDefaultURLPathLen = arraysize(kDefaultURLPath) - 1; | |
17 | |
18 // Selects and returns a virtual keyboard extension from |keyboards| which | |
19 // supports the |layout| and whose address is |url|. If |url| is NULL, URL | |
20 // checking is ignored. | |
21 const VirtualKeyboard* SelectVirtualKeyboardInternal( | |
22 const std::list<const VirtualKeyboard*>& keyboards, | |
23 const std::string& layout, | |
24 const GURL* url) { | |
bryeung
2011/08/03 22:04:46
Is it worth adding a map from url to VirtualKeyboa
Yusuke Sato
2011/08/04 06:26:56
Sure, added the map to make SetUserPreference func
| |
25 for (std::list<const VirtualKeyboard*>::const_iterator iter = | |
26 keyboards.begin(); iter != keyboards.end(); ++iter) { | |
27 const VirtualKeyboard* keyboard = *iter; | |
28 if (((!url) || (*url) == keyboard->url()) && | |
bryeung
2011/08/03 22:04:46
Can you remove the extra parens?
if ((!url || *ur
Yusuke Sato
2011/08/04 06:26:56
(I've removed the url parameter from the function.
| |
29 keyboard->IsLayoutSupported(layout)) { | |
30 return keyboard; | |
31 } | |
32 } | |
33 return NULL; | |
34 } | |
35 | |
36 } // namespace | |
37 | |
18 VirtualKeyboard::VirtualKeyboard(const GURL& url, | 38 VirtualKeyboard::VirtualKeyboard(const GURL& url, |
19 const std::set<std::string>& supported_layouts, | 39 const std::set<std::string>& supported_layouts, |
20 bool is_system) | 40 bool is_system) |
21 : url_(url), | 41 : url_(url), |
22 supported_layouts_(supported_layouts), | 42 supported_layouts_(supported_layouts), |
23 is_system_(is_system) { | 43 is_system_(is_system) { |
24 } | 44 } |
25 | 45 |
26 VirtualKeyboard::~VirtualKeyboard() { | 46 VirtualKeyboard::~VirtualKeyboard() { |
27 } | 47 } |
28 | 48 |
29 GURL VirtualKeyboard::GetURLForLayout(const std::string& layout) const { | 49 GURL VirtualKeyboard::GetURLForLayout(const std::string& layout) const { |
30 if (layout.empty()) { | 50 if (layout.empty()) { |
31 return url_; | 51 return url_; |
32 } | 52 } |
33 url_canon::Replacements<char> replacements; | 53 url_canon::Replacements<char> replacements; |
34 replacements.SetPath( | 54 replacements.SetPath( |
35 kDefaultURLPath, url_parse::Component(0, kDefaultURLPathLen)); | 55 kDefaultURLPath, url_parse::Component(0, kDefaultURLPathLen)); |
36 // TODO(yusukes): would be better to URL-encode the |layout|? | 56 // TODO(yusukes): would be better to URL-encode the |layout|? |
37 replacements.SetRef(layout.c_str(), url_parse::Component(0, layout.length())); | 57 replacements.SetRef(layout.c_str(), url_parse::Component(0, layout.length())); |
38 return url_.ReplaceComponents(replacements); | 58 return url_.ReplaceComponents(replacements); |
39 } | 59 } |
40 | 60 |
61 bool VirtualKeyboard::IsLayoutSupported(const std::string& layout) const { | |
62 return supported_layouts_.count(layout) > 0; | |
63 } | |
64 | |
41 VirtualKeyboardSelector::VirtualKeyboardSelector() | 65 VirtualKeyboardSelector::VirtualKeyboardSelector() |
42 : current_(NULL) { | 66 : current_(NULL) { |
43 } | 67 } |
44 | 68 |
45 VirtualKeyboardSelector::~VirtualKeyboardSelector() { | 69 VirtualKeyboardSelector::~VirtualKeyboardSelector() { |
46 STLDeleteElements(&keyboards_); | 70 STLDeleteElements(&keyboards_); |
47 STLDeleteElements(&system_keyboards_); | 71 STLDeleteElements(&system_keyboards_); |
48 } | 72 } |
49 | 73 |
50 void VirtualKeyboardSelector::AddVirtualKeyboard( | 74 void VirtualKeyboardSelector::AddVirtualKeyboard( |
(...skipping 10 matching lines...) Expand all Loading... | |
61 } | 85 } |
62 } | 86 } |
63 | 87 |
64 const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboard( | 88 const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboard( |
65 const std::string& layout) { | 89 const std::string& layout) { |
66 if (layout.empty()) { | 90 if (layout.empty()) { |
67 LOG(ERROR) << "No layout is specified"; | 91 LOG(ERROR) << "No layout is specified"; |
68 return NULL; | 92 return NULL; |
69 } | 93 } |
70 | 94 |
71 // First, check whether the current keyboard supports the layout. | 95 // First, check the user pref. |
72 if (current_ && current_->supported_layouts().count(layout) > 0) { | 96 std::map<std::string, const VirtualKeyboard*>::const_iterator iter = |
97 user_preference_.find(layout); | |
98 if (iter != user_preference_.end() && | |
99 iter->second->IsLayoutSupported(layout)) { | |
100 current_ = iter->second; | |
101 } | |
102 | |
103 // Second, check whether the current keyboard supports the layout. | |
104 if (current_ && current_->IsLayoutSupported(layout)) { | |
bryeung
2011/08/03 22:04:46
This should probably be an else if, or you'll be d
Yusuke Sato
2011/08/04 06:26:56
Done.
| |
73 return current_; | 105 return current_; |
74 } | 106 } |
75 | 107 |
76 const VirtualKeyboard* keyboard = SelectVirtualKeyboardInternal(layout); | 108 const VirtualKeyboard* keyboard = |
109 SelectVirtualKeyboardWithoutPreferences(layout); | |
77 if (!keyboard) { | 110 if (!keyboard) { |
78 VLOG(1) << "No virtual keyboard for " << layout << " is found"; | 111 VLOG(1) << "No virtual keyboard for " << layout << " is found"; |
79 return NULL; | 112 return NULL; |
80 } | 113 } |
81 | 114 |
82 current_ = keyboard; | 115 current_ = keyboard; |
83 return keyboard; | 116 return keyboard; |
84 } | 117 } |
85 | 118 |
86 const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboardInternal( | 119 bool VirtualKeyboardSelector::SetUserPreference( |
120 const std::string& layout, const GURL& url) { | |
121 const VirtualKeyboard* keyboard = | |
122 SelectVirtualKeyboardInternal(keyboards_, layout, &url); | |
123 if (!keyboard) | |
124 keyboard = SelectVirtualKeyboardInternal(system_keyboards_, layout, &url); | |
125 if (!keyboard) { | |
126 VLOG(1) << "Can't set user preference."; | |
127 return false; | |
128 } | |
129 | |
130 RemoveUserPreference(layout); | |
131 user_preference_.insert(std::make_pair(layout, keyboard)); | |
132 return true; | |
133 } | |
134 | |
135 void VirtualKeyboardSelector::RemoveUserPreference(const std::string& layout) { | |
136 user_preference_.erase(layout); | |
137 } | |
138 | |
139 const VirtualKeyboard* | |
140 VirtualKeyboardSelector::SelectVirtualKeyboardWithoutPreferences( | |
87 const std::string& layout) { | 141 const std::string& layout) { |
88 std::list<const VirtualKeyboard*>::const_iterator iter; | 142 const VirtualKeyboard* keyboard = |
89 for (iter = keyboards_.begin(); iter != keyboards_.end(); ++iter) { | 143 SelectVirtualKeyboardInternal(keyboards_, layout, NULL); |
90 if ((*iter)->supported_layouts().count(layout) > 0) { | 144 if (!keyboard) |
91 return *iter; | 145 keyboard = SelectVirtualKeyboardInternal(system_keyboards_, layout, NULL); |
92 } | 146 return keyboard; |
93 } | |
94 for (iter = system_keyboards_.begin(); | |
95 iter != system_keyboards_.end(); ++iter) { | |
96 if ((*iter)->supported_layouts().count(layout) > 0) { | |
97 return *iter; | |
98 } | |
99 } | |
100 return NULL; | |
101 } | 147 } |
102 | 148 |
103 } // namespace input_method | 149 } // namespace input_method |
104 } // namespace chromeos | 150 } // namespace chromeos |
OLD | NEW |