Bug 51134 - Move loading related code from MemoryCache to CachedResourceLoader
Summary: Move loading related code from MemoryCache to CachedResourceLoader
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Page Loading (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC OS X 10.5
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-15 14:13 PST by Antti Koivisto
Modified: 2011-01-10 03:15 PST (History)
5 users (show)

See Also:


Attachments
refactor loading code out of MemoryCache (36.74 KB, patch)
2010-12-15 16:23 PST, Antti Koivisto
no flags Details | Formatted Diff | Diff
fix style (36.66 KB, patch)
2010-12-16 05:53 PST, Antti Koivisto
darin: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Antti Koivisto 2010-12-15 14:13:36 PST
Cache should not be the component that initiates loads. MemoryCache::requestResource and similar should be moved/merged to CachedResourceLoader. This is likely to simplify the code too.
Comment 1 Antti Koivisto 2010-12-15 16:23:49 PST
Created attachment 76707 [details]
refactor loading code out of MemoryCache

Move loading related code from MemoryCache to CachedResourceLoader
- Merge MemoryCache::requestResource to CachedResourceLoader::requestResource
- Merge MemoryCache::requestUserCSSStyleSheet to CachedResourceLoader::requestUserCSSStyleSheet
- Move MemoryCache::revalidateResource to CachedResourceLoader::revalidateResource
- Add MemoryCache::add
- Refactor the decision on whether to reload, revalidate or use the existing resource to 
  a single function, CachedResourceLoader::determineRevalidationPolicy
Comment 2 WebKit Review Bot 2010-12-15 16:25:31 PST
Attachment 76707 [details] did not pass style-queue:

Failed to run "['WebKitTools/Scripts/check-webkit-style', '--diff-files', u'WebCore/ChangeLog', u'WebCore/css/CSSImageValue.cpp', u'WebCore/loader/ImageLoader.cpp', u'WebCore/loader/cache/CachedImage.cpp', u'WebCore/loader/cache/CachedResource.cpp', u'WebCore/loader/cache/CachedResource.h', u'WebCore/loader/cache/CachedResourceLoader.cpp', u'WebCore/loader/cache/CachedResourceLoader.h', u'WebCore/loader/cache/MemoryCache.cpp', u'WebCore/loader/cache/MemoryCache.h']" exit_code: 1
WebCore/loader/cache/CachedResourceLoader.cpp:57:  A case label should not be indented, but line up with its switch statement.  [whitespace/indent] [4]
Total errors found: 1 in 10 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Antti Koivisto 2010-12-16 05:53:34 PST
Created attachment 76761 [details]
fix style
Comment 4 Nate Chapin 2010-12-16 16:15:23 PST
Comment on attachment 76761 [details]
fix style

View in context: https://bugs.webkit.org/attachment.cgi?id=76761&action=review

Looks good to me, just a couple pedantic questions/comments.

I'm going to refrain from r+ing to give other folks a chance to chime in, especially since I'm not very familiar with the MemoryCache piece of this.

> WebCore/loader/cache/CachedResourceLoader.cpp:76
> +    default:
> +        break;
> +    }
> +    return 0;

Should we have an ASSERT_NOT_REACHED() here?

> WebCore/loader/cache/CachedResourceLoader.cpp:259
> -    // FIXME: Consider letting the embedder block mixed content loads.
> +

I'm not really familiar with the mixed content policy code, is this FIXME not valid anymore?

> WebCore/loader/cache/CachedResourceLoader.cpp:276
> +    if (!url.isValid())
> +        return 0;
> +    
> +    if (!canRequest(type, url))
> +        return 0;

