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

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_blocking_page.cc

Issue 56135: Adding unit tests for the SafeBrowsingBlockingPage class (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Implementation of the SafeBrowsingBlockingPage class. 5 // Implementation of the SafeBrowsingBlockingPage class.
6 6
7 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" 7 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
8 8
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 L"<a href=\"\" onClick=\"sendCommand('showDiagnostic'); return false;\" " 42 L"<a href=\"\" onClick=\"sendCommand('showDiagnostic'); return false;\" "
43 L"onMouseDown=\"return false;\">%ls</a>"; 43 L"onMouseDown=\"return false;\">%ls</a>";
44 44
45 // The commands returned by the page when the user performs an action. 45 // The commands returned by the page when the user performs an action.
46 static const char* const kShowDiagnosticCommand = "showDiagnostic"; 46 static const char* const kShowDiagnosticCommand = "showDiagnostic";
47 static const char* const kReportErrorCommand = "reportError"; 47 static const char* const kReportErrorCommand = "reportError";
48 static const char* const kLearnMoreCommand = "learnMore"; 48 static const char* const kLearnMoreCommand = "learnMore";
49 static const char* const kProceedCommand = "proceed"; 49 static const char* const kProceedCommand = "proceed";
50 static const char* const kTakeMeBackCommand = "takeMeBack"; 50 static const char* const kTakeMeBackCommand = "takeMeBack";
51 51
52 // static
53 SafeBrowsingBlockingPageFactory* SafeBrowsingBlockingPage::factory_ = NULL;
54
55 // The default SafeBrowsingBlockingPageFactory. Global, made a singleton so we
56 // don't leak it.
57 class SafeBrowsingBlockingPageFactoryImpl
58 : public SafeBrowsingBlockingPageFactory {
59 public:
60 SafeBrowsingBlockingPage* CreateSafeBrowsingPage(
61 SafeBrowsingService* service,
62 WebContents* web_contents,
63 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources) {
64 return new SafeBrowsingBlockingPage(service, web_contents,
65 unsafe_resources);
66 }
67
68 private:
69 friend struct DefaultSingletonTraits<SafeBrowsingBlockingPageFactoryImpl>;
70
71 SafeBrowsingBlockingPageFactoryImpl() { }
72
73 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingBlockingPageFactoryImpl);
74 };
75
52 SafeBrowsingBlockingPage::SafeBrowsingBlockingPage( 76 SafeBrowsingBlockingPage::SafeBrowsingBlockingPage(
53 SafeBrowsingService* sb_service, 77 SafeBrowsingService* sb_service,
54 WebContents* web_contents, 78 WebContents* web_contents,
55 const UnsafeResourceList& unsafe_resources) 79 const UnsafeResourceList& unsafe_resources)
56 : InterstitialPage(web_contents, 80 : InterstitialPage(web_contents,
57 IsMainPage(unsafe_resources), 81 IsMainPage(unsafe_resources),
58 unsafe_resources[0].url), 82 unsafe_resources[0].url),
59 sb_service_(sb_service), 83 sb_service_(sb_service),
60 is_main_frame_(IsMainPage(unsafe_resources)), 84 is_main_frame_(IsMainPage(unsafe_resources)),
61 unsafe_resources_(unsafe_resources) { 85 unsafe_resources_(unsafe_resources) {
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 366
343 // Check to see if some new notifications of unsafe resources have been 367 // Check to see if some new notifications of unsafe resources have been
344 // received while we were showing the interstitial. 368 // received while we were showing the interstitial.
345 UnsafeResourceMap* unsafe_resource_map = GetUnsafeResourcesMap(); 369 UnsafeResourceMap* unsafe_resource_map = GetUnsafeResourcesMap();
346 UnsafeResourceMap::iterator iter = unsafe_resource_map->find(tab()); 370 UnsafeResourceMap::iterator iter = unsafe_resource_map->find(tab());
347 SafeBrowsingBlockingPage* blocking_page = NULL; 371 SafeBrowsingBlockingPage* blocking_page = NULL;
348 if (iter != unsafe_resource_map->end() && !iter->second.empty()) { 372 if (iter != unsafe_resource_map->end() && !iter->second.empty()) {
349 // Build an interstitial for all the unsafe resources notifications. 373 // Build an interstitial for all the unsafe resources notifications.
350 // Don't show it now as showing an interstitial while an interstitial is 374 // Don't show it now as showing an interstitial while an interstitial is
351 // already showing would cause DontProceed() to be invoked. 375 // already showing would cause DontProceed() to be invoked.
352 blocking_page = new SafeBrowsingBlockingPage(sb_service_, tab(), 376 blocking_page = factory_->CreateSafeBrowsingPage(sb_service_, tab(),
353 iter->second); 377 iter->second);
354 unsafe_resource_map->erase(iter); 378 unsafe_resource_map->erase(iter);
355 } 379 }
356 380
357 InterstitialPage::Proceed(); 381 InterstitialPage::Proceed();
358 // We are now deleted. 382 // We are now deleted.
359 383
360 // Now that this interstitial is gone, we can show the new one. 384 // Now that this interstitial is gone, we can show the new one.
361 if (blocking_page) 385 if (blocking_page)
362 blocking_page->Show(); 386 blocking_page->Show();
363 } 387 }
(...skipping 20 matching lines...) Expand all
384 } 408 }
385 InterstitialPage::DontProceed(); 409 InterstitialPage::DontProceed();
386 // We are now deleted. 410 // We are now deleted.
387 } 411 }
388 412
389 // static 413 // static
390 void SafeBrowsingBlockingPage::NotifySafeBrowsingService( 414 void SafeBrowsingBlockingPage::NotifySafeBrowsingService(
391 SafeBrowsingService* sb_service, 415 SafeBrowsingService* sb_service,
392 const UnsafeResourceList& unsafe_resources, 416 const UnsafeResourceList& unsafe_resources,
393 bool proceed) { 417 bool proceed) {
394 base::Thread* io_thread = g_browser_process->io_thread(); 418 MessageLoop* message_loop;
395 if (!io_thread) 419 if (g_browser_process->io_thread())
396 return; 420 message_loop = g_browser_process->io_thread()->message_loop();
421 else // For unit-tests, just post on the current thread.
422 message_loop = MessageLoop::current();
397 423
398 io_thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 424 message_loop->PostTask(FROM_HERE, NewRunnableMethod(
399 sb_service, &SafeBrowsingService::OnBlockingPageDone, unsafe_resources, 425 sb_service, &SafeBrowsingService::OnBlockingPageDone, unsafe_resources,
400 proceed)); 426 proceed));
401 } 427 }
402 428
403 // static 429 // static
404 SafeBrowsingBlockingPage::UnsafeResourceMap* 430 SafeBrowsingBlockingPage::UnsafeResourceMap*
405 SafeBrowsingBlockingPage::GetUnsafeResourcesMap() { 431 SafeBrowsingBlockingPage::GetUnsafeResourcesMap() {
406 return Singleton<UnsafeResourceMap>::get(); 432 return Singleton<UnsafeResourceMap>::get();
407 } 433 }
408 434
409 // static 435 // static
410 void SafeBrowsingBlockingPage::ShowBlockingPage( 436 void SafeBrowsingBlockingPage::ShowBlockingPage(
411 SafeBrowsingService* sb_service, 437 SafeBrowsingService* sb_service,
412 const SafeBrowsingService::UnsafeResource& unsafe_resource) { 438 const SafeBrowsingService::UnsafeResource& unsafe_resource) {
413 WebContents* web_contents = tab_util::GetWebContentsByID( 439 WebContents* web_contents = tab_util::GetWebContentsByID(
414 unsafe_resource.render_process_host_id, unsafe_resource.render_view_id); 440 unsafe_resource.render_process_host_id, unsafe_resource.render_view_id);
415 441
416 if (!InterstitialPage::GetInterstitialPage(web_contents)) { 442 if (!InterstitialPage::GetInterstitialPage(web_contents)) {
417 // There are no interstitial currently showing in that tab, go ahead and 443 // There are no interstitial currently showing in that tab, go ahead and
418 // show this interstitial. 444 // show this interstitial.
419 std::vector<SafeBrowsingService::UnsafeResource> resources; 445 std::vector<SafeBrowsingService::UnsafeResource> resources;
420 resources.push_back(unsafe_resource); 446 resources.push_back(unsafe_resource);
447 // Set up the factory if this has not been done already (tests do that
448 // before this method is called).
449 if (!factory_)
450 factory_ = Singleton<SafeBrowsingBlockingPageFactoryImpl>::get();
421 SafeBrowsingBlockingPage* blocking_page = 451 SafeBrowsingBlockingPage* blocking_page =
422 new SafeBrowsingBlockingPage(sb_service, web_contents, resources); 452 factory_->CreateSafeBrowsingPage(sb_service, web_contents, resources);
423 blocking_page->Show(); 453 blocking_page->Show();
424 return; 454 return;
425 } 455 }
426 456
427 // Let's queue the interstitial. 457 // Let's queue the interstitial.
428 // Note we only expect resources from the page at this point. 458 // Note we only expect resources from the page at this point.
429 DCHECK(unsafe_resource.resource_type != ResourceType::MAIN_FRAME); 459 DCHECK(unsafe_resource.resource_type != ResourceType::MAIN_FRAME);
430 UnsafeResourceMap* unsafe_resource_map = GetUnsafeResourcesMap(); 460 UnsafeResourceMap* unsafe_resource_map = GetUnsafeResourcesMap();
431 (*unsafe_resource_map)[web_contents].push_back(unsafe_resource); 461 (*unsafe_resource_map)[web_contents].push_back(unsafe_resource);
432 } 462 }
433 463
434 // static 464 // static
435 bool SafeBrowsingBlockingPage::IsMainPage( 465 bool SafeBrowsingBlockingPage::IsMainPage(
436 const UnsafeResourceList& unsafe_resources) { 466 const UnsafeResourceList& unsafe_resources) {
437 return unsafe_resources.size() == 1 && 467 return unsafe_resources.size() == 1 &&
438 unsafe_resources[0].resource_type == ResourceType::MAIN_FRAME; 468 unsafe_resources[0].resource_type == ResourceType::MAIN_FRAME;
439 } 469 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698