summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ssl/ssl_manager.cc
blob: 58d7acbbf9d442eee90c69cc6bd95720c47274ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ssl/ssl_manager.h"

#include "base/message_loop.h"
#include "base/string_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/load_from_memory_cache_details.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/resource_request_details.h"
#include "chrome/browser/ssl/ssl_error_info.h"
#include "chrome/browser/ssl/ssl_host_state.h"
#include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/tab_contents/provisional_load_details.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/browser/tab_contents/web_contents.h"
#include "chrome/browser/ssl/ssl_policy.h"
#include "chrome/common/l10n_util.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "chrome/common/resource_bundle.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "net/base/cert_status_flags.h"
#include "net/base/net_errors.h"
#include "net/url_request/url_request.h"
#include "webkit/glue/resource_type.h"

#if defined(OS_WIN)
// TODO(port): Port these files.
#include "chrome/browser/load_notification_details.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/views/decision.h"
#include "chrome/views/link.h"
#else
#include "chrome/common/temp_scaffolding_stubs.h"
#endif


class SSLInfoBarDelegate : public ConfirmInfoBarDelegate {
 public:
  SSLInfoBarDelegate(TabContents* contents,
                     const std::wstring message,
                     const std::wstring& button_label,
                     Task* task)
    : ConfirmInfoBarDelegate(contents),
      message_(message),
      button_label_(button_label),
      task_(task) {
  }
  virtual ~SSLInfoBarDelegate() {}

  // Overridden from ConfirmInfoBarDelegate:
  virtual void InfoBarClosed() {
    delete this;
  }
  virtual std::wstring GetMessageText() const {
    return message_;
  }
  virtual SkBitmap* GetIcon() const {
    return ResourceBundle::GetSharedInstance().GetBitmapNamed(
        IDR_INFOBAR_SSL_WARNING);
  }
  virtual int GetButtons() const {
    return !button_label_.empty() ? BUTTON_OK : BUTTON_NONE;
  }
  virtual std::wstring GetButtonLabel(InfoBarButton button) const {
    return button_label_;
  }
  virtual bool Accept() {
    if (task_.get()) {
      task_->Run();
      task_.reset();  // Ensures we won't run the task again.
    }
    return true;
  }

 private:
  // Labels for the InfoBar's message and button.
  std::wstring message_;
  std::wstring button_label_;

  // A task to run when the InfoBar is accepted.
  scoped_ptr<Task> task_;

  DISALLOW_COPY_AND_ASSIGN(SSLInfoBarDelegate);
};

////////////////////////////////////////////////////////////////////////////////
// SSLManager

// static
void SSLManager::RegisterUserPrefs(PrefService* prefs) {
  prefs->RegisterIntegerPref(prefs::kMixedContentFiltering,
                             FilterPolicy::DONT_FILTER);
}

SSLManager::SSLManager(NavigationController* controller, Delegate* delegate)
    : delegate_(delegate),
      controller_(controller),
      ssl_host_state_(controller->profile()->GetSSLHostState()) {
  DCHECK(controller_);

  // If do delegate is supplied, use the default policy.
  if (!delegate_)
    delegate_ = SSLPolicy::GetDefaultPolicy();

  // Subscribe to various notifications.
  registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
                 Source<NavigationController>(controller_));
  registrar_.Add(this, NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
                 Source<NavigationController>(controller_));
  registrar_.Add(this, NotificationType::RESOURCE_RESPONSE_STARTED,
                 Source<NavigationController>(controller_));
  registrar_.Add(this, NotificationType::RESOURCE_RECEIVED_REDIRECT,
                 Source<NavigationController>(controller_));
  registrar_.Add(this, NotificationType::LOAD_FROM_MEMORY_CACHE,
                 Source<NavigationController>(controller_));
}

SSLManager::~SSLManager() {
}

// Delegate API method.
void SSLManager::ShowMessage(const std::wstring& msg) {
  ShowMessageWithLink(msg, std::wstring(), NULL);
}

