summaryrefslogtreecommitdiffstats
path: root/chrome/browser/profile.cc
blob: fb3508a4ca51981f61b0020de78c02e09360306e (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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
// 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/profile.h"

#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/lock.h"
#include "base/path_service.h"
#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/app/locales/locale_settings.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/greasemonkey_master.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/navigation_controller.h"
#include "chrome/browser/profile_manager.h"
#include "chrome/browser/render_process_host.h"
#include "chrome/browser/spellchecker.h"
#include "chrome/browser/tab_restore_service.h"
#include "chrome/browser/template_url_fetcher.h"
#include "chrome/browser/template_url_model.h"
#include "chrome/browser/visitedlink_master.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/net/cookie_monster_sqlite.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 "net/base/cookie_monster.h"
#include "net/base/cookie_policy.h"
#include "net/http/http_cache.h"
#include "net/proxy/proxy_service.h"
#include "net/url_request/url_request_context.h"
#include "webkit/glue/webkit_glue.h"

using base::Time;
using base::TimeDelta;

// Delay, in milliseconds, before we explicitly create the SessionService.
static const int kCreateSessionServiceDelayMS = 500;

// A pointer to the request context for the default profile.  See comments on
// Profile::GetDefaultRequestContext.
URLRequestContext* Profile::default_request_context_;

//static
void Profile::RegisterUserPrefs(PrefService* prefs) {
  prefs->RegisterBooleanPref(prefs::kSearchSuggestEnabled, true);
  prefs->RegisterBooleanPref(prefs::kSessionExitedCleanly, true);
  prefs->RegisterBooleanPref(prefs::kSafeBrowsingEnabled, true);
  prefs->RegisterLocalizedStringPref(prefs::kSpellCheckDictionary,
      IDS_SPELLCHECK_DICTIONARY);
  prefs->RegisterBooleanPref(prefs::kEnableSpellCheck, true);
}

//static
Profile* Profile::CreateProfile(const std::wstring& path) {
  return new ProfileImpl(path);
}

//static
URLRequestContext* Profile::GetDefaultRequestContext() {
  return default_request_context_;
}


// Sets up proxy info if it was specified, otherwise returns NULL. The
// returned pointer MUST be deleted by the caller if non-NULL.
static net::ProxyInfo* CreateProxyInfo(const CommandLine& command_line) {
  net::ProxyInfo* proxy_info = NULL;

  if (command_line.HasSwitch(switches::kProxyServer)) {
    proxy_info = new net::ProxyInfo();
    const std::wstring& proxy_server =
        command_line.GetSwitchValue(switches::kProxyServer);
    proxy_info->UseNamedProxy(WideToASCII(proxy_server));
  }

  return proxy_info;
}

// Releases the URLRequestContext and sends out a notification about it.
// Note: runs on IO thread.
static void ReleaseURLRequestContext(URLRequestContext* context) {
  NotificationService::current()->Notify(NOTIFY_URL_REQUEST_CONTEXT_RELEASED,
                                         Source<URLRequestContext>(context),
                                         NotificationService::NoDetails());
  context->Release();
}

// A context for URLRequests issued relative to this profile.
class ProfileImpl::RequestContext : public URLRequestContext,
                                    public NotificationObserver {
 public:
  // |cookie_store_path| is the local disk path at which the cookie store
  // is persisted.
  RequestContext(const std::wstring& cookie_store_path,
                 const std::wstring& disk_cache_path,
                 PrefService* prefs)
      : prefs_(prefs) {
    cookie_store_ = NULL;

    // setup user agent
    user_agent_ = webkit_glue::GetUserAgent();
    // set up Accept-Language and Accept-Charset header values
    // TODO(jungshik) : This may slow down http requests. Perhaps,
    // we have to come up with a better way to set up these values.
    accept_language_ = WideToASCII(prefs_->GetString(prefs::kAcceptLanguages));
    accept_charset_ = WideToASCII(prefs_->GetString(prefs::kDefaultCharset));
    accept_charset_ += ",*,utf-8";

    CommandLine command_line;

    scoped_ptr<net::ProxyInfo> proxy_info(CreateProxyInfo(command_line));
    net::HttpCache* cache =
        new net::HttpCache(proxy_info.get(), disk_cache_path, 0);

    bool record_mode = chrome::kRecordModeEnabled &&
                       CommandLine().HasSwitch(switches::kRecordMode);
    bool playback_mode = CommandLine().HasSwitch(switches::kPlaybackMode);

    if (record_mode || playback_mode) {
      // Don't use existing cookies and use an in-memory store.
      cookie_store_ = new net::CookieMonster();
      cache->set_mode(
          record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
    }
    http_transaction_factory_ = cache;

    // setup cookie store
    if (!cookie_store_) {
      DCHECK(!cookie_store_path.empty());
      cookie_db_.reset(new SQLitePersistentCookieStore(
          cookie_store_path, g_browser_process->db_thread()->message_loop()));
      cookie_store_ = new net::CookieMonster(cookie_db_.get());
    }

    cookie_policy_.SetType(net::CookiePolicy::FromInt(
        prefs_->GetInteger(prefs::kCookieBehavior)));

    // The first request context to be created is the one for the default
    // profile - at least until we support multiple profiles.
    if (!default_request_context_)
      default_request_context_ = this;
    NotificationService::current()->Notify(
        NOTIFY_DEFAULT_REQUEST_CONTEXT_AVAILABLE,
        NotificationService::AllSources(), NotificationService::NoDetails());

    // Register for notifications about prefs.
    prefs_->AddPrefObserver(prefs::kAcceptLanguages, this);
    prefs_->AddPrefObserver(prefs::kCookieBehavior, this);
  }

  // NotificationObserver implementation.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    if (NOTIFY_PREF_CHANGED == type) {
      std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
      PrefService* prefs = Source<PrefService>(source).ptr();
      DCHECK(pref_name_in && prefs);
      if (*pref_name_in == prefs::kAcceptLanguages) {
        std::string accept_language =
            WideToASCII(prefs->GetString(prefs::kAcceptLanguages));
        g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
            NewRunnableMethod(this,
                              &RequestContext::OnAcceptLanguageChange,
                              accept_language));
      } else if (*pref_name_in == prefs::kCookieBehavior) {
        net::CookiePolicy::Type type = net::CookiePolicy::FromInt(
            prefs_->GetInteger(prefs::kCookieBehavior));
        g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
            NewRunnableMethod(this,
                              &RequestContext::OnCookiePolicyChange,
                              type));
      }
    }
  }

  // Since ProfileImpl::RequestContext will be destroyed on IO thread, but all
  // PrefService observers are needed to clear in before destroying ProfileImpl.
  // So we use to CleanupBeforeDestroy to do this thing. This function need to
  // be called on destructor of ProfileImpl.
  void CleanupBeforeDestroy() {
    // Unregister for pref notifications.
    prefs_->RemovePrefObserver(prefs::kAcceptLanguages, this);
    prefs_->RemovePrefObserver(prefs::kCookieBehavior, this);
    prefs_ = NULL;
  }

  void OnAcceptLanguageChange(std::string accept_language) {
    DCHECK(MessageLoop::current() ==
           ChromeThread::GetMessageLoop(ChromeThread::IO));
    accept_language_ = accept_language;
  }

  void OnCookiePolicyChange(net::CookiePolicy::Type type) {
    DCHECK(MessageLoop::current() ==
           ChromeThread::GetMessageLoop(ChromeThread::IO));
    cookie_policy_.SetType(type);
  }

  virtual ~RequestContext() {
    DCHECK(NULL == prefs_);
    delete cookie_store_;
    delete http_transaction_factory_;

    if (default_request_context_ == this)
      default_request_context_ = NULL;
  }

 private:
  scoped_ptr<SQLitePersistentCookieStore> cookie_db_;
  PrefService* prefs_;
};

