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

Side by Side Diff: third_party/WebKit/Source/core/frame/FrameSerializer.cpp

Issue 2397983002: MHTML saving optimization: don't generate CSS code that will be discarded. (Closed)
Patch Set: Simplified code as lukasza@ suggested. Created 4 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 } else if (isHTMLStyleElement(element)) { 321 } else if (isHTMLStyleElement(element)) {
322 HTMLStyleElement& styleElement = toHTMLStyleElement(element); 322 HTMLStyleElement& styleElement = toHTMLStyleElement(element);
323 if (CSSStyleSheet* sheet = styleElement.sheet()) 323 if (CSSStyleSheet* sheet = styleElement.sheet())
324 serializeCSSStyleSheet(*sheet, KURL()); 324 serializeCSSStyleSheet(*sheet, KURL());
325 } 325 }
326 } 326 }
327 } 327 }
328 328
329 void FrameSerializer::serializeCSSStyleSheet(CSSStyleSheet& styleSheet, 329 void FrameSerializer::serializeCSSStyleSheet(CSSStyleSheet& styleSheet,
330 const KURL& url) { 330 const KURL& url) {
331 // If the URL is invalid or if it is a data URL this means that this CSS is
332 // defined inline, respectively in a <style> tag or in the data URL itself.
333 bool isInlineCss = !url.isValid() || url.protocolIsData();
334 // If this CSS is not inline then it is identifiable by its URL. So just skip
335 // it if it has already been analyzed before.
336 if (!isInlineCss && (m_resourceURLs.contains(url) ||
337 m_delegate.shouldSkipResourceWithURL(url))) {
338 return;
339 }
340
331 TRACE_EVENT2("page-serialization", "FrameSerializer::serializeCSSStyleSheet", 341 TRACE_EVENT2("page-serialization", "FrameSerializer::serializeCSSStyleSheet",
332 "type", "CSS", "url", url.elidedString().utf8().data()); 342 "type", "CSS", "url", url.elidedString().utf8().data());
333 // Only report UMA metric if this is not a reentrant CSS serialization call. 343 // Only report UMA metric if this is not a reentrant CSS serialization call.
334 double cssStartTime = 0; 344 double cssStartTime = 0;
335 if (!m_isSerializingCss) { 345 if (!m_isSerializingCss) {
336 m_isSerializingCss = true; 346 m_isSerializingCss = true;
337 cssStartTime = monotonicallyIncreasingTime(); 347 cssStartTime = monotonicallyIncreasingTime();
338 } 348 }
339 349
340 StringBuilder cssText; 350 // If this CSS is inlined its definition was already serialized with the frame
341 cssText.append("@charset \""); 351 // HTML code that was previously generated. No need to regenerate it here.
342 cssText.append(styleSheet.contents()->charset().lower()); 352 if (!isInlineCss) {
343 cssText.append("\";\n\n"); 353 StringBuilder cssText;
354 cssText.append("@charset \"");
355 cssText.append(styleSheet.contents()->charset().lower());
356 cssText.append("\";\n\n");
344 357
345 for (unsigned i = 0; i < styleSheet.length(); ++i) { 358 for (unsigned i = 0; i < styleSheet.length(); ++i) {
346 CSSRule* rule = styleSheet.item(i); 359 CSSRule* rule = styleSheet.item(i);
347 String itemText = rule->cssText(); 360 String itemText = rule->cssText();
348 if (!itemText.isEmpty()) { 361 if (!itemText.isEmpty()) {
349 cssText.append(itemText); 362 cssText.append(itemText);
350 if (i < styleSheet.length() - 1) 363 if (i < styleSheet.length() - 1)
351 cssText.append("\n\n"); 364 cssText.append("\n\n");
365 }
352 } 366 }
353 367
354 // Some rules have resources associated with them that we need to retrieve.
355 serializeCSSRule(rule);
356 }
357
358 if (shouldAddURL(url)) {
359 WTF::TextEncoding textEncoding(styleSheet.contents()->charset()); 368 WTF::TextEncoding textEncoding(styleSheet.contents()->charset());
360 ASSERT(textEncoding.isValid()); 369 ASSERT(textEncoding.isValid());
361 String textString = cssText.toString(); 370 String textString = cssText.toString();
362 CString text = 371 CString text =
363 textEncoding.encode(textString, WTF::CSSEncodedEntitiesForUnencodables); 372 textEncoding.encode(textString, WTF::CSSEncodedEntitiesForUnencodables);
364 m_resources->append( 373 m_resources->append(
365 SerializedResource(url, String("text/css"), 374 SerializedResource(url, String("text/css"),
366 SharedBuffer::create(text.data(), text.length()))); 375 SharedBuffer::create(text.data(), text.length())));
367 m_resourceURLs.add(url); 376 m_resourceURLs.add(url);
368 } 377 }
369 378
379 // Sub resources need to be serialized even if the CSS definition doesn't
380 // need to be.
381 for (unsigned i = 0; i < styleSheet.length(); ++i)
382 serializeCSSRule(styleSheet.item(i));
383
370 if (cssStartTime != 0) { 384 if (cssStartTime != 0) {
371 m_isSerializingCss = false; 385 m_isSerializingCss = false;
372 DEFINE_STATIC_LOCAL(CustomCountHistogram, cssHistogram, 386 DEFINE_STATIC_LOCAL(CustomCountHistogram, cssHistogram,
373 ("PageSerialization.SerializationTime.CSSElement", 0, 387 ("PageSerialization.SerializationTime.CSSElement", 0,
374 maxSerializationTimeUmaMicroseconds, 50)); 388 maxSerializationTimeUmaMicroseconds, 50));
375 cssHistogram.count( 389 cssHistogram.count(
376 static_cast<int64_t>((monotonicallyIncreasingTime() - cssStartTime) * 390 static_cast<int64_t>((monotonicallyIncreasingTime() - cssStartTime) *
377 secondsToMicroseconds)); 391 secondsToMicroseconds));
378 } 392 }
379 } 393 }
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 emitsMinus = ch == '-'; 555 emitsMinus = ch == '-';
542 builder.append(ch); 556 builder.append(ch);
543 } 557 }
544 CString escapedUrl = builder.toString().ascii(); 558 CString escapedUrl = builder.toString().ascii();
545 return String::format("saved from url=(%04d)%s", 559 return String::format("saved from url=(%04d)%s",
546 static_cast<int>(escapedUrl.length()), 560 static_cast<int>(escapedUrl.length()),
547 escapedUrl.data()); 561 escapedUrl.data());
548 } 562 }
549 563
550 } // namespace blink 564 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698