void SSLManager::ShowMessageWithLink(const std::wstring& msg,
                                     const std::wstring& link_text,
                                     Task* task) {
  if (controller_->GetPendingEntry()) {
    // The main frame is currently loading, wait until the load is committed so
    // to show the error on the right page (once the location bar shows the
    // correct url).
    if (std::find(pending_messages_.begin(), pending_messages_.end(), msg) ==
        pending_messages_.end())
      pending_messages_.push_back(SSLMessageInfo(msg, link_text, task));

    return;
  }

  NavigationEntry* entry = controller_->GetActiveEntry();
  if (!entry)
    return;

  // Don't show the message if the user doesn't expect an authenticated session.
  if (entry->ssl().security_style() <= SECURITY_STYLE_UNAUTHENTICATED)
    return;

  if (controller_->active_contents()) {
    controller_->active_contents()->AddInfoBar(
        new SSLInfoBarDelegate(controller_->active_contents(), msg, link_text,
                               task));
  }
}

// Delegate API method.
bool SSLManager::SetMaxSecurityStyle(SecurityStyle style) {
  NavigationEntry* entry = controller_->GetActiveEntry();
  if (!entry) {
    NOTREACHED();
    return false;
  }

  if (entry->ssl().security_style() > style) {
    entry->ssl().set_security_style(style);
    return true;
  }
  return false;
}

// Delegate API method.
void SSLManager::AddMessageToConsole(const std::wstring& msg,
                                     ConsoleMessageLevel level) {
  TabContents* tab_contents = controller_->GetTabContents(TAB_CONTENTS_WEB);
  if (!tab_contents)
    return;
  WebContents* web_contents = tab_contents->AsWebContents();
  if (!web_contents)
    return;

  web_contents->render_view_host()->AddMessageToConsole(
      std::wstring(), msg, level);
}

// Delegate API method.
void SSLManager::DenyCertForHost(net::X509Certificate* cert,
                                 const std::string& host) {
  // Remember that we don't like this cert for this host.
  ssl_host_state_->DenyCertForHost(cert, host);
}

// Delegate API method.
void SSLManager::AllowCertForHost(net::X509Certificate* cert,
                                  const std::string& host) {
  ssl_host_state_->AllowCertForHost(cert, host);
}

// Delegate API method.
net::X509Certificate::Policy::Judgment SSLManager::QueryPolicy(
    net::X509Certificate* cert, const std::string& host) {
  return ssl_host_state_->QueryPolicy(cert, host);
}

bool SSLManager::CanShowInsecureContent(const GURL& url) {
  return ssl_host_state_->CanShowInsecureContent(url);
}

void SSLManager::AllowShowInsecureContentForURL(const GURL& url) {
  ssl_host_state_->AllowShowInsecureContentForURL(url);
}

bool SSLManager::ProcessedSSLErrorFromRequest() const {
  NavigationEntry* entry = controller_->GetActiveEntry();
  if (!entry) {
    NOTREACHED();
    return false;
  }

  return net::IsCertStatusError(entry->ssl().cert_status());
}

////////////////////////////////////////////////////////////////////////////////
// ErrorHandler

SSLManager::ErrorHandler::ErrorHandler(ResourceDispatcherHost* rdh,
                                       URLRequest* request,
                                       MessageLoop* ui_loop)
    : ui_loop_(ui_loop),
      io_loop_(MessageLoop::current()),
      manager_(NULL),
      request_id_(0, 0),
      resource_dispatcher_host_(rdh),
      request_url_(request->url()),
      request_has_been_notified_(false) {
  DCHECK(MessageLoop::current() != ui_loop);

  ResourceDispatcherHost::ExtraRequestInfo* info =
      ResourceDispatcherHost::ExtraInfoForRequest(request);
  request_id_.render_process_host_id = info->render_process_host_id;
  request_id_.request_id = info->request_id;

  if (!tab_util::GetTabContentsID(request,
                                  &render_process_host_id_,
                                  &tab_contents_id_))
    NOTREACHED();

  // This makes sure we don't disappear on the IO thread until we've given an
  // answer to the URLRequest.
  //
  // Release in CompleteCancelRequest, CompleteContinueRequest,
  // CompleteStartRequest or CompleteTakeNoAction.
  AddRef();
}

