PageRenderTime 65ms CodeModel.GetById 42ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/update-engine/Core/KSPrefetchAction.m

http://macfuse.googlecode.com/
Objective C | 107 lines | 63 code | 17 blank | 27 comment | 9 complexity | 93c5192e31fab771be1f2edd6151de4a MD5 | raw file
  1// Copyright 2008 Google Inc.
  2//
  3// Licensed under the Apache License, Version 2.0 (the "License");
  4// you may not use this file except in compliance with the License.
  5// You may obtain a copy of the License at
  6//
  7//     http://www.apache.org/licenses/LICENSE-2.0
  8//
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15#import "KSPrefetchAction.h"
 16#import "KSUpdateEngine.h"
 17#import "KSActionPipe.h"
 18#import "KSDownloadAction.h"
 19#import "KSActionProcessor.h"
 20#import "KSUpdateInfo.h"
 21
 22
 23@implementation KSPrefetchAction
 24
 25+ (id)actionWithEngine:(KSUpdateEngine *)engine {
 26  return [[[self alloc] initWithEngine:engine] autorelease];
 27}
 28
 29- (id)init {
 30  return [self initWithEngine:nil];
 31}
 32
 33- (id)initWithEngine:(KSUpdateEngine *)engine {
 34  if ((self = [super init])) {
 35    engine_ = [engine retain];
 36    if (engine_ == nil) {
 37      [self release];
 38      return nil;
 39    }
 40  }
 41  return self;
 42}
 43
 44- (void)dealloc {
 45  [engine_ release];
 46  [super dealloc];
 47}
 48
 49- (void)performAction {  
 50  NSArray *availableUpdates = [[self inPipe] contents];
 51  // Our output must always be the same as our input, so we'll set that up now
 52  [[self outPipe] setContents:availableUpdates];
 53  
 54  if (availableUpdates == nil) {
 55    GTMLoggerInfo(@"no updates available.");
 56    [[self processor] finishedProcessing:self successfully:YES];
 57    return;
 58  }
 59
 60  _GTMDevAssert(engine_ != nil, @"engine_ must not be nil");
 61  
 62  // Send the available updates to the delegate to figure out which ones should
 63  // be prefetched. The delegate will return an array of product dictionaries
 64  // that we should prefetch.
 65  //
 66  // Security note:
 67  // The delegate is untrusted so we can't trust the product dictionaries that
 68  // we get back. So, we use the returned product dictionaries to filter our
 69  // original list of |availableUpdates| to the ones the delegate requested.
 70  NSArray *updatesToPrefetch = [engine_ action:self
 71                        shouldPrefetchProducts:availableUpdates];
 72  
 73  // Filter our list of available updates to only those that the delegate told
 74  // us to prefetch.
 75  NSArray *prefetches =
 76    [availableUpdates filteredArrayUsingPredicate:
 77     [NSPredicate predicateWithFormat:
 78      @"SELF IN %@", updatesToPrefetch]];
 79  
 80  // Use -description because it prints nicer than the way CF would format it
 81  GTMLoggerInfo(@"prefetches=%@", [prefetches description]);
 82  
 83  // Convert each dictionary in |prefetches| into a KSDownloadAction and
 84  // enqueue it on our subProcessor
 85  NSEnumerator *prefetchEnumerator = [prefetches objectEnumerator];
 86  KSUpdateInfo *info = nil;
 87  while ((info = [prefetchEnumerator nextObject])) {
 88    NSString *dmgName =
 89    [[info productID] stringByAppendingPathExtension:@"dmg"];
 90    KSAction *action =
 91    [KSDownloadAction actionWithURL:[info codebaseURL]
 92                               size:[[info codeSize] intValue]
 93                               hash:[info codeHash]
 94                               name:dmgName];
 95    [[self subProcessor] enqueueAction:action];
 96  }
 97
 98  if ([[[self subProcessor] actions] count] == 0) {
 99    GTMLoggerInfo(@"No prefetch downloads created.");
100    [[self processor] finishedProcessing:self successfully:YES];
101    return;
102  }
103  
104  [[self subProcessor] startProcessing];
105}
106
107@end