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

Side by Side Diff: shell/application_manager/fetcher.cc

Issue 1276073004: Offline By Default (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Follow review Created 5 years, 3 months 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "shell/application_manager/fetcher.h" 5 #include "shell/application_manager/fetcher.h"
6 6
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
7 #include "url/gurl.h" 9 #include "url/gurl.h"
8 10
9 namespace shell { 11 namespace shell {
10 12
11 const char Fetcher::kMojoMagic[] = "#!mojo "; 13 namespace {
12 const size_t Fetcher::kMaxShebangLength = 2048; 14 const char kMojoMagic[] = "#!mojo ";
15 const size_t kMaxShebangLength = 2048;
16 }
13 17
14 Fetcher::Fetcher(const FetchCallback& loader_callback) 18 Fetcher::Fetcher(const FetchCallback& loader_callback)
15 : loader_callback_(loader_callback) { 19 : loader_callback_(loader_callback) {
16 } 20 }
17 21
18 Fetcher::~Fetcher() { 22 Fetcher::~Fetcher() {
19 } 23 }
20 24
21 bool Fetcher::PeekContentHandler(std::string* mojo_shebang, 25 bool Fetcher::PeekContentHandler(std::string* mojo_shebang,
22 GURL* mojo_content_handler_url) { 26 GURL* mojo_content_handler_url) {
23 // TODO(aa): I guess this should just go in ApplicationManager now. 27 // TODO(aa): I guess this should just go in ApplicationManager now.
24 std::string shebang; 28 std::string shebang;
25 if (HasMojoMagic() && PeekFirstLine(&shebang)) { 29 if (HasMojoMagic() && PeekFirstLine(&shebang)) {
26 GURL url(shebang.substr(arraysize(kMojoMagic) - 1, std::string::npos)); 30 GURL url(shebang.substr(arraysize(kMojoMagic) - 1, std::string::npos));
27 if (url.is_valid()) { 31 if (url.is_valid()) {
28 *mojo_shebang = shebang; 32 *mojo_shebang = shebang;
29 *mojo_content_handler_url = url; 33 *mojo_content_handler_url = url;
30 return true; 34 return true;
31 } 35 }
32 } 36 }
33 return false; 37 return false;
34 } 38 }
35 39
40 // static
41 bool Fetcher::HasMojoMagic(const base::FilePath& path) {
42 DCHECK(!path.empty());
43 std::string magic;
44 ReadFileToString(path, &magic, strlen(kMojoMagic));
45 return magic == kMojoMagic;
46 }
47
48 // static
49 bool Fetcher::PeekFirstLine(const base::FilePath& path, std::string* line) {
50 DCHECK(!path.empty());
51 std::string start_of_file;
52 ReadFileToString(path, &start_of_file, kMaxShebangLength);
53 size_t return_position = start_of_file.find('\n');
54 if (return_position == std::string::npos)
55 return false;
56 *line = start_of_file.substr(0, return_position + 1);
57 return true;
58 }
59
36 } // namespace shell 60 } // namespace shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698