void SSLManager::ErrorHandler::Dispatch() {
  DCHECK(MessageLoop::current() == ui_loop_);

  TabContents* web_contents =
      tab_util::GetWebContentsByID(render_process_host_id_, tab_contents_id_);

  if (!web_contents) {
    // We arrived on the UI thread, but the tab we're looking for is no longer
    // here.
    OnDispatchFailed();
    return;
  }

  // Hand ourselves off to the SSLManager.
  manager_ = web_contents->controller()->ssl_manager();
  OnDispatched();
}

WebContents* SSLManager::ErrorHandler::GetWebContents() {
  return tab_util::GetWebContentsByID(render_process_host_id_,
                                      tab_contents_id_);
}

void SSLManager::ErrorHandler::CancelRequest() {
  DCHECK(MessageLoop::current() == ui_loop_);

  // We need to complete this task on the IO thread.
  io_loop_->PostTask(FROM_HERE, NewRunnableMethod(
       this, &SSLManager::ErrorHandler::CompleteCancelRequest,
       net::ERR_ABORTED));
}

void SSLManager::ErrorHandler::DenyRequest() {
  DCHECK(MessageLoop::current() == ui_loop_);

  // We need to complete this task on the IO thread.
  io_loop_->PostTask(FROM_HERE, NewRunnableMethod(
      this, &SSLManager::ErrorHandler::CompleteCancelRequest,
      net::ERR_INSECURE_RESPONSE));
}

void SSLManager::ErrorHandler::ContinueRequest() {
  DCHECK(MessageLoop::current() == ui_loop_);

  // We need to complete this task on the IO thread.
  io_loop_->PostTask(FROM_HERE, NewRunnableMethod(
      this, &SSLManager::ErrorHandler::CompleteContinueRequest));
}

void SSLManager::ErrorHandler::StartRequest(FilterPolicy::Type filter_policy) {
  DCHECK(MessageLoop::current() == ui_loop_);

  // We need to complete this task on the IO thread.
  io_loop_->PostTask(FROM_HERE, NewRunnableMethod(
      this, &SSLManager::ErrorHandler::CompleteStartRequest, filter_policy));
}

void SSLManager::ErrorHandler::TakeNoAction() {
  DCHECK(MessageLoop::current() == ui_loop_);

  // We need to complete this task on the IO thread.
  io_loop_->PostTask(FROM_HERE, NewRunnableMethod(
      this, &SSLManager::ErrorHandler::CompleteTakeNoAction));
}

void SSLManager::ErrorHandler::CompleteCancelRequest(int error) {
  DCHECK(MessageLoop::current() == io_loop_);

  // It is important that we notify the URLRequest only once.  If we try to
  // notify the request twice, it may no longer exist and |this| might have
  // already have been deleted.
  DCHECK(!request_has_been_notified_);

  if (!request_has_been_notified_) {
    URLRequest* request = resource_dispatcher_host_->GetURLRequest(request_id_);
    if (request) {
      // The request can be NULL if it was cancelled by the renderer (as the
      // result of the user navigating to a new page from the location bar).
      DLOG(INFO) << "CompleteCancelRequest() url: " << request->url().spec();
      SSLManager::CertError* cert_error = AsCertError();
      if (cert_error)
        request->SimulateSSLError(error, cert_error->ssl_info());
      else
        request->SimulateError(error);
    }
    request_has_been_notified_ = true;

    // We're done with this object on the IO thread.
    Release();
  }
}

void SSLManager::ErrorHandler::CompleteContinueRequest() {
  DCHECK(MessageLoop::current() == io_loop_);

  // It is important that we notify the URLRequest only once.  If we try to
  // notify the request twice, it may no longer exist and |this| might have
  // already have been deleted.
  DCHECK(!request_has_been_notified_);

  if (!request_has_been_notified_) {
    URLRequest* request = resource_dispatcher_host_->GetURLRequest(request_id_);
    if (request) {
      // The request can be NULL if it was cancelled by the renderer (as the
      // result of the user navigating to a new page from the location bar).
      DLOG(INFO) << "CompleteContinueRequest() url: " << request->url().spec();
      request->ContinueDespiteLastError();
    }
    request_has_been_notified_ = true;

    // We're done with this object on the IO thread.
    Release();
  }
}