////////////////////////////////////////////////////////////////////////////////
//
// An URLRequestContext proxy for OffTheRecord. This request context is
// really a proxy to the original profile's request context but set
// is_off_the_record_ to true.
//
// TODO(ACW). Do we need to share the FtpAuthCache with the real request context
// see bug 974328
//
// TODO(jackson): http://b/issue?id=1197350 Remove duplicated code from above.
//
////////////////////////////////////////////////////////////////////////////////
class OffTheRecordRequestContext : public URLRequestContext,
                                   public NotificationObserver {
 public:
  explicit OffTheRecordRequestContext(Profile* profile) {
    DCHECK(!profile->IsOffTheRecord());
    prefs_ = profile->GetPrefs();
    DCHECK(prefs_);

    // The OffTheRecordRequestContext is owned by the OffTheRecordProfileImpl
    // which is itself owned by the original profile. We reference the original
    // context to make sure it doesn't go away when we delete the object graph.
    original_context_ = profile->GetRequestContext();

    CommandLine command_line;
    scoped_ptr<net::ProxyInfo> proxy_info(CreateProxyInfo(command_line));

    http_transaction_factory_ = new net::HttpCache(NULL, 0);
    cookie_store_ = new net::CookieMonster;
    cookie_policy_.SetType(net::CookiePolicy::FromInt(
        prefs_->GetInteger(prefs::kCookieBehavior)));
    user_agent_ = original_context_->user_agent();
    accept_language_ = original_context_->accept_language();
    accept_charset_ = original_context_->accept_charset();
    is_off_the_record_ = true;

    // Register for notifications about prefs.
    prefs_->AddPrefObserver(prefs::kAcceptLanguages, this);
    prefs_->AddPrefObserver(prefs::kCookieBehavior, this);
  }

  // Since OffTheRecordProfileImpl maybe be destroyed after destroying
  // PrefService, but all PrefService observers are needed to clear in
  // before destroying PrefService. So we use to CleanupBeforeDestroy
  // to do this thing. This function need to be called on destructor
  // of ProfileImpl.
  void CleanupBeforeDestroy() {
    // Unregister for pref notifications.
    prefs_->RemovePrefObserver(prefs::kAcceptLanguages, this);
    prefs_->RemovePrefObserver(prefs::kCookieBehavior, this);
    prefs_ = NULL;
  }

  // NotificationObserver implementation.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    if (NOTIFY_PREF_CHANGED == type) {
      std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
      PrefService* prefs = Source<PrefService>(source).ptr();
      DCHECK(pref_name_in && prefs);
      if (*pref_name_in == prefs::kAcceptLanguages) {
        std::string accept_language =
            WideToASCII(prefs->GetString(prefs::kAcceptLanguages));
        g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
            NewRunnableMethod(
                this,
                &OffTheRecordRequestContext::OnAcceptLanguageChange,
                accept_language));
      } else if (*pref_name_in == prefs::kCookieBehavior) {
        net::CookiePolicy::Type type = net::CookiePolicy::FromInt(
            prefs_->GetInteger(prefs::kCookieBehavior));
        g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
            NewRunnableMethod(this,
                              &OffTheRecordRequestContext::OnCookiePolicyChange,
                              type));
      }
    }
  }

  void OnAcceptLanguageChange(std::string accept_language) {
    DCHECK(MessageLoop::current() ==
           ChromeThread::GetMessageLoop(ChromeThread::IO));
    accept_language_ = accept_language;
  }

  void OnCookiePolicyChange(net::CookiePolicy::Type type) {
    DCHECK(MessageLoop::current() ==
           ChromeThread::GetMessageLoop(ChromeThread::IO));
    cookie_policy_.SetType(type);
  }

  virtual ~OffTheRecordRequestContext() {
    DCHECK(NULL == prefs_);
    delete cookie_store_;
    delete http_transaction_factory_;
    // The OffTheRecordRequestContext simply act as a proxy to the real context.
    // There is nothing else to delete.
  }

 private:
  // The original (non off the record) URLRequestContext.
  scoped_refptr<URLRequestContext> original_context_;
  PrefService* prefs_;

  DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordRequestContext);
};

