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

Side by Side Diff: chrome/browser/ui/cocoa/view_id_util.mm

Issue 5711001: Add a new GetInstance() method for remaining files with singleton classes under chrome/browser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 10 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "chrome/browser/ui/cocoa/view_id_util.h" 5 #import "chrome/browser/ui/cocoa/view_id_util.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
9 #include <map> 9 #include <map>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/lazy_instance.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/singleton.h"
14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 14 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
15 #import "chrome/browser/ui/cocoa/tab_strip_controller.h" 15 #import "chrome/browser/ui/cocoa/tab_strip_controller.h"
16 16
17 namespace { 17 namespace {
18 18
19 // TODO(suzhe): After migrating to Mac OS X 10.6, we may use Objective-C's new 19 // TODO(suzhe): After migrating to Mac OS X 10.6, we may use Objective-C's new
20 // "Associative References" feature to attach the ViewID to the view directly 20 // "Associative References" feature to attach the ViewID to the view directly
21 // rather than using a separated map. 21 // rather than using a separated map.
22 typedef std::map<NSView*, ViewID> ViewIDMap; 22 typedef std::map<NSView*, ViewID> ViewIDMap;
23 23
24 static base::LazyInstance<ViewIDMap> g_view_id_map(base::LINKER_INITIALIZED);
25
24 // Returns the view's nearest descendant (including itself) with a specific 26 // Returns the view's nearest descendant (including itself) with a specific
25 // ViewID, or nil if no subview has that ViewID. 27 // ViewID, or nil if no subview has that ViewID.
26 NSView* FindViewWithID(NSView* view, ViewID viewID) { 28 NSView* FindViewWithID(NSView* view, ViewID viewID) {
27 if ([view viewID] == viewID) 29 if ([view viewID] == viewID)
28 return view; 30 return view;
29 31
30 for (NSView* subview in [view subviews]) { 32 for (NSView* subview in [view subviews]) {
31 NSView* result = FindViewWithID(subview, viewID); 33 NSView* result = FindViewWithID(subview, viewID);
32 if (result != nil) 34 if (result != nil)
33 return result; 35 return result;
34 } 36 }
35 return nil; 37 return nil;
36 } 38 }
37 39
38 } // anonymous namespace 40 } // anonymous namespace
39 41
40 namespace view_id_util { 42 namespace view_id_util {
41 43
42 void SetID(NSView* view, ViewID viewID) { 44 void SetID(NSView* view, ViewID viewID) {
43 DCHECK(view); 45 DCHECK(view);
44 DCHECK(viewID != VIEW_ID_NONE); 46 DCHECK(viewID != VIEW_ID_NONE);
45 // We handle VIEW_ID_TAB_0 to VIEW_ID_TAB_LAST in GetView() function directly. 47 // We handle VIEW_ID_TAB_0 to VIEW_ID_TAB_LAST in GetView() function directly.
46 DCHECK(!((viewID >= VIEW_ID_TAB_0) && (viewID <= VIEW_ID_TAB_LAST))); 48 DCHECK(!((viewID >= VIEW_ID_TAB_0) && (viewID <= VIEW_ID_TAB_LAST)));
47 (*Singleton<ViewIDMap>::get())[view] = viewID; 49 g_view_id_map.Get()[view] = viewID;
48 } 50 }
49 51
50 void UnsetID(NSView* view) { 52 void UnsetID(NSView* view) {
51 DCHECK(view); 53 DCHECK(view);
52 Singleton<ViewIDMap>::get()->erase(view); 54 g_view_id_map.Get().erase(view);
53 } 55 }
54 56
55 NSView* GetView(NSWindow* window, ViewID viewID) { 57 NSView* GetView(NSWindow* window, ViewID viewID) {
56 DCHECK(viewID != VIEW_ID_NONE); 58 DCHECK(viewID != VIEW_ID_NONE);
57 DCHECK(window); 59 DCHECK(window);
58 60
59 // As tabs can be created, destroyed or rearranged dynamically, we handle them 61 // As tabs can be created, destroyed or rearranged dynamically, we handle them
60 // here specially. 62 // here specially.
61 if (viewID >= VIEW_ID_TAB_0 && viewID <= VIEW_ID_TAB_LAST) { 63 if (viewID >= VIEW_ID_TAB_0 && viewID <= VIEW_ID_TAB_LAST) {
62 BrowserWindowController* windowController = [window windowController]; 64 BrowserWindowController* windowController = [window windowController];
63 DCHECK([windowController isKindOfClass:[BrowserWindowController class]]); 65 DCHECK([windowController isKindOfClass:[BrowserWindowController class]]);
64 TabStripController* tabStripController = 66 TabStripController* tabStripController =
65 [windowController tabStripController]; 67 [windowController tabStripController];
66 DCHECK(tabStripController); 68 DCHECK(tabStripController);
67 NSUInteger count = [tabStripController viewsCount]; 69 NSUInteger count = [tabStripController viewsCount];
68 DCHECK(count); 70 DCHECK(count);
69 NSUInteger index = 71 NSUInteger index =
70 (viewID == VIEW_ID_TAB_LAST ? count - 1 : viewID - VIEW_ID_TAB_0); 72 (viewID == VIEW_ID_TAB_LAST ? count - 1 : viewID - VIEW_ID_TAB_0);
71 return index < count ? [tabStripController viewAtIndex:index] : nil; 73 return index < count ? [tabStripController viewAtIndex:index] : nil;
72 } 74 }
73 75
74 return FindViewWithID([[window contentView] superview], viewID); 76 return FindViewWithID([[window contentView] superview], viewID);
75 } 77 }
76 78
77 } // namespace view_id_util 79 } // namespace view_id_util
78 80
79 @implementation NSView (ViewID) 81 @implementation NSView (ViewID)
80 82
81 - (ViewID)viewID { 83 - (ViewID)viewID {
82 ViewIDMap* map = Singleton<ViewIDMap>::get(); 84 ViewIDMap* map = g_view_id_map.Pointer();
83 ViewIDMap::const_iterator iter = map->find(self); 85 ViewIDMap::const_iterator iter = map->find(self);
84 return iter != map->end() ? iter->second : VIEW_ID_NONE; 86 return iter != map->end() ? iter->second : VIEW_ID_NONE;
85 } 87 }
86 88
87 @end 89 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698