void SSLManager::ErrorHandler::CompleteStartRequest(
    FilterPolicy::Type filter_policy) {
  DCHECK(MessageLoop::current() == io_loop_);

  // It is important that we notify the URLRequest only once.  If we try to
  // notify the request twice, it may no longer exist and |this| might have
  // already have been deleted.
  DCHECK(!request_has_been_notified_);

  if (request_has_been_notified_)
    return;

  URLRequest* request = resource_dispatcher_host_->GetURLRequest(request_id_);
  if (request) {
    // The request can be NULL if it was cancelled by the renderer (as the
    // result of the user navigating to a new page from the location bar).
    DLOG(INFO) << "CompleteStartRequest() url: " << request->url().spec();
    // The request should not have been started (SUCCESS is the initial state).
    DCHECK(request->status().status() == URLRequestStatus::SUCCESS);
    ResourceDispatcherHost::ExtraRequestInfo* info =
        ResourceDispatcherHost::ExtraInfoForRequest(request);
    info->filter_policy = filter_policy;
    request->Start();
  }
  request_has_been_notified_ = true;

  // We're done with this object on the IO thread.
  Release();
}

void SSLManager::ErrorHandler::CompleteTakeNoAction() {
  DCHECK(MessageLoop::current() == io_loop_);

  // It is important that we notify the URLRequest only once.  If we try to
  // notify the request twice, it may no longer exist and |this| might have
  // already have been deleted.
  DCHECK(!request_has_been_notified_);

  if (!request_has_been_notified_) {
    request_has_been_notified_ = true;

    // We're done with this object on the IO thread.
    Release();
  }
}


////////////////////////////////////////////////////////////////////////////////
// CertError

SSLManager::CertError::CertError(
    ResourceDispatcherHost* rdh,
    URLRequest* request,
    ResourceType::Type resource_type,
    int cert_error,
    net::X509Certificate* cert,
    MessageLoop* ui_loop)
    : ErrorHandler(rdh, request, ui_loop),
      cert_error_(cert_error),
      resource_type_(resource_type) {
  DCHECK(request == resource_dispatcher_host_->GetURLRequest(request_id_));

  // We cannot use the request->ssl_info(), it's not been initialized yet, so
  // we have to set the fields manually.
  ssl_info_.cert = cert;
  ssl_info_.SetCertError(cert_error);
}

// static
void SSLManager::OnSSLCertificateError(ResourceDispatcherHost* rdh,
                                       URLRequest* request,
                                       int cert_error,
                                       net::X509Certificate* cert,
                                       MessageLoop* ui_loop) {
  DLOG(INFO) << "OnSSLCertificateError() cert_error: " << cert_error <<
                " url: " << request->url().spec();

  ResourceDispatcherHost::ExtraRequestInfo* info =
      ResourceDispatcherHost::ExtraInfoForRequest(request);
  DCHECK(info);

  // A certificate error occurred.  Construct a CertError object and hand it
  // over to the UI thread for processing.
  ui_loop->PostTask(FROM_HERE,
      NewRunnableMethod(new CertError(rdh, request, info->resource_type,
                                      cert_error, cert, ui_loop),
                        &CertError::Dispatch));
}

// static
void SSLManager::OnMixedContentRequest(ResourceDispatcherHost* rdh,
                                       URLRequest* request,
                                       MessageLoop* ui_loop) {
  ui_loop->PostTask(FROM_HERE,
      NewRunnableMethod(new MixedContentHandler(rdh, request, ui_loop),
                        &MixedContentHandler::Dispatch));
}

void SSLManager::OnCertError(CertError* error) {
  // Ask our delegate to deal with the error.
  NavigationEntry* entry = controller_->GetActiveEntry();
  // We might not have a navigation entry in some cases (e.g. when a
  // HTTPS page opens a popup with no URL and then populate it with
  // document.write()). See bug http://crbug.com/3845.
  if (!entry)
    return;

  delegate()->OnCertError(entry->url(), error);
}

void SSLManager::OnMixedContent(MixedContentHandler* mixed_content) {
  // Ask our delegate to deal with the mixed content.
  NavigationEntry* entry = controller_->GetActiveEntry();
  // We might not have a navigation entry in some cases (e.g. when a
  // HTTPS page opens a popup with no URL and then populate it with
  // document.write()). See bug http://crbug.com/3845.
  if (!entry)
    return;

  delegate()->OnMixedContent(controller_, entry->url(), mixed_content);
}