////////////////////////////////////////////////////////////////////////////////
//
// OffTheRecordProfileImpl is a profile subclass that wraps an existing profile
// to  make it suitable for the off the record mode.
//
////////////////////////////////////////////////////////////////////////////////
class OffTheRecordProfileImpl : public Profile,
                                public NotificationObserver {
 public:
  explicit OffTheRecordProfileImpl(Profile* real_profile)
      : profile_(real_profile),
        start_time_(Time::Now()) {
    request_context_ = new OffTheRecordRequestContext(real_profile);
    request_context_->AddRef();
    // Register for browser close notifications so we can detect when the last
    // off-the-record window is closed, in which case we can clean our states
    // (cookies, downloads...).
    NotificationService::current()->AddObserver(
        this, NOTIFY_BROWSER_CLOSED, NotificationService::AllSources());
  }

  virtual ~OffTheRecordProfileImpl() {
    if (request_context_) {
      request_context_->CleanupBeforeDestroy();
      // Clean up request context on IO thread.
      g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
          NewRunnableFunction(&ReleaseURLRequestContext, request_context_));
      request_context_ = NULL;
    }
    NotificationService::current()->RemoveObserver(
        this, NOTIFY_BROWSER_CLOSED, NotificationService::AllSources());
  }

  virtual std::wstring GetPath() { return profile_->GetPath(); }

  virtual bool IsOffTheRecord() {
    return true;
  }

  virtual Profile* GetOffTheRecordProfile() {
    return this;
  }

  virtual Profile* GetOriginalProfile() {
    return profile_;
  }

  virtual VisitedLinkMaster* GetVisitedLinkMaster() {
    return profile_->GetVisitedLinkMaster();
  }

  virtual GreasemonkeyMaster* GetGreasemonkeyMaster() {
    return profile_->GetGreasemonkeyMaster();
  }

  virtual HistoryService* GetHistoryService(ServiceAccessType sat) {
    if (sat == EXPLICIT_ACCESS) {
      return profile_->GetHistoryService(sat);
    } else {
      NOTREACHED() << "This profile is OffTheRecord";
      return NULL;
    }
  }

  virtual WebDataService* GetWebDataService(ServiceAccessType sat) {
    if (sat == EXPLICIT_ACCESS) {
      return profile_->GetWebDataService(sat);
    } else {
      NOTREACHED() << "This profile is OffTheRecord";
      return NULL;
    }
  }

  virtual PrefService* GetPrefs() {
    return profile_->GetPrefs();
  }

  virtual TemplateURLModel* GetTemplateURLModel() {
    return profile_->GetTemplateURLModel();
  }

  virtual TemplateURLFetcher* GetTemplateURLFetcher() {
    return profile_->GetTemplateURLFetcher();
  }

  virtual DownloadManager* GetDownloadManager() {
    if (!download_manager_.get()) {
      scoped_refptr<DownloadManager> dlm(new DownloadManager);
      dlm->Init(this);
      download_manager_.swap(dlm);
    }
    return download_manager_.get();
  }

  virtual bool HasCreatedDownloadManager() const {
    return (download_manager_.get() != NULL);
  }

  virtual URLRequestContext* GetRequestContext() {
    return request_context_;
  }

  virtual SessionService* GetSessionService() {
    // Don't save any sessions when off the record.
    return NULL;
  }

  virtual void ShutdownSessionService() {
    // We don't allow a session service, nothing to do.
  }

  virtual bool HasSessionService() const {
    // We never have a session service.
    return false;
  }

  virtual std::wstring GetName() {
    return profile_->GetName();
  }

  virtual void SetName(const std::wstring& name) {
    profile_->SetName(name);
  }

  virtual std::wstring GetID() {
    return profile_->GetID();
  }

  virtual void SetID(const std::wstring& id) {
    profile_->SetID(id);
  }

  virtual bool DidLastSessionExitCleanly() {
    return profile_->DidLastSessionExitCleanly();
  }

  virtual BookmarkModel* GetBookmarkModel() {
    return profile_->GetBookmarkModel();
  }