Is there any particular reason to split this out into 2 if statements?
Comment 5 Antti Koivisto 2010-12-17 01:39:55 PST
(In reply to comment #4)
> (From update of attachment 76761 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=76761&action=review
> 
> Looks good to me, just a couple pedantic questions/comments.
> 
> I'm going to refrain from r+ing to give other folks a chance to chime in, especially since I'm not very familiar with the MemoryCache piece of this.
> 
> > WebCore/loader/cache/CachedResourceLoader.cpp:76
> > +    default:
> > +        break;
> > +    }
> > +    return 0;
> 
> Should we have an ASSERT_NOT_REACHED() here?

Copy code but yeah, we should.

> 
> > WebCore/loader/cache/CachedResourceLoader.cpp:259
> > -    // FIXME: Consider letting the embedder block mixed content loads.
> > +
> 
> I'm not really familiar with the mixed content policy code, is this FIXME not valid anymore?

That was removed by accident (I still don't know what it means).

> 
> > WebCore/loader/cache/CachedResourceLoader.cpp:276
> > +    if (!url.isValid())
> > +        return 0;
> > +    
> > +    if (!canRequest(type, url))
> > +        return 0;
> 
> Is there any particular reason to split this out into 2 if statements?

I thought a security check deserves a line of it's own. As the comment below says, this should merge with the other similar security check but  I didn't want to change the existing behavior.
Comment 6 Alexey Proskuryakov 2010-12-20 15:17:13 PST
> > > -    // FIXME: Consider letting the embedder block mixed content loads.
> That was removed by accident (I still don't know what it means).

That comment is easier to understand when considered together with one before the switch block:

    // Note: Currently, we always allow mixed content, but we generate a
    //       callback to the FrameLoaderClient in case the embedder wants to
    //       update any security indicators.

For example, an https page that includes non-https scripts (and really, any subresources) should not be considered secure, and Safari doesn't display a lock icon for that. But loading non-https content is always risky, letting the party in control of your network connection run any sorts of attacks on authenticated https pages.

I'm not sure why there is anything to consider - maybe the person who wrote this could tell why a way to block such loads wasn't added right away.
Comment 7 Darin Adler 2010-12-29 16:47:08 PST
Comment on attachment 76761 [details]
fix style

View in context: https://bugs.webkit.org/attachment.cgi?id=76761&action=review

> WebCore/css/CSSImageValue.cpp:75
> +        CachedImage* cachedImage = loader->requestImage(url);
>          if (cachedImage) {

Could put the definition inside the if statement.

> WebCore/loader/cache/CachedResource.h:54
> +    friend class CachedResourceLoader;

Is this really necessary? Can we do something to avoid it?

> WebCore/loader/cache/CachedResourceLoader.cpp:74
> +    default:
> +        break;

Normally we try to avoid having a default. That way, the compiler will warn us if we forget to include a case. Just leaving out "default: break" should do the trick.
Comment 8 Antti Koivisto 2010-12-30 13:00:31 PST
http://trac.webkit.org/changeset/74807
Comment 9 Antti Koivisto 2010-12-30 13:02:58 PST
> > WebCore/loader/cache/CachedResource.h:54
> > +    friend class CachedResourceLoader;
> 
> Is this really necessary? Can we do something to avoid it?

I gather you are not a fan of using friends for subsystem internal API?

I made some more functions public (with a comment) and got rid of the friend.
Comment 10 Dimitri Glazkov (Google) 2011-01-09 10:18:13 PST
Comment on attachment 76761 [details]
fix style

View in context: https://bugs.webkit.org/attachment.cgi?id=76761&action=review

> WebCore/ChangeLog:19
> +        * css/CSSImageValue.cpp:
> +        (WebCore::CSSImageValue::cachedImage):
> +            
> +            Remove a code path that called MemoryCache::requestResource directly. This code path would have crashed
> +            if ever taken (since it passes null CachedResourceLoader pointer).

Are you sure this is the case? I have a sneaking suspicion that this change is causing random memory heap corruption issues, as being investigated here:

http://code.google.com/p/chromium/issues/detail?id=68516
Comment 11 Dimitri Glazkov (Google) 2011-01-09 12:58:18 PST
(In reply to comment #10)
> (From update of attachment 76761 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=76761&action=review
> 
> > WebCore/ChangeLog:19
> > +        * css/CSSImageValue.cpp:
> > +        (WebCore::CSSImageValue::cachedImage):
> > +            
> > +            Remove a code path that called MemoryCache::requestResource directly. This code path would have crashed
> > +            if ever taken (since it passes null CachedResourceLoader pointer).
> 
> Are you sure this is the case? I have a sneaking suspicion that this change is causing random memory heap corruption issues, as being investigated here:
> 
> http://code.google.com/p/chromium/issues/detail?id=68516

Following this trail:

CSSImageValue::cachedImage is only called from CSSStyleSelector::loadPendingImages, which is in turn called by:

CSSStyleSelector::styleForElement
CSSStyleSelector::styleForPage
CSSStyleSelector::pseudoStyleForElement
CSSStyleSelector::keyframeStylesForAnimation

Since, in cachedImage(), the "loader" is document()->cachedResourceLoader() and returns an OwnPtr, could any of the methods above be invoked after the Document is destroyed?

If they could, we can have memory corruption. Right?
Comment 12 Antti Koivisto 2011-01-10 03:15:14 PST
(In reply to comment #10)
> Are you sure this is the case? I have a sneaking suspicion that this change is causing random memory heap corruption issues, as being investigated here:
> 
> http://code.google.com/p/chromium/issues/detail?id=68516

If CSSImageValue::cachedImage was called with null loader (which is the case I removed), it seems certain it would crash immediately when dereferencing in

if (CachedImage* cachedImage = loader->requestImage(url)) {

I can't see how it could survive that to corrupt memory. 

Can you enable that ASSERT(loader) crash in your reliability bot build to be sure?