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

Side by Side Diff: ios/chrome/browser/ui/chrome_web_view_factory.mm

Issue 2588713002: Upstream Chrome on iOS source code [4/11]. (Closed)
Patch Set: Created 4 years 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
OLDNEW
(Empty)
1 // Copyright 2012 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 #import "ios/chrome/browser/ui/chrome_web_view_factory.h"
6
7 #include "base/base64.h"
8 #include "base/logging.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
11 #include "ios/web/net/request_group_util.h"
12 #include "ios/web/net/request_tracker_impl.h"
13 #include "ios/web/public/web_thread.h"
14 #include "net/cookies/cookie_store.h"
15 #include "net/url_request/url_request_context.h"
16 #include "net/url_request/url_request_context_getter.h"
17
18 NSString* const kExternalUserAgent = @"UIWebViewForExternalContent";
19
20 namespace ChromeWebView {
21 // Shared desktop user agent used to mimic Safari on a mac.
22 NSString* const kDesktopUserAgent =
23 @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) "
24 @"AppleWebKit/600.7.12 (KHTML, like Gecko) "
25 @"Version/8.0.7 "
26 @"Safari/600.7.12";
27
28 NSString* const kExternalRequestGroupID = @"kExternalRequestGroupID";
29
30 // Returns an absolute path where sharing service data is stored (such as
31 // cookies and server-bound certificates).
32 const base::FilePath GetExternalServicePath(
33 ios::ChromeBrowserState* browser_state,
34 IOSWebViewFactoryExternalService service) {
35 const base::FilePath path =
36 browser_state->GetOriginalChromeBrowserState()->GetStatePath();
37 switch (service) {
38 case SSO_AUTHENTICATION:
39 return path.Append("SSOAuthentication");
40 case NUM_SHARING_SERVICES:
41 NOTREACHED();
42 return path.Append("ExternalUnknownService");
43 }
44 }
45 } // namespace ChromeWebView
46
47 namespace {
48 ios::ChromeBrowserState* g_external_browser_state = nullptr;
49 scoped_refptr<web::RequestTrackerImpl> g_request_tracker;
50
51 // Empty callback used by ClearCookiesOnIOThread below.
52 void DoNothing(int n) {}
53
54 // Clears the cookies.
55 void ClearCookiesOnIOThread(net::URLRequestContextGetter* context_getter,
56 base::Time delete_begin,
57 base::Time delete_end) {
58 DCHECK(context_getter);
59 DCHECK_CURRENTLY_ON(web::WebThread::IO);
60 net::CookieStore* cookie_store =
61 context_getter->GetURLRequestContext()->cookie_store();
62 cookie_store->DeleteAllCreatedBetweenAsync(delete_begin, delete_end,
63 base::Bind(&DoNothing));
64 }
65
66 // Registers |user_agent| as the user agent string to be used by the UIWebView
67 // instances that are created from now on.
68 void RegisterUserAgentForUIWebView(NSString* user_agent) {
69 [[NSUserDefaults standardUserDefaults] registerDefaults:@{
70 @"UserAgent" : user_agent,
71 }];
72 }
73
74 } // namespace
75
76 @implementation ChromeWebViewFactory
77
78 + (void)setBrowserStateToUseForExternal:(ios::ChromeBrowserState*)browserState {
79 g_external_browser_state = browserState;
80 }
81
82 + (void)externalSessionFinished {
83 g_external_browser_state = nullptr;
84 if (g_request_tracker.get())
85 g_request_tracker->Close();
86 g_request_tracker = nullptr;
87 }
88
89 + (net::URLRequestContextGetter*)requestContextForExternalService:
90 (IOSWebViewFactoryExternalService)externalService {
91 DCHECK(g_external_browser_state);
92 const base::FilePath servicePath =
93 [ChromeWebViewFactory partitionPathForExternalService:externalService];
94 return g_external_browser_state->CreateIsolatedRequestContext(servicePath);
95 }
96
97 + (base::FilePath)partitionPathForExternalService:
98 (IOSWebViewFactoryExternalService)externalService {
99 return ChromeWebView::GetExternalServicePath(g_external_browser_state,
100 externalService);
101 }
102
103 + (ChromeBrowserStateIOData*)ioDataForExternalWebViews {
104 DCHECK(g_external_browser_state);
105 return g_external_browser_state->GetIOData();
106 }
107
108 + (void)clearExternalCookies:(IOSWebViewFactoryExternalService)externalService
109 browserState:(ios::ChromeBrowserState*)browserState
110 fromTime:(base::Time)deleteBegin
111 toTime:(base::Time)deleteEnd {
112 const base::FilePath servicePath =
113 ChromeWebView::GetExternalServicePath(browserState, externalService);
114 scoped_refptr<net::URLRequestContextGetter> contextGetter =
115 browserState->CreateIsolatedRequestContext(servicePath);
116 [self clearCookiesForContextGetter:contextGetter
117 fromTime:deleteBegin
118 toTime:deleteEnd];
119 }
120
121 + (void)clearCookiesForContextGetter:
122 (scoped_refptr<net::URLRequestContextGetter>)contextGetter
123 fromTime:(base::Time)deleteBegin
124 toTime:(base::Time)deleteEnd {
125 DCHECK(contextGetter.get());
126 web::WebThread::PostTask(
127 web::WebThread::IO, FROM_HERE,
128 base::Bind(&ClearCookiesOnIOThread, base::RetainedRef(contextGetter),
129 deleteBegin, deleteEnd));
130 }
131
132 + (void)clearExternalCookies:(ios::ChromeBrowserState*)browserState
133 fromTime:(base::Time)deleteBegin
134 toTime:(base::Time)deleteEnd {
135 for (unsigned int i = 0; i < NUM_SHARING_SERVICES; ++i) {
136 [ChromeWebViewFactory
137 clearExternalCookies:static_cast<IOSWebViewFactoryExternalService>(i)
138 browserState:browserState
139 fromTime:deleteBegin
140 toTime:deleteEnd];
141 }
142 }
143
144 #pragma mark -
145 #pragma mark ChromeWebViewFactory
146 + (UIWebView*)newExternalWebView:
147 (IOSWebViewFactoryExternalService)externalService {
148 // All UIWebView's created for sharing share the same user agent, as there is
149 // no need for them to be differentiated and this choice ensures that only
150 // one request tracker is created on a per-sharing-session basis.
151 NSString* userAgent = web::AddRequestGroupIDToUserAgent(
152 kExternalUserAgent, ChromeWebView::kExternalRequestGroupID);
153 RegisterUserAgentForUIWebView(userAgent);
154 if (!g_request_tracker.get()) {
155 DCHECK(g_external_browser_state);
156 g_request_tracker = web::RequestTrackerImpl::CreateTrackerForRequestGroupID(
157 ChromeWebView::kExternalRequestGroupID, g_external_browser_state,
158 [self requestContextForExternalService:externalService], nil);
159 }
160 return [[UIWebView alloc] initWithFrame:CGRectZero];
161 }
162
163 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/chrome_web_view_factory.h ('k') | ios/chrome/browser/ui/chrome_web_view_factory_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698