#ifdef CHROME_PERSONALIZATION
  virtual ProfilePersonalization* GetProfilePersonalization() {
    return profile_->GetProfilePersonalization();
  }
#endif

  virtual bool IsSameProfile(Profile* profile) {
    if (profile == static_cast<Profile*>(this))
      return true;
    return profile == profile_;
  }

  virtual Time GetStartTime() const {
    return start_time_;
  }

  virtual TabRestoreService* GetTabRestoreService() {
    return NULL;
  }

  virtual void ResetTabRestoreService() {
  }

  virtual void ReinitializeSpellChecker() {
    profile_->ReinitializeSpellChecker();
  }

  virtual SpellChecker* GetSpellChecker() {
    return profile_->GetSpellChecker();
  }

  virtual void MarkAsCleanShutdown() {
  }

  virtual void ExitedOffTheRecordMode() {
    // Drop our download manager so we forget about all the downloads made
    // in off-the-record mode.
    download_manager_ = NULL;
  }

  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    DCHECK_EQ(NOTIFY_BROWSER_CLOSED, type);
    // We are only interested in OTR browser closing.
    if (Source<Browser>(source)->profile() != this)
      return;

    // Let's check if we still have an Off The Record window opened.
    // Note that we check against 1 as this notification is sent before the
    // browser window is actually removed from the list.
    if (BrowserList::GetBrowserCount(this) <= 1)
      ExitedOffTheRecordMode();
  }

 private:
  // The real underlying profile.
  Profile* profile_;

  // A proxy to the real request context.
  OffTheRecordRequestContext* request_context_;

  // The download manager that only stores downloaded items in memory.
  scoped_refptr<DownloadManager> download_manager_;

  // Time we were started.
  Time start_time_;

  DISALLOW_EVIL_CONSTRUCTORS(OffTheRecordProfileImpl);
};

