Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Side by Side Diff: content/browser/download/mhtml_generation_manager.cc

Issue 8674002: Switch MHTMLGenerationManager to use a Callback. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "content/browser/download/mhtml_generation_manager.h" 5 #include "content/browser/download/mhtml_generation_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/platform_file.h" 8 #include "base/platform_file.h"
9 #include "content/browser/renderer_host/render_process_host_impl.h" 9 #include "content/browser/renderer_host/render_process_host_impl.h"
10 #include "content/browser/renderer_host/render_view_host.h" 10 #include "content/browser/renderer_host/render_view_host.h"
11 #include "content/browser/tab_contents/tab_contents.h" 11 #include "content/browser/tab_contents/tab_contents.h"
12 #include "content/public/browser/notification_service.h" 12 #include "content/public/browser/notification_service.h"
13 #include "content/common/view_messages.h" 13 #include "content/common/view_messages.h"
14 #include "content/public/browser/notification_types.h" 14 #include "content/public/browser/notification_types.h"
15 15
16 using content::BrowserThread; 16 using content::BrowserThread;
17 17
18 MHTMLGenerationManager::Job::Job() 18 MHTMLGenerationManager::Job::Job()
19 : browser_file(base::kInvalidPlatformFileValue), 19 : browser_file(base::kInvalidPlatformFileValue),
20 renderer_file(IPC::InvalidPlatformFileForTransit()), 20 renderer_file(IPC::InvalidPlatformFileForTransit()),
21 process_id(-1), 21 process_id(-1),
22 routing_id(-1) { 22 routing_id(-1) {
23 } 23 }
24 24
25 MHTMLGenerationManager::Job::~Job() {
26 }
27
25 MHTMLGenerationManager::MHTMLGenerationManager() { 28 MHTMLGenerationManager::MHTMLGenerationManager() {
26 } 29 }
27 30
28 MHTMLGenerationManager::~MHTMLGenerationManager() { 31 MHTMLGenerationManager::~MHTMLGenerationManager() {
29 } 32 }
30 33
31 void MHTMLGenerationManager::GenerateMHTML(TabContents* tab_contents, 34 void MHTMLGenerationManager::GenerateMHTML(TabContents* tab_contents,
32 const FilePath& file) { 35 const FilePath& file,
36 const GenerateMHTMLCallback& callback) {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
34 static int id_counter = 0; 38 static int id_counter = 0;
35 39
36 int job_id = id_counter++; 40 int job_id = id_counter++;
37 Job job; 41 Job job;
38 job.file_path = file; 42 job.file_path = file;
39 job.process_id = tab_contents->GetRenderProcessHost()->GetID(); 43 job.process_id = tab_contents->GetRenderProcessHost()->GetID();
40 job.routing_id = tab_contents->render_view_host()->routing_id(); 44 job.routing_id = tab_contents->render_view_host()->routing_id();
45 job.callback = callback;
41 id_to_job_[job_id] = job; 46 id_to_job_[job_id] = job;
42 47
43 base::ProcessHandle renderer_process = 48 base::ProcessHandle renderer_process =
44 tab_contents->GetRenderProcessHost()->GetHandle(); 49 tab_contents->GetRenderProcessHost()->GetHandle();
45 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 50 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
46 base::Bind(&MHTMLGenerationManager::CreateFile, this, 51 base::Bind(&MHTMLGenerationManager::CreateFile, this,
47 job_id, file, renderer_process)); 52 job_id, file, renderer_process));
48 } 53 }
49 54
50 void MHTMLGenerationManager::MHTMLGenerated(int job_id, int64 mhtml_data_size) { 55 void MHTMLGenerationManager::MHTMLGenerated(int job_id, int64 mhtml_data_size) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 } 107 }
103 108
104 void MHTMLGenerationManager::JobFinished(int job_id, int64 file_size) { 109 void MHTMLGenerationManager::JobFinished(int job_id, int64 file_size) {
105 IDToJobMap::iterator iter = id_to_job_.find(job_id); 110 IDToJobMap::iterator iter = id_to_job_.find(job_id);
106 if (iter == id_to_job_.end()) { 111 if (iter == id_to_job_.end()) {
107 NOTREACHED(); 112 NOTREACHED();
108 return; 113 return;
109 } 114 }
110 115
111 Job& job = iter->second; 116 Job& job = iter->second;
112 117 job.callback.Run(job.file_path, file_size);
113 RenderViewHost* rvh = RenderViewHost::FromID(job.process_id, job.routing_id);
114 if (rvh) {
115 NotificationDetails details;
116 details.file_path = job.file_path;
117 details.file_size = file_size;
118
119 content::NotificationService::current()->Notify(
120 content::NOTIFICATION_MHTML_GENERATED,
121 content::Source<RenderViewHost>(rvh),
122 content::Details<NotificationDetails>(&details));
123 }
124 118
125 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 119 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
126 base::Bind(&MHTMLGenerationManager::CloseFile, this, job.browser_file)); 120 base::Bind(&MHTMLGenerationManager::CloseFile, this, job.browser_file));
127 121
128 id_to_job_.erase(job_id); 122 id_to_job_.erase(job_id);
129 } 123 }
130 124
131 void MHTMLGenerationManager::CloseFile(base::PlatformFile file) { 125 void MHTMLGenerationManager::CloseFile(base::PlatformFile file) {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
133 base::ClosePlatformFile(file); 127 base::ClosePlatformFile(file);
134 } 128 }
OLDNEW
« no previous file with comments | « content/browser/download/mhtml_generation_manager.h ('k') | content/public/browser/notification_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698