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

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: Further optimization. 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 bool acceptableUrl = url.isValid() && !url.protocolIsData();
Łukasz Anforowicz 2016/10/07 19:07:48 nit: Maybe add a comment what is the meaning of |a
carlosk 2016/10/07 21:54:24 Done.
332 // If this CSS is identifiable by its URL and it has already been analyzed
333 // before then just skip it.
334 if (acceptableUrl && (m_resourceURLs.contains(url) ||
335 m_delegate.shouldSkipResourceWithURL(url))) {
336 return;
337 }
338
331 TRACE_EVENT2("page-serialization", "FrameSerializer::serializeCSSStyleSheet", 339 TRACE_EVENT2("page-serialization", "FrameSerializer::serializeCSSStyleSheet",
332 "type", "CSS", "url", url.elidedString().utf8().data()); 340 "type", "CSS", "url", url.elidedString().utf8().data());
333 // Only report UMA metric if this is not a reentrant CSS serialization call. 341 // Only report UMA metric if this is not a reentrant CSS serialization call.
334 double cssStartTime = 0; 342 double cssStartTime = 0;
335 if (!m_isSerializingCss) { 343 if (!m_isSerializingCss) {
336 m_isSerializingCss = true; 344 m_isSerializingCss = true;
337 cssStartTime = monotonicallyIncreasingTime(); 345 cssStartTime = monotonicallyIncreasingTime();
338 } 346 }
339 347
340 StringBuilder cssText; 348 if (acceptableUrl) {
341 cssText.append("@charset \""); 349 StringBuilder cssText;
342 cssText.append(styleSheet.contents()->charset().lower()); 350 cssText.append("@charset \"");
343 cssText.append("\";\n\n"); 351 cssText.append(styleSheet.contents()->charset().lower());
352 cssText.append("\";\n\n");
344 353
345 for (unsigned i = 0; i < styleSheet.length(); ++i) { 354 for (unsigned i = 0; i < styleSheet.length(); ++i) {
346 CSSRule* rule = styleSheet.item(i); 355 CSSRule* rule = styleSheet.item(i);
347 String itemText = rule->cssText(); 356 String itemText = rule->cssText();
348 if (!itemText.isEmpty()) { 357 if (!itemText.isEmpty()) {
349 cssText.append(itemText); 358 cssText.append(itemText);
350 if (i < styleSheet.length() - 1) 359 if (i < styleSheet.length() - 1)
351 cssText.append("\n\n"); 360 cssText.append("\n\n");
361 }
362
363 // Some rules have resources associated with them that we need to
364 // retrieve.
365 serializeCSSRule(rule);
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);
377 } else {
Łukasz Anforowicz 2016/10/07 19:07:48 nit: Maybe code would be cleaner if the loop below
carlosk 2016/10/07 21:54:24 I considered that but thought it would make the me
378 // Even if no text is needed, associated resources must be fetched.
Łukasz Anforowicz 2016/10/07 19:07:48 nit: I am not sure if this comment will be easy to
carlosk 2016/10/07 21:54:24 Indeed, this was unclear. I improved the overall c
379 for (unsigned i = 0; i < styleSheet.length(); ++i)
380 serializeCSSRule(styleSheet.item(i));
368 } 381 }
369 382
370 if (cssStartTime != 0) { 383 if (cssStartTime != 0) {
371 m_isSerializingCss = false; 384 m_isSerializingCss = false;
372 DEFINE_STATIC_LOCAL(CustomCountHistogram, cssHistogram, 385 DEFINE_STATIC_LOCAL(CustomCountHistogram, cssHistogram,
373 ("PageSerialization.SerializationTime.CSSElement", 0, 386 ("PageSerialization.SerializationTime.CSSElement", 0,
374 maxSerializationTimeUmaMicroseconds, 50)); 387 maxSerializationTimeUmaMicroseconds, 50));
375 cssHistogram.count( 388 cssHistogram.count(
376 static_cast<int64_t>((monotonicallyIncreasingTime() - cssStartTime) * 389 static_cast<int64_t>((monotonicallyIncreasingTime() - cssStartTime) *
377 secondsToMicroseconds)); 390 secondsToMicroseconds));
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 emitsMinus = ch == '-'; 554 emitsMinus = ch == '-';
542 builder.append(ch); 555 builder.append(ch);
543 } 556 }
544 CString escapedUrl = builder.toString().ascii(); 557 CString escapedUrl = builder.toString().ascii();
545 return String::format("saved from url=(%04d)%s", 558 return String::format("saved from url=(%04d)%s",
546 static_cast<int>(escapedUrl.length()), 559 static_cast<int>(escapedUrl.length()),
547 escapedUrl.data()); 560 escapedUrl.data());
548 } 561 }
549 562
550 } // namespace blink 563 } // 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