void SSLManager::Observe(NotificationType type,
                         const NotificationSource& source,
                         const NotificationDetails& details) {
  // We should only be getting notifications from our controller.
  DCHECK(source == Source<NavigationController>(controller_));

  // Dispatch by type.
  switch (type.value) {
    case NotificationType::NAV_ENTRY_COMMITTED:
      DidCommitProvisionalLoad(details);
      break;
    case NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR:
      DidFailProvisionalLoadWithError(
          Details<ProvisionalLoadDetails>(details).ptr());
      break;
    case NotificationType::RESOURCE_RESPONSE_STARTED:
      DidStartResourceResponse(Details<ResourceRequestDetails>(details).ptr());
      break;
    case NotificationType::RESOURCE_RECEIVED_REDIRECT:
      DidReceiveResourceRedirect(
          Details<ResourceRedirectDetails>(details).ptr());
      break;
    case NotificationType::LOAD_FROM_MEMORY_CACHE:
      DidLoadFromMemoryCache(
          Details<LoadFromMemoryCacheDetails>(details).ptr());
      break;
    default:
      NOTREACHED() << "The SSLManager received an unexpected notification.";
  }
}

void SSLManager::InitializeEntryIfNeeded(NavigationEntry* entry) {
  DCHECK(entry);

  // If the security style of the entry is SECURITY_STYLE_UNKNOWN, then it is a
  // fresh entry and should get the default style.
  if (entry->ssl().security_style() == SECURITY_STYLE_UNKNOWN) {
    entry->ssl().set_security_style(
        delegate()->GetDefaultStyle(entry->url()));
  }
}

void SSLManager::NavigationStateChanged() {
  NavigationEntry* active_entry = controller_->GetActiveEntry();
  if (!active_entry)
    return;  // Nothing showing yet.

  // This might be a new entry we've never seen before.
  InitializeEntryIfNeeded(active_entry);
}

void SSLManager::DidLoadFromMemoryCache(LoadFromMemoryCacheDetails* details) {
  DCHECK(details);

  // Simulate loading this resource through the usual path.
  // Note that we specify SUB_RESOURCE as the resource type as WebCore only
  // caches sub-resources.
  delegate()->OnRequestStarted(this, details->url(),
                               ResourceType::SUB_RESOURCE,
                               details->ssl_cert_id(),
                               details->ssl_cert_status());
}

void SSLManager::DidCommitProvisionalLoad(
    const NotificationDetails& in_details) {
  NavigationController::LoadCommittedDetails* details =
      Details<NavigationController::LoadCommittedDetails>(in_details).ptr();

  // Ignore in-page navigations, they should not change the security style or
  // the info-bars.
  if (details->is_in_page)
    return;

  // Decode the security details.
  int ssl_cert_id, ssl_cert_status, ssl_security_bits;
  DeserializeSecurityInfo(details->serialized_security_info,
                          &ssl_cert_id, &ssl_cert_status, &ssl_security_bits);

  bool changed = false;
  if (details->is_main_frame) {
    // Update the SSL states of the pending entry.
    NavigationEntry* entry = controller_->GetActiveEntry();
    if (entry) {
      // We may not have an entry if this is a navigation to an initial blank
      // page. Reset the SSL information and add the new data we have.
      entry->ssl() = NavigationEntry::SSLStatus();
      InitializeEntryIfNeeded(entry);  // For security_style.
      entry->ssl().set_cert_id(ssl_cert_id);
      entry->ssl().set_cert_status(ssl_cert_status);
      entry->ssl().set_security_bits(ssl_security_bits);
      changed = true;
    }

    ShowPendingMessages();
  }

  // An HTTPS response may not have a certificate for some reason.  When that
  // happens, use the unauthenticated (HTTP) rather than the authentication
  // broken security style so that we can detect this error condition.
  if (net::IsCertStatusError(ssl_cert_status) &&
      !details->is_content_filtered) {
    changed |= SetMaxSecurityStyle(SECURITY_STYLE_AUTHENTICATION_BROKEN);
    if (!details->is_main_frame &&
        !details->entry->ssl().has_unsafe_content()) {
      details->entry->ssl().set_has_unsafe_content();
      changed = true;
    }
  } else if (details->entry->url().SchemeIsSecure() && !ssl_cert_id) {
    if (details->is_main_frame) {
      changed |= SetMaxSecurityStyle(SECURITY_STYLE_UNAUTHENTICATED);
    } else {
      // If the frame has been blocked we keep our security style as
      // authenticated in that case as nothing insecure is actually showing or
      // loaded.
      if (!details->is_content_filtered &&
          !details->entry->ssl().has_mixed_content()) {
        details->entry->ssl().set_has_mixed_content();
        changed = true;
      }
    }
  }

  if (changed) {
    // Only send the notification when something actually changed.
    NotificationService::current()->Notify(
        NotificationType::SSL_STATE_CHANGED,
        Source<NavigationController>(controller_),
        NotificationService::NoDetails());
  }
}

