Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: chrome/browser/chromeos/input_method/virtual_keyboard_selector.cc

Issue 7497028: Add SetUserPreference function to VirtualKeyboardSelector (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 { 10 namespace {
11
11 const char kDefaultURLPath[] = "index.html"; 12 const char kDefaultURLPath[] = "index.html";
12 const size_t kDefaultURLPathLen = arraysize(kDefaultURLPath) - 1; 13 const size_t kDefaultURLPathLen = arraysize(kDefaultURLPath) - 1;
14
15 bool SameOrNull(const GURL* rhs, const GURL* lhs) {
16 if (!rhs || !lhs) {
17 return true;
18 }
19 return *rhs == *lhs;
20 }
21
13 } // namespace 22 } // namespace
14 23
15 namespace chromeos { 24 namespace chromeos {
16 namespace input_method { 25 namespace input_method {
17 26
18 VirtualKeyboard::VirtualKeyboard(const GURL& url, 27 VirtualKeyboard::VirtualKeyboard(const GURL& url,
19 const std::set<std::string>& supported_layouts, 28 const std::set<std::string>& supported_layouts,
20 bool is_system) 29 bool is_system)
21 : url_(url), 30 : url_(url),
22 supported_layouts_(supported_layouts), 31 supported_layouts_(supported_layouts),
23 is_system_(is_system) { 32 is_system_(is_system) {
24 } 33 }
25 34
26 VirtualKeyboard::~VirtualKeyboard() { 35 VirtualKeyboard::~VirtualKeyboard() {
27 } 36 }
28 37
29 GURL VirtualKeyboard::GetURLForLayout(const std::string& layout) const { 38 GURL VirtualKeyboard::GetURLForLayout(const std::string& layout) const {
30 if (layout.empty()) { 39 if (layout.empty()) {
31 return url_; 40 return url_;
32 } 41 }
33 url_canon::Replacements<char> replacements; 42 url_canon::Replacements<char> replacements;
34 replacements.SetPath( 43 replacements.SetPath(
35 kDefaultURLPath, url_parse::Component(0, kDefaultURLPathLen)); 44 kDefaultURLPath, url_parse::Component(0, kDefaultURLPathLen));
36 // TODO(yusukes): would be better to URL-encode the |layout|? 45 // TODO(yusukes): would be better to URL-encode the |layout|?
37 replacements.SetRef(layout.c_str(), url_parse::Component(0, layout.length())); 46 replacements.SetRef(layout.c_str(), url_parse::Component(0, layout.length()));
38 return url_.ReplaceComponents(replacements); 47 return url_.ReplaceComponents(replacements);
39 } 48 }
40 49
50 bool VirtualKeyboard::LayoutIsSupported(const std::string& layout) const {
51 return supported_layouts_.count(layout) > 0;
52 }
53
41 VirtualKeyboardSelector::VirtualKeyboardSelector() 54 VirtualKeyboardSelector::VirtualKeyboardSelector()
42 : current_(NULL) { 55 : current_(NULL) {
43 } 56 }
44 57
45 VirtualKeyboardSelector::~VirtualKeyboardSelector() { 58 VirtualKeyboardSelector::~VirtualKeyboardSelector() {
46 STLDeleteElements(&keyboards_); 59 STLDeleteElements(&keyboards_);
47 STLDeleteElements(&system_keyboards_); 60 STLDeleteElements(&system_keyboards_);
48 } 61 }
49 62
50 void VirtualKeyboardSelector::AddVirtualKeyboard( 63 void VirtualKeyboardSelector::AddVirtualKeyboard(
(...skipping 10 matching lines...) Expand all
61 } 74 }
62 } 75 }
63 76
64 const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboard( 77 const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboard(
65 const std::string& layout) { 78 const std::string& layout) {
66 if (layout.empty()) { 79 if (layout.empty()) {
67 LOG(ERROR) << "No layout is specified"; 80 LOG(ERROR) << "No layout is specified";
68 return NULL; 81 return NULL;
69 } 82 }
70 83
71 // First, check whether the current keyboard supports the layout. 84 // First, check the user pref.
72 if (current_ && current_->supported_layouts().count(layout) > 0) { 85 std::map<std::string, const VirtualKeyboard*>::const_iterator iter =
86 user_preference_.find(layout);
87 if (iter != user_preference_.end()) {
88 current_ = iter->second;
bryeung 2011/07/28 18:31:48 Before throwing away the value of current_, maybe
Yusuke Sato 2011/07/29 05:15:35 Done.
89 }
90
91 // Second, check whether the current keyboard supports the layout.
92 if (current_ && current_->LayoutIsSupported(layout)) {
73 return current_; 93 return current_;
74 } 94 }
75 95
76 const VirtualKeyboard* keyboard = SelectVirtualKeyboardInternal(layout); 96 const VirtualKeyboard* keyboard = SelectVirtualKeyboardByLayout(layout);
bryeung 2011/07/28 18:31:48 Maybe SelectVirtualKeyboardWithoutPreferences woul
Yusuke Sato 2011/07/29 05:15:35 Done.
77 if (!keyboard) { 97 if (!keyboard) {
78 VLOG(1) << "No virtual keyboard for " << layout << " is found"; 98 VLOG(1) << "No virtual keyboard for " << layout << " is found";
79 return NULL; 99 return NULL;
80 } 100 }
81 101
82 current_ = keyboard; 102 current_ = keyboard;
83 return keyboard; 103 return keyboard;
84 } 104 }
85 105
86 const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboardInternal( 106 bool VirtualKeyboardSelector::SetUserPreference(
107 const std::string& layout, const GURL& url) {
108 const VirtualKeyboard* keyboard =
109 SelectVirtualKeyboardByUrl(keyboards_, layout, &url);
110 if (!keyboard)
111 keyboard = SelectVirtualKeyboardByUrl(system_keyboards_, layout, &url);
112 if (!keyboard) {
113 VLOG(1) << "Can't set user preference.";
114 return false;
115 }
116
117 RemoveUserPreference(layout);
118 user_preference_.insert(std::make_pair(layout, keyboard));
119 return true;
120 }
121
122 void VirtualKeyboardSelector::RemoveUserPreference(const std::string& layout) {
123 user_preference_.erase(layout);
124 }
125
126 const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboardByLayout(
87 const std::string& layout) { 127 const std::string& layout) {
128 const VirtualKeyboard* keyboard =
129 SelectVirtualKeyboardByUrl(keyboards_, layout, NULL); // don't check URL
130 if (!keyboard)
131 keyboard = SelectVirtualKeyboardByUrl(system_keyboards_, layout, NULL);
132 return keyboard;
133 }
134
135 // static
136 const VirtualKeyboard* VirtualKeyboardSelector::SelectVirtualKeyboardByUrl(
137 const std::list<const VirtualKeyboard*>& keyboards,
138 const std::string& layout,
139 const GURL* url) {
88 std::list<const VirtualKeyboard*>::const_iterator iter; 140 std::list<const VirtualKeyboard*>::const_iterator iter;
mazda 2011/07/28 07:08:16 How about moving this to the initializer expressio
Yusuke Sato 2011/07/29 05:15:35 Done.
89 for (iter = keyboards_.begin(); iter != keyboards_.end(); ++iter) { 141 for (iter = keyboards.begin(); iter != keyboards.end(); ++iter) {
90 if ((*iter)->supported_layouts().count(layout) > 0) { 142 const VirtualKeyboard* keyboard = *iter;
91 return *iter; 143 if (SameOrNull(url, &(keyboard->url())) &&
bryeung 2011/07/28 18:31:48 This could give bad results if somehow there is a
Yusuke Sato 2011/07/29 05:15:35 Done.
92 } 144 keyboard->LayoutIsSupported(layout)) {
93 } 145 return keyboard;
94 for (iter = system_keyboards_.begin();
95 iter != system_keyboards_.end(); ++iter) {
96 if ((*iter)->supported_layouts().count(layout) > 0) {
97 return *iter;
98 } 146 }
99 } 147 }
100 return NULL; 148 return NULL;
101 } 149 }
102 150
103 } // namespace input_method 151 } // namespace input_method
104 } // namespace chromeos 152 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698