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

Side by Side Diff: chrome/browser/net/chrome_url_request_context.cc

Issue 16408: Refactor the two URLRequestContext subclasses in profile.cc into a new shared (Closed)
Patch Set: aa@chromium.org Created 11 years, 12 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 | « chrome/browser/net/chrome_url_request_context.h ('k') | chrome/browser/profile.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/net/chrome_url_request_context.h"
6
7 #include "base/command_line.h"
8 #include "base/string_util.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chrome_thread.h"
11 #include "chrome/browser/profile.h"
12 #include "chrome/common/chrome_constants.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "net/http/http_cache.h"
16 #include "net/proxy/proxy_service.h"
17 #include "webkit/glue/webkit_glue.h"
18
19 // Sets up proxy info if it was specified, otherwise returns NULL. The
20 // returned pointer MUST be deleted by the caller if non-NULL.
21 static net::ProxyInfo* CreateProxyInfo() {
22 net::ProxyInfo* proxy_info = NULL;
23
24 CommandLine command_line;
25 if (command_line.HasSwitch(switches::kProxyServer)) {
26 proxy_info = new net::ProxyInfo();
27 const std::wstring& proxy_server =
28 command_line.GetSwitchValue(switches::kProxyServer);
29 proxy_info->UseNamedProxy(WideToASCII(proxy_server));
30 }
31
32 return proxy_info;
33 }
34
35 // static
36 ChromeURLRequestContext* ChromeURLRequestContext::CreateOriginal(
37 Profile* profile, const std::wstring& cookie_store_path,
38 const std::wstring& disk_cache_path) {
39 DCHECK(!profile->IsOffTheRecord());
40 ChromeURLRequestContext* context = new ChromeURLRequestContext(profile);
41
42 scoped_ptr<net::ProxyInfo> proxy_info(CreateProxyInfo());
43 context->proxy_service_ = net::ProxyService::Create(proxy_info.get());
44
45 net::HttpCache* cache =
46 new net::HttpCache(context->proxy_service_, disk_cache_path, 0);
47
48 CommandLine command_line;
49 bool record_mode = chrome::kRecordModeEnabled &&
50 command_line.HasSwitch(switches::kRecordMode);
51 bool playback_mode = command_line.HasSwitch(switches::kPlaybackMode);
52
53 if (record_mode || playback_mode) {
54 // Don't use existing cookies and use an in-memory store.
55 context->cookie_store_ = new net::CookieMonster();
56 cache->set_mode(
57 record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
58 }
59 context->http_transaction_factory_ = cache;
60
61 // setup cookie store
62 if (!context->cookie_store_) {
63 DCHECK(!cookie_store_path.empty());
64 context->cookie_db_.reset(new SQLitePersistentCookieStore(
65 cookie_store_path, g_browser_process->db_thread()->message_loop()));
66 context->cookie_store_ = new net::CookieMonster(context->cookie_db_.get());
67 }
68
69 return context;
70 }
71
72 // static
73 ChromeURLRequestContext* ChromeURLRequestContext::CreateOffTheRecord(
74 Profile* profile) {
75 DCHECK(profile->IsOffTheRecord());
76 ChromeURLRequestContext* context = new ChromeURLRequestContext(profile);
77
78 // Share the same proxy service as the original profile. This proxy
79 // service's lifespan is dependent on the lifespan of the original profile,
80 // which we reference (see above).
81 context->proxy_service_ =
82 profile->GetOriginalProfile()->GetRequestContext()->proxy_service();
83
84 context->http_transaction_factory_ =
85 new net::HttpCache(context->proxy_service_, 0);
86 context->cookie_store_ = new net::CookieMonster;
87
88 return context;
89 }
90
91 ChromeURLRequestContext::ChromeURLRequestContext(Profile* profile)
92 : prefs_(profile->GetPrefs()),
93 is_off_the_record_(profile->IsOffTheRecord()) {
94 user_agent_ = webkit_glue::GetUserAgent();
95
96 // set up Accept-Language and Accept-Charset header values
97 // TODO(jungshik) : This may slow down http requests. Perhaps,
98 // we have to come up with a better way to set up these values.
99 accept_language_ = WideToASCII(prefs_->GetString(prefs::kAcceptLanguages));
100 accept_charset_ = WideToASCII(prefs_->GetString(prefs::kDefaultCharset));
101 accept_charset_ += ",*,utf-8";
102
103 cookie_policy_.SetType(net::CookiePolicy::FromInt(
104 prefs_->GetInteger(prefs::kCookieBehavior)));
105
106 prefs_->AddPrefObserver(prefs::kAcceptLanguages, this);
107 prefs_->AddPrefObserver(prefs::kCookieBehavior, this);
108 }
109
110 // NotificationObserver implementation.
111 void ChromeURLRequestContext::Observe(NotificationType type,
112 const NotificationSource& source,
113 const NotificationDetails& details) {
114 if (NOTIFY_PREF_CHANGED == type) {
115 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
116 PrefService* prefs = Source<PrefService>(source).ptr();
117 DCHECK(pref_name_in && prefs);
118 if (*pref_name_in == prefs::kAcceptLanguages) {
119 std::string accept_language =
120 WideToASCII(prefs->GetString(prefs::kAcceptLanguages));
121 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
122 NewRunnableMethod(this,
123 &ChromeURLRequestContext::OnAcceptLanguageChange,
124 accept_language));
125 } else if (*pref_name_in == prefs::kCookieBehavior) {
126 net::CookiePolicy::Type type = net::CookiePolicy::FromInt(
127 prefs_->GetInteger(prefs::kCookieBehavior));
128 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
129 NewRunnableMethod(this,
130 &ChromeURLRequestContext::OnCookiePolicyChange,
131 type));
132 }
133 } else {
134 NOTREACHED();
135 }
136 }
137
138 void ChromeURLRequestContext::CleanupOnUIThread() {
139 // Unregister for pref notifications.
140 prefs_->RemovePrefObserver(prefs::kAcceptLanguages, this);
141 prefs_->RemovePrefObserver(prefs::kCookieBehavior, this);
142 prefs_ = NULL;
143 }
144
145 void ChromeURLRequestContext::OnAcceptLanguageChange(std::string accept_language ) {
146 DCHECK(MessageLoop::current() ==
147 ChromeThread::GetMessageLoop(ChromeThread::IO));
148 accept_language_ = accept_language;
149 }
150
151 void ChromeURLRequestContext::OnCookiePolicyChange(net::CookiePolicy::Type type) {
152 DCHECK(MessageLoop::current() ==
153 ChromeThread::GetMessageLoop(ChromeThread::IO));
154 cookie_policy_.SetType(type);
155 }
156
157 ChromeURLRequestContext::~ChromeURLRequestContext() {
158 DCHECK(NULL == prefs_);
159
160 NotificationService::current()->Notify(NOTIFY_URL_REQUEST_CONTEXT_RELEASED,
161 Source<URLRequestContext>(this),
162 NotificationService::NoDetails());
163
164 delete cookie_store_;
165 delete http_transaction_factory_;
166
167 // Do not delete the proxy service in the case of OTR, as it is owned by the
168 // original URLRequestContext.
169 if (!is_off_the_record_)
170 delete proxy_service_;
171 }
OLDNEW
« no previous file with comments | « chrome/browser/net/chrome_url_request_context.h ('k') | chrome/browser/profile.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698