1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #import "chrome/browser/ui/cocoa/applescript/bookmark_item_applescript.h" 6 7 #include "base/strings/sys_string_conversions.h" 8 #include "chrome/browser/profiles/profile_manager.h" 9 #import "chrome/browser/ui/cocoa/applescript/error_applescript.h" 10 #include "components/bookmarks/browser/bookmark_model.h" 11 12 @interface BookmarkItemAppleScript() 13 @property (nonatomic, copy) NSString* tempURL; 14 @end 15 16 @implementation BookmarkItemAppleScript 17 18 @synthesize tempURL = tempURL_; 19 20 - (id)init { 21 if ((self = [super init])) { 22 [self setTempURL:@""]; 23 } 24 return self; 25 } 26 27 - (void)dealloc { 28 [tempURL_ release]; 29 [super dealloc]; 30 } 31 32 - (void)setBookmarkNode:(const BookmarkNode*)aBookmarkNode { 33 [super setBookmarkNode:aBookmarkNode]; 34 [self setURL:[self tempURL]]; 35 } 36 37 - (NSString*)URL { 38 if (!bookmarkNode_) 39 return tempURL_; 40 41 return base::SysUTF8ToNSString(bookmarkNode_->url().spec()); 42 } 43 44 - (void)setURL:(NSString*)aURL { 45 // If a scripter sets a URL before the node is added, URL is saved at a 46 // temporary location. 47 if (!bookmarkNode_) { 48 [self setTempURL:aURL]; 49 return; 50 } 51 52 BookmarkModel* model = [self bookmarkModel]; 53 if (!model) 54 return; 55 56 GURL url(base::SysNSStringToUTF8(aURL)); 57 if (!url.is_valid()) { 58 AppleScript::SetError(AppleScript::errInvalidURL); 59 return; 60 } 61 62 model->SetURL(bookmarkNode_, url); 63 } 64 65 @end 66