Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ios / web / ui_web_view_util.mm
blob35c634798b7279c0a5779c8ed7f24b230929c74b
1 // Copyright 2014 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.
5 #import "ios/web/ui_web_view_util.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "ios/web/net/request_group_util.h"
11 #include "ios/web/public/web_client.h"
13 namespace {
14 // Returns true if given Request Group ID string contains only decimal digits.
15 BOOL IsValidRequestGroupID(NSString* request_group_id) {
16   return [[NSCharacterSet decimalDigitCharacterSet] isSupersetOfSet:
17       [NSCharacterSet characterSetWithCharactersInString:request_group_id]];
19 }  // namespace
21 namespace web {
23 void BuildAndRegisterUserAgentForUIWebView(NSString* request_group_id,
24                                            BOOL use_desktop_user_agent) {
25   DCHECK(!request_group_id || IsValidRequestGroupID(request_group_id));
26   DCHECK(web::GetWebClient());
27   std::string baseUserAgent = web::GetWebClient()->GetUserAgent(
28       use_desktop_user_agent);
29   web::RegisterUserAgentForUIWebView(
30       web::AddRequestGroupIDToUserAgent(base::SysUTF8ToNSString(baseUserAgent),
31                                         request_group_id));
34 void RegisterUserAgentForUIWebView(NSString* user_agent) {
35   [[NSUserDefaults standardUserDefaults] registerDefaults:@{
36     @"UserAgent" : user_agent
37   }];
40 }  // namespace web