ProfileImpl::ProfileImpl(const std::wstring& path)
    : path_(path),
      off_the_record_(false),
      history_service_created_(false),
      created_web_data_service_(false),
      created_download_manager_(false),
      request_context_(NULL),
      start_time_(Time::Now()),
      spellchecker_(NULL),
#ifdef CHROME_PERSONALIZATION
      personalization_(NULL),
#endif
      shutdown_session_service_(false) {
  DCHECK(!path.empty()) << "Using an empty path will attempt to write " <<
                            "profile files to the root directory!";
  create_session_service_timer_.Start(
      TimeDelta::FromMilliseconds(kCreateSessionServiceDelayMS), this,
      &ProfileImpl::EnsureSessionServiceCreated);
  PrefService* prefs = GetPrefs();
  prefs->AddPrefObserver(prefs::kSpellCheckDictionary, this);
  prefs->AddPrefObserver(prefs::kEnableSpellCheck, this);
}

ProfileImpl::~ProfileImpl() {
  tab_restore_service_.reset();

  StopCreateSessionServiceTimer();
  // TemplateURLModel schedules a task on the WebDataService from its
  // destructor. Delete it first to ensure the task gets scheduled before we
  // shut down the database.
  template_url_model_.reset();

  // The download manager queries the history system and should be deleted
  // before the history is shutdown so it can properly cancel all requests.
  download_manager_ = NULL;

  // Remove pref observers.
  PrefService* prefs = GetPrefs();
  prefs->RemovePrefObserver(prefs::kSpellCheckDictionary, this);
  prefs->RemovePrefObserver(prefs::kEnableSpellCheck, this);

#ifdef CHROME_PERSONALIZATION
  personalization_.reset();
#endif

  // Both HistoryService and WebDataService maintain threads for background
  // processing. Its possible each thread still has tasks on it that have
  // increased the ref count of the service. In such a situation, when we
  // decrement the refcount, it won't be 0, and the threads/databases aren't
  // properly shut down. By explicitly calling Cleanup/Shutdown we ensure the
  // databases are properly closed.
  if (web_data_service_.get())
    web_data_service_->Shutdown();

  if (history_service_.get())
    history_service_->Cleanup();

  // The I/O thread may be NULL during testing.
  base::Thread* io_thread = g_browser_process->io_thread();

  if (spellchecker_) {
    // The spellchecker must be deleted on the I/O thread. During testing, we
    // don't have an I/O thread.
    if (io_thread)
      io_thread->message_loop()->ReleaseSoon(FROM_HERE, spellchecker_);
    else
      spellchecker_->Release();
  }

  if (request_context_) {
    request_context_->CleanupBeforeDestroy();
    // Clean up request context on IO thread.
    io_thread->message_loop()->PostTask(FROM_HERE,
        NewRunnableFunction(&ReleaseURLRequestContext, request_context_));
    request_context_ = NULL;
  }

  // HistoryService may call into the BookmarkModel, as such we need to
  // delete HistoryService before the BookmarkModel. The destructor for
  // HistoryService will join with HistoryService's backend thread so that
  // by the time the destructor has finished we're sure it will no longer call
  // into the BookmarkModel.
  history_service_ = NULL;
  bookmark_bar_model_.reset();

  MarkAsCleanShutdown();
}