void SSLManager::DidFailProvisionalLoadWithError(
    ProvisionalLoadDetails* details) {
  DCHECK(details);

  // Ignore in-page navigations.
  if (details->in_page_navigation())
    return;

  if (details->main_frame())
    ClearPendingMessages();
}

void SSLManager::DidStartResourceResponse(ResourceRequestDetails* details) {
  DCHECK(details);

  // Notify our delegate that we started a resource request.  Ideally, the
  // delegate should have the ability to cancel the request, but we can't do
  // that yet.
  delegate()->OnRequestStarted(this, details->url(),
                               details->resource_type(),
                               details->ssl_cert_id() ,
                               details->ssl_cert_status());
}

void SSLManager::DidReceiveResourceRedirect(ResourceRedirectDetails* details) {
  // TODO(jcampan): when we receive a redirect for a sub-resource, we may want
  // to clear any mixed/unsafe content error that it may have triggered.
}

void SSLManager::ShowPendingMessages() {
  std::vector<SSLMessageInfo>::const_iterator iter;
  for (iter = pending_messages_.begin();
       iter != pending_messages_.end(); ++iter) {
    ShowMessageWithLink(iter->message, iter->link_text, iter->action);
  }
  ClearPendingMessages();
}

void SSLManager::ClearPendingMessages() {
  pending_messages_.clear();
}

// static
std::string SSLManager::SerializeSecurityInfo(int cert_id,
                                              int cert_status,
                                              int security_bits) {
  Pickle pickle;
  pickle.WriteInt(cert_id);
  pickle.WriteInt(cert_status);
  pickle.WriteInt(security_bits);
  return std::string(static_cast<const char*>(pickle.data()), pickle.size());
}

// static
bool SSLManager::DeserializeSecurityInfo(const std::string& state,
                                         int* cert_id,
                                         int* cert_status,
                                         int* security_bits) {
  DCHECK(cert_id && cert_status && security_bits);
  if (state.empty()) {
    // No SSL used.
    *cert_id = 0;
    *cert_status = 0;
    *security_bits = -1;
    return false;
  }

  Pickle pickle(state.data(), static_cast<int>(state.size()));
  void * iter = NULL;
  pickle.ReadInt(&iter, cert_id);
  pickle.ReadInt(&iter, cert_status);
  pickle.ReadInt(&iter, security_bits);
  return true;
}

// static
bool SSLManager::GetEVCertNames(const net::X509Certificate& cert,
                                std::wstring* short_name,
                                std::wstring* ca_name) {
  DCHECK(short_name || ca_name);

  // EV are required to have an organization name and country.
  if (cert.subject().organization_names.empty() ||
      cert.subject().country_name.empty()) {
    NOTREACHED();
    return false;
  }

  if (short_name) {
    *short_name = l10n_util::GetStringF(
        IDS_SECURE_CONNECTION_EV,
        UTF8ToWide(cert.subject().organization_names[0]),
        UTF8ToWide(cert.subject().country_name));
  }

  if (ca_name) {
    // TODO(wtc): should we show the root CA's name instead?
    *ca_name = l10n_util::GetStringF(
        IDS_SECURE_CONNECTION_EV_CA,
        UTF8ToWide(cert.issuer().organization_names[0]));
  }
  return true;
}