| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/browser/memory/memory_debugger.h" | 5 #import "ios/chrome/browser/memory/memory_debugger.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/ios/ios_util.h" | |
| 12 #import "base/mac/scoped_nsobject.h" | 11 #import "base/mac/scoped_nsobject.h" |
| 13 #import "ios/chrome/browser/memory/memory_metrics.h" | 12 #import "ios/chrome/browser/memory/memory_metrics.h" |
| 14 #include "ios/chrome/browser/ui/ui_util.h" | 13 #include "ios/chrome/browser/ui/ui_util.h" |
| 15 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 14 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 // The number of bytes in a megabyte. | 17 // The number of bytes in a megabyte. |
| 19 const CGFloat kNumBytesInMB = 1024 * 1024; | 18 const CGFloat kNumBytesInMB = 1024 * 1024; |
| 20 // The horizontal and vertical padding between subviews. | 19 // The horizontal and vertical padding between subviews. |
| 21 const CGFloat kPadding = 10; | 20 const CGFloat kPadding = 10; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 } | 52 } |
| 54 | 53 |
| 55 - (instancetype)init { | 54 - (instancetype)init { |
| 56 self = [super initWithFrame:CGRectZero]; | 55 self = [super initWithFrame:CGRectZero]; |
| 57 if (self) { | 56 if (self) { |
| 58 _font.reset([[UIFont systemFontOfSize:14] retain]); | 57 _font.reset([[UIFont systemFontOfSize:14] retain]); |
| 59 self.backgroundColor = [UIColor colorWithWhite:0.8f alpha:0.9f]; | 58 self.backgroundColor = [UIColor colorWithWhite:0.8f alpha:0.9f]; |
| 60 self.opaque = NO; | 59 self.opaque = NO; |
| 61 | 60 |
| 62 [self addSubviews]; | 61 [self addSubviews]; |
| 63 [self adjustForOrientation:nil]; | |
| 64 [self sizeToFit]; | 62 [self sizeToFit]; |
| 65 [self registerForNotifications]; | 63 [self registerForNotifications]; |
| 66 } | 64 } |
| 67 return self; | 65 return self; |
| 68 } | 66 } |
| 69 | 67 |
| 70 // NSTimers create a retain cycle so they must be invalidated before this | 68 // NSTimers create a retain cycle so they must be invalidated before this |
| 71 // instance can be deallocated. | 69 // instance can be deallocated. |
| 72 - (void)invalidateTimers { | 70 - (void)invalidateTimers { |
| 73 [_refreshTimer invalidate]; | 71 [_refreshTimer invalidate]; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 [self addLabelWithText:@"Set refresh interval (secs)" | 160 [self addLabelWithText:@"Set refresh interval (secs)" |
| 163 input:_refreshField | 161 input:_refreshField |
| 164 inputTarget:self | 162 inputTarget:self |
| 165 inputAction:@selector(updateRefreshInterval) | 163 inputAction:@selector(updateRefreshInterval) |
| 166 atIndex:index++]; | 164 atIndex:index++]; |
| 167 [_refreshField setText:@"0.5"]; | 165 [_refreshField setText:@"0.5"]; |
| 168 [self updateRefreshInterval]; | 166 [self updateRefreshInterval]; |
| 169 } | 167 } |
| 170 | 168 |
| 171 - (void)registerForNotifications { | 169 - (void)registerForNotifications { |
| 172 // On iOS 7, the screen coordinate system is not dependent on orientation so | |
| 173 // the debugger has to handle its own rotation. | |
| 174 if (!base::ios::IsRunningOnIOS8OrLater()) { | |
| 175 // Register to receive orientation notifications. | |
| 176 [[NSNotificationCenter defaultCenter] | |
| 177 addObserver:self | |
| 178 selector:@selector(adjustForOrientation:) | |
| 179 name:UIDeviceOrientationDidChangeNotification | |
| 180 object:nil]; | |
| 181 } | |
| 182 | |
| 183 // Register to receive memory warning. | 170 // Register to receive memory warning. |
| 184 [[NSNotificationCenter defaultCenter] | 171 [[NSNotificationCenter defaultCenter] |
| 185 addObserver:self | 172 addObserver:self |
| 186 selector:@selector(lowMemoryWarningReceived:) | 173 selector:@selector(lowMemoryWarningReceived:) |
| 187 name:UIApplicationDidReceiveMemoryWarningNotification | 174 name:UIApplicationDidReceiveMemoryWarningNotification |
| 188 object:nil]; | 175 object:nil]; |
| 189 | 176 |
| 190 // Register to receive keyboard will show notification. | 177 // Register to receive keyboard will show notification. |
| 191 [[NSNotificationCenter defaultCenter] | 178 [[NSNotificationCenter defaultCenter] |
| 192 addObserver:self | 179 addObserver:self |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 } | 359 } |
| 373 | 360 |
| 374 #pragma mark Rotation notification callback | 361 #pragma mark Rotation notification callback |
| 375 | 362 |
| 376 - (void)didMoveToSuperview { | 363 - (void)didMoveToSuperview { |
| 377 UIView* superview = [self superview]; | 364 UIView* superview = [self superview]; |
| 378 if (superview) | 365 if (superview) |
| 379 [self setCenter:[superview center]]; | 366 [self setCenter:[superview center]]; |
| 380 } | 367 } |
| 381 | 368 |
| 382 - (void)adjustForOrientation:(NSNotification*)notification { | |
| 383 if (base::ios::IsRunningOnIOS8OrLater()) { | |
| 384 return; | |
| 385 } | |
| 386 UIInterfaceOrientation orientation = | |
| 387 [[UIApplication sharedApplication] statusBarOrientation]; | |
| 388 if (orientation == _currentOrientation) { | |
| 389 return; | |
| 390 } | |
| 391 _currentOrientation = orientation; | |
| 392 CGFloat angle; | |
| 393 switch (orientation) { | |
| 394 case UIInterfaceOrientationPortrait: | |
| 395 angle = 0; | |
| 396 break; | |
| 397 case UIInterfaceOrientationPortraitUpsideDown: | |
| 398 angle = M_PI; | |
| 399 break; | |
| 400 case UIInterfaceOrientationLandscapeLeft: | |
| 401 angle = -M_PI_2; | |
| 402 break; | |
| 403 case UIInterfaceOrientationLandscapeRight: | |
| 404 angle = M_PI_2; | |
| 405 break; | |
| 406 case UIInterfaceOrientationUnknown: | |
| 407 default: | |
| 408 angle = 0; | |
| 409 } | |
| 410 | |
| 411 // Since the debugger view is in screen coordinates and handles its own | |
| 412 // rotation via the |transform| property, the view's position after rotation | |
| 413 // can be unexpected and partially off-screen. Centering the view before | |
| 414 // rotating it ensures that the view remains within the bounds of the screen. | |
| 415 if (self.superview) { | |
| 416 self.center = self.superview.center; | |
| 417 } | |
| 418 self.transform = CGAffineTransformMakeRotation(angle); | |
| 419 } | |
| 420 | |
| 421 #pragma mark Keyboard notification callbacks | 369 #pragma mark Keyboard notification callbacks |
| 422 | 370 |
| 423 // Ensures the debugger is visible by shifting it up as the keyboard animates | 371 // Ensures the debugger is visible by shifting it up as the keyboard animates |
| 424 // in. | 372 // in. |
| 425 - (void)keyboardWillShow:(NSNotification*)notification { | 373 - (void)keyboardWillShow:(NSNotification*)notification { |
| 426 NSDictionary* userInfo = [notification userInfo]; | 374 NSDictionary* userInfo = [notification userInfo]; |
| 427 NSValue* keyboardFrameValue = | 375 NSValue* keyboardFrameValue = |
| 428 [userInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; | 376 [userInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; |
| 429 CGFloat keyboardHeight = CurrentKeyboardHeight(keyboardFrameValue); | 377 CGFloat keyboardHeight = CurrentKeyboardHeight(keyboardFrameValue); |
| 430 | 378 |
| 431 // Get the coord of the bottom of the debugger's frame. This is orientation | 379 // Get the coord of the bottom of the debugger's frame. |
| 432 // dependent on iOS 7 because the debugger is in screen coords. | |
| 433 CGFloat bottomOfFrame = CGRectGetMaxY(self.frame); | 380 CGFloat bottomOfFrame = CGRectGetMaxY(self.frame); |
| 434 if (!base::ios::IsRunningOnIOS8OrLater() && IsLandscape()) | |
| 435 bottomOfFrame = CGRectGetMaxX(self.frame); | |
| 436 | 381 |
| 437 // Shift the debugger up by the "height" of the keyboard, but since the | 382 // Shift the debugger up by the "height" of the keyboard, but since the |
| 438 // keyboard rect is in screen coords, use the orientation to find the height. | 383 // keyboard rect is in screen coords, use the orientation to find the height. |
| 439 CGFloat distanceFromBottom = CurrentScreenHeight() - bottomOfFrame; | 384 CGFloat distanceFromBottom = CurrentScreenHeight() - bottomOfFrame; |
| 440 _keyboardOffset = -1 * fmax(0.0f, keyboardHeight - distanceFromBottom); | 385 _keyboardOffset = -1 * fmax(0.0f, keyboardHeight - distanceFromBottom); |
| 441 [self animateForKeyboardNotification:notification | 386 [self animateForKeyboardNotification:notification |
| 442 withOffset:CGPointMake(0, _keyboardOffset)]; | 387 withOffset:CGPointMake(0, _keyboardOffset)]; |
| 443 } | 388 } |
| 444 | 389 |
| 445 // Shifts the debugger back down when the keyboard is hidden. | 390 // Shifts the debugger back down when the keyboard is hidden. |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 [alert addAction:[UIAlertAction actionWithTitle:@"OK" | 544 [alert addAction:[UIAlertAction actionWithTitle:@"OK" |
| 600 style:UIAlertActionStyleDefault | 545 style:UIAlertActionStyleDefault |
| 601 handler:nil]]; | 546 handler:nil]]; |
| 602 [[[[UIApplication sharedApplication] keyWindow] rootViewController] | 547 [[[[UIApplication sharedApplication] keyWindow] rootViewController] |
| 603 presentViewController:alert | 548 presentViewController:alert |
| 604 animated:YES | 549 animated:YES |
| 605 completion:nil]; | 550 completion:nil]; |
| 606 } | 551 } |
| 607 | 552 |
| 608 @end | 553 @end |
| OLD | NEW |