std::wstring ProfileImpl::GetPath() {
  return path_;
}

bool ProfileImpl::IsOffTheRecord() {
  return false;
}

Profile* ProfileImpl::GetOffTheRecordProfile() {
  if (!off_the_record_profile_.get()) {
    scoped_ptr<OffTheRecordProfileImpl> p(new OffTheRecordProfileImpl(this));
    off_the_record_profile_.swap(p);
  }
  return off_the_record_profile_.get();
}

Profile* ProfileImpl::GetOriginalProfile() {
  return this;
}

static void BroadcastNewHistoryTable(base::SharedMemory* table_memory) {
  if (!table_memory)
    return;

  // send to all RenderProcessHosts
  for (RenderProcessHost::iterator i = RenderProcessHost::begin();
       i != RenderProcessHost::end(); i++) {
    if (!i->second->channel())
      continue;

    base::SharedMemoryHandle new_table;
    HANDLE process = i->second->process().handle();
    if (!process) {
      // process can be null if it's started with the --single-process flag.
      process = GetCurrentProcess();
    }

    table_memory->ShareToProcess(process, &new_table);
    IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(new_table);
    i->second->channel()->Send(msg);
  }
}

VisitedLinkMaster* ProfileImpl::GetVisitedLinkMaster() {
  if (!visited_link_master_.get()) {
    scoped_ptr<VisitedLinkMaster> visited_links(
      new VisitedLinkMaster(g_browser_process->file_thread(),
                            BroadcastNewHistoryTable, this));
    if (!visited_links->Init())
      return NULL;
    visited_link_master_.swap(visited_links);
  }

  return visited_link_master_.get();
}

GreasemonkeyMaster* ProfileImpl::GetGreasemonkeyMaster() {
  if (!greasemonkey_master_.get()) {
    std::wstring script_dir_str;
    PathService::Get(chrome::DIR_USER_SCRIPTS, &script_dir_str);
    FilePath script_dir(script_dir_str);
    greasemonkey_master_ =
        new GreasemonkeyMaster(g_browser_process->file_thread()->message_loop(),
                               script_dir);
  }

  return greasemonkey_master_.get();
}

PrefService* ProfileImpl::GetPrefs() {
  if (!prefs_.get()) {
    prefs_.reset(new PrefService(GetPrefFilePath()));

    // The Profile class and ProfileManager class may read some prefs so
    // register known prefs as soon as possible.
    Profile::RegisterUserPrefs(prefs_.get());
    ProfileManager::RegisterUserPrefs(prefs_.get());

    // The last session exited cleanly if there is no pref for
    // kSessionExitedCleanly or the value for kSessionExitedCleanly is true.
    last_session_exited_cleanly_ =
        prefs_->GetBoolean(prefs::kSessionExitedCleanly);
    // Mark the session as open.
    prefs_->SetBoolean(prefs::kSessionExitedCleanly, false);
    // Make sure we save to disk that the session has opened.
    prefs_->ScheduleSavePersistentPrefs(g_browser_process->file_thread());
  }

  return prefs_.get();
}

std::wstring ProfileImpl::GetPrefFilePath() {
  std::wstring pref_file_path = path_;
  file_util::AppendToPath(&pref_file_path, chrome::kPreferencesFilename);
  return pref_file_path;
}

URLRequestContext* ProfileImpl::GetRequestContext() {
  if (!request_context_) {
    std::wstring cookie_path = GetPath();
    file_util::AppendToPath(&cookie_path, chrome::kCookieFilename);
    std::wstring cache_path = GetPath();
    file_util::AppendToPath(&cache_path, chrome::kCacheDirname);
    request_context_ =
        new ProfileImpl::RequestContext(cookie_path, cache_path, GetPrefs());
    request_context_->AddRef();

    DCHECK(request_context_->cookie_store());
  }

  return request_context_;
}

