| OLD | NEW | 
|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "chrome/browser/instant/instant_io_context.h" | 5 #include "chrome/browser/instant/instant_io_context.h" | 
| 6 | 6 | 
| 7 #include "content/public/browser/browser_thread.h" | 7 #include "content/public/browser/browser_thread.h" | 
| 8 #include "content/public/browser/resource_context.h" | 8 #include "content/public/browser/resource_context.h" | 
| 9 #include "content/public/browser/resource_request_info.h" | 9 #include "content/public/browser/resource_request_info.h" | 
|  | 10 #include "googleurl/src/gurl.h" | 
| 10 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" | 
| 11 | 12 | 
| 12 using content::BrowserThread; | 13 using content::BrowserThread; | 
| 13 | 14 | 
| 14 namespace { | 15 namespace { | 
| 15 | 16 | 
| 16 // Retrieves the Instant data from the |context|'s named user-data. | 17 // Retrieves the Instant data from the |context|'s named user-data. | 
| 17 InstantIOContext* GetDataForResourceContext( | 18 InstantIOContext* GetDataForResourceContext( | 
| 18     content::ResourceContext* context) { | 19     content::ResourceContext* context) { | 
| 19   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 20   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 
| 20   return base::UserDataAdapter<InstantIOContext>::Get( | 21   return base::UserDataAdapter<InstantIOContext>::Get( | 
| 21       context, InstantIOContext::kInstantIOContextKeyName); | 22       context, InstantIOContext::kInstantIOContextKeyName); | 
| 22 } | 23 } | 
| 23 | 24 | 
|  | 25 InstantIOContext* GetDataForRequest(const net::URLRequest* request) { | 
|  | 26   const content::ResourceRequestInfo* info = | 
|  | 27       content::ResourceRequestInfo::ForRequest(request); | 
|  | 28   if (!info) | 
|  | 29     return NULL; | 
|  | 30 | 
|  | 31   return GetDataForResourceContext(info->GetContext()); | 
|  | 32 } | 
|  | 33 | 
| 24 }  // namespace | 34 }  // namespace | 
| 25 | 35 | 
| 26 const char InstantIOContext::kInstantIOContextKeyName[] = "instant_io_context"; | 36 const char InstantIOContext::kInstantIOContextKeyName[] = "instant_io_context"; | 
| 27 | 37 | 
| 28 InstantIOContext::InstantIOContext() { | 38 InstantIOContext::InstantIOContext() { | 
| 29   // The InstantIOContext is created on the UI thread but is accessed | 39   // The InstantIOContext is created on the UI thread but is accessed | 
| 30   // on the IO thread. | 40   // on the IO thread. | 
| 31   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 41   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 32 } | 42 } | 
| 33 | 43 | 
| (...skipping 24 matching lines...) Expand all  Loading... | 
| 58 } | 68 } | 
| 59 | 69 | 
| 60 // static | 70 // static | 
| 61 void InstantIOContext::ClearInstantProcessesOnIO( | 71 void InstantIOContext::ClearInstantProcessesOnIO( | 
| 62     scoped_refptr<InstantIOContext> instant_io_context) { | 72     scoped_refptr<InstantIOContext> instant_io_context) { | 
| 63   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 73   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 
| 64   instant_io_context->process_ids_.clear(); | 74   instant_io_context->process_ids_.clear(); | 
| 65 } | 75 } | 
| 66 | 76 | 
| 67 // static | 77 // static | 
|  | 78 void InstantIOContext::AddMostVisitedItemIDOnIO( | 
|  | 79     scoped_refptr<InstantIOContext> instant_io_context, | 
|  | 80     uint64 most_visited_item_id, const GURL& url) { | 
|  | 81   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 
|  | 82   instant_io_context->most_visited_item_id_to_url_map_[most_visited_item_id] = | 
|  | 83       url; | 
|  | 84 } | 
|  | 85 | 
|  | 86 // static | 
|  | 87 void InstantIOContext::DeleteMostVisitedURLsOnIO( | 
|  | 88     scoped_refptr<InstantIOContext> instant_io_context, | 
|  | 89     std::vector<uint64> deleted_ids, bool all_history) { | 
|  | 90   if (all_history) { | 
|  | 91     instant_io_context->most_visited_item_id_to_url_map_.clear(); | 
|  | 92     return; | 
|  | 93   } | 
|  | 94 | 
|  | 95   for (size_t i = 0; i < deleted_ids.size(); ++i) { | 
|  | 96     instant_io_context->most_visited_item_id_to_url_map_.erase( | 
|  | 97         instant_io_context->most_visited_item_id_to_url_map_.find( | 
|  | 98             deleted_ids[i])); | 
|  | 99   } | 
|  | 100 } | 
|  | 101 | 
|  | 102 // static | 
| 68 bool InstantIOContext::ShouldServiceRequest(const net::URLRequest* request) { | 103 bool InstantIOContext::ShouldServiceRequest(const net::URLRequest* request) { | 
| 69   const content::ResourceRequestInfo* info = | 104   const content::ResourceRequestInfo* info = | 
| 70       content::ResourceRequestInfo::ForRequest(request); | 105       content::ResourceRequestInfo::ForRequest(request); | 
| 71   if (!info) | 106   if (!info) | 
| 72     return false; | 107     return false; | 
| 73 | 108 | 
| 74   InstantIOContext* instant_io_context = | 109   InstantIOContext* instant_io_context = GetDataForRequest(request); | 
| 75       GetDataForResourceContext(info->GetContext()); |  | 
| 76   if (!instant_io_context) | 110   if (!instant_io_context) | 
| 77     return false; | 111     return false; | 
| 78 | 112 | 
| 79   int process_id = -1; | 113   int process_id = -1; | 
| 80   int render_view_id = -1; | 114   int render_view_id = -1; | 
| 81   if (info->GetAssociatedRenderView(&process_id, &render_view_id) && | 115   if (info->GetAssociatedRenderView(&process_id, &render_view_id) && | 
| 82       instant_io_context->IsInstantProcess(process_id)) | 116       instant_io_context->IsInstantProcess(process_id)) | 
| 83     return true; | 117     return true; | 
| 84   return false; | 118   return false; | 
| 85 } | 119 } | 
| 86 | 120 | 
|  | 121 // static | 
|  | 122 bool InstantIOContext::GetURLForMostVisitedItemId( | 
|  | 123     const net::URLRequest* request, | 
|  | 124     uint64 most_visited_item_id, | 
|  | 125     GURL* url) { | 
|  | 126   InstantIOContext* instant_io_context = GetDataForRequest(request); | 
|  | 127   if (!instant_io_context) { | 
|  | 128     *url = GURL(); | 
|  | 129     return false; | 
|  | 130   } | 
|  | 131 | 
|  | 132   return instant_io_context->GetURLForMostVisitedItemId(most_visited_item_id, | 
|  | 133                                                         url); | 
|  | 134 } | 
|  | 135 | 
| 87 bool InstantIOContext::IsInstantProcess(int process_id) const { | 136 bool InstantIOContext::IsInstantProcess(int process_id) const { | 
| 88   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 137   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 
| 89   return process_ids_.count(process_id) != 0; | 138   return process_ids_.count(process_id) != 0; | 
| 90 } | 139 } | 
|  | 140 | 
|  | 141 bool InstantIOContext::GetURLForMostVisitedItemId(uint64 most_visited_item_id, | 
|  | 142                                                   GURL* url) { | 
|  | 143   std::map<uint64, GURL>::iterator it = | 
|  | 144       most_visited_item_id_to_url_map_.find(most_visited_item_id); | 
|  | 145   if (it != most_visited_item_id_to_url_map_.end()) { | 
|  | 146     *url = it->second; | 
|  | 147     return true; | 
|  | 148   } | 
|  | 149   *url = GURL(); | 
|  | 150   return false; | 
|  | 151 } | 
| OLD | NEW | 
|---|