Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/logging.h" | |
| 7 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/net/referrer.h" | 9 #include "chrome/browser/net/referrer.h" |
| 9 #include "chrome/browser/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
| 10 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h" | 11 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h" |
| 11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 13 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
| 13 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 14 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 Browser* GetBrowser() { | 21 Browser* GetBrowser() { |
| 21 chrome::ScopedTabbedBrowserDisplayer browser_displayer( | 22 chrome::ScopedTabbedBrowserDisplayer browser_displayer( |
| 22 ProfileManager::GetActiveUserProfile()); | 23 ProfileManager::GetActiveUserProfile()); |
| 23 DCHECK(browser_displayer.browser()); | 24 DCHECK(browser_displayer.browser()); |
| 24 return browser_displayer.browser(); | 25 return browser_displayer.browser(); |
| 25 } | 26 } |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 BluetoothChooserController::BluetoothChooserController( | 30 BluetoothChooserController::BluetoothChooserController( |
| 30 content::RenderFrameHost* owner, | 31 content::RenderFrameHost* owner, |
| 31 const content::BluetoothChooser::EventHandler& event_handler) | 32 const content::BluetoothChooser::EventHandler& event_handler) |
| 32 : ChooserController(owner, | 33 : ChooserController(owner, |
| 33 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, | 34 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_ORIGIN, |
| 34 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), | 35 IDS_BLUETOOTH_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), |
| 35 event_handler_(event_handler) {} | 36 event_handler_(event_handler), |
| 37 no_devices_text_(l10n_util::GetStringUTF16( | |
| 38 IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT)) {} | |
| 36 | 39 |
| 37 BluetoothChooserController::~BluetoothChooserController() {} | 40 BluetoothChooserController::~BluetoothChooserController() {} |
| 38 | 41 |
| 42 base::string16 BluetoothChooserController::GetNoOptionsText() const { | |
| 43 return no_devices_text_; | |
| 44 } | |
| 45 | |
| 39 base::string16 BluetoothChooserController::GetOkButtonLabel() const { | 46 base::string16 BluetoothChooserController::GetOkButtonLabel() const { |
| 40 return l10n_util::GetStringUTF16( | 47 return l10n_util::GetStringUTF16( |
| 41 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); | 48 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); |
| 42 } | 49 } |
| 43 | 50 |
| 44 size_t BluetoothChooserController::NumOptions() const { | 51 size_t BluetoothChooserController::NumOptions() const { |
| 45 return device_names_and_ids_.size(); | 52 return device_names_and_ids_.size(); |
| 46 } | 53 } |
| 47 | 54 |
| 48 base::string16 BluetoothChooserController::GetOption(size_t index) const { | 55 base::string16 BluetoothChooserController::GetOption(size_t index) const { |
| 49 DCHECK_LT(index, device_names_and_ids_.size()); | 56 DCHECK_LT(index, device_names_and_ids_.size()); |
| 50 const base::string16& device_name = device_names_and_ids_[index].first; | 57 const base::string16& device_name = device_names_and_ids_[index].first; |
| 51 const auto& it = device_name_map_.find(device_name); | 58 const auto& it = device_name_map_.find(device_name); |
| 52 DCHECK(it != device_name_map_.end()); | 59 DCHECK(it != device_name_map_.end()); |
| 53 return it->second == 1 | 60 return it->second == 1 |
| 54 ? device_name | 61 ? device_name |
| 55 : l10n_util::GetStringFUTF16( | 62 : l10n_util::GetStringFUTF16( |
| 56 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name, | 63 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name, |
| 57 base::UTF8ToUTF16(device_names_and_ids_[index].second)); | 64 base::UTF8ToUTF16(device_names_and_ids_[index].second)); |
| 58 } | 65 } |
| 59 | 66 |
| 67 void BluetoothChooserController::RefreshOptions() { | |
| 68 ClearAllDevices(); | |
| 69 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); | |
| 70 } | |
| 71 | |
| 72 base::string16 BluetoothChooserController::GetStatus() const { | |
| 73 return status_text_; | |
| 74 } | |
| 75 | |
| 60 void BluetoothChooserController::Select(size_t index) { | 76 void BluetoothChooserController::Select(size_t index) { |
| 61 DCHECK_LT(index, device_names_and_ids_.size()); | 77 DCHECK_LT(index, device_names_and_ids_.size()); |
| 62 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, | 78 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, |
| 63 device_names_and_ids_[index].second); | 79 device_names_and_ids_[index].second); |
| 64 } | 80 } |
| 65 | 81 |
| 66 void BluetoothChooserController::Cancel() { | 82 void BluetoothChooserController::Cancel() { |
| 67 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, | 83 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, |
| 68 std::string()); | 84 std::string()); |
| 69 } | 85 } |
| 70 | 86 |
| 71 void BluetoothChooserController::Close() { | 87 void BluetoothChooserController::Close() { |
| 72 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, | 88 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, |
| 73 std::string()); | 89 std::string()); |
| 74 } | 90 } |
| 75 | 91 |
| 76 void BluetoothChooserController::OpenHelpCenterUrl() const { | 92 void BluetoothChooserController::OpenHelpCenterUrl() const { |
| 77 GetBrowser()->OpenURL(content::OpenURLParams( | 93 GetBrowser()->OpenURL(content::OpenURLParams( |
| 78 GURL(chrome::kChooserBluetoothOverviewURL), content::Referrer(), | 94 GURL(chrome::kChooserBluetoothOverviewURL), content::Referrer(), |
| 79 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 95 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 80 false /* is_renderer_initialized */)); | 96 false /* is_renderer_initialized */)); |
| 81 } | 97 } |
| 82 | 98 |
| 99 void BluetoothChooserController::OnAdapterPresenceChanged( | |
| 100 content::BluetoothChooser::AdapterPresence presence) { | |
| 101 ClearAllDevices(); | |
| 102 switch (presence) { | |
| 103 case content::BluetoothChooser::AdapterPresence::ABSENT: | |
| 104 NOTREACHED(); | |
| 105 break; | |
| 106 case content::BluetoothChooser::AdapterPresence::POWERED_OFF: | |
| 107 no_devices_text_ = | |
| 108 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_ADAPTER_OFF); | |
| 109 status_text_ = base::string16(); | |
| 110 if (observer()) | |
| 111 observer()->OnAdapterEnabledChanged( | |
|
scheib
2016/07/19 21:18:13
In a seperate patch, consider naming ChooserContro
juncai
2016/07/19 22:58:24
I will do this in a separate patch.
| |
| 112 false /* Bluetooth adapter is turned off */); | |
| 113 break; | |
| 114 case content::BluetoothChooser::AdapterPresence::POWERED_ON: | |
| 115 no_devices_text_ = | |
| 116 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT); | |
| 117 status_text_ = | |
| 118 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN); | |
| 119 if (observer()) | |
| 120 observer()->OnAdapterEnabledChanged( | |
| 121 true /* Bluetooth adapter is turned on */); | |
| 122 break; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 void BluetoothChooserController::OnDiscoveryStateChanged( | |
| 127 content::BluetoothChooser::DiscoveryState state) { | |
| 128 switch (state) { | |
| 129 case content::BluetoothChooser::DiscoveryState::DISCOVERING: | |
| 130 status_text_ = | |
| 131 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING); | |
| 132 if (observer()) | |
| 133 observer()->OnRefreshStateChanged( | |
| 134 true /* Refreshing options is in progress */); | |
| 135 break; | |
| 136 case content::BluetoothChooser::DiscoveryState::IDLE: | |
| 137 case content::BluetoothChooser::DiscoveryState::FAILED_TO_START: | |
| 138 status_text_ = | |
| 139 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN); | |
| 140 if (observer()) | |
| 141 observer()->OnRefreshStateChanged( | |
| 142 false /* Refreshing options is complete */); | |
| 143 break; | |
| 144 } | |
| 145 } | |
| 146 | |
| 83 void BluetoothChooserController::AddDevice(const std::string& device_id, | 147 void BluetoothChooserController::AddDevice(const std::string& device_id, |
| 84 const base::string16& device_name) { | 148 const base::string16& device_name) { |
| 85 device_names_and_ids_.push_back(std::make_pair(device_name, device_id)); | 149 device_names_and_ids_.push_back(std::make_pair(device_name, device_id)); |
| 86 ++device_name_map_[device_name]; | 150 ++device_name_map_[device_name]; |
| 87 if (observer()) | 151 if (observer()) |
| 88 observer()->OnOptionAdded(device_names_and_ids_.size() - 1); | 152 observer()->OnOptionAdded(device_names_and_ids_.size() - 1); |
| 89 } | 153 } |
| 90 | 154 |
| 91 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { | 155 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { |
| 92 for (auto it = device_names_and_ids_.begin(); | 156 for (auto it = device_names_and_ids_.begin(); |
| 93 it != device_names_and_ids_.end(); ++it) { | 157 it != device_names_and_ids_.end(); ++it) { |
| 94 if (it->second == device_id) { | 158 if (it->second == device_id) { |
| 95 size_t index = it - device_names_and_ids_.begin(); | 159 size_t index = it - device_names_and_ids_.begin(); |
| 96 DCHECK_GT(device_name_map_[it->first], 0); | 160 DCHECK_GT(device_name_map_[it->first], 0); |
| 97 if (--device_name_map_[it->first] == 0) | 161 if (--device_name_map_[it->first] == 0) |
| 98 device_name_map_.erase(it->first); | 162 device_name_map_.erase(it->first); |
| 99 device_names_and_ids_.erase(it); | 163 device_names_and_ids_.erase(it); |
| 100 if (observer()) | 164 if (observer()) |
| 101 observer()->OnOptionRemoved(index); | 165 observer()->OnOptionRemoved(index); |
| 102 return; | 166 return; |
| 103 } | 167 } |
| 104 } | 168 } |
| 105 } | 169 } |
| 170 | |
| 171 void BluetoothChooserController::ClearAllDevices() { | |
| 172 device_names_and_ids_.clear(); | |
| 173 device_name_map_.clear(); | |
| 174 } | |
| OLD | NEW |