HistoryService* ProfileImpl::GetHistoryService(ServiceAccessType sat) {
  if (!history_service_created_) {
    history_service_created_ = true;
    scoped_refptr<HistoryService> history(new HistoryService(this));
    if (!history->Init(GetPath(), GetBookmarkModel()))
      return NULL;
    history_service_.swap(history);

    // Send out the notification that the history service was created.
    NotificationService::current()->
        Notify(NOTIFY_HISTORY_CREATED, Source<Profile>(this),
               Details<HistoryService>(history_service_.get()));
  }
  return history_service_.get();
}

TemplateURLModel* ProfileImpl::GetTemplateURLModel() {
  if (!template_url_model_.get())
    template_url_model_.reset(new TemplateURLModel(this));
  return template_url_model_.get();
}

TemplateURLFetcher* ProfileImpl::GetTemplateURLFetcher() {
  if (!template_url_fetcher_.get())
    template_url_fetcher_.reset(new TemplateURLFetcher(this));
  return template_url_fetcher_.get();
}

WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) {
  if (!created_web_data_service_)
    CreateWebDataService();
  return web_data_service_.get();
}

void ProfileImpl::CreateWebDataService() {
  DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL);
  created_web_data_service_ = true;
  scoped_refptr<WebDataService> wds(new WebDataService());
  if (!wds->Init(GetPath()))
    return;
  web_data_service_.swap(wds);
}

DownloadManager* ProfileImpl::GetDownloadManager() {
  if (!created_download_manager_) {
    scoped_refptr<DownloadManager> dlm(new DownloadManager);
    dlm->Init(this);
    created_download_manager_ = true;
    download_manager_.swap(dlm);
  }
  return download_manager_.get();
}

bool ProfileImpl::HasCreatedDownloadManager() const {
  return created_download_manager_;
}

SessionService* ProfileImpl::GetSessionService() {
  if (!session_service_.get() && !shutdown_session_service_) {
    session_service_ = new SessionService(this);
    session_service_->ResetFromCurrentBrowsers();
  }
  return session_service_.get();
}

void ProfileImpl::ShutdownSessionService() {
  if (shutdown_session_service_)
    return;

  // We're about to exit, force creation of the session service if it hasn't
  // been created yet. We do this to ensure session state matches the point in
  // time the user exited.
  GetSessionService();
  shutdown_session_service_ = true;
  session_service_ = NULL;
}

bool ProfileImpl::HasSessionService() const {
  return (session_service_.get() != NULL);
}

std::wstring ProfileImpl::GetName() {
  return GetPrefs()->GetString(prefs::kProfileName);
}
void ProfileImpl::SetName(const std::wstring& name) {
  GetPrefs()->SetString(prefs::kProfileName, name);
}

std::wstring ProfileImpl::GetID() {
  return GetPrefs()->GetString(prefs::kProfileID);
}
void ProfileImpl::SetID(const std::wstring& id) {
  GetPrefs()->SetString(prefs::kProfileID, id);
}

bool ProfileImpl::DidLastSessionExitCleanly() {
  // last_session_exited_cleanly_ is set when the preferences are loaded. Force
  // it to be set by asking for the prefs.
  GetPrefs();
  return last_session_exited_cleanly_;
}

BookmarkModel* ProfileImpl::GetBookmarkModel() {
  if (!bookmark_bar_model_.get()) {
    bookmark_bar_model_.reset(new BookmarkModel(this));
    bookmark_bar_model_->Load();
  }
  return bookmark_bar_model_.get();
}

bool ProfileImpl::IsSameProfile(Profile* profile) {
  if (profile == static_cast<Profile*>(this))
    return true;
  OffTheRecordProfileImpl* otr_profile = off_the_record_profile_.get();
  return otr_profile && profile == static_cast<Profile*>(otr_profile);
}

Time ProfileImpl::GetStartTime() const {
  return start_time_;
}

TabRestoreService* ProfileImpl::GetTabRestoreService() {
  if (!tab_restore_service_.get())
    tab_restore_service_.reset(new TabRestoreService(this));
  return tab_restore_service_.get();
}

void ProfileImpl::ResetTabRestoreService() {
  tab_restore_service_.reset(NULL);
}

