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

Side by Side Diff: chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc

Issue 2155743002: Add throbber and status text to WebBluetooth chooser UI on non-Mac desktops (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 4 years, 5 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/ui/bluetooth/bluetooth_chooser_controller.h" 5 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_controller.h"
6 6
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/net/referrer.h" 8 #include "chrome/browser/net/referrer.h"
9 #include "chrome/browser/profiles/profile_manager.h" 9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h" 10 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h"
(...skipping 14 matching lines...) Expand all
25 } 25 }
26 26
27 } // namespace 27 } // namespace
28 28
29 BluetoothChooserController::BluetoothChooserController( 29 BluetoothChooserController::BluetoothChooserController(
30 content::RenderFrameHost* owner, 30 content::RenderFrameHost* owner,
31 const content::BluetoothChooser::EventHandler& event_handler) 31 const content::BluetoothChooser::EventHandler& event_handler)
32 : ChooserController(owner, 32 : ChooserController(owner,
33 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, 33 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN,
34 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), 34 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME),
35 event_handler_(event_handler) {} 35 event_handler_(event_handler),
36 no_devices_text_(l10n_util::GetStringUTF16(
37 IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {}
36 38
37 BluetoothChooserController::~BluetoothChooserController() {} 39 BluetoothChooserController::~BluetoothChooserController() {}
38 40
41 base::string16 BluetoothChooserController::GetNoOptionsText() const {
42 return no_devices_text_;
43 }
44
39 base::string16 BluetoothChooserController::GetOkButtonLabel() const { 45 base::string16 BluetoothChooserController::GetOkButtonLabel() const {
40 return l10n_util::GetStringUTF16( 46 return l10n_util::GetStringUTF16(
41 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); 47 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT);
42 } 48 }
43 49
44 size_t BluetoothChooserController::NumOptions() const { 50 size_t BluetoothChooserController::NumOptions() const {
45 return device_names_and_ids_.size(); 51 return device_names_and_ids_.size();
46 } 52 }
47 53
48 base::string16 BluetoothChooserController::GetOption(size_t index) const { 54 base::string16 BluetoothChooserController::GetOption(size_t index) const {
49 DCHECK_LT(index, device_names_and_ids_.size()); 55 DCHECK_LT(index, device_names_and_ids_.size());
50 const base::string16& device_name = device_names_and_ids_[index].first; 56 const base::string16& device_name = device_names_and_ids_[index].first;
51 const auto& it = device_name_map_.find(device_name); 57 const auto& it = device_name_map_.find(device_name);
52 DCHECK(it != device_name_map_.end()); 58 DCHECK(it != device_name_map_.end());
53 return it->second == 1 59 return it->second == 1
54 ? device_name 60 ? device_name
55 : l10n_util::GetStringFUTF16( 61 : l10n_util::GetStringFUTF16(
56 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name, 62 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name,
57 base::UTF8ToUTF16(device_names_and_ids_[index].second)); 63 base::UTF8ToUTF16(device_names_and_ids_[index].second));
58 } 64 }
59 65
66 void BluetoothChooserController::RefreshOptions() {
67 ClearAllDevices();
68 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string());
69 }
70
71 base::string16 BluetoothChooserController::GetStatus() const {
72 return status_text_;
73 }
74
60 void BluetoothChooserController::Select(size_t index) { 75 void BluetoothChooserController::Select(size_t index) {
61 DCHECK_LT(index, device_names_and_ids_.size()); 76 DCHECK_LT(index, device_names_and_ids_.size());
62 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, 77 event_handler_.Run(content::BluetoothChooser::Event::SELECTED,
63 device_names_and_ids_[index].second); 78 device_names_and_ids_[index].second);
64 } 79 }
65 80
66 void BluetoothChooserController::Cancel() { 81 void BluetoothChooserController::Cancel() {
67 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, 82 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED,
68 std::string()); 83 std::string());
69 } 84 }
70 85
71 void BluetoothChooserController::Close() { 86 void BluetoothChooserController::Close() {
72 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, 87 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED,
73 std::string()); 88 std::string());
74 } 89 }
75 90
76 void BluetoothChooserController::OpenHelpCenterUrl() const { 91 void BluetoothChooserController::OpenHelpCenterUrl() const {
77 GetBrowser()->OpenURL(content::OpenURLParams( 92 GetBrowser()->OpenURL(content::OpenURLParams(
78 GURL(chrome::kChooserBluetoothOverviewURL), content::Referrer(), 93 GURL(chrome::kChooserBluetoothOverviewURL), content::Referrer(),
79 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_AUTO_TOPLEVEL, 94 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
80 false /* is_renderer_initialized */)); 95 false /* is_renderer_initialized */));
81 } 96 }
82 97
98 void BluetoothChooserController::UpdateAdapterPresence(
99 content::BluetoothChooser::AdapterPresence presence) {
100 ClearAllDevices();
101 switch (presence) {
102 case content::BluetoothChooser::AdapterPresence::ABSENT:
msw 2016/07/18 23:01:30 Should this case send observer()->AdapterEnabled(f
juncai 2016/07/19 20:42:46 When there is no Bluetooth adapter, the Bluetooth
msw 2016/07/19 22:08:10 Acknowledged.
103 break;
104 case content::BluetoothChooser::AdapterPresence::POWERED_OFF:
105 no_devices_text_ = l10n_util::GetStringUTF16(
106 IDS_BLUETOOTH_DEVICE_CHOOSER_NO_DEVICES_FOUND_ADAPTER_OFF_PROMPT);
107 status_text_ = base::string16();
108 if (observer())
109 observer()->AdapterEnabled(false /* Bluetooth adapter is truned off */);
msw 2016/07/18 23:01:30 nit: 'turned'
juncai 2016/07/19 20:42:46 Done.
110 break;
111 case content::BluetoothChooser::AdapterPresence::POWERED_ON:
112 no_devices_text_ =
113 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT);
114 status_text_ =
115 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN);
116 if (observer())
117 observer()->AdapterEnabled(true /* Bluetooth adapter is turned on */);
118 break;
119 }
120 }
121
122 void BluetoothChooserController::UpdateDiscoveryState(
123 content::BluetoothChooser::DiscoveryState state) {
124 switch (state) {
125 case content::BluetoothChooser::DiscoveryState::DISCOVERING:
126 status_text_ =
127 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING);
128 if (observer())
129 observer()->SetRefreshing(true /* Refreshing options is in progress */);
130 break;
131 case content::BluetoothChooser::DiscoveryState::IDLE:
132 case content::BluetoothChooser::DiscoveryState::FAILED_TO_START:
133 status_text_ =
134 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN);
135 if (observer())
136 observer()->SetRefreshing(false /* Refreshing options is complete*/);
137 break;
138 }
139 }
140
83 void BluetoothChooserController::AddDevice(const std::string& device_id, 141 void BluetoothChooserController::AddDevice(const std::string& device_id,
84 const base::string16& device_name) { 142 const base::string16& device_name) {
85 device_names_and_ids_.push_back(std::make_pair(device_name, device_id)); 143 device_names_and_ids_.push_back(std::make_pair(device_name, device_id));
86 ++device_name_map_[device_name]; 144 ++device_name_map_[device_name];
87 if (observer()) 145 if (observer())
88 observer()->OnOptionAdded(device_names_and_ids_.size() - 1); 146 observer()->OnOptionAdded(device_names_and_ids_.size() - 1);
89 } 147 }
90 148
91 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { 149 void BluetoothChooserController::RemoveDevice(const std::string& device_id) {
92 for (auto it = device_names_and_ids_.begin(); 150 for (auto it = device_names_and_ids_.begin();
93 it != device_names_and_ids_.end(); ++it) { 151 it != device_names_and_ids_.end(); ++it) {
94 if (it->second == device_id) { 152 if (it->second == device_id) {
95 size_t index = it - device_names_and_ids_.begin(); 153 size_t index = it - device_names_and_ids_.begin();
96 DCHECK_GT(device_name_map_[it->first], 0); 154 DCHECK_GT(device_name_map_[it->first], 0);
97 if (--device_name_map_[it->first] == 0) 155 if (--device_name_map_[it->first] == 0)
98 device_name_map_.erase(it->first); 156 device_name_map_.erase(it->first);
99 device_names_and_ids_.erase(it); 157 device_names_and_ids_.erase(it);
100 if (observer()) 158 if (observer())
101 observer()->OnOptionRemoved(index); 159 observer()->OnOptionRemoved(index);
102 return; 160 return;
103 } 161 }
104 } 162 }
105 } 163 }
164
165 void BluetoothChooserController::ClearAllDevices() {
166 device_names_and_ids_.clear();
167 device_name_map_.clear();
168 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698