| 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 #import "ios/chrome/common/physical_web/physical_web_scanner.h" | 5 #import "ios/chrome/common/physical_web/physical_web_scanner.h" |
| 6 | 6 |
| 7 #import <CoreBluetooth/CoreBluetooth.h> | 7 #import <CoreBluetooth/CoreBluetooth.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #import "base/ios/weak_nsobject.h" | 12 #import "base/ios/weak_nsobject.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #import "base/mac/scoped_nsobject.h" | 14 #import "base/mac/scoped_nsobject.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "components/physical_web/data_source/physical_web_data_source.h" | 18 #include "components/physical_web/data_source/physical_web_data_source.h" |
| 19 #include "device/bluetooth/uribeacon/uri_encoder.h" | 19 #include "device/bluetooth/uribeacon/uri_encoder.h" |
| 20 #import "ios/chrome/common/physical_web/physical_web_device.h" | 20 #import "ios/chrome/common/physical_web/physical_web_device.h" |
| 21 #import "ios/chrome/common/physical_web/physical_web_request.h" | 21 #import "ios/chrome/common/physical_web/physical_web_request.h" |
| 22 #import "ios/chrome/common/physical_web/physical_web_types.h" | 22 #import "ios/chrome/common/physical_web/physical_web_types.h" |
| 23 | 23 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." |
| 26 #endif |
| 27 |
| 24 namespace { | 28 namespace { |
| 25 | 29 |
| 26 NSString* const kUriBeaconServiceUUID = @"FED8"; | 30 NSString* const kUriBeaconServiceUUID = @"FED8"; |
| 27 NSString* const kEddystoneBeaconServiceUUID = @"FEAA"; | 31 NSString* const kEddystoneBeaconServiceUUID = @"FEAA"; |
| 28 | 32 |
| 29 // The length of time in seconds since a URL was last seen before it should be | 33 // The length of time in seconds since a URL was last seen before it should be |
| 30 // considered lost (ie, no longer nearby). | 34 // considered lost (ie, no longer nearby). |
| 31 const NSTimeInterval kLostThresholdSeconds = 15.0; | 35 const NSTimeInterval kLostThresholdSeconds = 15.0; |
| 32 // The time interval in seconds between checks for lost URLs. | 36 // The time interval in seconds between checks for lost URLs. |
| 33 const NSTimeInterval kUpdateIntervalSeconds = 6.0; | 37 const NSTimeInterval kUpdateIntervalSeconds = 6.0; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 return nil; | 128 return nil; |
| 125 } | 129 } |
| 126 | 130 |
| 127 - (void)dealloc { | 131 - (void)dealloc { |
| 128 [centralManager_ setDelegate:nil]; | 132 [centralManager_ setDelegate:nil]; |
| 129 centralManager_.reset(); | 133 centralManager_.reset(); |
| 130 if (updateTimer_.get()) { | 134 if (updateTimer_.get()) { |
| 131 [updateTimer_ invalidate]; | 135 [updateTimer_ invalidate]; |
| 132 updateTimer_.reset(); | 136 updateTimer_.reset(); |
| 133 } | 137 } |
| 134 [super dealloc]; | |
| 135 } | 138 } |
| 136 | 139 |
| 137 - (void)start { | 140 - (void)start { |
| 138 [self stop]; | 141 [self stop]; |
| 139 [finalUrls_ removeAllObjects]; | 142 [finalUrls_ removeAllObjects]; |
| 140 [devicesUrls_ removeAllObjects]; | 143 [devicesUrls_ removeAllObjects]; |
| 141 [devices_ removeAllObjects]; | 144 [devices_ removeAllObjects]; |
| 142 started_ = YES; | 145 started_ = YES; |
| 143 if ([self bluetoothEnabled]) | 146 if ([self bluetoothEnabled]) |
| 144 [self reallyStart]; | 147 [self reallyStart]; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 [updateTimer_ invalidate]; | 248 [updateTimer_ invalidate]; |
| 246 updateTimer_.reset(); | 249 updateTimer_.reset(); |
| 247 } | 250 } |
| 248 | 251 |
| 249 NSArray* serviceUUIDs = @[ | 252 NSArray* serviceUUIDs = @[ |
| 250 [CBUUID UUIDWithString:kUriBeaconServiceUUID], | 253 [CBUUID UUIDWithString:kUriBeaconServiceUUID], |
| 251 [CBUUID UUIDWithString:kEddystoneBeaconServiceUUID] | 254 [CBUUID UUIDWithString:kEddystoneBeaconServiceUUID] |
| 252 ]; | 255 ]; |
| 253 if (onLostDetectionEnabled_) { | 256 if (onLostDetectionEnabled_) { |
| 254 // Register a repeating timer to periodically check for lost URLs. | 257 // Register a repeating timer to periodically check for lost URLs. |
| 255 updateTimer_.reset( | 258 updateTimer_.reset([NSTimer |
| 256 [[NSTimer scheduledTimerWithTimeInterval:kUpdateIntervalSeconds | 259 scheduledTimerWithTimeInterval:kUpdateIntervalSeconds |
| 257 target:self | 260 target:self |
| 258 selector:@selector(onUpdateTimeElapsed:) | 261 selector:@selector(onUpdateTimeElapsed:) |
| 259 userInfo:nil | 262 userInfo:nil |
| 260 repeats:YES] retain]); | 263 repeats:YES]); |
| 261 } | 264 } |
| 262 [centralManager_ scanForPeripheralsWithServices:serviceUUIDs options:nil]; | 265 [centralManager_ scanForPeripheralsWithServices:serviceUUIDs options:nil]; |
| 263 } | 266 } |
| 264 | 267 |
| 265 - (void)reallyStop { | 268 - (void)reallyStop { |
| 266 if (updateTimer_.get()) { | 269 if (updateTimer_.get()) { |
| 267 [updateTimer_ invalidate]; | 270 [updateTimer_ invalidate]; |
| 268 updateTimer_.reset(); | 271 updateTimer_.reset(); |
| 269 } | 272 } |
| 270 | 273 |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 scanTimestamp:[NSDate date]]; | 468 scanTimestamp:[NSDate date]]; |
| 466 } | 469 } |
| 467 | 470 |
| 468 - (void)requestMetadataForDevice:(PhysicalWebDevice*)device { | 471 - (void)requestMetadataForDevice:(PhysicalWebDevice*)device { |
| 469 base::scoped_nsobject<PhysicalWebRequest> request( | 472 base::scoped_nsobject<PhysicalWebRequest> request( |
| 470 [[PhysicalWebRequest alloc] initWithDevice:device]); | 473 [[PhysicalWebRequest alloc] initWithDevice:device]); |
| 471 PhysicalWebRequest* strongRequest = request.get(); | 474 PhysicalWebRequest* strongRequest = request.get(); |
| 472 [pendingRequests_ addObject:strongRequest]; | 475 [pendingRequests_ addObject:strongRequest]; |
| 473 base::WeakNSObject<PhysicalWebScanner> weakSelf(self); | 476 base::WeakNSObject<PhysicalWebScanner> weakSelf(self); |
| 474 [request start:^(PhysicalWebDevice* device, NSError* error) { | 477 [request start:^(PhysicalWebDevice* device, NSError* error) { |
| 475 base::scoped_nsobject<PhysicalWebScanner> strongSelf([weakSelf retain]); | 478 base::scoped_nsobject<PhysicalWebScanner> strongSelf(weakSelf); |
| 476 if (!strongSelf) { | 479 if (!strongSelf) { |
| 477 return; | 480 return; |
| 478 } | 481 } |
| 479 // ignore if there's an error. | 482 // ignore if there's an error. |
| 480 if (!error) { | 483 if (!error) { |
| 481 if (![strongSelf.get()->finalUrls_ containsObject:[device url]]) { | 484 if (![strongSelf.get()->finalUrls_ containsObject:[device url]]) { |
| 482 [strongSelf.get()->devices_ addObject:device]; | 485 [strongSelf.get()->devices_ addObject:device]; |
| 483 [strongSelf.get()->delegate_ scannerUpdatedDevices:weakSelf]; | 486 [strongSelf.get()->delegate_ scannerUpdatedDevices:weakSelf]; |
| 484 [strongSelf.get()->finalUrls_ addObject:[device url]]; | 487 [strongSelf.get()->finalUrls_ addObject:[device url]]; |
| 485 } | 488 } |
| 486 } | 489 } |
| 487 [strongSelf.get()->pendingRequests_ removeObject:strongRequest]; | 490 [strongSelf.get()->pendingRequests_ removeObject:strongRequest]; |
| 488 }]; | 491 }]; |
| 489 } | 492 } |
| 490 | 493 |
| 491 @end | 494 @end |
| OLD | NEW |