// To be run in the IO thread to notify all resource message filters that the 
// spellchecker has changed.
class NotifySpellcheckerChangeTask : public Task {
 public:
  NotifySpellcheckerChangeTask(
      Profile* profile,
      const SpellcheckerReinitializedDetails& spellchecker)
      : profile_(profile),
        spellchecker_(spellchecker) {
  }

 private:
  void Run(void) {
    NotificationService::current()->Notify(
        NOTIFY_SPELLCHECKER_REINITIALIZED,
        Source<Profile>(profile_),
        Details<SpellcheckerReinitializedDetails>(&spellchecker_));
  }

  Profile* profile_;
  SpellcheckerReinitializedDetails spellchecker_;
};

void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
  // The I/O thread may be NULL during testing.
  base::Thread* io_thread = g_browser_process->io_thread();
  if (spellchecker_) {
    // The spellchecker must be deleted on the I/O thread.
    // A dummy variable to aid in logical clarity.
    SpellChecker* last_spellchecker = spellchecker_;

    if (io_thread)
      io_thread->message_loop()->ReleaseSoon(FROM_HERE, last_spellchecker);
    else  //  during testing, we don't have an I/O thread
      last_spellchecker->Release();
  }

  // Retrieve the (perhaps updated recently) dictionary name from preferences.
  PrefService* prefs = GetPrefs();
  bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);

  if (enable_spellcheck) {
    std::wstring dict_dir;
    PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
    std::wstring language = prefs->GetString(prefs::kSpellCheckDictionary);

    // Note that, as the object pointed to by previously by spellchecker_ 
    // is being deleted in the io thread, the spellchecker_ can be made to point
    // to a new object (RE-initialized) in parallel in this UI thread.
    spellchecker_ = new SpellChecker(dict_dir, language, GetRequestContext(), 
                                     L"");
    spellchecker_->AddRef();  // Manual refcounting.
  } else {
    spellchecker_ = NULL;
  }

  if (need_to_broadcast && io_thread) {  // Notify resource message filters.
    SpellcheckerReinitializedDetails scoped_spellchecker;
    scoped_spellchecker.spellchecker = spellchecker_;
    if (io_thread) {
      io_thread->message_loop()->PostTask(
          FROM_HERE, 
          new NotifySpellcheckerChangeTask(this, scoped_spellchecker));
    }
  }
}

void ProfileImpl::ReinitializeSpellChecker() {
  InitializeSpellChecker(true);
}

SpellChecker* ProfileImpl::GetSpellChecker() {
  if (!spellchecker_) {
    // This is where spellchecker gets initialized. Note that this is being
    // initialized in the ui_thread. However, this is not a problem as long as
    // it is *used* in the io thread.
    // TODO (sidchat) One day, change everything so that spellchecker gets
    // initialized in the IO thread itself.
    InitializeSpellChecker(false);
  }

  return spellchecker_;
}

void ProfileImpl::MarkAsCleanShutdown() {
  if (prefs_.get()) {
    // The session cleanly exited, set kSessionExitedCleanly appropriately.
    prefs_->SetBoolean(prefs::kSessionExitedCleanly, true);

    // NOTE: If you change what thread this writes on, be sure and update
    // ChromeFrame::EndSession().
    prefs_->SavePersistentPrefs(g_browser_process->file_thread());
  }
}

void ProfileImpl::Observe(NotificationType type,
                          const NotificationSource& source,
                          const NotificationDetails& details) {
  if (NOTIFY_PREF_CHANGED == type) {
    std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
    PrefService* prefs = Source<PrefService>(source).ptr();
    DCHECK(pref_name_in && prefs);
    if (*pref_name_in == prefs::kSpellCheckDictionary ||
        *pref_name_in == prefs::kEnableSpellCheck) {
      InitializeSpellChecker(true);
    }
  }
}

void ProfileImpl::StopCreateSessionServiceTimer() {
  create_session_service_timer_.Stop();
}

#ifdef CHROME_PERSONALIZATION
ProfilePersonalization* ProfileImpl::GetProfilePersonalization() {
  if (!personalization_.get())
    personalization_.reset(
        Personalization::CreateProfilePersonalization(this));
  return personalization_.get();
}
#endif