Home | History | Annotate | Download | only in profiles
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_
      6 #define CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_
      7 #pragma once
      8 
      9 #include "base/basictypes.h"
     10 #include "base/file_path.h"
     11 #include "base/hash_tables.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "chrome/browser/profiles/profile_io_data.h"
     15 
     16 class ChromeURLRequestContext;
     17 class ChromeURLRequestContextGetter;
     18 class Profile;
     19 
     20 // OffTheRecordProfile owns a OffTheRecordProfileIOData::Handle, which holds a
     21 // reference to the OffTheRecordProfileIOData. OffTheRecordProfileIOData is
     22 // intended to own all the objects owned by OffTheRecordProfile which live on
     23 // the IO thread, such as, but not limited to, network objects like
     24 // CookieMonster, HttpTransactionFactory, etc. OffTheRecordProfileIOData is
     25 // owned by the OffTheRecordProfile and OffTheRecordProfileIOData's
     26 // ChromeURLRequestContexts. When all of them go away, then ProfileIOData will
     27 // be deleted. Note that the OffTheRecordProfileIOData will typically outlive
     28 // the Profile it is "owned" by, so it's important for OffTheRecordProfileIOData
     29 // not to hold any references to the Profile beyond what's used by LazyParams
     30 // (which should be deleted after lazy initialization).
     31 
     32 class OffTheRecordProfileIOData : public ProfileIOData {
     33  public:
     34   class Handle {
     35    public:
     36     explicit Handle(Profile* profile);
     37     ~Handle();
     38 
     39     const content::ResourceContext& GetResourceContext() const;
     40     scoped_refptr<ChromeURLRequestContextGetter>
     41         GetMainRequestContextGetter() const;
     42     scoped_refptr<ChromeURLRequestContextGetter>
     43         GetExtensionsRequestContextGetter() const;
     44     scoped_refptr<ChromeURLRequestContextGetter>
     45         GetIsolatedAppRequestContextGetter(
     46             const std::string& app_id) const;
     47 
     48    private:
     49     typedef base::hash_map<std::string,
     50                            scoped_refptr<ChromeURLRequestContextGetter> >
     51         ChromeURLRequestContextGetterMap;
     52 
     53     // Lazily initialize ProfileParams. We do this on the calls to
     54     // Get*RequestContextGetter(), so we only initialize ProfileParams right
     55     // before posting a task to the IO thread to start using them. This prevents
     56     // objects that are supposed to be deleted on the IO thread, but are created
     57     // on the UI thread from being unnecessarily initialized.
     58     void LazyInitialize() const;
     59 
     60     // Ordering is important here. Do not reorder unless you know what you're
     61     // doing. We need to release |io_data_| *before* the getters, because we
     62     // want to make sure that the last reference for |io_data_| is on the IO
     63     // thread. The getters will be deleted on the IO thread, so they will
     64     // release their refs to their contexts, which will release the last refs to
     65     // the ProfileIOData on the IO thread.
     66     mutable scoped_refptr<ChromeURLRequestContextGetter>
     67         main_request_context_getter_;
     68     mutable scoped_refptr<ChromeURLRequestContextGetter>
     69         extensions_request_context_getter_;
     70     mutable ChromeURLRequestContextGetterMap
     71         app_request_context_getter_map_;
     72     const scoped_refptr<OffTheRecordProfileIOData> io_data_;
     73 
     74     Profile* const profile_;
     75 
     76     mutable bool initialized_;
     77 
     78     DISALLOW_COPY_AND_ASSIGN(Handle);
     79   };
     80 
     81  private:
     82   friend class base::RefCountedThreadSafe<OffTheRecordProfileIOData>;
     83 
     84   typedef base::hash_map<std::string, net::HttpTransactionFactory* >
     85       HttpTransactionFactoryMap;
     86 
     87   OffTheRecordProfileIOData();
     88   ~OffTheRecordProfileIOData();
     89 
     90   virtual void LazyInitializeInternal(ProfileParams* profile_params) const;
     91   virtual scoped_refptr<RequestContext> InitializeAppRequestContext(
     92       scoped_refptr<ChromeURLRequestContext> main_context,
     93       const std::string& app_id) const;
     94   virtual scoped_refptr<ChromeURLRequestContext>
     95       AcquireMediaRequestContext() const;
     96   virtual scoped_refptr<ChromeURLRequestContext>
     97       AcquireIsolatedAppRequestContext(
     98           scoped_refptr<ChromeURLRequestContext> main_context,
     99           const std::string& app_id) const;
    100 
    101   mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
    102 
    103   // One HttpTransactionFactory per isolated app.
    104   mutable HttpTransactionFactoryMap app_http_factory_map_;
    105 
    106   DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileIOData);
    107 };
    108 
    109 #endif  // CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_
    110