Chromium Code Reviews| 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_service.h" | 5 #include "chrome/browser/instant/instant_service.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | |
| 7 #include "chrome/browser/instant/instant_io_context.h" | 8 #include "chrome/browser/instant/instant_io_context.h" |
| 9 #include "chrome/browser/instant/instant_service_factory.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 11 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| 10 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
| 11 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/notification_service.h" | 14 #include "content/public/browser/notification_service.h" |
| 13 #include "content/public/browser/notification_types.h" | 15 #include "content/public/browser/notification_types.h" |
| 14 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
| 15 #include "content/public/browser/url_data_source.h" | 17 #include "content/public/browser/url_data_source.h" |
| 18 #include "googleurl/src/gurl.h" | |
| 16 | 19 |
| 17 using content::BrowserThread; | 20 using content::BrowserThread; |
| 18 | 21 |
| 19 InstantService::InstantService(Profile* profile) | 22 InstantService::InstantService(Profile* profile) |
| 20 : profile_(profile) { | 23 : profile_(profile), |
| 24 last_url_id_(0) { | |
| 21 // Stub for unit tests. | 25 // Stub for unit tests. |
| 22 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 26 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
| 23 return; | 27 return; |
| 24 | 28 |
| 25 registrar_.Add(this, | 29 registrar_.Add(this, |
| 26 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 30 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 27 content::NotificationService::AllSources()); | 31 content::NotificationService::AllSources()); |
| 28 | 32 |
| 29 instant_io_context_ = new InstantIOContext(); | 33 instant_io_context_ = new InstantIOContext(); |
| 30 | 34 |
| 31 if (profile_ && profile_->GetResourceContext()) { | 35 if (profile_ && profile_->GetResourceContext()) { |
| 32 BrowserThread::PostTask( | 36 BrowserThread::PostTask( |
| 33 BrowserThread::IO, FROM_HERE, | 37 BrowserThread::IO, FROM_HERE, |
| 34 base::Bind(&InstantIOContext::SetUserDataOnIO, | 38 base::Bind(&InstantIOContext::SetUserDataOnIO, |
| 35 profile->GetResourceContext(), instant_io_context_)); | 39 profile->GetResourceContext(), instant_io_context_)); |
| 36 } | 40 } |
| 37 | 41 |
| 38 content::URLDataSource::Add(profile, new ThumbnailSource(profile)); | 42 content::URLDataSource::Add(profile, new ThumbnailSource(profile)); |
| 39 } | 43 } |
| 40 | 44 |
| 41 InstantService::~InstantService() { | 45 InstantService::~InstantService() { |
| 42 } | 46 } |
| 43 | 47 |
| 48 // static | |
| 49 const std::string InstantService::MaybeTranslateInstantPath( | |
| 50 Profile* profile, const std::string& path) { | |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 52 InstantService* instant_service = | |
| 53 InstantServiceFactory::GetForProfile(profile); | |
|
palmer
2013/03/11 20:42:31
Seems like you can make |profile| const?
dhollowa
2013/03/11 23:27:59
Difficult.
| |
| 54 if (!instant_service) | |
| 55 return path; | |
| 56 | |
| 57 uint64 restricted_id = 0; | |
| 58 if (base::StringToUint64(path, &restricted_id)) { | |
|
palmer
2013/03/11 20:42:31
I see. You should add this expectation to the docu
dhollowa
2013/03/11 23:27:59
Done.
| |
| 59 GURL url; | |
| 60 if (instant_service->GetURLForRestrictedId(restricted_id, &url)) | |
| 61 return url.spec(); | |
| 62 } | |
| 63 return path; | |
| 64 } | |
| 65 | |
| 66 // static | |
| 67 bool InstantService::IsInstantPath(const GURL& url) { | |
| 68 // Strip leading slash. | |
| 69 std::string path = url.path().substr(1); | |
| 70 | |
| 71 // Check that path is of restricted id form. | |
| 72 uint64 dummy = 0; | |
| 73 return base::StringToUint64(path, &dummy); | |
| 74 } | |
| 75 | |
| 44 void InstantService::AddInstantProcess(int process_id) { | 76 void InstantService::AddInstantProcess(int process_id) { |
| 45 process_ids_.insert(process_id); | 77 process_ids_.insert(process_id); |
| 46 | 78 |
| 47 if (instant_io_context_) { | 79 if (instant_io_context_) { |
| 48 BrowserThread::PostTask( | 80 BrowserThread::PostTask( |
| 49 BrowserThread::IO, FROM_HERE, | 81 BrowserThread::IO, FROM_HERE, |
| 50 base::Bind(&InstantIOContext::AddInstantProcessOnIO, | 82 base::Bind(&InstantIOContext::AddInstantProcessOnIO, |
| 51 instant_io_context_, process_id)); | 83 instant_io_context_, process_id)); |
| 52 } | 84 } |
| 53 } | 85 } |
| 54 | 86 |
| 55 bool InstantService::IsInstantProcess(int process_id) const { | 87 bool InstantService::IsInstantProcess(int process_id) const { |
| 56 return process_ids_.find(process_id) != process_ids_.end(); | 88 return process_ids_.find(process_id) != process_ids_.end(); |
| 57 } | 89 } |
| 58 | 90 |
| 91 uint64 InstantService::AddURL(const GURL& url) { | |
| 92 uint64 id = 0; | |
| 93 if (GetRestrictedIDForURL(url, &id)) | |
| 94 return id; | |
| 95 | |
| 96 last_url_id_++; | |
|
palmer
2013/03/11 20:42:31
Since incrementation is your ID creation disciplin
dhollowa
2013/03/11 23:27:59
It is a time/space trade-off yes. I'd prefer to k
palmer
2013/03/11 23:47:18
I'm only suggesting using a vector for id_to_url_m
| |
| 97 id_to_url_map_.insert(std::make_pair(last_url_id_, url)); | |
|
palmer
2013/03/11 20:42:31
Why not use the usual map[key] = value syntax?
dhollowa
2013/03/11 23:27:59
Done. Much better, thanks.
| |
| 98 url_to_id_map_.insert(std::make_pair(url, last_url_id_)); | |
| 99 | |
| 100 if (instant_io_context_) { | |
| 101 BrowserThread::PostTask( | |
| 102 BrowserThread::IO, FROM_HERE, | |
| 103 base::Bind(&InstantIOContext::AddRestrictedIDOnIO, | |
| 104 instant_io_context_, last_url_id_, url)); | |
| 105 } | |
| 106 | |
| 107 return last_url_id_; | |
| 108 } | |
| 109 | |
| 110 bool InstantService::GetRestrictedIDForURL(const GURL& url, | |
| 111 uint64 *restricted_id) { | |
| 112 std::map<GURL, uint64>::iterator it = url_to_id_map_.find(url); | |
| 113 if (it != url_to_id_map_.end()) { | |
| 114 *restricted_id = it->second; | |
| 115 return true; | |
| 116 } | |
| 117 *restricted_id = 0; | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 bool InstantService::GetURLForRestrictedId(uint64 restricted_id, GURL* url) { | |
| 122 std::map<uint64, GURL>::iterator it = id_to_url_map_.find(restricted_id); | |
| 123 if (it != id_to_url_map_.end()) { | |
| 124 *url = it->second; | |
| 125 return true; | |
| 126 } | |
| 127 *url = GURL(); | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 59 void InstantService::Shutdown() { | 131 void InstantService::Shutdown() { |
| 60 process_ids_.clear(); | 132 process_ids_.clear(); |
| 61 | 133 |
| 62 if (instant_io_context_) { | 134 if (instant_io_context_) { |
| 63 BrowserThread::PostTask( | 135 BrowserThread::PostTask( |
| 64 BrowserThread::IO, FROM_HERE, | 136 BrowserThread::IO, FROM_HERE, |
| 65 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 137 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
| 66 instant_io_context_)); | 138 instant_io_context_)); |
| 67 } | 139 } |
| 68 instant_io_context_ = NULL; | 140 instant_io_context_ = NULL; |
| 69 } | 141 } |
| 70 | 142 |
| 71 void InstantService::Observe(int type, | 143 void InstantService::Observe(int type, |
| 72 const content::NotificationSource& source, | 144 const content::NotificationSource& source, |
| 73 const content::NotificationDetails& details) { | 145 const content::NotificationDetails& details) { |
| 74 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | 146 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
| 75 int process_id = content::Source<content::RenderProcessHost>(source)->GetID(); | 147 int process_id = content::Source<content::RenderProcessHost>(source)->GetID(); |
| 76 process_ids_.erase(process_id); | 148 process_ids_.erase(process_id); |
| 77 | 149 |
| 78 if (instant_io_context_) { | 150 if (instant_io_context_) { |
| 79 BrowserThread::PostTask( | 151 BrowserThread::PostTask( |
| 80 BrowserThread::IO, FROM_HERE, | 152 BrowserThread::IO, FROM_HERE, |
| 81 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, | 153 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, |
| 82 instant_io_context_, process_id)); | 154 instant_io_context_, process_id)); |
| 83 } | 155 } |
| 84 } | 156 } |
| OLD | NEW |