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