Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/page_capture/page_capture_api.h" | 5 #include "chrome/browser/extensions/api/page_capture/page_capture_api.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | |
| 11 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/memory/ptr_util.h" | |
| 12 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/extensions/extension_tab_util.h" | 15 #include "chrome/browser/extensions/extension_tab_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profiles_state.h" | |
| 15 #include "content/public/browser/child_process_security_policy.h" | 18 #include "content/public/browser/child_process_security_policy.h" |
| 16 #include "content/public/browser/notification_details.h" | 19 #include "content/public/browser/notification_details.h" |
| 17 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
| 18 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/mhtml_generation_params.h" | 25 #include "content/public/common/mhtml_generation_params.h" |
| 23 #include "extensions/common/extension_messages.h" | 26 #include "extensions/common/extension_messages.h" |
| 24 | 27 |
| 28 #if defined (OS_CHROMEOS) | |
| 29 #include "chrome/browser/chromeos/extensions/public_session_permission_helper.h" | |
| 30 #endif | |
| 31 | |
| 25 using content::BrowserThread; | 32 using content::BrowserThread; |
| 26 using content::ChildProcessSecurityPolicy; | 33 using content::ChildProcessSecurityPolicy; |
| 27 using content::WebContents; | 34 using content::WebContents; |
| 28 using extensions::PageCaptureSaveAsMHTMLFunction; | 35 using extensions::PageCaptureSaveAsMHTMLFunction; |
| 29 using storage::ShareableFileReference; | 36 using storage::ShareableFileReference; |
| 30 | 37 |
| 31 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; | 38 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; |
| 32 | 39 |
| 33 namespace { | 40 namespace { |
| 34 | 41 |
| 35 const char kFileTooBigError[] = "The MHTML file generated is too big."; | 42 const char kFileTooBigError[] = "The MHTML file generated is too big."; |
| 36 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; | 43 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; |
| 37 const char kTemporaryFileError[] = "Failed to create a temporary file."; | 44 const char kTemporaryFileError[] = "Failed to create a temporary file."; |
| 38 const char kTabClosedError[] = "Cannot find the tab for this request."; | 45 const char kTabClosedError[] = "Cannot find the tab for this request."; |
| 46 #if defined(OS_CHROMEOS) | |
| 47 const char kUserDenied[] = "User denied request."; | |
| 48 #endif | |
| 39 | 49 |
| 40 } // namespace | 50 } // namespace |
| 41 | 51 |
| 42 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; | 52 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; |
| 43 | 53 |
| 44 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { | 54 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { |
| 45 } | 55 } |
| 46 | 56 |
| 47 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { | 57 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { |
| 48 if (mhtml_file_.get()) { | 58 if (mhtml_file_.get()) { |
| 49 storage::ShareableFileReference* to_release = mhtml_file_.get(); | 59 storage::ShareableFileReference* to_release = mhtml_file_.get(); |
| 50 to_release->AddRef(); | 60 to_release->AddRef(); |
| 51 mhtml_file_ = NULL; | 61 mhtml_file_ = NULL; |
| 52 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, to_release); | 62 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, to_release); |
| 53 } | 63 } |
| 54 } | 64 } |
| 55 | 65 |
| 56 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { | 66 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { |
| 57 test_delegate_ = delegate; | 67 test_delegate_ = delegate; |
| 58 } | 68 } |
| 59 | 69 |
| 60 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { | 70 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { |
| 61 params_ = SaveAsMHTML::Params::Create(*args_); | 71 params_ = SaveAsMHTML::Params::Create(*args_); |
| 62 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 72 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 63 | 73 |
| 64 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() | 74 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() |
| 65 | 75 |
| 76 // In Public Sessions, extensions (and apps) are force-installed by admin | |
| 77 // policy so the user does not get a chance to review the permissions for | |
| 78 // these extensions. This is not acceptable from a security/privacy | |
| 79 // standpoint, so when an extension uses the PageCapture API for the first | |
| 80 // time, we show the user a dialog where they can choose whether to allow the | |
| 81 // extension access to the API. | |
| 82 #if defined(OS_CHROMEOS) | |
| 83 if (profiles::IsPublicSession()) { | |
| 84 WebContents* web_contents = GetWebContents(); | |
| 85 if (!web_contents) { | |
| 86 ReturnFailure(kTabClosedError); | |
| 87 return true; | |
| 88 } | |
| 89 auto success_callback = base::Bind( | |
| 90 base::IgnoreResult(&BrowserThread::PostTask), BrowserThread::FILE, | |
| 91 FROM_HERE, | |
| 92 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, | |
| 93 base::Unretained(this))); | |
|
Devlin
2017/01/30 17:04:17
explain why the unretained()s are safe.
Alternati
Ivan Šandrk
2017/01/30 18:14:20
Done.
| |
| 94 auto failure_callback = | |
| 95 base::Bind(&PageCaptureSaveAsMHTMLFunction::ReturnFailure, | |
| 96 base::Unretained(this), kUserDenied); | |
| 97 extensions::PublicSessionPermissionHelper::HandlePermissionRequest( | |
| 98 *extension(), {APIPermission::kPageCapture}, web_contents, | |
| 99 success_callback, failure_callback); | |
| 100 return true; | |
| 101 } | |
| 102 #endif | |
| 103 | |
| 66 BrowserThread::PostTask( | 104 BrowserThread::PostTask( |
| 67 BrowserThread::FILE, FROM_HERE, | 105 BrowserThread::FILE, FROM_HERE, |
| 68 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); | 106 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); |
| 69 return true; | 107 return true; |
| 70 } | 108 } |
| 71 | 109 |
| 72 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( | 110 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( |
| 73 const IPC::Message& message) { | 111 const IPC::Message& message) { |
| 74 if (message.type() != ExtensionHostMsg_ResponseAck::ID) | 112 if (message.type() != ExtensionHostMsg_ResponseAck::ID) |
| 75 return false; | 113 return false; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 GetProfile(), | 231 GetProfile(), |
| 194 include_incognito(), | 232 include_incognito(), |
| 195 &browser, | 233 &browser, |
| 196 NULL, | 234 NULL, |
| 197 &web_contents, | 235 &web_contents, |
| 198 NULL)) { | 236 NULL)) { |
| 199 return NULL; | 237 return NULL; |
| 200 } | 238 } |
| 201 return web_contents; | 239 return web_contents; |
| 202 } | 240 } |
| OLD | NEW |