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.
4 
5 #include "ios/web/public/test/fakes/test_browser_state.h"
6 
7 #include "base/files/file_path.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/task/post_task.h"
10 #include "ios/web/public/thread/web_task_traits.h"
11 #include "ios/web/public/thread/web_thread.h"
12 #include "ios/web/test/test_url_constants.h"
13 #include "ios/web/webui/url_data_manager_ios_backend.h"
14 #include "net/url_request/url_request_context.h"
15 #include "net/url_request/url_request_context_getter.h"
16 #include "net/url_request/url_request_job_factory.h"
17 #include "net/url_request/url_request_test_util.h"
18 
19 namespace web {
20 
21 namespace {
22 
23 class TestContextURLRequestContextGetter : public net::URLRequestContextGetter {
24  public:
TestContextURLRequestContextGetter(web::BrowserState * browser_state)25   TestContextURLRequestContextGetter(web::BrowserState* browser_state) {
26     job_factory_.SetProtocolHandler(
27         kTestWebUIScheme,
28         web::URLDataManagerIOSBackend::CreateProtocolHandler(browser_state));
29     context_.set_job_factory(&job_factory_);
30   }
31 
GetURLRequestContext()32   net::URLRequestContext* GetURLRequestContext() override { return &context_; }
33 
GetNetworkTaskRunner() const34   scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
35       const override {
36     return base::CreateSingleThreadTaskRunner({web::WebThread::IO});
37   }
38 
39  private:
~TestContextURLRequestContextGetter()40   ~TestContextURLRequestContextGetter() override {}
41 
42   net::TestURLRequestContext context_;
43   net::URLRequestJobFactory job_factory_;
44 };
45 
46 }  // namespace
47 
48 // static
49 const char TestBrowserState::kCorsExemptTestHeaderName[] = "ExemptTest";
50 
TestBrowserState()51 TestBrowserState::TestBrowserState() : is_off_the_record_(false) {}
52 
~TestBrowserState()53 TestBrowserState::~TestBrowserState() {}
54 
IsOffTheRecord() const55 bool TestBrowserState::IsOffTheRecord() const {
56   return is_off_the_record_;
57 }
58 
GetStatePath() const59 base::FilePath TestBrowserState::GetStatePath() const {
60   return base::FilePath();
61 }
62 
GetRequestContext()63 net::URLRequestContextGetter* TestBrowserState::GetRequestContext() {
64   if (!request_context_)
65     request_context_ = new TestContextURLRequestContextGetter(this);
66   return request_context_.get();
67 }
68 
UpdateCorsExemptHeader(network::mojom::NetworkContextParams * params)69 void TestBrowserState::UpdateCorsExemptHeader(
70     network::mojom::NetworkContextParams* params) {
71   DCHECK(params);
72   params->cors_exempt_header_list.push_back(kCorsExemptTestHeaderName);
73 }
74 
SetOffTheRecord(bool flag)75 void TestBrowserState::SetOffTheRecord(bool flag) {
76   is_off_the_record_ = flag;
77 }
78 
79 scoped_refptr<network::SharedURLLoaderFactory>
GetSharedURLLoaderFactory()80 TestBrowserState::GetSharedURLLoaderFactory() {
81   return test_shared_url_loader_factory_
82              ? test_shared_url_loader_factory_
83              : BrowserState::GetSharedURLLoaderFactory();
84 }
85 
SetSharedURLLoaderFactory(scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory)86 void TestBrowserState::SetSharedURLLoaderFactory(
87     scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory) {
88   test_shared_url_loader_factory_ = std::move(shared_url_loader_factory);
89 }
90 
91 }  // namespace web
92