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

Side by Side Diff: chrome/browser/cocoa/custom_home_pages_model.mm

Issue 3023023: [Mac] Fix the custom homepages preferences. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Created 10 years, 4 months 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
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/cocoa/custom_home_pages_model.h" 5 #import "chrome/browser/cocoa/custom_home_pages_model.h"
6 6
7 #include "base/sys_string_conversions.h" 7 #include "base/sys_string_conversions.h"
8 #include "chrome/browser/history/history.h"
9 #include "chrome/browser/net/url_fixer_upper.h" 8 #include "chrome/browser/net/url_fixer_upper.h"
9 #include "chrome/browser/session_startup_pref.h"
10 10
11 NSString* const kHomepageEntryChangedNotification = 11 NSString* const kHomepageEntryChangedNotification =
12 @"kHomepageEntryChangedNotification"; 12 @"kHomepageEntryChangedNotification";
13 13
14 // An entry representing a single item in the custom home page model. Stores 14 @interface CustomHomePagesModel (Private)
15 // a url and a favicon. 15 - (void)setURLsInternal:(const std::vector<GURL>&)urls;
16 @interface CustomHomePageEntry : NSObject {
17 @private
18 scoped_nsobject<NSString> url_;
19 scoped_nsobject<NSImage> icon_;
20
21 // If non-zero, indicates we're loading the favicon for the page.
22 HistoryService::Handle icon_handle_;
23 }
24 @property(nonatomic, copy) NSString* URL;
25 @property(nonatomic, retain) NSImage* image;
26 @end 16 @end
27 17
28 //----------------------------------------------------------------------------
29
30
31 @implementation CustomHomePagesModel 18 @implementation CustomHomePagesModel
32 19
33 - (id)initWithProfile:(Profile*)profile { 20 - (id)initWithProfile:(Profile*)profile {
34 if ((self = [super init])) { 21 if ((self = [super init])) {
35 profile_ = profile; 22 profile_ = profile;
36 entries_.reset([[NSMutableArray alloc] init]); 23 entries_.reset([[NSMutableArray alloc] init]);
37 } 24 }
38 return self; 25 return self;
39 } 26 }
40 27
41 - (NSUInteger)countOfCustomHomePages { 28 - (NSUInteger)countOfCustomHomePages {
42 return [entries_ count]; 29 return [entries_ count];
43 } 30 }
44 31
45 - (id)objectInCustomHomePagesAtIndex:(NSUInteger)index { 32 - (id)objectInCustomHomePagesAtIndex:(NSUInteger)index {
46 return [entries_ objectAtIndex:index]; 33 return [entries_ objectAtIndex:index];
47 } 34 }
48 35
49 - (void)insertObject:(id)object inCustomHomePagesAtIndex:(NSUInteger)index { 36 - (void)insertObject:(id)object inCustomHomePagesAtIndex:(NSUInteger)index {
50 [entries_ insertObject:object atIndex:index]; 37 [entries_ insertObject:object atIndex:index];
51 } 38 }
52 39
53 - (void)removeObjectFromCustomHomePagesAtIndex:(NSUInteger)index { 40 - (void)removeObjectFromCustomHomePagesAtIndex:(NSUInteger)index {
54 [entries_ removeObjectAtIndex:index]; 41 [entries_ removeObjectAtIndex:index];
42 // Force a save.
43 [self validateURLs];
55 } 44 }
56 45
57 // Get/set the urls the model currently contains as a group. These will weed 46 // Get/set the urls the model currently contains as a group. These will weed
58 // out any URLs that are empty and not add them to the model. As a result, 47 // out any URLs that are empty and not add them to the model. As a result,
59 // the next time they're persisted to the prefs backend, they'll disappear. 48 // the next time they're persisted to the prefs backend, they'll disappear.
60 - (std::vector<GURL>)URLs { 49 - (std::vector<GURL>)URLs {
61 std::vector<GURL> urls; 50 std::vector<GURL> urls;
62 for (CustomHomePageEntry* entry in entries_.get()) { 51 for (CustomHomePageEntry* entry in entries_.get()) {
63 const char* urlString = [[entry URL] UTF8String]; 52 const char* urlString = [[entry URL] UTF8String];
64 if (urlString && std::strlen(urlString)) { 53 if (urlString && std::strlen(urlString)) {
65 urls.push_back(GURL(std::string(urlString))); 54 urls.push_back(GURL(std::string(urlString)));
66 } 55 }
67 } 56 }
68 return urls; 57 return urls;
69 } 58 }
70 59
71 - (void)setURLs:(const std::vector<GURL>&)urls { 60 - (void)setURLs:(const std::vector<GURL>&)urls {
72 [self willChangeValueForKey:@"customHomePages"]; 61 [self willChangeValueForKey:@"customHomePages"];
62 [self setURLsInternal:urls];
63 SessionStartupPref pref(SessionStartupPref::GetStartupPref(profile_));
64 pref.urls = urls;
65 SessionStartupPref::SetStartupPref(profile_, pref);
66 [self didChangeValueForKey:@"customHomePages"];
67 }
68
69 // Converts the C++ URLs to Cocoa objects without notifying KVO.
pink (ping after 24hrs) 2010/07/28 14:49:18 Should you explain why you don't want to notify kv
70 - (void)setURLsInternal:(const std::vector<GURL>&)urls {
73 [entries_ removeAllObjects]; 71 [entries_ removeAllObjects];
74 for (size_t i = 0; i < urls.size(); ++i) { 72 for (size_t i = 0; i < urls.size(); ++i) {
75 scoped_nsobject<CustomHomePageEntry> entry( 73 scoped_nsobject<CustomHomePageEntry> entry(
76 [[CustomHomePageEntry alloc] init]); 74 [[CustomHomePageEntry alloc] init]);
77 const char* urlString = urls[i].spec().c_str(); 75 const char* urlString = urls[i].spec().c_str();
78 if (urlString && std::strlen(urlString)) { 76 if (urlString && std::strlen(urlString)) {
79 [entry setURL:[NSString stringWithCString:urlString 77 [entry setURL:[NSString stringWithCString:urlString
80 encoding:NSUTF8StringEncoding]]; 78 encoding:NSUTF8StringEncoding]];
81 [entries_ addObject:entry]; 79 [entries_ addObject:entry];
82 } 80 }
83 } 81 }
82 }
83
84 - (void)reloadURLs {
85 [self willChangeValueForKey:@"customHomePages"];
86 SessionStartupPref pref(SessionStartupPref::GetStartupPref(profile_));
87 [self setURLsInternal:pref.urls];
84 [self didChangeValueForKey:@"customHomePages"]; 88 [self didChangeValueForKey:@"customHomePages"];
85 } 89 }
86 90
87 - (void)validateURLs { 91 - (void)validateURLs {
88 [self setURLs:[self URLs]]; 92 [self setURLs:[self URLs]];
89 } 93 }
90 94
91 - (void)setURLStringEmptyAt:(NSUInteger)index { 95 - (void)setURLStringEmptyAt:(NSUInteger)index {
92 // This replaces the data at |index| with an empty (invalid) URL string. 96 // This replaces the data at |index| with an empty (invalid) URL string.
93 CustomHomePageEntry* entry = [entries_ objectAtIndex:index]; 97 CustomHomePageEntry* entry = [entries_ objectAtIndex:index];
(...skipping 28 matching lines...) Expand all
122 } 126 }
123 127
124 - (void)setImage:(NSImage*)image { 128 - (void)setImage:(NSImage*)image {
125 icon_.reset(image); 129 icon_.reset(image);
126 } 130 }
127 131
128 - (NSImage*)image { 132 - (NSImage*)image {
129 return icon_.get(); 133 return icon_.get();
130 } 134 }
131 135
136 - (NSString*)description {
137 return url_.get();
138 }
139
132 @end 140 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/custom_home_pages_model.h ('k') | chrome/browser/cocoa/custom_home_pages_model_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698