summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/custom_home_pages_model.mm
blob: 9d47c415cadb649a34b83768edfec429d447210c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/cocoa/custom_home_pages_model.h"

#include "base/sys_string_conversions.h"
#include "chrome/browser/net/url_fixer_upper.h"
#include "chrome/browser/session_startup_pref.h"

NSString* const kHomepageEntryChangedNotification =
    @"kHomepageEntryChangedNotification";

@interface CustomHomePagesModel (Private)
- (void)setURLsInternal:(const std::vector<GURL>&)urls;
@end

@implementation CustomHomePagesModel

- (id)initWithProfile:(Profile*)profile {
  if ((self = [super init])) {
    profile_ = profile;
    entries_.reset([[NSMutableArray alloc] init]);
  }
  return self;
}

- (NSUInteger)countOfCustomHomePages {
  return [entries_ count];
}

- (id)objectInCustomHomePagesAtIndex:(NSUInteger)index {
  return [entries_ objectAtIndex:index];
}

- (void)insertObject:(id)object inCustomHomePagesAtIndex:(NSUInteger)index {
  [entries_ insertObject:object atIndex:index];
}

- (void)removeObjectFromCustomHomePagesAtIndex:(NSUInteger)index {
  [entries_ removeObjectAtIndex:index];
  // Force a save.
  [self validateURLs];
}

// Get/set the urls the model currently contains as a group. These will weed
// out any URLs that are empty and not add them to the model. As a result,
// the next time they're persisted to the prefs backend, they'll disappear.
- (std::vector<GURL>)URLs {
  std::vector<GURL> urls;
  for (CustomHomePageEntry* entry in entries_.get()) {
    const char* urlString = [[entry URL] UTF8String];
    if (urlString && std::strlen(urlString)) {
      urls.push_back(GURL(std::string(urlString)));
    }
  }
  return urls;
}

- (void)setURLs:(const std::vector<GURL>&)urls {
  [self willChangeValueForKey:@"customHomePages"];
  [self setURLsInternal:urls];
  SessionStartupPref pref(SessionStartupPref::GetStartupPref(profile_));
  pref.urls = urls;
  SessionStartupPref::SetStartupPref(profile_, pref);
  [self didChangeValueForKey:@"customHomePages"];
}

// Converts the C++ URLs to Cocoa objects without notifying KVO.
- (void)setURLsInternal:(const std::vector<GURL>&)urls {
  [entries_ removeAllObjects];
  for (size_t i = 0; i < urls.size(); ++i) {
    scoped_nsobject<CustomHomePageEntry> entry(
        [[CustomHomePageEntry alloc] init]);
    const char* urlString = urls[i].spec().c_str();
    if (urlString && std::strlen(urlString)) {
      [entry setURL:[NSString stringWithCString:urlString
                                       encoding:NSUTF8StringEncoding]];
      [entries_ addObject:entry];
    }
  }
}

- (void)reloadURLs {
  [self willChangeValueForKey:@"customHomePages"];
  SessionStartupPref pref(SessionStartupPref::GetStartupPref(profile_));
  [self setURLsInternal:pref.urls];
  [self didChangeValueForKey:@"customHomePages"];
}

- (void)validateURLs {
  [self setURLs:[self URLs]];
}

- (void)setURLStringEmptyAt:(NSUInteger)index {
  // This replaces the data at |index| with an empty (invalid) URL string.
  CustomHomePageEntry* entry = [entries_ objectAtIndex:index];
  [entry setURL:[NSString stringWithString:@""]];
}

@end

//---------------------------------------------------------------------------

@implementation CustomHomePageEntry

- (void)setURL:(NSString*)url {
  // |url| can be nil if the user cleared the text from the edit field.
  if (!url)
    url = [NSString stringWithString:@""];

  // Make sure the url is valid before setting it by fixing it up.
  std::string fixedUrl(URLFixerUpper::FixupURL(
      base::SysNSStringToUTF8(url), std::string()).possibly_invalid_spec());
  url_.reset([base::SysUTF8ToNSString(fixedUrl) retain]);

  // Broadcast that an individual item has changed.
  [[NSNotificationCenter defaultCenter]
      postNotificationName:kHomepageEntryChangedNotification object:nil];

  // TODO(pinkerton): fetch favicon, convert to NSImage http://crbug.com/34642
}

- (NSString*)URL {
  return url_.get();
}

- (void)setImage:(NSImage*)image {
  icon_.reset(image);
}

- (NSImage*)image {
  return icon_.get();
}

- (NSString*)description {
  return url_.get();
}

@end