OLD | NEW |
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 "chrome/browser/extensions/extension_save_page_api.h" | 5 #include "chrome/browser/extensions/extension_save_page_api.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/file_util.h" | 7 #include "base/file_util.h" |
9 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/extensions/extension_tab_util.h" | 9 #include "chrome/browser/extensions/extension_tab_util.h" |
11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
12 #include "chrome/common/extensions/extension_messages.h" | 11 #include "chrome/common/extensions/extension_messages.h" |
13 #include "content/browser/child_process_security_policy.h" | 12 #include "content/browser/child_process_security_policy.h" |
14 #include "content/browser/renderer_host/render_view_host.h" | 13 #include "content/browser/renderer_host/render_view_host.h" |
15 #include "content/browser/tab_contents/tab_contents.h" | 14 #include "content/browser/tab_contents/tab_contents.h" |
16 #include "content/browser/download/mhtml_generation_manager.h" | 15 #include "content/browser/download/mhtml_generation_manager.h" |
17 #include "content/public/browser/notification_details.h" | 16 #include "content/public/browser/notification_details.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // no longer used. | 98 // no longer used. |
100 mhtml_file_ = webkit_blob::DeletableFileReference::GetOrCreate(mhtml_path_, | 99 mhtml_file_ = webkit_blob::DeletableFileReference::GetOrCreate(mhtml_path_, |
101 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 100 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
102 | 101 |
103 TabContents* tab_contents = GetTabContents(); | 102 TabContents* tab_contents = GetTabContents(); |
104 if (!tab_contents) { | 103 if (!tab_contents) { |
105 ReturnFailure(kTabClosedError); | 104 ReturnFailure(kTabClosedError); |
106 return; | 105 return; |
107 } | 106 } |
108 | 107 |
109 MHTMLGenerationManager::GenerateMHTMLCallback callback = | 108 registrar_.Add( |
110 base::Bind(&SavePageAsMHTMLFunction::MHTMLGenerated, this); | 109 this, content::NOTIFICATION_MHTML_GENERATED, |
111 | 110 content::Source<RenderViewHost>(tab_contents->render_view_host())); |
| 111 // TODO(jcivelli): we should listen for navigation in the tab, tab closed, |
| 112 // renderer died. |
112 g_browser_process->mhtml_generation_manager()->GenerateMHTML( | 113 g_browser_process->mhtml_generation_manager()->GenerateMHTML( |
113 tab_contents, mhtml_path_, callback); | 114 tab_contents, mhtml_path_); |
114 } | 115 } |
115 | 116 |
116 void SavePageAsMHTMLFunction::MHTMLGenerated(const FilePath& file_path, | 117 void SavePageAsMHTMLFunction::Observe( |
117 int64 mhtml_file_size) { | 118 int type, |
118 DCHECK(mhtml_path_ == file_path); | 119 const content::NotificationSource& source, |
119 if (mhtml_file_size <= 0) { | 120 const content::NotificationDetails& details) { |
| 121 DCHECK(type == content::NOTIFICATION_MHTML_GENERATED); |
| 122 |
| 123 const MHTMLGenerationManager::NotificationDetails* save_details = |
| 124 content::Details<MHTMLGenerationManager::NotificationDetails>(details). |
| 125 ptr(); |
| 126 |
| 127 if (mhtml_path_ != save_details->file_path) { |
| 128 // This could happen if there are concurrent MHTML generations going on for |
| 129 // the same tab. |
| 130 LOG(WARNING) << "Received a notification that MHTML was generated but for a" |
| 131 " different file."; |
| 132 return; |
| 133 } |
| 134 |
| 135 registrar_.RemoveAll(); |
| 136 |
| 137 if (save_details->file_size <= 0) { |
120 ReturnFailure(kMHTMLGenerationFailedError); | 138 ReturnFailure(kMHTMLGenerationFailedError); |
121 return; | 139 return; |
122 } | 140 } |
123 | 141 |
124 if (mhtml_file_size > std::numeric_limits<int>::max()) { | 142 if (save_details->file_size > std::numeric_limits<int>::max()) { |
125 ReturnFailure(kFileTooBigError); | 143 ReturnFailure(kFileTooBigError); |
126 return; | 144 return; |
127 } | 145 } |
128 | 146 |
129 ReturnSuccess(mhtml_file_size); | 147 ReturnSuccess(save_details->file_size); |
130 } | 148 } |
131 | 149 |
132 void SavePageAsMHTMLFunction::ReturnFailure(const std::string& error) { | 150 void SavePageAsMHTMLFunction::ReturnFailure(const std::string& error) { |
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
134 | 152 |
135 error_ = error; | 153 error_ = error; |
136 | 154 |
137 SendResponse(false); | 155 SendResponse(false); |
138 | 156 |
139 Release(); // Balanced in Run() | 157 Release(); // Balanced in Run() |
(...skipping 27 matching lines...) Expand all Loading... |
167 TabContents* SavePageAsMHTMLFunction::GetTabContents() { | 185 TabContents* SavePageAsMHTMLFunction::GetTabContents() { |
168 Browser* browser = NULL; | 186 Browser* browser = NULL; |
169 TabContentsWrapper* tab_contents_wrapper = NULL; | 187 TabContentsWrapper* tab_contents_wrapper = NULL; |
170 | 188 |
171 if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), | 189 if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), |
172 &browser, NULL, &tab_contents_wrapper, NULL)) { | 190 &browser, NULL, &tab_contents_wrapper, NULL)) { |
173 return NULL; | 191 return NULL; |
174 } | 192 } |
175 return tab_contents_wrapper->tab_contents(); | 193 return tab_contents_wrapper->tab_contents(); |
176 } | 194 } |
OLD | NEW |