| 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 ~OfflinePageRequestInterceptor() override {} |
| 25 |
| 26 private: |
| 27 // Overrides from net::URLRequestInterceptor: |
| 28 net::URLRequestJob* MaybeInterceptRequest( |
| 29 net::URLRequest* request, |
| 30 net::NetworkDelegate* network_delegate) const override { |
| 31 OfflinePageRequestHandler* handler = OfflinePageRequestHandler::GetHandler( |
| 32 request); |
| 33 if (!handler) |
| 34 return nullptr; |
| 35 return handler->MaybeCreateJob(request, network_delegate, profile_id_); |
| 36 } |
| 37 |
| 38 // The profile for processing offline pages. Should not be NULL. |
| 39 void* profile_id_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestInterceptor); |
| 42 }; |
| 43 |
| 44 } // namespace |
| 45 |
| 46 // static |
| 47 void OfflinePageRequestHandler::InitializeHandler( |
| 48 net::URLRequest* request, |
| 49 content::ResourceType resource_type) { |
| 50 if (!IsOfflinePagesEnabled()) |
| 51 return; |
| 52 |
| 53 // Ignore the requests not for the main resource. |
| 54 if (resource_type != content::RESOURCE_TYPE_MAIN_FRAME) |
| 55 return; |
| 56 |
| 57 // Ignore non-http/https requests. |
| 58 if (!request->url().SchemeIsHTTPOrHTTPS()) |
| 59 return; |
| 60 |
| 61 // Ignore requests other than GET. |
| 62 if (request->method() != "GET") |
| 63 return; |
| 64 |
| 65 // Any more precise checks to see if the request should be intercepted are |
| 66 // asynchronous, so just create our handler in all cases. |
| 67 std::unique_ptr<OfflinePageRequestHandler> handler( |
| 68 new OfflinePageRequestHandler()); |
| 69 request->SetUserData(&kUserDataKey, handler.release()); |
| 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 |