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