1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/local_discovery/storage/privet_volume_lister.h" 6 7 #include "base/bind.h" 8 #include "base/message_loop/message_loop.h" 9 #include "base/time/time.h" 10 #include "chrome/browser/local_discovery/storage/privet_filesystem_constants.h" 11 12 #if defined(ENABLE_SERVICE_DISCOVERY) 13 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" 14 #endif 15 16 namespace local_discovery { 17 18 namespace { 19 20 #if defined(ENABLE_SERVICE_DISCOVERY) 21 const int kVolumeSearchDurationMs = 10000; 22 23 std::string EscapeSlashes(const std::string& str) { 24 std::string output = ""; 25 for (size_t i = 0; i < str.length(); i++) { 26 switch (str[i]) { 27 case '/': 28 output += "$s"; 29 break; 30 case '\\': 31 output += "$b"; 32 break; 33 case '$': 34 output += "$$"; 35 break; 36 default: 37 output += str[i]; 38 } 39 } 40 41 return output; 42 } 43 44 std::string RemoveSlashes(const std::string& str) { 45 std::string output = ""; 46 for (size_t i = 0; i < str.length(); i++) { 47 switch (str[i]) { 48 case '/': 49 case '\\': 50 break; 51 default: 52 output += str[i]; 53 } 54 } 55 56 return output; 57 } 58 #endif // ENABLE_SERVICE_DISCOVERY 59 60 } // namespace 61 62 PrivetVolumeLister::PrivetVolumeLister(const ResultCallback& callback) 63 : callback_(callback), weak_factory_(this) { 64 } 65 66 PrivetVolumeLister::~PrivetVolumeLister() { 67 } 68 69 void PrivetVolumeLister::Start() { 70 #if defined(ENABLE_SERVICE_DISCOVERY) 71 service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance(); 72 privet_lister_.reset(new PrivetDeviceListerImpl(service_discovery_client_, 73 this)); 74 privet_lister_->Start(); 75 privet_lister_->DiscoverNewDevices(false); 76 base::MessageLoop::current()->PostDelayedTask( 77 FROM_HERE, 78 base::Bind(&PrivetVolumeLister::FinishSearch, 79 weak_factory_.GetWeakPtr()), 80 base::TimeDelta::FromMilliseconds(kVolumeSearchDurationMs)); 81 #else 82 callback_.Run(std::vector<VolumeInfo>()); 83 #endif 84 } 85 86 #if defined(ENABLE_SERVICE_DISCOVERY) 87 void PrivetVolumeLister::DeviceChanged(bool added, 88 const std::string& name, 89 const DeviceDescription& description) { 90 if (added && description.type == kPrivetTypeStorage) { 91 VolumeInfo volume_info; 92 volume_info.volume_id = name; 93 volume_info.volume_label = description.name; 94 base::FilePath mount_root = base::FilePath( 95 local_discovery::kPrivetFilePath); 96 97 // HACK(noamsml): Add extra dummy folder with the volume label so that the 98 // file browser displays the correct volume label. 99 100 std::string volume_id = EscapeSlashes(volume_info.volume_id); 101 std::string volume_label = RemoveSlashes(volume_info.volume_label); 102 103 volume_info.volume_path = 104 mount_root.AppendASCII(volume_id).AppendASCII(volume_label); 105 available_volumes_.push_back(volume_info); 106 } 107 } 108 109 void PrivetVolumeLister::DeviceRemoved(const std::string& name) { 110 } 111 112 void PrivetVolumeLister::DeviceCacheFlushed() { 113 } 114 115 void PrivetVolumeLister::FinishSearch() { 116 privet_lister_.reset(); 117 service_discovery_client_ = NULL; 118 available_volumes_.swap(canonical_volume_list_); 119 callback_.Run(canonical_volume_list_); 120 } 121 #endif 122 123 } // namespace local_discovery 124