| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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/android/offline_pages/offline_page_request_handler.h" | |
| 6 | |
| 7 #include "components/offline_pages/offline_page_feature.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 #include "net/url_request/url_request.h" | |
| 10 #include "net/url_request/url_request_interceptor.h" | |
| 11 | |
| 12 namespace offline_pages { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 int kUserDataKey; // Only address is used. | |
| 17 | |
| 18 // An interceptor to hijack requests and potentially service them based on | |
| 19 // their offline information. | |
| 20 class OfflinePageRequestInterceptor : public net::URLRequestInterceptor { | |
| 21 public: | |
| 22 explicit OfflinePageRequestInterceptor(void* profile_id) | |
| 23 : profile_id_(profile_id) { | |
| 24 DCHECK(profile_id); | |
| 25 } | |
| 26 ~OfflinePageRequestInterceptor() override {} | |
| 27 | |
| 28 private: | |
| 29 // Overrides from net::URLRequestInterceptor: | |
| 30 net::URLRequestJob* MaybeInterceptRequest( | |
| 31 net::URLRequest* request, | |
| 32 net::NetworkDelegate* network_delegate) const override { | |
| 33 OfflinePageRequestHandler* handler = OfflinePageRequestHandler::GetHandler( | |
| 34 request); | |
| 35 if (!handler) | |
| 36 return nullptr; | |
| 37 return handler->MaybeCreateJob(request, network_delegate, profile_id_); | |
| 38 } | |
| 39 | |
| 40 // The profile for processing offline pages. | |
| 41 void* const profile_id_; | |
| 42 | |
| 43 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestInterceptor); | |
| 44 }; | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 // static | |
| 49 void OfflinePageRequestHandler::InitializeHandler( | |
| 50 net::URLRequest* request, | |
| 51 content::ResourceType resource_type) { | |
| 52 if (!IsOfflinePagesEnabled()) | |
| 53 return; | |
| 54 | |
| 55 // Ignore the requests not for the main resource. | |
| 56 if (resource_type != content::RESOURCE_TYPE_MAIN_FRAME) | |
| 57 return; | |
| 58 | |
| 59 // Ignore non-http/https requests. | |
| 60 if (!request->url().SchemeIsHTTPOrHTTPS()) | |
| 61 return; | |
| 62 | |
| 63 // Ignore requests other than GET. | |
| 64 if (request->method() != "GET") | |
| 65 return; | |
| 66 | |
| 67 // Any more precise checks to see if the request should be intercepted are | |
| 68 // asynchronous, so just create our handler in all cases. | |
| 69 request->SetUserData(&kUserDataKey, new OfflinePageRequestHandler()); | |
| 70 } | |
| 71 | |
| 72 // static | |
| 73 OfflinePageRequestHandler* OfflinePageRequestHandler::GetHandler( | |
| 74 net::URLRequest* request) { | |
| 75 return static_cast<OfflinePageRequestHandler*>( | |
| 76 request->GetUserData(&kUserDataKey)); | |
| 77 } | |
| 78 | |
| 79 // static | |
| 80 std::unique_ptr<net::URLRequestInterceptor> | |
| 81 OfflinePageRequestHandler::CreateInterceptor(void* profile_id) { | |
| 82 if (!IsOfflinePagesEnabled()) | |
| 83 return nullptr; | |
| 84 | |
| 85 return std::unique_ptr<net::URLRequestInterceptor>( | |
| 86 new OfflinePageRequestInterceptor(profile_id)); | |
| 87 } | |
| 88 | |
| 89 OfflinePageRequestHandler::OfflinePageRequestHandler() { | |
| 90 } | |
| 91 | |
| 92 OfflinePageRequestHandler::~OfflinePageRequestHandler() { | |
| 93 } | |
| 94 | |
| 95 net::URLRequestJob* OfflinePageRequestHandler::MaybeCreateJob( | |
| 96 net::URLRequest* request, | |
| 97 net::NetworkDelegate* network_delegate, | |
| 98 void* profile_id) { | |
| 99 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 100 | |
| 101 // TODO(jianli): to be implemented. | |
| 102 return nullptr; | |
| 103 } | |
| 104 | |
| 105 } // namespace offline_pages | |
| OLD | NEW |